Handle NativeMethodEnvironment::get_handle(Object* obj) {
if(obj->reference_p()) {
return current_native_frame_->get_handle(state_, obj);
} else if(obj->fixnum_p()) {
return reinterpret_cast<VALUE>(obj);
} else if(obj->symbol_p()) {
return reinterpret_cast<ID>(obj);
} else if(obj->nil_p()) {
return cCApiHandleQnil;
} else if(obj->false_p()) {
return cCApiHandleQfalse;
} else if(obj->true_p()) {
return cCApiHandleQtrue;
} else if(obj->undef_p()) {
return cCApiHandleQundef;
} else {
capi_raise_runtime_error("handle requested for unknown object type");
}
return 0;
}
Handle NativeMethodEnvironment::get_handle_global(Object* obj) {
Handles& global_handles = state_->shared.global_handles();
Handle handle = CAPI_APPLY_GLOBAL_TAG(global_handles.size());
global_handles.push_back(new RootHandle(state_, obj));
return handle;
}
Object* NativeMethodEnvironment::get_object(Handle handle) {
if(CAPI_REFERENCE_P(handle)) {
if(CAPI_GLOBAL_HANDLE_P(handle)) {
Handles& global_handles = state_->shared.global_handles();
RootHandle* root = global_handles[CAPI_STRIP_GLOBAL_TAG(handle)];
if(!root) {
capi_raise_runtime_error("Attempted to use deleted NativeMethod global handle");
}
Object* obj = root->get();
if(!obj) {
capi_raise_runtime_error("NativeMethod global handle refers to NULL object");
}
return obj;
} else {
current_native_frame_->get_object(handle);
}
} else if(FIXNUM_P(handle) || SYMBOL_P(handle)) {
return reinterpret_cast<Object*>(handle);
} else if(CAPI_FALSE_P(handle)) {
return Qfalse;
} else if(CAPI_TRUE_P(handle)) {
return Qtrue;
} else if(CAPI_NIL_P(handle)) {
return Qnil;
} else if(CAPI_UNDEF_P(handle)) {
return Qundef;
} else {
capi_raise_runtime_error("requested Object for unknown handle type");
}
return Qnil;
}