Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. int __cdecl main(int argc, char** argv)
  2. {
  3. if (argc < 2) {
  4. printf("Usage: %s <input file>\n", argv[0]);
  5. return 0;
  6. }
  7.  
  8. FILE * file;
  9. JsRuntimeHandle runtime;
  10. JsContextRef context;
  11. JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &runtime);
  12. JsCreateContext(runtime, &context);
  13. JsSetCurrentContext(context);
  14.  
  15. JsValueRef WebAssembly, funcPropId, global, undefined, result;
  16. JsGetGlobalObject(&global);
  17. JsGetUndefinedValue(&undefined);
  18.  
  19. const char* funcString = "WebAssembly";
  20. JsCreatePropertyId(funcString, strlen(funcString), &funcPropId);
  21. JsGetProperty(global, funcPropId, &WebAssembly);
  22.  
  23. JsErrorCode errcode;
  24. JsValueRef compilefunc, compilefuncPropId;
  25. const char* compilefuncString = "compile";
  26. errcode = JsCreatePropertyId(compilefuncString, strlen(compilefuncString), &compilefuncPropId);
  27. errcode = JsGetProperty(WebAssembly, compilefuncPropId, &compilefunc);
  28.  
  29. JsValueRef ArrayBuffer;
  30. BYTE* ArrayBufferpointer=NULL;
  31. unsigned int ArrayBufferLen;
  32.  
  33. file=fopen(argv[1], "rb");
  34. if(file==NULL)
  35. {
  36. return 0;
  37. }
  38.  
  39. char *fileContent;
  40. LoadBinaryFile(file, fileContent, ArrayBufferLen);
  41. if(fileContent && ArrayBufferLen>10)
  42. {
  43. fileContent[0]=0x00;
  44. fileContent[0]=0x61;
  45. fileContent[0]=0x73;
  46. fileContent[0]=0x6d;
  47. }
  48.  
  49. /* FILE* wfile;
  50. char name[100] = { 0 };
  51. milliseconds ms = duration_cast< milliseconds >(
  52. system_clock::now().time_since_epoch()
  53. );*/
  54.  
  55. /* sprintf_s(name, "array%lld.wasm", ms.count());
  56. fopen_s(&wfile,name, "wb");
  57. fwrite(fileContent, sizeof(char), ArrayBufferLen, wfile);
  58. fclose(wfile);*/
  59.  
  60. JsCreateExternalArrayBuffer(fileContent,ArrayBufferLen,NULL,NULL,&ArrayBuffer);
  61.  
  62. JsValueRef args[] = { undefined,ArrayBuffer };
  63. errcode = JsCallFunction(compilefunc, args, 2, &result);
  64.  
  65. fclose(file);
  66. if (fileContent)
  67. {
  68. free(fileContent);
  69. fileContent = nullptr;
  70. }
  71. JsSetCurrentContext(JS_INVALID_REFERENCE);
  72. JsDisposeRuntime(runtime);
  73.  
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement