Skip to content

Commit 3781ade

Browse files
committed
Adjust default memory limits; add method to set them.
1 parent bb0bcd7 commit 3781ade

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Sources/Substrata/Engine.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class JSEngine {
2121
self.context = JSContext(runtime: runtime)
2222
self.bridge = JSDataBridge()
2323

24-
JS_SetMaxStackSize(runtime, (1024 * 1024) * 10)
25-
JS_SetMemoryLimit(runtime, 1024 * 1024 * 1024)
24+
JS_SetMaxStackSize(runtime, (1024 * 1024) * 5)
25+
JS_SetMemoryLimit(runtime, (1024 * 1024) * 100)
2626

2727
setupDefaultObjects()
2828
}
@@ -32,6 +32,16 @@ public class JSEngine {
3232
JS_FreeRuntime(runtime)
3333
}
3434

35+
public func setLimits(stackSize: UInt, heapSize: UInt) {
36+
context.performThreadSafe {
37+
// Ensure we don't set ridiculous values
38+
let validatedStack = max(1024 * 64, min(stackSize, 1024 * 1024 * 64)) // 64KB min, 64MB max
39+
let validatedHeap = max(1024 * 1024, min(heapSize, 1024 * 1024 * 1024)) // 1MB min, 1GB max
40+
JS_SetMaxStackSize(runtime, size_t(validatedStack))
41+
JS_SetMemoryLimit(runtime, size_t(validatedHeap))
42+
}
43+
}
44+
3545
public func shutdown() {
3646
context.shutdown()
3747
}

0 commit comments

Comments
 (0)