Skip to content

Commit 951b6f9

Browse files
nidindcodeIO
authored andcommitted
Add shared memory support to compiler (AssemblyScript#494)
1 parent 0c64f21 commit 951b6f9

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

cli/asc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ exports.main = function main(argv, options, callback) {
421421
assemblyscript.setTarget(compilerOptions, 0);
422422
assemblyscript.setNoAssert(compilerOptions, args.noAssert);
423423
assemblyscript.setImportMemory(compilerOptions, args.importMemory);
424+
assemblyscript.setSharedMemory(compilerOptions, args.sharedMemory);
424425
assemblyscript.setImportTable(compilerOptions, args.importTable);
425426
assemblyscript.setMemoryBase(compilerOptions, args.memoryBase >>> 0);
426427
assemblyscript.setSourceMap(compilerOptions, args.sourceMap != null);

cli/asc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@
101101
"type": "b",
102102
"default": false
103103
},
104+
"sharedMemory": {
105+
"description": "Declare memory as shared by settings the max shared memory.",
106+
"type": "i",
107+
"default": 0
108+
},
104109
"memoryBase": {
105110
"description": "Sets the start offset of compiler-generated static memory.",
106111
"type": "i",

src/compiler.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ export class Options {
186186
noAssert: bool = false;
187187
/** If true, imports the memory provided by the embedder. */
188188
importMemory: bool = false;
189+
/** If greater than zero, declare memory as shared by setting max memory to sharedMemory. */
190+
sharedMemory: i32 = 0;
189191
/** If true, imports the function table provided by the embedder. */
190192
importTable: bool = false;
191193
/** If true, generates information necessary for source maps. */
@@ -400,18 +402,19 @@ export class Compiler extends DiagnosticEmitter {
400402
}
401403

402404
// set up memory
405+
var isSharedMemory = options.hasFeature(Feature.THREADS) && options.sharedMemory > 0;
403406
module.setMemory(
404407
this.options.memoryBase /* is specified */ || this.memorySegments.length
405408
? i64_low(i64_shr_u(i64_align(memoryOffset, 0x10000), i64_new(16, 0)))
406409
: 0,
407-
Module.UNLIMITED_MEMORY,
410+
isSharedMemory ? options.sharedMemory : Module.UNLIMITED_MEMORY,
408411
this.memorySegments,
409412
options.target,
410413
"memory"
411414
);
412415

413416
// import memory if requested (default memory is named '0' by Binaryen)
414-
if (options.importMemory) module.addMemoryImport("0", "env", "memory");
417+
if (options.importMemory) module.addMemoryImport("0", "env", "memory", isSharedMemory);
415418

416419
// set up function table
417420
var functionTable = this.functionTable;

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ export function setImportMemory(options: Options, importMemory: bool): void {
9797
options.importMemory = importMemory;
9898
}
9999

100+
/** Sets the `sharedMemory` option. */
101+
export function setSharedMemory(options: Options, sharedMemory: i32): void {
102+
options.sharedMemory = sharedMemory;
103+
}
104+
100105
/** Sets the `importTable` option. */
101106
export function setImportTable(options: Options, importTable: bool): void {
102107
options.importTable = importTable;

0 commit comments

Comments
 (0)