main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include "quickjs/quickjs.h"
  6. #include "quickjs/quickjs-libc.h"
  7. #ifdef _WIN32
  8. #include <Windows.h>
  9. #include <direct.h>
  10. static JSValue js_os_exec_win(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
  11. JSValue args = argv[0];
  12. JSValue val = JS_GetPropertyStr(ctx, args, "length");
  13. uint32_t exec_argc;
  14. JS_ToUint32(ctx, &exec_argc, val);
  15. char **exec_argv = js_mallocz(ctx, sizeof(exec_argv[0]) * (exec_argc + 1));
  16. for(int i = 0; i < exec_argc; i++) {
  17. val = JS_GetPropertyUint32(ctx, args, i);
  18. exec_argv[i] = JS_ToCString(ctx, val);
  19. JS_FreeValue(ctx, val);
  20. }
  21. exec_argv[exec_argc] = NULL;
  22. if (argc >= 2) {
  23. JSValue options = argv[1];
  24. val = JS_GetPropertyStr(ctx, options, "cwd");
  25. if (!JS_IsUndefined(val)) {
  26. char *cwd = JS_ToCString(ctx, val);
  27. JS_FreeValue(ctx, val);
  28. _chdir(cwd);
  29. }
  30. }
  31. char cmd[1024];
  32. cmd[0] = 0;
  33. for (int i = 0; i < exec_argc; ++i) {
  34. strcat(cmd, exec_argv[i]);
  35. strcat(cmd, " ");
  36. }
  37. STARTUPINFO si;
  38. PROCESS_INFORMATION pi;
  39. ZeroMemory(&si, sizeof(si));
  40. si.cb = sizeof(si);
  41. ZeroMemory(&pi, sizeof(pi));
  42. CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
  43. WaitForSingleObject(pi.hProcess, INFINITE);
  44. CloseHandle(pi.hProcess);
  45. CloseHandle(pi.hThread);
  46. return JS_UNDEFINED;
  47. }
  48. void hlslbin(const char *from, const char *to);
  49. JSValue js_hlslbin(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
  50. const char *from = JS_ToCString(ctx, argv[0]);
  51. const char *to = JS_ToCString(ctx, argv[1]);
  52. hlslbin(from , to);
  53. return JS_UNDEFINED;
  54. }
  55. #endif
  56. void alang(char *source, char *output);
  57. static JSValue js_alang(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
  58. const char *source = JS_ToCString(ctx, argv[0]);
  59. const char *output = JS_ToCString(ctx, argv[1]);
  60. alang(source, output);
  61. return JS_UNDEFINED;
  62. }
  63. int ashader(char *shader_lang, char *from, char *to);
  64. static JSValue js_ashader(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
  65. const char *shader_lang = JS_ToCString(ctx, argv[0]);
  66. const char *from = JS_ToCString(ctx, argv[1]);
  67. const char *to = JS_ToCString(ctx, argv[2]);
  68. ashader(shader_lang, from, to);
  69. return JS_UNDEFINED;
  70. }
  71. void export_k(const char *from, const char *to);
  72. JSValue js_export_k(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
  73. const char *from = JS_ToCString(ctx, argv[0]);
  74. const char *to = JS_ToCString(ctx, argv[1]);
  75. export_k(from, to);
  76. return JS_UNDEFINED;
  77. }
  78. void export_ico(const char *from, const char *to);
  79. JSValue js_export_ico(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
  80. const char *from = JS_ToCString(ctx, argv[0]);
  81. const char *to = JS_ToCString(ctx, argv[1]);
  82. export_ico(from, to);
  83. return JS_UNDEFINED;
  84. }
  85. void export_png(const char *from, const char *to, int width, int height);
  86. JSValue js_export_png(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
  87. const char *from = JS_ToCString(ctx, argv[0]);
  88. const char *to = JS_ToCString(ctx, argv[1]);
  89. int32_t width;
  90. JS_ToInt32(ctx, &width, argv[2]);
  91. int32_t height;
  92. JS_ToInt32(ctx, &height, argv[3]);
  93. export_png(from, to, width, height);
  94. return JS_UNDEFINED;
  95. }
  96. int main(int argc, char **argv) {
  97. FILE *fp = fopen(argv[1], "rb");
  98. fseek(fp , 0, SEEK_END);
  99. int size = ftell(fp);
  100. rewind(fp);
  101. char *buffer = malloc(size + 1);
  102. buffer[size] = 0;
  103. fread(buffer, size, 1, fp);
  104. fclose(fp);
  105. JSRuntime *runtime = JS_NewRuntime();
  106. JSContext *ctx = JS_NewContext(runtime);
  107. js_std_add_helpers(ctx, argc, argv);
  108. js_init_module_std(ctx, "std");
  109. js_init_module_os(ctx, "os");
  110. JSValue global_obj = JS_GetGlobalObject(ctx);
  111. JSValue amake = JS_NewObject(ctx);
  112. JS_SetPropertyStr(ctx, amake, "export_k", JS_NewCFunction(ctx, js_export_k, "export_k", 2));
  113. JS_SetPropertyStr(ctx, amake, "export_ico", JS_NewCFunction(ctx, js_export_ico, "export_ico", 2));
  114. JS_SetPropertyStr(ctx, amake, "export_png", JS_NewCFunction(ctx, js_export_png, "export_png", 4));
  115. #ifdef _WIN32
  116. JS_SetPropertyStr(ctx, amake, "os_exec_win", JS_NewCFunction(ctx, js_os_exec_win, "os_exec_win", 1));
  117. JS_SetPropertyStr(ctx, amake, "hlslbin", JS_NewCFunction(ctx, js_hlslbin, "hlslbin", 1));
  118. #endif
  119. JS_SetPropertyStr(ctx, amake, "alang", JS_NewCFunction(ctx, js_alang, "alang", 2));
  120. JS_SetPropertyStr(ctx, amake, "ashader", JS_NewCFunction(ctx, js_ashader, "ashader", 3));
  121. JS_SetPropertyStr(ctx, global_obj, "amake", amake);
  122. JS_FreeValue(ctx, global_obj);
  123. JSValue ret = JS_Eval(ctx, buffer, size, "make.js", JS_EVAL_TYPE_MODULE);
  124. if (JS_IsException(ret)) {
  125. js_std_dump_error(ctx);
  126. JS_ResetUncatchableError(ctx);
  127. }
  128. JS_RunGC(runtime);
  129. free(buffer);
  130. return 0;
  131. }