DirectMyFile / coreutils

Coreutils in Dart
4 stars 3 forks source link

Don't use directly "unsafe_exetension" use "binary_types" instead. #1

Closed mezoni closed 9 years ago

mezoni commented 9 years ago

Don't use directly "unsafe_exetension" use "binary_types" instead. This is harmful to use volatile package "unsafe extension". It does not intended for direct use and never be as such.

Example how to correctly use "binary_types":

static int getErrorNumber() {
  return Unsafe.readIntPtr(0, libc.symbol("errno"));
}
// Only for speed up
static final INT_T = types["int"];  

static int getErrorNumber() {
  return INT_T.extern(dl.symbol("errno")).value;
}

Yes, "binary_types" are slower than "unsafe_exetension" but not so much and they are intended for such usage.

P.S. Possible in the future an "unsafe_exetension" will be unavailable but "binary_types" will remain. Also "binary_types" are type safe (as far as possible).

DirectCodeBot commented 9 years ago

Thanks, was trying to figure that out :)

On Wednesday, March 18, 2015, mezoni notifications@github.com wrote:

Don't use directly "unsafe_exetension" use "binary_types" instead. This is harmful to use volatile package "unsafe extension". It does not intended for direct use and never be as such.

Example how to correctly use "binary_types":

static int getErrorNumber() { return Unsafe.readIntPtr(0, libc.symbol("errno")); }

// Only for speed upstatic final INT_T = types["int"]; static int getErrorNumber() { return INT_T.extern(dl.symbol("errno")).value; }

Yes, "binary_types" are slower than "unsafe_exetension" but not so much and they are intended for such usage.

P.S. Possible in the future an "unsafe_exetension" will be unavailable but "binary_types" will remain. Also "binary_types" are type safe (as far as possible).

— Reply to this email directly or view it on GitHub https://github.com/DirectMyFile/coreutils/issues/1.

mezoni commented 9 years ago

Another way.

// Somewhere
BinaryData errno = types["int"].extern(dl.symbol("errno"));

int getErrorNumber() {
  return errno.value;
}
azenla commented 9 years ago

Ahh nice :)