NewRuntime creates a new quickjs runtime with simplified interrupt handling.
(opts ...Option)
| 510 | |
| 511 | // NewRuntime creates a new quickjs runtime with simplified interrupt handling. |
| 512 | func NewRuntime(opts ...Option) *Runtime { |
| 513 | options := &Options{ |
| 514 | timeout: 0, |
| 515 | memoryLimit: 0, |
| 516 | gcThreshold: -1, |
| 517 | maxStackSize: 0, |
| 518 | canBlock: true, |
| 519 | moduleImport: false, |
| 520 | strip: 0, |
| 521 | ownerGoroutineCheck: true, |
| 522 | strictThreadAffinity: false, |
| 523 | } |
| 524 | for _, opt := range opts { |
| 525 | opt(options) |
| 526 | } |
| 527 | |
| 528 | rt := &Runtime{ |
| 529 | ref: C.JS_NewRuntime(), |
| 530 | options: options, |
| 531 | } |
| 532 | registerRuntime(rt.ref, rt) |
| 533 | C.SetPromiseRejectionTracker(rt.ref, 1) |
| 534 | |
| 535 | // Configure runtime options |
| 536 | if rt.options.memoryLimit > 0 { |
| 537 | rt.SetMemoryLimit(rt.options.memoryLimit) |
| 538 | } |
| 539 | |
| 540 | if rt.options.gcThreshold >= -1 { |
| 541 | rt.SetGCThreshold(rt.options.gcThreshold) |
| 542 | } |
| 543 | |
| 544 | rt.SetMaxStackSize(rt.options.maxStackSize) |
| 545 | |
| 546 | if rt.options.canBlock { |
| 547 | C.JS_SetCanBlock(rt.ref, C.bool(true)) |
| 548 | } |
| 549 | |
| 550 | if rt.options.strip > 0 { |
| 551 | rt.SetStripInfo(rt.options.strip) |
| 552 | } |
| 553 | |
| 554 | if rt.options.moduleImport { |
| 555 | rt.SetModuleImport(rt.options.moduleImport) |
| 556 | } |
| 557 | |
| 558 | // Set timeout after other options (will override interrupt handler) |
| 559 | if rt.options.timeout > 0 { |
| 560 | rt.SetExecuteTimeout(rt.options.timeout) |
| 561 | } |
| 562 | |
| 563 | return rt |
| 564 | } |
| 565 | |
| 566 | // RunGC will call quickjs's garbage collector. |
| 567 | func (r *Runtime) RunGC() { |