Skip to content

Commit 24463c8

Browse files
ExelooMartinFillon
authored andcommitted
fix(ecs): fix tests
1 parent 3c3b912 commit 24463c8

File tree

14 files changed

+82
-58
lines changed

14 files changed

+82
-58
lines changed

packages/core/src/application/nanoforge-application.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {
2+
ApplicationContext,
23
type IAssetManagerLibrary,
34
type IComponentSystemLibrary,
45
type ILibrary,
56
type INetworkLibrary,
67
type IRunOptions,
78
} from "@nanoforge/common";
8-
import { ApplicationContext } from "@nanoforge/common/src/context/contexts/application.context";
99

1010
import { Core } from "../core/core";
1111
import { ApplicationConfig } from "./application-config";

packages/ecs/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,5 +268,7 @@ src/**/*.d.ts
268268

269269
# pubilc directory
270270
lib/libecs.wasm
271+
lib/index.d.ts
272+
lib/index.js
271273
compile_commands.json
272274
emsdk/

packages/ecs/Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ $(TS_NAME): $(OBJ)
5252

5353
clean:
5454
$(RM) $(OBJ)
55-
$(RM) -r $(OUT_DIR)
5655
$(RM) $(JS_NAME) $(WASM_NAME) $(HTML_NAME) $(TS_NAME)
5756

5857
fclean: clean
5958

60-
tests: LDFLAGS += -s MODULARIZE=1 -s STANDALONE_WASM=1
59+
tests: LDFLAGS += -s MODULARIZE=1
6160
tests: $(OBJ)
6261
@mkdir -p $(OUT_DIR)
63-
$(CC) $(OBJ) $(LDFLAGS) -o $(JS_NAME)
62+
$(CC) $(OBJ) $(LDFLAGS) --emit-tsd $(TS_NAME) -o $(JS_NAME)
6463

6564
re: fclean all
6665

packages/ecs/lib/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
import "libecs.wasm";
2-
31
export * from "./libecs";
42
export { default as Module } from "./libecs";

