Skip to content

Commit

Permalink
Fixed __proto__ getter.
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed Feb 29, 2020
1 parent 2a7d122 commit 281a94b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion builtin_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (r *Runtime) builtinJSON_decodeObject(d *json.Decoder) (*Object, error) {
return nil, err
}

if key == "__proto__" {
if key == __proto__ {
descr := propertyDescr{
Value: value,
Writable: FLAG_TRUE,
Expand Down
2 changes: 1 addition & 1 deletion compiler_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ func (e *compiledObjectLiteral) emitGetter(putOnStack bool) {
e.c.compileExpression(prop.Value).emitGetter(true)
switch prop.Kind {
case "value":
if prop.Key == "__proto__" {
if prop.Key == __proto__ {
e.c.emit(setProto)
} else {
e.c.emit(setProp1(prop.Key))
Expand Down
4 changes: 2 additions & 2 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (o *baseObject) put(n Value, val Value, throw bool) {

func (o *baseObject) getOwnProp(name string) Value {
v := o.values[name]
if v == nil && name == "__proto" {
if v == nil && name == __proto__ {
return o.prototype
}
return v
Expand All @@ -233,7 +233,7 @@ func (o *baseObject) putStr(name string, val Value, throw bool) {
return
}

if name == "__proto__" {
if name == __proto__ {
if !o.extensible {
o.val.runtime.typeErrorResult(throw, "%s is not extensible", o.val)
return
Expand Down
7 changes: 7 additions & 0 deletions runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,13 @@ func TestAutoBoxing(t *testing.T) {
testScript1(SCRIPT, valueTrue, t)
}

func TestProtoGetter(t *testing.T) {
const SCRIPT = `
({}).__proto__ === Object.prototype && [].__proto__ === Array.prototype;
`
testScript1(SCRIPT, valueTrue, t)
}

/*
func TestArrayConcatSparse(t *testing.T) {
function foo(a,b,c)
Expand Down
6 changes: 5 additions & 1 deletion string.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"unicode/utf8"
)

const (
__proto__ = "__proto__"
)

var (
stringTrue valueString = asciiString("true")
stringFalse valueString = asciiString("false")
Expand All @@ -22,7 +26,7 @@ var (
stringPlusInfinity = asciiString("+Infinity")
stringNegInfinity = asciiString("-Infinity")
stringEmpty valueString = asciiString("")
string__proto__ valueString = asciiString("__proto__")
string__proto__ valueString = asciiString(__proto__)

stringError valueString = asciiString("Error")
stringTypeError valueString = asciiString("TypeError")
Expand Down
2 changes: 1 addition & 1 deletion vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ type _setProto struct{}
var setProto _setProto

func (_setProto) exec(vm *vm) {
vm.r.toObject(vm.stack[vm.sp-2]).self.putStr("__proto__", vm.stack[vm.sp-1], true)
vm.r.toObject(vm.stack[vm.sp-2]).self.putStr(__proto__, vm.stack[vm.sp-1], true)

vm.sp--
vm.pc++
Expand Down

0 comments on commit 281a94b

Please sign in to comment.