// Copyright (c) 2015 Tzvetan Mikov.
// Licensed under the Apache License v2.0. See LICENSE in the project
// root for complete license information.


exports.exit = function exit (code)
{
    __asm__({},[],[["code", code | 0]],[],
        "::exit((int)%[code].raw.nval);"
    );
};

@@ -276,8 +276,1200 @@
   );%0A%7D;%0A
+%0Afunction strerror (errno)%0A%7B%0A    return __asm__(%7B%7D,%5B%22res%22%5D, %5B%5B%22errno%22, errno%7C0%5D%5D, %5B%5D,%0A        %22%25%5Bres%5D = js::makeStringValueFromASCII(%25%5B%25frame%5D, ::strerror((int)%25%5Berrno%5D.raw.nval));%22%0A    );%0A%7D%0A%0Afunction throwIOError (errno, path, syscall)%0A%7B%0A    var msg = strerror(errno);%0A    if (path)%0A        msg = msg + %22 '%22 + path + %22'%22;%0A    var e = new Error(msg);%0A    e.errno = errno;%0A    //TODO: e.code = %22???%22 // and also append it to the message%0A    if (path !== undefined)%0A        e.path = path;%0A    if (syscall !== undefined)%0A        e.syscall = syscall;%0A%0A    throw e;%0A%7D%0A%0A__asmh__(%7B%7D,%22#include %3Cunistd.h%3E%22);%0A__asmh__(%7B%7D,%22#include %3Cerrno.h%3E%22);%0A%0Aexports.cwd = function cwd ()%0A%7B%0A    var errno = 0;%0A    var res = __asm__(%7B%7D,%5B%22res%22%5D,%5B%5B%22errno%22, errno%5D%5D,%5B%5D,%0A        %22char * s = ::getcwd(NULL, 0);%5Cn%22 +%0A        %22if (s) %7B%5Cn%22 +%0A        %22  %25%5Berrno%5D = js::makeNumberValue(0);%5Cn%22 +%0A        %22  %25%5Bres%5D = js::makeStringValueFromUnvalidated(%25%5B%25frame%5D, s);%5Cn%22 +%0A        %22  ::free(s);%5Cn%22 +%0A        %22%7D else %7B%5Cn%22 +%0A        %22  %25%5Berrno%5D = js::makeNumberValue(errno);%5Cn%22 +%0A        %22  %25%5Bres%5D = JS_NULL_VALUE;%5Cn%22 +%0A        %22%7D%22%0A    );%0A%0A    if (res === null)%0A        throwIOError(errno, undefined, %22getcwd%22);%0A    return res;%0A%7D;%0A

implement process.cwd()