Skip to content

Some minor refactor & partial windows support #17

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/cxx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ jobs:

steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: preconfigure
run: ${{ matrix.preconfigure }}
- name: configure
run: cmake -S . -B build_dir -Wdev -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -DCMAKE_C_FLAGS="${{ matrix.cflags }}" -DCMAKE_CXX_FLAGS="${{ matrix.cflags }}" -DCMAKE_EXE_LINKER_FLAGS="${{ matrix.cflags }}" ${{ matrix.cmake_opts }}
run: cmake -S . -B build_dir -Wdev -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -DCMAKE_C_FLAGS="${{ matrix.cflags }}" -DCMAKE_CXX_FLAGS="${{ matrix.cflags }}" -DCMAKE_EXE_LINKER_FLAGS="${{ matrix.cflags }}" ${{ matrix.cmake_opts }} -DBUILD_TESTING=1
- name: build
run: cmake --build build_dir --verbose
- name: test
run: ctest --extra-verbose
working-directory: build_dir
env:
# extra options for address sanitizer
ASAN_OPTIONS: strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1
ASAN_OPTIONS: strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vs
out/
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "quickjs"]
path = quickjs
url = https://github.com/bellard/quickjs
branch = master
91 changes: 83 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,91 @@
cmake_minimum_required(VERSION 3.8)
cmake_minimum_required(VERSION 3.15)
project(quickjspp)

set(CMAKE_CXX_STANDARD 17)
#set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)

if(CMAKE_COMPILER_IS_GNUCC)
add_compile_options(-Wall -Wno-unused-parameter)
option(QUICKJS_BIGNUM_ENABLE "Should bignum support be enabled" ON)
option(QUICKJS_ENABLE_AVX2 "Should use AVX2 as a consideration in compiling" ON)

