Skip to content

Commit

Permalink
Add LibUV for Node env
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-thierry committed Jun 8, 2020
1 parent ab3fe50 commit 270d651
Show file tree
Hide file tree
Showing 111 changed files with 47,147 additions and 12 deletions.
14 changes: 10 additions & 4 deletions compiler/native/env/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
var NODE =
{
name: "node",
main: "std.cpp",
main: "node.cpp",
compiler: "g++",
stdlib: ["console", "Object", "Math", "JSON"],
check: {
Expand Down Expand Up @@ -83,24 +83,30 @@ var NODE =
else if(preset == "speed") return `${compiler} ${_stack} ${_in} /std:c++14 /D CL_WINDOWS=1 /Ox /I "${CONFIG.win_inc_ucrt}" "${CONFIG.win_lib_um}\\Uuid.Lib" "${CONFIG.win_lib_um}\\kernel32.Lib" "${CONFIG.win_lib_ucrt}\\libucrt.lib" /EHsc ${COMPILER.LIBS} /o ${out}`;
}

var _uvParam = "";
if(os.platform() == "win32") _uvParam = `-D_WIN32_WINNT=0x0600 -Wno-narrowing -D_GNU_SOURCE -I${extern}/libuv/include/ -Iextern/libuv/src/ -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE ${extern}/libuv/include/uv-win.h ${extern}/libuv/src/win/async.c ${extern}/libuv/src/win/atomicops-inl.h ${extern}/libuv/src/win/core.c ${extern}/libuv/src/win/dl.c ${extern}/libuv/src/win/error.c ${extern}/libuv/src/win/fs.c ${extern}/libuv/src/win/fs-event.c ${extern}/libuv/src/win/getaddrinfo.c ${extern}/libuv/src/win/getnameinfo.c ${extern}/libuv/src/win/handle.c ${extern}/libuv/src/win/handle-inl.h ${extern}/libuv/src/win/internal.h ${extern}/libuv/src/win/loop-watcher.c ${extern}/libuv/src/win/pipe.c ${extern}/libuv/src/win/thread.c ${extern}/libuv/src/win/poll.c ${extern}/libuv/src/win/process.c ${extern}/libuv/src/win/process-stdio.c ${extern}/libuv/src/win/req.c ${extern}/libuv/src/win/req-inl.h ${extern}/libuv/src/win/signal.c ${extern}/libuv/src/win/snprintf.c ${extern}/libuv/src/win/stream.c ${extern}/libuv/src/win/stream-inl.h ${extern}/libuv/src/win/tcp.c ${extern}/libuv/src/win/tty.c ${extern}/libuv/src/win/timer.c ${extern}/libuv/src/win/udp.c ${extern}/libuv/src/win/util.c ${extern}/libuv/src/win/winapi.c ${extern}/libuv/src/win/winapi.h ${extern}/libuv/src/win/winsock.c ${extern}/libuv/src/win/winsock.h ${extern}/libuv/src/*.h ${extern}/libuv/src/*.c`;

var _uvLib = "";
if(os.platform() == "win32") _uvLib = "-lm -ladvapi32 -liphlpapi -lpsapi -lshell32 -luser32 -luserenv -lwsock32 -lws2_32";

if(_stack) _stack = "-Wl,--stack," + _stack;
else _stack = "";

if(preset == "none")
{
return `${compiler} ${_stack} -std=c++11 ${_in} -O1 -fpermissive -w -s ${COMPILER.LIBS} -o ${out}`;
return `${compiler} ${_stack} -std=c++11 ${_uvParam} ${_in} -O1 -fpermissive -w -s ${COMPILER.LIBS} ${_uvLib} && mv a.exe ${out}`;
}
else if(preset == "size")
{
return `${compiler} ${_stack} -std=c++11 ${_in} -Os -fno-exceptions -fno-rtti -fno-stack-protector -fomit-frame-pointer -fpermissive -w -s ${COMPILER.LIBS} -o ${out}`;
return `${compiler} ${_stack} -std=c++11 ${_uvParam} ${_in} -Os -fno-exceptions -fno-rtti -fno-stack-protector -fomit-frame-pointer -fpermissive -w -s ${COMPILER.LIBS} ${_uvLib} && mv a.exe ${out}`;
}
else
{
var _opt = "-O";
if(os.platform() == "darwin" || compiler.indexOf("clang") > -1) _opt += "3";
else _opt += "fast";

return `${compiler} ${_stack} -std=c++11 ${_in} ${_opt} -fpermissive -w -s ${COMPILER.LIBS} -o ${out}`;
return `${compiler} ${_stack} -std=c++11 ${_uvParam} ${_in} ${_opt} -fpermissive -w -s ${COMPILER.LIBS} ${_uvLib} && mv a.exe ${out}`;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/native/squel/android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
using namespace std;

#include "njs.h"
using namespace NECTAR;

JNIEnv* globalEnv;
jobject mainObject;
Expand Down
1 change: 1 addition & 0 deletions compiler/native/squel/arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#define __NJS_ARDUINO 1

#include "njs.h"
using namespace NECTAR;

// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }
Expand Down
73 changes: 73 additions & 0 deletions compiler/native/squel/node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This file is part of NectarJS
* Copyright (c) 2017-2020 Adrien THIERRY
* http://nectarjs.com - https://www.linkedin.com/in/adrien-thierry-fr/
*
* sources : https://github.com/nectarjs/nectarjs/
*
* this program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial activities involving the NectarJS software without
* disclosing the source code of your own applications. Visit http://seraum.com/
* and feel free to contact us.
*
*/

#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include <vector>
#include <memory>
#include "uv.h"

using namespace std;


#include "njs.h"
using namespace NECTAR;

{INCLUDE}

{DECL}

int main(int argc, char* argv[])
{
var __NJS_ARGS = __NJS_Create_Array();
for( int i = 0; i < argc; i++)
{
__NJS_Object_Set(i, __NJS_VAR(argv[i]), __NJS_ARGS);
}

{INIT}

uv_loop_t *loop = (uv_loop_t *)malloc(sizeof(uv_loop_t));
uv_loop_init(loop);

{CODE};

printf("Exiting.\n");
uv_run(loop, UV_RUN_DEFAULT);

uv_loop_close(loop);
free(loop);
return 0;

return 0;
}
2 changes: 2 additions & 0 deletions compiler/native/squel/std.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@

using namespace std;


#include "njs.h"
using namespace NECTAR;

{INCLUDE}

Expand Down
1 change: 1 addition & 0 deletions compiler/native/squel/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
using namespace std;

#include "njs.h"
using namespace NECTAR;

{INCLUDE}

Expand Down
28 changes: 22 additions & 6 deletions compiler/native/src/njs.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ enum __NJS_TYPE
#define __NJS_GET_STRING(_var) _var.get().s->__NJS_VALUE.c_str()
#define var __NJS_VAR
#define let __NJS_VAR
#define undefined __NJS_VAR()
#define __NJS_Create_Boolean(_value) __NJS_VAR(__NJS_BOOLEAN, _value)
#define __NJS_Create_Number(_value) __NJS_VAR( _value)
#define __NJS_Create_Function(_value) __NJS_VAR(__NJS_FUNCTION, _value)
Expand All @@ -56,10 +55,13 @@ enum __NJS_TYPE
#define __NJS_Create_Infinity() __NJS_VAR(__NJS_INFINITY, 0)
#define Infinity __NJS_Create_Infinity()
#define __NJS_Create_Null() __NJS_VAR(__NJS_NULL, 0)
#define null __NJS_Create_Null()
//#define __NJS_Init_Null __NJS_VAR(__NJS_NULL, 0)
//#define null __NJS_Create_Null()
#define __NJS_Create_Lambda(name) function<__NJS_VAR (vector<var>)>* name = new function<__NJS_VAR (vector<var>)>([](vector<var> __NJS_VARARGS)
/*** END HELPERS ***/



struct __NJS_VAR;

class __NJS_Class_Object
Expand Down Expand Up @@ -113,7 +115,7 @@ class __NJS_Class_Native
vector<pair<char*, __NJS_VAR>> __OBJECT;
};

union val
union __NJS_VAL
{
int i;
double d;
Expand All @@ -130,9 +132,9 @@ union val
int FREE_PTR = -1;
int REGISTER_PTR = 0;
#ifdef CL_WINDOWS
val REGISTER[{{REGISTER}}];
__NJS_VAL REGISTER[{{REGISTER}}];
#else
val REGISTER[{{REGISTER}}]{(val){.i=0}};
__NJS_VAL REGISTER[{{REGISTER}}]{(__NJS_VAL){.i=0}};
#endif
int FREE[{{REGISTER}}] = {0};

Expand Down Expand Up @@ -218,7 +220,7 @@ struct __NJS_VAR
__NJS_TYPE type;
int _ptr = -1;

inline val get() const
inline __NJS_VAL get() const
{
return REGISTER[_ptr];
}
Expand Down Expand Up @@ -678,6 +680,13 @@ struct __NJS_VAR
}
};


