Bug 1832850 - Part 5: Move the allocateObject definition into gc/Nursery.h r=jandem
[gecko.git] / js / src / jsapi-tests / testFunctionBinding.cpp
blob6a69e323491469590d376b2a909e3babaa6905fe
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:
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/. */
10 #include "mozilla/Utf8.h" // mozilla::Utf8Unit
12 #include "js/CallAndConstruct.h"
13 #include "js/CompilationAndEvaluation.h" // JS::CompileFunction
14 #include "js/SourceText.h" // JS::Source{Ownership,Text}
15 #include "jsapi-tests/tests.h"
16 #include "util/Text.h"
18 using namespace js;
20 BEGIN_TEST(test_functionBinding) {
21 RootedFunction fun(cx);
23 JS::CompileOptions options(cx);
24 options.setFileAndLine(__FILE__, __LINE__);
26 JS::RootedObjectVector emptyScopeChain(cx);
28 // Named function shouldn't have it's binding.
30 static const char s1chars[] = "return (typeof s1) == 'undefined';";
32 JS::SourceText<mozilla::Utf8Unit> srcBuf;
33 CHECK(srcBuf.init(cx, s1chars, js_strlen(s1chars),
34 JS::SourceOwnership::Borrowed));
36 fun = JS::CompileFunction(cx, emptyScopeChain, options, "s1", 0, nullptr,
37 srcBuf);
38 CHECK(fun);
41 JS::RootedValueVector args(cx);
42 RootedValue rval(cx);
43 CHECK(JS::Call(cx, UndefinedHandleValue, fun, args, &rval));
44 CHECK(rval.isBoolean());
45 CHECK(rval.toBoolean());
47 // Named function shouldn't have `anonymous` binding.
49 static const char s2chars[] = "return (typeof anonymous) == 'undefined';";
51 JS::SourceText<mozilla::Utf8Unit> srcBuf;
52 CHECK(srcBuf.init(cx, s2chars, js_strlen(s2chars),
53 JS::SourceOwnership::Borrowed));
55 fun = JS::CompileFunction(cx, emptyScopeChain, options, "s2", 0, nullptr,
56 srcBuf);
57 CHECK(fun);
60 CHECK(JS::Call(cx, UndefinedHandleValue, fun, args, &rval));
61 CHECK(rval.isBoolean());
62 CHECK(rval.toBoolean());
64 // Anonymous function shouldn't have `anonymous` binding.
66 static const char s3chars[] = "return (typeof anonymous) == 'undefined';";
68 JS::SourceText<mozilla::Utf8Unit> srcBuf;
69 CHECK(srcBuf.init(cx, s3chars, js_strlen(s3chars),
70 JS::SourceOwnership::Borrowed));
72 fun = JS::CompileFunction(cx, emptyScopeChain, options, nullptr, 0, nullptr,
73 srcBuf);
74 CHECK(fun);
77 CHECK(JS::Call(cx, UndefinedHandleValue, fun, args, &rval));
78 CHECK(rval.isBoolean());
79 CHECK(rval.toBoolean());
81 return true;
83 END_TEST(test_functionBinding)