Skip to content

Instantly share code, notes, and snippets.

@tj
Created October 6, 2010 17:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tj/613704 to your computer and use it in GitHub Desktop.
Save tj/613704 to your computer and use it in GitHub Desktop.
function InstantiateFunction(data, name) {
// We need a reference to kApiFunctionCache in the stack frame
// if we need to bail out from a stack overflow.
var cache = kApiFunctionCache;
var serialNumber = %GetTemplateField(data, kApiSerialNumberOffset);
var isFunctionCached =
(serialNumber in cache) && (cache[serialNumber] != kUninitialized);
if (!isFunctionCached) {
try {
cache[serialNumber] = null;
var fun = %CreateApiFunction(data);
if (name) %FunctionSetName(fun, name);
cache[serialNumber] = fun;
var prototype = %GetTemplateField(data, kApiPrototypeTemplateOffset);
fun.prototype = prototype ? Instantiate(prototype) : {};
%SetProperty(fun.prototype, "constructor", fun, DONT_ENUM);
var parent = %GetTemplateField(data, kApiParentTemplateOffset);
if (parent) {
var parent_fun = Instantiate(parent);
fun.prototype.__proto__ = parent_fun.prototype;
}
ConfigureTemplateInstance(fun, data);
} catch (e) {
cache[serialNumber] = kUninitialized;
throw e;
}
}
return cache[serialNumber];
}
@aheckmann
Copy link

What is this?

@tj
Copy link
Author

tj commented Oct 6, 2010

v8's function init code, Tim was wondering where .constructor came from

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