testFunctionBinding.cpp (2686B)
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 * Test function name binding. 5 */ 6 /* This Source Code Form is subject to the terms of the Mozilla Public 7 * License, v. 2.0. If a copy of the MPL was not distributed with this 8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 9 10 #include "mozilla/Utf8.h" // mozilla::Utf8Unit 11 12 #include "js/CallAndConstruct.h" 13 #include "js/CompilationAndEvaluation.h" // JS::CompileFunction 14 #include "js/EnvironmentChain.h" // JS::EnvironmentChain 15 #include "js/SourceText.h" // JS::Source{Ownership,Text} 16 #include "jsapi-tests/tests.h" 17 #include "util/Text.h" 18 19 using namespace js; 20 21 BEGIN_TEST(test_functionBinding) { 22 RootedFunction fun(cx); 23 24 JS::CompileOptions options(cx); 25 options.setFileAndLine(__FILE__, __LINE__); 26 27 JS::EnvironmentChain emptyEnvChain(cx, JS::SupportUnscopables::No); 28 29 // Named function shouldn't have it's binding. 30 { 31 static const char s1chars[] = "return (typeof s1) == 'undefined';"; 32 33 JS::SourceText<mozilla::Utf8Unit> srcBuf; 34 CHECK(srcBuf.init(cx, s1chars, js_strlen(s1chars), 35 JS::SourceOwnership::Borrowed)); 36 37 fun = JS::CompileFunction(cx, emptyEnvChain, options, "s1", 0, nullptr, 38 srcBuf); 39 CHECK(fun); 40 } 41 42 JS::RootedValueVector args(cx); 43 RootedValue rval(cx); 44 CHECK(JS::Call(cx, UndefinedHandleValue, fun, args, &rval)); 45 CHECK(rval.isBoolean()); 46 CHECK(rval.toBoolean()); 47 48 // Named function shouldn't have `anonymous` binding. 49 { 50 static const char s2chars[] = "return (typeof anonymous) == 'undefined';"; 51 52 JS::SourceText<mozilla::Utf8Unit> srcBuf; 53 CHECK(srcBuf.init(cx, s2chars, js_strlen(s2chars), 54 JS::SourceOwnership::Borrowed)); 55 56 fun = JS::CompileFunction(cx, emptyEnvChain, options, "s2", 0, nullptr, 57 srcBuf); 58 CHECK(fun); 59 } 60 61 CHECK(JS::Call(cx, UndefinedHandleValue, fun, args, &rval)); 62 CHECK(rval.isBoolean()); 63 CHECK(rval.toBoolean()); 64 65 // Anonymous function shouldn't have `anonymous` binding. 66 { 67 static const char s3chars[] = "return (typeof anonymous) == 'undefined';"; 68 69 JS::SourceText<mozilla::Utf8Unit> srcBuf; 70 CHECK(srcBuf.init(cx, s3chars, js_strlen(s3chars), 71 JS::SourceOwnership::Borrowed)); 72 73 fun = JS::CompileFunction(cx, emptyEnvChain, options, nullptr, 0, nullptr, 74 srcBuf); 75 CHECK(fun); 76 } 77 78 CHECK(JS::Call(cx, UndefinedHandleValue, fun, args, &rval)); 79 CHECK(rval.isBoolean()); 80 CHECK(rval.toBoolean()); 81 82 return true; 83 } 84 END_TEST(test_functionBinding)