Browse Source

Merge branch 'master' into es6

# Conflicts:
#	runtime_test.go
Dmitry Panov 5 years ago
parent
commit
83452bfa75
2 changed files with 47 additions and 3 deletions
  1. 27 3
      func.go
  2. 20 0
      runtime_test.go

+ 27 - 3
func.go

@@ -36,17 +36,41 @@ func (f *nativeFuncObject) exportType() reflect.Type {
 	return reflect.TypeOf(f.f)
 	return reflect.TypeOf(f.f)
 }
 }
 
 
-func (f *funcObject) getPropStr(name string) Value {
-	switch name {
-	case "prototype":
+func (f *funcObject) _addProto(n string) Value {
+	if n == "prototype" {
 		if _, exists := f.values["prototype"]; !exists {
 		if _, exists := f.values["prototype"]; !exists {
 			return f.addPrototype()
 			return f.addPrototype()
 		}
 		}
 	}
 	}
+	return nil
+}
+
+func (f *funcObject) getPropStr(name string) Value {
+	if v := f._addProto(name); v != nil {
+		return v
+	}
 
 
 	return f.baseObject.getPropStr(name)
 	return f.baseObject.getPropStr(name)
 }
 }
 
 
+func (f *funcObject) putStr(name string, val Value, throw bool) {
+	f._addProto(name)
+	f.baseObject.putStr(name, val, throw)
+}
+
+func (f *funcObject) put(n Value, val Value, throw bool) {
+	f.putStr(n.String(), val, throw)
+}
+
+func (f *funcObject) deleteStr(name string, throw bool) bool {
+	f._addProto(name)
+	return f.baseObject.deleteStr(name, throw)
+}
+
+func (f *funcObject) delete(n Value, throw bool) bool {
+	return f.deleteStr(n.String(), throw)
+}
+
 func (f *funcObject) addPrototype() Value {
 func (f *funcObject) addPrototype() Value {
 	proto := f.val.runtime.NewObject()
 	proto := f.val.runtime.NewObject()
 	proto.self._putProp("constructor", f.val, true, false, true)
 	proto.self._putProp("constructor", f.val, true, false, true)

+ 20 - 0
runtime_test.go

@@ -1230,6 +1230,26 @@ func TestProtoGetter(t *testing.T) {
 	testScript1(SCRIPT, valueTrue, t)
 	testScript1(SCRIPT, valueTrue, t)
 }
 }
 
 
+func TestFuncProto(t *testing.T) {
+	const SCRIPT = `
+	"use strict";
+	function A() {}
+	A.__proto__ = Object;
+	A.prototype = {};
+
+	function B() {}
+	B.__proto__ = Object.create(null);
+	var thrown = false;
+	try {
+		delete B.prototype;
+	} catch (e) {
+		thrown = e instanceof TypeError;
+	}
+	thrown;
+	`
+	testScript1(SCRIPT, valueTrue, t)
+}
+
 func TestSymbol1(t *testing.T) {
 func TestSymbol1(t *testing.T) {
 	const SCRIPT = `
 	const SCRIPT = `
 		Symbol.toPrimitive[Symbol.toPrimitive]() === Symbol.toPrimitive;
 		Symbol.toPrimitive[Symbol.toPrimitive]() === Symbol.toPrimitive;