Handle<Object> SorrowContext::SetupInternals(int argc, const char *argv[]) { HandleScope scope; Local<FunctionTemplate> internals_template = FunctionTemplate::New(); internals = Persistent<Object>::New(internals_template->GetFunction()->NewInstance()); Local<Object> global = Context::GetCurrent()->Global(); SET_METHOD(global, "quit", Quit) SET_METHOD(global, "version", Version) SET_METHOD(internals, "compile", CompileScript) if (argc) { Local<Array> lineArgs = Array::New(argc-1); for (int i = 0; i +1 < argc; i++) { lineArgs->Set(Integer::New(i), V8_STR(argv[i+1])); } internals->Set(V8_STR("args"), lineArgs); } else { internals->Set(V8_STR("args"), Array::New()); } Handle<Object> libsObject = Object::New(); LoadNativeLibraries(libsObject); internals->Set(V8_STR("stdlib"), libsObject); Handle<ObjectTemplate> env = ObjectTemplate::New(); env->SetNamedPropertyHandler(EnvGetter); internals->Set(V8_STR("env"), env->NewInstance()); internals->Set(V8_STR("global"), global); binarytypes = new BinaryTypes(internals); iostreams = new IOStreams(internals); Filesystem::Initialize(internals); Extensions::Initialize(internals); TryCatch tryCatch; Local<Value> func = ExecuteString(V8_STR(sorrow_native), V8_STR("sorrow.js")); if (tryCatch.HasCaught()) { ReportException(&tryCatch); exit(10); } ASSERT_PIN(func->IsFunction(), "sorrow.js main function not found"); Local<Function> f = Local<Function>::Cast(func); Local<Value> args[1] = { Local<Value>::New(internals) }; f->Call(global, 1, args); if (tryCatch.HasCaught()) { ReportException(&tryCatch); exit(11); } return internals; } // SetupInternals
static Handle<ObjectTemplate> PrepareTemplate() { HandleScope handle_scope; Handle<ObjectTemplate> result = ObjectTemplate::New(); result->SetInternalFieldCount(2); result->SetNamedPropertyHandler(ObjectGet, ObjectSet); return handle_scope.Close(result); }
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(); }