Skip to content

Commit

Permalink
fix OCD and use constructor for share arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Dec 10, 2020
1 parent 1b56ea9 commit d453b82
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func newBoundInitContext(base *InitContext, ctxPtr *context.Context, rt *goja.Ru
// NewShare ...
// TODO rename
// TODO constructor?
func (i *InitContext) NewShare(name string, call goja.Callable) goja.Value {
func (i *InitContext) XShare(name string, call goja.Callable) goja.Value {
i.sharesLock.Lock()
defer i.sharesLock.Unlock()
value, ok := i.shares[name]
Expand Down
8 changes: 5 additions & 3 deletions js/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ const arrayWrapperCode = `(function(val) {
get: function(target, property, receiver) {
switch (property){
case "length":
return target.length()
return target.length();
case Symbol.iterator:
return function() {return target.iterator()}
return function() {
return target.iterator();
};
}
var i = parseInt(property);
if (isNaN(i)) {
Expand All @@ -106,5 +108,5 @@ const arrayWrapperCode = `(function(val) {
return target.get(i);
}
};
return new Proxy(val, arrayHandler)
return new Proxy(val, arrayHandler);
})`
28 changes: 14 additions & 14 deletions js/share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,34 @@ function generateArray() {
return arr;
}
var s = newShare("something", generateArray);
var s = new Share("something", generateArray);
var er = "";
try {
var p = newShare("wat", function() {return "whatever"});
var p = new Share("wat", function() {return "whatever"});
throw "the previous line should've errored";
} catch (e) {
if (!e.toString().includes("only arrays can be made into shared objects")) {
er = "wrong error " + e.toString()
er = "wrong error " + e.toString();
}
}
if (er != "") {
throw er
throw er;
}
exports.default = function() {
if (s[2] !== "something2") {
throw new Error("bad s[2]="+s[2])
throw new Error("bad s[2]="+s[2]);
}
if (s.length != 50) {
throw new Error("bad length " +_s.length)
throw new Error("bad length " +_s.length);
}
var i = 0;
for (var v of s) {
if (v !== "something"+i) {
throw new Error("bad v="+v+" for i="+i)
throw new Error("bad v="+v+" for i="+i);
}
i++;
}
Expand All @@ -83,29 +83,29 @@ exports.default = function() {
try {
s.something = 21
throw "the previous line should've errored s.something = 21"
throw "the previous line should've errored s.something = 21";
} catch(e) {
if (!e.toString().includes("Host object field something cannot be made configurable")) {
er = "wrong error " + e.toString()
er = "wrong error " + e.toString();
}
}
if (er != "") {
throw er
throw er;
}
try {
s[1]= "21"
s[1]= "21";
throw "the previous line should've errored"
throw "the previous line should've errored";
} catch(e) {
if (!e.toString().includes("Host object field 1 cannot be made configurable")) {
er = "wrong error " + e.toString()
er = "wrong error " + e.toString();
}
}
if (er != "") {
throw er
throw er;
}
}`

Expand Down

0 comments on commit d453b82

Please sign in to comment.