GithubHelp home page GithubHelp logo

add support for exported classes about as-bind HOT 4 OPEN

mathe42 avatar mathe42 commented on June 10, 2024 8
add support for exported classes

from as-bind.

Comments (4)

mathe42 avatar mathe42 commented on June 10, 2024

Or instead of wrapping useing a proxy.

from as-bind.

mathe42 avatar mathe42 commented on June 10, 2024

Got a small working prototype (all manual but can be automated):

Assemblyscript

export class Foo {
  constructor(public str: string) {}

  getString(): string {
    return this.str
  }
}

Host:

import * as AsBind from "/node_modules/as-bind/dist/as-bind.esm.js";

const asyncTask = async () => {
  const wasm = await fetch("/build/optimized.wasm").then(v => v.arrayBuffer());

  const asBindInstance = await AsBind.instantiate(wasm);

  let e = asBindInstance.exports

  // When not supported noop
  const pointerRegistry = FinalizationRegistry ? new FinalizationRegistry(ptr => {
    e.__unpin(ptr)
  }) : {register(){}};

  function createProxyClass(klass, definition) {
    const newConstructor = new Proxy(klass, {
      construct(target, args) {
        // TODO: wrap args - get from definition
        const ptr = new target(...args)
        return newConstructor.wrap(ptr)
      },
      get(_, prop) {
        if (prop === 'wrap') {
          return (ptr) => {
            e.__pin(ptr)

            const instance = klass.wrap(ptr)

            const a = new Proxy({}, {
              ownKeys() {
                return Reflect.ownKeys(instance);
              },
              defineProperty() {
                throw new Error("Not allowed!")
              },
              deleteProperty() {
                throw new Error("Not allowed!")
              },
              get(_, ...args) {
                if (prop === '__collect') {
                  return () => e.__unpin(ptr)
                }
                const value = Reflect.get(instance, ...args)
                // TODO: wrap function - or wrap value
                return value
              },
              set(_, ...args) {
                // TODO: check if is settable; wrap value if needed
                return Reflect.set(instance, ...args)
              },
              setPrototypeOf() {
                throw new Error("Not allowed!")
              },
              preventExtensions() {
                throw new Error("Not allowed!")
              },
              isExtensible() {
                return false;
              },
              has(_, p) {
                return Reflect.has(instance, p)
              },
              getPrototypeOf() {
                throw new Error("Not allowed!")
              }
            })

            // handle GC
            pointerRegistry.register(a, ptr)

            return a
          }
        }
        return undefined
      },
      set() {
        throw new Error("Not allowed!")
      }
    })

    return newConstructor
  }


  window.e = e
  window.Foo = createProxyClass(e.Foo, {
    constructor: {
      memberType: "constructor",
      parameterTypes: ["~lib/string/String"],
      returnType: null
    },
    str: {
      memberType: "field",
      set: true,
      get: true,
      type: "~lib/string/String"
    },
    getString: {
      memberType: "function",
      parameterTypes: [],
      returnType: "~lib/string/String"
    }
  })

  // example usage
  const sptr = e.__newString("test")
  const f = new Foo(sptr)
  console.log(e.__getString(f.getString())) // "test"
  const sptr2 = e.__newString("test 42")
  f.str = sptr2
  console.log(e.__getString(f.getString())) // "test 42"
};
asyncTask();

from as-bind.

mathe42 avatar mathe42 commented on June 10, 2024

@torch2424 If this looks good to you I will integrate it. :D

from as-bind.

torch2424 avatar torch2424 commented on June 10, 2024

@mathe42 Thank you very much for drafting up this implementation! 😄 Yeah! I like this approach and it makes sense to me, so let's go for it! 😄

from as-bind.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.