Reviewers: danno, Mikhail Naganov (Chromium),

Description:
Provide access to function inferred name in v8 public API

Please review this at https://chromiumcodereview.appspot.com/9146039/

SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/

Affected files:
  M     include/v8.h
  M     src/api.cc
  M     test/cctest/test-api.cc


Index: include/v8.h
===================================================================
--- include/v8.h        (revision 10490)
+++ include/v8.h        (working copy)
@@ -1732,6 +1732,14 @@
   V8EXPORT Handle<Value> GetName() const;

   /**
+   * Name inferred from variable or property assignment of this function.
+   * Used to facilitate debugging and profiling of JavaScript code written
+   * in OO style, where almost all functions are anonymous but are assigned
+   * to object properties.
+   */
+  V8EXPORT Handle<Value> GetInferredName() const;
+
+  /**
    * Returns zero based line number of function body and
    * kLineOffsetNotFound if no information available.
    */
Index: src/api.cc
===================================================================
--- src/api.cc  (revision 10490)
+++ src/api.cc  (working copy)
@@ -3622,6 +3622,12 @@
 }


+Handle<Value> Function::GetInferredName() const {
+  i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
+ return Utils::ToLocal(i::Handle<i::Object>(func->shared()->inferred_name()));
+}
+
+
 ScriptOrigin Function::GetScriptOrigin() const {
   i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
   if (func->shared()->script()->IsScript()) {
Index: test/cctest/test-api.cc
===================================================================
--- test/cctest/test-api.cc     (revision 10490)
+++ test/cctest/test-api.cc     (working copy)
@@ -14038,6 +14038,17 @@
   CHECK_EQ(0, script_origin_g.ResourceLineOffset()->Int32Value());
 }

+THREADED_TEST(FunctionGetInferredName) {
+  v8::HandleScope scope;
+  LocalContext env;
+  v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test"));
+  v8::Handle<v8::String> script = v8::String::New(
+      "var foo = { bar : { baz : function() {}}}; var f = foo.bar.baz;");
+  v8::Script::Compile(script, &origin)->Run();
+  v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
+      env->Global()->Get(v8::String::New("f")));
+  CHECK_EQ("foo.bar.baz", *v8::String::AsciiValue(f->GetInferredName()));
+}

 THREADED_TEST(ScriptLineNumber) {
   v8::HandleScope scope;


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to