Skip to content
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

Implement get_module_property and add JS type #25

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jerbob92
Copy link
Owner

@jerbob92 jerbob92 commented Dec 4, 2023

Add JS Array types with a basic implementation (buffer and length properties).

@codecov-commenter
Copy link

codecov-commenter commented Dec 4, 2023

Codecov Report

Attention: 65 lines in your changes are missing coverage. Please review.

Comparison is base (f3ff5ac) 70.49% compared to head (5dd5423) 70.01%.

Files Patch % Lines
js/array.go 0.00% 44 Missing ⚠️
internal/emval.go 77.21% 9 Missing and 9 partials ⚠️
internal/memory_view.go 62.50% 2 Missing and 1 partial ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #25      +/-   ##
==========================================
- Coverage   70.49%   70.01%   -0.48%     
==========================================
  Files          21       22       +1     
  Lines        4894     5023     +129     
==========================================
+ Hits         3450     3517      +67     
- Misses       1062     1114      +52     
- Partials      382      392      +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@agnivade agnivade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The emval part looks good except for the pointer part. The js adapters need some more modifications. I am looking into it. The emscripten code works in a super roundabout way. I'll keep you posted.

if !ok {
panic(fmt.Errorf("could not create memory view"))
}
stack[0] = api.EncodeI32(engine.emvalEngine.toHandle(js.Int8Array{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to pass pointers for all the structs. Otherwise it fails with:

error while calling embind function full_default: reflect: Elem of invalid type js.Uint8Array (recovered by wazero)

@agnivade
Copy link

agnivade commented Dec 5, 2023

So this is what eventually worked for me for Float32Array:

type float32Array struct {
	BackingBuffer []uint8 `embind_arg:"0"`
	ByteOffset    uint32  `embind_arg:"1"`
	Length        int32   `embind_arg:"2"`
	Constructor   *float32Array
	Buffer        []float32
}

func NewFloat32Array(in []float32) *float32Array {
	return &float32Array{
		Buffer:      in,
		Constructor: &float32Array{},
		Length:      int32(len(in)),
	}
}

func (fa *float32Array) Set(in *float32Array) {
	reslice := fa.BackingBuffer[fa.ByteOffset : fa.ByteOffset+(uint32(fa.Length)*4)]
	conv := unsafe.Slice((*uint8)(unsafe.Pointer(&in.Buffer[0])), len(in.Buffer)*4)
	copy(reslice, conv)
	fa.Buffer = in.Buffer
	fa.Length = in.Length
}

I don't really need the New method. The embind_args are necessary because of https://github.com/ggerganov/whisper.cpp/blob/f0efd0202dce4173e2835b187a6d745af3ee59cb/examples/whisper.wasm/emscripten.cpp#L86.

And then in the Set method, we need to copy the slice. The Uint8Array works fine.

EDIT: I think it's perfectly fine to expect a typed input to the Set method. I see that you have made it work with any. I'll leave it to you, but don't think there is a need for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants