examples.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link href="style.css" rel="stylesheet">
  5. <title>MuJS Examples</title>
  6. </head>
  7. <body>
  8. <header>
  9. <h1>MuJS Examples</h1>
  10. </header>
  11. <nav>
  12. <a href="introduction.html">Introduction</a>
  13. <a href="reference.html">Reference</a>
  14. <a href="examples.html">Examples</a>
  15. <a href="license.html">License</a>
  16. <a href="http://git.ghostscript.com/?p=mujs.git;a=summary">Source</a>
  17. <a href="https://bugs.ghostscript.com/">Bugs</a>
  18. </nav>
  19. <article>
  20. <h2>A stand-alone interpreter</h2>
  21. <pre>
  22. #include &lt;stdio.h&gt;
  23. #include &lt;mujs.h&gt;
  24. int main(int argc, char **argv)
  25. {
  26. char line[256];
  27. js_State *J = js_newstate(NULL, NULL, JS_STRICT);
  28. while (fgets(line, sizeof line, stdin))
  29. js_dostring(J, line);
  30. js_freestate(J);
  31. }
  32. </pre>
  33. <h2>Hello, world!</h2>
  34. <pre>
  35. #include &lt;stdio.h&gt;
  36. #include &lt;mujs.h&gt;
  37. static void hello(js_State *J)
  38. {
  39. const char *name = js_tostring(J, 1);
  40. printf("Hello, %s!\n", name);
  41. js_pushundefined(J);
  42. }
  43. int main(int argc, char **argv)
  44. {
  45. js_State *J = js_newstate(NULL, NULL, JS_STRICT);
  46. js_newcfunction(J, hello, "hello", 1);
  47. js_setglobal(J, "hello");
  48. js_dostring(J, "hello('world');");
  49. js_freestate(J);
  50. }
  51. </pre>
  52. <h2>Configuration file</h2>
  53. <pre>
  54. js_dofile(J, "config.js")
  55. js_getglobal(J, "foo");
  56. foo = js_tonumber(J, -1);
  57. js_pop(J, 1);
  58. </pre>
  59. <h2>Object manipulation</h2>
  60. <pre>
  61. // t = { foo: 42, bar: true }
  62. js_newobject(J);
  63. {
  64. js_pushnumber(J, 42);
  65. js_setproperty(J, -2, "foo");
  66. js_pushboolean(J, 1);
  67. js_setproperty(J, -2, "bar");
  68. }
  69. js_setglobal(J, "t");
  70. </pre>
  71. <h2>Callbacks from C to JS (by name)</h2>
  72. <pre>
  73. static int call_callback(js_State *J, const char *arg1, int arg2)
  74. {
  75. int result;
  76. /* Find the function to call. */
  77. js_getglobal(J, "my_callback");
  78. /* Push arguments to function. */
  79. js_pushnull(J); /* the 'this' object to use */
  80. js_pushstring(J, arg1);
  81. js_pushnumber(J, arg2);
  82. /* Call function and check for exceptions. */
  83. if (js_pcall(J, 2)) {
  84. fprintf(stderr, "an exception occurred in the javascript callback\n");
  85. js_pop(J, 1);
  86. return -1;
  87. }
  88. /* Retrieve return value. */
  89. result = js_tonumber(J, -1);
  90. js_pop(J, 1);
  91. return result;
  92. }
  93. </pre>
  94. <h2>Callbacks from C to JS</h2>
  95. <pre>
  96. const char *handle = NULL; /* handle to stowed away js function */
  97. static void set_callback(js_State *J)
  98. {
  99. if (handle)
  100. js_unref(J, handle); /* delete old function */
  101. js_copy(J, 1);
  102. handle = js_ref(J); /* stow the js function in the registry */
  103. }
  104. static void call_callback(js_State *J, int arg1, int arg2)
  105. {
  106. js_getregistry(J, handle); /* retrieve the js function from the registry */
  107. js_pushnull(J);
  108. js_pushnumber(J, arg1);
  109. js_pushnumber(J, arg2);
  110. js_pcall(J, 2);
  111. js_pop(J, 1);
  112. }
  113. </pre>
  114. <h2>Complete userdata example</h2>
  115. <pre>
  116. #include &lt;stdio.h&gt;
  117. #include &lt;mujs.h&gt;
  118. #define TAG "File"
  119. static void new_File(js_State *J)
  120. {
  121. FILE *file;
  122. if (js_isundefined(J, 1)) {
  123. file = stdin;
  124. } else {
  125. const char *filename = js_tostring(J, 1);
  126. file = fopen(filename, "r");
  127. if (!file)
  128. js_error(J, "cannot open file: '%s'", filename);
  129. }
  130. js_currentfunction(J);
  131. js_getproperty(J, -1, "prototype");
  132. js_newuserdata(J, TAG, file);
  133. }
  134. static void File_prototype_readByte(js_State *J)
  135. {
  136. FILE *file = js_touserdata(J, 0, TAG);
  137. js_pushnumber(J, getc(file));
  138. }
  139. static void File_prototype_readLine(js_State *J)
  140. {
  141. char line[256], *s;
  142. FILE *file = js_touserdata(J, 0, TAG);
  143. s = fgets(line, sizeof line, file);
  144. if (s)
  145. js_pushstring(J, line);
  146. else
  147. js_pushnull(J);
  148. }
  149. static void File_prototype_close(js_State *J)
  150. {
  151. FILE *file = js_touserdata(J, 0, TAG);
  152. fclose(file);
  153. js_pushundefined(J);
  154. }
  155. void initfile(js_State *J)
  156. {
  157. js_getglobal(J, "Object");
  158. js_getproperty(J, -1, "prototype"); // File.prototype.[[Prototype]] = Object.prototype
  159. js_newuserdata(J, TAG, stdin); // File.prototype.[[Userdata]] = stdin
  160. {
  161. js_newcfunction(J, File_prototype_readByte, "File.prototype.readByte", 0);
  162. js_defproperty(J, -2, "readByte", JS_DONTENUM);
  163. js_newcfunction(J, File_prototype_readLine, "File.prototype.readLine", 0);
  164. js_defproperty(J, -2, "readLine", JS_DONTENUM);
  165. js_newcfunction(J, File_prototype_close, "File.prototype.close", 0);
  166. js_defproperty(J, -2, "close", JS_DONTENUM);
  167. }
  168. js_newcconstructor(J, new_File, new_File, "File", 1);
  169. js_defglobal(J, "File", JS_DONTENUM);
  170. }
  171. </pre>
  172. </article>
  173. <footer>
  174. <a href="http://artifex.com"><img src="artifex-logo.png" align="right"></a>
  175. Copyright &copy; 2013-2017 Artifex Software Inc.
  176. </footer>
  177. </body>
  178. </html>