Browse Source

Made wrapped simple maps extensible.

Dmitry Panov 8 years ago
parent
commit
c77e15cd9a
2 changed files with 35 additions and 1 deletions
  1. 3 1
      object_gomap.go
  2. 32 0
      object_gomap_test.go

+ 3 - 1
object_gomap.go

@@ -14,6 +14,7 @@ func (o *objectGoMapSimple) init() {
 	o.baseObject.init()
 	o.baseObject.init()
 	o.prototype = o.val.runtime.global.ObjectPrototype
 	o.prototype = o.val.runtime.global.ObjectPrototype
 	o.class = classObject
 	o.class = classObject
+	o.extensible = true
 }
 }
 
 
 func (o *objectGoMapSimple) _get(n Value) Value {
 func (o *objectGoMapSimple) _get(n Value) Value {
@@ -73,8 +74,9 @@ func (o *objectGoMapSimple) _has(n Value) bool {
 func (o *objectGoMapSimple) putStr(name string, val Value, throw bool) {
 func (o *objectGoMapSimple) putStr(name string, val Value, throw bool) {
 	if o.extensible || o._hasStr(name) {
 	if o.extensible || o._hasStr(name) {
 		o.data[name] = val.Export()
 		o.data[name] = val.Export()
+	} else {
+		o.val.runtime.typeErrorResult(throw, "Host object is not extensible")
 	}
 	}
-	o.val.runtime.typeErrorResult(throw, "Host object is not extensible")
 }
 }
 
 
 func (o *objectGoMapSimple) hasProperty(n Value) bool {
 func (o *objectGoMapSimple) hasProperty(n Value) bool {

+ 32 - 0
object_gomap_test.go

@@ -145,6 +145,38 @@ func TestGomapProto(t *testing.T) {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
 
 
+	if !v.StrictEquals(valueTrue) {
+		t.Fatalf("Expected true, got %v", v)
+	}
+}
+
+func TestGoMapExtensibility(t *testing.T) {
+	const SCRIPT = `
+	"use strict";
+	o.test = 42;
+	Object.preventExtensions(o);
+	o.test = 43;
+	try {
+		o.test1 = 42;
+	} catch (e) {
+		if (!(e instanceof TypeError)) {
+			throw e;
+		}
+	}
+	o.test === 43 && o.test1 === undefined;
+	`
+
+	r := New()
+	r.Set("o", map[string]interface{}{})
+	v, err := r.RunString(SCRIPT)
+	if err != nil {
+		if ex, ok := err.(*Exception); ok {
+			t.Fatal(ex.String())
+		} else {
+			t.Fatal(err)
+		}
+	}
+
 	if !v.StrictEquals(valueTrue) {
 	if !v.StrictEquals(valueTrue) {
 		t.Fatalf("Expected true, got %v", v)
 		t.Fatalf("Expected true, got %v", v)
 	}
 	}