ffi: canonical static access ffi::load (module namespace)
- '::' accesses a static/class member (here the module namespace ffi), '.'/':' sends to an instance. ffi is a module, so ffi::load is the canonical form; the native now accepts the receiver passed by '::'. - example, tests and README use ffi::load; '.load' still works (compat). - 69/69 tests
This commit is contained in:
1 parent
05f31aed68
commit
4dae4ee1cd
4 files changed
+28
-10
No files matched your search
@@ -257,14 +257,18 @@ uma assinatura declarada, usando **libffi** para a chamada ABI-correta
|
||||
(é o mesmo mecanismo por trás do ctypes do Python):
|
||||
|
||||
```lua
|
||||
local m = ffi.load("libm.so.6")
|
||||
local m = ffi::load("libm.so.6")
|
||||
local pow = m:func("pow", "double(double,double)")
|
||||
print(pow(2, 10)) -- 1024
|
||||
|
||||
local c = ffi.load("libc.so.6")
|
||||
local c = ffi::load("libc.so.6")
|
||||
local strlen = c:func("strlen", "long(string)")
|
||||
```
|
||||
|
||||
`ffi` é um módulo (namespace): `::` acessa o membro estático
|
||||
(`ffi::load`), enquanto `:` envia mensagem a uma instância
|
||||
(`m:func(...)`).
|
||||
|
||||
Gramática da assinatura: `RET(ARG,ARG,...)`, com tipos `void`, `bool`,
|
||||
`char`, `int8/uint8`, `int16/uint16`, `int/long/int32/uint32`,
|
||||
`int64/uint64/longlong`, `float`, `double`, `string`, ponteiros.
|
||||
|
||||
+2
-2
@@ -3,14 +3,14 @@
|
||||
-- Like Python's ctypes/CDLL: load a shared object, bind a symbol with a
|
||||
-- signature, and call it. libffi performs the ABI-correct call.
|
||||
|
||||
local m = ffi.load("libm.so.6")
|
||||
local m = ffi::load("libm.so.6")
|
||||
local pow = m:func("pow", "double(double,double)")
|
||||
print("pow(2,10) =", pow(2, 10))
|
||||
|
||||
local sqrt = m:func("sqrt", "double(double)")
|
||||
print("sqrt(144) =", sqrt(144))
|
||||
|
||||
local c = ffi.load("libc.so.6")
|
||||
local c = ffi::load("libc.so.6")
|
||||
local getpid = c:func("getpid", "int()")
|
||||
local pid = getpid()
|
||||
print("getpid > 0:", pid > 0)
|
||||
|
||||
+17
-3
@@ -353,16 +353,30 @@ CAT_VALUE_Value *cat_types_ffi_bind(CAT_VALUE_Value *lib, const char *name,
|
||||
return &f->base;
|
||||
}
|
||||
|
||||
/* `ffi.load(path)` module function. */
|
||||
/* `ffi::load(path)` module function (static access).
|
||||
*
|
||||
* `::` sends the module as the receiver, so with `ffi::load("x")` the
|
||||
* path is args[1]; a plain `load("x")` passes it as args[0]. Accept
|
||||
* both by taking the first TEXT argument. */
|
||||
static int ffi_load_native(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
CAT_VALUE_Value *path = NULL;
|
||||
int i;
|
||||
|
||||
(void)vm;
|
||||
if (nargs < 1) {
|
||||
for (i = 0; i < nargs; i++) {
|
||||
if (args[i] != NULL && args[i]->vtable != NULL &&
|
||||
args[i]->vtable->kind == CAT_VALUE_KIND_TEXT) {
|
||||
path = args[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (path == NULL) {
|
||||
*out = cat_types_nil_get();
|
||||
return 0;
|
||||
}
|
||||
*out = cat_types_ffi_load(cat_types_text_bytes(args[0]));
|
||||
*out = cat_types_ffi_load(cat_types_text_bytes(path));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -3,11 +3,11 @@
|
||||
-- Deterministic output (no getpid): loads, symbol binding, signatures,
|
||||
-- return conversions, and the nil-on-error contract.
|
||||
|
||||
local m = ffi.load("libm.so.6")
|
||||
local c = ffi.load("libc.so.6")
|
||||
local m = ffi::load("libm.so.6")
|
||||
local c = ffi::load("libc.so.6")
|
||||
|
||||
-- load failure / bad library -> nil
|
||||
local bad = ffi.load("no_such_library_xyz.so.999")
|
||||
local bad = ffi::load("no_such_library_xyz.so.999")
|
||||
print("bad load nil:", bad == nil)
|
||||
print("bad load type:", type(bad))
|
||||
|
||||
|
||||
Reference in new issue
Block a user