如何在 JXCore C++应用程序中使用"require"

How to use "require" in JXCore C++ application

本文关键字:require 应用程序 JXCore C++      更新时间:2023-10-16

我正在尝试使用JXCore来调用我的c ++程序中的一些节点.js代码。 如果我注释掉以下行,我可以使用附加的代码运行我的C++程序:"var path = require('fs'); n"所以我基本上将我的问题缩小到何时使用"require"命令。 如果我直接从 jx 运行它,代码就可以工作。 我也在使用Visual Studio,如果这有所作为的话。你如何告诉 jx 像"fs"这样的附加模块在哪里?

#include "stdafx.h"
#include <windows.h>
using namespace std;
#include "jx.h"
#include <node.h>
#include <v8.h>
#define flush_console(...)        
do {                            
fprintf(stdout, __VA_ARGS__); 
fflush(stdout);               
} while (0)
void ConvertResult(JXValue *result, std::string &to_result) {
switch (result->type_) {
case RT_Null:
to_result = "null";
break;
case RT_Undefined:
to_result = "undefined";
break;
case RT_Boolean:
to_result = JX_GetBoolean(result) ? "true" : "false";
break;
case RT_Int32: {
std::stringstream ss;
ss << JX_GetInt32(result);
to_result = ss.str();
} break;
case RT_Double: {
std::stringstream ss;
ss << JX_GetDouble(result);
to_result = ss.str();
} break;
case RT_Buffer: {
to_result = JX_GetString(result);
} break;
case RT_JSON:
case RT_String: {
to_result = JX_GetString(result);
} break;
case RT_Error: {
to_result = JX_GetString(result);
} break;
default:
to_result = "null";
return;
}
}
void callback(JXResult *results, int argc) {
// do nothing
}
void sampleMethod(JXResult *results, int argc) {
flush_console("sampleMethod Called;n");
std::stringstream ss_result;
for (int i = 0; i < argc; i++) {
std::string str_result;
ConvertResult(&results[i], str_result);
ss_result << i << " : ";
ss_result << str_result << "n";
}
flush_console("%s", ss_result.str().c_str());
// return an Array back to JS Land
const char *str = "[1, 2, 3]";
// results[argc] corresponds to return value
JX_SetJSON(&results[argc], str, strlen(str));
}
void TEST_JSON(char * logger, int log_length, char *path)
{
JXValue result;

JX_Evaluate(
"var path = require('fs'); n"
"var FileParser = require('./FileParser'); n"
"var eventDate; n"
"var depth; n"
"var moisture; n"
"var pressure; n"
"var code; n"
"var message; n"
"var token; n"
"var results; n"
"var action; n"
"try { n"
"console.log(); n"
"console.log('//--------------------------------------------------------'); n"
"console.log('//    Hello'); n"
"console.log();  n"
"} catch (err) { n"
"} n", "myscript", &result);
JX_Free(&result);
// loop for possible IO
// or JX_Loop() without usleep / while

//return 0;
}
void startNodeInitialize(char *path)
{
//char *path = args[0];
// Call JX_Initialize only once per app
JX_Initialize(path, callback);
// Creates a new engine for the current thread
// It's our first engine instance hence it will be the
// parent engine for all the other engine instances.
// If you need to destroy this engine instance, you should
// destroy everything else first. For the sake of this sample
// we have our first instance sitting on the main thread
// and it will be destroyed when the app exists.
JX_InitializeNewEngine();
char *contents = "console.log('hi world');";
// define the entry file contents
JX_DefineMainFile(contents);
// define native -named- method
// we will be reaching to this method from the javascript side like this;
// process.natives.sampleMethod( ... )
JX_DefineExtension("sampleMethod", sampleMethod);
JX_StartEngine();
}
void startNodeEngine(void)
{
// Creates a new engine for the current thread
// It's our first engine instance hence it will be the
// parent engine for all the other engine instances.
// If you need to destroy this engine instance, you should
// destroy everything else first. For the sake of this sample
// we have our first instance sitting on the main thread
// and it will be destroyed when the app exists.
JX_InitializeNewEngine();
char *contents = "console.log('hi world');";
// define the entry file contents
JX_DefineMainFile(contents);
// define native -named- method
// we will be reaching to this method from the javascript side like this;
// process.natives.sampleMethod( ... )
JX_DefineExtension("sampleMethod", sampleMethod);
JX_StartEngine();
}

在这里找到答案:https://github.com/jxcore/jxcore/blob/master/test/native-interface/test-require/test-posix.cpp#L9

基本上,"require"需要在全局中定义。