Skip to content

Commit 3fb730b

Browse files
Fix WASM global vars (#2006)
* fix global wasm vars with one simple trick * fix ts tests * changeset
1 parent 48e925c commit 3fb730b

14 files changed

+61
-1
lines changed

.changeset/tangy-clowns-thank.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-ts': patch
3+
---
4+
5+
fix global variables in wasm

packages/ts/chain/arweave.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import '../common/eager_offset';
12
import { Bytes } from '../common/collections';
23

34
// Most types from this namespace are direct mappings or adaptations from:

packages/ts/chain/cosmos.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import '../common/eager_offset';
12
import { Bytes } from '../common/collections';
23

34
export namespace cosmos {

packages/ts/chain/ethereum.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import '../common/eager_offset';
12
import { Bytes, Wrapped } from '../common/collections';
23
import { Address, BigInt } from '../common/numbers';
34

packages/ts/chain/near.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import '../common/eager_offset';
12
import { Bytes } from '../common/collections';
23
import { BigInt } from '../common/numbers';
34

packages/ts/chain/starknet.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import '../common/eager_offset';
12
import { Bytes } from '../common/collections';
23
import { BigInt } from '../common/numbers';
34

packages/ts/common/conversion.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import './eager_offset';
12
import { Bytes } from './collections';
23

34
/** Host type conversion interface */

packages/ts/common/datasource.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import './eager_offset';
12
import { Entity } from './collections';
23
import { Address } from './numbers';
34

packages/ts/common/eager_offset.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// # What is this file?
2+
// This file is a "hack" to allow global variables in subgraphs and
3+
// on this library (`graph-ts`).
4+
//
5+
// # Why is it needed?
6+
// It's necessary because of one of the features of the AssemblyScript
7+
// compiler we use, the stub runtime.
8+
//
9+
// The problem happens because we call the stub runtime allocation
10+
// (`__alloc`) directly on Rust side (`graph-node`), and that doesn't
11+
// trigger some AssemblyScript aspects of the code.
12+
//
13+
// If you take a look at the stub runtime's code, you'll see that the
14+
// `__alloc` function uses a variable named `offset` tagged as `lazy`.
15+
// Like said above, since we call it on Rust side, this variable is not
16+
// "triggered" to be used, then it's declared below the `__alloc` call
17+
// in the compiled WASM code.
18+
//
19+
// That makes the `graph-node` WASM runtime break because of this out
20+
// of order variable usage.
21+
//
22+
// # How does this fix the issue?
23+
// The way this workaround works is by calling the `__alloc` function
24+
// before everything in the AssemblyScript side. This makes the `offset`
25+
// `lazy` variable be eagerly evaluated when the mappings are compiled
26+
// (since they always import `graph-ts`).
27+
//
28+
// So when we're on Rust side calling `__alloc` it will be fine, because
29+
// the `offset` is declared before call (order fixed because of this file).
30+
//
31+
// The 0 argument to the function call is just because we need no memory
32+
// to be allocated.
33+
//
34+
// # IMPORTANT
35+
// This should be imported in EVERY file which uses external namespaces (`graph-node` host-exports code),
36+
// just to make sure no one imports a file directly and gets an error on global variables.
37+
//
38+
// # Reference
39+
// - Runtimes in AS: https://www.assemblyscript.org/garbage-collection.html#runtime-variants
40+
// - `offset` variable in question: https://github.com/AssemblyScript/assemblyscript/blob/f4091b8f3b6b029d30cd917cf84d97421faadeeb/std/assembly/rt/stub.ts#L9
41+
// @ts-expect-error We do not want to expose __alloc, hence why we just ignore the error
42+
__alloc(0);

packages/ts/common/json.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import './eager_offset';
12
import { Bytes, Result } from './collections';
23
import { BigInt } from './numbers';
34
import { JSONValue } from './value';

packages/ts/common/numbers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import './eager_offset';
12
import { ByteArray, Bytes } from './collections';
23
import { typeConversion } from './conversion';
34

packages/ts/common/value.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import './eager_offset';
12
import { Bytes, TypedMap } from './collections';
23
import { json } from './json';
34
import { Address, BigDecimal, BigInt } from './numbers';

packages/ts/common/yaml.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import './eager_offset';
12
import { Bytes, Result, TypedMap } from './collections';
23
import { BigInt } from './numbers';
34

@@ -294,6 +295,6 @@ export class YAMLTaggedValue {
294295
return true;
295296
}
296297

297-
return !(a! == b!);
298+
return !(a == b);
298299
}
299300
}

packages/ts/test/test.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async function main() {
2828
fs.copyFileSync('common/collections.ts', 'test/temp_lib/common/collections.ts');
2929
fs.copyFileSync('common/conversion.ts', 'test/temp_lib/common/conversion.ts');
3030
fs.copyFileSync('common/datasource.ts', 'test/temp_lib/common/datasource.ts');
31+
fs.copyFileSync('common/eager_offset.ts', 'test/temp_lib/common/eager_offset.ts');
3132
fs.copyFileSync('common/json.ts', 'test/temp_lib/common/json.ts');
3233
fs.copyFileSync('common/numbers.ts', 'test/temp_lib/common/numbers.ts');
3334
fs.copyFileSync('common/value.ts', 'test/temp_lib/common/value.ts');
@@ -50,6 +51,7 @@ async function main() {
5051
fs.unlinkSync('test/temp_lib/common/collections.ts');
5152
fs.unlinkSync('test/temp_lib/common/conversion.ts');
5253
fs.unlinkSync('test/temp_lib/common/datasource.ts');
54+
fs.unlinkSync('test/temp_lib/common/eager_offset.ts');
5355
fs.unlinkSync('test/temp_lib/common/json.ts');
5456
fs.unlinkSync('test/temp_lib/common/numbers.ts');
5557
fs.unlinkSync('test/temp_lib/common/value.ts');

0 commit comments

Comments
 (0)