Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/mujs/docs/examples.html @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 <!DOCTYPE html> | |
| 2 <html> | |
| 3 <head> | |
| 4 <link href="style.css" rel="stylesheet"> | |
| 5 <title>MuJS Examples</title> | |
| 6 </head> | |
| 7 | |
| 8 <body> | |
| 9 | |
| 10 <header> | |
| 11 <h1>MuJS Examples</h1> | |
| 12 </header> | |
| 13 | |
| 14 <nav> | |
| 15 <a href="introduction.html">Introduction</a> | |
| 16 <a href="reference.html">Reference</a> | |
| 17 <a href="examples.html">Examples</a> | |
| 18 <a href="license.html">License</a> | |
| 19 <a href="http://git.ghostscript.com/?p=mujs.git;a=summary">Source</a> | |
| 20 <a href="https://bugs.ghostscript.com/">Bugs</a> | |
| 21 </nav> | |
| 22 | |
| 23 <article> | |
| 24 | |
| 25 <h2>A stand-alone interpreter</h2> | |
| 26 | |
| 27 <pre> | |
| 28 #include <stdio.h> | |
| 29 #include <mujs.h> | |
| 30 | |
| 31 int main(int argc, char **argv) | |
| 32 { | |
| 33 char line[256]; | |
| 34 js_State *J = js_newstate(NULL, NULL, JS_STRICT); | |
| 35 while (fgets(line, sizeof line, stdin)) | |
| 36 js_dostring(J, line); | |
| 37 js_freestate(J); | |
| 38 } | |
| 39 </pre> | |
| 40 | |
| 41 <h2>Hello, world!</h2> | |
| 42 | |
| 43 <pre> | |
| 44 #include <stdio.h> | |
| 45 #include <mujs.h> | |
| 46 | |
| 47 static void hello(js_State *J) | |
| 48 { | |
| 49 const char *name = js_tostring(J, 1); | |
| 50 printf("Hello, %s!\n", name); | |
| 51 js_pushundefined(J); | |
| 52 } | |
| 53 | |
| 54 int main(int argc, char **argv) | |
| 55 { | |
| 56 js_State *J = js_newstate(NULL, NULL, JS_STRICT); | |
| 57 | |
| 58 js_newcfunction(J, hello, "hello", 1); | |
| 59 js_setglobal(J, "hello"); | |
| 60 | |
| 61 js_dostring(J, "hello('world');"); | |
| 62 | |
| 63 js_freestate(J); | |
| 64 } | |
| 65 </pre> | |
| 66 | |
| 67 <h2>Configuration file</h2> | |
| 68 | |
| 69 <pre> | |
| 70 js_dofile(J, "config.js") | |
| 71 | |
| 72 js_getglobal(J, "foo"); | |
| 73 foo = js_tonumber(J, -1); | |
| 74 js_pop(J, 1); | |
| 75 </pre> | |
| 76 | |
| 77 <h2>Object manipulation</h2> | |
| 78 | |
| 79 <pre> | |
| 80 // t = { foo: 42, bar: true } | |
| 81 | |
| 82 js_newobject(J); | |
| 83 { | |
| 84 js_pushnumber(J, 42); | |
| 85 js_setproperty(J, -2, "foo"); | |
| 86 js_pushboolean(J, 1); | |
| 87 js_setproperty(J, -2, "bar"); | |
| 88 } | |
| 89 js_setglobal(J, "t"); | |
| 90 </pre> | |
| 91 | |
| 92 <h2>Callbacks from C to JS (by name)</h2> | |
| 93 | |
| 94 <pre> | |
| 95 static int call_callback(js_State *J, const char *arg1, int arg2) | |
| 96 { | |
| 97 int result; | |
| 98 | |
| 99 /* Find the function to call. */ | |
| 100 js_getglobal(J, "my_callback"); | |
| 101 | |
| 102 /* Push arguments to function. */ | |
| 103 js_pushnull(J); /* the 'this' object to use */ | |
| 104 js_pushstring(J, arg1); | |
| 105 js_pushnumber(J, arg2); | |
| 106 | |
| 107 /* Call function and check for exceptions. */ | |
| 108 if (js_pcall(J, 2)) { | |
| 109 fprintf(stderr, "an exception occurred in the javascript callback\n"); | |
| 110 js_pop(J, 1); | |
| 111 return -1; | |
| 112 } | |
| 113 | |
| 114 /* Retrieve return value. */ | |
| 115 result = js_tonumber(J, -1); | |
| 116 js_pop(J, 1); | |
| 117 | |
| 118 return result; | |
| 119 } | |
| 120 </pre> | |
| 121 | |
| 122 <h2>Callbacks from C to JS</h2> | |
| 123 | |
| 124 <pre> | |
| 125 const char *handle = NULL; /* handle to stowed away js function */ | |
| 126 | |
| 127 static void set_callback(js_State *J) | |
| 128 { | |
| 129 if (handle) | |
| 130 js_unref(J, handle); /* delete old function */ | |
| 131 js_copy(J, 1); | |
| 132 handle = js_ref(J); /* stow the js function in the registry */ | |
| 133 } | |
| 134 | |
| 135 static void call_callback(js_State *J, int arg1, int arg2) | |
| 136 { | |
| 137 js_getregistry(J, handle); /* retrieve the js function from the registry */ | |
| 138 js_pushnull(J); | |
| 139 js_pushnumber(J, arg1); | |
| 140 js_pushnumber(J, arg2); | |
| 141 js_pcall(J, 2); | |
| 142 js_pop(J, 1); | |
| 143 } | |
| 144 </pre> | |
| 145 | |
| 146 <h2>Complete userdata example</h2> | |
| 147 | |
| 148 <pre> | |
| 149 #include <stdio.h> | |
| 150 #include <mujs.h> | |
| 151 | |
| 152 #define TAG "File" | |
| 153 | |
| 154 static void new_File(js_State *J) | |
| 155 { | |
| 156 FILE *file; | |
| 157 | |
| 158 if (js_isundefined(J, 1)) { | |
| 159 file = stdin; | |
| 160 } else { | |
| 161 const char *filename = js_tostring(J, 1); | |
| 162 file = fopen(filename, "r"); | |
| 163 if (!file) | |
| 164 js_error(J, "cannot open file: '%s'", filename); | |
| 165 } | |
| 166 | |
| 167 js_currentfunction(J); | |
| 168 js_getproperty(J, -1, "prototype"); | |
| 169 js_newuserdata(J, TAG, file); | |
| 170 } | |
| 171 | |
| 172 static void File_prototype_readByte(js_State *J) | |
| 173 { | |
| 174 FILE *file = js_touserdata(J, 0, TAG); | |
| 175 js_pushnumber(J, getc(file)); | |
| 176 } | |
| 177 | |
| 178 static void File_prototype_readLine(js_State *J) | |
| 179 { | |
| 180 char line[256], *s; | |
| 181 FILE *file = js_touserdata(J, 0, TAG); | |
| 182 s = fgets(line, sizeof line, file); | |
| 183 if (s) | |
| 184 js_pushstring(J, line); | |
| 185 else | |
| 186 js_pushnull(J); | |
| 187 } | |
| 188 | |
| 189 static void File_prototype_close(js_State *J) | |
| 190 { | |
| 191 FILE *file = js_touserdata(J, 0, TAG); | |
| 192 fclose(file); | |
| 193 js_pushundefined(J); | |
| 194 } | |
| 195 | |
| 196 void initfile(js_State *J) | |
| 197 { | |
| 198 js_getglobal(J, "Object"); | |
| 199 js_getproperty(J, -1, "prototype"); // File.prototype.[[Prototype]] = Object.prototype | |
| 200 js_newuserdata(J, TAG, stdin); // File.prototype.[[Userdata]] = stdin | |
| 201 { | |
| 202 js_newcfunction(J, File_prototype_readByte, "File.prototype.readByte", 0); | |
| 203 js_defproperty(J, -2, "readByte", JS_DONTENUM); | |
| 204 | |
| 205 js_newcfunction(J, File_prototype_readLine, "File.prototype.readLine", 0); | |
| 206 js_defproperty(J, -2, "readLine", JS_DONTENUM); | |
| 207 | |
| 208 js_newcfunction(J, File_prototype_close, "File.prototype.close", 0); | |
| 209 js_defproperty(J, -2, "close", JS_DONTENUM); | |
| 210 } | |
| 211 js_newcconstructor(J, new_File, new_File, "File", 1); | |
| 212 js_defglobal(J, "File", JS_DONTENUM); | |
| 213 } | |
| 214 </pre> | |
| 215 | |
| 216 </article> | |
| 217 | |
| 218 <footer> | |
| 219 <a href="http://artifex.com"><img src="artifex-logo.png" align="right"></a> | |
| 220 Copyright © 2013-2017 Artifex Software Inc. | |
| 221 </footer> | |
| 222 | |
| 223 </body> | |
| 224 </html> |
