Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REALMC-7470 pull latest commits from dop251/goja #3

Merged
merged 8 commits into from
Jan 6, 2021
Merged
30 changes: 2 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,34 +212,8 @@ There are two standard mappers: [TagFieldNameMapper](https://godoc.org/github.co
Native Constructors
-------------------

In order to implement a constructor function in Go:
```go
func MyObject(call goja.ConstructorCall) *Object {
// call.This contains the newly created object as per http://www.ecma-international.org/ecma-262/5.1/index.html#sec-13.2.2
// call.Arguments contain arguments passed to the function

call.This.Set("method", method)

//...

// If return value is a non-nil *Object, it will be used instead of call.This
// This way it is possible to return a Go struct or a map converted
// into goja.Value using runtime.ToValue(), however in this case
// instanceof will not work as expected.
return nil
}

runtime.Set("MyObject", MyObject)

```

Then it can be used in JS as follows:

```js
var o = new MyObject(arg);
var o1 = MyObject(arg); // same thing
o instanceof MyObject && o1 instanceof MyObject; // true
```
In order to implement a constructor function in Go use `func (goja.ConstructorCall) *goja.Object`.
See [Runtime.ToValue()](https://godoc.org/github.com/dop251/goja#Runtime.ToValue) documentation for more details.

Regular Expressions
-------------------
Expand Down
3 changes: 0 additions & 3 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/dop251/goja/file"
"github.com/dop251/goja/token"
"github.com/dop251/goja/unistring"
"github.com/go-sourcemap/sourcemap"
)

// All nodes implement the Node interface.
Expand Down Expand Up @@ -399,8 +398,6 @@ type Program struct {
DeclarationList []Declaration

File *file.File

SourceMap *sourcemap.Consumer
}

// ==== //
Expand Down
16 changes: 8 additions & 8 deletions builtin_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func arraySpeciesCreate(obj *Object, size int64) *Object {
if isArray(obj) {
v := obj.self.getStr("constructor", nil)
if constructObj, ok := v.(*Object); ok {
v = constructObj.self.getSym(symSpecies, nil)
v = constructObj.self.getSym(SymSpecies, nil)
if v == _null {
v = nil
}
Expand Down Expand Up @@ -260,7 +260,7 @@ func (r *Runtime) arrayproto_toLocaleString(call FunctionCall) Value {
}

func isConcatSpreadable(obj *Object) bool {
spreadable := obj.self.getSym(symIsConcatSpreadable, nil)
spreadable := obj.self.getSym(SymIsConcatSpreadable, nil)
if spreadable != nil && spreadable != _undefined {
return spreadable.ToBoolean()
}
Expand Down Expand Up @@ -1059,7 +1059,7 @@ func (r *Runtime) checkStdArray(v Value) *arrayObject {

func (r *Runtime) checkStdArrayIter(v Value) *arrayObject {
if arr := r.checkStdArray(v); arr != nil &&
arr.getSym(symIterator, nil) == r.global.arrayValues {
arr.getSym(SymIterator, nil) == r.global.arrayValues {

return arr
}
Expand Down Expand Up @@ -1098,7 +1098,7 @@ func (r *Runtime) array_from(call FunctionCall) Value {
}
}
var arr *Object
if usingIterator := toMethod(r.getV(items, symIterator)); usingIterator != nil {
if usingIterator := toMethod(r.getV(items, SymIterator)); usingIterator != nil {
if ctor != nil {
arr = ctor([]Value{}, nil)
} else {
Expand Down Expand Up @@ -1242,7 +1242,7 @@ func (r *Runtime) createArrayProto(val *Object) objectImpl {
r.global.arrayValues = valuesFunc
o._putProp("values", valuesFunc, true, false, true)

o._putSym(symIterator, valueProp(valuesFunc, true, false, true))
o._putSym(SymIterator, valueProp(valuesFunc, true, false, true))

bl := r.newBaseObject(nil, classObject)
bl.setOwnStr("copyWithin", valueTrue, true)
Expand All @@ -1253,7 +1253,7 @@ func (r *Runtime) createArrayProto(val *Object) objectImpl {
bl.setOwnStr("includes", valueTrue, true)
bl.setOwnStr("keys", valueTrue, true)
bl.setOwnStr("values", valueTrue, true)
o._putSym(symUnscopables, valueProp(bl.val, false, false, true))
o._putSym(SymUnscopables, valueProp(bl.val, false, false, true))

return o
}
Expand All @@ -1263,7 +1263,7 @@ func (r *Runtime) createArray(val *Object) objectImpl {
o._putProp("from", r.newNativeFunc(r.array_from, nil, "from", nil, 1), true, false, true)
o._putProp("isArray", r.newNativeFunc(r.array_isArray, nil, "isArray", nil, 1), true, false, true)
o._putProp("of", r.newNativeFunc(r.array_of, nil, "of", nil, 0), true, false, true)
o._putSym(symSpecies, &valueProperty{
o._putSym(SymSpecies, &valueProperty{
getterFunc: r.newNativeFunc(r.returnThis, nil, "get [Symbol.species]", nil, 0),
accessor: true,
configurable: true,
Expand All @@ -1276,7 +1276,7 @@ func (r *Runtime) createArrayIterProto(val *Object) objectImpl {
o := newBaseObjectObj(val, r.global.IteratorPrototype, classObject)

o._putProp("next", r.newNativeFunc(r.arrayIterProto_next, nil, "next", nil, 0), true, false, true)
o._putSym(symToStringTag, valueProp(asciiString(classArrayIterator), false, false, true))
o._putSym(SymToStringTag, valueProp(asciiString(classArrayIterator), false, false, true))

return o
}
Expand Down
2 changes: 1 addition & 1 deletion builtin_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ func (r *Runtime) createDateProto(val *Object) objectImpl {
o._putProp("toISOString", r.newNativeFunc(r.dateproto_toISOString, nil, "toISOString", nil, 0), true, false, true)
o._putProp("toJSON", r.newNativeFunc(r.dateproto_toJSON, nil, "toJSON", nil, 1), true, false, true)

o._putSym(symToPrimitive, valueProp(r.newNativeFunc(r.dateproto_toPrimitive, nil, "[Symbol.toPrimitive]", nil, 1), false, false, true))
o._putSym(SymToPrimitive, valueProp(r.newNativeFunc(r.dateproto_toPrimitive, nil, "[Symbol.toPrimitive]", nil, 1), false, false, true))

return o
}
Expand Down
2 changes: 1 addition & 1 deletion builtin_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (r *Runtime) initFunction() {
o._putProp("bind", r.newNativeFunc(r.functionproto_bind, nil, "bind", nil, 1), true, false, true)
o._putProp("call", r.newNativeFunc(r.functionproto_call, nil, "call", nil, 1), true, false, true)
o._putProp("toString", r.newNativeFunc(r.functionproto_toString, nil, "toString", nil, 0), true, false, true)
o._putSym(symHasInstance, valueProp(r.newNativeFunc(r.functionproto_hasInstance, nil, "[Symbol.hasInstance]", nil, 1), false, false, false))
o._putSym(SymHasInstance, valueProp(r.newNativeFunc(r.functionproto_hasInstance, nil, "[Symbol.hasInstance]", nil, 1), false, false, false))

r.global.Function = r.newNativeFuncConstruct(r.builtin_Function, "Function", r.global.FunctionPrototype, 1)
r.addToGlobal("Function", r.global.Function)
Expand Down
2 changes: 1 addition & 1 deletion builtin_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func (r *Runtime) initJSON() {
JSON := r.newBaseObject(r.global.ObjectPrototype, "JSON")
JSON._putProp("parse", r.newNativeFunc(r.builtinJSON_parse, nil, "parse", nil, 2), true, false, true)
JSON._putProp("stringify", r.newNativeFunc(r.builtinJSON_stringify, nil, "stringify", nil, 3), true, false, true)
JSON._putSym(symToStringTag, valueProp(asciiString(classJSON), false, false, true))
JSON._putSym(SymToStringTag, valueProp(asciiString(classJSON), false, false, true))

r.addToGlobal("JSON", JSON.val)
}
8 changes: 4 additions & 4 deletions builtin_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,15 @@ func (r *Runtime) createMapProto(val *Object) objectImpl {

entriesFunc := r.newNativeFunc(r.mapProto_entries, nil, "entries", nil, 0)
o._putProp("entries", entriesFunc, true, false, true)
o._putSym(symIterator, valueProp(entriesFunc, true, false, true))
o._putSym(symToStringTag, valueProp(asciiString(classMap), false, false, true))
o._putSym(SymIterator, valueProp(entriesFunc, true, false, true))
o._putSym(SymToStringTag, valueProp(asciiString(classMap), false, false, true))

return o
}

func (r *Runtime) createMap(val *Object) objectImpl {
o := r.newNativeConstructOnly(val, r.builtin_newMap, r.global.MapPrototype, "Map", 0)
o._putSym(symSpecies, &valueProperty{
o._putSym(SymSpecies, &valueProperty{
getterFunc: r.newNativeFunc(r.returnThis, nil, "get [Symbol.species]", nil, 0),
accessor: true,
configurable: true,
Expand All @@ -298,7 +298,7 @@ func (r *Runtime) createMapIterProto(val *Object) objectImpl {
o := newBaseObjectObj(val, r.global.IteratorPrototype, classObject)

o._putProp("next", r.newNativeFunc(r.mapIterProto_next, nil, "next", nil, 0), true, false, true)
o._putSym(symToStringTag, valueProp(asciiString(classMapIterator), false, false, true))
o._putSym(SymToStringTag, valueProp(asciiString(classMapIterator), false, false, true))

return o
}
Expand Down
2 changes: 1 addition & 1 deletion builtin_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (r *Runtime) createMath(val *Object) objectImpl {
m._putProp("PI", valueFloat(math.Pi), false, false, false)
m._putProp("SQRT1_2", valueFloat(sqrt1_2), false, false, false)
m._putProp("SQRT2", valueFloat(math.Sqrt2), false, false, false)
m._putSym(symToStringTag, valueProp(asciiString(classMath), false, false, true))
m._putSym(SymToStringTag, valueProp(asciiString(classMath), false, false, true))

m._putProp("abs", r.newNativeFunc(r.math_abs, nil, "abs", nil, 1), true, false, true)
m._putProp("acos", r.newNativeFunc(r.math_acos, nil, "acos", nil, 1), true, false, true)
Expand Down
6 changes: 3 additions & 3 deletions builtin_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func (r *Runtime) objectproto_toString(call FunctionCall) Value {
} else {
clsName = obj.self.className()
}
if tag := obj.self.getSym(symToStringTag, nil); tag != nil {
if tag := obj.self.getSym(SymToStringTag, nil); tag != nil {
if str, ok := tag.(valueString); ok {
clsName = str.String()
}
Expand Down Expand Up @@ -585,8 +585,8 @@ func (r *Runtime) initObject() {
o._putProp("setPrototypeOf", r.newNativeFunc(r.object_setPrototypeOf, nil, "setPrototypeOf", nil, 2), true, false, true)

entriesFunc := r.newNativeFunc(r.object_entries, nil, "entries", nil, 1)
o._putSym(symIterator, valueProp(entriesFunc, true, false, true))
o._putSym(symToStringTag, valueProp(asciiString(classObject), false, false, true))
o._putSym(SymIterator, valueProp(entriesFunc, true, false, true))
o._putSym(SymToStringTag, valueProp(asciiString(classObject), false, false, true))

o._putProp("values", r.newNativeFunc(r.object_values, nil, "values", nil, 1), true, false, true)
o._putProp("entries", r.newNativeFunc(r.object_entries, nil, "entries", nil, 1), true, false, true)
Expand Down
6 changes: 3 additions & 3 deletions builtin_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (r *Runtime) proxyproto_nativehandler_getOwnPropertyDescriptor(native func(
if len(call.Arguments) >= 2 {
if t, ok := call.Argument(0).(*Object); ok {
switch p := call.Argument(1).(type) {
case *valueSymbol:
case *Symbol:
return _undefined
default:
desc := native(t, p.String())
Expand Down Expand Up @@ -109,7 +109,7 @@ func (r *Runtime) proxyproto_nativehandler_gen_obj_string_bool(name proxyTrap, n
if len(call.Arguments) >= 2 {
if t, ok := call.Argument(0).(*Object); ok {
switch p := call.Argument(1).(type) {
case *valueSymbol:
case *Symbol:
return valueFalse
default:
o := native(t, p.String())
Expand All @@ -129,7 +129,7 @@ func (r *Runtime) proxyproto_nativehandler_get(native func(*Object, string, *Obj
if t, ok := call.Argument(0).(*Object); ok {
if r, ok := call.Argument(2).(*Object); ok {
switch p := call.Argument(1).(type) {
case *valueSymbol:
case *Symbol:
return _undefined
default:
return native(t, p.String(), r)
Expand Down
10 changes: 5 additions & 5 deletions builtin_regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,15 +1147,15 @@ func (r *Runtime) initRegExp() {
accessor: true,
}, false)

o._putSym(symMatch, valueProp(r.newNativeFunc(r.regexpproto_stdMatcher, nil, "[Symbol.match]", nil, 1), true, false, true))
o._putSym(symSearch, valueProp(r.newNativeFunc(r.regexpproto_stdSearch, nil, "[Symbol.search]", nil, 1), true, false, true))
o._putSym(symSplit, valueProp(r.newNativeFunc(r.regexpproto_stdSplitter, nil, "[Symbol.split]", nil, 2), true, false, true))
o._putSym(symReplace, valueProp(r.newNativeFunc(r.regexpproto_stdReplacer, nil, "[Symbol.replace]", nil, 2), true, false, true))
o._putSym(SymMatch, valueProp(r.newNativeFunc(r.regexpproto_stdMatcher, nil, "[Symbol.match]", nil, 1), true, false, true))
o._putSym(SymSearch, valueProp(r.newNativeFunc(r.regexpproto_stdSearch, nil, "[Symbol.search]", nil, 1), true, false, true))
o._putSym(SymSplit, valueProp(r.newNativeFunc(r.regexpproto_stdSplitter, nil, "[Symbol.split]", nil, 2), true, false, true))
o._putSym(SymReplace, valueProp(r.newNativeFunc(r.regexpproto_stdReplacer, nil, "[Symbol.replace]", nil, 2), true, false, true))
o.guard("exec", "global", "multiline", "ignoreCase", "unicode", "sticky")

r.global.RegExp = r.newNativeFunc(r.builtin_RegExp, r.builtin_newRegExp, "RegExp", r.global.RegExpPrototype, 2)
rx := r.global.RegExp.self
rx._putSym(symSpecies, &valueProperty{
rx._putSym(SymSpecies, &valueProperty{
getterFunc: r.newNativeFunc(r.returnThis, nil, "get [Symbol.species]", nil, 0),
accessor: true,
configurable: true,
Expand Down
8 changes: 4 additions & 4 deletions builtin_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,15 @@ func (r *Runtime) createSetProto(val *Object) objectImpl {
o._putProp("values", valuesFunc, true, false, true)
o._putProp("keys", valuesFunc, true, false, true)
o._putProp("entries", r.newNativeFunc(r.setProto_entries, nil, "entries", nil, 0), true, false, true)
o._putSym(symIterator, valueProp(valuesFunc, true, false, true))
o._putSym(symToStringTag, valueProp(asciiString(classSet), false, false, true))
o._putSym(SymIterator, valueProp(valuesFunc, true, false, true))
o._putSym(SymToStringTag, valueProp(asciiString(classSet), false, false, true))

return o
}

func (r *Runtime) createSet(val *Object) objectImpl {
o := r.newNativeConstructOnly(val, r.builtin_newSet, r.global.SetPrototype, "Set", 0)
o._putSym(symSpecies, &valueProperty{
o._putSym(SymSpecies, &valueProperty{
getterFunc: r.newNativeFunc(r.returnThis, nil, "get [Symbol.species]", nil, 0),
accessor: true,
configurable: true,
Expand All @@ -231,7 +231,7 @@ func (r *Runtime) createSetIterProto(val *Object) objectImpl {
o := newBaseObjectObj(val, r.global.IteratorPrototype, classObject)

o._putProp("next", r.newNativeFunc(r.setIterProto_next, nil, "next", nil, 0), true, false, true)
o._putSym(symToStringTag, valueProp(asciiString(classSetIterator), false, false, true))
o._putSym(SymToStringTag, valueProp(asciiString(classSetIterator), false, false, true))

return o
}
Expand Down
20 changes: 10 additions & 10 deletions builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func toString(arg Value) valueString {
if s, ok := arg.(valueString); ok {
return s
}
if s, ok := arg.(*valueSymbol); ok {
return s.desc
if s, ok := arg.(*Symbol); ok {
return s.descriptiveString()
}
return arg.toString()
}
Expand Down Expand Up @@ -345,7 +345,7 @@ func (r *Runtime) stringproto_match(call FunctionCall) Value {
r.checkObjectCoercible(call.This)
regexp := call.Argument(0)
if regexp != _undefined && regexp != _null {
if matcher := toMethod(r.getV(regexp, symMatch)); matcher != nil {
if matcher := toMethod(r.getV(regexp, SymMatch)); matcher != nil {
return matcher(FunctionCall{
ctx: r.vm.ctx,
This: regexp,
Expand All @@ -363,7 +363,7 @@ func (r *Runtime) stringproto_match(call FunctionCall) Value {
rx = r.newRegExp(regexp, nil, r.global.RegExpPrototype).self.(*regexpObject)
}

if matcher, ok := r.toObject(rx.getSym(symMatch, nil)).self.assertCallable(); ok {
if matcher, ok := r.toObject(rx.getSym(SymMatch, nil)).self.assertCallable(); ok {
return matcher(FunctionCall{
ctx: r.vm.ctx,
This: rx.val,
Expand Down Expand Up @@ -628,7 +628,7 @@ func (r *Runtime) stringproto_replace(call FunctionCall) Value {
searchValue := call.Argument(0)
replaceValue := call.Argument(1)
if searchValue != _undefined && searchValue != _null {
if replacer := toMethod(r.getV(searchValue, symReplace)); replacer != nil {
if replacer := toMethod(r.getV(searchValue, SymReplace)); replacer != nil {
return replacer(FunctionCall{
ctx: r.vm.ctx,
This: searchValue,
Expand All @@ -653,7 +653,7 @@ func (r *Runtime) stringproto_search(call FunctionCall) Value {
r.checkObjectCoercible(call.This)
regexp := call.Argument(0)
if regexp != _undefined && regexp != _null {
if searcher := toMethod(r.getV(regexp, symSearch)); searcher != nil {
if searcher := toMethod(r.getV(regexp, SymSearch)); searcher != nil {
return searcher(FunctionCall{
ctx: r.vm.ctx,
This: regexp,
Expand All @@ -671,7 +671,7 @@ func (r *Runtime) stringproto_search(call FunctionCall) Value {
rx = r.newRegExp(regexp, nil, r.global.RegExpPrototype).self.(*regexpObject)
}

if searcher, ok := r.toObject(rx.getSym(symSearch, nil)).self.assertCallable(); ok {
if searcher, ok := r.toObject(rx.getSym(SymSearch, nil)).self.assertCallable(); ok {
return searcher(FunctionCall{
ctx: r.vm.ctx,
This: rx.val,
Expand Down Expand Up @@ -728,7 +728,7 @@ func (r *Runtime) stringproto_split(call FunctionCall) Value {
separatorValue := call.Argument(0)
limitValue := call.Argument(1)
if separatorValue != _undefined && separatorValue != _null {
if splitter := toMethod(r.getV(separatorValue, symSplit)); splitter != nil {
if splitter := toMethod(r.getV(separatorValue, SymSplit)); splitter != nil {
return splitter(FunctionCall{
ctx: r.vm.ctx,
This: separatorValue,
Expand Down Expand Up @@ -910,7 +910,7 @@ func (r *Runtime) createStringIterProto(val *Object) objectImpl {
o := newBaseObjectObj(val, r.global.IteratorPrototype, classObject)

o._putProp("next", r.newNativeFunc(r.stringIterProto_next, nil, "next", nil, 0), true, false, true)
o._putSym(symToStringTag, valueProp(asciiString(classStringIterator), false, false, true))
o._putSym(SymToStringTag, valueProp(asciiString(classStringIterator), false, false, true))

return o
}
Expand Down Expand Up @@ -950,7 +950,7 @@ func (r *Runtime) initString() {
o._putProp("trimStart", r.newNativeFunc(r.stringproto_trimStart, nil, "trimStart", nil, 0), true, false, true)
o._putProp("valueOf", r.newNativeFunc(r.stringproto_valueOf, nil, "valueOf", nil, 0), true, false, true)

o._putSym(symIterator, valueProp(r.newNativeFunc(r.stringproto_iterator, nil, "[Symbol.iterator]", nil, 0), true, false, true))
o._putSym(SymIterator, valueProp(r.newNativeFunc(r.stringproto_iterator, nil, "[Symbol.iterator]", nil, 0), true, false, true))

// Annex B
o._putProp("substr", r.newNativeFunc(r.stringproto_substr, nil, "substr", nil, 2), true, false, true)
Expand Down
Loading