NerdLang / nerd

🔱 Javascript's God Mode. No VM. No Bytecode. No GC. Just native binaries.
https://nectarjs.com
MIT License
3.58k stars 127 forks source link

javascript file can not exists name is 'main' function #151

Closed fjqingyou closed 2 years ago

fjqingyou commented 2 years ago
root@HL:nodejs# cat /etc/banner
  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 OpenWrt 19.07-SNAPSHOT, r11413+27-1a922066d9
 -----------------------------------------------------
root@HL:nodejs# uname -a
Linux HL 4.14.275 #0 SMP Fri May 20 18:35:57 2022 mips GNU/Linux
root@HL:nodejs# node --version
v14.19.1
root@HL:nodejs# gcc --version
gcc (OpenWrt GCC 7.4.0) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
root@HL:nodejs# g++ --version
g++ (OpenWrt GCC 7.4.0) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
root@HL:nodejs# pwd
/mnt/mstat/sources/test/nodejs
root@HL:nodejs# cat helloworld.js 

function main(){
    console.log("hello world");
}

main();

root@HL:nodejs# nectar helloworld.js
[*] Generating source file
[*] Compiling with preset: speed
[+] Creating Nectar binary lib for std_linux_0.7.115
[+] Compiling with precompiled Nectar lib
Error: Command failed: g++  -std=c++17 "/mnt/mstat/sources/test/nodejs/.nectar/cached_std_linux_0.7.115/nectar.o" "/mnt/mstat/sources/test/nodejs/.nectar/86uzq/helloworld.cpp" -Ofast -lpthread -s   -o "/mnt/mstat/sources/test/nodejs/helloworld"  
/mnt/mstat/sources/test/nodejs/.nectar/86uzq/helloworld.cpp:43:482: error: cannot declare '::main' to be a global variable
  var console;;var performance;;var Error;;var RegExp;;var Number;;var Object;;var Math;;var JSON;;var Array;;var Date;;var __NJS_Create_Object;;var __MODULE_gchcs4u94z;;var __MODULE_hp57np9gna;;var _performance;;var __MODULE_uv9rxwa1pn;;var __MODULE_eqkxnwtuv2;;var _regexp;;var __MODULE_066oord9sv;;var _Number;;var __MODULE_7tr71cumo6;;var __MODULE_zn71zum893;;var __MODULE_vygvtmrx7l;;var __MODULE_w7j1wvcu68;;var __MODULE_c2fwz11rb4;;var _date;;var __e6xr2l;;var __ml80hn;;var main;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ^~~~
/mnt/mstat/sources/test/nodejs/.nectar/86uzq/helloworld.cpp: In function 'int main(int, char**)':
/mnt/mstat/sources/test/nodejs/.nectar/86uzq/helloworld.cpp:47:32: error: 'int main(int, char**)' redeclared as different kind of symbol
 int main(int argc, char* argv[])
                                ^
/mnt/mstat/sources/test/nodejs/.nectar/86uzq/helloworld.cpp:43:482: note: previous declaration 'NectarCore::Global::var main'
  var console;;var performance;;var Error;;var RegExp;;var Number;;var Object;;var Math;;var JSON;;var Array;;var Date;;var __NJS_Create_Object;;var __MODULE_gchcs4u94z;;var __MODULE_hp57np9gna;;var _performance;;var __MODULE_uv9rxwa1pn;;var __MODULE_eqkxnwtuv2;;var _regexp;;var __MODULE_066oord9sv;;var _Number;;var __MODULE_7tr71cumo6;;var __MODULE_zn71zum893;;var __MODULE_vygvtmrx7l;;var __MODULE_w7j1wvcu68;;var __MODULE_c2fwz11rb4;;var _date;;var __e6xr2l;;var __ml80hn;;var main;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ^~~~

    at checkExecSyncError (child_process.js:790:11)
    at execSync (child_process.js:863:15)
    at Compiler.Compile (/mnt/mstat/npm/prefix/lib/node_modules/nectarjs/compiler/native/compiler.js:278:3)
    at /mnt/mstat/npm/prefix/lib/node_modules/nectarjs/nectar.js:570:13
    at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:71:3) {
  status: 1,
  signal: null,
  output: [
    null,
    <Buffer >,
    <Buffer 2f 6d 6e 74 2f 6d 73 74 61 74 2f 73 6f 75 72 63 65 73 2f 74 65 73 74 2f 6e 6f 64 65 6a 73 2f 2e 6e 65 63 74 61 72 2f 38 36 75 7a 71 2f 68 65 6c 6c 6f ... 2452 more bytes>
  ],
  pid: 23872,
  stdout: <Buffer >,
  stderr: <Buffer 2f 6d 6e 74 2f 6d 73 74 61 74 2f 73 6f 75 72 63 65 73 2f 74 65 73 74 2f 6e 6f 64 65 6a 73 2f 2e 6e 65 63 74 61 72 2f 38 36 75 7a 71 2f 68 65 6c 6c 6f ... 2452 more bytes>
}
[!] Compilation error
fjqingyou commented 2 years ago

i try modify js and retry build

root@HL:nodejs# cat helloworld.js 

function main_local(){
    console.log("hello world");
}

main_local();

it's success!

so javascript file can not exists name is 'main' function ?

adrien-thierry commented 2 years ago

Hi !

In fact, as we transpile JS in C++, the main function should be marked as keyword to avoid using it,

thank you for your tests, it's a good news that it's working on OpenWrt/MIPS