本文整理汇总了C++中NativeMethodEnvironment类的典型用法代码示例。如果您正苦于以下问题:C++ NativeMethodEnvironment类的具体用法?C++ NativeMethodEnvironment怎么用?C++ NativeMethodEnvironment使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NativeMethodEnvironment类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: rb_struct_define
VALUE rb_struct_define(const char *name, ...) {
va_list args;
va_start(args, name);
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
VALUE nm;
if(name) {
nm = rb_str_new2(name);
} else {
nm = Qnil;
}
char *sym;
Array* array = Array::create(env->state(), 0);
while((sym = va_arg(args, char*)) != 0) {
array->append(env->state(), env->get_object(rb_intern(sym)));
}
va_end(args);
return rb_funcall(rb_cStruct, rb_intern("make_struct"), 2, nm, env->get_handle(array));
}
示例2: rb_big2ulong
unsigned long rb_big2ulong(VALUE obj) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Object* object = env->get_object(obj);
if(object->nil_p()) {
rb_raise(rb_eTypeError, "no implicit conversion from nil to unsigned long");
} else if(Bignum* big = try_as<Bignum>(object)) {
if((size_t)mp_count_bits(big->mp_val()) > sizeof(long) * CHAR_BIT)
rb_raise(rb_eRangeError, "bignum too big to convert into long");
unsigned long val = big->to_ulong();
if(big->mp_val()->sign == MP_NEG) {
if((long)val < 0)
rb_raise(rb_eRangeError, "bignum out of range of unsigned long");
return -val;
}
return val;
}
rb_raise(rb_eArgError, "parameter is not a Bignum");
return 0;
}
示例3: capi_thread_create
VALUE capi_thread_create(VALUE (*func)(ANYARGS), void* arg, const char* name, const char* file) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
State* state = env->state();
NativeMethod* nm = NativeMethod::create(state,
String::create(state, file), G(thread),
state->symbol(name), (void*)func,
Fixnum::from(1), 0);
Pointer* ptr = Pointer::create(state, arg);
Thread* thr = Thread::create(env->state(), G(thread), run_function);
thr->locals_store(state, state->symbol("function"), nm);
thr->locals_store(state, state->symbol("argument"), ptr);
thr->group(state, state->vm()->thread.get()->group());
VALUE thr_handle = env->get_handle(thr);
thr->fork(state);
return thr_handle;
}
示例4: rb_thread_call_with_gvl
void* rb_thread_call_with_gvl(void* (*func)(void*), void* data) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
env->state()->shared().enter_capi(env->state());
env->state()->gc_dependent();
void* ret = (*func)(data);
env->state()->gc_independent();
env->state()->shared().leave_capi(env->state());
return ret;
}
示例5: rb_define_class_under
/** @note Shares code with rb_define_module_under, change there too. --rue */
VALUE rb_define_class_under(VALUE parent_handle, const char* name, VALUE superclass_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Module* parent = c_as<Module>(env->get_object(parent_handle));
Class* superclass = c_as<Class>(env->get_object(superclass_handle));
Symbol* constant = env->state()->symbol(name);
bool created = false;
Class* cls = rubinius::Helpers::open_class(env->state(),
env->current_call_frame(), parent, superclass, constant, &created);
return env->get_handle_global(cls);
}
示例6: rb_const_get_from
VALUE rb_const_get_from(VALUE module_handle, ID id_name) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
State* state = env->state();
Symbol* name = reinterpret_cast<Symbol*>(id_name);
Module* module = c_as<Module>(env->get_object(module_handle));
ConstantMissingReason reason = vNonExistent;
while(!module->nil_p()) {
Object* val = module->get_const(state, name, G(sym_private), &reason);
if(reason == vFound) {
if(Autoload* autoload = try_as<Autoload>(val)) {
return capi_fast_call(env->get_handle(autoload),
rb_intern("call"), 1, module_handle);
}
return env->get_handle(val);
}
module = module->superclass();
}
return const_missing(module_handle, id_name);
}
示例7: rb_thread_call_without_gvl2
// THAR BE EVEN MORE DRAGONS.
//
// When venturing through the valleys of the unmanaged, our hero must
// remain vigilant and disciplined! If she should ever find a VALUE for
// a reference in her travels: Look away! For these VALUEs are pure
// death! Our hero must steel herself and continue on her quest, returning
// as soon as possible to the castle of the managed.
void* rb_thread_call_without_gvl2(void *(*func)(void *data), void* data1,
rb_unblock_function_t ubf, void* ubf_data) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
State* state = env->state();
void* ret = NULL;
if(!state->check_async(env->current_call_frame())) {
return ret;
}
if(ubf == RUBY_UBF_IO || ubf == RUBY_UBF_PROCESS) {
state->vm()->interrupt_with_signal();
} else {
state->vm()->wait_on_custom_function(ubf, ubf_data);
}
LEAVE_CAPI(env->state());
{
GCIndependent guard(env);
ret = (*func)(data1);
}
ENTER_CAPI(env->state());
state->vm()->clear_waiter();
return ret;
}
示例8: rb_thread_call_without_gvl2
// THAR BE EVEN MORE DRAGONS.
//
// When venturing through the valleys of the unmanaged, our hero must
// remain vigilant and disciplined! If she should ever find a VALUE for
// a reference in her travels: Look away! For these VALUEs are pure
// death! Our hero must steel herself and continue on her quest, returning
// as soon as possible to the castle of the managed.
void* rb_thread_call_without_gvl2(void *(*func)(void *data), void* data1,
rb_unblock_function_t ubf, void* ubf_data) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
State* state = env->state();
void* ret = NULL;
if(state->vm()->thread_interrupted_p(state)) {
return ret;
}
if(ubf == RUBY_UBF_IO || ubf == RUBY_UBF_PROCESS) {
state->vm()->interrupt_with_signal();
} else {
state->vm()->wait_on_custom_function(ubf, ubf_data);
}
LEAVE_CAPI(env->state());
{
UnmanagedPhase unmanaged(env->state());
ret = (*func)(data1);
}
ENTER_CAPI(env->state());
state->vm()->clear_waiter();
return ret;
}
示例9: rb_thread_wait_fd
/*
* rb_thread_wait_fd actually waits until a read is
* available on the given fd
*/
void rb_thread_wait_fd(int fd) {
fd_set fds;
FD_ZERO(&fds);
FD_SET((int_fd_t)fd, &fds);
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
State* state = env->state();
LEAVE_CAPI(state);
{
GCIndependent guard(env);
state->vm()->interrupt_with_signal();
state->vm()->thread->sleep(state, cTrue);
int ready = 0;
while(!ready) {
ready = select(fd+1, &fds, 0, 0, 0);
}
state->vm()->thread->sleep(state, cFalse);
state->vm()->clear_waiter();
}
ENTER_CAPI(env->state());
}
示例10: rb_define_module_under
/** @note Shares code with rb_define_class_under, change there too. --rue */
VALUE rb_define_module_under(VALUE parent_handle, const char* name) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Module* parent = c_as<Module>(env->get_object(parent_handle));
Symbol* constant = env->state()->symbol(name);
LEAVE_CAPI(env->state());
Module* module = rubinius::Helpers::open_module(env->state(),
env->current_call_frame(), parent, constant);
// The call above could have triggered an Autoload resolve, which may
// raise an exception, so we have to check the value returned.
if(!module) env->current_ep()->return_to(env);
// Grab the module handle before grabbing the lock
// so the Module isn't accidentally GC'ed.
VALUE module_handle = env->get_handle(module);
ENTER_CAPI(env->state());
return module_handle;
}
示例11: rb_thread_select
int rb_thread_select(int max, fd_set* read, fd_set* write, fd_set* except,
struct timeval *input_tv)
{
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
int ret = 0;
struct timeval tv;
struct timeval absolute_tv;
struct timeval* tvp = NULL;
if(input_tv) {
// We make a new timeval rather than using input_tv because we modify it.
tv = *input_tv;
tvp = &tv;
gettimeofday(&absolute_tv, NULL);
timeradd(&absolute_tv, &tv, &absolute_tv);
}
for(;;) {
{
GlobalLock::UnlockGuard guard(env);
ret = select(max, read, write, except, tvp);
}
if(!env->state()->check_async(env->current_call_frame())) {
// Ok, there was an exception raised by an async event. We need
// to unwind through the caller back the entrance to the native
// method.
// Only handle true exceptions being raised, eat all other requests
// for now.
if(env->state()->thread_state()->raise_reason() == cException) {
capi::capi_raise_backend(env->state()->thread_state()->current_exception());
} else {
env->state()->thread_state()->clear();
}
}
if(ret < 0 && errno == EINTR) {
if(input_tv) {
struct timeval cur_tv;
gettimeofday(&cur_tv, NULL);
timersub(&absolute_tv, &cur_tv, &tv);
}
} else {
break;
}
}
return ret;
}
示例12: rb_define_module_under
/** @note Shares code with rb_define_class_under, change there too. --rue */
VALUE rb_define_module_under(VALUE parent_handle, const char* name) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Module* parent = c_as<Module>(env->get_object(parent_handle));
Symbol* constant = env->state()->symbol(name);
Module* module = rubinius::Helpers::open_module(env->state(),
env->current_call_frame(), parent, constant);
return env->get_handle(module);
}
示例13: rb_const_get
VALUE rb_const_get(VALUE module_handle, ID id_name) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Symbol* name = reinterpret_cast<Symbol*>(id_name);
Module* module = c_as<Module>(env->get_object(module_handle));
bool found = false;
while(!module->nil_p()) {
Object* val = module->get_const(env->state(), name, &found);
if(found) {
if(Autoload* autoload = try_as<Autoload>(val)) {
return capi_fast_call(env->get_handle(autoload), rb_intern("call"), 0);
}
return env->get_handle(val);
}
module = module->superclass();
}
// Try from Object as well.
module = env->state()->globals().object.get();
while(!module->nil_p()) {
Object* val = module->get_const(env->state(), name, &found);
if(found) {
if(Autoload* autoload = try_as<Autoload>(val)) {
return capi_fast_call(env->get_handle(autoload), rb_intern("call"), 0);
}
return env->get_handle(val);
}
module = module->superclass();
}
return const_missing(module_handle, id_name);
}
示例14: capi_define_method
void capi_define_method(const char* file, VALUE target,
const char* name, CApiGenericFunction fptr,
int arity, CApiMethodKind kind)
{
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
State* state = env->state();
Symbol* method_name = state->symbol(name);
Module* module = NULL;
if(kind == cCApiSingletonMethod) {
module = c_as<Module>(env->get_object(target)->singleton_class(env->state()));
} else {
module = c_as<Module>(env->get_object(target));
}
NativeMethod* method = NULL;
method = NativeMethod::create(state, String::create(state, file),
module, method_name,
(void*)fptr, Fixnum::from(arity),
env->current_native_frame()->capi_lock_index());
Symbol* visibility;
switch(kind) {
case cCApiPrivateMethod:
visibility = G(sym_private);
break;
case cCApiProtectedMethod:
visibility = G(sym_protected);
break;
default: /* Also catches singletons for now. @todo Verify OK. --rue */
visibility = G(sym_public);
break;
}
module->add_method(state, method_name, method, visibility);
System::vm_reset_method_cache(env->state(), module, method_name, env->current_call_frame());
}
示例15: rb_block_call
VALUE rb_block_call(VALUE obj, ID meth, int argc, VALUE* argv,
VALUE(*cb)(ANYARGS), VALUE cb_data) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
if(cb) {
Proc* prc = capi::wrap_c_function((void*)cb, cb_data, C_BLOCK_CALL);
env->set_outgoing_block(env->get_handle(prc));
} else {
env->set_outgoing_block(env->get_handle(env->block()));
}
return rb_funcall2(obj, meth, argc, argv);
}