cpp.js 1.0.0-alpha.4 → 1.0.0-beta.2

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.4",
3
+ "version": "1.0.0-beta.2",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "homepage": "https://cpp.js.org",
@@ -13,7 +13,7 @@
13
13
  "@rollup/plugin-commonjs": "^26.0.1",
14
14
  "@rollup/plugin-node-resolve": "^15.2.3",
15
15
  "@rollup/plugin-virtual": "^3.0.2",
16
- "commander": "^9.4.1",
16
+ "commander": "^12.1.0",
17
17
  "glob": "^8.0.3",
18
18
  "rollup": "^4.18.0",
19
19
  "rollup-plugin-uglify": "^6.0.4"
@@ -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
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { Command, Argument } from 'commander';
3
+ import { Command, Option } from 'commander';
4
4
  import fs from 'fs';
5
5
  import glob from 'glob';
6
6
  import { createDir } from './utils/createTempDir.js';
@@ -14,30 +14,26 @@ const program = new Command();
14
14
 
15
15
  program
16
16
  .name('cpp.js')
17
- .description('Compile c++ files to webassembly.')
17
+ .description('Compile C++ files to WebAssembly and native platforms.')
18
18
  .version(packageJSON.version)
19
19
  .showHelpAfterError();
20
20
 
21
- const commandGenerate = program.command('generate')
22
- .description('Generate app or lib.')
23
- .addArgument(new Argument('<type>', 'Generation type').choices(['app', 'lib']))
24
- .option('-b, --base <base>', 'base path')
25
- .option('-p, --platform <platform>', 'platform (all)', 'all', ['all', 'wasm', 'android', 'ios'])
26
- .option('-o, --output <string>', 'Output path');
21
+ const commandBuild = program.command('build')
22
+ .description('compile the project that was set up using Cpp.js')
23
+ .addOption(new Option('-p, --platform <platform>', 'target platform').default('all').choices(['all', 'wasm', 'android', 'ios']));
27
24
 
28
25
  const commandRun = program.command('run')
29
- .description('Run docker application');
26
+ .description('run docker application');
30
27
 
31
28
  const commandPostInstall = program.command('postinstall')
32
- .description('npm postinstall');
29
+ .description('prepare the required packages for Cpp.js after installation');
33
30
 
34
31
  program.parse(process.argv);
35
32
 
