Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file exists function help #12186

Closed
WingDust opened this issue Sep 12, 2020 · 3 comments
Closed

file exists function help #12186

WingDust opened this issue Sep 12, 2020 · 3 comments
Labels

Comments

@WingDust
Copy link

emscripten version :2.0.1
I try to write a C++ function for validate file exists.
here is my code

#ifndef EM_PORT_API
# if defined(__EMSCRIPTEN__)
#   include <emscripten.h>
#   if defined(__cplusplus)
#       define EM_PORT_API(rettype) extern "C" rettype EMSCRIPTEN_KEEPALIVE
#   else
#       define EM_PORT_API(rettype) EMSCRIPTEN_KEEPALIVE
#   endif
# else
#   if defined(__cplusplus)
#       define EM_PORT_API(rettype) extern "C" rettype
#   else 
#       define EM_PORT_API(rettype) rettype
#   endif
# endif
#endif

#ifdef _WIN64
#include <io.h>
#else
#include <unistd.h>
#endif

EM_PORT_API(int) exists(const char *pathname)
{
#ifdef _WIN64
  return _access(pathname,0);
#else
  return access(pathname,F_OK);
#endif
}

Actually I write for run in Electron
but I don't how to use it after I compiled it get a.out.js a.out.wasm
Mostly I saw the example is for web or use node test.js
can help me ?
or this function can achieve another way

@curiousdannii
Copy link
Contributor

curiousdannii commented Sep 12, 2020

You probably should look at these pages of the docs, which will let you create a JS function which you can call from your C code. That JS function can then use the Node API to check if the file exists.

https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#calling-javascript-from-c-c

@WingDust
Copy link
Author

WingDust commented Sep 13, 2020

@curiousdannii
I rewrite C++ code

#include "/usr/lib/emscripten/system/include/emscripten.h"
#include <string>
using namespace std;

#ifndef EM_PORT_API
# if defined(__EMSCRIPTEN__)
#   include <emscripten.h>
#   if defined(__cplusplus)
#       define EM_PORT_API(rettype) extern "C" rettype EMSCRIPTEN_KEEPALIVE
#   else
#       define EM_PORT_API(rettype) EMSCRIPTEN_KEEPALIVE
#   endif
# else
#   if defined(__cplusplus)
#       define EM_PORT_API(rettype) extern "C" rettype
#   else 
#       define EM_PORT_API(rettype) rettype
#   endif
# endif
#endif



//EMSCRIPTEN_KEEPALIVE void hi(const char* p){
  EM_PORT_API(int) hi(const char* p){
  // string a = "function show(arg){console.log(arg);};";
     string b = p;
    // string c = "show(\"";
    // string d = "\");";
    //
    string test = "const fs = require('fs');";
    string a = "function show(arg){let check = fs.existsSync(arg";
    string d = ");if(check){return 1}else{return 0};};show(\"";
    string e = "\");";

    // string run = a+c+p+d;
    string run = test+a+d+b+e;
    int num = emscripten_run_script_int(run.c_str());
    return num;
}

compile em++ run.cpp -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall','cwrap']" -o run.js

and do test for node is correct
test.js:
run node test.js

var em_module = require('./run.js');
em_module['onRuntimeInitialized'] = onRuntimeInitialized;

//console.log(em_module.run());
//em_module._hi('qweqwe');

function onRuntimeInitialized(){
    var s = '/home/wingdust/Programming/Emcc/for.txt';
    let a = em_module.ccall('hi','number',['string'],[s])
    console.log(a)
  
}

Screenshot_20200913_092029

But I want use it for electron+typescript+vite ,problem show up
And I still try write run.d.ts for typescript according #10271
run.d.ts:

/// <reference types="emscripten"/>
interface em_moduel extends EmscriptenModule {
    ccall:typeof ccall;
    hi(arg:string):number;
}

And In Electron code
typescript:

/// <reference types="emscripten"/>
 let em_moudle = require("../cplusplus/run.js")
em_moudle['onRuntimeInitialized'] = onRuntimeInitialized

function  onRuntimeInitialized() {
  let a = '../assets/electron.png'
  let b = em_moudle.ccall('hi','number',['string'],[a])
  console.log(b);
}

let em_moudle = require("../cplusplus/run.js") it's error
image

It' has any experience to how to use wasm file for Electron

@stale
Copy link

stale bot commented Sep 21, 2021

This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.

@stale stale bot added the wontfix label Sep 21, 2021
@stale stale bot closed this as completed Apr 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants