Skip to content

add functions <signal.h> , getenv/system (node.js)<stdlib> #3948

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

Closed
wants to merge 15 commits into from
Closed

add functions <signal.h> , getenv/system (node.js)<stdlib> #3948

wants to merge 15 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Dec 2, 2015


library.js , add system


i have updated in my branch library.js
to implement : system <stdlib.h>
it works on node.js
usage :

system ( "dir" ) ; 
system ( "node -v" ) ;
system ( "dir c:\\windows" );
system ( "em++ node001-getenv.cpp -o main.js" );

emscripten.h , add getenv


i have addedd getenv command <stlib.h>,,
it work on node.js
usage :

 char *s = getenv ( process.env.APPDATA );
 printf ( "\n NODE.JS <%s> " ,s  ) ; 
 printf ( "\n NODE.JS <%s> " , getenv ( process.env.HOMEDRIVE ) ) ;
 printf ( "\n NODE.JS <%s> " , getenv ( process.env ) ) ; 

emscrpiten.h, added pause


raise SIGSTOP and SIGCONT;
usage :
pause(1000);


emscripten.h, added MACRO


__SIGRTMIN
__SIGRTMAX

to implement other two function :

__libc_current_sigrtmax
__libc_current_sigrtmin


library.signal.js


addedd :

signal , raise , pause ,
sigprocmask , __libc_current_sigrtmax ,
__libc_current_sigrtmin


example


add example, for this command , in tests :

node001-getenv.cpp
node002-system.cpp
signal001-signal-raise.cpp
signal002-pause.cpp
signal003-sigprocmask.cpp

implemented getenv on node.js
check memory leaks :

	printf ( "\n NODE.JS <%s> " ,getenv ( process.env.APPDATA )  ) ;
implemented : system command, works on node.js :

usage :
	system ( "dir" ) ; 
	system ( "node -v" ) ;
	system ( "dir c:\\windows" );
	system ( "em++ node001-getenv.cpp -o main.js" );