36
33
  switch (program.args[0]) {
37
- case 'generate': {
38
- const type = commandGenerate.args[0];
39
- const { output, platform, base } = commandGenerate.opts();
40
- generate(type, platform, output, base);
34
+ case 'build': {
35
+ const { platform } = commandBuild.opts();
36
+ build(platform);
41
37
  break;
42
38
  }
43
39
  case 'run': {
@@ -55,7 +51,15 @@ switch (program.args[0]) {
55
51
 
56
52
  function postInstall() {
57
53
  const projectPath = process.env.PWD;
58
- if (!fs.existsSync(`${projectPath}/cppjs.config.js`) && !fs.existsSync(`${projectPath}/cppjs.config.cjs`) && !fs.existsSync(`${projectPath}/cppjs.config.mjs`)) {
54
+ const isDarwin = process.platform === 'darwin';
55
+ if (
56
+ !isDarwin
57
+ || (
58
+ !fs.existsSync(`${projectPath}/cppjs.config.js`)
59
+ && !fs.existsSync(`${projectPath}/cppjs.config.cjs`)
60
+ && !fs.existsSync(`${projectPath}/cppjs.config.mjs`)
61
+ )
62
+ ) {
59
63
  return;
60
64
  }
61
65
 
@@ -78,49 +82,48 @@ function postInstall() {
78
82
 
79
83
  function run(programName, params) {
80
84
  const compiler = new CppjsCompiler();
81
- compiler.run(programName, params);
85
+ compiler.run(programName, params, { console: true });
82
86
  }
83
87
 
84
- function generate(type, platform) {
88
+ function build(platform) {
85
89
  const compiler2 = new CppjsCompiler();
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
-
93
- if (platform === 'all' || platform === 'wasm') {
94
- if (!fs.existsSync(`${compiler2.config.paths.output}/prebuilt/Emscripten-x86_64`)) {
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
- });
101
- }
90
+
91
+ const modules = [];
92
+ compiler2.config.paths.module.forEach((modulePath) => {
93
+ modules.push(...glob.sync('**/*.i', { absolute: true, cwd: modulePath }));
94
+ modules.push(...glob.sync('*.i', { absolute: true, cwd: modulePath }));
95
+ });
96
+
97
+ if (platform === 'all' || platform === 'wasm') {
98
+ if (!fs.existsSync(`${compiler2.config.paths.output}/prebuilt/Emscripten-x86_64`)) {
99
+ buildWasm();
100
+ modules.forEach((modulePath) => {
101
+ const fileName = modulePath.split('/').at(-1);
102
+ createDir('prebuilt/Emscripten-x86_64/swig', compiler2.config.paths.output);
103
+ fs.copyFileSync(modulePath, `${compiler2.config.paths.output}/prebuilt/Emscripten-x86_64/swig/${fileName}`);
104
+ });
102
105
  }
106
+ }
103
107
 
104
- if (platform === 'wasm') return;
105
- const platforms = {
106
- all: ['Android-arm64-v8a', 'iOS-iphoneos', 'iOS-iphonesimulator'],
107
- android: ['Android-arm64-v8a'],
108
- ios: ['iOS-iphoneos', 'iOS-iphonesimulator'],
109
- };
110
- platforms[platform].forEach((p) => {
111
- if (!fs.existsSync(`${compiler2.config.paths.output}/prebuilt/${p}`)) {
112
- const compiler = new CppjsCompiler(p);
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
- });
119
- }
120
- });
121
- if (platform === 'all' || platform === 'ios') {
122
- compiler2.finishBuild();
108
+ if (platform === 'wasm') return;
109
+ const platforms = {
110
+ all: ['Android-arm64-v8a', 'iOS-iphoneos', 'iOS-iphonesimulator'],
111
+ android: ['Android-arm64-v8a'],
112
+ ios: ['iOS-iphoneos', 'iOS-iphonesimulator'],
113
+ };
114
+ platforms[platform].forEach((p) => {
115
+ if (!fs.existsSync(`${compiler2.config.paths.output}/prebuilt/${p}`)) {
116
+ const compiler = new CppjsCompiler(p);
117
+ compiler.createLib();
118
+ modules.forEach((modulePath) => {
119
+ const fileName = modulePath.split('/').at(-1);
120
+ createDir(`prebuilt/${p}/swig`, compiler2.config.paths.output);
121
+ fs.copyFileSync(modulePath, `${compiler2.config.paths.output}/prebuilt/${p}/swig/${fileName}`);
122
+ });
123
123
  }
124
+ });
125
+ if (platform === 'all' || platform === 'ios') {
126
+ compiler2.finishBuild();
124
127
  }
125
128
 
126
129
  const distCmakeContent = fs.readFileSync(`${compiler2.config.paths.cli}/assets/dist.cmake`, { encoding: 'utf8', flag: 'r' })
@@ -128,7 +131,7 @@ function generate(type, platform) {
128
131
  fs.writeFileSync(`${compiler2.config.paths.output}/prebuilt/CMakeLists.txt`, distCmakeContent);
129
132
  }
130
133
 
131
- async function generateWasmLib() {
134
+ async function buildWasm() {
132
135
  let headers = [];
133
136
 
134
137
  const compiler = new CppjsCompiler();
@@ -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,7 @@ 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
10
  if (fs.existsSync('/tmp/cppjs/live')) fs.unlinkSync('/tmp/cppjs/live');
11
11
  fs.symlinkSync(compiler.config.paths.base, '/tmp/cppjs/live');
12
12
 
@@ -35,9 +35,9 @@ export default async function createWasm(compiler, options, buildAll = false) {
35
35
  run(compiler, 'emcc', [
36
36
  '-lembind', '-Wl,--whole-archive',
37
37
  ...libs, ...(options.cc || []),
38
- '-s', 'WASM=1', '-s', 'MODULARIZE=1',
38
+ '-s', 'WASM=1', '-s', 'MODULARIZE=1', '-s', 'DYNAMIC_EXECUTION=0',
39
39
  '-s', 'RESERVED_FUNCTION_POINTERS=200', '-s', 'DISABLE_EXCEPTION_CATCHING=0', '-s', 'FORCE_FILESYSTEM=1',
40
- '-s', 'TOTAL_MEMORY=512MB', '-s', 'ALLOW_MEMORY_GROWTH=1',
40
+ '-s', 'ALLOW_MEMORY_GROWTH=1',
41
41
  '-s', 'EXPORTED_RUNTIME_METHODS=["FS", "ENV"]',
42
42
  '-o', `${output}/${compiler.config.general.name}.js`,
43
43
  ...data,
@@ -46,9 +46,9 @@ export default async function createWasm(compiler, options, buildAll = false) {
46
46
  run(compiler, 'emcc', [
47
47
  '-lembind', '-Wl,--whole-archive', '-lnodefs.js',
48
48
  ...libs, ...(options.cc || []),
49
- '-s', 'WASM=1', '-s', 'MODULARIZE=1',
49
+ '-s', 'WASM=1', '-s', 'MODULARIZE=1', '-s', 'DYNAMIC_EXECUTION=0',
50
50
  '-s', 'RESERVED_FUNCTION_POINTERS=200', '-s', 'DISABLE_EXCEPTION_CATCHING=0', '-s', 'FORCE_FILESYSTEM=1', '-s', 'NODERAWFS',
51
- '-s', 'TOTAL_MEMORY=512MB', '-s', 'ALLOW_MEMORY_GROWTH=1',
51
+ '-s', 'ALLOW_MEMORY_GROWTH=1',
52
52
  '-s', 'EXPORTED_RUNTIME_METHODS=["FS", "ENV", "NODEFS"]',
53
53
  '-o', `${output}/${compiler.config.general.name}.js`,
54
54
  ]);