Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / js / src / jsapi-tests / testLookup.cpp
blob24a1afa6fd40f89ef2a9d089ec387e72cb906ba0
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
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/. */
8 #include "js/PropertyAndElement.h" // JS_DefineProperty, JS_DefinePropertyById, JS_GetProperty
9 #include "jsapi-tests/tests.h"
10 #include "vm/JSFunction.h" // for js::IsInternalFunctionObject
12 #include "vm/JSObject-inl.h"
14 BEGIN_TEST(testLookup_bug522590) {
15 // Define a function that makes method-bearing objects.
16 JS::RootedValue x(cx);
17 EXEC("function mkobj() { return {f: function () {return 2;}} }");
19 // Calling mkobj() multiple times must create multiple functions in ES5.
20 EVAL("mkobj().f !== mkobj().f", &x);
21 CHECK(x.isTrue());
23 // Now make x.f a method.
24 EVAL("mkobj()", &x);
25 JS::RootedObject xobj(cx, x.toObjectOrNull());
27 // This lookup must not return an internal function object.
28 JS::RootedValue r(cx);
29 CHECK(JS_GetProperty(cx, xobj, "f", &r));
30 CHECK(r.isObject());
31 JSObject* funobj = &r.toObject();
32 CHECK(funobj->is<JSFunction>());
33 CHECK(!js::IsInternalFunctionObject(*funobj));
35 return true;
37 END_TEST(testLookup_bug522590)
39 static const JSClass DocumentAllClass = {"DocumentAll",
40 JSCLASS_EMULATES_UNDEFINED};
42 bool document_resolve(JSContext* cx, JS::HandleObject obj, JS::HandleId id,
43 bool* resolvedp) {
44 // If id is "all", resolve document.all=true.
45 JS::RootedValue v(cx);
46 if (!JS_IdToValue(cx, id, &v)) {
47 return false;
50 if (v.isString()) {
51 JSString* str = v.toString();
52 JSLinearString* linearStr = JS_EnsureLinearString(cx, str);
53 if (!linearStr) {
54 return false;
56 if (JS_LinearStringEqualsLiteral(linearStr, "all")) {
57 JS::Rooted<JSObject*> docAll(cx, JS_NewObject(cx, &DocumentAllClass));
58 if (!docAll) {
59 return false;
62 JS::Rooted<JS::Value> allValue(cx, JS::ObjectValue(*docAll));
63 if (!JS_DefinePropertyById(cx, obj, id, allValue, JSPROP_RESOLVING)) {
64 return false;
67 *resolvedp = true;
68 return true;
72 *resolvedp = false;
73 return true;
76 static const JSClassOps document_classOps = {
77 nullptr, // addProperty
78 nullptr, // delProperty
79 nullptr, // enumerate
80 nullptr, // newEnumerate
81 document_resolve, // resolve
82 nullptr, // mayResolve
83 nullptr, // finalize
84 nullptr, // call
85 nullptr, // construct
86 nullptr, // trace
89 static const JSClass document_class = {"document", 0, &document_classOps};
91 BEGIN_TEST(testLookup_bug570195) {
92 JS::RootedObject obj(cx, JS_NewObject(cx, &document_class));
93 CHECK(obj);
94 CHECK(JS_DefineProperty(cx, global, "document", obj, 0));
95 JS::RootedValue v(cx);
96 EVAL("document.all ? true : false", &v);
97 CHECK(v.isFalse());
98 EVAL("document.hasOwnProperty('all')", &v);
99 CHECK(v.isTrue());
100 return true;
102 END_TEST(testLookup_bug570195)