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

57 Commits

Author SHA1 Message Date
Steven Hartland
026a1d9a9c
chore: lint and naming refactor (#475)
Enable more linters, address the issues and do a major naming refactor
to use golang lower camelCase identifiers for types, functions, methods
and variable names.

Also: 
* Clean up inline generation so it doesn't rely on temporary variables.
* Remove unused functions generated by inline.pl.
2022-12-04 21:49:38 +00:00
Steven Hartland
ad8875609a
feat: use exported methods when setting go values (#469)
When putting JavaScript objects into _go*object use exported values.
This reverts PR #467 in favour of this more complete work.

Fixes #118 #252

Co-authored-by: Dmitry Panov <dop@itoolabs.com>
2022-11-28 20:58:40 +00:00
Steven Hartland
ec2dc5ec24
fix: map export (#467)
When exporting a map check for values which are otto.Value and process
them. This is a one level export and no additional traversal is done.

Fixes #252
2022-11-28 17:21:20 +00:00
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
Douglas Thrift
12a6322603
fix: export when an array has multiple types (#387)
Fix a panic in value export when an array has multiple types of arrays.

This takes into account the types of elements in sub arrays, slices, etc.

Fixes #279
Fixes #377
2021-10-04 14:44:30 +01:00
Iskander (Alex) Sharipov
ca9b4f9067
fix: replace single case type switches with if (#322)
Replace single case type switches with if statement.

Found using https://go-critic.github.io/overview.html#singleCaseSwitch-ref
2021-09-27 16:47:34 +01:00
Steven Hartland
9297a9abe4
feat: add github action tests and linting (#418)
Leverage github actions for tests and linting.

This includes fixing a bunch of issues highlighted by golangci
including:
* Dead code.
* Ineffectual assigns.
* Goto warnings.
* Nil return err.
* Reused literal strings.
* Test parameter order.

Also:
* Setup clog.
2021-09-27 16:19:28 +01:00
Steven Hartland
209b1fb8e8
Revert "feat: support null value returns for toValue (#325)" (#417)
This reverts commit 26cb6ccce6.
2021-09-27 12:00:52 +01:00
Ryan
26cb6ccce6
feat: support null value returns for toValue (#325)
Add nullValue as the return value for the nil case in toValue.

Co-authored-by: Ryan Macleod <ryan.macleod@cumul8.com>
2021-09-27 11:47:21 +01:00
Alexander F Rødseth
e3f7ae2533 Consistent use of "value Value"/"vl Value" 2016-06-24 09:53:47 +02: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
f271e0183c don't try to construct a common type array if there is no type 2016-04-25 11:58:49 +10:00
deoxxa
8dcf49dfe7 amend type sniffing to work with null 2016-04-25 11:58:30 +10:00
deoxxa
da054efc5f fixes #165 - crash exporting an array with undefined as the first element 2016-04-04 00:25:24 +10:00
Steven Hartland
526e6b2be2 Merge pull request #106 from multiplay/float-precision
Prevent loss of float precision
2015-12-01 13:13:51 +00:00
Steven Hartland
e378501ad1 Merge pull request #108 from multiplay/array-types
Export arrays to common type if possible
2015-12-01 13:13:00 +00:00
Robert Krimen
7597815bd0 Fix toReflectValue(0) => float32
This fixes #123
2015-05-07 22:53:02 -07:00
Steven Hartland
abcc601db6 Prevent loss of float precision
When automatically converting a float to int type ensure we don't loose any precision.
2015-02-19 11:33:21 +00:00
Steven Hartland
72d92c8e19 Export arrays to common type if possible
If there is a common type for all values in an array export to a slice of that type otherwise return a slice of interfaces.
2015-02-17 23:18:23 +00: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
709a0aa7ff Value.string(), .float64(), .bool(), etc. 2014-06-06 21:29:40 -07:00
Robert Krimen
b813fe3676 Fix JSON.stringify treating large integers as float64
This fixes #80
2014-06-06 21:08:59 -07:00
Robert Krimen
6c7c425424 Fix toReflectValue to handle Struct, Map, Slice, etc.
This fixes #79
2014-06-04 18:08:42 -07:00
Robert Krimen
98be5e74da go vet
This fixes #76

I cheated on some of these.
2014-05-28 21:46:08 -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
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
Daniel Cannon
661a61c5a0 Fix toValue not properly converting type aliases
This fixes #61
2014-03-16 08:46:20 -07:00
Robert Krimen
41e5803a57 Fix Value.isArray => isArray 2014-02-01 11:09:14 -08:00
Robert Krimen
3ba06f003c Fix NaN/+0/-0 comparison in Array.prototype.indexOf 2014-01-24 21:20:22 -08:00
Robert Krimen
4c1bc5857b Fix documentation (nikai3d)
This fixes #45

The errors are manifest in the README, but this is automatically generated from the go documentation, so we have to fix them at the source
2014-01-19 10:52:14 -08:00
Daniel Cannon
fd6b047e2b Updated toValue to properly handle nil pointers (dancannon)
Also, added test
2014-01-18 17:35:26 -08:00
Robert Krimen
091c371aea Change handling of undefined in .Export
This fixes #28
2013-06-22 16:33:41 +02:00
Robert Krimen
4ebf6416d0 Fix Uint32 Array/String indexing
Also, a bunch of toValue_* streamlining
And maybe a few miscellaneous tweaks
2013-06-22 15:49:22 +02:00
Robert Krimen
73bf2f9fa8 Implement return/break/continue without panic/recover
This fixes #25
2013-06-13 22:17:04 -07:00
Robert Krimen
c55510cb36 Inline context initialization & improve _object.value
* Context setup is now done via _newContext.
* _newContext is a function that resides in inline.go. _newContext is very flat,
resulting in almost no function calls (a 180 from the earlier status quo).
* inline.go is a Go source file that is built by Perl (via inline).
* Lots of crufty functions removed (along with all of their TODO & FIXME).
* In addition, before, the underlying value of _object.value was a pointer to
something. This made for extra work, since the type of _object.value is interface{},
which is already something of a pointer. Now, the underlying value of _object.value
in Function, Date, RegExp, ..., is a struct value.
* type_function.go was streamlined, removing superfluous struct fields and methods.
* There is now less "digging" to get to the actual value of a function, which is important
when makings lots of calls.

Before (without inline):

    PASS
    BenchmarkNew        2000           1067871 ns/op
    ok      github.com/robertkrimen/otto    3.336s
    PASS
    BenchmarkNew        2000           1077644 ns/op
    ok      github.com/robertkrimen/otto    3.367s

After (with inline):

    PASS
    BenchmarkNew       10000            364418 ns/op
    ok      github.com/robertkrimen/otto    4.616s
    PASS
    BenchmarkNew       10000            307241 ns/op
    ok      github.com/robertkrimen/otto    4.051s

This (partially) fixes #22
2013-06-09 18:28:18 -07:00
Robert Krimen
30e4c4b96d Fix .ToValue([]string{...}).Export() to return []string{...}
This fixes #24
2013-06-06 20:18:37 -07:00
Robert Krimen
7ff3e8668e Allow (internal) enumeration through _every_ Object property
For .freeze, .isFrozen, etc.
2013-06-02 15:21:25 -07:00
Robert Krimen
f745b98f2b Handle struct, map, & array/slice in exportNative 2013-05-30 18:57:33 -07:00
Robert Krimen
58eefe5c3d Handle toReflectValue of interface{}
Fix #19
2013-05-29 22:45:01 -07:00
Robert Krimen
83c56dd73d Add Otto.Call 2013-05-19 19:03:05 -07:00
Robert Krimen
3c139264ef Handle ToValue(nil) 2013-05-19 17:43:08 -07:00
Robert Krimen
adf21d0208 No rush to change the interface of Value.Export() 2013-05-15 19:30:39 -07:00
Robert Krimen
faaed09553 Change Export() to have new interface and slightly different behavior 2013-05-15 19:04:39 -07:00
Robert Krimen
7e2b4f2063 Rewrite of property handling to be more robust and compliant
* No more _stash
* Now using a "virtual table" system via _objectClass
* Make Array.concat GoArray compatible (via .isArray())

Fix #16
2013-05-12 14:14:51 -07:00
Robert Krimen
6c5259a651 Add Object.toLocaleString 2013-05-02 21:06:22 +02:00
Robert Krimen
4c85422172 Make encodeURI correspond to the 15.1.3.3 algorithm and encodeURIComponent ito 15.1.3.4 2013-05-02 20:23:06 +02:00
Robert Krimen
f3da7237f2 Add Object.getPrototypeOf 2013-03-05 12:03:33 -08:00