Skip to content

Commit

Permalink
REALMC-7470 pull latest commits from dop251/goja (dop251#3)
Browse files Browse the repository at this point in the history
* Do not create an object if a native constructor is called without 'new'. Closes dop251#228.

* Fixed documentation

* Exposed Symbol

* Added Runtime.NewArray()

* Detect source map URL when the source is wrapped in "(function() { ... })". See dop251#235.

* Introduced options for parser. Added options to set a custom source map loader or to disable source map support. See dop251#235.

* Use source name provided by source map. See dop251#235.

Co-authored-by: Dmitry Panov <[email protected]>
  • Loading branch information
2 people authored and tsedgwick committed Feb 24, 2021
1 parent 710cb80 commit 53a4571
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 11 deletions.
6 changes: 3 additions & 3 deletions builtin_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,9 @@ func (r *Runtime) initObject() {
o._putProp("setPrototypeOf", r.newNativeFunc(r.object_setPrototypeOf, nil, "setPrototypeOf", nil, 2), true, false, true)
o._putProp("values", r.newNativeFunc(r.object_values, nil, "values", nil, 1), 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))
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._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
39 changes: 39 additions & 0 deletions builtin_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,42 @@ func (r *Runtime) initProxy() {
r.global.Proxy = r.newLazyObject(r.createProxy)
r.addToGlobal("Proxy", r.global.Proxy)
}

func (np *nativeProxyHandler) MemUsage(ctx *MemUsageContext) (uint64, error) {
// p := np.handler
// if p == nil || ctx.IsObjVisited(p) {
// return SizeEmpty, nil
// }
// ctx.VisitObj(p)

// if err := ctx.Descend(); err != nil {
// return 0, err
// }

// total := SizeEmpty
// inc, baseObjetErr := p.baseObject.MemUsage(ctx)
// total += inc
// if baseObjetErr != nil {
// return total, baseObjetErr
// }

// if p.target != nil {
// inc, err := p.target.MemUsage(ctx)
// total += inc
// if err != nil {
// return total, err
// }
// }

// if p.handler != nil {
// inc, err := p.handler.MemUsage(ctx)
// total += inc
// if err != nil {
// return total, err
// }
// }

// ctx.Ascend()

return 0, nil
}
2 changes: 2 additions & 0 deletions builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ func (r *Runtime) stringproto_match(call FunctionCall) Value {

if matcher, ok := r.toObject(rx.getSym(SymMatch, nil)).self.assertCallable(); ok {
return matcher(FunctionCall{
ctx: r.vm.ctx,
This: rx.val,
Arguments: []Value{call.This.toString()},
})
Expand All @@ -388,6 +389,7 @@ func (r *Runtime) stringproto_matchAll(call FunctionCall) Value {
}
if matcher := toMethod(r.getV(regexp, SymMatchAll)); matcher != nil {
return matcher(FunctionCall{
ctx: r.vm.ctx,
This: regexp,
Arguments: []Value{call.This},
})
Expand Down
40 changes: 40 additions & 0 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ type proxyHandler interface {
construct(target *Object, args []Value, newTarget *Object) (Value, bool)

toObject(*Runtime) *Object

MemUsage(ctx *MemUsageContext) (uint64, error)
}

type jsProxyHandler struct {
Expand Down Expand Up @@ -1078,3 +1080,41 @@ func (p *proxyObject) MemUsage(ctx *MemUsageContext) (uint64, error) {

return total, nil
}

func (p *jsProxyHandler) MemUsage(ctx *MemUsageContext) (uint64, error) {
if p == nil || ctx.IsObjVisited(p.handler.self) {
return SizeEmpty, nil
}
ctx.VisitObj(p.handler.self)

if err := ctx.Descend(); err != nil {
return 0, err
}

total := SizeEmpty
// inc, baseObjetErr := p.handler.MemUsage(ctx)
// total += inc
// if baseObjetErr != nil {
// return total, baseObjetErr
// }

// if p.target != nil {
// inc, err := p.target.MemUsage(ctx)
// total += inc
// if err != nil {
// return total, err
// }
// }

if p.handler != nil {
inc, err := p.handler.MemUsage(ctx)
total += inc
if err != nil {
return total, err
}
}

ctx.Ascend()

return total, nil
}
15 changes: 7 additions & 8 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,13 @@ func (o valueUnresolved) assertString() (valueString, bool) {
return nil, false
}

func (s *Symbol) IsNumber() bool {
return false
}

func (s *Symbol) IsObject() bool {
return false
}
func (s *Symbol) ToInteger() int64 {
panic(typeError("Cannot convert a Symbol value to a number"))
}
Expand Down Expand Up @@ -1488,10 +1495,6 @@ func (s *Symbol) ToNumber() Value {
panic(typeError("Cannot convert a Symbol value to a number"))
}

func (s *Symbol) IsNumber() bool {
return false
}

func (s *Symbol) ToBoolean() bool {
return true
}
Expand Down Expand Up @@ -1535,10 +1538,6 @@ func (s *Symbol) MemUsage(ctx *MemUsageContext) (uint64, error) {
return 0, nil
}

func (s *Symbol) IsObject() bool {
return false
}

func exportValue(v Value, ctx *objectExportCtx) interface{} {
if obj, ok := v.(*Object); ok {
return obj.self.export(ctx)
Expand Down

0 comments on commit 53a4571

Please sign in to comment.