37 using ::v8::internal::Factory;
38 using ::v8::internal::Handle;
39 using ::v8::internal::Heap;
40 using ::v8::internal::Isolate;
41 using ::v8::internal::JSFunction;
43 using ::v8::internal::Runtime;
44 using ::v8::internal::Script;
45 using ::v8::internal::SmartArrayPointer;
46 using ::v8::internal::SharedFunctionInfo;
47 using ::v8::internal::String;
53 static void InitializeVM() {
64 const char* func_pos_src,
65 const char* ref_inferred_name) {
67 Handle<Object> obj = v8::Utils::OpenHandle(*script);
68 Handle<SharedFunctionInfo> shared_function;
69 if (obj->IsSharedFunctionInfo()) {
71 Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(*obj));
74 Handle<SharedFunctionInfo>(JSFunction::cast(*obj)->shared());
76 Handle<Script> i_script(Script::cast(shared_function->script()));
77 CHECK(i_script->source()->IsString());
78 Handle<String> script_src(String::cast(i_script->source()));
81 Handle<String> func_pos_str =
83 int func_pos = Runtime::StringMatch(Isolate::Current(),
89 #ifdef ENABLE_DEBUGGER_SUPPORT
91 Isolate::Current()->debug()->PrepareForBreakPoints();
92 Object* shared_func_info_ptr =
93 Isolate::Current()->debug()->FindSharedFunctionInfoInScript(i_script,
95 CHECK(shared_func_info_ptr !=
HEAP->undefined_value());
96 Handle<SharedFunctionInfo> shared_func_info(
97 SharedFunctionInfo::cast(shared_func_info_ptr));
100 SmartArrayPointer<char> inferred_name =
101 shared_func_info->inferred_name()->ToCString();
102 CHECK_EQ(ref_inferred_name, *inferred_name);
103 #endif // ENABLE_DEBUGGER_SUPPORT
117 "fun1 = function() { return 1; }\n"
118 "fun2 = function() { return 2; }\n");
119 CheckFunctionName(script,
"return 1",
"fun1");
120 CheckFunctionName(script,
"return 2",
"fun2");
129 "var fun1 = function() { return 1; }\n"
130 "var fun2 = function() { return 2; }\n");
131 CheckFunctionName(script,
"return 1",
"fun1");
132 CheckFunctionName(script,
"return 2",
"fun2");
141 "function outer() {\n"
142 " var fun1 = function() { return 1; }\n"
143 " var fun2 = function() { return 2; }\n"
145 CheckFunctionName(script,
"return 1",
"fun1");
146 CheckFunctionName(script,
"return 2",
"fun2");
155 "function MyClass() {\n"
156 " this.method1 = function() { return 1; }\n"
157 " this.method2 = function() { return 2; }\n"
159 CheckFunctionName(script,
"return 1",
"MyClass.method1");
160 CheckFunctionName(script,
"return 2",
"MyClass.method2");
169 "function createMyObj() {\n"
171 " obj.method1 = function() { return 1; }\n"
172 " obj.method2 = function() { return 2; }\n"
175 CheckFunctionName(script,
"return 1",
"obj.method1");
176 CheckFunctionName(script,
"return 2",
"obj.method2");
185 "function MyClass() {}\n"
186 "MyClass.static1 = function() { return 1; }\n"
187 "MyClass.static2 = function() { return 2; }\n"
188 "MyClass.MyInnerClass = {}\n"
189 "MyClass.MyInnerClass.static3 = function() { return 3; }\n"
190 "MyClass.MyInnerClass.static4 = function() { return 4; }");
191 CheckFunctionName(script,
"return 1",
"MyClass.static1");
192 CheckFunctionName(script,
"return 2",
"MyClass.static2");
193 CheckFunctionName(script,
"return 3",
"MyClass.MyInnerClass.static3");
194 CheckFunctionName(script,
"return 4",
"MyClass.MyInnerClass.static4");
203 "function MyClass() {}\n"
204 "MyClass.prototype.method1 = function() { return 1; }\n"
205 "MyClass.prototype.method2 = function() { return 2; }\n"
206 "MyClass.MyInnerClass = function() {}\n"
207 "MyClass.MyInnerClass.prototype.method3 = function() { return 3; }\n"
208 "MyClass.MyInnerClass.prototype.method4 = function() { return 4; }");
209 CheckFunctionName(script,
"return 1",
"MyClass.method1");
210 CheckFunctionName(script,
"return 2",
"MyClass.method2");
211 CheckFunctionName(script,
"return 3",
"MyClass.MyInnerClass.method3");
212 CheckFunctionName(script,
"return 4",
"MyClass.MyInnerClass.method4");
221 "function MyClass() {}\n"
222 "MyClass.prototype = {\n"
223 " method1: function() { return 1; },\n"
224 " method2: function() { return 2; } }");
225 CheckFunctionName(script,
"return 1",
"MyClass.method1");
226 CheckFunctionName(script,
"return 2",
"MyClass.method2");
235 "function f1(a) { return a(); }\n"
236 "function f2(a, b) { return a() + b(); }\n"
237 "var result1 = f1(function() { return 1; })\n"
238 "var result2 = f2(function() { return 2; }, function() { return 3; })");
240 CheckFunctionName(script,
"return 1",
"");
241 CheckFunctionName(script,
"return 2",
"");
242 CheckFunctionName(script,
"return 3",
"");
246 TEST(MultipleFuncsConditional) {
252 " function() { return 1; } :\n"
253 " function() { return 2; }");
254 CheckFunctionName(script,
"return 1",
"fun1");
255 CheckFunctionName(script,
"return 2",
"fun1");
264 "function MyClass() {}\n"
265 "MyClass.prototype = {\n"
266 " method1: 0 ? function() { return 1; } :\n"
267 " function() { return 2; } }");
268 CheckFunctionName(script,
"return 1",
"MyClass.method1");
269 CheckFunctionName(script,
"return 2",
"MyClass.method1");
280 "var result = function(p,a,c,k,e,d)"
281 "{return p}(\"if blah blah\",62,1976,\'a|b\'.split(\'|\'),0,{})\n"
283 CheckFunctionName(script,
"return p",
"");
292 "var fun1 = fun2 = function () { return 1; }\n"
293 "var bar1 = bar2 = bar3 = function () { return 2; }\n"
294 "foo1 = foo2 = function () { return 3; }\n"
295 "baz1 = baz2 = baz3 = function () { return 4; }");
296 CheckFunctionName(script,
"return 1",
"fun2");
297 CheckFunctionName(script,
"return 2",
"bar3");
298 CheckFunctionName(script,
"return 3",
"foo2");
299 CheckFunctionName(script,
"return 4",
"baz3");
308 "function Foo() {}\n"
309 "var foo = new Foo(function() { return 1; })\n"
310 "var bar = new Foo(function() { return 2; }, function() { return 3; })");
311 CheckFunctionName(script,
"return 1",
"");
312 CheckFunctionName(script,
"return 2",
"");
313 CheckFunctionName(script,
"return 3",
"");
322 "function createMyObj() {\n"
324 " obj[\"method1\"] = function() { return 1; }\n"
325 " obj[\"method2\"] = function() { return 2; }\n"
328 CheckFunctionName(script,
"return 1",
"obj.method1");
329 CheckFunctionName(script,
"return 2",
"obj.method2");
338 "function createMyObj() {\n"
340 " var methodName = \"method1\";\n"
341 " obj[methodName] = function() { return 1; }\n"
342 " methodName = \"method2\";\n"
343 " obj[methodName] = function() { return 2; }\n"
347 CheckFunctionName(script,
"return 1",
"obj.(anonymous function)");
348 CheckFunctionName(script,
"return 2",
"obj.(anonymous function)");
352 TEST(FactoryHashmapConditional) {
357 "function createMyObj() {\n"
359 " obj[0 ? \"method1\" : \"method2\"] = function() { return 1; }\n"
363 CheckFunctionName(script,
"return 1",
"obj.(anonymous function)");
367 TEST(GlobalAssignmentAndCall) {
372 "var Foo = function() {\n"
375 "var Baz = Bar = function() {\n"
379 CheckFunctionName(script,
"return 1",
"");
381 CheckFunctionName(script,
"return 2",
"Bar");
390 "(function Enclosing() {\n"
392 " Foo = function() {\n"
395 " var Baz = Bar = function() {\n"
400 CheckFunctionName(script,
"return 1",
"");
404 CheckFunctionName(script,
"return 2",
"Bar");
408 TEST(MethodAssignmentInAnonymousFunctionCall) {
414 " var EventSource = function () { };\n"
415 " EventSource.prototype.addListener = function () {\n"
418 " this.PublicEventSource = EventSource;\n"
420 CheckFunctionName(script,
"return 2012",
"EventSource.addListener");
424 TEST(ReturnAnonymousFunction) {
430 " function wrapCode() {\n"
431 " return function () {\n"
437 " return wrapCode();\n"
442 CheckFunctionName(script,
"return 2012",
"");
static Local< Script > Compile(Handle< String > source, ScriptOrigin *origin=NULL, ScriptData *pre_data=NULL, Handle< String > script_data=Handle< String >())
#define CHECK_EQ(expected, value)
static V8EXPORT Local< String > New(const char *data, int length=-1)
#define CHECK_NE(unexpected, value)
Vector< const char > CStrVector(const char *data)
static Persistent< Context > New(ExtensionConfiguration *extensions=NULL, Handle< ObjectTemplate > global_template=Handle< ObjectTemplate >(), Handle< Value > global_object=Handle< Value >())