-
-
Notifications
You must be signed in to change notification settings - Fork 45
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
Add a helper method to copy an array of numbers to a JS TypedArray #31
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does DOMKit need this for anything? If so, wouldn't there be a use case for conversion in the other direction, from JS to Swift? Do I understand correctly that it's not supported in this PR? If it is planned, it would be totally fine (preferred even) to land that as a separate PR, just wanted to clarify it.
I did a quick search, and it seems like a TypedArray is used as input to the |
Also happy to write something that goes the other way now, though. |
Runtime/src/index.ts
Outdated
elementsPtr: pointer, length: number, | ||
result_obj: pointer | ||
) => { | ||
const ArrayType: TypedArray = this.heap.referenceHeap(0)[JavaScriptTypedArrayKind[kind] + 'Array'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't abuse address 0
just to access global context.
It would be better to store global or window as a member of SwiftRuntime and access it directly 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created a globalVariable
variable as a replacement. Is that a good name or should I go with something else?
I’ve created a |
…h test errors on the JS side happen in
This matches the Objective-C names and better corresponds with their tasks (freeHeap just decreases ref count so it doesn’t always free). I’m not as sure about allocHeap → retain because it adds to the heap in addition to increasing the refcount.
Previously it would just return undefined which would throw an error when attempting to use the object later on. Now you’re able to demangle the stack trace to find exactly where you’re misusing a ref.
Another consideration: While working on #26, I changed the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all, I'm not confident with the current interface design.
When bridging JavaScript object into Swift, the class inheritance relationship information is not given into Swift world.
So they are flattened into JSObjectRef and users need to use JavaScript cast outside of Swift system. (In this case, JavaScript cast means a cast that checks the type of value at runtime using instanceof when casting like JSArrayRef.)
But for JSFunctionRef, we need to consider it as a special class because JavaScript cast depends on JSFunction itself. For example, if JavaScriptKit treats JSFunction as well as JSArrayRef, when casting a JSObjectRef into JSFunctionRef, JavaScript cast system needs to check that the object is a function using obj instanceof Function
. But how can we get Function
class? It can't.
For this reason, JSFunctionRef and JSObjectRef should be distinguished when taking it from JavaScript.
I'd like to see the JSBridgedClass
impl. Could you send another PR to review it?
} | ||
|
||
public init(length: Int) { | ||
let jsObject = Element.typedArrayClass.new(length) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about having the jsObject in JSTypedArray instead of manual reference count management?
@@ -8,6 +8,17 @@ type pointer = number; | |||
interface GlobalVariable { } | |||
declare const window: GlobalVariable; | |||
declare const global: GlobalVariable; | |||
let globalVariable: any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this variable can be capsulized in SwiftRuntimeHeap.
I prefer current generic class version. 👍 |
It’s not 100% clean yet (there’s a few other things in the PR that are unrelated) but I added it in #26. (see diff between it and this PR) There’s the Then there’s the
I might be misunderstanding this but there is a |
I'm thinking of generating some WebGLKit code, thankfully an IDL file for it is available. Using typed arrays is pretty much a requirement for WebGL, so I'd be very happy for this PR to move forward. Is there any way I can help here? |
The main sticking point is on the bridging API. I think the one in #26 can be extracted out to another PR, if @kateinoigakukun likes it. |
Typed array change proposal
Extracted from #26.