testForceLexicalInitialization.cpp (1496B)
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/. */ 7 8 #include "jsfriendapi.h" 9 #include "jsapi-tests/tests.h" 10 #include "vm/EnvironmentObject.h" 11 12 BEGIN_TEST(testForceLexicalInitialization) { 13 // Attach an uninitialized lexical to a scope and ensure that it's 14 // set to undefined 15 JS::Rooted<js::GlobalObject*> g(cx, cx->global()); 16 JS::Rooted<js::GlobalLexicalEnvironmentObject*> env( 17 cx, js::GlobalLexicalEnvironmentObject::create(cx, g)); 18 19 JS::RootedValue uninitialized(cx, JS::MagicValue(JS_UNINITIALIZED_LEXICAL)); 20 JS::Rooted<js::PropertyName*> name(cx, 21 Atomize(cx, "foopi", 4)->asPropertyName()); 22 JS::RootedId id(cx, NameToId(name)); 23 unsigned attrs = JSPROP_ENUMERATE | JSPROP_PERMANENT; 24 25 CHECK(NativeDefineDataProperty(cx, env, id, uninitialized, attrs)); 26 27 // Verify that "foopi" is uninitialized 28 const JS::Value v = env->getSlot(env->lookup(cx, id)->slot()); 29 CHECK(v.isMagic(JS_UNINITIALIZED_LEXICAL)); 30 31 ForceLexicalInitialization(cx, env); 32 33 // Verify that "foopi" has been initialized to undefined 34 const JS::Value v2 = env->getSlot(env->lookup(cx, id)->slot()); 35 CHECK(v2.isUndefined()); 36 37 return true; 38 } 39 END_TEST(testForceLexicalInitialization)