swiftwasm/JavaScriptKit

Accessing localStorage fails

carson-katri opened this issue · 6 comments

Here's a simple example:

let localStorage = JSObjectRef.global.localStorage.object!
print(localStorage.getItem!("hello"))
_ = localStorage.setItem!("hello", "world")
print(localStorage.getItem!("hello"))

Prints null then .string("world") as expected.

However, if you refresh the site after the value is set, it crashes with Reflect.get called on non-object.

@carson-katri Thanks for report! Could you try this branch #19 ?

Sorry, same crash 😕

@carson-katri Hmm, did you reference local runtime library in this repo instead of the released runtime library?
I think you may be using version 0.4.0. Please specify local built runtime library following these steps.

  1. Build runtime library on your machine
$ pwd
path/to/JavaScriptKit
$ cd Runtime && npm run build
  1. Update your package.json
{
  ...
  "dependencies": {
    ...
-    "javascript-kit-swift": "0.4.0"
+    "javascript-kit-swift": "file:path/to/JavaScriptKit/Runtime"
  },
  ...
}
  1. Update your Package.swift
let package = Package(
    ...
-    .package(url: "https://github.com/kateinoigakukun/JavaScriptKit.git", .revision("master")),
+    dependencies: [.package(name: "JavaScriptKit", path: "path/to/JavaScriptKit")],
    ...
)

I assume, if the project is built with carton it uses the runtime shipped with the dev entrypoint: https://github.com/swiftwasm/carton/blob/main/package.json

Most probably easier to test that branch without carton, maybe through some custom webpack project.

Got it working, thanks!