Skip to content

Commit dd702cc

Browse files
Making syscall/js CSP compatible (#1168)
Replaces use of dynamically evaluated code with helpers in prelude.
1 parent 59aa6d1 commit dd702cc

File tree

3 files changed

+47
-49
lines changed

3 files changed

+47
-49
lines changed

compiler/natives/src/syscall/js/js.go

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,23 @@ const (
2222
TypeFunction
2323
)
2424

25+
// Same order as Type constants
26+
var typeNames = []string{
27+
"undefined",
28+
"null",
29+
"boolean",
30+
"number",
31+
"string",
32+
"symbol",
33+
"object",
34+
"function",
35+
}
36+
2537
func (t Type) String() string {
26-
switch t {
27-
case TypeUndefined:
28-
return "undefined"
29-
case TypeNull:
30-
return "null"
31-
case TypeBoolean:
32-
return "boolean"
33-
case TypeNumber:
34-
return "number"
35-
case TypeString:
36-
return "string"
37-
case TypeSymbol:
38-
return "symbol"
39-
case TypeObject:
40-
return "object"
41-
case TypeFunction:
42-
return "function"
43-
default:
38+
if int(t) < 0 || len(typeNames) <= int(t) {
4439
panic("bad type")
4540
}
41+
return typeNames[t]
4642
}
4743

4844
func (t Type) isObject() bool {
@@ -110,40 +106,30 @@ func objectToValue(obj *js.Object) Value {
110106
}
111107

112108
var (
113-
id *js.Object
114-
instanceOf *js.Object
115-
getValueType *js.Object
109+
id *js.Object
110+
instanceOf *js.Object
111+
typeOf *js.Object
116112
)
117113

118114
func init() {
119115
if js.Global != nil {
120-
id = js.Global.Call("Function", "x", "return x")
121-
instanceOf = js.Global.Call("Function", "x", "y", "return x instanceof y")
122-
getValueType = js.Global.Call("Function", "x", `
123-
if (typeof(x) === "undefined") {
124-
return 0; // TypeUndefined
125-
}
126-
if (x === null) {
127-
return 1; // TypeNull
128-
}
129-
if (typeof(x) === "boolean") {
130-
return 2; // TypeBoolean
131-
}
132-
if (typeof(x) === "number") {
133-
return 3; // TypeNumber
134-
}
135-
if (typeof(x) === "string") {
136-
return 4; // TypeString
137-
}
138-
if (typeof(x) === "symbol") {
139-
return 5; // TypeSymbol
140-
}
141-
if (typeof(x) === "function") {
142-
return 7; // TypeFunction
143-
}
144-
return 6; // TypeObject
145-
`)
116+
id = js.Global.Get("$id")
117+
instanceOf = js.Global.Get("$instanceOf")
118+
typeOf = js.Global.Get("$typeOf")
119+
}
120+
}
121+
122+
func getValueType(obj *js.Object) Type {
123+
if obj == nil {
124+
return TypeNull
125+
}
126+
name := typeOf.Invoke(obj).String()
127+
for type2, name2 := range typeNames {
128+
if name == name2 {
129+
return Type(type2)
130+
}
146131
}
132+
return TypeObject
147133
}
148134

149135
func ValueOf(x interface{}) Value {
@@ -322,7 +308,7 @@ func (v Value) Truthy() bool {
322308
}
323309

324310
func (v Value) Type() Type {
325-
return Type(getValueType.Invoke(v.internal()).Int())
311+
return Type(getValueType(v.internal()))
326312
}
327313

328314
func (v Value) IsNull() bool {

compiler/prelude/prelude.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,4 +564,16 @@ var $unsafeMethodToFunction = function(typ, name, isPtr) {
564564
}
565565
}
566566
};
567+
568+
var $id = function(x) {
569+
return x;
570+
};
571+
572+
var $instanceOf = function(x, y) {
573+
return x instanceof y;
574+
};
575+
576+
var $typeOf = function(x) {
577+
return typeof(x);
578+
};
567579
`

0 commit comments

Comments
 (0)