Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / js / src / jsapi-tests / testSourcePolicy.cpp
blobb6f9d4a8d78d892b506e310fbf207f9c2d96c865
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "mozilla/Utf8.h" // mozilla::Utf8Unit
7 #include "js/CompilationAndEvaluation.h" // JS::CompileFunction, JS::Evaluate
8 #include "js/GlobalObject.h" // JS_NewGlobalObject
9 #include "js/MemoryFunctions.h"
10 #include "js/SourceText.h" // JS::Source{Ownership,Text}
11 #include "jsapi-tests/tests.h"
12 #include "vm/JSScript.h"
14 BEGIN_TEST(testBug795104) {
15 JS::RealmOptions options;
16 options.behaviors().setDiscardSource(true);
18 JS::RootedObject g(cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr,
19 JS::FireOnNewGlobalHook, options));
20 CHECK(g);
22 JSAutoRealm ar(cx, g);
24 const size_t strLen = 60002;
25 char* s = static_cast<char*>(JS_malloc(cx, strLen));
26 CHECK(s);
28 s[0] = '"';
29 memset(s + 1, 'x', strLen - 2);
30 s[strLen - 1] = '"';
32 JS::SourceText<mozilla::Utf8Unit> srcBuf;
33 CHECK(srcBuf.init(cx, s, strLen, JS::SourceOwnership::Borrowed));
35 JS::CompileOptions opts(cx);
37 // We don't want an rval for our JS::Evaluate call
38 opts.setNoScriptRval(true);
40 JS::RootedValue unused(cx);
41 CHECK(JS::Evaluate(cx, opts, srcBuf, &unused));
43 JS::RootedFunction fun(cx);
44 JS::RootedObjectVector emptyScopeChain(cx);
46 // But when compiling a function we don't want to use no-rval
47 // mode, since it's not supported for functions.
48 opts.setNoScriptRval(false);
50 fun = JS::CompileFunction(cx, emptyScopeChain, opts, "f", 0, nullptr, srcBuf);
51 CHECK(fun);
53 JS_free(cx, s);
55 return true;
57 END_TEST(testBug795104)