| 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 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* Same-thread compilation and evaluation APIs. */ |
| 8 |
|
| 9 |
#include "js/CompilationAndEvaluation.h" |
| 10 |
|
| 11 |
#include "mozilla/Maybe.h" // mozilla::None, mozilla::Some |
| 12 |
#include "mozilla/Utf8.h" // mozilla::Utf8Unit |
| 13 |
|
| 14 |
#include <utility> // std::move |
| 15 |
|
| 16 |
#include "jsapi.h" // JS_WrapValue |
| 17 |
#include "jstypes.h" // JS_PUBLIC_API |
| 18 |
|
| 19 |
#include "debugger/DebugAPI.h" |
| 20 |
#include "frontend/BytecodeCompiler.h" // frontend::{CompileGlobalScript, CompileStandaloneFunction, CompileStandaloneFunctionInNonSyntacticScope} |
| 21 |
#include "frontend/CompilationStencil.h" // for frontened::{CompilationStencil, BorrowingCompilationStencil, CompilationGCOutput, InitialStencilAndDelazifications} |
| 22 |
#include "frontend/FrontendContext.h" // js::AutoReportFrontendContext |
| 23 |
#include "frontend/Parser.h" // frontend::Parser, frontend::ParseGoal |
| 24 |
#include "frontend/StencilXdr.h" // js::XDRStencilEncoder |
| 25 |
#include "js/CharacterEncoding.h" // JS::UTF8Chars, JS::ConstUTF8CharsZ, JS::UTF8CharsToNewTwoByteCharsZ |
| 26 |
#include "js/ColumnNumber.h" // JS::ColumnNumberOneOrigin |
| 27 |
#include "js/EnvironmentChain.h" // JS::EnvironmentChain |
| 28 |
#include "js/experimental/JSStencil.h" // JS::Stencil |
| 29 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 30 |
#include "js/RootingAPI.h" // JS::Rooted |
| 31 |
#include "js/SourceText.h" // JS::SourceText |
| 32 |
#include "js/Transcoding.h" // JS::TranscodeBuffer, JS::IsTranscodeFailureResult |
| 33 |
#include "js/TypeDecls.h" // JS::HandleObject, JS::MutableHandleScript |
| 34 |
#include "js/Utility.h" // js::MallocArena, JS::UniqueTwoByteChars |
| 35 |
#include "js/Value.h" // JS::Value |
| 36 |
#include "util/CompleteFile.h" // js::FileContents, js::ReadCompleteFile |
| 37 |
#include "util/Identifier.h" // js::IsIdentifier |
| 38 |
#include "util/StringBuilder.h" // js::StringBuilder |
| 39 |
#include "vm/EnvironmentObject.h" // js::CreateNonSyntacticEnvironmentChain |
| 40 |
#include "vm/ErrorReporting.h" // js::ErrorMetadata, js::ReportCompileErrorLatin1 |
| 41 |
#include "vm/Interpreter.h" // js::Execute |
| 42 |
#include "vm/JSContext.h" // JSContext |
| 43 |
#include "vm/JSScript.h" // js::ScriptSourceObject |
| 44 |
#include "vm/Xdr.h" // XDRResult |
| 45 |
|
| 46 |
#include "vm/JSContext-inl.h" // JSContext::check |
| 47 |
|
| 48 |
using mozilla::Utf8Unit; |
| 49 |
|
| 50 |
using JS::CompileOptions; |
| 51 |
using JS::HandleObject; |
| 52 |
using JS::ReadOnlyCompileOptions; |
| 53 |
using JS::SourceOwnership; |
| 54 |
using JS::SourceText; |
| 55 |
using JS::UniqueTwoByteChars; |
| 56 |
using JS::UTF8Chars; |
| 57 |
using JS::UTF8CharsToNewTwoByteCharsZ; |
| 58 |
|
| 59 |
using namespace js; |
| 60 |
|
| 61 |
JS_PUBLIC_API void JS::detail::ReportSourceTooLong(JSContext* cx) { |
| 62 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 63 |
JSMSG_SOURCE_TOO_LONG); |
| 64 |
} |
| 65 |
|
| 66 |
static void ReportSourceTooLongImpl(JS::FrontendContext* fc, ...) { |
| 67 |
va_list args; |
| 68 |
va_start(args, fc); |
| 69 |
|
| 70 |
js::ErrorMetadata metadata; |
| 71 |
metadata.filename = JS::ConstUTF8CharsZ("<unknown>"); |
| 72 |
metadata.lineNumber = 0; |
| 73 |
metadata.columnNumber = JS::ColumnNumberOneOrigin(); |
| 74 |
metadata.lineLength = 0; |
| 75 |
metadata.tokenOffset = 0; |
| 76 |
metadata.isMuted = false; |
| 77 |
|
| 78 |
js::ReportCompileErrorLatin1VA(fc, std::move(metadata), nullptr, |
| 79 |
JSMSG_SOURCE_TOO_LONG, &args); |
| 80 |
|
| 81 |
va_end(args); |
| 82 |
} |
| 83 |
|
| 84 |
JS_PUBLIC_API void JS::detail::ReportSourceTooLong(JS::FrontendContext* fc) { |
| 85 |
ReportSourceTooLongImpl(fc); |
| 86 |
} |
| 87 |
|
| 88 |
template <typename Unit> |
| 89 |
static JSScript* CompileSourceBuffer(JSContext* cx, |
| 90 |
const ReadOnlyCompileOptions& options, |
| 91 |
SourceText<Unit>& srcBuf) { |
| 92 |
ScopeKind scopeKind = |
| 93 |
options.nonSyntacticScope ? ScopeKind::NonSyntactic : ScopeKind::Global; |
| 94 |
|
| 95 |
MOZ_ASSERT(!cx->zone()->isAtomsZone()); |
| 96 |
AssertHeapIsIdle(); |
| 97 |
CHECK_THREAD(cx); |
| 98 |
|
| 99 |
JS::Rooted<JSScript*> script(cx); |
| 100 |
{ |
| 101 |
AutoReportFrontendContext fc(cx); |
| 102 |
script = frontend::CompileGlobalScript(cx, &fc, options, srcBuf, scopeKind); |
| 103 |
} |
| 104 |
return script; |
| 105 |
} |
| 106 |
|
| 107 |
JSScript* JS::Compile(JSContext* cx, const ReadOnlyCompileOptions& options, |
| 108 |
SourceText<char16_t>& srcBuf) { |
| 109 |
return CompileSourceBuffer(cx, options, srcBuf); |
| 110 |
} |
| 111 |
|
| 112 |
JSScript* JS::Compile(JSContext* cx, const ReadOnlyCompileOptions& options, |
| 113 |
SourceText<Utf8Unit>& srcBuf) { |
| 114 |
return CompileSourceBuffer(cx, options, srcBuf); |
| 115 |
} |
| 116 |
|
| 117 |
static bool StartCollectingDelazifications(JSContext* cx, |
| 118 |
JS::Handle<ScriptSourceObject*> sso, |
| 119 |
JS::Stencil* stencil, |
| 120 |
bool& alreadyStarted) { |
| 121 |
if (sso->isCollectingDelazifications()) { |
| 122 |
alreadyStarted = true; |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
alreadyStarted = false; |
| 127 |
|
| 128 |
// We don't support asm.js in XDR. |
| 129 |
// Failures are reported by the FinishCollectingDelazifications function |
| 130 |
// below. |
| 131 |
if (stencil->getInitial()->hasAsmJS()) { |
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
if (!sso->maybeGetStencils()) { |
| 136 |
RefPtr stencils = stencil; |
| 137 |
sso->setStencils(stencils.forget()); |
| 138 |
} else { |
| 139 |
MOZ_ASSERT(sso->maybeGetStencils() == stencil); |
| 140 |
} |
| 141 |
sso->setCollectingDelazifications(); |
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
JS_PUBLIC_API bool JS::StartCollectingDelazifications( |
| 146 |
JSContext* cx, JS::Handle<JSScript*> script, JS::Stencil* stencil, |
| 147 |
bool& alreadyStarted) { |
| 148 |
JS::Rooted<ScriptSourceObject*> sso(cx, script->sourceObject()); |
| 149 |
return ::StartCollectingDelazifications(cx, sso, stencil, alreadyStarted); |
| 150 |
} |
| 151 |
|
| 152 |
JS_PUBLIC_API bool JS::StartCollectingDelazifications( |
| 153 |
JSContext* cx, JS::Handle<JSObject*> module, JS::Stencil* stencil, |
| 154 |
bool& alreadyStarted) { |
| 155 |
JS::Rooted<ScriptSourceObject*> sso( |
| 156 |
cx, module->as<ModuleObject>().scriptSourceObject()); |
| 157 |
return ::StartCollectingDelazifications(cx, sso, stencil, alreadyStarted); |
| 158 |
} |
| 159 |
|
| 160 |
static bool FinishCollectingDelazifications(JSContext* cx, |
| 161 |
JS::Handle<ScriptSourceObject*> sso, |
| 162 |
JS::TranscodeBuffer& buffer) { |
| 163 |
if (!sso->isCollectingDelazifications()) { |
| 164 |
JS_ReportErrorASCII(cx, "Not collecting delazifications"); |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
RefPtr<frontend::InitialStencilAndDelazifications> stencils = |
| 169 |
sso->maybeGetStencils(); |
| 170 |
sso->unsetCollectingDelazifications(); |
| 171 |
|
| 172 |
AutoReportFrontendContext fc(cx); |
| 173 |
UniquePtr<frontend::CompilationStencil> stencilHolder; |
| 174 |
const frontend::CompilationStencil* stencil; |
| 175 |
|
| 176 |
if (stencils->canLazilyParse()) { |
| 177 |
stencilHolder.reset(stencils->getMerged(&fc)); |
| 178 |
if (!stencilHolder) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
stencil = stencilHolder.get(); |
| 182 |
} else { |
| 183 |
stencil = stencils->getInitial(); |
| 184 |
} |
| 185 |
|
| 186 |
XDRStencilEncoder encoder(&fc, buffer); |
| 187 |
XDRResult res = encoder.codeStencil(sso->source(), *stencil); |
| 188 |
if (res.isErr()) { |
| 189 |
if (JS::IsTranscodeFailureResult(res.unwrapErr())) { |
| 190 |
fc.clearAutoReport(); |
| 191 |
JS_ReportErrorASCII(cx, "XDR encoding failure"); |
| 192 |
} |
| 193 |
return false; |
| 194 |
} |
| 195 |
return true; |
| 196 |
} |
| 197 |
|
| 198 |
static bool FinishCollectingDelazifications(JSContext* cx, |
| 199 |
JS::Handle<ScriptSourceObject*> sso, |
| 200 |
JS::Stencil** stencilOut) { |
| 201 |
if (!sso->isCollectingDelazifications()) { |
| 202 |
JS_ReportErrorASCII(cx, "Not collecting delazifications"); |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
RefPtr<frontend::InitialStencilAndDelazifications> stencils = |
| 207 |
sso->maybeGetStencils(); |
| 208 |
sso->unsetCollectingDelazifications(); |
| 209 |
|
| 210 |
stencils.forget(stencilOut); |
| 211 |
return true; |
| 212 |
} |
| 213 |
|
| 214 |
JS_PUBLIC_API bool JS::FinishCollectingDelazifications( |
| 215 |
JSContext* cx, JS::HandleScript script, JS::TranscodeBuffer& buffer) { |
| 216 |
JS::Rooted<ScriptSourceObject*> sso(cx, script->sourceObject()); |
| 217 |
return ::FinishCollectingDelazifications(cx, sso, buffer); |
| 218 |
} |
| 219 |
|
| 220 |
JS_PUBLIC_API bool JS::FinishCollectingDelazifications( |
| 221 |
JSContext* cx, JS::HandleScript script, JS::Stencil** stencilOut) { |
| 222 |
JS::Rooted<ScriptSourceObject*> sso(cx, script->sourceObject()); |
| 223 |
return ::FinishCollectingDelazifications(cx, sso, stencilOut); |
| 224 |
} |
| 225 |
|
| 226 |
JS_PUBLIC_API bool JS::FinishCollectingDelazifications( |
| 227 |
JSContext* cx, JS::Handle<JSObject*> module, JS::TranscodeBuffer& buffer) { |
| 228 |
JS::Rooted<ScriptSourceObject*> sso( |
| 229 |
cx, module->as<ModuleObject>().scriptSourceObject()); |
| 230 |
return ::FinishCollectingDelazifications(cx, sso, buffer); |
| 231 |
} |
| 232 |
|
| 233 |
JS_PUBLIC_API void JS::AbortCollectingDelazifications(JS::HandleScript script) { |
| 234 |
if (!script) { |
| 235 |
return; |
| 236 |
} |
| 237 |
script->sourceObject()->unsetCollectingDelazifications(); |
| 238 |
} |
| 239 |
|
| 240 |
JS_PUBLIC_API void JS::AbortCollectingDelazifications( |
| 241 |
JS::Handle<JSObject*> module) { |
| 242 |
module->as<ModuleObject>() |
| 243 |
.scriptSourceObject() |
| 244 |
->unsetCollectingDelazifications(); |
| 245 |
} |
| 246 |
|
| 247 |
JSScript* JS::CompileUtf8File(JSContext* cx, |
| 248 |
const ReadOnlyCompileOptions& options, |
| 249 |
FILE* file) { |
| 250 |
FileContents buffer(cx); |
| 251 |
if (!ReadCompleteFile(cx, file, buffer)) { |
| 252 |
return nullptr; |
| 253 |
} |
| 254 |
|
| 255 |
SourceText<Utf8Unit> srcBuf; |
| 256 |
if (!srcBuf.init(cx, reinterpret_cast<const char*>(buffer.begin()), |
| 257 |
buffer.length(), SourceOwnership::Borrowed)) { |
| 258 |
return nullptr; |
| 259 |
} |
| 260 |
|
| 261 |
return CompileSourceBuffer(cx, options, srcBuf); |
| 262 |
} |
| 263 |
|
| 264 |
JSScript* JS::CompileUtf8Path(JSContext* cx, |
| 265 |
const ReadOnlyCompileOptions& optionsArg, |
| 266 |
const char* filename) { |
| 267 |
AutoFile file; |
| 268 |
if (!file.open(cx, filename)) { |
| 269 |
return nullptr; |
| 270 |
} |
| 271 |
|
| 272 |
CompileOptions options(cx, optionsArg); |
| 273 |
options.setFileAndLine(filename, 1); |
| 274 |
return CompileUtf8File(cx, options, file.fp()); |
| 275 |
} |
| 276 |
|
| 277 |
JS_PUBLIC_API bool JS_Utf8BufferIsCompilableUnit(JSContext* cx, |
| 278 |
HandleObject obj, |
| 279 |
const char* utf8, |
| 280 |
size_t length) { |
| 281 |
AssertHeapIsIdle(); |
| 282 |
CHECK_THREAD(cx); |
| 283 |
cx->check(obj); |
| 284 |
|
| 285 |
cx->clearPendingException(); |
| 286 |
|
| 287 |
JS::UniqueTwoByteChars chars{ |
| 288 |
UTF8CharsToNewTwoByteCharsZ(cx, UTF8Chars(utf8, length), &length, |
| 289 |
js::MallocArena) |
| 290 |
.get()}; |
| 291 |
if (!chars) { |
| 292 |
return true; |
| 293 |
} |
| 294 |
|
| 295 |
using frontend::FullParseHandler; |
| 296 |
using frontend::ParseGoal; |
| 297 |
using frontend::Parser; |
| 298 |
|
| 299 |
AutoReportFrontendContext fc(cx, |
| 300 |
AutoReportFrontendContext::Warning::Suppress); |
| 301 |
CompileOptions options(cx); |
| 302 |
Rooted<frontend::CompilationInput> input(cx, |
| 303 |
frontend::CompilationInput(options)); |
| 304 |
if (!input.get().initForGlobal(&fc)) { |
| 305 |
return false; |
| 306 |
} |
| 307 |
|
| 308 |
LifoAllocScope allocScope(&cx->tempLifoAlloc()); |
| 309 |
js::frontend::NoScopeBindingCache scopeCache; |
| 310 |
frontend::CompilationState compilationState(&fc, allocScope, input.get()); |
| 311 |
if (!compilationState.init(&fc, &scopeCache)) { |
| 312 |
return false; |
| 313 |
} |
| 314 |
|
| 315 |
// Warnings and errors during parsing shouldn't be reported. |
| 316 |
fc.clearAutoReport(); |
| 317 |
|
| 318 |
Parser<FullParseHandler, char16_t> parser(&fc, options, chars.get(), length, |
| 319 |
compilationState, |
| 320 |
/* syntaxParser = */ nullptr); |
| 321 |
if (!parser.checkOptions() || parser.parse().isErr()) { |
| 322 |
// We ran into an error. If it was because we ran out of source, we |
| 323 |
// return false so our caller knows to try to collect more buffered |
| 324 |
// source. |
| 325 |
if (parser.isUnexpectedEOF()) { |
| 326 |
return false; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
// Return true on any out-of-memory error or non-EOF-related syntax error, so |
| 331 |
// our caller doesn't try to collect more buffered source. |
| 332 |
return true; |
| 333 |
} |
| 334 |
|
| 335 |
class FunctionCompiler { |
| 336 |
private: |
| 337 |
JSContext* const cx_; |
| 338 |
Rooted<JSAtom*> nameAtom_; |
| 339 |
StringBuilder funStr_; |
| 340 |
|
| 341 |
uint32_t parameterListEnd_ = 0; |
| 342 |
bool nameIsIdentifier_ = true; |
| 343 |
|
| 344 |
public: |
| 345 |
explicit FunctionCompiler(JSContext* cx, FrontendContext* fc) |
| 346 |
: cx_(cx), nameAtom_(cx), funStr_(fc) { |
| 347 |
AssertHeapIsIdle(); |
| 348 |
CHECK_THREAD(cx); |
| 349 |
MOZ_ASSERT(!cx->zone()->isAtomsZone()); |
| 350 |
} |
| 351 |
|
| 352 |
[[nodiscard]] bool init(const char* name, unsigned nargs, |
| 353 |
const char* const* argnames) { |
| 354 |
if (!funStr_.ensureTwoByteChars()) { |
| 355 |
return false; |
| 356 |
} |
| 357 |
if (!funStr_.append("function ")) { |
| 358 |
return false; |
| 359 |
} |
| 360 |
|
| 361 |
if (name) { |
| 362 |
size_t nameLen = strlen(name); |
| 363 |
|
| 364 |
nameAtom_ = Atomize(cx_, name, nameLen); |
| 365 |
if (!nameAtom_) { |
| 366 |
return false; |
| 367 |
} |
| 368 |
|
| 369 |
// If the name is an identifier, we can just add it to source text. |
| 370 |
// Otherwise we'll have to set it manually later. |
| 371 |
nameIsIdentifier_ = |
| 372 |
IsIdentifier(reinterpret_cast<const Latin1Char*>(name), nameLen); |
| 373 |
if (nameIsIdentifier_) { |
| 374 |
if (!funStr_.append(nameAtom_)) { |
| 375 |
return false; |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
if (!funStr_.append("(")) { |
| 381 |
return false; |
| 382 |
} |
| 383 |
|
| 384 |
for (unsigned i = 0; i < nargs; i++) { |
| 385 |
if (i != 0) { |
| 386 |
if (!funStr_.append(", ")) { |
| 387 |
return false; |
| 388 |
} |
| 389 |
} |
| 390 |
if (!funStr_.append(argnames[i], strlen(argnames[i]))) { |
| 391 |
return false; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
// Remember the position of ")". |
| 396 |
parameterListEnd_ = funStr_.length(); |
| 397 |
static_assert(FunctionConstructorMedialSigils[0] == ')'); |
| 398 |
|
| 399 |
return funStr_.append(FunctionConstructorMedialSigils.data(), |
| 400 |
FunctionConstructorMedialSigils.length()); |
| 401 |
} |
| 402 |
|
| 403 |
template <typename Unit> |
| 404 |
[[nodiscard]] inline bool addFunctionBody(const SourceText<Unit>& srcBuf) { |
| 405 |
return funStr_.append(srcBuf.get(), srcBuf.length()); |
| 406 |
} |
| 407 |
|
| 408 |
JSFunction* finish(const JS::EnvironmentChain& envChain, |
| 409 |
const ReadOnlyCompileOptions& optionsArg) { |
| 410 |
using js::frontend::FunctionSyntaxKind; |
| 411 |
|
| 412 |
if (!funStr_.append(FunctionConstructorFinalBrace.data(), |
| 413 |
FunctionConstructorFinalBrace.length())) { |
| 414 |
return nullptr; |
| 415 |
} |
| 416 |
|
| 417 |
size_t newLen = funStr_.length(); |
| 418 |
UniqueTwoByteChars stolen(funStr_.stealChars()); |
| 419 |
if (!stolen) { |
| 420 |
return nullptr; |
| 421 |
} |
| 422 |
|
| 423 |
SourceText<char16_t> newSrcBuf; |
| 424 |
if (!newSrcBuf.init(cx_, std::move(stolen), newLen)) { |
| 425 |
return nullptr; |
| 426 |
} |
| 427 |
|
| 428 |
RootedObject enclosingEnv(cx_); |
| 429 |
ScopeKind kind; |
| 430 |
if (envChain.empty()) { |
| 431 |
// A compiled function has a burned-in environment chain, so if no exotic |
| 432 |
// environment was requested, we can use the global lexical environment |
| 433 |
// directly and not need to worry about any potential non-syntactic scope. |
| 434 |
enclosingEnv = &cx_->global()->lexicalEnvironment(); |
| 435 |
kind = ScopeKind::Global; |
| 436 |
} else { |
| 437 |
enclosingEnv = CreateNonSyntacticEnvironmentChain(cx_, envChain); |
| 438 |
if (!enclosingEnv) { |
| 439 |
return nullptr; |
| 440 |
} |
| 441 |
kind = ScopeKind::NonSyntactic; |
| 442 |
} |
| 443 |
|
| 444 |
cx_->check(enclosingEnv); |
| 445 |
|
| 446 |
// Make sure the static scope chain matches up when we have a |
| 447 |
// non-syntactic scope. |
| 448 |
MOZ_ASSERT_IF(!enclosingEnv->is<GlobalLexicalEnvironmentObject>(), |
| 449 |
kind == ScopeKind::NonSyntactic); |
| 450 |
|
| 451 |
CompileOptions options(cx_, optionsArg); |
| 452 |
options.setNonSyntacticScope(kind == ScopeKind::NonSyntactic); |
| 453 |
|
| 454 |
FunctionSyntaxKind syntaxKind = FunctionSyntaxKind::Statement; |
| 455 |
RootedFunction fun(cx_); |
| 456 |
if (kind == ScopeKind::NonSyntactic) { |
| 457 |
Rooted<Scope*> enclosingScope( |
| 458 |
cx_, GlobalScope::createEmpty(cx_, ScopeKind::NonSyntactic)); |
| 459 |
if (!enclosingScope) { |
| 460 |
return nullptr; |
| 461 |
} |
| 462 |
|
| 463 |
fun = js::frontend::CompileStandaloneFunctionInNonSyntacticScope( |
| 464 |
cx_, options, newSrcBuf, mozilla::Some(parameterListEnd_), syntaxKind, |
| 465 |
enclosingScope); |
| 466 |
} else { |
| 467 |
fun = js::frontend::CompileStandaloneFunction( |
| 468 |
cx_, options, newSrcBuf, mozilla::Some(parameterListEnd_), |
| 469 |
syntaxKind); |
| 470 |
} |
| 471 |
if (!fun) { |
| 472 |
return nullptr; |
| 473 |
} |
| 474 |
|
| 475 |
// When the function name isn't a valid identifier, the generated function |
| 476 |
// source in srcBuf won't include the name, so name the function manually. |
| 477 |
if (!nameIsIdentifier_) { |
| 478 |
fun->setAtom(nameAtom_); |
| 479 |
} |
| 480 |
|
| 481 |
if (fun->isInterpreted()) { |
| 482 |
fun->initEnvironment(enclosingEnv); |
| 483 |
} |
| 484 |
|
| 485 |
return fun; |
| 486 |
} |
| 487 |
}; |
| 488 |
|
| 489 |
JS_PUBLIC_API JSFunction* JS::CompileFunction( |
| 490 |
JSContext* cx, const EnvironmentChain& envChain, |
| 491 |
const ReadOnlyCompileOptions& options, const char* name, unsigned nargs, |
| 492 |
const char* const* argnames, SourceText<char16_t>& srcBuf) { |
| 493 |
ManualReportFrontendContext fc(cx); |
| 494 |
FunctionCompiler compiler(cx, &fc); |
| 495 |
if (!compiler.init(name, nargs, argnames) || |
| 496 |
!compiler.addFunctionBody(srcBuf)) { |
| 497 |
fc.failure(); |
| 498 |
return nullptr; |
| 499 |
} |
| 500 |
|
| 501 |
fc.ok(); |
| 502 |
return compiler.finish(envChain, options); |
| 503 |
} |
| 504 |
|
| 505 |
JS_PUBLIC_API JSFunction* JS::CompileFunction( |
| 506 |
JSContext* cx, const EnvironmentChain& envChain, |
| 507 |
const ReadOnlyCompileOptions& options, const char* name, unsigned nargs, |
| 508 |
const char* const* argnames, SourceText<Utf8Unit>& srcBuf) { |
| 509 |
ManualReportFrontendContext fc(cx); |
| 510 |
FunctionCompiler compiler(cx, &fc); |
| 511 |
if (!compiler.init(name, nargs, argnames) || |
| 512 |
!compiler.addFunctionBody(srcBuf)) { |
| 513 |
fc.failure(); |
| 514 |
return nullptr; |
| 515 |
} |
| 516 |
|
| 517 |
fc.ok(); |
| 518 |
return compiler.finish(envChain, options); |
| 519 |
} |
| 520 |
|
| 521 |
JS_PUBLIC_API JSFunction* JS::CompileFunctionUtf8( |
| 522 |
JSContext* cx, const EnvironmentChain& envChain, |
| 523 |
const ReadOnlyCompileOptions& options, const char* name, unsigned nargs, |
| 524 |
const char* const* argnames, const char* bytes, size_t length) { |
| 525 |
SourceText<Utf8Unit> srcBuf; |
| 526 |
if (!srcBuf.init(cx, bytes, length, SourceOwnership::Borrowed)) { |
| 527 |
return nullptr; |
| 528 |
} |
| 529 |
|
| 530 |
return CompileFunction(cx, envChain, options, name, nargs, argnames, srcBuf); |
| 531 |
} |
| 532 |
|
| 533 |
JS_PUBLIC_API void JS::ExposeScriptToDebugger(JSContext* cx, |
| 534 |
HandleScript script) { |
| 535 |
MOZ_ASSERT(cx); |
| 536 |
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime())); |
| 537 |
|
| 538 |
DebugAPI::onNewScript(cx, script); |
| 539 |
} |
| 540 |
|
| 541 |
JS_PUBLIC_API bool JS::UpdateDebugMetadata( |
| 542 |
JSContext* cx, Handle<JSScript*> script, const InstantiateOptions& options, |
| 543 |
HandleValue privateValue, HandleString elementAttributeName, |
| 544 |
HandleScript introScript, HandleScript scriptOrModule) { |
| 545 |
Rooted<ScriptSourceObject*> sso(cx, script->sourceObject()); |
| 546 |
|
| 547 |
if (!ScriptSourceObject::initElementProperties(cx, sso, |
| 548 |
elementAttributeName)) { |
| 549 |
return false; |
| 550 |
} |
| 551 |
|
| 552 |
// There is no equivalent of cross-compartment wrappers for scripts. If the |
| 553 |
// introduction script and ScriptSourceObject are in different compartments, |
| 554 |
// we would be creating a cross-compartment script reference, which is |
| 555 |
// forbidden. We can still store a CCW to the script source object though. |
| 556 |
RootedValue introductionScript(cx); |
| 557 |
if (introScript) { |
| 558 |
if (introScript->compartment() == cx->compartment()) { |
| 559 |
introductionScript.setPrivateGCThing(introScript); |
| 560 |
} |
| 561 |
} |
| 562 |
sso->setIntroductionScript(introductionScript); |
| 563 |
|
| 564 |
RootedValue privateValueStore(cx, UndefinedValue()); |
| 565 |
if (privateValue.isUndefined()) { |
| 566 |
// Set the private value to that of the script or module that this source is |
| 567 |
// part of, if any. |
| 568 |
if (scriptOrModule) { |
| 569 |
privateValueStore = scriptOrModule->sourceObject()->getPrivate(); |
| 570 |
} |
| 571 |
} else { |
| 572 |
privateValueStore = privateValue; |
| 573 |
} |
| 574 |
|
| 575 |
if (!privateValueStore.isUndefined()) { |
| 576 |
if (!JS_WrapValue(cx, &privateValueStore)) { |
| 577 |
return false; |
| 578 |
} |
| 579 |
} |
| 580 |
sso->setPrivate(cx->runtime(), privateValueStore); |
| 581 |
|
| 582 |
if (!options.hideScriptFromDebugger) { |
| 583 |
JS::ExposeScriptToDebugger(cx, script); |
| 584 |
} |
| 585 |
|
| 586 |
return true; |
| 587 |
} |
| 588 |
|
| 589 |
MOZ_NEVER_INLINE static bool ExecuteScript(JSContext* cx, HandleObject envChain, |
| 590 |
HandleScript script, |
| 591 |
MutableHandleValue rval) { |
| 592 |
MOZ_ASSERT(!cx->zone()->isAtomsZone()); |
| 593 |
AssertHeapIsIdle(); |
| 594 |
CHECK_THREAD(cx); |
| 595 |
cx->check(envChain, script); |
| 596 |
|
| 597 |
if (!envChain->is<GlobalLexicalEnvironmentObject>()) { |
| 598 |
MOZ_RELEASE_ASSERT(script->hasNonSyntacticScope()); |
| 599 |
} |
| 600 |
|
| 601 |
return Execute(cx, script, envChain, rval); |
| 602 |
} |
| 603 |
|
| 604 |
static bool ExecuteScript(JSContext* cx, const JS::EnvironmentChain& envChain, |
| 605 |
HandleScript script, MutableHandleValue rval) { |
| 606 |
RootedObject env(cx, CreateNonSyntacticEnvironmentChain(cx, envChain)); |
| 607 |
if (!env) { |
| 608 |
return false; |
| 609 |
} |
| 610 |
|
| 611 |
return ExecuteScript(cx, env, script, rval); |
| 612 |
} |
| 613 |
|
| 614 |
MOZ_NEVER_INLINE JS_PUBLIC_API bool JS_ExecuteScript(JSContext* cx, |
| 615 |
HandleScript scriptArg, |
| 616 |
MutableHandleValue rval) { |
| 617 |
RootedObject globalLexical(cx, &cx->global()->lexicalEnvironment()); |
| 618 |
return ExecuteScript(cx, globalLexical, scriptArg, rval); |
| 619 |
} |
| 620 |
|
| 621 |
MOZ_NEVER_INLINE JS_PUBLIC_API bool JS_ExecuteScript(JSContext* cx, |
| 622 |
HandleScript scriptArg) { |
| 623 |
RootedObject globalLexical(cx, &cx->global()->lexicalEnvironment()); |
| 624 |
RootedValue rval(cx); |
| 625 |
return ExecuteScript(cx, globalLexical, scriptArg, &rval); |
| 626 |
} |
| 627 |
|
| 628 |
MOZ_NEVER_INLINE JS_PUBLIC_API bool JS_ExecuteScript( |
| 629 |
JSContext* cx, const JS::EnvironmentChain& envChain, HandleScript scriptArg, |
| 630 |
MutableHandleValue rval) { |
| 631 |
return ExecuteScript(cx, envChain, scriptArg, rval); |
| 632 |
} |
| 633 |
|
| 634 |
MOZ_NEVER_INLINE JS_PUBLIC_API bool JS_ExecuteScript( |
| 635 |
JSContext* cx, const JS::EnvironmentChain& envChain, |
| 636 |
HandleScript scriptArg) { |
| 637 |
RootedValue rval(cx); |
| 638 |
return ExecuteScript(cx, envChain, scriptArg, &rval); |
| 639 |
} |
| 640 |
|
| 641 |
template <typename Unit> |
| 642 |
static bool EvaluateSourceBuffer(JSContext* cx, ScopeKind scopeKind, |
| 643 |
Handle<JSObject*> env, |
| 644 |
const ReadOnlyCompileOptions& optionsArg, |
| 645 |
SourceText<Unit>& srcBuf, |
| 646 |
MutableHandle<Value> rval) { |
| 647 |
CompileOptions options(cx, optionsArg); |
| 648 |
MOZ_ASSERT(!cx->zone()->isAtomsZone()); |
| 649 |
AssertHeapIsIdle(); |
| 650 |
CHECK_THREAD(cx); |
| 651 |
cx->check(env); |
| 652 |
MOZ_ASSERT_IF(!env->is<GlobalLexicalEnvironmentObject>(), |
| 653 |
scopeKind == ScopeKind::NonSyntactic); |
| 654 |
|
| 655 |
options.setNonSyntacticScope(scopeKind == ScopeKind::NonSyntactic); |
| 656 |
options.setIsRunOnce(true); |
| 657 |
|
| 658 |
AutoReportFrontendContext fc(cx); |
| 659 |
RootedScript script( |
| 660 |
cx, frontend::CompileGlobalScript(cx, &fc, options, srcBuf, scopeKind)); |
| 661 |
if (!script) { |
| 662 |
return false; |
| 663 |
} |
| 664 |
|
| 665 |
return Execute(cx, script, env, rval); |
| 666 |
} |
| 667 |
|
| 668 |
JS_PUBLIC_API bool JS::Evaluate(JSContext* cx, |
| 669 |
const ReadOnlyCompileOptions& options, |
| 670 |
SourceText<Utf8Unit>& srcBuf, |
| 671 |
MutableHandle<Value> rval) { |
| 672 |
RootedObject globalLexical(cx, &cx->global()->lexicalEnvironment()); |
| 673 |
return EvaluateSourceBuffer(cx, ScopeKind::Global, globalLexical, options, |
| 674 |
srcBuf, rval); |
| 675 |
} |
| 676 |
|
| 677 |
JS_PUBLIC_API bool JS::Evaluate(JSContext* cx, |
| 678 |
const ReadOnlyCompileOptions& optionsArg, |
| 679 |
SourceText<char16_t>& srcBuf, |
| 680 |
MutableHandleValue rval) { |
| 681 |
RootedObject globalLexical(cx, &cx->global()->lexicalEnvironment()); |
| 682 |
return EvaluateSourceBuffer(cx, ScopeKind::Global, globalLexical, optionsArg, |
| 683 |
srcBuf, rval); |
| 684 |
} |
| 685 |
|
| 686 |
JS_PUBLIC_API bool JS::Evaluate(JSContext* cx, const EnvironmentChain& envChain, |
| 687 |
const ReadOnlyCompileOptions& options, |
| 688 |
SourceText<char16_t>& srcBuf, |
| 689 |
MutableHandleValue rval) { |
| 690 |
RootedObject env(cx, CreateNonSyntacticEnvironmentChain(cx, envChain)); |
| 691 |
if (!env) { |
| 692 |
return false; |
| 693 |
} |
| 694 |
|
| 695 |
return EvaluateSourceBuffer(cx, ScopeKind::NonSyntactic, env, options, srcBuf, |
| 696 |
rval); |
| 697 |
} |
| 698 |
|
| 699 |
JS_PUBLIC_API bool JS::EvaluateUtf8Path( |
| 700 |
JSContext* cx, const ReadOnlyCompileOptions& optionsArg, |
| 701 |
const char* filename, MutableHandleValue rval) { |
| 702 |
FileContents buffer(cx); |
| 703 |
{ |
| 704 |
AutoFile file; |
| 705 |
if (!file.open(cx, filename) || !file.readAll(cx, buffer)) { |
| 706 |
return false; |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
CompileOptions options(cx, optionsArg); |
| 711 |
options.setFileAndLine(filename, 1); |
| 712 |
|
| 713 |
auto contents = reinterpret_cast<const char*>(buffer.begin()); |
| 714 |
size_t length = buffer.length(); |
| 715 |
|
| 716 |
JS::SourceText<Utf8Unit> srcBuf; |
| 717 |
if (!srcBuf.init(cx, contents, length, JS::SourceOwnership::Borrowed)) { |
| 718 |
return false; |
| 719 |
} |
| 720 |
|
| 721 |
return Evaluate(cx, options, srcBuf, rval); |
| 722 |
} |