@wllzhang/afsim-compiler 0.3.0

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.
package/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # @wllzhang/afsim-compiler
2
+
3
+ JSON to AFSIM DSL compiler. Native Rust library with Node.js FFI bindings.
4
+
5
+ Pass a JavaScript object describing your AFSIM scenario and get back the compiled DSL text — all in memory, no temp files needed.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @wllzhang/afsim-compiler
11
+ ```
12
+
13
+ The correct native binary for your platform is installed automatically via `optionalDependencies`.
14
+
15
+ ## Usage
16
+
17
+ ```javascript
18
+ import { compile } from '@wllzhang/afsim-compiler';
19
+
20
+ const scenario = {
21
+ "my_tracker": {
22
+ "type": "processor",
23
+ "parent_model": "WSF_PERFECT_TRACKER",
24
+ "update_interval": "1 s"
25
+ },
26
+ "my_fuse": {
27
+ "type": "processor",
28
+ "parent_model": "WSF_AIR_TARGET_FUSE"
29
+ },
30
+ "my_platform": {
31
+ "type": "platform_type",
32
+ "parent_model": "WSF_PLATFORM",
33
+ "icon": "missile",
34
+ "processors": ["my_tracker", "my_fuse"]
35
+ }
36
+ };
37
+
38
+ const dsl = compile(scenario);
39
+ console.log(dsl);
40
+ ```
41
+
42
+ Output:
43
+
44
+ ```
45
+ platform_type my_platform WSF_PLATFORM
46
+ icon missile
47
+ processor my_tracker WSF_PERFECT_TRACKER
48
+ update_interval 1 s
49
+ end_processor
50
+ processor my_fuse WSF_AIR_TARGET_FUSE
51
+ end_processor
52
+ end_platform_type
53
+ ```
54
+
55
+ ## API
56
+
57
+ ### `compile(data)`
58
+
59
+ | Parameter | Type | Description |
60
+ |-----------|------|-------------|
61
+ | `data` | `object \| string` | Scenario definition as a JS object or JSON string |
62
+ | **Returns** | `string` | Compiled AFSIM DSL text |
63
+ | **Throws** | `Error` | On invalid input or compilation failure |
64
+
65
+ ## Supported Platforms
66
+
67
+ | OS | Arch | Binary |
68
+ |----|------|--------|
69
+ | Windows | x64 | `afsim_compiler.dll` |
70
+ | Linux | x64 | `libafsim_compiler.so` |
71
+ | Linux | arm64 | `libafsim_compiler.so` |
72
+ | macOS | x64 | `libafsim_compiler.dylib` |
73
+ | macOS | arm64 | `libafsim_compiler.dylib` |
74
+
75
+ ## License
76
+
77
+ MIT
package/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Compile a JSON object into AFSIM DSL text.
3
+ *
4
+ * @param data - The scenario definition as a plain JS object (or a JSON string).
5
+ * @returns The compiled AFSIM DSL output.
6
+ * @throws If the input is invalid or compilation fails.
7
+ */
8
+ export declare function compile(data: Record<string, any> | string): string;
package/index.mjs ADDED
@@ -0,0 +1,66 @@
1
+ import koffi from 'koffi';
2
+ import { createRequire } from 'module';
3
+ import path from 'path';
4
+
5
+ const require = createRequire(import.meta.url);
6
+
7
+ const PLATFORMS = {
8
+ 'win32-x64': { pkg: '@wllzhang/afsim-compiler-win32-x64', file: 'afsim_compiler.dll' },
9
+ 'linux-x64': { pkg: '@wllzhang/afsim-compiler-linux-x64', file: 'libafsim_compiler.so' },
10
+ 'linux-arm64': { pkg: '@wllzhang/afsim-compiler-linux-arm64', file: 'libafsim_compiler.so' },
11
+ 'darwin-x64': { pkg: '@wllzhang/afsim-compiler-darwin-x64', file: 'libafsim_compiler.dylib' },
12
+ 'darwin-arm64': { pkg: '@wllzhang/afsim-compiler-darwin-arm64', file: 'libafsim_compiler.dylib' },
13
+ };
14
+
15
+ function loadLibrary() {
16
+ const key = `${process.platform}-${process.arch}`;
17
+ const entry = PLATFORMS[key];
18
+ if (!entry) {
19
+ throw new Error(
20
+ `@wllzhang/afsim-compiler: unsupported platform ${key}. ` +
21
+ `Supported: ${Object.keys(PLATFORMS).join(', ')}`
22
+ );
23
+ }
24
+
25
+ let pkgDir;
26
+ try {
27
+ const pkgJson = require.resolve(`${entry.pkg}/package.json`);
28
+ pkgDir = path.dirname(pkgJson);
29
+ } catch {
30
+ throw new Error(
31
+ `@wllzhang/afsim-compiler: platform package ${entry.pkg} is not installed. ` +
32
+ `Run: npm install ${entry.pkg}`
33
+ );
34
+ }
35
+
36
+ return koffi.load(path.join(pkgDir, entry.file));
37
+ }
38
+
39
+ const lib = loadLibrary();
40
+ const _compile_json = lib.func('compile_json', 'str', ['str']);
41
+ const _free_string = lib.func('free_string', 'void', ['str']);
42
+
43
+ /**
44
+ * Compile a JSON object into AFSIM DSL text.
45
+ *
46
+ * @param {Record<string, any>} data - The scenario definition as a plain JS object.
47
+ * @returns {string} The compiled AFSIM DSL output.
48
+ * @throws {Error} If the input is invalid or compilation fails.
49
+ */
50
+ export function compile(data) {
51
+ if (data === null || data === undefined) {
52
+ throw new Error('compile: input must be a non-null object');
53
+ }
54
+
55
+ const jsonStr = typeof data === 'string' ? data : JSON.stringify(data);
56
+ const result = _compile_json(jsonStr);
57
+
58
+ if (!result) {
59
+ throw new Error('compile: received null from native library');
60
+ }
61
+ if (result.startsWith('ERROR:')) {
62
+ throw new Error(result);
63
+ }
64
+
65
+ return result;
66
+ }
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@wllzhang/afsim-compiler",
3
+ "version": "0.3.0",
4
+ "description": "JSON to AFSIM DSL compiler — native Rust library with Node.js FFI bindings",
5
+ "type": "module",
6
+ "main": "index.mjs",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./index.mjs",
10
+ "types": "./index.d.ts"
11
+ }
12
+ },
13
+ "types": "index.d.ts",
14
+ "files": [
15
+ "index.mjs",
16
+ "index.d.ts",
17
+ "README.md"
18
+ ],
19
+ "dependencies": {
20
+ "koffi": "^2"
21
+ },
22
+ "optionalDependencies": {
23
+ "@wllzhang/afsim-compiler-win32-x64": "0.3.0",
24
+ "@wllzhang/afsim-compiler-linux-x64": "0.3.0",
25
+ "@wllzhang/afsim-compiler-linux-arm64": "0.3.0",
26
+ "@wllzhang/afsim-compiler-darwin-x64": "0.3.0",
27
+ "@wllzhang/afsim-compiler-darwin-arm64": "0.3.0"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/wllzhang/AFScriptGenerator.git",
32
+ "directory": "npm"
33
+ },
34
+ "homepage": "https://github.com/wllzhang/AFScriptGenerator#readme",
35
+ "license": "MIT",
36
+ "publishConfig": {
37
+ "access": "public"
38
+ }
39
+ }