packages/ecs/lib/libecs.js

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var Module = (() => {
2-
var _scriptName = import.meta.url;
3-
2+
var _scriptName = typeof document != 'undefined' ? document.currentScript?.src : undefined;
3+
if (typeof __filename != 'undefined') _scriptName = _scriptName || __filename;
44
return (
55
async function(moduleArg = {}) {
66
var moduleRtn;
@@ -40,11 +40,6 @@ var ENVIRONMENT_IS_NODE = typeof process == 'object' && typeof process.versions
4040
var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
4141

4242
if (ENVIRONMENT_IS_NODE) {
43-
// When building an ES module `require` is not normally available.
44-
// We need to use `createRequire()` to construct the require()` function.
45-
const { createRequire } = await import('module');
46-
/** @suppress{duplicate} */
47-
var require = createRequire(import.meta.url);
4843

4944
}
5045

@@ -93,12 +88,7 @@ if (ENVIRONMENT_IS_NODE) {
9388
var fs = require('fs');
9489
var nodePath = require('path');
9590

96-
// EXPORT_ES6 + ENVIRONMENT_IS_NODE always requires use of import.meta.url,
97-
// since there's no way getting the current absolute path of the module when
98-
// support for that is not available.
99-
if (!import.meta.url.startsWith('data:')) {
100-
scriptDirectory = nodePath.dirname(require('url').fileURLToPath(import.meta.url)) + '/';
101-
}
91+
scriptDirectory = __dirname + '/';
10292

10393
// include: node_shell_read.js
10494
readBinary = (filename) => {
@@ -445,15 +435,13 @@ function isExportedByForceFilesystem(name) {
445435
* their build, or no symbols that no longer exist.
446436
*/
447437
function hookGlobalSymbolAccess(sym, func) {
448-
if (typeof globalThis != 'undefined' && !Object.getOwnPropertyDescriptor(globalThis, sym)) {
449-
Object.defineProperty(globalThis, sym, {
450-
configurable: true,
451-
get() {
452-
func();
453-
return undefined;
454-
}
455-
});
456-
}
438+
// In MODULARIZE mode the generated code runs inside a function scope and not
439+
// the global scope, and JavaScript does not provide access to function scopes
440+
// so we cannot dynamically modify the scrope using `defineProperty` in this
441+
// case.
442+
//
443+
// In this mode we simply ignore requests for `hookGlobalSymbolAccess`. Since
444+
// this is a debug-only feature, skipping it is not major issue.
457445
}
458446

459447
function missingGlobal(sym, msg) {
@@ -721,11 +709,7 @@ function createExportWrapper(name, nargs) {
721709

722710
var wasmBinaryFile;
723711
function findWasmBinary() {
724-
if (Module['locateFile']) {
725712
return locateFile('libecs.wasm');
726-
}
727-
// Use bundler-friendly `new URL(..., import.meta.url)` pattern; works in browsers too.
728-
return new URL('libecs.wasm', import.meta.url).href;
729713
}
730714

731715
function getBinarySync(file) {
@@ -3826,4 +3810,10 @@ for (const prop of Object.keys(Module)) {
38263810
return real_Module(arg);
38273811
}
38283812
})();
3829-
export default Module;
3813+
if (typeof exports === 'object' && typeof module === 'object') {
3814+
module.exports = Module;
3815+
// This default export looks redundant, but it allows TS to import this
3816+
// commonjs style module.
3817+
module.exports.default = Module;
3818+
} else if (typeof define === 'function' && define['amd'])
3819+
define([], () => Module);

packages/ecs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"fix": "eslint . --fix && prettier --write .",
3535
"taze": "taze major -w",
3636
"lint-staged": "lint-staged",
37-
"test:unit": "make fclean && make tests -j 16 && jest --config ./jest.config.json"
37+
"test:unit": "make fclean && make tests -j 16 && pnpm clean:script && pnpm build:script && jest --config ./jest.config.json"
3838
},
3939
"dependencies": {
4040
"@nanoforge/common": "workspace:^"

packages/ecs/src/ecs-library.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
import { type Entity, type MainModule, Module, type Registry, type SparseArray } from "@lib";
21
import { type AssetManagerLibrary } from "@nanoforge/asset-manager";
32
import { BaseComponentSystemLibrary, type InitContext } from "@nanoforge/common";
43

4+
import { type Entity, type MainModule, Module, type Registry, type SparseArray } from "../lib";
5+
56
export class ECSLibrary extends BaseComponentSystemLibrary {
6-
name: string = "ECSLibrary";
7-
module: MainModule;
8-
registry: Registry;
9-
path: string = "libecs.wasm";
7+
private module: MainModule;
8+
private registry: Registry;
9+
private readonly path: string = "libecs.wasm";
10+
11+
get name(): string {
12+
return "ECSLibrary";
13+
}
1014

1115
async init(context: InitContext): Promise<void> {
1216
const wasmFile = await context.libraries
1317
.getAssetManager<AssetManagerLibrary>()
1418
.library.getWasm(this.path);
15-
Module({ locateFile: () => wasmFile.path }).then((module) => {
16-
this.module = module;
17-
this.registry = new module.Registry();
18-
return Promise.resolve();
19-
});
19+
this.module = await Module({ locateFile: () => wasmFile.path });
20+
this.registry = new this.module.Registry();
2021
}
2122

2223
async run(): Promise<void> {

packages/ecs/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import "../lib/libecs.wasm";
2+
3+
export * from "./ecs-library";
Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { ECSLibrary } from "../src/ecs-library";
1+
import { AssetManagerLibrary } from "@nanoforge/asset-manager";
2+
import { ApplicationContext, InitContext } from "@nanoforge/common";
3+
import { EditableLibraryManager } from "@nanoforge/core/src/common/library/manager/library.manager";
4+
import { ECSLibrary } from "@nanoforge/ecs/src/ecs-library";
25

36
class Position {
47
constructor(
@@ -11,17 +14,37 @@ class Position {
1114
}
1215

1316
describe("ECSLibrary", () => {
17+
let ecs: ECSLibrary;
18+
const assetManager = new AssetManagerLibrary();
19+
const appContext = new ApplicationContext();
20+
const libraryManager = new EditableLibraryManager();
21+
const context = new InitContext(appContext, libraryManager, {
22+
// @ts-ignore
23+
canvas: null,
24+
files: {
25+
assets: new Map(),
26+
wasm: new Map([["/libecs.wasm", "./lib/libecs.wasm"]]),
27+
wgsl: new Map(),
28+
},
29+
});
30+
libraryManager.setAssetManager(assetManager);
31+
32+
beforeAll(async () => {
33+
await assetManager.init(context);
34+
});
35+
36+
beforeEach(async () => {
37+
ecs = new ECSLibrary();
38+
await ecs.init(context);
39+
});
40+
1441
test("init and spawn entity", async () => {
15-
const ecs = new ECSLibrary("public");
16-
await ecs.init();
1742
const entity = ecs.createEntity();
1843
expect(entity).toBeDefined();
1944
expect(entity.get_id()).toBe(0);
2045
});
2146

2247
test("add component to entity", async () => {
23-
const ecs = new ECSLibrary("public/");
24-
await ecs.init();
2548
const entity = ecs.createEntity();
2649
const pos = new Position(1, 2);
2750
ecs.addComponent(entity, pos);
@@ -31,8 +54,6 @@ describe("ECSLibrary", () => {
3154
});
3255

3356
test("clear", async () => {
34-
const ecs = new ECSLibrary("public/");
35-
await ecs.init();
3657
await ecs.clear();
3758
});
3859
});

packages/ecs/test/tsconfig.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
{
2-
"extends": "../../../tsconfig.spec.json"
2+
"extends": "../../../tsconfig.spec.json",
3+
"compilerOptions": {
4+
"paths": {
5+
"@nanoforge/common": ["./packages/common"],
6+
"@nanoforge/core": ["./packages/core"],
7+
"@nanoforge/core/*": ["./packages/core/*"],
8+
"@nanoforge/asset-manager": ["./packages/asset-manager"],
9+
"@nanoforge/ecs": ["./packages/ecs"],
10+
"@nanoforge/ecs/*": ["./packages/ecs/*"],
11+
"@nanoforge/graphics-2d": ["./packages/graphics-2d"]
12+
}
13+
}
314
}

0 commit comments

Comments
 (0)