MCPcopy Create free account
hub / github.com/buke/quickjs-go / NewContextWithOptions

Method NewContextWithOptions

runtime.go:1150–1204  ·  view source on GitHub ↗

NewContextWithOptions creates a JavaScript context with configurable host bootstrap.

(opts ...ContextBootstrapOption)

Source from the content-addressed store, hash-verified

1148
1149// NewContextWithOptions creates a JavaScript context with configurable host bootstrap.
1150func (r *Runtime) NewContextWithOptions(opts ...ContextBootstrapOption) *Context {
1151 if r == nil {
1152 return nil
1153 }
1154 if !r.ensureOwnerAccess() {
1155 return nil
1156 }
1157 bootstrap := newContextBootstrapOptions(opts...)
1158
1159 r.mu.Lock()
1160 defer r.mu.Unlock()
1161 if r.closed.Load() || r.ref == nil {
1162 return nil
1163 }
1164
1165 if !r.stdHandlersInitialized {
1166 C.js_std_init_handlers(r.ref)
1167 r.stdHandlersInitialized = true
1168 }
1169
1170 // create a new context (heap, global object and context stack
1171 var ctx_ref *C.JSContext
1172 if runtimeNewContextHook != nil {
1173 ctx_ref = runtimeNewContextHook(r.ref)
1174 } else {
1175 ctx_ref = C.JS_NewContext(r.ref)
1176 }
1177 if ctx_ref == nil {
1178 return nil
1179 }
1180
1181 // Create Context with HandleStore for function management
1182 ctx := &Context{
1183 contextID: r.nextContextID(),
1184 ref: ctx_ref,
1185 runtime: r,
1186 handleStore: newHandleStore(), // Initialize HandleStore for function management
1187 }
1188 ctx.initScheduler()
1189
1190 // Register context mapping for C callbacks
1191 registerContext(ctx_ref, ctx)
1192 r.registerOwnedContext(ctx)
1193
1194 if bootstrap.loadStdOS && !ctx.BootstrapStdOS() {
1195 ctx.Close()
1196 return nil
1197 }
1198 if bootstrap.injectTimers && !ctx.BootstrapTimers() {
1199 ctx.Close()
1200 return nil
1201 }
1202
1203 return ctx
1204}
1205
1206func (r *Runtime) registerOwnedContext(ctx *Context) {
1207 if r == nil || ctx == nil || ctx.ref == nil {

Calls 11

ensureOwnerAccessMethod · 0.95
nextContextIDMethod · 0.95
initSchedulerMethod · 0.95
registerOwnedContextMethod · 0.95
BootstrapStdOSMethod · 0.95
CloseMethod · 0.95
BootstrapTimersMethod · 0.95
newHandleStoreFunction · 0.85
registerContextFunction · 0.85
LoadMethod · 0.80