cpp.js 1.0.0-alpha.3 → 1.0.0-beta.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cpp.js",
3
- "version": "1.0.0-alpha.3",
3
+ "version": "1.0.0-beta.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "homepage": "https://cpp.js.org",
@@ -152,10 +152,23 @@ function initCppJs(userConfig = {}) {
152
152
  return vector;
153
153
  },
154
154
  };
155
+ if (config.getWasmFunction) {
156
+ m.instantiateWasm = function instantiateWasm(info, receive) {
157
+ const instance = new WebAssembly.Instance(config.getWasmFunction(), info);
158
+ receive(instance);
159
+ return instance.exports;
160
+ };
161
+ }
155
162
  Module(m).then(resolve).catch(reject);
156
163
  });
157
164
 
158
165
  return cppJsPromise;
159
166
  }
160
167
 
168
+ if (typeof globalThis === 'object') {
169
+ globalThis.CppJs = {
170
+ initCppJs,
171
+ };
172
+ }
173
+
161
174
  export default initCppJs;
@@ -121,10 +121,23 @@ function initCppJs(userConfig = {}) {
121
121
  return vector;
122
122
  },
123
123
  };
124
+ if (config.getWasmFunction) {
125
+ m.instantiateWasm = function instantiateWasm(info, receive) {
126
+ const instance = new WebAssembly.Instance(config.getWasmFunction(), info);
127
+ receive(instance);
128
+ return instance.exports;
129
+ };
130
+ }
124
131
  Module(m).then(resolve).catch(reject);
125
132
  });
126
133
 
127
134
  return cppJsPromise;
128
135
  }
129
136
 
137
+ if (typeof globalThis === 'object') {
138
+ globalThis.CppJs = {
139
+ initCppJs,
140
+ };
141
+ }
142
+
130
143
  export default initCppJs;
