2017-07-12 2 views
0

Ich versuche, JXCore zu verwenden, um einige node.js Code in meinem C++ - Programm aufzurufen. Ich bin in der Lage, meine C++ - Programm mit dem beigefügten Code auszuführen, wenn ich die Zeile auskommentieren: "var path = require('fs'); \n" Also habe ich mein Problem im Grunde eingegrenzt, wenn ich den Befehl "require" verwende. Der Code funktioniert, wenn ich ihn direkt von jx aus starte. Ich benutze auch Visual Studio, wenn das einen Unterschied macht. Wie sagst du jx wo die zusätzlichen Module wie 'fs' sind?Wie zu verwenden "require" in JXCore C++ - Anwendung

#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(); 
} 

Antwort

Verwandte Themen