本文整理汇总了C++中NativeFunction类的典型用法代码示例。如果您正苦于以下问题:C++ NativeFunction类的具体用法?C++ NativeFunction怎么用?C++ NativeFunction使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NativeFunction类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: test_bind_with_unsigned_long
void test_bind_with_unsigned_long() {
String* name = String::create(state, "dummy_unsigned_long");
Array* args = Array::create(state, 1);
args->set(state, 0, Fixnum::from(RBX_FFI_TYPE_ULONG));
Object* ret = Fixnum::from(RBX_FFI_TYPE_ULONG);
NativeFunction *func = NativeFunction::bind(state, Qnil, name, args, ret);
TS_ASSERT(!func->nil_p());
Array* input = Array::create(state, 1);
input->set(state, 0, Fixnum::from(13));
Arguments args_obj(input);
Object* out = func->call(state, args_obj, NULL);
TS_ASSERT(kind_of<Integer>(out));
TS_ASSERT_EQUALS(as<Integer>(out)->to_native(), (native_int)13);
input = Array::create(state, 1);
input->set(state, 0, Integer::from(state, (unsigned long)2147483647));
Arguments args_obj2(input);
out = func->call(state, args_obj2, NULL);
TS_ASSERT(kind_of<Integer>(out));
TS_ASSERT_EQUALS(as<Integer>(out)->to_native(), (native_int)2147483647);
}
示例2: test_bind_with_unsigned_short
void test_bind_with_unsigned_short() {
String* name = String::create(state, "dummy_unsigned_short");
Array* args = Array::create(state, 1);
args->set(state, 0, Fixnum::from(RBX_FFI_TYPE_USHORT));
Object* ret = Fixnum::from(RBX_FFI_TYPE_USHORT);
NativeFunction *func = NativeFunction::bind(state, Qnil, name, args, ret);
TS_ASSERT(!func->nil_p());
Array* input = Array::create(state, 2);
input->set(state, 0, Fixnum::from(0));
Arguments args_obj(input);
Object* out = func->call(state, args_obj, NULL);
TS_ASSERT(kind_of<Fixnum>(out));
input = Array::create(state, 2);
input->set(state, 0, out);
Arguments args_obj2(input);
out = func->call(state, args_obj2, NULL);
TS_ASSERT(kind_of<Fixnum>(out));
TS_ASSERT_EQUALS(out, Fixnum::from(0));
}
示例3: execute
/* Run when a NativeFunction is executed. Executes the related C function.
*/
ExecuteStatus NativeFunction::execute(STATE, Task* task, Message& msg) {
NativeFunction* nfunc = as<NativeFunction>(msg.method);
Object* obj = nfunc->call(state, &msg);
task->push(obj);
return cExecuteContinue;
}
示例4: test_bind_with_void
void test_bind_with_void() {
Pointer* name = Pointer::create(state, (void*)dummy_void);
Array* args = Array::create(state, 0);
Object* ret = Fixnum::from(RBX_FFI_TYPE_VOID);
NativeFunction *func = NativeFunction::generate(state, name, state->symbol("ffi"), args, ret);
TS_ASSERT(!func->nil_p());
Arguments args_obj;
Object* out = func->call(state, args_obj, NULL);
TS_ASSERT_EQUALS(out, Qnil);
}
示例5: test_bind_with_void
void test_bind_with_void() {
String* name = String::create(state, "dummy_void");
Array* args = Array::create(state, 0);
Object* ret = Fixnum::from(RBX_FFI_TYPE_VOID);
NativeFunction *func = NativeFunction::bind(state, Qnil, name, args, ret);
TS_ASSERT(!func->nil_p());
Arguments args_obj;
Object* out = func->call(state, args_obj, NULL);
TS_ASSERT_EQUALS(out, Qnil);
}
示例6: create
NativeFunction* NativeFunction::create(STATE, Object* name, int args) {
NativeFunction* nf = (NativeFunction*)state->new_object(G(native_function));
nf->primitive(state, state->symbol("nativefunction_call"));
nf->required(state, Fixnum::from(args));
nf->serial(state, Fixnum::from(0));
nf->name(state, name);
nf->file(state, state->symbol("<system>"));
nf->data(state, (MemoryPointer*)Qnil);
nf->set_executor(NativeFunction::execute);
return nf;
}
示例7: test_bind_with_string_and_ptr_for_null
void test_bind_with_string_and_ptr_for_null() {
Array* args = Array::create(state, 0);
Object* ret = Fixnum::from(RBX_FFI_TYPE_STRPTR);
String* name = String::create(state, "null_string");
NativeFunction *func = NativeFunction::bind(state, Qnil, name, args, ret);
TS_ASSERT(!func->nil_p());
Arguments args_obj;
Array* out = try_as<Array>(func->call(state, args_obj, NULL));
TS_ASSERT(kind_of<Array>(out));
TS_ASSERT_EQUALS(out->get(state, 0), Qnil);
TS_ASSERT_EQUALS(out->get(state, 1), Qnil);
}
示例8: test_bind_with_string_and_ptr_for_null
void test_bind_with_string_and_ptr_for_null() {
Array* args = Array::create(state, 0);
Object* ret = Fixnum::from(RBX_FFI_TYPE_STRPTR);
Pointer* name = Pointer::create(state, (void*)null_string);
NativeFunction *func = NativeFunction::generate(state, name, state->symbol("ffi"), args, ret);
TS_ASSERT(!func->nil_p());
Arguments args_obj;
Array* out = try_as<Array>(func->call(state, args_obj, NULL));
TS_ASSERT(kind_of<Array>(out));
TS_ASSERT_EQUALS(out->get(state, 0), Qnil);
TS_ASSERT_EQUALS(out->get(state, 1), Qnil);
}
示例9: execute
/* Run when a NativeFunction is executed. Executes the related C function.
*/
Object* NativeFunction::execute(STATE, CallFrame* call_frame, Dispatch& msg,
Arguments& args) {
NativeFunction* nfunc = as<NativeFunction>(msg.method);
state->set_call_frame(call_frame);
#ifdef RBX_PROFILER
if(unlikely(state->shared.profiling()))
state->profiler()->enter_method(msg, args);
#endif
Object* obj = nfunc->call(state, args);
#ifdef RBX_PROFILER
if(unlikely(state->shared.profiling()))
state->profiler()->leave();
#endif
return obj;
}
示例10: test_bind_with_state
void test_bind_with_state() {
global_state = state;
String* name = String::create(state, "dummy_state");
Array* args = Array::create(state, 1);
args->set(state, 0, Fixnum::from(RBX_FFI_TYPE_STATE));
Object* ret = Fixnum::from(RBX_FFI_TYPE_INT);
NativeFunction *func = NativeFunction::bind(state, Qnil, name, args, ret);
TS_ASSERT(!func->nil_p());
Arguments args_obj;
Object* out = func->call(state, args_obj, NULL);
TS_ASSERT(kind_of<Fixnum>(out));
TS_ASSERT(as<Fixnum>(out)->to_native());
}
示例11: test_bind_with_state
void test_bind_with_state() {
global_state = state;
Pointer* name = Pointer::create(state, (void*)dummy_state);
Array* args = Array::create(state, 1);
args->set(state, 0, Fixnum::from(RBX_FFI_TYPE_STATE));
Object* ret = Fixnum::from(RBX_FFI_TYPE_INT);
NativeFunction *func = NativeFunction::generate(state, name, state->symbol("ffi"), args, ret);
TS_ASSERT(!func->nil_p());
Arguments args_obj;
Object* out = func->call(state, args_obj, NULL);
TS_ASSERT(kind_of<Fixnum>(out));
TS_ASSERT(as<Fixnum>(out)->to_native());
}
示例12: test_bind_with_double
void test_bind_with_double() {
Pointer* name = Pointer::create(state, (void*)dummy_double);
Array* args = Array::create(state, 1);
args->set(state, 0, Fixnum::from(RBX_FFI_TYPE_DOUBLE));
Object* ret = Fixnum::from(RBX_FFI_TYPE_DOUBLE);
NativeFunction *func = NativeFunction::generate(state, name, state->symbol("ffi"), args, ret);
TS_ASSERT(!func->nil_p());
Array* input = Array::create(state, 1);
input->set(state, 0, Float::create(state, 13.2));
Arguments args_obj(input);
Object* out = func->call(state, args_obj, NULL);
TS_ASSERT(kind_of<Float>(out));
TS_ASSERT_EQUALS(as<Float>(out)->val, 13.2);
}
示例13: test_bind_with_double
void test_bind_with_double() {
String* name = String::create(state, "dummy_double");
Array* args = Array::create(state, 1);
args->set(state, 0, Fixnum::from(RBX_FFI_TYPE_DOUBLE));
Object* ret = Fixnum::from(RBX_FFI_TYPE_DOUBLE);
NativeFunction *func = NativeFunction::bind(state, Qnil, name, args, ret);
TS_ASSERT(!func->nil_p());
Array* input = Array::create(state, 1);
input->set(state, 0, Float::create(state, 13.2));
Arguments args_obj(input);
Object* out = func->call(state, args_obj, NULL);
TS_ASSERT(kind_of<Float>(out));
TS_ASSERT_EQUALS(as<Float>(out)->val, 13.2);
}
示例14: test_bind_with_short
void test_bind_with_short() {
Pointer* name = Pointer::create(state, (void*)dummy_short);
Array* args = Array::create(state, 1);
args->set(state, 0, Fixnum::from(RBX_FFI_TYPE_SHORT));
Object* ret = Fixnum::from(RBX_FFI_TYPE_SHORT);
NativeFunction *func = NativeFunction::generate(state, name, state->symbol("ffi"), args, ret);
TS_ASSERT(!func->nil_p());
Array* input = Array::create(state, 1);
input->set(state, 0, Fixnum::from(13));
Arguments args_obj(input);
Object* out = func->call(state, args_obj, NULL);
TS_ASSERT(kind_of<Fixnum>(out));
TS_ASSERT_EQUALS(as<Fixnum>(out)->to_native(), 13);
}
示例15: test_bind_with_string
void test_bind_with_string() {
Pointer* name = Pointer::create(state, (void*)strlen);
Array* args = Array::create(state, 1);
args->set(state, 0, Fixnum::from(RBX_FFI_TYPE_STRING));
Object* ret = Fixnum::from(RBX_FFI_TYPE_INT);
NativeFunction *func = NativeFunction::generate(state, name, state->symbol("ffi"), args, ret);
TS_ASSERT(!func->nil_p());
Array* input = Array::create(state, 1);
input->set(state, 0, String::create(state, "strlen"));
Arguments args_obj(input);
Object* out = func->call(state, args_obj, NULL);
TS_ASSERT(out->fixnum_p());
TS_ASSERT_EQUALS(as<Integer>(out)->to_native(), 6);
}