Comments (4)
Or instead of wrapping useing a proxy.
from as-bind.
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.
@torch2424 If this looks good to you I will integrate it. :D
from as-bind.
@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)
- [good first issue] Error handling for wrong JS glue.
- How does this compare to host bindings? HOT 1
- File as-bind.ts not found
- Not working with the latest AssemblyScript version HOT 3
- Add support for Array<T> HOT 2
- Find a better way to transpose the type table in the README
- as-bind seems to not be able to handle proxies? HOT 1
- Add links to as-loader package HOT 1
- RFE: Document the metadata format HOT 7
- Using AsBind in Node can have some package resolution issues
- The new version(0.7.0) doesn't work. HOT 6
- README is not keeping up with the new version(0.7.0) HOT 1
- Strings can get corrupted, due to them not being a Pinned GC Object HOT 4
- Does as-bind work with the newest assemblyscript version? HOT 2
- [Feature Request] add binding support for declare fucntion exported in a namespace HOT 3
- [Feature Request] add support for generic function HOT 1
- [Bug] Float32Array was bind to ArrayBuffer
- __getArray is not a function HOT 2
- feature request: generate typescript definiton for module HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from as-bind.