if (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:DebugDLL>")
if(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug") )
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
endif()
elseif (CMAKE_COMPILER_IS_GNUCC)
add_compile_options(-Wall -Wno-unused-parameter)
endif()

if(UNIX)
find_package(Threads)
set(quickjs_extra_libs ${CMAKE_DL_LIBS} m Threads::Threads)
elseif (WIN32 AND NOT(MSYS OR MINGW))
# Run vcpkg install pthreads to install PThreads4W support
find_package(pthreads)
set(quickjs_extra_libs PThreads4W::PThreads4W)
endif()

file(STRINGS ${PROJECT_SOURCE_DIR}/quickjs/VERSION version)

set(quickjs_dir ${PROJECT_SOURCE_DIR}/quickjs)
set(quickjs_def "")

if (QUICKJS_BIGNUM_ENABLE)
set(quickjs_def ${quickjs_def} CONFIG_BIGNUM)
endif()

if (UNIX)
set(quickjs_def ${quickjs_def} _GNU_SOURCE)
elseif (WIN32 AND NOT(MSYS OR MINGW))
add_library(quickjs_win32_compat win32_compat/time.cpp)
target_include_directories(quickjs_win32_compat INTERFACE ${PROJECT_SOURCE_DIR}/win32_compat)
endif()

if (MSYS OR MINGW)
set(quickjs_def ${quickjs_def} CONFIG_VERSION="\"${version}\"")
else()
set(quickjs_def ${quickjs_def} CONFIG_VERSION="${version}")
endif()

add_library(libbf ${quickjs_dir}/libbf.c)
if (QUICKJS_ENABLE_AVX2)
target_compile_definitions(libbf PUBLIC __AVX2__)
# https://stackoverflow.com/a/5101557/3289081
set_target_properties(libbf PROPERTIES COMPILE_FLAGS "/arch:AVX2 /Oi")
endif()

add_library(libunicode ${quickjs_dir}/libunicode.c)
add_library(libregexp ${quickjs_dir}/libregexp.c)
add_library(cutils ${quickjs_dir}/cutils.c)

add_library(quickjs ${quickjs_dir}/quickjs.c ${quickjs_dir}/quickjs-libc.c)
target_compile_definitions(quickjs PRIVATE ${quickjs_def})
target_include_directories(quickjs INTERFACE ${PROJECT_SOURCE_DIR}/quickjs/)
target_link_libraries(quickjs PUBLIC ${quickjs_extra_libs} PRIVATE libbf libunicode libregexp cutils)
if (WIN32 AND NOT(MSYS OR MINGW))
target_compile_definitions(quickjs PRIVATE
ssize_t=SSIZE_T popen=_popen pclose=_pclose
_CRT_SECURE_NO_WARNINGS
)
target_link_libraries(quickjs PRIVATE quickjs_win32_compat)
endif()

add_subdirectory(quickjs)

add_library(quickjs-dumpleaks INTERFACE)
target_link_libraries(quickjs-dumpleaks INTERFACE quickjs)
target_compile_definitions(quickjs-dumpleaks INTERFACE DUMP_LEAKS=1)

add_library(quickjspp INTERFACE)
target_include_directories(quickjspp INTERFACE ${PROJECT_SOURCE_DIR}/include/)
target_link_libraries(quickjspp INTERFACE quickjs)

add_library(quickjspp-dumpleaks INTERFACE)
target_include_directories(quickjspp-dumpleaks INTERFACE ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(quickjspp-dumpleaks INTERFACE quickjs-dumpleaks)

add_executable(qjs qjs.cpp)
target_link_libraries(qjs quickjs)
target_link_libraries(qjs quickjspp)

enable_testing()
add_subdirectory(test)
if (BUILD_TESTING)
enable_testing()
add_subdirectory(test)
endif()
80 changes: 80 additions & 0 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "clang_cl_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"cmakeToolchain": "",
"variables": [
{
"name": "VCPKG_TARGET_TRIPLET",
"value": "x64-windows-static",
"type": "STRING"
}
]
},
{
"name": "x64-Release",
"generator": "Ninja",
"configurationType": "Release",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"cmakeToolchain": "",
"inheritEnvironments": [ "clang_cl_x64" ],
"variables": [
{
"name": "VCPKG_TARGET_TRIPLET",
"value": "x64-windows-static",
"type": "STRING"
}
]
},
{
"name": "x64-MinSizeRel",
"generator": "Ninja",
"configurationType": "MinSizeRel",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"cmakeToolchain": "",
"inheritEnvironments": [ "clang_cl_x64" ],
"variables": [
{
"name": "VCPKG_TARGET_TRIPLET",
"value": "x64-windows-static",
"type": "STRING"
}
]
},
{
"name": "x64-RelWithDebInfo",
"generator": "Ninja",
"configurationType": "RelWithDebInfo",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"cmakeToolchain": "",
"inheritEnvironments": [ "clang_cl_x64" ],
"variables": [
{
"name": "VCPKG_TARGET_TRIPLET",
"value": "x64-windows-static",
"type": "STRING"
}
]
}
]
}
4 changes: 2 additions & 2 deletions quickjspp.hpp → include/quickjspp.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "quickjs/quickjs.h"
#include "quickjs/quickjs-libc.h"
#include <quickjs.h>
#include <quickjs-libc.h>

#include <vector>
#include <string_view>
Expand Down
15 changes: 8 additions & 7 deletions qjs.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "quickjspp.hpp"

#include <quickjspp.hpp>

#include <iostream>

Expand All @@ -25,18 +24,20 @@ int main(int argc, char ** argv)
js_init_module_os(ctx, "os");

/* make 'std' and 'os' visible to non module code */
const char * str = "import * as std from 'std';\n"
"import * as os from 'os';\n"
"globalThis.std = std;\n"
"globalThis.os = os;\n";
const char * str = R"(
import * as std from 'std';
import * as os from 'os';
globalThis.std = std;
globalThis.os = os;
)";
context.eval(str, "<input>", JS_EVAL_TYPE_MODULE);

try
{
if(argv[1])
context.evalFile(argv[1], JS_EVAL_TYPE_MODULE);
}
catch(exception)
catch(qjs::exception)
{
//js_std_dump_error(ctx);
auto exc = context.getException();
Expand Down
1 change: 1 addition & 0 deletions quickjs
Submodule quickjs added at 7c312d
20 changes: 0 additions & 20 deletions quickjs/CMakeLists.txt

This file was deleted.

1 change: 0 additions & 1 deletion quickjs/VERSION

This file was deleted.

Loading