c++node.jsjxcore

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


I'm attempting to use JXCore in order to call up some node.js code inside my c++ program. I'm able to run my C++ program with the attached code if I comment out the line: "var path = require('fs'); \n" So I've basically narrowed down my issue to when I use the "require" command. The code works if I run it from jx directly. I'm also using Visual Studio if that makes a difference. How do you tell jx where the additional modules like 'fs' are?

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

Solution

  • Found the answer on here: https://github.com/jxcore/jxcore/blob/master/test/native-interface/test-require/test-posix.cpp#L9

    Basically, 'require' needs to be defined in a global.