cpp.js 0.6.0 → 1.0.0-alpha.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 Buğra Sarı
3
+ Copyright (c) 2024 Buğra Sarı
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
Binary file
package/package.json CHANGED
@@ -1,15 +1,22 @@
1
1
  {
2
2
  "name": "cpp.js",
3
- "version": "0.6.0",
3
+ "version": "1.0.0-alpha.2",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
+ "homepage": "https://cpp.js.org",
7
+ "repository": "https://github.com/bugra9/cpp.js.git",
6
8
  "bin": {
7
9
  "cpp.js": "./src/bin.js"
8
10
  },
9
- "main": "/src/index.js",
11
+ "main": "src/index.js",
10
12
  "dependencies": {
13
+ "@rollup/plugin-commonjs": "^26.0.1",
14
+ "@rollup/plugin-node-resolve": "^15.2.3",
15
+ "@rollup/plugin-virtual": "^3.0.2",
11
16
  "commander": "^9.4.1",
12
- "glob": "^8.0.3"
17
+ "glob": "^8.0.3",
18
+ "rollup": "^4.18.0",
19
+ "rollup-plugin-uglify": "^6.0.4"
13
20
  },
14
21
  "devDependencies": {
15
22
  "mocha": "^10.2.0"
@@ -1,6 +1,18 @@
1
1
  cmake_minimum_required(VERSION 3.25)
2
+ set(CMAKE_CXX_STANDARD 17)
2
3
  project("${PROJECT_NAME}")
3
- set(PACKAGE_HOST "${CMAKE_SYSTEM_NAME}-${CMAKE_HOST_SYSTEM_PROCESSOR}")
4
+
5
+ if (CMAKE_SYSTEM_NAME STREQUAL "iOS")
6
+ if (CMAKE_OSX_SYSROOT MATCHES "/iPhoneOS.platform/")
7
+ set(PACKAGE_HOST "iOS-iphoneos")
8
+ elseif(CMAKE_OSX_SYSROOT MATCHES "/iPhoneSimulator.platform/")
9
+ set(PACKAGE_HOST "iOS-iphonesimulator")
10
+ else()
11
+ set(PACKAGE_HOST "iOS-unknown")
12
+ endif()
13
+ else()
14
+ set(PACKAGE_HOST "${CMAKE_SYSTEM_NAME}-${CMAKE_HOST_SYSTEM_PROCESSOR}")
15
+ endif()
4
16
 
5
17
  option(BUILD_BRIDGE "Build Bridge" OFF)
6
18
  option(BUILD_SOURCE "Build Source" OFF)
@@ -18,24 +30,29 @@ if (BUILD_BRIDGE)
18
30
  endif(BUILD_BRIDGE)
19
31
 
20
32
  set(SRC_FILES "${BUILD_SRC_FILES}" "${BRIDGE_SRC_FILES}")
21
- add_library("${PROJECT_NAME}" STATIC ${SRC_FILES})
33
+ add_library("${PROJECT_NAME}" "${BUILD_TYPE}" ${SRC_FILES})
34
+
35
+ set(EXTRA_LINK_LIBRARIES "")
36
+ if(ANDROID)
37
+ set(EXTRA_LINK_LIBRARIES "log")
38
+ endif()
22
39
 
23
40
  if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "")
24
- target_link_libraries("${PROJECT_NAME}" "${DEPENDS_CMAKE_NAMES}")
41
+ target_link_libraries("${PROJECT_NAME}" "${DEPENDS_CMAKE_NAMES}" "${EXTRA_LINK_LIBRARIES}")
25
42
  endif()
26
43
 
27
44
  file(GLOB_RECURSE HEADER_FILES ${HEADER_GLOB})
28
45
  target_include_directories("${PROJECT_NAME}" PUBLIC "${HEADER_DIR}")
29
46
 
30
47
  if (BUILD_SOURCE)
31
- target_sources("${PROJECT_NAME}"
32
- PUBLIC FILE_SET HEADERS
33
- BASE_DIRS "${HEADER_DIR}"
34
- FILES "${HEADER_FILES}")
35
- string(REPLACE "-" ";" HEADER_FOLDER ${PROJECT_NAME})
36
- list(GET HEADER_FOLDER 2 HEADER_FOLDER)
37
- install(TARGETS "${PROJECT_NAME}" DESTINATION "prebuilt/${PACKAGE_HOST}/lib")
38
- install(TARGETS "${PROJECT_NAME}" FILE_SET HEADERS DESTINATION "prebuilt/${PACKAGE_HOST}/include/cppjs-lib-${HEADER_FOLDER}")
48
+ install(TARGETS "${PROJECT_NAME}" DESTINATION "lib")
49
+ if(NOT "${HEADER_FILES}" STREQUAL "")
50
+ target_sources("${PROJECT_NAME}"
51
+ PUBLIC FILE_SET HEADERS
52
+ BASE_DIRS "${HEADER_DIR}"
53
+ FILES "${HEADER_FILES}")
54
+ install(TARGETS "${PROJECT_NAME}" FILE_SET HEADERS DESTINATION "include")
55
+ endif()
39
56
  endif(BUILD_SOURCE)
40
57
  unset(BUILD_SOURCE CACHE)
41
58
 
@@ -0,0 +1,161 @@
1
+ /* eslint-disable import/no-unresolved */
2
+ /* eslint-disable import/first */
3
+ /* eslint-disable no-restricted-syntax */
4
+ /* eslint-disable no-undef */
5
+ import Module from 'cpp.js/module';
6
+ import systemConfig from 'cpp.js/systemConfig';
7
+
8
+ const VIRTUAL_PATH = '/virtual';
9
+ const AUTO_MOUNTED_PATH = `${VIRTUAL_PATH}/automounted`;
10
+ let cppJsPromise;
11
+
12
+ function isObject(item) {
13
+ return (item && typeof item === 'object' && !Array.isArray(item));
14
+ }
15
+
16
+ function mergeDeep(target, ...sources) {
17
+ if (!sources.length) return target;
18
+ const source = sources.shift();
19
+
20
+ if (isObject(target) && isObject(source)) {
21
+ for (const key in source) {
22
+ if (isObject(source[key])) {
23
+ if (!target[key]) Object.assign(target, { [key]: {} });
24
+ mergeDeep(target[key], source[key]);
25
+ } else {
26
+ Object.assign(target, { [key]: source[key] });
27
+ }
28
+ }
29
+ }
30
+
31
+ return mergeDeep(target, ...sources);
32
+ }
33
+
34
+ function initCppJs(userConfig = {}) {
35
+ if (cppJsPromise) return cppJsPromise;
36
+
37
+ const config = mergeDeep(systemConfig, userConfig);
38
+
39
+ cppJsPromise = new Promise((resolve, reject) => {
40
+ const m = {
41
+ print(text) {
42
+ if (config.logHandler) {
43
+ config.logHandler(text, 'stdout');
44
+ } else {
45
+ console.debug(`wasm stdout: ${text}`);
46
+ }
47
+ },
48
+ printErr(text) {
49
+ if (config.errorHandler) {
50
+ config.errorHandler(text, 'stderr');
51
+ } else {
52
+ console.error(`wasm stderr: ${text}`);
53
+ }
54
+ },
55
+ locateFile(fileName) {
56
+ let path = fileName;
57
+ if (config.paths && config.paths.wasm && fileName.endsWith('.wasm')) {
58
+ path = config.paths.wasm;
59
+ } else if (config.paths && config.paths.data && (fileName.endsWith('.data.txt') || fileName.endsWith('.data'))) {
60
+ path = config.paths.data;
61
+ }
62
+
63
+ let prefix = '';
64
+ if (config.path) {
65
+ prefix = config.path;
66
+ if (prefix.slice(-1) !== '/') prefix += '/';
67
+ }
68
+
69
+ let output = prefix + path;
70
+ if (output.endsWith('.data')) output += '.txt';
71
+ if (output.substring(0, 4) !== 'http' && output[0] !== '/') output = `/${output}`;
72
+ return output;
73
+ },
74
+
75
+ preRun: [
76
+ ({ ENV }) => {
77
+ if (ENV && config && config.env) {
78
+ Object.entries(config.env).forEach(([key, value]) => {
79
+ // eslint-disable-next-line no-param-reassign
80
+ ENV[key] = value;
81
+ });
82
+ }
83
+ },
84
+ ],
85
+ onRuntimeInitialized() {
86
+ m.FS.mkdir(VIRTUAL_PATH);
87
+ m.FS.mkdir(AUTO_MOUNTED_PATH);
88
+ if (config.onRuntimeInitialized) config.onRuntimeInitialized(m);
89
+ },
90
+ generateVirtualPath() {
91
+ const rand = Math.floor(Math.random() * 1000000);
92
+ const path = `${AUTO_MOUNTED_PATH}/${rand}`;
93
+ m.FS.mkdir(path);
94
+ return path;
95
+ },
96
+ unmount() {
97
+ if (typeof importScripts === 'function') {
98
+ m.FS.unmount(VIRTUAL_PATH);
99
+ }
100
+ },
101
+ autoMountFiles(files) {
102
+ return new Promise((resolve2) => {
103
+ if (files.length === 0) {
104
+ resolve2([]);
105
+ } else if (typeof importScripts === 'function') {
106
+ const virtualPath = m.generateVirtualPath();
107
+ m.FS.mount(m.WORKERFS, { files }, virtualPath);
108
+ resolve2(files.map((f) => `${virtualPath}/${f.name}`));
109
+ } else {
110
+ const promises = [];
111
+ [...files].forEach((file) => {
112
+ promises.push(file.arrayBuffer());
113
+ });
114
+ Promise.all(promises).then((buffers) => {
115
+ const virtualPath = m.generateVirtualPath();
116
+ buffers.forEach((buffer, i) => {
117
+ const ss = new Uint8Array(buffer);
118
+ m.FS.writeFile(`${virtualPath}/${files[i].name}`, ss);
119
+ });
120
+ resolve2([...files].map((f) => `${virtualPath}/${f.name}`));
121
+ });
122
+ }
123
+ });
124
+ },
125
+ getFileBytes(path) {
126
+ if (!path) return new Uint8Array();
127
+ return m.FS.readFile(path, { encoding: 'binary' });
128
+ },
129
+ getFileList(path = 'virtual') {
130
+ const contents = path.split('/').reduce((accumulator, currentValue) => accumulator.contents[currentValue], m.FS.root).contents;
131
+ const fileList = [];
132
+ Object.keys(contents).forEach((name) => {
133
+ const obj = contents[name];
134
+ if (obj.usedBytes) fileList.push({ path: `/${path}/${name}`, size: obj.usedBytes });
135
+ else if (obj.contents) fileList.push(...m.getFileList(`${path}/${name}`));
136
+ });
137
+ return fileList;
138
+ },
139
+
140
+ toArray(vector) {
141
+ const output = [];
142
+ for (let i = 0; i < vector.size(); i += 1) {
143
+ output.push(vector.get(i));
144
+ }
145
+ return output;
146
+ },
147
+ toVector(VectorClass, array = []) {
148
+ const vector = new VectorClass();
149
+ array.forEach((item) => {
150
+ vector.push_back(item);
151
+ });
152
+ return vector;
153
+ },
154
+ };
155
+ Module(m).then(resolve).catch(reject);
156
+ });
157
+
158
+ return cppJsPromise;
159
+ }
160
+
161
+ export default initCppJs;
@@ -1,19 +1,44 @@
1
- cmake_minimum_required(VERSION 3.25)
1
+ cmake_minimum_required(VERSION 3.28)
2
2
  set(CMAKE_CXX_STANDARD 11)
3
3
  set(PROJECT_NAME "___PROJECT_NAME___")
4
+ set(PROJECT_LIBS "___PROJECT_LIBS___")
4
5
  project("${PROJECT_NAME}")
5
6
 
6
- set(PACKAGE_HOST "${CMAKE_SYSTEM_NAME}-${CMAKE_HOST_SYSTEM_PROCESSOR}")
7
+ if(ANDROID)
8
+ set(PACKAGE_HOST "${CMAKE_SYSTEM_NAME}-${CMAKE_ANDROID_ARCH_ABI}")
9
+ set(PACKAGE_DIR "${PROJECT_SOURCE_DIR}/${PACKAGE_HOST}/lib")
10
+ elseif(APPLE)
11
+ if (CMAKE_SYSTEM_NAME STREQUAL "iOS")
12
+ set(PACKAGE_DIR "${PROJECT_SOURCE_DIR}")
13
+ else()
14
+ set(PACKAGE_HOST "${CMAKE_SYSTEM_NAME}-${CMAKE_HOST_SYSTEM_PROCESSOR}")
15
+ set(PACKAGE_DIR "${PROJECT_SOURCE_DIR}/${PACKAGE_HOST}/lib")
16
+ endif()
17
+ elseif(UNIX)
18
+ set(PACKAGE_HOST "${CMAKE_SYSTEM_NAME}-${CMAKE_HOST_SYSTEM_PROCESSOR}")
19
+ set(PACKAGE_DIR "${PROJECT_SOURCE_DIR}/${PACKAGE_HOST}/lib")
20
+ else()
21
+ set(PACKAGE_HOST "${CMAKE_SYSTEM_NAME}-${CMAKE_HOST_SYSTEM_PROCESSOR}")
22
+ set(PACKAGE_DIR "${PROJECT_SOURCE_DIR}/${PACKAGE_HOST}/lib")
23
+ endif()
7
24
 
8
- find_library(PROJECT_LIBRARY
9
- NAMES "${PROJECT_NAME}"
10
- PATHS "${PROJECT_SOURCE_DIR}/${PACKAGE_HOST}/lib"
11
- NO_CACHE
12
- NO_DEFAULT_PATH
13
- NO_CMAKE_FIND_ROOT_PATH
14
- REQUIRED
15
- )
25
+ set(PROJECT_LIBS_DIR)
26
+ foreach(L IN LISTS PROJECT_LIBS)
27
+ SET(FOUND_LIB "FOUND_LIB-NOTFOUND")
28
+ find_library(FOUND_LIB
29
+ NAMES "${L}"
30
+ PATHS "${PACKAGE_DIR}"
31
+ NO_CACHE
32
+ NO_DEFAULT_PATH
33
+ NO_CMAKE_FIND_ROOT_PATH
34
+ REQUIRED
35
+ )
36
+ LIST(APPEND PROJECT_LIBS_DIR ${FOUND_LIB})
37
+ endforeach()
16
38
 
17
39
  add_library("${PROJECT_NAME}" INTERFACE)
18
- target_link_libraries("${PROJECT_NAME}" INTERFACE "${PROJECT_LIBRARY}")
40
+ target_link_libraries("${PROJECT_NAME}" INTERFACE "${PROJECT_LIBS_DIR}")
41
+
42
+ if(NOT APPLE)
19
43
  target_include_directories("${PROJECT_NAME}" INTERFACE "${PROJECT_SOURCE_DIR}/${PACKAGE_HOST}/include")
44
+ endif()