shithub: libmujs

Download patch

ref: 7bbec417c841652108c01120a39550c01e010097
parent: a462ed445fba17987b9138c0c9b14d4e640910ee
author: Tor Andersson <tor.andersson@artifex.com>
date: Fri Feb 5 11:43:46 EST 2016

Add callback example to docs to show how to use js_ref.

--- a/docs/examples.html
+++ b/docs/examples.html
@@ -75,6 +75,30 @@
 js_setglobal(J, "t");
 </pre>
 
+<h2>Callbacks from C to JS</h2>
+
+<pre>
+const char *handle = NULL; /* handle to stowed away js function */
+
+static void set_callback(js_State *J)
+{
+	if (handle)
+		js_unref(J, handle); /* delete old function */
+	js_copy(J, 1);
+	handle = js_ref(J); /* stow the js function in the registry */
+}
+
+static void call_callback(js_State *J, int arg1, int arg2)
+{
+	js_getregistry(J, handle); /* retrieve the js function from the registry */
+	js_pushnull(J);
+	js_pushnumber(J, arg1);
+	js_pushnumber(J, arg2);
+	js_pcall(J, 2);
+	js_pop(J, 1);
+}
+</pre>
+
 <h2>Complete userdata example</h2>
 
 <pre>