Skip to content

Commit c1476e7

Browse files
committed
1 parent cc9ff81 commit c1476e7

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

src/compiler.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,7 @@ export class Compiler extends DiagnosticEmitter {
14911491
/** Ensures that the specified string exists in static memory and returns a pointer to it. */
14921492
ensureStaticString(stringValue: string): ExpressionRef {
14931493
var program = this.program;
1494+
var module = this.module;
14941495
var rtHeaderSize = program.runtimeHeaderSize;
14951496
var stringInstance = assert(program.stringInstance);
14961497
var stringSegment: MemorySegment;
@@ -1509,12 +1510,14 @@ export class Compiler extends DiagnosticEmitter {
15091510
}
15101511
var ref = i64_add(stringSegment.offset, i64_new(rtHeaderSize));
15111512
this.currentType = stringInstance.type;
1513+
var ptr: ExpressionRef;
15121514
if (this.options.isWasm64) {
1513-
return this.module.i64(i64_low(ref), i64_high(ref));
1515+
ptr = module.i64(i64_low(ref), i64_high(ref));
15141516
} else {
15151517
assert(i64_is_u32(ref));
1516-
return this.module.i32(i64_low(ref));
1518+
ptr = module.i32(i64_low(ref));
15171519
}
1520+
return module.relocMem(ptr);
15181521
}
15191522

15201523
ensureStaticArrayBuffer(elementType: Type, values: ExpressionRef[]): MemorySegment {
@@ -7606,9 +7609,14 @@ export class Compiler extends DiagnosticEmitter {
76067609
let arraySegment = this.ensureStaticArrayHeader(elementType, bufferSegment);
76077610
let arrayAddress = i64_add(arraySegment.offset, i64_new(runtimeHeaderSize));
76087611
this.currentType = arrayType;
7609-
return program.options.isWasm64
7610-
? this.module.i64(i64_low(arrayAddress), i64_high(arrayAddress))
7611-
: this.module.i32(i64_low(arrayAddress));
7612+
let ptr: ExpressionRef;
7613+
if (program.options.isWasm64) {
7614+
ptr = module.i64(i64_low(arrayAddress), i64_high(arrayAddress));
7615+
} else {
7616+
assert(i64_is_u32(arrayAddress));
7617+
ptr = module.i32(i64_low(arrayAddress));
7618+
}
7619+
return module.relocMem(ptr);
76127620

76137621
// otherwise allocate a new array header and make it wrap a copy of the static buffer
76147622
} else {

src/module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ export class Module {
472472
this.tbase = globalName ? this.allocStringCached(globalName) : 0;
473473
}
474474

475-
private relocMem(ptr: ExpressionRef): ExpressionRef {
475+
relocMem(ptr: ExpressionRef): ExpressionRef {
476476
var mbase = this.mbase;
477477
if (!mbase) return ptr;
478478
var ref = this.ref;
@@ -483,7 +483,7 @@ export class Module {
483483
}
484484
}
485485

486-
private relocTbl(idx: ExpressionRef): ExpressionRef {
486+
relocTbl(idx: ExpressionRef): ExpressionRef {
487487
var tbase = this.tbase;
488488
if (!tbase) return idx;
489489
var ref = this.ref;

tests/compiler/relocatable.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
exports.preInstantiate = function(imports, exports) {
22
// compiler generates initial = 1 because it doesn't know the imported value
33
// of env.memory_base yet, hence we need to import a suitable memory as well:
4-
imports["env"] = {
5-
"memory": new WebAssembly.Memory({ initial: 2 }),
6-
"memory_base": 65536,
7-
"table_base": 100
8-
};
4+
imports["env"]["memory"] = new WebAssembly.Memory({ initial: 2 });
5+
imports["env"]["memory_base"] = 65536;
6+
imports["env"]["table_base"] = 100;
97
};

tests/compiler/relocatable.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
const someStaticStuff: i32[] = [0];
1+
const staticString = "42";
2+
const staticArray: i32[] = [42];
23

3-
someStaticStuff;
4+
@external("env", "memory_base")
5+
declare const __memory_base: usize;
6+
7+
assert(changetype<usize>(staticString) > __memory_base);
8+
assert(changetype<usize>(staticArray) > __memory_base);
9+
10+
assert(staticString == "42");
11+
// assert(staticArray[0] == 42);

0 commit comments

Comments
 (0)