js_test.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Host test for js_mu.c (the MuJS-style API over QuickJS) and the engine's
  2. * modern-syntax coverage the browser relies on. Run: make test-js */
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "js_mu.h"
  6. static int fails;
  7. static char last_report[256];
  8. static void report(js_State *J, const char *msg)
  9. {
  10. (void)J;
  11. snprintf(last_report, sizeof(last_report), "%s", msg);
  12. }
  13. /* Evaluate `src` and compare String(result) with `want`. */
  14. static void expect(js_State *J, const char *src, const char *want)
  15. {
  16. char wrapped[2048];
  17. const char *got;
  18. snprintf(wrapped, sizeof(wrapped), "globalThis.__r = (function(){ %s })();",
  19. src);
  20. if (js_ploadstring(J, "[test]", wrapped)) {
  21. printf("FAIL compile: %s\n %s\n", src, js_trystring(J, -1, "?"));
  22. js_pop(J, 1);
  23. fails++;
  24. return;
  25. }
  26. js_pushundefined(J);
  27. if (js_pcall(J, 0)) {
  28. printf("FAIL run: %s\n %s\n", src, js_trystring(J, -1, "?"));
  29. js_pop(J, 1);
  30. fails++;
  31. return;
  32. }
  33. js_pop(J, 1);
  34. js_runjobs(J);
  35. js_getglobal(J, "__r");
  36. got = js_tostring(J, -1);
  37. if (strcmp(got, want)) {
  38. printf("FAIL %s\n got %s\n want %s\n", src, got, want);
  39. fails++;
  40. }
  41. js_pop(J, 1);
  42. }
  43. /* A C function that throws a TypeError for a negative argument. */
  44. static void c_checked(js_State *J)
  45. {
  46. int v = js_tointeger(J, 1);
  47. if (v < 0)
  48. js_typeerror(J, "negative %d", v);
  49. js_pushnumber(J, v * 2);
  50. }
  51. /* Used with `new`: returns its own object, like the DOM Event constructors. */
  52. static void c_make(js_State *J)
  53. {
  54. js_newobject(J);
  55. js_copy(J, 1);
  56. js_setproperty(J, -2, "type");
  57. }
  58. static void c_data(js_State *J)
  59. {
  60. js_pushnumber(J, (double)(unsigned long)js_currentfunctiondata(J));
  61. }
  62. /* Userdata: "kind" is a read-only hook property, "value" is stored in C,
  63. * everything else falls through to ordinary expando properties. */
  64. static double ud_value = 1;
  65. static int ud_has(js_State *J, void *p, const char *name)
  66. {
  67. if (!strcmp(name, "kind")) {
  68. js_pushstring(J, (const char *)p);
  69. return 1;
  70. }
  71. if (!strcmp(name, "value")) {
  72. js_pushnumber(J, ud_value);
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. static int ud_put(js_State *J, void *p, const char *name)
  78. {
  79. (void)p;
  80. if (!strcmp(name, "value")) {
  81. ud_value = js_tonumber(J, -1);
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. int main(void)
  87. {
  88. js_State *J = js_newstate(0, 0, 0);
  89. js_setreport(J, report);
  90. js_newcfunction(J, c_checked, "checked", 1);
  91. js_defglobal(J, "checked", JS_DONTENUM);
  92. js_newcfunction(J, c_make, "Make", 1);
  93. js_defglobal(J, "Make", JS_DONTENUM);
  94. js_newcfunctionx(J, c_data, "data", 0, (void *)42, 0);
  95. js_defglobal(J, "data", JS_DONTENUM);
  96. js_pushnull(J);
  97. js_newuserdatax(J, "thing", (void *)"widget", ud_has, ud_put, 0, 0);
  98. js_defglobal(J, "thing", JS_DONTENUM);
  99. /* modern syntax */
  100. expect(J, "const a = [1, 2, 3]; return a.map(x => x * 2).join(',');", "2,4,6");
  101. expect(J, "let s = 0; for (const v of [1, 2, 3]) s += v; return s;", "6");
  102. expect(J, "const n = 5; return `n=${n + 1}`;", "n=6");
  103. expect(J, "class A { constructor(x) { this.x = x; } get d() { return this.x * 2; } }"
  104. " class B extends A {} return new B(4).d;", "8");
  105. expect(J, "const {p, q = 3} = {p: 1}; const [x, ...r] = [7, 8, 9];"
  106. " return [p, q, x, r.length].join(',');", "1,3,7,2");
  107. expect(J, "const o = {a: 1, ...{b: 2}}; return Object.entries(o).flat().join('');", "a1b2");
  108. expect(J, "return [1, 2, 3].includes(2) && 'abc'.startsWith('ab') &&"
  109. " Array.from(new Set([1, 1, 2])).length === 2;", "true");
  110. expect(J, "const m = new Map([[1, 'x']]); return m.get(1) + (null ?? 'd') + (undefined?.q ?? 'e');", "xde");
  111. expect(J, "let out = ''; Promise.resolve(3).then(v => { globalThis.__p = v; });"
  112. " return 'sync';", "sync");
  113. expect(J, "return String(globalThis.__p);", "3");
  114. expect(J, "async function f() { await null; return 7; } f().then(v => globalThis.__q = v); return 0;", "0");
  115. expect(J, "return String(globalThis.__q);", "7");
  116. expect(J, "return 2n ** 64n + 1n;", "18446744073709551617");
  117. expect(J, "return /(?<y>\\d{4})/.exec('in 2026').groups.y;", "2026");
  118. /* C functions: arguments, return value, function data, throws */
  119. expect(J, "return checked(21);", "42");
  120. expect(J, "try { checked(-1); return 'no'; } catch (e) {"
  121. " return e instanceof TypeError ? e.message : 'wrong'; }", "negative -1");
  122. expect(J, "return data();", "42");
  123. expect(J, "return new Make('click').type;", "click");
  124. /* userdata hooks and expandos */
  125. expect(J, "return thing.kind;", "widget");
  126. expect(J, "thing.value = 9; return thing.value * 2;", "18");
  127. expect(J, "thing.extra = {k: 1}; return thing.extra.k + ('extra' in thing ? 1 : 0);", "2");
  128. expect(J, "return Object.keys(thing).join(',');", "extra");
  129. expect(J, "delete thing.extra; return typeof thing.extra;", "undefined");
  130. /* registry refs */
  131. js_pushstring(J, "kept");
  132. {
  133. char ref[32];
  134. snprintf(ref, sizeof(ref), "%s", js_ref(J));
  135. js_getregistry(J, ref);
  136. if (strcmp(js_tostring(J, -1), "kept")) {
  137. printf("FAIL registry\n");
  138. fails++;
  139. }
  140. js_pop(J, 1);
  141. js_unref(J, ref);
  142. }
  143. /* the wall-clock budget turns a runaway loop into an exception */
  144. js_setlimit(J, 200, 0);
  145. if (js_ploadstring(J, "[loop]", "for (;;) {}") == 0) {
  146. js_pushundefined(J);
  147. if (!js_pcall(J, 0)) {
  148. printf("FAIL loop was not interrupted\n");
  149. fails++;
  150. }
  151. js_pop(J, 1);
  152. }
  153. js_setlimit(J, 0, 0);
  154. expect(J, "return 'alive';", "alive");
  155. if (js_gettop(J) != 0) {
  156. printf("FAIL stack not balanced: %d\n", js_gettop(J));
  157. fails++;
  158. }
  159. js_freestate(J);
  160. printf("js test: %s (%d failure(s))\n", fails ? "FAIL" : "OK", fails);
  161. return fails != 0;
  162. }