From 7318098a6068e312c787eca834baef3daf03a642 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Tue, 17 Jan 2017 22:18:00 +0100 Subject: [PATCH] Add a simple example. --- docs/examples.html | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/docs/examples.html b/docs/examples.html index 91f2812..144d622 100644 --- a/docs/examples.html +++ b/docs/examples.html @@ -75,6 +75,35 @@ js_newobject(J); js_setglobal(J, "t"); +

Callbacks from C to JS (by name)

+ +
+static int call_callback(js_State *J, const char *arg1, int arg2)
+{
+	int result;
+
+	/* Find the function to call. */
+	js_getglobal(J, "my_callback");
+
+	/* Push arguments to function. */
+	js_pushnull(J); /* the 'this' object to use */
+	js_pushstring(J, arg1);
+	js_pushnumber(J, arg2);
+
+	/* Call function and check for exceptions. */
+	if (js_pcall(J, 2)) {
+		fprintf(stderr, "an exception occurred in the javascript callback\n");
+		js_pop(J, 1);
+		return -1;
+	}
+
+	/* Retrieve return value. */
+	result = js_tonumber(J, -1);
+	js_pop(J, 1);
+
+	return result;
+}
+
 

Callbacks from C to JS