otto.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*
  2. Package otto is a JavaScript parser and interpreter written natively in Go.
  3. http://godoc.org/github.com/robertkrimen/otto
  4. import (
  5. "github.com/robertkrimen/otto"
  6. )
  7. Run something in the VM
  8. vm := otto.New()
  9. vm.Run(`
  10. abc = 2 + 2;
  11. console.log("The value of abc is " + abc); // 4
  12. `)
  13. Get a value out of the VM
  14. value, err := vm.Get("abc")
  15. value, _ := value.ToInteger()
  16. }
  17. Set a number
  18. vm.Set("def", 11)
  19. vm.Run(`
  20. console.log("The value of def is " + def);
  21. // The value of def is 11
  22. `)
  23. Set a string
  24. vm.Set("xyzzy", "Nothing happens.")
  25. vm.Run(`
  26. console.log(xyzzy.length); // 16
  27. `)
  28. Get the value of an expression
  29. value, _ = vm.Run("xyzzy.length")
  30. // iv is an int64 with a value of 16
  31. iv, _ := value.ToInteger()
  32. An error happens
  33. value, err = vm.Run("abcdefghijlmnopqrstuvwxyz.length")
  34. if err != nil {
  35. // err = ReferenceError: abcdefghijlmnopqrstuvwxyz is not defined
  36. // If there is an error, then value.IsUndefined() is true
  37. ...
  38. }
  39. Set a Go function
  40. vm.Set("sayHello", func(call otto.FunctionCall) otto.Value {
  41. fmt.Printf("Hello, %s.\n", call.Argument(0).String())
  42. return otto.Value{}
  43. })
  44. Set a Go function that returns something useful
  45. vm.Set("twoPlus", func(call otto.FunctionCall) otto.Value {
  46. right, _ := call.Argument(0).ToInteger()
  47. result, _ := vm.ToValue(2 + right)
  48. return result
  49. })
  50. Use the functions in JavaScript
  51. result, _ = vm.Run(`
  52. sayHello("Xyzzy"); // Hello, Xyzzy.
  53. sayHello(); // Hello, undefined
  54. result = twoPlus(2.0); // 4
  55. `)
  56. # Parser
  57. A separate parser is available in the parser package if you're just interested in building an AST.
  58. http://godoc.org/github.com/robertkrimen/otto/parser
  59. Parse and return an AST
  60. filename := "" // A filename is optional
  61. src := `
  62. // Sample xyzzy example
  63. (function(){
  64. if (3.14159 > 0) {
  65. console.log("Hello, World.");
  66. return;
  67. }
  68. var xyzzy = NaN;
  69. console.log("Nothing happens.");
  70. return xyzzy;
  71. })();
  72. `
  73. // Parse some JavaScript, yielding a *ast.Program and/or an ErrorList
  74. program, err := parser.ParseFile(nil, filename, src, 0)
  75. otto
  76. You can run (Go) JavaScript from the commandline with: http://github.com/robertkrimen/otto/tree/master/otto
  77. $ go get -v github.com/robertkrimen/otto/otto
  78. Run JavaScript by entering some source on stdin or by giving otto a filename:
  79. $ otto example.js
  80. underscore
  81. Optionally include the JavaScript utility-belt library, underscore, with this import:
  82. import (
  83. "github.com/robertkrimen/otto"
  84. _ "github.com/robertkrimen/otto/underscore"
  85. )
  86. // Now every otto runtime will come loaded with underscore
  87. For more information: http://github.com/robertkrimen/otto/tree/master/underscore
  88. # Caveat Emptor
  89. The following are some limitations with otto:
  90. - "use strict" will parse, but does nothing.
  91. - The regular expression engine (re2/regexp) is not fully compatible with the ECMA5 specification.
  92. - Otto targets ES5. ES6 features (eg: Typed Arrays) are not supported.
  93. # Regular Expression Incompatibility
  94. Go translates JavaScript-style regular expressions into something that is "regexp" compatible via `parser.TransformRegExp`.
  95. Unfortunately, RegExp requires backtracking for some patterns, and backtracking is not supported by the standard Go engine: https://code.google.com/p/re2/wiki/Syntax
  96. Therefore, the following syntax is incompatible:
  97. (?=) // Lookahead (positive), currently a parsing error
  98. (?!) // Lookahead (backhead), currently a parsing error
  99. \1 // Backreference (\1, \2, \3, ...), currently a parsing error
  100. A brief discussion of these limitations: "Regexp (?!re)" https://groups.google.com/forum/?fromgroups=#%21topic/golang-nuts/7qgSDWPIh_E
  101. More information about re2: https://code.google.com/p/re2/
  102. In addition to the above, re2 (Go) has a different definition for \s: [\t\n\f\r ].
  103. The JavaScript definition, on the other hand, also includes \v, Unicode "Separator, Space", etc.
  104. # Halting Problem
  105. If you want to stop long running executions (like third-party code), you can use the interrupt channel to do this:
  106. package main
  107. import (
  108. "errors"
  109. "fmt"
  110. "os"
  111. "time"
  112. "github.com/robertkrimen/otto"
  113. )
  114. var halt = errors.New("Stahp")
  115. func main() {
  116. runUnsafe(`var abc = [];`)
  117. runUnsafe(`
  118. while (true) {
  119. // Loop forever
  120. }`)
  121. }
  122. func runUnsafe(unsafe string) {
  123. start := time.Now()
  124. defer func() {
  125. duration := time.Since(start)
  126. if caught := recover(); caught != nil {
  127. if caught == halt {
  128. fmt.Fprintf(os.Stderr, "Some code took to long! Stopping after: %v\n", duration)
  129. return
  130. }
  131. panic(caught) // Something else happened, repanic!
  132. }
  133. fmt.Fprintf(os.Stderr, "Ran code successfully: %v\n", duration)
  134. }()
  135. vm := otto.New()
  136. vm.Interrupt = make(chan func(), 1) // The buffer prevents blocking
  137. go func() {
  138. time.Sleep(2 * time.Second) // Stop after two seconds
  139. vm.Interrupt <- func() {
  140. panic(halt)
  141. }
  142. }()
  143. vm.Run(unsafe) // Here be dragons (risky code)
  144. }
  145. Where is setTimeout/setInterval?
  146. These timing functions are not actually part of the ECMA-262 specification. Typically, they belong to the `windows` object (in the browser).
  147. It would not be difficult to provide something like these via Go, but you probably want to wrap otto in an event loop in that case.
  148. For an example of how this could be done in Go with otto, see natto:
  149. http://github.com/robertkrimen/natto
  150. Here is some more discussion of the issue:
  151. * http://book.mixu.net/node/ch2.html
  152. * http://en.wikipedia.org/wiki/Reentrancy_%28computing%29
  153. * http://aaroncrane.co.uk/2009/02/perl_safe_signals/
  154. */
  155. package otto
  156. import (
  157. "encoding/json"
  158. "errors"
  159. "strings"
  160. "github.com/robertkrimen/otto/file"
  161. "github.com/robertkrimen/otto/registry"
  162. )
  163. // Otto is the representation of the JavaScript runtime.
  164. // Each instance of Otto has a self-contained namespace.
  165. type Otto struct {
  166. // Interrupt is a channel for interrupting the runtime. You can use this to halt a long running execution, for example.
  167. // See "Halting Problem" for more information.
  168. Interrupt chan func()
  169. runtime *runtime
  170. }
  171. // New will allocate a new JavaScript runtime.
  172. func New() *Otto {
  173. o := &Otto{
  174. runtime: newContext(),
  175. }
  176. o.runtime.otto = o
  177. o.runtime.traceLimit = 10
  178. if err := o.Set("console", o.runtime.newConsole()); err != nil {
  179. panic(err)
  180. }
  181. registry.Apply(func(entry registry.Entry) {
  182. if _, err := o.Run(entry.Source()); err != nil {
  183. panic(err)
  184. }
  185. })
  186. return o
  187. }
  188. func (o *Otto) clone() *Otto {
  189. n := &Otto{
  190. runtime: o.runtime.clone(),
  191. }
  192. n.runtime.otto = n
  193. return n
  194. }
  195. // Run will allocate a new JavaScript runtime, run the given source
  196. // on the allocated runtime, and return the runtime, resulting value, and
  197. // error (if any).
  198. //
  199. // src may be a string, a byte slice, a bytes.Buffer, or an io.Reader, but it MUST always be in UTF-8.
  200. //
  201. // src may also be a Script.
  202. //
  203. // src may also be a Program, but if the AST has been modified, then runtime behavior is undefined.
  204. func Run(src interface{}) (*Otto, Value, error) {
  205. otto := New()
  206. value, err := otto.Run(src) // This already does safety checking
  207. return otto, value, err
  208. }
  209. // Run will run the given source (parsing it first if necessary), returning the resulting value and error (if any)
  210. //
  211. // src may be a string, a byte slice, a bytes.Buffer, or an io.Reader, but it MUST always be in UTF-8.
  212. //
  213. // If the runtime is unable to parse source, then this function will return undefined and the parse error (nothing
  214. // will be evaluated in this case).
  215. //
  216. // src may also be a Script.
  217. //
  218. // src may also be a Program, but if the AST has been modified, then runtime behavior is undefined.
  219. func (o Otto) Run(src interface{}) (Value, error) {
  220. value, err := o.runtime.cmplRun(src, nil)
  221. if !value.safe() {
  222. value = Value{}
  223. }
  224. return value, err
  225. }
  226. // Eval will do the same thing as Run, except without leaving the current scope.
  227. //
  228. // By staying in the same scope, the code evaluated has access to everything
  229. // already defined in the current stack frame. This is most useful in, for
  230. // example, a debugger call.
  231. func (o Otto) Eval(src interface{}) (Value, error) {
  232. if o.runtime.scope == nil {
  233. o.runtime.enterGlobalScope()
  234. defer o.runtime.leaveScope()
  235. }
  236. value, err := o.runtime.cmplEval(src, nil)
  237. if !value.safe() {
  238. value = Value{}
  239. }
  240. return value, err
  241. }
  242. // Get the value of the top-level binding of the given name.
  243. //
  244. // If there is an error (like the binding does not exist), then the value
  245. // will be undefined.
  246. func (o Otto) Get(name string) (Value, error) {
  247. value := Value{}
  248. err := catchPanic(func() {
  249. value = o.getValue(name)
  250. })
  251. if !value.safe() {
  252. value = Value{}
  253. }
  254. return value, err
  255. }
  256. func (o Otto) getValue(name string) Value {
  257. return o.runtime.globalStash.getBinding(name, false)
  258. }
  259. // Set the top-level binding of the given name to the given value.
  260. //
  261. // Set will automatically apply ToValue to the given value in order
  262. // to convert it to a JavaScript value (type Value).
  263. //
  264. // If there is an error (like the binding is read-only, or the ToValue conversion
  265. // fails), then an error is returned.
  266. //
  267. // If the top-level binding does not exist, it will be created.
  268. func (o Otto) Set(name string, value interface{}) error {
  269. val, err := o.ToValue(value)
  270. if err != nil {
  271. return err
  272. }
  273. return catchPanic(func() {
  274. o.setValue(name, val)
  275. })
  276. }
  277. func (o Otto) setValue(name string, value Value) {
  278. o.runtime.globalStash.setValue(name, value, false)
  279. }
  280. // SetDebuggerHandler sets the debugger handler to fn.
  281. func (o Otto) SetDebuggerHandler(fn func(vm *Otto)) {
  282. o.runtime.debugger = fn
  283. }
  284. // SetRandomSource sets the random source to fn.
  285. func (o Otto) SetRandomSource(fn func() float64) {
  286. o.runtime.random = fn
  287. }
  288. // SetStackDepthLimit sets an upper limit to the depth of the JavaScript
  289. // stack. In simpler terms, this limits the number of "nested" function calls
  290. // you can make in a particular interpreter instance.
  291. //
  292. // Note that this doesn't take into account the Go stack depth. If your
  293. // JavaScript makes a call to a Go function, otto won't keep track of what
  294. // happens outside the interpreter. So if your Go function is infinitely
  295. // recursive, you're still in trouble.
  296. func (o Otto) SetStackDepthLimit(limit int) {
  297. o.runtime.stackLimit = limit
  298. }
  299. // SetStackTraceLimit sets an upper limit to the number of stack frames that
  300. // otto will use when formatting an error's stack trace. By default, the limit
  301. // is 10. This is consistent with V8 and SpiderMonkey.
  302. //
  303. // TODO: expose via `Error.stackTraceLimit`.
  304. func (o Otto) SetStackTraceLimit(limit int) {
  305. o.runtime.traceLimit = limit
  306. }
  307. // MakeCustomError creates a new Error object with the given name and message,
  308. // returning it as a Value.
  309. func (o Otto) MakeCustomError(name, message string) Value {
  310. return o.runtime.toValue(o.runtime.newError(name, o.runtime.toValue(message), 0))
  311. }
  312. // MakeRangeError creates a new RangeError object with the given message,
  313. // returning it as a Value.
  314. func (o Otto) MakeRangeError(message string) Value {
  315. return o.runtime.toValue(o.runtime.newRangeError(o.runtime.toValue(message)))
  316. }
  317. // MakeSyntaxError creates a new SyntaxError object with the given message,
  318. // returning it as a Value.
  319. func (o Otto) MakeSyntaxError(message string) Value {
  320. return o.runtime.toValue(o.runtime.newSyntaxError(o.runtime.toValue(message)))
  321. }
  322. // MakeTypeError creates a new TypeError object with the given message,
  323. // returning it as a Value.
  324. func (o Otto) MakeTypeError(message string) Value {
  325. return o.runtime.toValue(o.runtime.newTypeError(o.runtime.toValue(message)))
  326. }
  327. // Context is a structure that contains information about the current execution
  328. // context.
  329. type Context struct {
  330. This Value
  331. Symbols map[string]Value
  332. Filename string
  333. Callee string
  334. Stacktrace []string
  335. Line int
  336. Column int
  337. }
  338. // Context returns the current execution context of the vm, traversing up to
  339. // ten stack frames, and skipping any innermost native function stack frames.
  340. func (o Otto) Context() Context {
  341. return o.ContextSkip(10, true)
  342. }
  343. // ContextLimit returns the current execution context of the vm, with a
  344. // specific limit on the number of stack frames to traverse, skipping any
  345. // innermost native function stack frames.
  346. func (o Otto) ContextLimit(limit int) Context {
  347. return o.ContextSkip(limit, true)
  348. }
  349. // ContextSkip returns the current execution context of the vm, with a
  350. // specific limit on the number of stack frames to traverse, optionally
  351. // skipping any innermost native function stack frames.
  352. func (o Otto) ContextSkip(limit int, skipNative bool) Context {
  353. // Ensure we are operating in a scope
  354. if o.runtime.scope == nil {
  355. o.runtime.enterGlobalScope()
  356. defer o.runtime.leaveScope()
  357. }
  358. curScope := o.runtime.scope
  359. frm := curScope.frame
  360. for skipNative && frm.native && curScope.outer != nil {
  361. curScope = curScope.outer
  362. frm = curScope.frame
  363. }
  364. // Get location information
  365. var ctx Context
  366. ctx.Filename = "<unknown>"
  367. ctx.Callee = frm.callee
  368. switch {
  369. case frm.native:
  370. ctx.Filename = frm.nativeFile
  371. ctx.Line = frm.nativeLine
  372. ctx.Column = 0
  373. case frm.file != nil:
  374. ctx.Filename = "<anonymous>"
  375. if p := frm.file.Position(file.Idx(frm.offset)); p != nil {
  376. ctx.Line = p.Line
  377. ctx.Column = p.Column
  378. if p.Filename != "" {
  379. ctx.Filename = p.Filename
  380. }
  381. }
  382. }
  383. // Get the current scope this Value
  384. ctx.This = objectValue(curScope.this)
  385. // Build stacktrace (up to 10 levels deep)
  386. ctx.Symbols = make(map[string]Value)
  387. ctx.Stacktrace = append(ctx.Stacktrace, frm.location())
  388. for limit != 0 {
  389. // Get variables
  390. stash := curScope.lexical
  391. for {
  392. for _, name := range getStashProperties(stash) {
  393. if _, ok := ctx.Symbols[name]; !ok {
  394. ctx.Symbols[name] = stash.getBinding(name, true)
  395. }
  396. }
  397. stash = stash.outer()
  398. if stash == nil || stash.outer() == nil {
  399. break
  400. }
  401. }
  402. curScope = curScope.outer
  403. if curScope == nil {
  404. break
  405. }
  406. if curScope.frame.offset >= 0 {
  407. ctx.Stacktrace = append(ctx.Stacktrace, curScope.frame.location())
  408. }
  409. limit--
  410. }
  411. return ctx
  412. }
  413. // Call the given JavaScript with a given this and arguments.
  414. //
  415. // If this is nil, then some special handling takes place to determine the proper
  416. // this value, falling back to a "standard" invocation if necessary (where this is
  417. // undefined).
  418. //
  419. // If source begins with "new " (A lowercase new followed by a space), then
  420. // Call will invoke the function constructor rather than performing a function call.
  421. // In this case, the this argument has no effect.
  422. //
  423. // // value is a String object
  424. // value, _ := vm.Call("Object", nil, "Hello, World.")
  425. //
  426. // // Likewise...
  427. // value, _ := vm.Call("new Object", nil, "Hello, World.")
  428. //
  429. // // This will perform a concat on the given array and return the result
  430. // // value is [ 1, 2, 3, undefined, 4, 5, 6, 7, "abc" ]
  431. // value, _ := vm.Call(`[ 1, 2, 3, undefined, 4 ].concat`, nil, 5, 6, 7, "abc")
  432. func (o Otto) Call(source string, this interface{}, argumentList ...interface{}) (Value, error) {
  433. thisValue := Value{}
  434. construct := false
  435. if strings.HasPrefix(source, "new ") {
  436. source = source[4:]
  437. construct = true
  438. }
  439. // FIXME enterGlobalScope
  440. o.runtime.enterGlobalScope()
  441. defer func() {
  442. o.runtime.leaveScope()
  443. }()
  444. if !construct && this == nil {
  445. program, err := o.runtime.cmplParse("", source+"()", nil)
  446. if err == nil {
  447. if node, ok := program.body[0].(*nodeExpressionStatement); ok {
  448. if node, ok2 := node.expression.(*nodeCallExpression); ok2 {
  449. var value Value
  450. if err = catchPanic(func() {
  451. value = o.runtime.cmplEvaluateNodeCallExpression(node, argumentList)
  452. }); err != nil {
  453. return Value{}, err
  454. }
  455. return value, nil
  456. }
  457. }
  458. }
  459. } else {
  460. value, err := o.ToValue(this)
  461. if err != nil {
  462. return Value{}, err
  463. }
  464. thisValue = value
  465. }
  466. val := thisValue
  467. fn, err := o.Run(source)
  468. if err != nil {
  469. return Value{}, err
  470. }
  471. if construct {
  472. result, err2 := fn.constructSafe(o.runtime, val, argumentList...)
  473. if err2 != nil {
  474. return Value{}, err2
  475. }
  476. return result, nil
  477. }
  478. result, err := fn.Call(val, argumentList...)
  479. if err != nil {
  480. return Value{}, err
  481. }
  482. return result, nil
  483. }
  484. // Object will run the given source and return the result as an object.
  485. //
  486. // For example, accessing an existing object:
  487. //
  488. // object, _ := vm.Object(`Number`)
  489. //
  490. // Or, creating a new object:
  491. //
  492. // object, _ := vm.Object(`({ xyzzy: "Nothing happens." })`)
  493. //
  494. // Or, creating and assigning an object:
  495. //
  496. // object, _ := vm.Object(`xyzzy = {}`)
  497. // object.Set("volume", 11)
  498. //
  499. // If there is an error (like the source does not result in an object), then
  500. // nil and an error is returned.
  501. func (o Otto) Object(source string) (*Object, error) {
  502. value, err := o.runtime.cmplRun(source, nil)
  503. if err != nil {
  504. return nil, err
  505. }
  506. if value.IsObject() {
  507. return value.Object(), nil
  508. }
  509. return nil, errors.New("value is not an object")
  510. }
  511. // ToValue will convert an interface{} value to a value digestible by otto/JavaScript.
  512. func (o Otto) ToValue(value interface{}) (Value, error) {
  513. return o.runtime.safeToValue(value)
  514. }
  515. // Copy will create a copy/clone of the runtime.
  516. //
  517. // Copy is useful for saving some time when creating many similar runtimes.
  518. //
  519. // This method works by walking the original runtime and cloning each object, scope, stash,
  520. // etc. into a new runtime.
  521. //
  522. // Be on the lookout for memory leaks or inadvertent sharing of resources.
  523. func (o *Otto) Copy() *Otto {
  524. out := &Otto{
  525. runtime: o.runtime.clone(),
  526. }
  527. out.runtime.otto = out
  528. return out
  529. }
  530. // Object is the representation of a JavaScript object.
  531. type Object struct {
  532. object *object
  533. value Value
  534. }
  535. // Call a method on the object.
  536. //
  537. // It is essentially equivalent to:
  538. //
  539. // var method, _ := object.Get(name)
  540. // method.Call(object, argumentList...)
  541. //
  542. // An undefined value and an error will result if:
  543. //
  544. // 1. There is an error during conversion of the argument list
  545. // 2. The property is not actually a function
  546. // 3. An (uncaught) exception is thrown
  547. func (o Object) Call(name string, argumentList ...interface{}) (Value, error) {
  548. // TODO: Insert an example using JavaScript below...
  549. // e.g., Object("JSON").Call("stringify", ...)
  550. function, err := o.Get(name)
  551. if err != nil {
  552. return Value{}, err
  553. }
  554. return function.Call(o.Value(), argumentList...)
  555. }
  556. // Value returns the value of o.
  557. func (o Object) Value() Value {
  558. return o.value
  559. }
  560. // Get the value of the property with the given name.
  561. func (o Object) Get(name string) (Value, error) {
  562. value := Value{}
  563. err := catchPanic(func() {
  564. value = o.object.get(name)
  565. })
  566. if !value.safe() {
  567. value = Value{}
  568. }
  569. return value, err
  570. }
  571. // Set the property of the given name to the given value.
  572. //
  573. // An error will result if the setting the property triggers an exception (i.e. read-only),
  574. // or there is an error during conversion of the given value.
  575. func (o Object) Set(name string, value interface{}) error {
  576. val, err := o.object.runtime.safeToValue(value)
  577. if err != nil {
  578. return err
  579. }
  580. return catchPanic(func() {
  581. o.object.put(name, val, true)
  582. })
  583. }
  584. // Keys gets the keys for the given object.
  585. //
  586. // Equivalent to calling Object.keys on the object.
  587. func (o Object) Keys() []string {
  588. var keys []string
  589. o.object.enumerate(false, func(name string) bool {
  590. keys = append(keys, name)
  591. return true
  592. })
  593. return keys
  594. }
  595. // KeysByParent gets the keys (and those of the parents) for the given object,
  596. // in order of "closest" to "furthest".
  597. func (o Object) KeysByParent() [][]string {
  598. var a [][]string
  599. for o := o.object; o != nil; o = o.prototype {
  600. var l []string
  601. o.enumerate(false, func(name string) bool {
  602. l = append(l, name)
  603. return true
  604. })
  605. a = append(a, l)
  606. }
  607. return a
  608. }
  609. // Class will return the class string of the object.
  610. //
  611. // The return value will (generally) be one of:
  612. //
  613. // Object
  614. // Function
  615. // Array
  616. // String
  617. // Number
  618. // Boolean
  619. // Date
  620. // RegExp
  621. func (o Object) Class() string {
  622. return o.object.class
  623. }
  624. // MarshalJSON implements json.Marshaller.
  625. func (o Object) MarshalJSON() ([]byte, error) {
  626. var goValue interface{}
  627. switch value := o.object.value.(type) {
  628. case *goStructObject:
  629. goValue = value.value.Interface()
  630. case *goMapObject:
  631. goValue = value.value.Interface()
  632. case *goArrayObject:
  633. goValue = value.value.Interface()
  634. case *goSliceObject:
  635. goValue = value.value.Interface()
  636. default:
  637. // It's a JS object; pass it to JSON.stringify:
  638. var result []byte
  639. err := catchPanic(func() {
  640. resultVal := builtinJSONStringify(FunctionCall{
  641. runtime: o.object.runtime,
  642. ArgumentList: []Value{o.value},
  643. })
  644. result = []byte(resultVal.String())
  645. })
  646. return result, err
  647. }
  648. return json.Marshal(goValue)
  649. }