| 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 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 7 |
|
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include "jsapi.h" |
| 11 |
|
| 12 |
#include "frontend/CompilationStencil.h" |
| 13 |
#include "js/CompilationAndEvaluation.h" |
| 14 |
#include "js/EnvironmentChain.h" // JS::EnvironmentChain |
| 15 |
#include "js/experimental/CompileScript.h" |
| 16 |
#include "js/experimental/JSStencil.h" |
| 17 |
#include "js/Modules.h" |
| 18 |
#include "js/PropertyAndElement.h" // JS_GetProperty, JS_HasOwnProperty, JS_SetProperty |
| 19 |
#include "js/Transcoding.h" |
| 20 |
#include "jsapi-tests/tests.h" |
| 21 |
#include "vm/HelperThreads.h" // js::RunPendingSourceCompressions |
| 22 |
#include "vm/Monitor.h" // js::Monitor, js::AutoLockMonitor |
| 23 |
|
| 24 |
BEGIN_TEST(testStencil_Basic) { |
| 25 |
const char* chars = |
| 26 |
"function f() { return 42; }" |
| 27 |
"f();"; |
| 28 |
auto result = basic_test<char, mozilla::Utf8Unit>(chars); |
| 29 |
CHECK(result); |
| 30 |
|
| 31 |
const char16_t* chars16 = |
| 32 |
u"function f() { return 42; }" |
| 33 |
u"f();"; |
| 34 |
auto result16 = basic_test<char16_t, char16_t>(chars16); |
| 35 |
CHECK(result16); |
| 36 |
|
| 37 |
return true; |
| 38 |
} |
| 39 |
|
| 40 |
template <typename CharT, typename SourceT> |
| 41 |
bool basic_test(const CharT* chars) { |
| 42 |
size_t length = std::char_traits<CharT>::length(chars); |
| 43 |
|
| 44 |
JS::SourceText<SourceT> srcBuf; |
| 45 |
CHECK(srcBuf.init(cx, chars, length, JS::SourceOwnership::Borrowed)); |
| 46 |
|
| 47 |
JS::CompileOptions options(cx); |
| 48 |
RefPtr<JS::Stencil> stencil = |
| 49 |
JS::CompileGlobalScriptToStencil(cx, options, srcBuf); |
| 50 |
CHECK(stencil); |
| 51 |
|
| 52 |
JS::InstantiateOptions instantiateOptions(options); |
| 53 |
JS::RootedScript script( |
| 54 |
cx, JS::InstantiateGlobalStencil(cx, instantiateOptions, stencil)); |
| 55 |
CHECK(script); |
| 56 |
|
| 57 |
JS::RootedValue rval(cx); |
| 58 |
CHECK(JS_ExecuteScript(cx, script, &rval)); |
| 59 |
CHECK(rval.isNumber() && rval.toNumber() == 42); |
| 60 |
|
| 61 |
return true; |
| 62 |
} |
| 63 |
END_TEST(testStencil_Basic) |
| 64 |
|
| 65 |
BEGIN_TEST(testStencil_Module) { |
| 66 |
const char* chars = |
| 67 |
"export function f() { return 42; }" |
| 68 |
"globalThis.x = f();"; |
| 69 |
auto result = basic_test<char, mozilla::Utf8Unit>(chars); |
| 70 |
CHECK(result); |
| 71 |
|
| 72 |
const char16_t* chars16 = |
| 73 |
u"export function f() { return 42; }" |
| 74 |
u"globalThis.x = f();"; |
| 75 |
auto result16 = basic_test<char16_t, char16_t>(chars16); |
| 76 |
CHECK(result16); |
| 77 |
|
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
template <typename CharT, typename SourceT> |
| 82 |
bool basic_test(const CharT* chars) { |
| 83 |
size_t length = std::char_traits<CharT>::length(chars); |
| 84 |
|
| 85 |
JS::SourceText<SourceT> srcBuf; |
| 86 |
CHECK(srcBuf.init(cx, chars, length, JS::SourceOwnership::Borrowed)); |
| 87 |
|
| 88 |
JS::CompileOptions options(cx); |
| 89 |
options.setFile("testStencil_Module"); |
| 90 |
|
| 91 |
RefPtr<JS::Stencil> stencil = |
| 92 |
JS::CompileModuleScriptToStencil(cx, options, srcBuf); |
| 93 |
CHECK(stencil); |
| 94 |
|
| 95 |
JS::InstantiateOptions instantiateOptions(options); |
| 96 |
JS::RootedObject moduleObject( |
| 97 |
cx, JS::InstantiateModuleStencil(cx, instantiateOptions, stencil)); |
| 98 |
CHECK(moduleObject); |
| 99 |
|
| 100 |
// Link and evaluate the module graph. The link step used to be call |
| 101 |
// "instantiate" but is unrelated to the concept in Stencil with same name. |
| 102 |
JS::RootedValue rval(cx); |
| 103 |
CHECK(JS::ModuleLink(cx, moduleObject)); |
| 104 |
CHECK(JS::ModuleEvaluate(cx, moduleObject, &rval)); |
| 105 |
CHECK(!rval.isUndefined()); |
| 106 |
|
| 107 |
js::RunJobs(cx); |
| 108 |
CHECK(JS_GetProperty(cx, global, "x", &rval)); |
| 109 |
CHECK(rval.isNumber() && rval.toNumber() == 42); |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
END_TEST(testStencil_Module) |
| 114 |
|
| 115 |
BEGIN_TEST(testStencil_NonSyntactic) { |
| 116 |
const char* chars = |
| 117 |
"function f() { return x; }" |
| 118 |
"f();"; |
| 119 |
|
| 120 |
JS::SourceText<mozilla::Utf8Unit> srcBuf; |
| 121 |
CHECK(srcBuf.init(cx, chars, strlen(chars), JS::SourceOwnership::Borrowed)); |
| 122 |
|
| 123 |
JS::CompileOptions options(cx); |
| 124 |
options.setNonSyntacticScope(true); |
| 125 |
|
| 126 |
RefPtr<JS::Stencil> stencil = |
| 127 |
JS::CompileGlobalScriptToStencil(cx, options, srcBuf); |
| 128 |
CHECK(stencil); |
| 129 |
|
| 130 |
JS::InstantiateOptions instantiateOptions(options); |
| 131 |
JS::RootedScript script( |
| 132 |
cx, JS::InstantiateGlobalStencil(cx, instantiateOptions, stencil)); |
| 133 |
CHECK(script); |
| 134 |
|
| 135 |
JS::RootedObject obj(cx, JS_NewPlainObject(cx)); |
| 136 |
JS::RootedValue val(cx, JS::Int32Value(42)); |
| 137 |
CHECK(obj); |
| 138 |
CHECK(JS_SetProperty(cx, obj, "x", val)); |
| 139 |
|
| 140 |
JS::EnvironmentChain envChain(cx, JS::SupportUnscopables::No); |
| 141 |
CHECK(envChain.append(obj)); |
| 142 |
|
| 143 |
JS::RootedValue rval(cx); |
| 144 |
CHECK(JS_ExecuteScript(cx, envChain, script, &rval)); |
| 145 |
CHECK(rval.isNumber() && rval.toNumber() == 42); |
| 146 |
|
| 147 |
return true; |
| 148 |
} |
| 149 |
END_TEST(testStencil_NonSyntactic) |
| 150 |
|
| 151 |
BEGIN_TEST(testStencil_MultiGlobal) { |
| 152 |
const char* chars = |
| 153 |
"/**************************************/" |
| 154 |
"/**************************************/" |
| 155 |
"/**************************************/" |
| 156 |
"/**************************************/" |
| 157 |
"/**************************************/" |
| 158 |
"/**************************************/" |
| 159 |
"function f() { return 42; }" |
| 160 |
"f();"; |
| 161 |
|
| 162 |
JS::SourceText<mozilla::Utf8Unit> srcBuf; |
| 163 |
CHECK(srcBuf.init(cx, chars, strlen(chars), JS::SourceOwnership::Borrowed)); |
| 164 |
|
| 165 |
JS::CompileOptions options(cx); |
| 166 |
RefPtr<JS::Stencil> stencil = |
| 167 |
JS::CompileGlobalScriptToStencil(cx, options, srcBuf); |
| 168 |
CHECK(stencil); |
| 169 |
|
| 170 |
CHECK(RunInNewGlobal(cx, stencil)); |
| 171 |
CHECK(RunInNewGlobal(cx, stencil)); |
| 172 |
CHECK(RunInNewGlobal(cx, stencil)); |
| 173 |
|
| 174 |
// Start any pending SourceCompressionTasks now to confirm nothing fell apart |
| 175 |
// when using a JS::Stencil multiple times. |
| 176 |
CHECK(strlen(chars) > js::ScriptSource::MinimumCompressibleLength); |
| 177 |
js::RunPendingSourceCompressions(cx->runtime()); |
| 178 |
|
| 179 |
return true; |
| 180 |
} |
| 181 |
bool RunInNewGlobal(JSContext* cx, RefPtr<JS::Stencil> stencil) { |
| 182 |
JS::RootedObject otherGlobal(cx, createGlobal()); |
| 183 |
CHECK(otherGlobal); |
| 184 |
|
| 185 |
JSAutoRealm ar(cx, otherGlobal); |
| 186 |
|
| 187 |
JS::InstantiateOptions instantiateOptions; |
| 188 |
JS::RootedScript script( |
| 189 |
cx, JS::InstantiateGlobalStencil(cx, instantiateOptions, stencil)); |
| 190 |
CHECK(script); |
| 191 |
|
| 192 |
JS::RootedValue rval(cx); |
| 193 |
CHECK(JS_ExecuteScript(cx, script, &rval)); |
| 194 |
CHECK(rval.isNumber() && rval.toNumber() == 42); |
| 195 |
|
| 196 |
return true; |
| 197 |
} |
| 198 |
END_TEST(testStencil_MultiGlobal) |
| 199 |
|
| 200 |
BEGIN_TEST(testStencil_Transcode) { |
| 201 |
JS::SetProcessBuildIdOp(TestGetBuildId); |
| 202 |
|
| 203 |
JS::TranscodeBuffer buffer; |
| 204 |
|
| 205 |
{ |
| 206 |
const char* chars = |
| 207 |
"function f() { return 42; }" |
| 208 |
"f();"; |
| 209 |
|
| 210 |
JS::SourceText<mozilla::Utf8Unit> srcBuf; |
| 211 |
CHECK(srcBuf.init(cx, chars, strlen(chars), JS::SourceOwnership::Borrowed)); |
| 212 |
|
| 213 |
JS::CompileOptions options(cx); |
| 214 |
RefPtr<JS::Stencil> stencil = |
| 215 |
JS::CompileGlobalScriptToStencil(cx, options, srcBuf); |
| 216 |
CHECK(stencil); |
| 217 |
|
| 218 |
// Encode Stencil to XDR |
| 219 |
JS::TranscodeResult res = JS::EncodeStencil(cx, stencil, buffer); |
| 220 |
CHECK(res == JS::TranscodeResult::Ok); |
| 221 |
CHECK(!buffer.empty()); |
| 222 |
|
| 223 |
// Instantiate and Run |
| 224 |
JS::InstantiateOptions instantiateOptions(options); |
| 225 |
JS::RootedScript script( |
| 226 |
cx, JS::InstantiateGlobalStencil(cx, instantiateOptions, stencil)); |
| 227 |
JS::RootedValue rval(cx); |
| 228 |
CHECK(script); |
| 229 |
CHECK(JS_ExecuteScript(cx, script, &rval)); |
| 230 |
CHECK(rval.isNumber() && rval.toNumber() == 42); |
| 231 |
} |
| 232 |
|
| 233 |
// Create a new global |
| 234 |
CHECK(createGlobal()); |
| 235 |
JSAutoRealm ar(cx, global); |
| 236 |
|
| 237 |
// Confirm it doesn't have the old code |
| 238 |
bool found = false; |
| 239 |
CHECK(JS_HasOwnProperty(cx, global, "f", &found)); |
| 240 |
CHECK(!found); |
| 241 |
|
| 242 |
{ |
| 243 |
// Decode the stencil into new range |
| 244 |
RefPtr<JS::Stencil> stencil; |
| 245 |
|
| 246 |
{ |
| 247 |
JS::DecodeOptions decodeOptions; |
| 248 |
JS::TranscodeRange range(buffer.begin(), buffer.length()); |
| 249 |
JS::TranscodeResult res = |
| 250 |
JS::DecodeStencil(cx, decodeOptions, range, getter_AddRefs(stencil)); |
| 251 |
CHECK(res == JS::TranscodeResult::Ok); |
| 252 |
} |
| 253 |
|
| 254 |
{ |
| 255 |
JS::FrontendContext* fc = JS::NewFrontendContext(); |
| 256 |
JS::DecodeOptions decodeOptions; |
| 257 |
JS::TranscodeRange range(buffer.begin(), buffer.length()); |
| 258 |
JS::TranscodeResult res = |
| 259 |
JS::DecodeStencil(fc, decodeOptions, range, getter_AddRefs(stencil)); |
| 260 |
CHECK(res == JS::TranscodeResult::Ok); |
| 261 |
JS::DestroyFrontendContext(fc); |
| 262 |
} |
| 263 |
|
| 264 |
// Delete the buffer to verify that the decoded stencil has no dependency |
| 265 |
// to the buffer. |
| 266 |
memset(buffer.begin(), 0, buffer.length()); |
| 267 |
buffer.clear(); |
| 268 |
|
| 269 |
// Instantiate and Run |
| 270 |
JS::InstantiateOptions instantiateOptions; |
| 271 |
JS::RootedScript script( |
| 272 |
cx, JS::InstantiateGlobalStencil(cx, instantiateOptions, stencil)); |
| 273 |
stencil = nullptr; |
| 274 |
JS::RootedValue rval(cx); |
| 275 |
CHECK(script); |
| 276 |
CHECK(JS_ExecuteScript(cx, script, &rval)); |
| 277 |
CHECK(rval.isNumber() && rval.toNumber() == 42); |
| 278 |
} |
| 279 |
|
| 280 |
return true; |
| 281 |
} |
| 282 |
static bool TestGetBuildId(JS::BuildIdCharVector* buildId) { |
| 283 |
const char buildid[] = "testXDR"; |
| 284 |
return buildId->append(buildid, sizeof(buildid)); |
| 285 |
} |
| 286 |
END_TEST(testStencil_Transcode) |
| 287 |
|
| 288 |
BEGIN_TEST(testStencil_TranscodeBorrowing) { |
| 289 |
JS::SetProcessBuildIdOp(TestGetBuildId); |
| 290 |
|
| 291 |
JS::TranscodeBuffer buffer; |
| 292 |
|
| 293 |
{ |
| 294 |
const char* chars = |
| 295 |
"function f() { return 42; }" |
| 296 |
"f();"; |
| 297 |
|
| 298 |
JS::SourceText<mozilla::Utf8Unit> srcBuf; |
| 299 |
CHECK(srcBuf.init(cx, chars, strlen(chars), JS::SourceOwnership::Borrowed)); |
| 300 |
|
| 301 |
JS::CompileOptions options(cx); |
| 302 |
RefPtr<JS::Stencil> stencil = |
| 303 |
JS::CompileGlobalScriptToStencil(cx, options, srcBuf); |
| 304 |
CHECK(stencil); |
| 305 |
|
| 306 |
// Encode Stencil to XDR |
| 307 |
JS::TranscodeResult res = JS::EncodeStencil(cx, stencil, buffer); |
| 308 |
CHECK(res == JS::TranscodeResult::Ok); |
| 309 |
CHECK(!buffer.empty()); |
| 310 |
} |
| 311 |
|
| 312 |
JS::RootedScript script(cx); |
| 313 |
{ |
| 314 |
JS::TranscodeRange range(buffer.begin(), buffer.length()); |
| 315 |
JS::DecodeOptions decodeOptions; |
| 316 |
decodeOptions.borrowBuffer = true; |
| 317 |
RefPtr<JS::Stencil> stencil; |
| 318 |
JS::TranscodeResult res = |
| 319 |
JS::DecodeStencil(cx, decodeOptions, range, getter_AddRefs(stencil)); |
| 320 |
CHECK(res == JS::TranscodeResult::Ok); |
| 321 |
|
| 322 |
JS::InstantiateOptions instantiateOptions; |
| 323 |
script = JS::InstantiateGlobalStencil(cx, instantiateOptions, stencil); |
| 324 |
CHECK(script); |
| 325 |
} |
| 326 |
|
| 327 |
// Delete the buffer to verify that the instantiated script has no dependency |
| 328 |
// to the buffer. |
| 329 |
memset(buffer.begin(), 0, buffer.length()); |
| 330 |
buffer.clear(); |
| 331 |
|
| 332 |
JS::RootedValue rval(cx); |
| 333 |
CHECK(JS_ExecuteScript(cx, script, &rval)); |
| 334 |
CHECK(rval.isNumber() && rval.toNumber() == 42); |
| 335 |
|
| 336 |
return true; |
| 337 |
} |
| 338 |
static bool TestGetBuildId(JS::BuildIdCharVector* buildId) { |
| 339 |
const char buildid[] = "testXDR"; |
| 340 |
return buildId->append(buildid, sizeof(buildid)); |
| 341 |
} |
| 342 |
END_TEST(testStencil_TranscodeBorrowing) |