Skip to content

Instantly share code, notes, and snippets.

@mraleph
Created February 2, 2011 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mraleph/808039 to your computer and use it in GitHub Desktop.
Save mraleph/808039 to your computer and use it in GitHub Desktop.
v8::Local<v8::Function> MkFunction(v8::Handle<v8::String> body) {
v8::HandleScope scope;
// Get global object
v8::Local<v8::Object> global = v8::Context::GetCurrent()->Global();
// Get built-in Function constructor (see ECMA-262 5th edition 15.3.2)
v8::Local<v8::Function> function_ctor =
v8::Local<v8::Function>::Cast(global->Get(v8::String::New("Function")));
// Invoke Function constructor to create function with the given body and no arguments
v8::Handle<v8::Value> argv[1] = { body };
v8::Local<v8::Object> function = function_ctor->NewInstance(1, argv);
return scope.Close(v8::Local<v8::Function>::Cast(function));
}
@leontiy
Copy link

leontiy commented Mar 10, 2011

Readability masterpiece. =)

@mraleph
Copy link
Author

mraleph commented Mar 10, 2011

I hope there is no sarcasm in your words as I tried to make it as readable as possible --- this is an answer to a question of a V8 embedder :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment