Debian Codegraph @

js/src/jsapi-tests/testJSEvaluateScript.cpp

raw
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
5 #include "js/CompilationAndEvaluation.h"
6 #include "js/EnvironmentChain.h" // JS::EnvironmentChain
7 #include "js/PropertyAndElement.h" // JS_AlreadyHasOwnProperty, JS_HasProperty
8 #include "js/SourceText.h"
9 #include "jsapi-tests/tests.h"
10 #include "util/Text.h"
11
12 BEGIN_TEST(testJSEvaluateScript) {
13 JS::RootedObject obj(cx, JS_NewPlainObject(cx));
14 CHECK(obj);
15
16 static const char16_t src[] = u"var x = 5;";
17
18 JS::RootedValue retval(cx);
19 JS::CompileOptions opts(cx);
20 JS::EnvironmentChain envChain(cx, JS::SupportUnscopables::No);
21 CHECK(envChain.append(obj));
22
23 JS::SourceText<char16_t> srcBuf;
24 CHECK(srcBuf.init(cx, src, js_strlen(src), JS::SourceOwnership::Borrowed));
25
26 CHECK(JS::Evaluate(cx, envChain, opts.setFileAndLine(__FILE__, __LINE__),
27 srcBuf, &retval));
28
29 bool hasProp = true;
30 CHECK(JS_AlreadyHasOwnProperty(cx, obj, "x", &hasProp));
31 CHECK(hasProp);
32
33 hasProp = false;
34 CHECK(JS_HasProperty(cx, global, "x", &hasProp));
35 CHECK(!hasProp);
36
37 return true;
38 }
39 END_TEST(testJSEvaluateScript)