namespace NECTAR
{
__NJS_VAR null = __NJS_Create_Null();
__NJS_VAR undefined = __NJS_VAR();
}

/*** VARIADIC CALLS ***/
template<class... Args>
__NJS_VAR __NJS_Back_Var_Call_Function(__NJS_VAR _obj, Args... args)
Expand Down Expand Up @@ -1268,10 +1277,17 @@ __NJS_VAR __NJS_Create_Native(void* _native)
return __NJS_VAR(__NJS_UNDEFINED, _native);
}

/*
void* __NJS_Get_Function(__NJS_VAR _fn)
{
return _fn.get().f;
}
*/

function<var (vector<var>)>* __NJS_Get_Function(__NJS_VAR _v)
{
return (function<var (vector<var>)>*)_v.get().f->__NJS_VALUE;
}

void* __NJS_Get_Native(__NJS_VAR _native)
{
Expand Down
12 changes: 12 additions & 0 deletions example/wasm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html>
<script>
var Module = {};

function WasmLoaded()
{
console.log(Module.callNectar("callFromJS", "Hello From NectarJS !"));
}
</script>
<script src="wasm_export.asm.js"></script>
</html>
15 changes: 15 additions & 0 deletions example/wasm_export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var WASM = require("WASM");

// function proto for binding : string(string)
function callFromJS(_data)
{
if(!_data) WASM.run("document.write('<h1>No Data</h1>');");
else WASM.run("document.write('<h1>" + _data + "</h1>');");
return "Executed from WASM";
}

// bind function to be executed in Browser env with Module.callNectar(_functionName, _data);
WASM.bind("callFromJS", callFromJS);

// Fire a JS function to know when env is set up, see wasm.html
WASM.run("WasmLoaded();");
54 changes: 54 additions & 0 deletions extern/libuv/include/android-ifaddrs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 1995, 1999
* Berkeley Software Design, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* BSDI ifaddrs.h,v 2.5 2000/02/23 14:51:59 dab Exp
*/

#ifndef _IFADDRS_H_
#define _IFADDRS_H_

struct ifaddrs {
struct ifaddrs *ifa_next;
char *ifa_name;
unsigned int ifa_flags;
struct sockaddr *ifa_addr;
struct sockaddr *ifa_netmask;
struct sockaddr *ifa_dstaddr;
void *ifa_data;
};

/*
* This may have been defined in <net/if.h>. Note that if <net/if.h> is
* to be included it must be included before this header file.
*/
#ifndef ifa_broadaddr
#define ifa_broadaddr ifa_dstaddr /* broadcast address interface */
#endif

#include <sys/cdefs.h>

__BEGIN_DECLS
extern int getifaddrs(struct ifaddrs **ifap);
extern void freeifaddrs(struct ifaddrs *ifa);
__END_DECLS

#endif
64 changes: 64 additions & 0 deletions extern/libuv/include/pthread-barrier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright (c) 2016, Kari Tristan Helgason <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#ifndef _UV_PTHREAD_BARRIER_
#define _UV_PTHREAD_BARRIER_
#include <errno.h>
#include <pthread.h>
#include <semaphore.h> /* sem_t */

#define PTHREAD_BARRIER_SERIAL_THREAD 0x12345

/*
* To maintain ABI compatibility with
* libuv v1.x struct is padded according
* to target platform
*/
#if defined(__ANDROID__)
# define UV_BARRIER_STRUCT_PADDING \
sizeof(pthread_mutex_t) + \
sizeof(pthread_cond_t) + \
sizeof(unsigned int) - \
sizeof(void *)
#elif defined(__APPLE__)
# define UV_BARRIER_STRUCT_PADDING \
sizeof(pthread_mutex_t) + \
2 * sizeof(sem_t) + \
2 * sizeof(unsigned int) - \
sizeof(void *)
#endif

typedef struct {
pthread_mutex_t mutex;
pthread_cond_t cond;
unsigned threshold;
unsigned in;
unsigned out;
} _uv_barrier;

typedef struct {
_uv_barrier* b;
char _pad[UV_BARRIER_STRUCT_PADDING];
} pthread_barrier_t;

int pthread_barrier_init(pthread_barrier_t* barrier,
const void* barrier_attr,
unsigned count);

int pthread_barrier_wait(pthread_barrier_t* barrier);
int pthread_barrier_destroy(pthread_barrier_t *barrier);

#endif /* _UV_PTHREAD_BARRIER_ */
Loading

0 comments on commit 270d651

Please sign in to comment.