Skip to content

Will AS have support for pointers and references? #1228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mpmartins opened this issue Apr 16, 2020 · 7 comments
Closed

Will AS have support for pointers and references? #1228

mpmartins opened this issue Apr 16, 2020 · 7 comments

Comments

@mpmartins
Copy link

Hey guys,

I was just curious about this and I wanted to hear your thoughts.
Do you guys intend to add support for * and & to the language?

I understand that this might have consequences with the GC implementation and to TS Portability, but there are situations in which we need to call another application ABI and they take pointers as parameters.

See this example from Yuval Kohavi (@yuval-k) here:
https://github.com/solo-io/proxy-runtime/blob/master/assembly/runtime.ts#L207-L212
In order to make this work, he had to create a Reference object like so:
https://github.com/solo-io/proxy-runtime/blob/master/assembly/runtime.ts#L33-L39

This was a clever workaround, but do you guys intend to provide better syntax for such things? What are your thoughts about it?

Thanks,
Mario

@MaxGraey
Copy link
Member

MaxGraey commented Apr 16, 2020

Refs / pointers could be emulated via arrays

other solution:

const outPtrToInt = __alloc(sizeof<u32>(), 0);
importedHostFn(outPtrToInt, ...);
...
// use / read data from outPtrToInt 
...
__free(outPtrToInt);

@dcodeIO
Copy link
Member

dcodeIO commented Apr 16, 2020

One aspect there is that AssemblyScript doesn't have a stack on its own (in linear memory) but exclusively relies on Wasm's execution stack, which one can't take an address of / into. So, typically, one would either make a heap allocation or reserve some space in static memory to reuse for this purpose, as Max pointed out. Open to suggestions on how to improve this :)

@dcodeIO
Copy link
Member

dcodeIO commented Apr 18, 2020

Related: #1233 Might be useful here since it works very much like static in C/C++.

@mpmartins
Copy link
Author

Well, I had something like this in my mind. But, to be honest, I don't really understand all the implications...

Add the following global functions:

@global @unsafea
export function __pointer(reference: any): usize;

@global @unsafe
export function __reference(pointer: usize): any;

After that, * and & could be syntactic sugar that calls these functions in the background.

@MaxGraey
Copy link
Member

WebAssembly hasn't shadow stack. It means only heap or objects in static sections could be interpret as references. In this case only objects could reference and dereference via ptr = changetype<usize>(obj) / obj = changetype<Obj>(ptr). That's quite sufficient for all unsafe stuffs.

@MaxGraey
Copy link
Member

Btw if you want more high-level and safe pointer (and zero-cost btw) you may interested in this experiment: https://github.com/AssemblyScript/assemblyscript/blob/master/tests/compiler/std/pointer.ts#L3

@stale
Copy link

stale bot commented May 29, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label May 29, 2020
@stale stale bot closed this as completed Jun 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants