Skip to content

Commit 88eabd8

Browse files
authored
Expose loadStructInfo JS macro function add additional struct/macro definitions. (#22251)
Eventually it would be nice to enable external use of the gen_struct_info.py script to generate these things, but this is a step in that direction.
1 parent 5af95da commit 88eabd8

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

src/modules.mjs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,23 @@ function addToLibrary(obj, options = null) {
285285
let structs = {};
286286
let defines = {};
287287

288+
/**
289+
* Read JSON file containing struct and macro/define information
290+
* that can then be used in JavaScript via macros.
291+
*/
292+
function loadStructInfo(filename) {
293+
const temp = JSON.parse(read(filename));
294+
Object.assign(structs, temp.structs);
295+
Object.assign(defines, temp.defines);
296+
}
297+
288298
if (!BOOTSTRAPPING_STRUCT_INFO) {
289-
let structInfoFile = 'struct_info_generated.json';
299+
// Load struct and define information.
290300
if (MEMORY64) {
291-
structInfoFile = 'struct_info_generated_wasm64.json';
301+
loadStructInfo('struct_info_generated_wasm64.json');
302+
} else {
303+
loadStructInfo('struct_info_generated.json');
292304
}
293-
// Load struct and define information.
294-
const temp = JSON.parse(read(structInfoFile));
295-
structs = temp.structs;
296-
defines = temp.defines;
297305
}
298306

299307
// Use proxy objects for C_DEFINES and C_STRUCTS so that we can give useful
@@ -510,6 +518,7 @@ function exportRuntime() {
510518

511519
addToCompileTimeContext({
512520
exportRuntime,
521+
loadStructInfo,
513522
LibraryManager,
514523
librarySymbols,
515524
addToLibrary,

src/utility.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ export function loadSettingsFile(f) {
328328
}
329329

330330
export function runInMacroContext(code, options) {
331+
compileTimeContext['__filename'] = options.filename;
332+
compileTimeContext['__dirname'] = path.dirname(options.filename);
331333
return vm.runInNewContext(code, compileTimeContext, options);
332334
}
333335

test/other/test_extra_struct_info.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
console.error("(before) AF_INET=" + {{{ cDefs.AF_INET }}});
2+
3+
/* Loading this struct info file will override the default AF_INET value */
4+
{{{ loadStructInfo(__dirname + '/test_extra_struct_info.json'), null }}}
5+
6+
console.error("(after) AF_INET=" + {{{ cDefs.AF_INET }}});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"defines": {
3+
"AF_INET": 42
4+
}
5+
}

test/test_other.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15010,3 +15010,8 @@ def test_save_temp(self):
1501015010
self.assertExists('hello_world.bc')
1501115011
# emcc takes care of creating the .o
1501215012
self.assertExists('hello_world.o')
15013+
15014+
def test_extra_struct_info(self):
15015+
stderr = self.run_process([EMCC, test_file('hello_world.c'), '--js-library', test_file('other/test_extra_struct_info.js')], stderr=PIPE).stderr
15016+
self.assertContained('(before) AF_INET=2', stderr)
15017+
self.assertContained('(after) AF_INET=42', stderr)

0 commit comments

Comments
 (0)