-
Notifications
You must be signed in to change notification settings - Fork 1
/
05-addMapIntrinsic.html
45 lines (40 loc) · 1.31 KB
/
05-addMapIntrinsic.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!DOCTYPE html>
<html>
<link rel="shortcut icon" href="#">
<head>
<meta charset="UTF-8">
<title>MiniScript.TS - Adding Map intrinsics</title>
<script type="text/javascript" src="../../dist/miniscript-ts.js"></script>
</head>
<body>
<div>
This example shows how to add a map intrinsic function.
</div>
<div>
Open the developer console to see results!
</div>
<script type="text/javascript">
const interp = new MiniScriptTs.Interpreter();
// A typical pattern is to have a map which represents either a "super-type"
// or a singleton object. In order for this to be accessible one needs to
// define in turn an intrinsic function that returns such value.
// First we need the map / object.
// The interpreter provides a `newMap` function for that!
const myType = interp.newMap();
// Then we create the intrinsic function to get ahold of it.
interp.addIntrinsic("MyType", () => {
return myType;
});
// At this point you can do `new MyType` on MiniScript.
// Now let's augment the type with an intrinsic map-function.
interp.addMapIntrinsic(myType, "someMethod", () => {
console.log("Method called!");
});
// Let's use what we have
let code = `
m = new MyType
m.someMethod`;
interp.runSrcCode(code);
</script>
</body>
</html>