| 29 | } |
| 30 | |
| 31 | int main(int argc, const char** argv) { |
| 32 | if (argc <= 1) { |
| 33 | printf("Should specify the list of modules to load en evaluate\n"); |
| 34 | return EXIT_FAILURE; |
| 35 | } |
| 36 | |
| 37 | auto context = tsn_init(argc, argv); |
| 38 | if (context == nullptr) { |
| 39 | std::cout << "Failed to init tsn" << std::endl; |
| 40 | return EXIT_FAILURE; |
| 41 | } |
| 42 | |
| 43 | auto global = JS_GetGlobalObject(context); |
| 44 | |
| 45 | // Create global var scriptArgs for command line arguments |
| 46 | auto scriptArgs = JS_NewArray(context); |
| 47 | JS_SetPropertyStr(context, global, "scriptArgs", scriptArgs); |
| 48 | |
| 49 | for (int i = 1; i < argc; i++) { |
| 50 | // Parse command line |
| 51 | auto command = split(std::string(argv[i]), ' '); |
| 52 | const auto& moduleName = command[0]; |
| 53 | // Add script name to scriptArgs[0] |
| 54 | auto argObj = JS_NewString(context, moduleName.data()); |
| 55 | JS_SetPropertyUint32(context, scriptArgs, 0, argObj); |
| 56 | // Add arguments |
| 57 | for (size_t j = 1; j < command.size(); ++j) { |
| 58 | auto argObj = JS_NewString(context, command[j].data()); |
| 59 | JS_SetPropertyUint32(context, scriptArgs, j, argObj); |
| 60 | } |
| 61 | |
| 62 | auto path = Path(std::string_view(moduleName.data())); |
| 63 | |
| 64 | auto moduleNameWithoutExtension = Path(path).removeFileExtension().toString(); |
| 65 | |
| 66 | if (!tsn_contains_module(moduleNameWithoutExtension.c_str())) { |
| 67 | // Interpreter mode |
| 68 | VALDI_INFO(ConsoleLogger::getLogger(), "Evaluating module '{}' as interpreted", path.toString()); |
| 69 | |
| 70 | auto loadResult = DiskUtils::load(path); |
| 71 | if (!loadResult) { |
| 72 | VALDI_ERROR(ConsoleLogger::getLogger(), "Failed to load module: {}", loadResult.error()); |
| 73 | tsn_fini(&context); |
| 74 | return EXIT_FAILURE; |
| 75 | } |
| 76 | |
| 77 | auto moduleData = loadResult.moveValue(); |
| 78 | |
| 79 | auto script = std::string(reinterpret_cast<const char*>(moduleData.data()), moduleData.size()); |
| 80 | auto retval = tsn_eval(context, script.data(), script.size(), moduleName.data(), 0); |
| 81 | if (tsn_is_exception(context, retval)) { |
| 82 | std::cout << "Failed to load module" << std::endl; |
| 83 | tsn_dump_error(context); |
| 84 | tsn_fini(&context); |
| 85 | return EXIT_FAILURE; |
| 86 | } |
| 87 | |
| 88 | tsn_release(context, retval); |
nothing calls this directly
no test coverage detected