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/EnvironmentChain.h" // JS::EnvironmentChain
9 #include "js/GlobalObject.h" // JS_NewGlobalObject
10 #include "js/MemoryFunctions.h"
11 #include "js/SourceText.h" // JS::Source{Ownership,Text}
12 #include "jsapi-tests/tests.h"
13 #include "vm/JSScript.h"
15 BEGIN_TEST(testBug795104
) {
16 JS::RealmOptions options
;
17 options
.behaviors().setDiscardSource(true);
19 JS::RootedObject
g(cx
, JS_NewGlobalObject(cx
, getGlobalClass(), nullptr,
20 JS::FireOnNewGlobalHook
, options
));
23 JSAutoRealm
ar(cx
, g
);
25 const size_t strLen
= 60002;
26 char* s
= static_cast<char*>(JS_malloc(cx
, strLen
));
30 memset(s
+ 1, 'x', strLen
- 2);
33 JS::SourceText
<mozilla::Utf8Unit
> srcBuf
;
34 CHECK(srcBuf
.init(cx
, s
, strLen
, JS::SourceOwnership::Borrowed
));
36 JS::CompileOptions
opts(cx
);
38 // We don't want an rval for our JS::Evaluate call
39 opts
.setNoScriptRval(true);
41 JS::RootedValue
unused(cx
);
42 CHECK(JS::Evaluate(cx
, opts
, srcBuf
, &unused
));
44 JS::RootedFunction
fun(cx
);
45 JS::EnvironmentChain
emptyEnvChain(cx
, JS::SupportUnscopables::No
);
47 // But when compiling a function we don't want to use no-rval
48 // mode, since it's not supported for functions.
49 opts
.setNoScriptRval(false);
51 fun
= JS::CompileFunction(cx
, emptyEnvChain
, opts
, "f", 0, nullptr, srcBuf
);
58 END_TEST(testBug795104
)