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 7cc4418
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
8 changes: 4 additions & 4 deletions js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ 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 {
// XShare is a constructor returning a shareable read-only array (general objects coming)
// indentified by the name and having their contents be whatever the call returns - again only
// arrays currently supported.
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 7cc4418

Please sign in to comment.