Skip to content

Commit 022a8cd

Browse files
JanWielemakerclaude
andcommitted
FIXED: WASM: read a JavaScript object reference back using blob(resolve)
JavaScript objects are now written as <js>(Id,Class) rather than <js_Class>(Id). The name between the brackets must be the name of the blob type for the reader to find the object back, so the blob type is renamed from `js_object' to `js'. Writing the class no longer creates an atom (which was never unregistered): the %As of SfprintfX() quotes it if needed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017dv8qG8JrSfiHguZaFbLAW
1 parent 767066d commit 022a8cd

3 files changed

Lines changed: 26 additions & 24 deletions

File tree

library/wasm.pl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,13 @@
247247
% Test whether a Prolog term is a JavaScript object.
248248

249249
is_object(Term) :-
250-
blob(Term, js_object).
250+
blob(Term, js).
251251

252252
is_object(Term, Class), atom(Class) =>
253-
blob(Term, js_object),
253+
blob(Term, js),
254254
true := Term.instanceof(Class).
255255
is_object(Term, Class), var(Class) =>
256-
blob(Term, js_object),
256+
blob(Term, js),
257257
Class := Term.instanceof().
258258

259259

man/wasm.plx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,10 @@ Instances of \ctype{ArrayBuffer} are translated into a Prolog
511511
string that consists of characters in the range $0\ldots{}255$.
512512
\definition{Objects of a one class not being \const{Object}}
513513
Instances of non-plain JavaScript objects are translated into a
514-
Prolog \jargon{blob}. Such objects are written as \verb$<js_Class(id)>$.
515-
The Prolog interface allows for passing the objects back and calling
516-
methods on them. See \secref{wasm-js-call}.
514+
Prolog \jargon{blob}. Such objects are written as
515+
\verb$<js>(Id,Class)$. The Prolog interface allows for passing
516+
the objects back and calling methods on them. See
517+
\secref{wasm-js-call}.
517518
\end{description}
518519

519520

@@ -614,7 +615,7 @@ following ``functions'' are handled directly by the implementation.
614615
quotes. For example:
615616
\begin{code}
616617
?- W := window, T := W.instanceof('Window').
617-
W = <js_Window>(1),
618+
W = <js>(1,'Window'),
618619
T = true.
619620
\end{code}
620621
\termitem{-}{Any}
@@ -696,9 +697,9 @@ that await/2 allows, for example, downloading a URL from Prolog:
696697
\begin{code}
697698
?- FP := fetch("test.pl"), await(FP, Response),
698699
TP := Response.text(), await(TP, T).
699-
FP = <js_Promise>(4),
700-
Response = <js_Response>(5),
701-
TP = <js_Promise>(6),
700+
FP = <js>(4,'Promise'),
701+
Response = <js>(5,'Response'),
702+
TP = <js>(6,'Promise'),
702703
T = "% :- debug(js) ...".
703704
\end{code}
704705

src/wasm/pl-wasm.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,33 +159,34 @@ typedef struct objref
159159
} objref;
160160

161161

162-
static atom_t
162+
/* js_obj_class() returns the class name as a malloc'ed UTF-8 string. */
163+
164+
static char *
163165
js_obj_class(int32_t id)
164-
{ char *str = (char*)EM_ASM_PTR({
166+
{ return (char*)EM_ASM_PTR({
165167
const s = prolog_js_obj_class_name($0);
166168
const len = lengthBytesUTF8(s)+1;
167169
const mem = _malloc(len);
168170
stringToUTF8(s, mem, len);
169171
return mem;
170172
}, id);
171-
atom_t a = PL_new_atom_mbchars(REP_UTF8, (size_t)-1, str);
172-
free(str);
173-
174-
return a;
175173
}
176174

177175

176+
/* Write a JavaScript object reference as <js>(Id,Class). The
177+
* name between the brackets must be the name of our blob type: that is
178+
* what allows read_term/2,3 to find the object back using the option
179+
* blob(resolve). The class is written as a quoted atom.
180+
*/
181+
178182
static int
179183
write_jsobj_ref(IOSTREAM *out, atom_t aref, int flags)
180184
{ objref *ref = PL_blob_data(aref, NULL, NULL);
185+
char *cname = js_obj_class(ref->id);
181186
(void)flags;
182-
atom_t cname = js_obj_class(ref->id);
183-
const wchar_t *s;
184187

185-
PL_STRINGS_MARK();
186-
s = PL_atom_wchars(cname, NULL);
187-
SfprintfX(out, "<js_%Ws>(%d)", s, ref->id);
188-
PL_STRINGS_RELEASE();
188+
SfprintfX(out, "<js>(%d,%UAs)", ref->id, cname);
189+
free(cname);
189190

190191
return true;
191192
}
@@ -206,7 +207,7 @@ save_jsobj_ref(atom_t aref, IOSTREAM *fd)
206207
{ objref *ref = PL_blob_data(aref, NULL, NULL);
207208
(void)fd;
208209

209-
return PL_warning("Cannot save reference to <js_object>(%d)",
210+
return PL_warning("Cannot save reference to <js>(%d)",
210211
ref->id);
211212
}
212213

@@ -222,7 +223,7 @@ load_jsobj_ref(IOSTREAM *fd)
222223
static PL_blob_t js_obj_blob =
223224
{ PL_BLOB_MAGIC,
224225
PL_BLOB_UNIQUE,
225-
"js_object",
226+
"js",
226227
release_jsobj_blob,
227228
NULL,
228229
write_jsobj_ref,

0 commit comments

Comments
 (0)