package/src/bin.js CHANGED
@@ -84,11 +84,23 @@ function run(programName, params) {
84
84
  function generate(type, platform) {
85
85
  const compiler2 = new CppjsCompiler();
86
86
  if (type === 'lib') {
87
+ const modules = [];
88
+ compiler2.config.paths.module.forEach((modulePath) => {
89
+ modules.push(...glob.sync('**/*.i', { absolute: true, cwd: modulePath }));
90
+ modules.push(...glob.sync('*.i', { absolute: true, cwd: modulePath }));
91
+ });
92
+
87
93
  if (platform === 'all' || platform === 'wasm') {
88
94
  if (!fs.existsSync(`${compiler2.config.paths.output}/prebuilt/Emscripten-x86_64`)) {
89
95
  generateWasmLib();
96
+ modules.forEach((modulePath) => {
97
+ const fileName = modulePath.split('/').at(-1);
98
+ createDir('prebuilt/Emscripten-x86_64/swig', compiler2.config.paths.output);
99
+ fs.copyFileSync(modulePath, `${compiler2.config.paths.output}/prebuilt/Emscripten-x86_64/swig/${fileName}`);
100
+ });
90
101
  }
91
102
  }
103
+
92
104
  if (platform === 'wasm') return;
93
105
  const platforms = {
94
106
  all: ['Android-arm64-v8a', 'iOS-iphoneos', 'iOS-iphonesimulator'],
@@ -99,6 +111,11 @@ function generate(type, platform) {
99
111
  if (!fs.existsSync(`${compiler2.config.paths.output}/prebuilt/${p}`)) {
100
112
  const compiler = new CppjsCompiler(p);
101
113
  compiler.createLib();
114
+ modules.forEach((modulePath) => {
115
+ const fileName = modulePath.split('/').at(-1);
116
+ createDir(`prebuilt/${p}/swig`, compiler2.config.paths.output);
117
+ fs.copyFileSync(modulePath, `${compiler2.config.paths.output}/prebuilt/${p}/swig/${fileName}`);
118
+ });
102
119
  }
103
120
  });
104
121
  if (platform === 'all' || platform === 'ios') {
@@ -126,7 +143,16 @@ async function generateWasmLib() {
126
143
 
127
144
  compiler.createBridge();
128
145
  await compiler.createWasm({ cc: ['-O3'] });
129
- createDir('prebuilt/Emscripten-x86_64', compiler.config.paths.output);
146
+ createDir('prebuilt/Emscripten-x86_64/lib', compiler.config.paths.output);
147
+
148
+ fs.renameSync(`${compiler.config.paths.temp}/lib/lib${compiler.config.general.name}.a`, `${compiler.config.paths.output}/prebuilt/Emscripten-x86_64/lib/lib${compiler.config.general.name}.a`);
149
+ fs.renameSync(`${compiler.config.paths.temp}/include/`, `${compiler.config.paths.output}/prebuilt/Emscripten-x86_64/include`);
150
+
151
+ fs.rmSync(`${compiler.config.paths.output}/data`, { recursive: true, force: true });
152
+ if (fs.existsSync(`${compiler.config.paths.temp}/data`)) {
153
+ fs.renameSync(`${compiler.config.paths.temp}/data`, `${compiler.config.paths.output}/data`);
154
+ }
155
+
130
156
  fs.copyFileSync(`${compiler.config.paths.temp}/${compiler.config.general.name}.node.js`, `${compiler.config.paths.output}/${compiler.config.general.name}.node.js`);
131
157
  fs.copyFileSync(`${compiler.config.paths.temp}/${compiler.config.general.name}.browser.js`, `${compiler.config.paths.output}/${compiler.config.general.name}.browser.js`);
132
158
  fs.copyFileSync(`${compiler.config.paths.temp}/${compiler.config.general.name}.wasm`, `${compiler.config.paths.output}/${compiler.config.general.name}.wasm`);
@@ -134,10 +160,5 @@ async function generateWasmLib() {
134
160
  fs.copyFileSync(`${compiler.config.paths.temp}/${compiler.config.general.name}.data.txt`, `${compiler.config.paths.output}/${compiler.config.general.name}.data.txt`);
135
161
  }
136
162
 
137
- fs.renameSync(`${compiler.config.paths.temp}/lib/`, `${compiler.config.paths.output}/prebuilt/Emscripten-x86_64/lib`);
138
- fs.renameSync(`${compiler.config.paths.temp}/include/`, `${compiler.config.paths.output}/prebuilt/Emscripten-x86_64/include`);
139
-
140
- fs.rmSync(`${compiler.config.paths.output}/data`, { recursive: true, force: true });
141
- fs.renameSync(`${compiler.config.paths.temp}/data`, `${compiler.config.paths.output}/data`);
142
163
  fs.rmSync(compiler.config.paths.temp, { recursive: true, force: true });
143
164
  }
@@ -1,23 +1,28 @@
1
- import glob from 'glob';
2
1
  import getBaseInfo from '../utils/getBaseInfo.js';
3
2
  import getPathInfo from '../utils/getPathInfo.js';
3
+ import { getDependencyParams } from './getCmakeParams.js';
4
4
  import run from './run.js';
5
5
 
6
6
  const platform = 'Emscripten-x86_64';
7
7
 
8
8
  export default function createBridge(compiler) {
9
9
  const bridges = [];
10
+
11
+ const allHeaders = getDependencyParams(compiler.config).headerPathWithDepends.split(';');
12
+
13
+ let includePath = [
14
+ ...compiler.config.getAllDependencies().map((d) => `${d.paths.output}/prebuilt/${platform}/include`),
15
+ ...compiler.config.getAllDependencies().map((d) => `${d.paths.output}/prebuilt/${platform}/swig`),
16
+ ...compiler.config.paths.header,
17
+ ...allHeaders,
18
+ ].filter((path) => !!path.toString()).map((path) => `-I/tmp/cppjs/live/${getPathInfo(path, compiler.config.paths.base).relative}`);
19
+ includePath = [...new Set(includePath)];
20
+
10
21
  compiler.interfaces.forEach((filePath) => {
11
22
  const input = getPathInfo(filePath, compiler.config.paths.base);
12
23
  const output = getPathInfo(`${compiler.config.paths.temp}/bridge`, compiler.config.paths.base);
13
24
  const base = getBaseInfo(compiler.config.paths.base);
14
25
 
15
- const includePath = [
16
- ...compiler.config.getAllDependencies().map((d) => `${d.paths.output}/prebuilt/${platform}/include`),
17
- ...compiler.config.getAllDependencies().map((d) => `${d.paths.output}/prebuilt/${platform}/swig`),
18
- ...compiler.config.paths.header,
19
- ].filter((path) => !!path.toString()).map((path) => `-I/tmp/cppjs/live/${getPathInfo(path, compiler.config.paths.base).relative}`);
20
-
21
26
  run(compiler, 'swig', [
22
27
  '-c++',
23
28
  '-embind',
@@ -6,7 +6,10 @@ import getLibs from './getLibs.js';
6
6
  import getData from './getData.js';
7
7
  import getPathInfo from '../utils/getPathInfo.js';
8
8
 
9
- export default async function createWasm(compiler, options, buildAll = false) {
9
+ export default async function createWasm(compiler, options = {}, buildAll = false) {
10
+ if (fs.existsSync('/tmp/cppjs/live')) fs.unlinkSync('/tmp/cppjs/live');
11
+ fs.symlinkSync(compiler.config.paths.base, '/tmp/cppjs/live');
12
+
10
13
  const output = `/tmp/cppjs/live/${getPathInfo(compiler.config.paths.temp, compiler.config.paths.base).relative}`;
11
14
 
12
15
  let params = getCmakeParams(compiler.config, '/tmp/cppjs/live/', true, false);
@@ -28,13 +31,13 @@ export default async function createWasm(compiler, options, buildAll = false) {
28
31
  run(compiler, 'emmake', ['make', 'install']);
29
32
 
30
33
  const libs = getLibs(compiler.config, '/tmp/cppjs/live/');
31
- const data = Object.entries(getData(compiler.config, 'data', '/tmp/cppjs/live/', 'Emscripten-x86_64', 'browser')).map(([key, value]) => ['--preload-file', `${key}@${value}`]).flat();
34
+ const data = Object.entries(getData(compiler.config, 'data', '/tmp/cppjs/live/', 'Emscripten-x86_64', 'browser')).map(([key, value]) => ['--preload-file', `${key.replaceAll('@', '@@')}@${value}`]).flat();
32
35
  run(compiler, 'emcc', [
33
36
  '-lembind', '-Wl,--whole-archive',
34
37
  ...libs, ...(options.cc || []),
35
- '-s', 'WASM=1', '-s', 'MODULARIZE=1',
38
+ '-s', 'WASM=1', '-s', 'MODULARIZE=1', '-s', 'DYNAMIC_EXECUTION=0',
36
39
  '-s', 'RESERVED_FUNCTION_POINTERS=200', '-s', 'DISABLE_EXCEPTION_CATCHING=0', '-s', 'FORCE_FILESYSTEM=1',
37
- '-s', 'TOTAL_MEMORY=512MB', '-s', 'ALLOW_MEMORY_GROWTH=1',
40
+ '-s', 'ALLOW_MEMORY_GROWTH=1',
38
41
  '-s', 'EXPORTED_RUNTIME_METHODS=["FS", "ENV"]',
39
42
  '-o', `${output}/${compiler.config.general.name}.js`,
40
43
  ...data,
@@ -43,9 +46,9 @@ export default async function createWasm(compiler, options, buildAll = false) {
43
46
  run(compiler, 'emcc', [
44
47
  '-lembind', '-Wl,--whole-archive', '-lnodefs.js',
45
48
  ...libs, ...(options.cc || []),
46
- '-s', 'WASM=1', '-s', 'MODULARIZE=1',
49
+ '-s', 'WASM=1', '-s', 'MODULARIZE=1', '-s', 'DYNAMIC_EXECUTION=0',
47
50
  '-s', 'RESERVED_FUNCTION_POINTERS=200', '-s', 'DISABLE_EXCEPTION_CATCHING=0', '-s', 'FORCE_FILESYSTEM=1', '-s', 'NODERAWFS',
48
- '-s', 'TOTAL_MEMORY=512MB', '-s', 'ALLOW_MEMORY_GROWTH=1',
51
+ '-s', 'ALLOW_MEMORY_GROWTH=1',
49
52
  '-s', 'EXPORTED_RUNTIME_METHODS=["FS", "ENV", "NODEFS"]',
50
53
  '-o', `${output}/${compiler.config.general.name}.js`,
51
54
  ]);
@@ -6,17 +6,16 @@ function getPath(config, path, pathPrefix) {
6
6
  if (!pathPrefix) {
7
7
  return getPathInfo(path, config.paths.base).absolute;
8
8
  }
9
-
10
9
  return `${pathPrefix}${getPathInfo(path, config.paths.base).relative}`;
11
10
  }
12
11
 
13
- function getRecursiveData(obj, dependency, field, pathPrefix, platform, subPlatform) {
12
+ function getRecursiveData(obj, config, dependency, field, pathPrefix, platform, subPlatform) {
14
13
  const platformName = subPlatform ? `${platform}-${subPlatform}` : platform;
15
14
  if (dependency?.platform?.[platformName]?.[field]) {
16
15
  Object.entries(dependency.platform[platformName][field]).forEach(([dKey, value]) => {
17
16
  if (field === 'data') {
18
17
  const a = `${dependency.paths.project}/dist/prebuilt/${platform}/${dKey}`;
19
- const key = getPath(dependency, a, pathPrefix);
18
+ const key = getPath(config, a, pathPrefix);
20
19
  obj[key] = value;
21
20
  } else {
22
21
  obj[dKey] = value;
@@ -25,13 +24,13 @@ function getRecursiveData(obj, dependency, field, pathPrefix, platform, subPlatf
25
24
  }
26
25
 
27
26
  dependency.dependencies.forEach((dep) => {
28
- getRecursiveData(obj, dep, field, pathPrefix, platform, subPlatform);
27
+ getRecursiveData(obj, config, dep, field, pathPrefix, platform, subPlatform);
29
28
  });
30
29
  }
31
30
 
32
31
  export default function getData(config, field, pathPrefix, platform = 'Emscripten-x86_64', subPlatform) {
33
32
  const output = {};
34
- getRecursiveData(output, config, field, pathPrefix, platform, subPlatform);
33
+ getRecursiveData(output, config, config, field, pathPrefix, platform, subPlatform);
35
34
 
36
35
  return output;
37
36
  }