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/. */
7 /* JavaScript API for compiling scripts to stencil without depending on
10 #ifndef js_experimental_CompileScript_h
11 #define js_experimental_CompileScript_h
14 #include "js/ErrorReport.h" // JSErrorReport
15 #include "js/experimental/JSStencil.h"
16 #include "js/GCAnnotations.h"
17 #include "js/Modules.h"
19 #include "js/UniquePtr.h"
22 class FrontendContext
;
24 struct CompilationInput
;
25 } // namespace frontend
29 using FrontendContext
= js::FrontendContext
;
31 // Create a new front-end context.
32 JS_PUBLIC_API
JS::FrontendContext
* NewFrontendContext();
34 // Destroy a front-end context allocated with NewFrontendContext.
35 JS_PUBLIC_API
void DestroyFrontendContext(JS::FrontendContext
* fc
);
37 // Set the size of the native stack that should not be exceed. To disable
38 // stack size checking pass 0.
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
);
45 // Return the stack quota that can be passed to SetNativeStackQuota, for given
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
);
51 // Returns true if there was any error reported to given FrontendContext.
52 JS_PUBLIC_API
bool HadFrontendErrors(JS::FrontendContext
* fc
);
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.
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
);
67 // Returns an error report if given JS::FrontendContext had error and it has
68 // an error report associated.
70 // This can be nullptr even if JS::HadFrontendErrors returned true, if
71 // the error is one of:
74 // * allocation overflow
76 // The returned pointer is valid only while the given JS::FrontendContext is
79 // See ConvertFrontendErrorsToRuntimeErrors for options parameter.
80 JS_PUBLIC_API
const JSErrorReport
* GetFrontendErrorReport(
81 JS::FrontendContext
* fc
, const JS::ReadOnlyCompileOptions
& options
);
83 // Returns true if the JS::FrontendContext had over recuresed error.
84 JS_PUBLIC_API
bool HadFrontendOverRecursed(JS::FrontendContext
* fc
);
86 // Returns true if the JS::FrontendContext had out of memory error.
87 JS_PUBLIC_API
bool HadFrontendOutOfMemory(JS::FrontendContext
* fc
);
89 // Returns true if the JS::FrontendContext had allocation overflow error.
90 JS_PUBLIC_API
bool HadFrontendAllocationOverflow(JS::FrontendContext
* fc
);
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
);
96 // Returns the number of warnings reported to the JS::FrontendContext.
97 JS_PUBLIC_API
size_t GetFrontendWarningCount(JS::FrontendContext
* fc
);
99 // Returns an error report represents the index-th warning.
101 // The returned pointer is valid only while the JS::FrontendContext is alive.
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
);
108 extern JS_PUBLIC_API already_AddRefed
<JS::Stencil
> CompileGlobalScriptToStencil(
109 JS::FrontendContext
* fc
, const JS::ReadOnlyCompileOptions
& options
,
110 JS::SourceText
<mozilla::Utf8Unit
>& srcBuf
);
112 extern JS_PUBLIC_API already_AddRefed
<JS::Stencil
> CompileGlobalScriptToStencil(
113 JS::FrontendContext
* fc
, const JS::ReadOnlyCompileOptions
& options
,
114 JS::SourceText
<char16_t
>& srcBuf
);
116 extern JS_PUBLIC_API already_AddRefed
<JS::Stencil
> CompileModuleScriptToStencil(
117 JS::FrontendContext
* fc
, const JS::ReadOnlyCompileOptions
& options
,
118 JS::SourceText
<mozilla::Utf8Unit
>& srcBuf
);
120 extern JS_PUBLIC_API already_AddRefed
<JS::Stencil
> CompileModuleScriptToStencil(
121 JS::FrontendContext
* fc
, const JS::ReadOnlyCompileOptions
& options
,
122 JS::SourceText
<char16_t
>& srcBuf
);
124 extern JS_PUBLIC_API
bool PrepareForInstantiate(
125 JS::FrontendContext
* fc
, JS::Stencil
& stencil
,
126 JS::InstantiationStorage
& storage
);
130 #endif // js_experimental_CompileScript_h