@magnusopera/fscript 0.83.2 → 0.83.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ // Run after `make fable-build`: node src/FScript.JavaScript/examples/hosting.mjs
2
+ // In an installed application, import from "@magnusopera/fscript" instead.
3
+ import {T, HostTypes, HostFunctions, registration, load, invoke, getValue} from '../dist/Library.js';
4
+
5
+ const counterType = HostTypes.describe({name: 'Counter', fields: {Initial: T.int}});
6
+ const pointType = HostTypes.describe({name: 'Point', fields: {X: T.int}, scriptConstructible: true});
7
+ const types = registration({types: [counterType, pointType]});
8
+ const create = HostFunctions.create({
9
+ name: 'Counter.create', parameters: [T.int], result: T.named('Counter'),
10
+ invoke([initial]) {
11
+ return HostTypes.wrap(types, 'Counter', {Initial: initial}, {current: initial.value});
12
+ },
13
+ });
14
+ const next = HostFunctions.create({
15
+ name: 'Counter.next', parameters: [T.named('Counter')], result: T.int,
16
+ invoke([counter]) {
17
+ // Mutation is explicit. The visible Initial snapshot stays unchanged.
18
+ const state = HostTypes.unwrap(types, 'Counter', counter);
19
+ state.current += 1n;
20
+ return state.current;
21
+ },
22
+ });
23
+ const apply = HostFunctions.create({
24
+ name: 'Host.apply', parameters: [T.func(T.int, T.int), T.int], result: T.int,
25
+ invoke([fn, value], context) { return context.apply(fn, [value]); },
26
+ });
27
+ const environment = registration({types: [counterType, pointType], functions: [create, next, apply]});
28
+ const script = load(`
29
+ export let counter = Counter.create 40
30
+ export let advance () = Counter.next counter
31
+ export let point = { X = Host.apply (fun n -> n + 1) 41 }
32
+ `, {registration: environment});
33
+ console.log(invoke(script, 'advance', [null]).value.toString()); // 41
34
+ console.log(invoke(script, 'advance', [null]).value.toString()); // 42
35
+ console.log(getValue(script, 'counter').fields.Initial.value.toString()); // 40
36
+ console.log(HostTypes.unwrap(types, 'Point', getValue(script, 'point')).X.value.toString()); // 42
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@magnusopera/fscript",
3
- "version": "0.83.2",
3
+ "version": "0.83.3",
4
4
  "description": "Embeddable FScript language engine: parser, type inference, and evaluator.",
5
5
  "type": "module",
6
6
  "main": "./dist/Library.js",
7
7
  "exports": "./dist/Library.js",
8
8
  "files": [
9
9
  "dist",
10
+ "examples",
10
11
  "README.md"
11
12
  ],
12
13
  "scripts": {