Skip to content
Snippets Groups Projects
Commit 829b17e2 authored by Cheng Shao's avatar Cheng Shao
Browse files

wasm: make JSVal internal Weak# point to lifted JSVal

JSVal has an internal Weak# with the unlifted JSVal# object as key to
arrange its builtin finalization logic. The Weak# used to designate
Unit_closure as a dummy value; now this commit designates the lifted
JSVal closure as the Weak# value. This allows the implementation of
mkWeakJSVal which can be used to observe the liveliness of a JSVal and
attach a user-specified finalizer.

(cherry picked from commit 4f342431)
(cherry picked from commit 4941e126)
parent 1b9ce4c2
No related branches found
No related tags found
No related merge requests found
...@@ -82,7 +82,7 @@ newtype JSVal# ...@@ -82,7 +82,7 @@ newtype JSVal#
= JSVal# (Any :: UnliftedType) = JSVal# (Any :: UnliftedType)
data JSVal data JSVal
= forall a . JSVal JSVal# (Weak# JSVal#) (StablePtr# a) = forall a . JSVal JSVal# (Weak# JSVal) (StablePtr# a)
freeJSVal :: JSVal -> IO () freeJSVal :: JSVal -> IO ()
freeJSVal v@(JSVal _ w sp) = do freeJSVal v@(JSVal _ w sp) = do
......
...@@ -107,7 +107,6 @@ HaskellObj rts_mkJSVal(Capability *cap, HsJSVal v) { ...@@ -107,7 +107,6 @@ HaskellObj rts_mkJSVal(Capability *cap, HsJSVal v) {
SET_HDR(w, &stg_WEAK_info, CCS_SYSTEM); SET_HDR(w, &stg_WEAK_info, CCS_SYSTEM);
w->cfinalizers = (StgClosure *)cfin; w->cfinalizers = (StgClosure *)cfin;
w->key = p; w->key = p;
w->value = Unit_closure;
w->finalizer = &stg_NO_FINALIZER_closure; w->finalizer = &stg_NO_FINALIZER_closure;
w->link = cap->weak_ptr_list_hd; w->link = cap->weak_ptr_list_hd;
cap->weak_ptr_list_hd = w; cap->weak_ptr_list_hd = w;
...@@ -120,7 +119,9 @@ HaskellObj rts_mkJSVal(Capability *cap, HsJSVal v) { ...@@ -120,7 +119,9 @@ HaskellObj rts_mkJSVal(Capability *cap, HsJSVal v) {
box->payload[0] = p; box->payload[0] = p;
box->payload[1] = (HaskellObj)w; box->payload[1] = (HaskellObj)w;
box->payload[2] = NULL; box->payload[2] = NULL;
return TAG_CLOSURE(1, box);
w->value = TAG_CLOSURE(1, box);
return w->value;
} }
__attribute__((import_module("ghc_wasm_jsffi"), import_name("getJSVal"))) __attribute__((import_module("ghc_wasm_jsffi"), import_name("getJSVal")))
......
...@@ -27,7 +27,7 @@ foreign export javascript "testDynExportFree sync" ...@@ -27,7 +27,7 @@ foreign export javascript "testDynExportFree sync"
-- JSVal#. Do not use this in your own codebase since this is purely -- JSVal#. Do not use this in your own codebase since this is purely
-- an implementation detail of JSVal and subject to change! -- an implementation detail of JSVal and subject to change!
jsvalWeak :: JSVal -> Weak JSVal jsvalWeak :: JSVal -> Weak JSVal
jsvalWeak (JSVal _ w _) = Weak $ unsafeCoerce# Weak w jsvalWeak (JSVal _ w _) = Weak w
probeWeak :: Weak v -> IO () probeWeak :: Weak v -> IO ()
probeWeak wk = print =<< isJust <$> deRefWeak wk probeWeak wk = print =<< isJust <$> deRefWeak wk
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment