1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00
Commit Graph

69 Commits

Author SHA1 Message Date
Steven Hartland
a3b51c68f2
chore: enable whitespace linter and fix errors (#456)
Enable the whitespace linter and fix the errors resulting from it.

Also:
* Remove unneeded golangci-lint config for disabled linters.
* Correct test number for panic in previous commit.
2022-11-26 13:33:09 +00:00
Jens Alfke
09fc211e5a
feat: implement MarshalJSON for Value and Object (#437)
Value and Object now conform to the json.Marshaler interface and produce the
correct JSON when passed (directly or indirectly) to json.Marshal().

Before, both types would marshal as "{}" because they're structs with no public
fields.

This fixes some nasty problems marshaling object trees that mix Go and JS
collections. This can happen when you pass a Go collection into a JS function,
which modifies it adding JS values; and then back in Go you marshal the
collection to JSON. Before this commit the JS values would marshal to "{}".
(The new unit test TestNestedJSONMarshaling demonstrates this.)

Fixes #262
2022-10-11 18:56:42 +01:00
Steven Hartland
7009038f79
fix: linting errors (#441)
Disable new linters which aren't compatible with this code module.

Upgrade github actions to fix caching issues.

Run go mod to bring in new styling.

Remove space on nolint declarations.

Apply all changes to whitespace as required to pass goimports linter.

Only trigger checks on pull_request which works for pulls from other
forks, where as push only works from the same repo.
2022-10-08 00:12:19 +01:00
Andy Hochhaus
29f673c4d2 Document that otto targets ES5 2016-09-18 17:59:18 -07:00
deoxxa
ccf93984d9 implement configurable stack trace limit 2016-05-08 18:13:57 +10:00
deoxxa
668c95f04e make call stacks aware of native functions
* add stackFramesToPop argument to error factories
* put native functions in their own stack frames
* add tests for native stack frames
* amend Context functionality to account for native frames
2016-05-07 20:55:09 +10:00
deoxxa
bd5fb254e3 add source map support
This patch implements source map support in the parser, the runtime, the
script record, and the stack trace printing.

The library used to parse and use the source maps is gopkg.in/sourcemap.v1.
Unlike earlier versions of this patch, the consumer of otto does not need
parse the source map on their own - it's now handled similarly to parsing
JavaScript content.

To use a source map, the consumer must explicitly parse their source into
a `Script` object with `Otto.CompileWithSourceMap`. The script record
returned from that call will carry source map information with it, and
all location-related functions should reflect the original source
positions.
2016-05-02 19:34:04 +10:00
Conrad Pankoff
a55c9c9461 Merge pull request #176 from deoxxa/add-autocomplete-to-repl
add tab-complete to repl
2016-05-02 19:23:55 +10:00
deoxxa
a7e69fb1ca add error creator functions
This change adds a handful of functions to `otto.Otto` type that make
it easier to create proper `Error` values from native code. Previously,
the only way to do this was to call the error's constructor from
JavaScript like `vm.Call("TypeError", "message")`. `Call` can fail for
various reasons, and also modifies the current call stack.

These new functions can't fail, and since they don't involve any
JavaScript execution, won't modify the call stack. The new functions
are:

* `MakeCustomError(name, message string) Value`
* `MakeRangeError(message string) Value`
* `MakeSyntaxError(message string) Value`
* `MakeTypeError(message string) Value`

`MakeCustomError` creates an `Error` object with a specific `name` value.
The other functions cover some common error types, and call specific
functions in the runtime to construct errors with the correct prototypes.
If we need to implement any other error types, it'll mostly be copy/paste.
2016-04-30 12:45:33 +10:00
deoxxa
9ce459fe6b amend docs on Keys/KeysByParent 2016-04-28 22:44:27 +10:00
deoxxa
6e7c8df250 add KeysByParent function for recursively getting object keys 2016-04-27 16:08:07 +10:00
deoxxa
029a80224c document SetStackDepthLimit 2016-04-25 11:36:52 +10:00
deoxxa
c142472392 add simple stack depth limit mechanism 2016-04-04 00:07:41 +10:00
deoxxa
fd1eddd0f9 add support for an external Math.random() provider 2016-02-18 22:08:25 +11:00
Andrew Montgomery-Hurrell
45c7a8df39 Add Context method to aid debugging
This change introduces a Context method to otto that allows developers to get
information about the current execution context. The method returns a Context
struct that contains information such as the filename, line and column of the
current execution, the current value of this, the stacktrace and the available
symbols at the current context.
2015-12-05 18:03:36 +00:00
Conrad Pankoff
4a7676e51c Merge pull request #142 from deoxxa/add-debugger-hook
add a way to trigger something with `debugger`
2015-12-05 21:14:00 +11:00
deoxxa
44a4b3115d support and test Eval() without an existing scope 2015-12-05 20:24:12 +11:00
deoxxa
7f15b1724e add a way to trigger something with debugger 2015-12-05 19:25:33 +11:00
deoxxa
72211f7dbb add Eval() to execute code in the current vm scope 2015-12-05 19:05:21 +11:00
Robert Krimen
e6768252c2 Improve error reporting
* Delay entering global scope on code evaluation, not runtime creation

This fixes #66
2014-06-12 21:27:32 -07:00
Robert Krimen
1a2fa0b815 Fix "Halting Problem" example
As discussed in #71, the original example contains a race condition that could cause the goroutine to block forever.
2014-05-31 13:38:11 -07:00
Robert Krimen
f09ce5eac2 Add mutex locking for .Copy() 2014-05-30 20:27:30 -07:00
Robert Krimen
918abeb8d8 The zero value of Value is now defined to be undefined
Previously, it was a publically accessible but invalid value (valueEmpty).

* Deprecate internal use of UndefinedValue(), NullValue(), FalseValue(), TrueValue()
* Guard against Empty, Result, Reference values from escaping the package
2014-05-28 21:23:32 -07:00
Robert Krimen
9cd045ef04 Simplification & refactor of (parts of) the runtime
* Proper lowercasing for internal stuff
* *Environment => *_stash
* ExecutionContext => _scope
* Simpler & shallower call/construct mechanics
* Remove unnecessary fields & methods
* Better scoping (no more stack): []*_scope => _scope.outer
* Some speed improvements

In preparation for #66
2014-05-27 22:05:35 -07:00
Robert Krimen
6d506b4b2f Update documention to mention natto 2014-04-26 22:49:27 -07:00
Robert Krimen
bf7b16f4a3 Add internal compilation step
* Streamline what we get from "otto/parser"
* Get rid of some "otto/parser" cruft
* FunctionExpression => FunctionLiteral
* The debugger statement (debugger) should do nothing (not panic)
* Fix aspects of function expression call evaluation
2014-04-19 14:05:51 -07:00
Robert Krimen
7721396b67 Fix Date tests to always mock UTC 2014-04-13 15:07:29 -07:00
Robert Krimen
ddca88af9b Add compilation (Script, vm.Compile, etc.) 2014-04-13 15:04:32 -07:00
Robert Krimen
ba4c8b0769 Fix documentation 2014-04-11 18:23:34 -07:00
Robert Krimen
f04cfab02d Add ability to parse []byte, *bytes.Buffer, io.Reader 2014-04-11 18:07:57 -07:00
Robert Krimen
ad8a97c028 New parser
* Faster, more straightforward, etc.
* More advanced object literals (get ..., set ...)
* More tests using JavaScript from the wild (http://cdnjs.com/)
2014-04-10 20:42:25 -07:00
Robert Krimen
ec106f69e0 Add Object.Keys()
This fixes #62
2014-04-03 18:25:53 -07:00
Robert Krimen
70e6bec5e0 Make sure to set the Otto/runtime dependency when copying
This is a problem in #57
2014-02-16 21:22:30 -08:00
Robert Krimen
f0159cfec0 Add note about setTimeout/setInterval
This involves #53
2014-02-01 16:52:57 -08:00
Robert Krimen
096cd7e450 Add ability to interrupt the runtime
This fixes #12, #35
2014-02-01 15:49:17 -08:00
Robert Krimen
8cd34bce0a Fix groups.google.com link 2014-02-01 12:05:08 -08:00
Robert Krimen
4fc2e41b28 Add link to godoc.org
This fixes #52
2014-02-01 12:01:52 -08:00
Robert Krimen
ea621687a4 Add Otto.Copy() 2013-07-14 14:20:01 -07:00
Robert Krimen
d42dcb705e Fix documentation
This fixes #26
2013-06-09 18:58:24 -07:00
Robert Krimen
10bb73298b Edit documentation 2013-05-19 23:03:45 -07:00
Robert Krimen
18630616eb Remove throw behavior from Otto.Call 2013-05-19 21:08:32 -07:00
Robert Krimen
83c56dd73d Add Otto.Call 2013-05-19 19:03:05 -07:00
Robert Krimen
791a2c0c8e Add FunctionCall.Otto 2013-05-19 14:57:01 -07:00
Robert Krimen
699232d49a Consolidate run/runSafe into _runtime 2013-05-18 16:18:17 -07:00
Robert Krimen
7b0eac16f9 Add Number.toPrecision 2013-03-07 08:42:48 -08:00
Robert Krimen
e5f0da1420 Add Number.toExponential 2013-03-07 08:34:12 -08:00
Robert Krimen
2e7e3e17fd Add Number.toFixed 2013-03-07 08:22:23 -08:00
Robert Krimen
795cc4935d Add remarks about ToValue & Otto.ToValue 2013-02-04 10:41:17 -08:00
Robert Krimen
a879744c20 Add Go <=> JavaScript type interaction
Via reflection for struct, map, and slice/array
Fix #10
2013-02-04 10:31:44 -08:00
Robert Krimen
38f6d71ff2 Remove remarks about UTF-16 incompatibility 2013-01-27 10:29:57 -08:00