Skip to content

Commit f100d50

Browse files
committed
runtime: Add _start function for apiVersion v0.0.5
Now we pass a flag (--explicitStart) to `asc` (AssemblyScript compiler) for it to export the `_start` funcion for us to call it manually. This is being done so the host-exports work properly.
1 parent 6d0a3c8 commit f100d50

24 files changed

+83
-3
lines changed

runtime/test/src/test.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ fn mock_context(
234234
}
235235

236236
trait WasmInstanceExt {
237+
fn invoke_export0(&self, f: &str);
237238
fn invoke_export<C, R>(&self, f: &str, arg: AscPtr<C>) -> AscPtr<R>;
238239
fn invoke_export2<C, D, R>(&self, f: &str, arg0: AscPtr<C>, arg1: AscPtr<D>) -> AscPtr<R>;
239240
fn invoke_export2_void<C, D>(
@@ -247,6 +248,11 @@ trait WasmInstanceExt {
247248
}
248249

249250
impl WasmInstanceExt for WasmInstance<Chain> {
251+
fn invoke_export0(&self, f: &str) {
252+
let func = self.get_func(f).typed().unwrap().clone();
253+
let _: () = func.call(()).unwrap();
254+
}
255+
250256
fn invoke_export<C, R>(&self, f: &str, arg: AscPtr<C>) -> AscPtr<R> {
251257
let func = self.get_func(f).typed().unwrap().clone();
252258
let ptr: u32 = func.call(arg.wasm_ptr()).unwrap();
@@ -1387,3 +1393,24 @@ async fn detect_contract_calls() {
13871393
v0_0_4();
13881394
v0_0_5();
13891395
}
1396+
1397+
#[tokio::test]
1398+
async fn allocate_global() {
1399+
fn v0_0_5() {
1400+
let module = test_module(
1401+
"AllocateGlobal",
1402+
mock_data_source("allocate_global.wasm", API_VERSION_0_0_5),
1403+
API_VERSION_0_0_5,
1404+
);
1405+
1406+
// Assert globals can be allocated and don't break the heap
1407+
module.invoke_export0("assert_global_works");
1408+
}
1409+
1410+
// Only in apiVersion v0.0.5 because there's no issue in older versions.
1411+
// The problem with the new one is related to the AS stub runtime `offset`
1412+
// variable not being initialized (lazy) before we use it so this test checks
1413+
// that it works (at the moment using __alloc call to force offset to be eagerly
1414+
// evaluated).
1415+
v0_0_5();
1416+
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// TODO: move to import file
2+
__alloc(0);
3+
4+
enum IndexForAscTypeId {
5+
STRING = 0,
6+
ARRAY_BUFFER = 1,
7+
UINT8_ARRAY = 6,
8+
}
9+
10+
export function id_of_type(type_id_index: IndexForAscTypeId): usize {
11+
switch (type_id_index) {
12+
case IndexForAscTypeId.STRING:
13+
return idof<string>();
14+
case IndexForAscTypeId.ARRAY_BUFFER:
15+
return idof<ArrayBuffer>();
16+
case IndexForAscTypeId.UINT8_ARRAY:
17+
return idof<Uint8Array>();
18+
default:
19+
return 0;
20+
}
21+
}
22+
23+
export function allocate(n: usize): usize {
24+
return __alloc(n);
25+
}
26+
let globalOne = bigInt.fromString("1")
27+
28+
type BigInt = Uint8Array;
29+
30+
declare namespace bigInt {
31+
function fromString(s: string): BigInt
32+
}
33+
34+
35+
export function assert_global_works(): void {
36+
let localOne = bigInt.fromString("1")
37+
assert(globalOne != localOne)
38+
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

runtime/wasm/src/module/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ impl<C: Blockchain> WasmInstance<C> {
266266
) -> Result<WasmInstance<C>, anyhow::Error> {
267267
let mut linker = wasmtime::Linker::new(&wasmtime::Store::new(valid_module.module.engine()));
268268
let host_fns = ctx.host_fns.cheap_clone();
269+
let api_version = ctx.host_exports.api_version.clone();
269270

270271
// Used by exports to access the instance context. There are two ways this can be set:
271272
// - After instantiation, if no host export is called in the start function.
@@ -524,6 +525,18 @@ impl<C: Blockchain> WasmInstance<C> {
524525
)?);
525526
}
526527

528+
match api_version {
529+
version if version <= Version::new(0, 0, 4) => {}
530+
_ => {
531+
instance
532+
.get_func("_start")
533+
.context("`_start` function not found")?
534+
.typed::<(), ()>()?
535+
.call(())
536+
.unwrap();
537+
}
538+
}
539+
527540
Ok(WasmInstance {
528541
instance,
529542
instance_ctx: shared_ctx,

tests/integration-tests/host-exports/src/mapping.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Trigger } from "../generated/Contract/Contract";
22
import { Address, BigDecimal, BigInt, ethereum } from "@graphprotocol/graph-ts";
33

4-
// Test that host exports work in globals.
4+
// TODO: remove this, graph-ts should guarantee this
5+
__alloc(0);
6+
57
let one = BigDecimal.fromString("1");
68

79
export function handleTrigger(event: Trigger): void {

tests/integration-tests/yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@
783783

784784
"@graphprotocol/graph-cli@https://github.com/graphprotocol/graph-cli#otavio/update-assembly-script":
785785
version "0.20.1"
786-
resolved "https://github.com/graphprotocol/graph-cli#4b0246c0f6bd7a77e41d135ee6fbc4370ef7d3a4"
786+
resolved "https://github.com/graphprotocol/graph-cli#8ff5148bb7c5f73a7cc73f77acbb68716696c45a"
787787
dependencies:
788788
assemblyscript "0.19.2"
789789
chalk "^3.0.0"
@@ -816,7 +816,7 @@
816816

817817
"@graphprotocol/graph-ts@https://github.com/graphprotocol/graph-ts#otavio/update-assembly-script":
818818
version "0.21.0"
819-
resolved "https://github.com/graphprotocol/graph-ts#9781f5f687831a1bbf2bda4c8c5d8d375c3fafa7"
819+
resolved "https://github.com/graphprotocol/graph-ts#da9d30f105695aa86df90ed559fc984768e0a9f1"
820820
dependencies:
821821
assemblyscript "0.19.2"
822822

0 commit comments

Comments
 (0)