|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
3 */ |
|
4 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
7 |
|
8 #include "jsfun.h" // for js::IsInternalFunctionObject |
|
9 |
|
10 #include "jsapi-tests/tests.h" |
|
11 |
|
12 #include "jsobjinlines.h" |
|
13 |
|
14 BEGIN_TEST(testLookup_bug522590) |
|
15 { |
|
16 // Define a function that makes method-bearing objects. |
|
17 JS::RootedValue x(cx); |
|
18 EXEC("function mkobj() { return {f: function () {return 2;}} }"); |
|
19 |
|
20 // Calling mkobj() multiple times must create multiple functions in ES5. |
|
21 EVAL("mkobj().f !== mkobj().f", &x); |
|
22 CHECK_SAME(x, JSVAL_TRUE); |
|
23 |
|
24 // Now make x.f a method. |
|
25 EVAL("mkobj()", &x); |
|
26 JS::RootedObject xobj(cx, JSVAL_TO_OBJECT(x)); |
|
27 |
|
28 // This lookup must not return an internal function object. |
|
29 JS::RootedValue r(cx); |
|
30 CHECK(JS_LookupProperty(cx, xobj, "f", &r)); |
|
31 CHECK(r.isObject()); |
|
32 JSObject *funobj = &r.toObject(); |
|
33 CHECK(funobj->is<JSFunction>()); |
|
34 CHECK(!js::IsInternalFunctionObject(funobj)); |
|
35 |
|
36 return true; |
|
37 } |
|
38 END_TEST(testLookup_bug522590) |
|
39 |
|
40 static const JSClass DocumentAllClass = { |
|
41 "DocumentAll", |
|
42 JSCLASS_EMULATES_UNDEFINED, |
|
43 JS_PropertyStub, |
|
44 JS_DeletePropertyStub, |
|
45 JS_PropertyStub, |
|
46 JS_StrictPropertyStub, |
|
47 JS_EnumerateStub, |
|
48 JS_ResolveStub, |
|
49 JS_ConvertStub |
|
50 }; |
|
51 |
|
52 bool |
|
53 document_resolve(JSContext *cx, JS::HandleObject obj, JS::HandleId id, |
|
54 JS::MutableHandleObject objp) |
|
55 { |
|
56 // If id is "all", resolve document.all=true. |
|
57 JS::RootedValue v(cx); |
|
58 if (!JS_IdToValue(cx, id, &v)) |
|
59 return false; |
|
60 if (JSVAL_IS_STRING(v)) { |
|
61 JSString *str = JSVAL_TO_STRING(v); |
|
62 JSFlatString *flatStr = JS_FlattenString(cx, str); |
|
63 if (!flatStr) |
|
64 return false; |
|
65 if (JS_FlatStringEqualsAscii(flatStr, "all")) { |
|
66 JS::Rooted<JSObject*> docAll(cx, |
|
67 JS_NewObject(cx, &DocumentAllClass, JS::NullPtr(), JS::NullPtr())); |
|
68 if (!docAll) |
|
69 return false; |
|
70 JS::Rooted<JS::Value> allValue(cx, ObjectValue(*docAll)); |
|
71 bool ok = JS_DefinePropertyById(cx, obj, id, allValue, nullptr, nullptr, 0); |
|
72 objp.set(ok ? obj.get() : nullptr); |
|
73 return ok; |
|
74 } |
|
75 } |
|
76 objp.set(nullptr); |
|
77 return true; |
|
78 } |
|
79 |
|
80 static const JSClass document_class = { |
|
81 "document", JSCLASS_NEW_RESOLVE, |
|
82 JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub, |
|
83 JS_EnumerateStub, (JSResolveOp) document_resolve, JS_ConvertStub |
|
84 }; |
|
85 |
|
86 BEGIN_TEST(testLookup_bug570195) |
|
87 { |
|
88 JS::RootedObject obj(cx, JS_NewObject(cx, &document_class, JS::NullPtr(), JS::NullPtr())); |
|
89 CHECK(obj); |
|
90 CHECK(JS_DefineProperty(cx, global, "document", obj, 0)); |
|
91 JS::RootedValue v(cx); |
|
92 EVAL("document.all ? true : false", &v); |
|
93 CHECK_SAME(v, JSVAL_FALSE); |
|
94 EVAL("document.hasOwnProperty('all')", &v); |
|
95 CHECK_SAME(v, JSVAL_TRUE); |
|
96 return true; |
|
97 } |
|
98 END_TEST(testLookup_bug570195) |