Skip to content

Instantly share code, notes, and snippets.

@zpao
Created April 29, 2011 18:25
Show Gist options
  • Save zpao/948762 to your computer and use it in GitHub Desktop.
Save zpao/948762 to your computer and use it in GitHub Desktop.
void
test_PrototypeInScript()
{
HandleScope handle_scope;
Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
Local<Object> obj;
Local<FunctionTemplate> obj_tmpl = FunctionTemplate::New();
Local<FunctionTemplate> ctor_tmpl;
ctor_tmpl = Local<FunctionTemplate>::New(obj_tmpl);
ctor_tmpl->SetClassName(String::NewSymbol("Testing"));
obj = Local<Object>::New(obj_tmpl->GetFunction()->NewInstance());
obj->Set(String::NewSymbol("Testing"),
ctor_tmpl->GetFunction());
Local<Value> f_value =
Script::Compile(String::New(
"(function(o) {"
"var Testing = o.Testing;"
"Testing.prototype.magical = function(foo) { return 42; }"
"});"
))->Run();
do_check_true(f_value->IsFunction());
Local<Function> f = Local<Function>::Cast(f_value);
Local<Object> global = Context::GetCurrent()->Global();
Local<Value> args[1] = { Local<Value>::New(obj) };
f->Call(global, 1, args);
Local<Value> magical = obj->Get(String::New("magical"));
do_check_true(magical->IsFunction());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment