Skip to content

Will AS have support for pointers and references? #1228

Closed

Description

@mpmartins

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

Activity

  1. MaxGraey commented on Apr 16, 2020

    @MaxGraey
    Member

    Refs / pointers could be emulated via arrays

    other solution:

    const outPtrToInt = __alloc(sizeof<u32>(), 0);
    importedHostFn(outPtrToInt, ...);
    ...
    // use / read data from outPtrToInt 
    ...
    __free(outPtrToInt);
  2. dcodeIO commented on Apr 16, 2020

    @dcodeIO
    Member

    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 :)

  3. dcodeIO commented on Apr 18, 2020

    @dcodeIO
    Member

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

  4. mpmartins commented on Apr 28, 2020

    @mpmartins
    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.

  5. MaxGraey commented on Apr 28, 2020

    @MaxGraey
    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.

  6. MaxGraey commented on Apr 28, 2020

    @MaxGraey
    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

  7. stale commented on May 29, 2020

    @stale

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions