| 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 |
/* JavaScript API for compiling scripts to stencil without depending on |
| 8 |
* JSContext. */ |
| 9 |
|
| 10 |
#ifndef js_experimental_CompileScript_h |
| 11 |
#define js_experimental_CompileScript_h |
| 12 |
|
| 13 |
#include "jspubtd.h" |
| 14 |
#include "js/ErrorReport.h" // JSErrorReport |
| 15 |
#include "js/experimental/JSStencil.h" |
| 16 |
#include "js/GCAnnotations.h" |
| 17 |
#include "js/Modules.h" |
| 18 |
#include "js/Stack.h" |
| 19 |
#include "js/UniquePtr.h" |
| 20 |
|
| 21 |
namespace js { |
| 22 |
class FrontendContext; |
| 23 |
namespace frontend { |
| 24 |
struct CompilationInput; |
| 25 |
} // namespace frontend |
| 26 |
} // namespace js |
| 27 |
|
| 28 |
namespace JS { |
| 29 |
using FrontendContext = js::FrontendContext; |
| 30 |
|
| 31 |
// Create a new front-end context. |
| 32 |
JS_PUBLIC_API JS::FrontendContext* NewFrontendContext(); |
| 33 |
|
| 34 |
// Destroy a front-end context allocated with NewFrontendContext. |
| 35 |
JS_PUBLIC_API void DestroyFrontendContext(JS::FrontendContext* fc); |
| 36 |
|
| 37 |
// Set the size of the native stack that should not be exceed. To disable |
| 38 |
// stack size checking pass 0. |
| 39 |
// |
| 40 |
// WARNING: When the stack size checking is enabled, the JS::FrontendContext |
| 41 |
// can be used only in the thread where JS::SetNativeStackQuota is called. |
| 42 |
JS_PUBLIC_API void SetNativeStackQuota(JS::FrontendContext* fc, |
| 43 |
JS::NativeStackSize stackSize); |
| 44 |
|
| 45 |
// Return the stack quota that can be passed to SetNativeStackQuota, for given |
| 46 |
// stack size. |
| 47 |
// This subtracts a margin from given stack size, to make sure the stack quota |
| 48 |
// check performed internally is sufficient. |
| 49 |
JS_PUBLIC_API JS::NativeStackSize ThreadStackQuotaForSize(size_t stackSize); |
| 50 |
|
| 51 |
// Returns true if there was any error reported to given FrontendContext. |
| 52 |
JS_PUBLIC_API bool HadFrontendErrors(JS::FrontendContext* fc); |
| 53 |
|
| 54 |
// Convert the error reported to FrontendContext into runtime error in |
| 55 |
// JSContext. Returns false if the error cannot be converted (such as due to |
| 56 |
// OOM). An error might still be reported to the given JSContext. Also, returns |
| 57 |
// false when OOM is converted. Returns true otherwise. |
| 58 |
// |
| 59 |
// The options parameter isn't actually used, but the CompileOptions |
| 60 |
// provided to the compile/decode operation owns the filename pointer |
| 61 |
// that the error and warnings reported to FrontendContext point to, |
| 62 |
// so the CompileOptions must be alive until this call. |
| 63 |
JS_PUBLIC_API bool ConvertFrontendErrorsToRuntimeErrors( |
| 64 |
JSContext* cx, JS::FrontendContext* fc, |
| 65 |
const JS::ReadOnlyCompileOptions& options); |
| 66 |
|
| 67 |
// Returns an error report if given JS::FrontendContext had error and it has |
| 68 |
// an error report associated. |
| 69 |
// |
| 70 |
// This can be nullptr even if JS::HadFrontendErrors returned true, if |
| 71 |
// the error is one of: |
| 72 |
// * over recursed |
| 73 |
// * out of memory |
| 74 |
// * allocation overflow |
| 75 |
// |
| 76 |
// The returned pointer is valid only while the given JS::FrontendContext is |
| 77 |
// alive. |
| 78 |
// |
| 79 |
// See ConvertFrontendErrorsToRuntimeErrors for options parameter. |
| 80 |
JS_PUBLIC_API const JSErrorReport* GetFrontendErrorReport( |
| 81 |
JS::FrontendContext* fc, const JS::ReadOnlyCompileOptions& options); |
| 82 |
|
| 83 |
// Returns true if the JS::FrontendContext had over recuresed error. |
| 84 |
JS_PUBLIC_API bool HadFrontendOverRecursed(JS::FrontendContext* fc); |
| 85 |
|
| 86 |
// Returns true if the JS::FrontendContext had out of memory error. |
| 87 |
JS_PUBLIC_API bool HadFrontendOutOfMemory(JS::FrontendContext* fc); |
| 88 |
|
| 89 |
// Returns true if the JS::FrontendContext had allocation overflow error. |
| 90 |
JS_PUBLIC_API bool HadFrontendAllocationOverflow(JS::FrontendContext* fc); |
| 91 |
|
| 92 |
// Clear errors reported to the JS::FrontendContext. |
| 93 |
// No-op when there's no errors. |
| 94 |
JS_PUBLIC_API void ClearFrontendErrors(JS::FrontendContext* fc); |
| 95 |
|
| 96 |
// Returns the number of warnings reported to the JS::FrontendContext. |
| 97 |
JS_PUBLIC_API size_t GetFrontendWarningCount(JS::FrontendContext* fc); |
| 98 |
|
| 99 |
// Returns an error report represents the index-th warning. |
| 100 |
// |
| 101 |
// The returned pointer is valid only while the JS::FrontendContext is alive. |
| 102 |
// |
| 103 |
// See ConvertFrontendErrorsToRuntimeErrors for options parameter. |
| 104 |
JS_PUBLIC_API const JSErrorReport* GetFrontendWarningAt( |
| 105 |
JS::FrontendContext* fc, size_t index, |
| 106 |
const JS::ReadOnlyCompileOptions& options); |
| 107 |
|
| 108 |
extern JS_PUBLIC_API already_AddRefed<JS::Stencil> CompileGlobalScriptToStencil( |
| 109 |
JS::FrontendContext* fc, const JS::ReadOnlyCompileOptions& options, |
| 110 |
JS::SourceText<mozilla::Utf8Unit>& srcBuf); |
| 111 |
|
| 112 |
extern JS_PUBLIC_API already_AddRefed<JS::Stencil> CompileGlobalScriptToStencil( |
| 113 |
JS::FrontendContext* fc, const JS::ReadOnlyCompileOptions& options, |
| 114 |
JS::SourceText<char16_t>& srcBuf); |
| 115 |
|
| 116 |
extern JS_PUBLIC_API already_AddRefed<JS::Stencil> CompileModuleScriptToStencil( |
| 117 |
JS::FrontendContext* fc, const JS::ReadOnlyCompileOptions& options, |
| 118 |
JS::SourceText<mozilla::Utf8Unit>& srcBuf); |
| 119 |
|
| 120 |
extern JS_PUBLIC_API already_AddRefed<JS::Stencil> CompileModuleScriptToStencil( |
| 121 |
JS::FrontendContext* fc, const JS::ReadOnlyCompileOptions& options, |
| 122 |
JS::SourceText<char16_t>& srcBuf); |
| 123 |
|
| 124 |
extern JS_PUBLIC_API bool PrepareForInstantiate( |
| 125 |
JS::FrontendContext* fc, JS::Stencil& stencil, |
| 126 |
JS::InstantiationStorage& storage); |
| 127 |
|
| 128 |
} // namespace JS |
| 129 |
|
| 130 |
#endif // js_experimental_CompileScript_h |