Commit 380f196d authored by jan.koester's avatar jan.koester
Browse files

test

parent f0110058
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -50,8 +50,9 @@ static void signalHandler(int sig) {
}
#endif
int main(int argc, char *argv[]) {
    if (argc == 4 && std::string(argv[1]) == "--node-bridge-run") {
        return blogi::webedit::nodebridge::runNodeRunnerInProcess(argv[2], argv[3]);
    if (int bridgeRc = blogi::webedit::nodebridge::runNodeRunnerFromArgs(argc, argv);
        bridgeRc >= 0) {
        return bridgeRc;
    }

    cmdplus::CmdController cmdCtl(cmdplus::CmdController::getInstance());
+74 −17
Original line number Diff line number Diff line
#pragma once

#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
@@ -21,13 +21,7 @@

namespace blogi::webedit::nodebridge {

inline std::string getEnvOrDefault(const char *key, const char *fallback) {
    const char *v = std::getenv(key);
    if (v && v[0] != '\0') {
        return std::string(v);
    }
    return std::string(fallback);
}
inline constexpr const char *kBridgeRunnerArg = "--node-bridge-run";

inline std::string getSelfExecutablePath() {
#ifdef _WIN32
@@ -100,18 +94,81 @@ inline int runNodeRunnerInProcess(const std::string &reqPath,

    std::vector<std::string> args;
    args.emplace_back("blogi-editor-node");
    args.emplace_back("-e");
    args.emplace_back(bridgeScript);
    args.emplace_back(reqPath);
    args.emplace_back(resPath);

    std::vector<char*> argv;
    argv.reserve(args.size());
    for (auto &arg : args) {
        argv.push_back(arg.data());
    bool initialized = false;
    auto teardown = [&initialized]() {
        if (initialized) {
            node::TearDownOncePerProcess();
            initialized = false;
        }
    };

    auto init = node::InitializeOncePerProcess(args);
    if (!init) {
        std::cerr << "Node bridge init failed: InitializeOncePerProcess returned null" << std::endl;
        return 1;
    }
    initialized = true;

    int rc = init->exit_code();
    if (!init->errors().empty()) {
        for (const auto &err : init->errors()) {
            std::cerr << "Node bridge init warning/error: " << err << std::endl;
        }
    }
    if (init->early_return()) {
        std::cerr << "Node bridge init aborted early with exit code " << rc << std::endl;
        teardown();
        return rc;
    }

    std::vector<std::string> envErrors;
    auto setup = node::CommonEnvironmentSetup::Create(
        init->platform(),
        &envErrors,
        init->args(),
        init->exec_args());
    if (!envErrors.empty()) {
        for (const auto &err : envErrors) {
            std::cerr << "Node bridge environment error: " << err << std::endl;
        }
    }
    if (!setup) {
        std::cerr << "Node bridge setup failed: CommonEnvironmentSetup::Create returned null" << std::endl;
        teardown();
        return 1;
    }

    if (node::LoadEnvironment(setup->env(), bridgeScript).IsEmpty()) {
        std::cerr << "Node bridge runtime error: LoadEnvironment returned empty result" << std::endl;
        teardown();
        return 1;
    }

    return node::Start(static_cast<int>(argv.size()), argv.data());
    auto loopResult = node::SpinEventLoop(setup->env());
    if (loopResult.IsNothing()) {
        std::cerr << "Node bridge runtime error: SpinEventLoop returned no exit code" << std::endl;
        teardown();
        return 1;
    }

    rc = loopResult.FromJust();
    setup.reset();
    teardown();
    return rc;
}

inline bool isBridgeRunnerInvocation(int argc, char *argv[]) {
    return argc == 4 && argv != nullptr && argv[1] != nullptr && std::strcmp(argv[1], kBridgeRunnerArg) == 0;
}

inline int runNodeRunnerFromArgs(int argc, char *argv[]) {
    if (!isBridgeRunnerInvocation(argc, argv)) {
        return -1;
    }
    return runNodeRunnerInProcess(argv[2], argv[3]);
}

inline int runNodeRunner(const std::string &reqPath,
@@ -122,7 +179,7 @@ inline int runNodeRunner(const std::string &reqPath,
    }

#ifdef _WIN32
    std::string cmdLine = "\"" + selfPath + "\" --node-bridge-run \"" + reqPath + "\" \"" + resPath + "\"";
    std::string cmdLine = "\"" + selfPath + "\" " + std::string(kBridgeRunnerArg) + " \"" + reqPath + "\" \"" + resPath + "\"";
    std::vector<char> mutableCmd(cmdLine.begin(), cmdLine.end());
    mutableCmd.push_back('\0');

@@ -162,7 +219,7 @@ inline int runNodeRunner(const std::string &reqPath,
    if (pid == 0) {
        execl(selfPath.c_str(),
              selfPath.c_str(),
              "--node-bridge-run",
              kBridgeRunnerArg,
              reqPath.c_str(),
              resPath.c_str(),
              static_cast<char *>(nullptr));