void test_obj_defprop() { HandleScope handle_scope; Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Handle<Object> obj = Object::New(); Handle<Value> data = Integer::New(2); obj->SetAccessor(String::New("myprop"), ReadTestVal, WriteTestVal, data); Local<Object> global = context->Global(); global->Set(String::New("testobj"), obj); Handle<String> source = String::New("var n = testobj.myprop; testobj.myprop = (n+9); testobj.myprop"); // Compile the source code. Handle<Script> script = Script::Compile(source); // Run the script to get the result. Handle<Value> result = script->Run(); do_check_true(!result.IsEmpty()); do_check_true(result->IsInt32()); JSInt32 i = result->Int32Value(); do_check_eq(12, i); context.Dispose(); }
int V8Scope::type( const char *field ){ V8_SIMPLE_HEADER Handle<Value> v = get( field ); if ( v->IsNull() ) return jstNULL; if ( v->IsUndefined() ) return Undefined; if ( v->IsString() ) return String; if ( v->IsFunction() ) return Code; if ( v->IsArray() ) return Array; if ( v->IsBoolean() ) return Bool; if ( v->IsInt32() ) return NumberInt; if ( v->IsNumber() ) return NumberDouble; if ( v->IsExternal() ){ uassert( 10230 , "can't handle external yet" , 0 ); return -1; } if ( v->IsDate() ) return Date; if ( v->IsObject() ) return Object; throw UserException( 12509, (string)"don't know what this is: " + field ); }
void test_obj_propexn() { HandleScope handle_scope; Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Handle<Object> obj = Object::New(); obj->SetAccessor(String::New("myprop"), ReadExn, WriteExn); Local<Object> global = context->Global(); global->Set(String::New("testobj"), obj); Handle<String> source = String::New("var n = 0;" "try { testobj.myprop; } catch (e) { n += e; };" "try { testobj.myprop = (n+9); } catch (e) { n += e; }; n"); // Compile the source code. Handle<Script> script = Script::Compile(source); TryCatch trycatch; // Run the script to get the result. Handle<Value> result = script->Run(); do_check_false(result.IsEmpty()); do_check_true(result->IsInt32()); do_check_false(trycatch.HasCaught()); JSInt32 i = result->Int32Value(); do_check_eq(13, i); context.Dispose(); }
void test_Exception() { HandleScope handle_scope; Persistent<Context> context = Context::New(); Handle<Script> script = Script::New(String::New("function foo(x) { throw x; };")); Context::Scope scope(context); TryCatch trycatch; Handle<Value> v = script->Run(); do_check_true(!v.IsEmpty()); do_check_true(!trycatch.HasCaught()); Handle<Function> fn = context->Global()->Get(String::NewSymbol("foo")).As<Function>(); do_check_true(!fn.IsEmpty()); Local<Value> args[1] = { Integer::New(4) }; v = fn->Call(context->Global(), 1, args); do_check_true(v.IsEmpty()); do_check_true(trycatch.HasCaught()); Handle<Value> exn = trycatch.Exception(); do_check_true(exn->IsInt32()); do_check_eq(exn->Int32Value(), 4); context.Dispose(); }
void printV8Value(Handle<Value> value, bool force=false) { Logging::Level level = force ? Logging::ERROR : Logging::INFO; if (!Logging::shouldShow(level)) return; HandleScope handleScope; if (value.IsEmpty()) Logging::log(level, "Empty handle\r\n"); else if (value->IsInt32()) Logging::log(level, "INT: %d\r\n", value->IntegerValue()); else if (value->IsNull()) Logging::log(level, "NULL (null)\r\n"); else if (value->IsUndefined()) Logging::log(level, "VOID (undefined)\r\n"); else if (value->IsBoolean()) Logging::log(level, "BOOLEAN: %d\r\n", value->BooleanValue()); else if (value->IsNumber()) Logging::log(level, "NUMBER: %f\r\n", value->NumberValue()); else if (value->IsString()) Logging::log(level, "STRING: ?\r\n"); else if (value->IsObject()) Logging::log(level, "OBJECT (object)\r\n"); else Logging::log(level, "Uncertain V8 value\n"); }
int Conv::GetNaturalType(Handle<Value> val) { if(val.IsEmpty()) return TYPE_INVALID; if(val->IsUndefined()) return TYPE_UNDEFINED; if(val->IsNull()) return TYPE_NULL; if(val->IsBoolean() || val->IsBooleanObject()) return TYPE_BOOL; if(val->IsInt32()) return TYPE_INT; if(val->IsUint32()) return TYPE_LONG; if(val->IsNumber() || val->IsNumberObject()) return TYPE_DOUBLE; if(val->IsString() || val->IsStringObject()) return TYPE_STRING; return GetNaturalType(Handle<Object>::Cast(val)); }
int obj2addr (Handle<Object> obj, struct sockaddr_in* addr) { HandleScope scope; String::AsciiValue a (obj->Get(JS_str("addr"))); Handle<Value> p = obj->Get(JS_str("port")); memset(addr,0,sizeof(struct sockaddr_in)); if (1!=inet_pton(AF_INET,*a,&(addr->sin_addr.s_addr))) return -1; if (!p->IsInt32()) return -2; addr->sin_port = htons((uint16_t)p->Uint32Value()); addr->sin_family = AF_INET; return 0; }
inline bool setField(OGRFeature* f, int field_index, Handle<Value> val){ if (val->IsInt32()) { f->SetField(field_index, val->Int32Value()); } else if (val->IsNumber()) { f->SetField(field_index, val->NumberValue()); } else if (val->IsString()) { std::string str = *NanUtf8String(val); f->SetField(field_index, str.c_str()); } else if(val->IsNull() || val->IsUndefined()) { f->UnsetField(field_index); } else { return true; } return false; }
VALUE rr_v82rb(Handle<Value> value) { if (value.IsEmpty()) { return rr_v8_value_empty(); } if (value->IsUndefined() || value->IsNull()) { return Qnil; } if (value->IsExternal()) { return rr_reflect_v8_external(value); } if (value->IsUint32()) { return UINT2NUM(value->Uint32Value()); } if (value->IsInt32()) { return INT2FIX(value->Int32Value()); } if (value->IsBoolean()) { return value->BooleanValue() ? Qtrue : Qfalse; } if (value->IsNumber()) { return rb_float_new(value->NumberValue()); } if (value->IsString()) { return rr_reflect_v8_string(value); } if (value->IsFunction()) { return rr_reflect_v8_function(value); } if (value->IsArray()) { return rr_reflect_v8_array(value); } if (value->IsDate()) { return rr_reflect_v8_date(value); } if (value->IsObject()) { return rr_reflect_v8_object(value); } return rr_wrap_v8_value(value); }
void test_obj_tmplexn() { HandleScope scope; Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Handle<ObjectTemplate> tmpl = ObjectTemplate::New(); tmpl->SetNamedPropertyHandler(NamedGetterThrows, NamedSetterThrows, 0, NamedDeleterThrows); tmpl->SetIndexedPropertyHandler(IndexedGetterThrows, IndexedSetterThrows, 0, IndexedDeleterThrows); Handle<Object> obj = tmpl->NewInstance(); Local<Object> global = context->Global(); global->Set(String::New("testobj"), obj); Handle<String> source = String::New("var n = 0;" "try { testobj.myprop; } catch (e) { n += e; };" "try { testobj.myprop = (n+9); } catch (e) { n += e; }" "try { delete testobj.myprop; } catch (e) { n += e; };" "try { testobj[4]; } catch (e) { n += e; };" "try { testobj[5] = (n+9); } catch (e) { n += e; }" "try { delete testobj[6]; } catch (e) { n += e; }; n"); // Compile the source code. Handle<Script> script = Script::Compile(source); TryCatch trycatch; // Run the script to get the result. Handle<Value> result = script->Run(); do_check_false(result.IsEmpty()); do_check_true(result->IsInt32()); do_check_false(trycatch.HasCaught()); JSInt32 i = result->Int32Value(); do_check_eq(i, 21); context.Dispose(); }
jsvalue JsEngine::AnyFromV8(Handle<Value> value, Handle<Object> thisArg) { jsvalue v; // Initialize to a generic error. v.type = JSVALUE_TYPE_UNKNOWN_ERROR; v.length = 0; v.value.str = 0; if (value->IsNull() || value->IsUndefined()) { v.type = JSVALUE_TYPE_NULL; } else if (value->IsBoolean()) { v.type = JSVALUE_TYPE_BOOLEAN; v.value.i32 = value->BooleanValue() ? 1 : 0; } else if (value->IsInt32()) { v.type = JSVALUE_TYPE_INTEGER; v.value.i32 = value->Int32Value(); } else if (value->IsUint32()) { v.type = JSVALUE_TYPE_INDEX; v.value.i64 = value->Uint32Value(); } else if (value->IsNumber()) { v.type = JSVALUE_TYPE_NUMBER; v.value.num = value->NumberValue(); } else if (value->IsString()) { v = StringFromV8(value); } else if (value->IsDate()) { v.type = JSVALUE_TYPE_DATE; v.value.num = value->NumberValue(); } else if (value->IsArray()) { Handle<Array> object = Handle<Array>::Cast(value->ToObject()); v.length = object->Length(); jsvalue* array = new jsvalue[v.length]; if (array != NULL) { for(int i = 0; i < v.length; i++) { array[i] = AnyFromV8(object->Get(i)); } v.type = JSVALUE_TYPE_ARRAY; v.value.arr = array; } } else if (value->IsFunction()) { Handle<Function> function = Handle<Function>::Cast(value); jsvalue* array = new jsvalue[2]; if (array != NULL) { array[0].value.ptr = new Persistent<Object>(Persistent<Function>::New(function)); array[0].length = 0; array[0].type = JSVALUE_TYPE_WRAPPED; if (!thisArg.IsEmpty()) { array[1].value.ptr = new Persistent<Object>(Persistent<Object>::New(thisArg)); array[1].length = 0; array[1].type = JSVALUE_TYPE_WRAPPED; } else { array[1].value.ptr = NULL; array[1].length = 0; array[1].type = JSVALUE_TYPE_NULL; } v.type = JSVALUE_TYPE_FUNCTION; v.value.arr = array; } } else if (value->IsObject()) { Handle<Object> obj = Handle<Object>::Cast(value); if (obj->InternalFieldCount() == 1) v = ManagedFromV8(obj); else v = WrappedFromV8(obj); } return v; }
/* DESCRIPTION Processing in binds PARAMETERS: Handle value, bind struct, eBaton struct */ void Connection::GetInBindParams (Handle<Value> v8val, Bind* bind, eBaton* executeBaton, BindType type) { bind->ind = 0; if(v8val->IsUndefined() || v8val->IsNull()) { bind->value = NULL; bind->ind = -1; bind->type = dpi::DpiVarChar; } else if(v8val->IsString()) { if( bind->type && bind->type != DATA_STR ) { executeBaton->error= NJSMessages::getErrorMsg( errBindValueAndTypeMismatch, 2); goto exitGetInBindParams; } v8::String::Utf8Value str(v8val->ToString()); bind->type = dpi::DpiVarChar; if(type == BIND_INOUT) { bind->len = str.length(); } else // IN { bind->maxSize = bind->len = str.length(); } DPI_SZ_TYPE size = (bind->maxSize >= bind->len ) ? bind->maxSize : bind->len; if(size) { bind->value = (char*)malloc(size); if(str.length()) memcpy(bind->value, *str, str.length()); } } else if(v8val->IsInt32()) { if( bind->type && bind->type != DATA_NUM ) { executeBaton->error= NJSMessages::getErrorMsg( errBindValueAndTypeMismatch, 2); goto exitGetInBindParams; } bind->type = dpi::DpiInteger; bind->maxSize = bind->len = sizeof(int); bind->value = (int*)malloc(bind->len); *(int*)(bind->value) = v8val->ToInt32()->Value(); } else if(v8val->IsUint32()) { if( bind->type && bind->type != DATA_NUM ) { executeBaton->error= NJSMessages::getErrorMsg( errBindValueAndTypeMismatch, 2); goto exitGetInBindParams; } bind->type = dpi::DpiUnsignedInteger; bind->maxSize = bind->len = sizeof(unsigned int); bind->value = (unsigned int*)malloc(bind->len); *(unsigned int*)(bind->value) = v8val->ToUint32()->Value(); } else if(v8val->IsNumber()) { if( bind->type && bind->type != DATA_NUM ) { executeBaton->error= NJSMessages::getErrorMsg(errBindValueAndTypeMismatch, 2); goto exitGetInBindParams; } bind->type = dpi::DpiDouble; bind->maxSize = bind->len = sizeof(double); bind->value = (double*)malloc(bind->len); *(double*)(bind->value) = v8val->NumberValue(); } else if(v8val->IsDate ()) { if( bind->type && bind->type != DATA_DATE ) { executeBaton->error= NJSMessages::getErrorMsg(errBindValueAndTypeMismatch, 2); goto exitGetInBindParams; } /* This has to be allocated after stmt is initialized */ bind->dttmarr = NULL ; bind->extvalue = (long double *) malloc (sizeof ( long double ) ); bind->value = (long double *)malloc (sizeof ( long double )); bind->type = dpi::DpiTimestampLTZ; bind->len = 0; bind->maxSize = 0; /* Convert v8::Date value to long double */ Connection::v8Date2OraDate ( v8val, bind); } else { executeBaton->error= NJSMessages::getErrorMsg(errInvalidBindDataType,2); goto exitGetInBindParams; } executeBaton->binds.push_back(bind); exitGetInBindParams: ; }
extern "C" bool isInt32(Handle<Value> n) { return n->IsInt32(); }