Skip to content

Commit

Permalink
Adding an option that enables/disables stack-trace-related extensions.
Browse files Browse the repository at this point in the history
(cherry picked from commit 23bfc0d)
  • Loading branch information
iamstolis authored and woess committed Aug 3, 2024
1 parent 5041fbf commit fd4b7be
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected final int doInt(
@Cached IsNumberNode isNumber,
@Cached JSToIntegerAsLongNode toInteger) {
JSContext context = getJSContext();
if (context.isOptionV8CompatibilityMode() || context.getLanguageOptions().isMLEMode()) {
if (context.getLanguageOptions().stackTraceAPI()) {
JSFunctionObject errorConstructor = getRealm().getErrorConstructor(JSErrorType.Error);
if (JSProperty.isData(getStackTraceLimit.getPropertyFlagsOrDefault(errorConstructor, JSError.STACK_TRACE_LIMIT_PROPERTY_NAME, JSProperty.ACCESSOR))) {
Object value = getStackTraceLimit.getOrDefault(errorConstructor, JSError.STACK_TRACE_LIMIT_PROPERTY_NAME, Undefined.instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,11 @@ public Map<String, String> apply(String value) {
public static final OptionKey<Boolean> V8_INTRINSICS = new OptionKey<>(false);
@CompilationFinal private boolean v8Intrinsics;

public static final String STACK_TRACE_API_NAME = JS_OPTION_PREFIX + "stack-trace-api";
@Option(name = STACK_TRACE_API_NAME, category = OptionCategory.EXPERT, help = "Enable Stack Trace API (Error.captureStackTrace/prepareStackTrace/stackTraceLimit).") //
public static final OptionKey<Boolean> STACK_TRACE_API = new OptionKey<>(true);
@CompilationFinal private boolean stackTraceAPI;

public enum UnhandledRejectionsTrackingMode {
NONE,
WARN,
Expand Down Expand Up @@ -792,6 +797,7 @@ private void cacheOptions(SandboxPolicy sandboxPolicy) {
this.scopeOptimization = readBooleanOption(SCOPE_OPTIMIZATION);
this.allowNarrowSpacesInDateFormat = ALLOW_NARROW_SPACES_IN_DATE_FORMAT.hasBeenSet(optionValues) ? readBooleanOption(ALLOW_NARROW_SPACES_IN_DATE_FORMAT) : !isV8CompatibilityMode();
this.v8Intrinsics = readBooleanOption(V8_INTRINSICS);
this.stackTraceAPI = readBooleanOption(STACK_TRACE_API);
}

private UnhandledRejectionsTrackingMode readUnhandledRejectionsMode() {
Expand Down Expand Up @@ -1257,6 +1263,10 @@ public boolean isV8Intrinsics() {
return v8Intrinsics;
}

public boolean isStackTraceAPI() {
return stackTraceAPI;
}

public short getFrequencyBasedPropertyCacheLimit() {
return frequencyBasedPropertyCacheLimit;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public static JSException create(JSErrorType type, String message, Throwable cau

public static int getStackTraceLimit(JSRealm realm) {
JSContextOptions contextOptions = realm.getContextOptions();
if (contextOptions.isV8CompatibilityMode() || contextOptions.isMLEMode()) {
if (contextOptions.isStackTraceAPI()) {
JSFunctionObject errorConstructor = realm.getErrorConstructor(JSErrorType.Error);
DynamicObjectLibrary lib = DynamicObjectLibrary.getUncached();
if (JSProperty.isData(lib.getPropertyFlagsOrDefault(errorConstructor, JSError.STACK_TRACE_LIMIT_PROPERTY_NAME, JSProperty.ACCESSOR))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public record JSLanguageOptions(
boolean functionStatementError,
boolean constAsVar,
boolean profileTime,
boolean stackTraceAPI,
String locale) {

public static JSLanguageOptions fromOptionValues(SandboxPolicy sandboxPolicy, OptionValues optionValues) {
Expand Down Expand Up @@ -207,6 +208,7 @@ public static JSLanguageOptions fromContextOptions(JSContextOptions options) {
boolean functionStatementError = options.isFunctionStatementError();
boolean constAsVar = options.isConstAsVar();
boolean profileTime = options.isProfileTime();
boolean stackTraceAPI = options.isStackTraceAPI();
String locale = options.getLocale();

return new JSLanguageOptions(
Expand Down Expand Up @@ -282,6 +284,7 @@ public static JSLanguageOptions fromContextOptions(JSContextOptions options) {
functionStatementError,
constAsVar,
profileTime,
stackTraceAPI,
locale);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2243,7 +2243,6 @@ public void addOptionalGlobals() {
setupJavaInterop();
}
addCommonJSGlobals();
addErrorStackTraceLimit();
}

private void addGlobalGlobal() {
Expand All @@ -2252,13 +2251,6 @@ private void addGlobalGlobal() {
}
}

private void addErrorStackTraceLimit() {
if (getContextOptions().isV8CompatibilityMode() || getContextOptions().isMLEMode()) {
JSObject errorConstructor = getErrorConstructor(JSErrorType.Error);
JSObjectUtil.putDataProperty(errorConstructor, JSError.STACK_TRACE_LIMIT_PROPERTY_NAME, getContextOptions().getStackTraceLimit(), JSAttributes.getDefault());
}
}

private void addShellGlobals() {
if (getContextOptions().isShell()) {
GlobalBuiltins.GLOBAL_SHELL.forEachBuiltin((Builtin builtin) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.oracle.truffle.js.runtime.GraalJSException;
import com.oracle.truffle.js.runtime.GraalJSException.JSStackTraceElement;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.JSContextOptions;
import com.oracle.truffle.js.runtime.JSErrorType;
import com.oracle.truffle.js.runtime.JSException;
import com.oracle.truffle.js.runtime.JSRealm;
Expand Down Expand Up @@ -209,8 +210,9 @@ public static JSConstructor createErrorConstructor(JSRealm realm, JSErrorType er
JSObjectUtil.putConstructorProperty(classPrototype, errorConstructor);
JSObjectUtil.putDataProperty(classPrototype, NAME, name, MESSAGE_ATTRIBUTES);
JSObjectUtil.putConstructorPrototypeProperty(errorConstructor, classPrototype);
if (errorType == JSErrorType.Error) {
if (realm.getContextOptions().isStackTraceAPI() && errorType == JSErrorType.Error) {
JSObjectUtil.putFunctionsFromContainer(realm, errorConstructor, ErrorFunctionBuiltins.BUILTINS);
JSObjectUtil.putDataProperty(errorConstructor, STACK_TRACE_LIMIT_PROPERTY_NAME, JSContextOptions.STACK_TRACE_LIMIT.getValue(realm.getOptions()), JSAttributes.getDefault());
}

return new JSConstructor(errorConstructor, classPrototype);
Expand Down Expand Up @@ -313,7 +315,7 @@ public static Object prepareStack(JSRealm realm, JSDynamicObject errorObj, Graal
*/
@TruffleBoundary
public static Object prepareStackNoCallback(JSRealm realm, JSDynamicObject errorObj, JSStackTraceElement[] jsStackTrace) {
if (realm.getContextOptions().isV8CompatibilityMode()) {
if (realm.getContextOptions().isStackTraceAPI()) {
JSFunctionObject error = realm.getErrorConstructor(JSErrorType.Error);
Object prepareStackTrace = JSObject.get(error, PREPARE_STACK_TRACE_NAME);
if (JSFunction.isJSFunction(prepareStackTrace)) {
Expand Down

0 comments on commit fd4b7be

Please sign in to comment.