@@ -225,6 +225,7 @@ import (
225225 "strings"
226226
227227 "github.com/robertkrimen/otto/file"
228+ "github.com/robertkrimen/otto/regexp"
228229 "github.com/robertkrimen/otto/registry"
229230)
230231
@@ -237,11 +238,60 @@ type Otto struct {
237238 runtime * runtime
238239}
239240
241+ // Option represents a Otto configuration option.
242+ type Option func (* Otto )
243+
244+ // Debugger is an option that sets the debugger handler to fn.
245+ func Debugger (fn func (vm * Otto )) Option {
246+ return func (o * Otto ) {
247+ o .runtime .debugger = fn
248+ }
249+ }
250+
251+ // RandomSource is an option that sets the random source to fn.
252+ func RandomSource (fn func () float64 ) Option {
253+ return func (o * Otto ) {
254+ o .runtime .random = fn
255+ }
256+ }
257+
258+ // Stack is an option that sets the stack depth to limit.
259+ // Default: 0 (unlimited).
260+ func Stack (limit int ) Option {
261+ return func (o * Otto ) {
262+ o .runtime .stackLimit = limit
263+ }
264+ }
265+
266+ // StackTrace is an option that sets the stack trace depth to limit.
267+ // Default: 10.
268+ func StackTrace (limit int ) Option {
269+ return func (o * Otto ) {
270+ o .runtime .traceLimit = limit
271+ }
272+ }
273+
274+ // RegExp is an option which sets the regular expression engine.
275+ // This can be used to enable ECMAScript compatible regular expression
276+ // processing using regexp2.Creator.
277+ // Default: standard regexp library using ECMA to RE2 transformation
278+ // which provides constant order matching but doesn't support backtracking.
279+ func RegExp (creator regexp.Creator ) Option {
280+ return func (o * Otto ) {
281+ o .runtime .regExp = creator
282+ }
283+ }
284+
240285// New will allocate a new JavaScript runtime.
241- func New () * Otto {
286+ func New (options ... Option ) * Otto {
242287 o := & Otto {
243288 runtime : newContext (),
244289 }
290+ o .runtime .regExp = & regexpStd {}
291+
292+ for _ , f := range options {
293+ f (o )
294+ }
245295 o .runtime .otto = o
246296 o .runtime .traceLimit = 10
247297 if err := o .Set ("console" , o .runtime .newConsole ()); err != nil {
@@ -360,11 +410,13 @@ func (o Otto) setValue(name string, value Value) {
360410}
361411
362412// SetDebuggerHandler sets the debugger handler to fn.
413+ // Deprecated: Use Debugger option.
363414func (o Otto ) SetDebuggerHandler (fn func (vm * Otto )) {
364415 o .runtime .debugger = fn
365416}
366417
367418// SetRandomSource sets the random source to fn.
419+ // Deprecated: Use RandomSource option.
368420func (o Otto ) SetRandomSource (fn func () float64 ) {
369421 o .runtime .random = fn
370422}
@@ -377,6 +429,7 @@ func (o Otto) SetRandomSource(fn func() float64) {
377429// JavaScript makes a call to a Go function, otto won't keep track of what
378430// happens outside the interpreter. So if your Go function is infinitely
379431// recursive, you're still in trouble.
432+ // Deprecated: Use Stack option.
380433func (o Otto ) SetStackDepthLimit (limit int ) {
381434 o .runtime .stackLimit = limit
382435}
@@ -386,6 +439,7 @@ func (o Otto) SetStackDepthLimit(limit int) {
386439// is 10. This is consistent with V8 and SpiderMonkey.
387440//
388441// TODO: expose via `Error.stackTraceLimit`.
442+ // Deprecated: Use StackTrace option.
389443func (o Otto ) SetStackTraceLimit (limit int ) {
390444 o .runtime .traceLimit = limit
391445}
0 commit comments