added get env
added pause
added  __SIGRTMIN
added  __SIGRTMAX
add signal , raise , pause ,
sigprocmask,
__libc_current_sigrtmax
__libc_current_sigrtmin
create example for getenv node.js
add example to use system command
on node.js
show usage of signal raise
add usage for pause() command
usage sig proc mask
@ghost ghost mentioned this pull request Dec 2, 2015
// Can't call external programs.
___setErrNo(ERRNO_CODES.EAGAIN);
return -1;
system: function(command)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our convention has the { on the same line

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i can repost you a code align ,with emscripten guide lines
{ in the same lines,

apply { in the same lines and tab code with 2 spaces
@ghost
Copy link
Author

ghost commented Dec 3, 2015

i need you ask a question : about free() . I have seen that this function
is not implemented yet, but counterpart in library.js _free, works good .
are there issues to implement correctly ?

if environmet is node
@kripken
Copy link
Member

kripken commented Dec 14, 2015

free is implemented in system/lib/dlmalloc.c.

@ghost
Copy link
Author

ghost commented Dec 15, 2015

you're right
i have removed , getenv from emscipten.h

 remove getnev implementation on emscripten.h. file to avoid confusion.
@ghost
Copy link
Author

ghost commented Dec 16, 2015

yes, but if you try this code, free does not work :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <emscripten.h>

int main ( void )
{
    // ************************************** free do not free
    char *s1 = (char*) malloc ( 256 );
    strcpy ( s1,"Hello\0" ) ;
    puts (s1);
    free(s1);
    puts(s1);

    // ************************************** free do not free
    char *s2  = (char* )EM_ASM_INT ( {
        var p = Module._malloc(256);
        setValue ( p + 0 , 65 , 'i8' ) ; // A
        setValue ( p + 1 , 66 , 'i8' ) ; // B
        setValue ( p + 2 , 67 , 'i8' ) ; // C
        setValue ( p + 3 , 0  , 'i8' ) ;
        return p ;
        } , NULL );
    puts(s2);
    free(s2);   // do not free
    puts(s2);

    // ************************************** _free do free
    /*
    EM_ASM_INT ( {
    return _free ( $0  );
    }, s1 ) ;
    EM_ASM_INT ( {
    return _free ( $0  );
    }, s1 ) ;
    */
    puts(s1);
    puts(s2);


    char * s3 = (char*) EM_ASM_INT ( {
        var  str = 'ciao' ;
        var  ret  = allocate(intArrayFromString(str), 'i8', ALLOC_NORMAL);
        return  ret ;
    }, NULL  ) ;

    puts( s3 ) ;
    free(s3); // do not free
    puts( s3 ) ;

    // ************************************** _free do free
    /*
    EM_ASM_INT ( {
        return _free ( $0  );
    }, s3 ) ;
    */
    puts( s3 ) ;

    return 0 ;
}

@kripken
Copy link
Member

kripken commented Dec 21, 2015

I don't understand, what do you mean by "free does not free"?

@ghost
Copy link
Author

ghost commented Dec 21, 2015

the command ' free ' does not release the allocated memory

2015-12-21 20:22 GMT+01:00 Alon Zakai [email protected]:

I don't understand, what do you mean by "free does not free"?


Reply to this email directly or view it on GitHub
#3948 (comment).

@ghost
Copy link
Author

ghost commented Dec 24, 2015

Happy Christmas Kripken !

2015-12-21 20:55 GMT+01:00 claudio daffra [email protected]:

the command ' free ' does not release the allocated memory

2015-12-21 20:22 GMT+01:00 Alon Zakai [email protected]:

I don't understand, what do you mean by "free does not free"?


Reply to this email directly or view it on GitHub
#3948 (comment).

@kripken
Copy link
Member

kripken commented Jan 7, 2016

Sorry for the delay here.

I still don't follow. How do you know it isn't freeing the memory?

@ghost
Copy link
Author

ghost commented Jan 9, 2016

excuse me, for my bad english
i'll post an issue soon
with more clarifications
regards Claudio

2016-01-07 1:54 GMT+01:00 Alon Zakai [email protected]:

Sorry for the delay here.

I still don't follow. How do you know it isn't freeing the memory?


Reply to this email directly or view it on GitHub
#3948 (comment).

@kripken
Copy link
Member

kripken commented Nov 2, 2018

This PR is quite old, closing. Feel free to reopen if still relevant.

@kripken kripken closed this Nov 2, 2018
@IngwiePhoenix
Copy link

Pretty sure I'll be poking a dead issue here.

I am poking around with WebAssembly and I want to access variables from the real environment - well, not me specificaly, but SWIG would like to. I run it via Node.

Studying the settings.js file containing several of the -s options, I came up with this configuration command:

$ /opt/emsdk/upstream/emscripten/emconfigure ./configure \
  CC=/opt/emsdk/upstream/emscripten/emcc \
  CXX=/opt/emsdk/upstream/emscripten/em++ \
  --without-pcre \
  LDFLAGS="-s ASSERTIONS=1 -lnodefs.js -s EXIT_RUNTIME=1 -s ALLOW_MEMORY_GROWTH=1 -s ALLOW_TABLE_GROWTH=1 -s ENVIRONMENT=node -s NODERAWFS=1 -s SUPPORT_ERRNO=1"

So far, so good. Passing environment variables though does not work unfortunately. Has a streamlined method of using environment variables been introduced in the meantime? Thanks!

@sbc100
Copy link
Collaborator

sbc100 commented Dec 12, 2020

Emscripten seeds its environment from the ENV JavaScript variable. This works the same on the web as under node.

If you would like to node-specific behaviour to mirror the real environment, and that is genuinely useful, and I think we would accept such a patch. But these change of changes that are node-specific can also hurt portability so we should consider them carefully.

@IngwiePhoenix
Copy link

Why not go for an option like -s NODERAWFS=1 - but for the environment? This option already makes it very obvious that it is node-specific. Alternatively, would there be a way to "link" this behaviour in? I saw the pre-/post-js but don't have a full idea of how I could use those to essentially for(var k in process.env) { ENV[k] = process.env[k]; }.

@sbc100
Copy link
Collaborator

sbc100 commented Dec 12, 2020

Sure either of those approaches would work.

That for loop in pre-js should work yes.

@IngwiePhoenix
Copy link

Had no luck so far. But I did find this:

  function getEnvStrings() {
      if (!getEnvStrings.strings) {
        // Default values.
        // Browser language detection #8751
        var lang = ((typeof navigator === 'object' && navigator.languages && navigator.languages[0]) || 'C').replace('-', '_') + '.UTF-8';
        var env = {
          'USER': 'web_user',
          'LOGNAME': 'web_user',
          'PATH': '/',
          'PWD': '/',
          'HOME': '/home/web_user',
          'LANG': lang,
          '_': getExecutableName()
        };
        // Apply the user-provided values, if any.
        for (var x in ENV) {
          env[x] = ENV[x];
        }
        var strings = [];
        for (var x in env) {
          strings.push(x + '=' + env[x]);
        }
        getEnvStrings.strings = strings;
      }
      return getEnvStrings.strings;
    }

The way pre-js is positioned, there is no viable solution to modify getEnvStrings at all to just reference process.env. In fact, if this behaviour was changed to something like this:

if(ENVIRONMENT_IS_NODE) {
  getEnvStrings.strings = process.env;
} else { /* ...implement the "standard" env as seen above. */ }

then getenv and friends would happily read and respond with the actual system environment.

Looks like my best bet at this point is to look where this is generated and how I can change/update that and maybe make it a PR or something. But for now, I am a little lost as to how I can just add this feature without neccessarily modifying the Emscripten source.

@IngwiePhoenix
Copy link

Well, I did find a method. Hacky, but for now, works.

LDFLAGS appended: --pre-js (pwd)/env-init.js -s INCOMING_MODULE_JS_API="['preRun']"
env-init.js:

Module.preRun = function customPreRun() {
  ENV = Object.create(process.env);
}

This allows this program to display the proper PATH:

#include <stdio.h>
#include <stdlib.h>
int main() { puts(getenv("PATH")); }

Not elegant but working. Wish there was an easier way to get this working, considering it's literally a one-liner. XD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants