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

feat: JS runtime #78

Merged
merged 14 commits into from
Apr 5, 2024
Merged

feat: JS runtime #78

merged 14 commits into from
Apr 5, 2024

Conversation

nasdf
Copy link
Collaborator

@nasdf nasdf commented Mar 21, 2024

Relevant issue(s)

Closes #79

Description

This PR adds a browser based JavaScript runtime to lens.

The runtime passes all tests within the host-go package.

Notable changes:

  • Lens path requires a file:// or http:// https:// prefix
  • The Data method of instance has been changed to Memory
  • runtimes.Default has been added to return a default runtime for the current environment.

Todo:

  • Come up with a strategy to test the runtime using the integration tests.
  • Add github action workflow for js runtime tests

Tasks

  • I made sure the code is well commented, particularly hard-to-understand areas.
  • I made sure the repository-held documentation is changed accordingly.
  • I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in tools/configs/validate-conventional-style.sh.
  • I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ...

How has this been tested?

make test:js

Specify the platform(s) on which this was tested:

  • MacOS

@nasdf nasdf self-assigned this Apr 4, 2024
@nasdf nasdf marked this pull request as ready for review April 4, 2024 21:30
@nasdf nasdf requested a review from AndrewSisley April 4, 2024 21:30
Copy link
Collaborator

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

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

praise: Was a really nice surprise as t how simple you managed to make this Keenan - really great job, I hope getting this deep into the guts of the lens codebase wasn't too painful.

praise: The change to the path variable is really nice :)

I have a handful of comments for you, but nothing too major - will hold off on approval until the Memory func thread has been resolved (I'll try to be prompt in giving any responses there)

Makefile Outdated
.PHONY: test\:js
test\:js:
$(MAKE) deps:test-js
$(MAKE) --no-print-directory -C ./host-go test:js
Copy link
Collaborator

Choose a reason for hiding this comment

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

nitpick: Please fix the last line (end in line-break), same for host-go/Makefile

@@ -47,12 +52,35 @@ func append[TSource any, TResult any](src enumerable.Enumerable[TSource], instan
//
// This is a fairly expensive operation.
func NewModule(runtime module.Runtime, path string) (module.Module, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

todo: Please document the new path behaviour and requirements on this public function.

GetData func() []byte
// Values written to memory will be made available to this module, however changes made by the
// module after this function has been called are not guaranteed to be visible to the returned io.Reader.
Memory func(offset int32) io.ReadWriter
Copy link
Collaborator

Choose a reason for hiding this comment

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

question/suggestion: I dont fully understand why the offset param has been added - could you not just change the return type to ReadWriteSeeker and leave the func parameterless? That would let the caller remain responsible for seeking, and keep this (custom) interface simpler (allowing complexity and documentation to be offloaded to a std lib interface).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried the io.Seeker route and it ended being more clunky than this approach.

Let me know if you are happy with the newly added module.Memory interface.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah fair enough - new version looks good, glad you managed you make use of std interfaces for it even without ReadWriteSeeker - thanks :) I also wonder if Memory and BytesMemory might be useful outside of lens at somepoint.


// OwnedBy hosts a reference to any object(s) that may be required to live in memory for the lifetime of this Module.
//
// This is very important when working with some libraries (such as wasmer-go), as without this, dependencies of other members
// of this Module may be garbage collected prematurely.
OwnedBy any
}

type SliceReadWriter struct {
Copy link
Collaborator

Choose a reason for hiding this comment

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

todo: Please document this and it's public functions.

Note: If you are able to remove the offset param from the Memory function, you might be able to find an existing 3rd party/std lib type that already implements ReadWriteSeeker for a byte[] and remove this type entirely

var _ module.Runtime = (*wRuntime)(nil)

func New() module.Runtime {
// https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface
Copy link
Collaborator

Choose a reason for hiding this comment

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

praise: Thank you for including the links to the documentation, I might try and do this in my work.

suggestion: It might be nice to include a brief summary along side the link - it is more immediate to readers, and if the link dies or is moved we will still have some documentation. (same goes for all doc link locations)

Copy link
Collaborator

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

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

LGTM, and looking forward to getting defra/lens in the browser :)

@nasdf nasdf merged commit 17d856b into main Apr 5, 2024
4 checks passed
@nasdf nasdf deleted the nasdf/feat/js-runtime branch April 5, 2024 19:15
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.

Add JS runtime
2 participants