js/src/jsapi-tests/testLookup.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial