// Runtime represents a Javascript runtime corresponding to an object heap. Several runtimes can exist at the same time but they cannot exchange objects. Inside a given runtime, no multi-threading is supported.
typeRuntimestruct{
ref*C.JSRuntime
options*Options
}
typeOptionsstruct{
timeoutuint64
memoryLimituint64
gcThresholduint64
maxStackSizeuint64
canBlockbool
moduleImportbool
}
typeOptionfunc(*Options)
// WithExecuteTimeout will set the runtime's execute timeout; default is 0
funcWithExecuteTimeout(timeoutuint64)Option{
returnfunc(o*Options){
o.timeout=timeout
}
}
// WithMemoryLimit will set the runtime memory limit; if not set, it will be unlimit.
funcWithMemoryLimit(memoryLimituint64)Option{
returnfunc(o*Options){
o.memoryLimit=memoryLimit
}
}
// WithGCThreshold will set the runtime's GC threshold; use -1 to disable automatic GC.
funcWithGCThreshold(gcThresholduint64)Option{
returnfunc(o*Options){
o.gcThreshold=gcThreshold
}
}
// WithMaxStackSize will set max runtime's stack size; default is 255
funcWithMaxStackSize(maxStackSizeuint64)Option{
returnfunc(o*Options){
o.maxStackSize=maxStackSize
}
}
// WithCanBlock will set the runtime's can block; default is true
funcWithCanBlock(canBlockbool)Option{
returnfunc(o*Options){
o.canBlock=canBlock
}
}
funcWithModuleImport(moduleImportbool)Option{
returnfunc(o*Options){
o.moduleImport=moduleImport
}
}
// NewRuntime creates a new quickjs runtime.
funcNewRuntime(opts...Option)Runtime{
runtime.LockOSThread()// prevent multiple quickjs runtime from being created