├── .coveralls.yml ├── .gitignore ├── .travis.yml ├── .travis ├── before_script.sh └── script.sh ├── LICENSE ├── Makefile ├── README.md ├── coverage.sh ├── examples ├── Makefile ├── dynamicarray.cpp ├── dynamicobject.cpp ├── helloworld.cpp ├── object.cpp └── square.cpp ├── grind.sh ├── src ├── libjsapi │ ├── Makefile │ ├── context.cpp │ ├── context.h │ ├── context_instance.cpp │ ├── context_instance.h │ ├── context_state.cpp │ ├── context_state.h │ ├── context_thread_guard.cpp │ ├── context_thread_guard.h │ ├── dynamic_array.cpp │ ├── dynamic_array.h │ ├── dynamic_object.cpp │ ├── dynamic_object.h │ ├── exceptions.cpp │ ├── exceptions.h │ ├── function_arguments.cpp │ ├── function_arguments.h │ ├── global.cpp │ ├── global.h │ ├── libjsapi.h │ ├── nbproject │ │ ├── Makefile-Debug.mk │ │ ├── Makefile-Release.mk │ │ ├── Makefile-impl.mk │ │ ├── Makefile-variables.mk │ │ ├── Package-Debug.bash │ │ ├── Package-Release.bash │ │ ├── configurations.xml │ │ └── project.xml │ ├── object.cpp │ ├── object.h │ ├── script.cpp │ ├── script.h │ ├── tests │ │ ├── call_js_function_tests.cpp │ │ ├── call_native_function_tests.cpp │ │ ├── function_arguments_tests.cpp │ │ ├── gc_tests.cpp │ │ ├── global_property_tests.cpp │ │ ├── multi_context_tests.cpp │ │ ├── script_exception_tests.cpp │ │ ├── simple_dynamic_array_tests.cpp │ │ ├── simple_dynamic_object_tests.cpp │ │ ├── simple_object_tests.cpp │ │ ├── simple_script_tests.cpp │ │ └── simple_value_tests.cpp │ ├── value.cpp │ ├── value.h │ └── vector_utils.h ├── make │ ├── coverage.mk │ ├── externals.mk │ ├── ldlibs.mk │ └── libmozglue.mk ├── testlibjsapi │ ├── Makefile │ ├── main.cpp │ └── nbproject │ │ ├── Makefile-Debug.mk │ │ ├── Makefile-Release.mk │ │ ├── Makefile-impl.mk │ │ ├── Makefile-variables.mk │ │ ├── Package-Debug.bash │ │ ├── Package-Release.bash │ │ ├── configurations.xml │ │ └── project.xml └── testlibjsapi_gtkmm │ ├── Makefile │ ├── Southport_Sunset.PNG │ ├── application.cpp │ ├── application.h │ ├── builder.cpp │ ├── builder.h │ ├── button.cpp │ ├── button.h │ ├── check_button.cpp │ ├── check_button.h │ ├── dom.js │ ├── drawing_area.cpp │ ├── drawing_area.glade │ ├── drawing_area.h │ ├── drawing_area.js │ ├── entry.cpp │ ├── entry.h │ ├── image_surface.cpp │ ├── image_surface.h │ ├── label.cpp │ ├── label.h │ ├── main.cpp │ ├── mandelbrot.glade │ ├── mandelbrot.js │ ├── nasa-9.png │ ├── nbproject │ ├── Makefile-Debug.mk │ ├── Makefile-Release.mk │ ├── Makefile-impl.mk │ ├── Makefile-variables.mk │ ├── Package-Debug.bash │ ├── Package-Release.bash │ ├── configurations.xml │ └── project.xml │ ├── test.glade │ ├── test.js │ ├── widget.cpp │ ├── widget.h │ ├── window.cpp │ └── window.h └── strip_mozjs.sh /.coveralls.yml: -------------------------------------------------------------------------------- 1 | repo_token: lRYqUCNORo2RZxQrKceswyMtKpVupiR90 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | 19 | # Compiled Static libraries 20 | *.lai 21 | *.la 22 | *.a 23 | *.lib 24 | 25 | # Executables 26 | *.exe 27 | *.out 28 | *.app 29 | /src/libjsapi/dist/ 30 | 31 | # ignore NetBeans related stuff 32 | /src/*/dist/ 33 | /src/*/build/ 34 | **/nbproject/private/ 35 | .dep.inc 36 | 37 | # ignore glade temp files 38 | *.glade~ 39 | 40 | # ignore externals 41 | externals/** 42 | 43 | # ignore the coverage output files 44 | coverage.info 45 | *.gcda 46 | *.gcno 47 | coverage/* 48 | 49 | # ignore the example output files 50 | examples/helloworld 51 | examples/square 52 | examples/object 53 | examples/dynamicobject 54 | examples/dynamicarray 55 | 56 | # ignore mac output files 57 | *.dSYM 58 | 59 | # ignore osx pollution 60 | .DS_Store 61 | 62 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | 3 | cache: ccache 4 | 5 | matrix: 6 | include: 7 | - os: linux 8 | sudo: false 9 | compiler: gcc 10 | env: _CC=gcc-4.9 _CXX=g++-4.9 _COV=gcov-4.9 11 | - os: linux 12 | sudo: false 13 | compiler: clang 14 | env: _CC=clang-3.6 _CXX=clang++-3.6 15 | - os: osx 16 | compiler: clang 17 | osx_image: xcode7.3 18 | env: _CC=clang _CXX=clang++ 19 | 20 | addons: 21 | apt: 22 | sources: 23 | - ubuntu-toolchain-r-test 24 | - llvm-toolchain-precise-3.6 25 | packages: 26 | - binutils-gold 27 | - autoconf2.13 28 | - g++-4.9 29 | - clang-3.6 30 | - zlib1g-dev 31 | - lcov 32 | - ruby 33 | - rubygems 34 | 35 | before_script: 36 | source ./.travis/before_script.sh 37 | 38 | script: 39 | source ./.travis/script.sh 40 | -------------------------------------------------------------------------------- /.travis/before_script.sh: -------------------------------------------------------------------------------- 1 | mkdir -p .travis.temp 2 | pushd .travis.temp 3 | 4 | if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then 5 | curl http://ftp.uk.debian.org/debian/pool/main/l/lcov/lcov_1.11.orig.tar.gz -O 6 | tar xfz lcov_1.11.orig.tar.gz 7 | mkdir -p lcov && make -C lcov-1.11/ install PREFIX=~/lcov 8 | export PATH=~/lcov/usr/bin:$PATH 9 | gem install coveralls-lcov 10 | elif [[ "$TRAVIS_OS_NAME" == "osx" ]]; then 11 | brew update 12 | test -d ~/.ccache && brew install ccache 13 | brew install autoconf@2.13 14 | fi 15 | 16 | popd 17 | -------------------------------------------------------------------------------- /.travis/script.sh: -------------------------------------------------------------------------------- 1 | # detect a missing ccache alias for the compiler we want 2 | CCACHE_BIN_PATH=`type -p ccache` 3 | if [ -n "${CCACHE_BIN_PATH}" ]; then 4 | echo "Found ccache at: ${CCACHE_BIN_PATH}" 5 | CCACHE_LIB_PATH=$(dirname `type -p g++ || type -p clang++` | grep ccache) 6 | echo "Found ccache lib at: ${CCACHE_LIB_PATH}" 7 | test ! -e "${CCACHE_LIB_PATH}/${_CC}" && export _CC="ccache ${_CC}" 8 | test ! -e "${CCACHE_LIB_PATH}/${_CXX}" && export _CXX="ccache ${_CXX}" 9 | fi 10 | 11 | # export CC, CXX and LDFLAGS into the environment 12 | export CC=${_CC} 13 | export CXX=${_CXX} 14 | export LDFLAGS=${LDFLAGS} 15 | 16 | # report what we are using 17 | echo ******************************************************************* 18 | echo CC=${CC} 19 | echo CXX=${CXX} 20 | echo LDFLAGS=${LDFLAGS} 21 | echo ******************************************************************* 22 | 23 | # build 24 | make CC="${_CC}" CXX="${_CXX}" LDFLAGS="${_LDFLAGS}" -j2 all && make CC="${_CC}" CXX="${_CXX}" LDFLAGS="${_LDFLAGS}" test || exit $? 25 | 26 | # generate coverage 27 | if [ "${_COV}" != "" ]; then 28 | ./coverage.sh "${_COV}" 29 | fi 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ripcord Software 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SHELL:=bash 2 | 3 | GTEST_VER:=1.7.0 4 | MOZJS_VER:=50.1.0 5 | MOZJS_VER_SUFFIX:= 6 | 7 | MOZJS_CONFIG_FLAGS+=--disable-jemalloc --disable-shared-js --disable-tests --enable-install-strip --without-intl-api 8 | 9 | CACHE_PATH:=~/Downloads 10 | 11 | EXTERNALS:=$(CURDIR)/externals 12 | INSTALLED:=$(EXTERNALS)/installed 13 | INSTALLED_INC:=$(INSTALLED)/include 14 | INSTALLED_LIB:=$(INSTALLED)/lib 15 | INSTALLED_DIRS:=$(EXTERNALS) $(INSTALLED) $(INSTALLED_INC) $(INSTALLED_LIB) 16 | 17 | MOZJS_ARCHIVE_NAME:=mozjs-$(MOZJS_VER).tar.bz2 18 | MOZJS_ARCHIVE_PATH:=$(EXTERNALS)/$(MOZJS_ARCHIVE_NAME) 19 | MOZJS_SOURCE_PATH:=$(EXTERNALS)/mozjs-$(MOZJS_VER) 20 | MOZJS_BUILD_PATH:=$(MOZJS_SOURCE_PATH)/js/src/build_OPT.OBJ 21 | MOZJS_H:=$(INSTALLED_INC)/mozjs/jsapi.h 22 | MOZJS_LIB:=$(INSTALLED_LIB)/libjs_static.ajs 23 | 24 | GTEST_ARCHIVE_NAME:=gtest-$(GTEST_VER).zip 25 | GTEST_ARCHIVE_PATH:=$(EXTERNALS)/$(GTEST_ARCHIVE_NAME) 26 | GTEST_H:=$(INSTALLED_INC)/gtest/gtest.h 27 | GTEST_LIB:=$(INSTALLED_LIB)/libgtest.a 28 | 29 | .PHONY: build all test examples clean .jsapi .googletest 30 | .PRECIOUS: $(MOZJS_ARCHIVE_PATH) $(GTEST_ARCHIVE_PATH) $(MOZJS_LIB) $(GTEST_LIB) 31 | .NOTPARALLEL: test 32 | 33 | build all test: .jsapi .googletest 34 | cd src/libjsapi && $(MAKE) $@ 35 | 36 | examples: build 37 | cd examples && $(MAKE) 38 | 39 | clean: 40 | cd src/libjsapi && $(MAKE) $@ 41 | cd examples && $(MAKE) $@ 42 | 43 | .jsapi: $(MOZJS_LIB) 44 | 45 | .googletest: $(GTEST_LIB) 46 | 47 | $(MOZJS_LIB): $(MOZJS_ARCHIVE_PATH) 48 | mkdir -p $(INSTALLED_DIRS) && \ 49 | cd $(MOZJS_BUILD_PATH) && \ 50 | $(MAKE) MAKEOVERRIDES= && \ 51 | $(MAKE) install MAKEOVERRIDES= && \ 52 | export LIBMOZGLUE=dist/sdk/lib/libmozglue.a && if [ -e $${LIBMOZGLUE} ]; then cp -fp $${LIBMOZGLUE} $(INSTALLED_LIB); fi && \ 53 | export LIBMOZGLUE=dist/sdk/lib/libmozglue.dylib && if [ -e $${LIBMOZGLUE} ]; then cp -fp $${LIBMOZGLUE} $(INSTALLED_LIB); fi && \ 54 | cd $(INSTALLED_INC) && \ 55 | if [ -L mozjs ]; then rm mozjs; fi && ln -s mozjs-?? mozjs 56 | 57 | $(MOZJS_ARCHIVE_PATH): 58 | mkdir -p $(INSTALLED_DIRS) && \ 59 | cd $(EXTERNALS) && \ 60 | (test -f $(CACHE_PATH)/$(MOZJS_ARCHIVE_NAME) && cp -fp $(CACHE_PATH)/$(MOZJS_ARCHIVE_NAME) .) || curl http://cdn.ripcordsoftware.com/mozjs-$(MOZJS_VER)$(MOZJS_VER_SUFFIX).tar.bz2 -o $(MOZJS_ARCHIVE_NAME) && \ 61 | mkdir -p $(MOZJS_SOURCE_PATH) && cd $(MOZJS_SOURCE_PATH) && \ 62 | tar xfj ../$(MOZJS_ARCHIVE_NAME) --strip-components=1 && \ 63 | mkdir -p $(MOZJS_BUILD_PATH) && cd $(MOZJS_BUILD_PATH) && \ 64 | CC="$(CC)" CFLAGS="$(CFLAGS)" CXX="$(CXX)" CXXFLAGS="$(CXXFLAGS)" ../configure --prefix=$(INSTALLED) $(MOZJS_CONFIG_FLAGS) 65 | 66 | $(GTEST_LIB): $(GTEST_ARCHIVE_PATH) 67 | mkdir -p $(INSTALLED_DIRS) && \ 68 | cd $(EXTERNALS)/gtest-$(GTEST_VER) && \ 69 | $(MAKE) MAKEOVERRIDES= && \ 70 | cp -Rfp include/* $(INSTALLED_INC) && \ 71 | cp -Rfp lib/.libs/* $(INSTALLED_LIB) 72 | 73 | $(GTEST_ARCHIVE_PATH): 74 | mkdir -p $(INSTALLED_DIRS) && \ 75 | cd $(EXTERNALS) && \ 76 | (test -f $(CACHE_PATH)/$(GTEST_ARCHIVE_NAME) && cp -fp $(CACHE_PATH)/$(GTEST_ARCHIVE_NAME) .) || curl http://cdn.ripcordsoftware.com/gtest-$(GTEST_VER).zip -o $(GTEST_ARCHIVE_NAME) && \ 77 | unzip -o $(GTEST_ARCHIVE_NAME) && \ 78 | cd gtest-$(GTEST_VER) && \ 79 | CC="$(CC)" CFLAGS="$(CFLAGS)" CXX="$(CXX)" CXXFLAGS="$(CXXFLAGS)" ./configure 80 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/RipcordSoftware/libjsapi.svg?branch=master)](https://travis-ci.org/RipcordSoftware/libjsapi) 2 | [![Coverage Status](https://coveralls.io/repos/RipcordSoftware/libjsapi/badge.svg)](https://coveralls.io/r/RipcordSoftware/libjsapi) 3 | [![License](http://img.shields.io/:license-mit-blue.svg)](http://doge.mit-license.org) 4 | 5 | # libjsapi 6 | A SpiderMonkey 50 (Mozilla) JSAPI wrapper for C++ 11. 7 | 8 | The JSAPI interface to the SpiderMonkey JavaScript VM can be difficult to integrate into 9 | applications without an intermediate abstraction layer managing object creation, object lifetimes and 10 | type mapping. This library is an attempt to hide the complexity of dealing with SpiderMonkey 11 | from modern C++ 11 applications. 12 | 13 | With `libjsapi` you will be able to: 14 | * Execute any valid JavaScript 15 | * Invoke JavaScript methods from C++ 16 | * Expose C++ methods to JavaScript 17 | * Expose C++ objects to JavaScript 18 | * Consume JavaScript objects in C++ 19 | * Handle errors with C++ exceptions 20 | * Create any number of Runtimes and Contexts - simultaneously on multiple threads 21 | 22 | ##Gratuitous Screenshot 23 | 24 | The image below shows `libjsapi` hosting a GTK+ application which emulates enough browser bits to render a funky JavaScript Mandelbrot application ported from https://github.com/cslarsen/mandelbrot-js. 25 | 26 | ![A JS generated Mandelbrot running under libjsapi](https://pbs.twimg.com/media/CEVOZlRWAAAt6-w.png:large) 27 | 28 | The window on the left is Firefox rendering a Mandelbrot fractal, on the right is the `libjsapi` application running (mainly) the same JavaScript code. 29 | 30 | ##Examples: 31 | 32 | The simplest thing that could possibly work: 33 | ```c++ 34 | #include 35 | #include "libjsapi.h" 36 | 37 | int main() { 38 | // create the context which hosts spidermonkey 39 | rs::jsapi::Context cx; 40 | 41 | // execute a script in the context of the runtime, getting the result 42 | rs::jsapi::Value result(cx); 43 | cx.Evaluate("(function(){return 42;})();", result); 44 | 45 | // output the result to the console 46 | std::cout << result << std::endl; 47 | } 48 | ``` 49 | 50 | Among other things we can expose C++ lambdas (and methods) to JS: 51 | ```c++ 52 | #include 53 | #include "libjsapi.h" 54 | 55 | int main() { 56 | // create the context which hosts spidermonkey 57 | rs::jsapi::Context cx; 58 | 59 | // define a function in global scope implemented by a C++ lambda 60 | rs::jsapi::Global::DefineFunction(cx, "getTheAnswer", 61 | [](const std::vector& args, rs::jsapi::Value& result) { 62 | result = 42; 63 | }); 64 | 65 | // call the native function from JS 66 | rs::jsapi::Value result(cx); 67 | cx.Evaluate("(function(){return getTheAnswer();})();", result); 68 | 69 | // output the result to the console 70 | std::cout << result << std::endl; 71 | } 72 | ``` 73 | 74 | The [wiki](https://github.com/RipcordSoftware/libjsapi/wiki) contains more background on JSAPI, libjsapi and further examples. 75 | 76 | # Building 77 | Requires: 78 | * GCC 4.8.1+ 79 | * GNU Make 80 | * lcov and Ruby (for test coverage) 81 | * valgrind (for memory analysis) 82 | 83 | To build type: 84 | ```bash 85 | $ make 86 | ``` 87 | 88 | The first `libjsapi` build may take a long time to complete since it will invoke a build of SpiderMonkey and GoogleTest. Subsequent builds do not require this step and will be much faster. 89 | 90 | To build tests with coverage: 91 | ```bash 92 | $ make test 93 | ``` 94 | 95 | To create a coverage report: 96 | ```bash 97 | $ ./coverage.sh 98 | ``` 99 | 100 | To create a valgrind report: 101 | ```bash 102 | $ ./grind.sh 103 | ``` 104 | 105 | You will find examples in the `examples` directory. You can build these after building libjsapi by running: 106 | ```bash 107 | $ cd examples 108 | $ make 109 | ``` 110 | 111 | If building is slow on your system then you can speed it up by stripping the debugging information from the mozjs library by running `./strip_mozjs.sh`. 112 | 113 | The [wiki](https://github.com/RipcordSoftware/libjsapi/wiki) contains details on building on different systems. 114 | -------------------------------------------------------------------------------- /coverage.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | GCOV_TOOL= 4 | if [ "$1" != "" ]; then 5 | GCOV_TOOL="--gcov-tool $1" 6 | else 7 | which lcov > /dev/null 8 | if [ $? -ne 0 ]; then 9 | echo "You must have lcov installed to run this script." 10 | echo "Install with 'yum install lcov' or 'apt-get install lcov'" 11 | exit 1 12 | fi 13 | fi 14 | 15 | lcov -b src/libjsapi --directory src/libjsapi/build/Debug/GNU-Linux-x86/ --capture --output-file coverage.info ${GCOV_TOOL} 16 | lcov --remove coverage.info 'tests/*' '/usr/*' '*/externals/*' --output-file coverage.info ${GCOV_TOOL} 17 | lcov --list coverage.info ${GCOV_TOOL} 18 | if [ "$CI" = "true" ] && [ "$TRAVIS" = "true" ]; then 19 | coveralls-lcov coverage.info 20 | else 21 | rm -rf coverage 22 | mkdir -p coverage 23 | cd coverage 24 | genhtml ../coverage.info 25 | cd .. 26 | fi 27 | -------------------------------------------------------------------------------- /examples/Makefile: -------------------------------------------------------------------------------- 1 | CXX:=g++ 2 | 3 | include ../src/make/coverage.mk 4 | include ../src/make/ldlibs.mk 5 | include ../src/make/libmozglue.mk 6 | 7 | JSLIBS:=../src/libjsapi/dist/Debug/GNU-Linux-x86/libjsapi.a ../externals/installed/lib/libjs_static.ajs $(call LIBMOZGLUE, ../externals/installed/lib) $(LDLIBS) 8 | CXXFLAGS:=--std=c++11 -I ../src/libjsapi -I ../externals/installed/include/mozjs-50/ $(JSLIBS) -lpthread `pkg-config --libs zlib` -ldl --coverage -g 9 | 10 | LDFLAGS:=$(LDLIBS) 11 | 12 | all: helloworld square object dynamicobject dynamicarray 13 | 14 | .PHONY: clean 15 | 16 | helloworld: helloworld.cpp 17 | $(CXX) $? $(CXXFLAGS) $(LDFLAGS) -o $@ 18 | 19 | square: square.cpp 20 | $(CXX) $? $(CXXFLAGS) $(LDFLAGS) -o $@ 21 | 22 | object: object.cpp 23 | $(CXX) $? $(CXXFLAGS) $(LDFLAGS) -o $@ 24 | 25 | dynamicobject: dynamicobject.cpp 26 | $(CXX) $? $(CXXFLAGS) $(LDFLAGS) -o $@ 27 | 28 | dynamicarray: dynamicarray.cpp 29 | $(CXX) $? $(CXXFLAGS) $(LDFLAGS) -o $@ 30 | 31 | clean: 32 | rm -f helloworld square object dynamicobject dynamicarray 33 | -------------------------------------------------------------------------------- /examples/dynamicarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "libjsapi.h" 5 | 6 | int main() { 7 | std::vector data = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 }; 8 | 9 | rs::jsapi::Context cx; 10 | 11 | // create a dynamic array which exposes the `data` vector to JS 12 | rs::jsapi::Value array(cx); 13 | rs::jsapi::DynamicArray::Create(cx, 14 | [&](int index, rs::jsapi::Value& value) { value = data[index]; }, 15 | nullptr, 16 | [&]() { return data.size(); }, 17 | nullptr, 18 | array); 19 | 20 | // create a function which returns the value of the item 'n' 21 | // on the passed array 22 | cx.Evaluate("var myfunc=function(arr, n){ return arr[n]; }"); 23 | 24 | // create an arguments instance so we can call the JS function 25 | rs::jsapi::FunctionArguments args(cx); 26 | args.Append(array); 27 | args.Append(0); 28 | 29 | // invoke the function and get the value of the index i 30 | for (int i = 0; i < data.size(); ++i) { 31 | args[1].setInt32(i); 32 | 33 | rs::jsapi::Value result(cx); 34 | cx.Call("myfunc", args, result); 35 | 36 | std::cout << result << std::endl; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /examples/dynamicobject.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "libjsapi.h" 6 | 7 | int main() { 8 | std::unordered_map data = { 9 | { "hello", "world" }, { "lorem", "ipsum" }, 10 | { "foo", "bar" } }; 11 | 12 | rs::jsapi::Context cx; 13 | 14 | // create a dynamic object which returns a string from the map 15 | rs::jsapi::Value obj(cx); 16 | rs::jsapi::DynamicObject::Create(cx, 17 | [&](const char* name, rs::jsapi::Value& value) { 18 | auto result = data.find(name); 19 | if (result != data.cend()) { 20 | value = result->second; 21 | } else { 22 | value.setUndefined(); 23 | } 24 | }, 25 | nullptr, 26 | nullptr, 27 | nullptr, 28 | obj); 29 | 30 | // create a function which returns the value of the field 'n' 31 | // on the passed object 32 | cx.Evaluate("var myfunc=function(o, n){return o[n];};"); 33 | 34 | rs::jsapi::FunctionArguments args(cx); 35 | args.Append(obj); 36 | args.Append("hello"); 37 | 38 | // invoke the function and get the value of the field 'hello' 39 | rs::jsapi::Value result(cx); 40 | cx.Call("myfunc", args, result); 41 | std::cout << result << std::endl; 42 | 43 | // invoke the function and get the value of the field 'lorem' 44 | args[1] = rs::jsapi::Value(cx, "lorem"); 45 | cx.Call("myfunc", args, result); 46 | std::cout << result << std::endl; 47 | 48 | // invoke the function and get the value of the field 'foo' 49 | args[1] = rs::jsapi::Value(cx, "foo"); 50 | cx.Call("myfunc", args, result); 51 | std::cout << result << std::endl; 52 | 53 | // invoke the function and get the value of the field 'xyz' 54 | args[1] = rs::jsapi::Value(cx, "xyz"); 55 | cx.Call("myfunc", args, result); 56 | std::cout << result << std::endl; 57 | } 58 | -------------------------------------------------------------------------------- /examples/helloworld.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "libjsapi.h" 3 | 4 | int main() { 5 | rs::jsapi::Context cx; 6 | 7 | // define a global function called echo which 8 | // prints the first argument on stdout 9 | rs::jsapi::Global::DefineFunction(cx, "echo", 10 | [](const std::vector& args, rs::jsapi::Value& result) { 11 | if (args.size() > 0) { 12 | std::cout << args[0] << std::endl; 13 | } 14 | }); 15 | 16 | // invoke echo passing a string 17 | rs::jsapi::FunctionArguments args(cx); 18 | args.Append("Hello world!!"); 19 | cx.Call("echo", args); 20 | 21 | // call the function from JavaScript 22 | cx.Evaluate("echo('lorem ipsum...');"); 23 | } 24 | -------------------------------------------------------------------------------- /examples/object.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "libjsapi.h" 3 | 4 | int main() { 5 | rs::jsapi::Context cx; 6 | 7 | // create an object with a single field 'the_answer' with a getter callback which always returns 42 8 | rs::jsapi::Value obj(cx); 9 | rs::jsapi::Object::Create(cx, { "the_answer" }, 10 | [](const char* name, rs::jsapi::Value& value) { value = 42; }, 11 | nullptr, 12 | {}, 13 | nullptr, 14 | obj); 15 | 16 | // create a function which returns the value of the field 'the_answer' on the passed object 17 | cx.Evaluate("var myfunc=function(o){return o.the_answer;};"); 18 | 19 | rs::jsapi::FunctionArguments args(cx); 20 | args.Append(obj); 21 | 22 | // invoke the function and get the result 23 | rs::jsapi::Value result(cx); 24 | cx.Call("myfunc", args, result); 25 | 26 | // output the result to the console 27 | std::cout << result << std::endl; 28 | } 29 | -------------------------------------------------------------------------------- /examples/square.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "libjsapi.h" 3 | 4 | int main() { 5 | rs::jsapi::Context cx; 6 | 7 | // define a function which squares the first argument and returns the result 8 | rs::jsapi::Global::DefineFunction(cx, "square", 9 | [](const std::vector& args, rs::jsapi::Value& result) { 10 | if (args.size() > 0) { 11 | result = args[0].toNumber() * args[0].toNumber(); 12 | } 13 | }); 14 | 15 | // call the function via JSAPI 16 | rs::jsapi::FunctionArguments args(cx); 17 | args.Append(5); 18 | rs::jsapi::Value result(cx); 19 | cx.Call("square", args, result); 20 | std::cout << result << std::endl; 21 | 22 | // call the function from JavaScript and return the result 23 | cx.Evaluate("(function(){return square(9);})();", result); 24 | std::cout << result << std::endl; 25 | } 26 | -------------------------------------------------------------------------------- /grind.sh: -------------------------------------------------------------------------------- 1 | !#/bin/sh 2 | make test 3 | 4 | rm -f valgrind-*.out 5 | 6 | for f in src/libjsapi/build/Debug/GNU-Linux-x86/tests/TestFiles/*; do 7 | valgrind --log-file=valgrind-%p.out $f --gtest_repeat=10 8 | done 9 | 10 | cat valgrind-*.out 11 | -------------------------------------------------------------------------------- /src/libjsapi/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # There exist several targets which are by default empty and which can be 3 | # used for execution of your targets. These targets are usually executed 4 | # before and after some main targets. They are: 5 | # 6 | # .build-pre: called before 'build' target 7 | # .build-post: called after 'build' target 8 | # .clean-pre: called before 'clean' target 9 | # .clean-post: called after 'clean' target 10 | # .clobber-pre: called before 'clobber' target 11 | # .clobber-post: called after 'clobber' target 12 | # .all-pre: called before 'all' target 13 | # .all-post: called after 'all' target 14 | # .help-pre: called before 'help' target 15 | # .help-post: called after 'help' target 16 | # 17 | # Targets beginning with '.' are not intended to be called on their own. 18 | # 19 | # Main targets can be executed directly, and they are: 20 | # 21 | # build build a specific configuration 22 | # clean remove built files from a configuration 23 | # clobber remove all built files 24 | # all build all configurations 25 | # help print help mesage 26 | # 27 | # Targets .build-impl, .clean-impl, .clobber-impl, .all-impl, and 28 | # .help-impl are implemented in nbproject/makefile-impl.mk. 29 | # 30 | # Available make variables: 31 | # 32 | # CND_BASEDIR base directory for relative paths 33 | # CND_DISTDIR default top distribution directory (build artifacts) 34 | # CND_BUILDDIR default top build directory (object files, ...) 35 | # CONF name of current configuration 36 | # CND_PLATFORM_${CONF} platform name (current configuration) 37 | # CND_ARTIFACT_DIR_${CONF} directory of build artifact (current configuration) 38 | # CND_ARTIFACT_NAME_${CONF} name of build artifact (current configuration) 39 | # CND_ARTIFACT_PATH_${CONF} path to build artifact (current configuration) 40 | # CND_PACKAGE_DIR_${CONF} directory of package (current configuration) 41 | # CND_PACKAGE_NAME_${CONF} name of package (current configuration) 42 | # CND_PACKAGE_PATH_${CONF} path to package (current configuration) 43 | # 44 | # NOCDDL 45 | 46 | 47 | # Environment 48 | MKDIR=mkdir 49 | CP=cp 50 | CCADMIN=CCadmin 51 | 52 | # include common vars 53 | include ../make/externals.mk 54 | include ../make/coverage.mk 55 | include ../make/ldlibs.mk 56 | 57 | # add libmozglue params to the linker options 58 | include ../make/libmozglue.mk 59 | LDLIBS:=$(call LIBMOZGLUE, $(EXT_INSTALLED_LIB)) $(LDLIBS) 60 | 61 | # build 62 | build: .build-post 63 | 64 | .build-pre: 65 | # Add your pre 'build' code here... 66 | 67 | .build-post: .build-impl 68 | # Add your post 'build' code here... 69 | 70 | 71 | # clean 72 | clean: .clean-post 73 | 74 | .clean-pre: 75 | # Add your pre 'clean' code here... 76 | 77 | .clean-post: .clean-impl 78 | # Add your post 'clean' code here... 79 | 80 | 81 | # clobber 82 | clobber: .clobber-post 83 | 84 | .clobber-pre: 85 | # Add your pre 'clobber' code here... 86 | 87 | .clobber-post: .clobber-impl 88 | # Add your post 'clobber' code here... 89 | 90 | 91 | # all 92 | all: .all-post 93 | 94 | .all-pre: 95 | # Add your pre 'all' code here... 96 | 97 | .all-post: .all-impl 98 | # Add your post 'all' code here... 99 | 100 | 101 | # build tests 102 | build-tests: .build-tests-post 103 | 104 | .build-tests-pre: 105 | # Add your pre 'build-tests' code here... 106 | 107 | .build-tests-post: .build-tests-impl 108 | # Add your post 'build-tests' code here... 109 | 110 | 111 | # run tests 112 | test: .test-post 113 | 114 | .test-pre: build-tests 115 | # Add your pre 'test' code here... 116 | if [ -e "$(EXT_INSTALLED_LIB)/libmozglue.dylib" ]; then cp -f "$(EXT_INSTALLED_LIB)/libmozglue.dylib" "$(CND_BUILDDIR)/${CONF}/${CND_PLATFORM_${CONF}}/tests/TestFiles"; fi 117 | 118 | .test-post: .test-impl 119 | # Add your post 'test' code here... 120 | 121 | 122 | # help 123 | help: .help-post 124 | 125 | .help-pre: 126 | # Add your pre 'help' code here... 127 | 128 | .help-post: .help-impl 129 | # Add your post 'help' code here... 130 | 131 | 132 | 133 | # include project implementation makefile 134 | include nbproject/Makefile-impl.mk 135 | 136 | # include project make variables 137 | include nbproject/Makefile-variables.mk 138 | -------------------------------------------------------------------------------- /src/libjsapi/context.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef RS_JSAPI_CONTEXT_H 26 | #define RS_JSAPI_CONTEXT_H 27 | 28 | #include 29 | 30 | #include 31 | 32 | #include "exceptions.h" 33 | #include "script.h" 34 | #include "context_instance.h" 35 | #include "context_thread_guard.h" 36 | 37 | namespace rs { 38 | namespace jsapi { 39 | 40 | class FunctionArguments; 41 | class Value; 42 | 43 | class Context final { 44 | public: 45 | Context(uint32_t maxBytes = JS::DefaultHeapMaxBytes, uint32_t maxNurseryBytes = JS::DefaultNurseryBytes, bool enableBaselineCompiler = true, bool enableIonCompiler = true); 46 | ~Context(); 47 | 48 | bool Evaluate(const char* script); 49 | bool Evaluate(const char* script, Value& result); 50 | bool Call(const char* name); 51 | bool Call(const char* name, const FunctionArguments& args); 52 | bool Call(const char* name, Value& result); 53 | bool Call(const char* name, const FunctionArguments& args, Value& result); 54 | static bool Call(Value& value, const FunctionArguments& args, bool throwOnError = true); 55 | static bool Call(Value& value, const FunctionArguments& args, Value& result, bool throwOnError = true); 56 | 57 | Context(const Context&) = delete; 58 | Context& operator =(const Context&) = delete; 59 | 60 | JSContext* getContext() { CheckCallingThread(); return cx_; } 61 | JS::HandleObject getGlobal() { CheckCallingThread(); return global_; } 62 | 63 | operator JSContext*() { return cx_; } 64 | 65 | void GCNow() { CheckCallingThread(); JS_GC(cx_); } 66 | void MaybeGC() { CheckCallingThread(); JS_MaybeGC(cx_); } 67 | 68 | private: 69 | friend bool Script::Compile(); 70 | friend bool Script::Execute(); 71 | friend bool Script::Execute(Value&); 72 | 73 | std::unique_ptr GetError(); 74 | std::unique_ptr GetContextError(); 75 | std::unique_ptr GetContextException(); 76 | 77 | static JSContext* NewContext(uint32_t maxbytes, uint32_t maxNurseryBytes); 78 | static void ReportWarning(JSContext *cx, const char *message, JSErrorReport *report); 79 | 80 | void DestroyContext(); 81 | 82 | void CheckCallingThread() { threadGuard_.CheckCallingThread(); } 83 | 84 | static JS::CompartmentOptions options_; 85 | 86 | ContextInstance inst_; 87 | const ContextThreadGuard threadGuard_; 88 | 89 | JSContext* cx_; 90 | JS::PersistentRootedObject global_; 91 | JSCompartment* oldCompartment_; 92 | 93 | std::unique_ptr exception_; 94 | }; 95 | 96 | }} 97 | 98 | #endif /* RS_JSAPI_CONTEXT_H */ -------------------------------------------------------------------------------- /src/libjsapi/context_instance.cpp: -------------------------------------------------------------------------------- 1 | #include "context_instance.h" 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | 9 | #ifdef __GNUC__ 10 | #define INIT_PRIORITY(N) __attribute__ ((init_priority (N + 101))) 11 | #else 12 | #error "Unable to set static initiaiization order on this platform. If you wish to fix this for your compiler please PR the change." 13 | #endif 14 | 15 | std::atomic rs::jsapi::ContextInstance::initCalled_ INIT_PRIORITY(0); 16 | std::atomic rs::jsapi::ContextInstance::count_ INIT_PRIORITY(0); 17 | std::mutex rs::jsapi::ContextInstance::m_ INIT_PRIORITY(0); 18 | std::atomic rs::jsapi::ContextInstance::context_; 19 | 20 | rs::jsapi::ContextInstance::ContextInstance() { 21 | if (count_ == 0 && !initCalled_) { 22 | std::lock_guard lk(m_); 23 | if (count_ == 0 && !initCalled_) { 24 | JS_Init(); 25 | 26 | context_ = JS_NewContext(JS::DefaultHeapMaxBytes, JS::DefaultNurseryBytes); 27 | JS::InitSelfHostedCode(context_); 28 | 29 | // shutdown JS at process end 30 | std::atexit(AtExit); 31 | 32 | // keep track of the number of instances 33 | ++count_; 34 | 35 | // don't allow init to be called more than once 36 | initCalled_= true; 37 | } else { 38 | ++count_; 39 | } 40 | } else { 41 | ++count_; 42 | } 43 | } 44 | 45 | rs::jsapi::ContextInstance::~ContextInstance() { 46 | --count_; 47 | } 48 | 49 | void rs::jsapi::ContextInstance::AtExit() { 50 | // ideally we should destroy the context, however Destroy() must be 51 | // called on the right thread - and we probably aren't on the right one 52 | // so it would just blow up 53 | //JS_DestroyContext(context_); 54 | 55 | JS_ShutDown(); 56 | } -------------------------------------------------------------------------------- /src/libjsapi/context_instance.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef RS_JSAPI_CONTEXT_INSTANCE_H 26 | #define RS_JSAPI_CONTEXT_INSTANCE_H 27 | 28 | #include 29 | #include 30 | 31 | class JSContext; 32 | 33 | namespace rs { 34 | namespace jsapi { 35 | 36 | class ContextInstance { 37 | public: 38 | ContextInstance(); 39 | ~ContextInstance(); 40 | 41 | static JSContext* GetParentContext() { return context_; } 42 | 43 | private: 44 | static std::atomic initCalled_; 45 | static std::atomic count_; 46 | static std::mutex m_; 47 | static std::atomic context_; 48 | 49 | static void AtExit(); 50 | }; 51 | 52 | }} 53 | 54 | #endif /* RS_JSAPI_CONTEXT_INSTANCE_H */ 55 | 56 | -------------------------------------------------------------------------------- /src/libjsapi/context_state.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #include "context_state.h" 26 | 27 | bool rs::jsapi::ContextState::NewState(JSContext* cx, JS::WarningReporter warningReporter, void* ptr) { 28 | auto state = GetState(cx); 29 | if (state == nullptr) { 30 | auto state = new State(warningReporter, ptr); 31 | JS_SetContextPrivate(cx, state); 32 | return true; 33 | } else { 34 | return false; 35 | } 36 | } 37 | 38 | bool rs::jsapi::ContextState::DeleteState(JSContext* cx) { 39 | auto state = GetState(cx); 40 | if (state != nullptr) { 41 | JS_SetContextPrivate(cx, nullptr); 42 | delete state; 43 | return true; 44 | } else { 45 | return false; 46 | } 47 | } 48 | 49 | rs::jsapi::ContextState::State* rs::jsapi::ContextState::GetState(JSContext* cx) { 50 | auto state = reinterpret_cast(JS_GetContextPrivate(cx)); 51 | return state; 52 | } 53 | 54 | bool rs::jsapi::ContextState::SetDataPtr(JSContext* cx, void* ptr) { 55 | auto state = GetState(cx); 56 | if (state != nullptr) { 57 | state->ptr_ = ptr; 58 | return true; 59 | } else { 60 | return false; 61 | } 62 | } 63 | 64 | void* rs::jsapi::ContextState::GetDataPtr(JSContext* cx) { 65 | void* ptr = nullptr; 66 | auto state = GetState(cx); 67 | if (state != nullptr) { 68 | ptr = state->ptr_; 69 | } 70 | return ptr; 71 | } 72 | 73 | void rs::jsapi::ContextState::ReportWarning(JSContext* cx, const char* message, JSErrorReport* report) { 74 | auto state = GetState(cx); 75 | if (state != nullptr) { 76 | auto reporter = state->warningReporter_.load(); 77 | if (reporter != nullptr) { 78 | reporter(cx, message, report); 79 | } 80 | } 81 | } 82 | 83 | void rs::jsapi::ContextState::DetachWarningReporter(JSContext* cx) { 84 | auto state = GetState(cx); 85 | if (state != nullptr) { 86 | state->warningReporter_ = nullptr; 87 | } 88 | } -------------------------------------------------------------------------------- /src/libjsapi/context_state.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef RS_JSAPI_CONTEXT_STATE_H 26 | #define RS_JSAPI_CONTEXT_STATE_H 27 | 28 | #include 29 | 30 | #include 31 | 32 | namespace rs { 33 | namespace jsapi { 34 | 35 | class ContextState final { 36 | public: 37 | 38 | static bool NewState(JSContext* cx, JS::WarningReporter = nullptr, void* ptr = nullptr); 39 | static bool DeleteState(JSContext* cx); 40 | 41 | static bool SetDataPtr(JSContext* cx, void* ptr); 42 | static void* GetDataPtr(JSContext* cx); 43 | 44 | static void ReportWarning(JSContext* cx, const char* message, JSErrorReport* report); 45 | static void DetachWarningReporter(JSContext* cx); 46 | 47 | private: 48 | 49 | struct State final { 50 | public: 51 | State(JS::WarningReporter warningReporter, void* ptr) : 52 | warningReporter_(warningReporter), ptr_(ptr) {} 53 | 54 | std::atomic warningReporter_; 55 | void* ptr_; 56 | }; 57 | 58 | static State* GetState(JSContext* cx); 59 | }; 60 | 61 | }} 62 | 63 | #endif /* RS_JSAPI_CONTEXT_STATE_H */ 64 | -------------------------------------------------------------------------------- /src/libjsapi/context_thread_guard.cpp: -------------------------------------------------------------------------------- 1 | #include "context_thread_guard.h" 2 | 3 | #include 4 | 5 | #include "exceptions.h" 6 | 7 | std::vector rs::jsapi::ContextThreadGuard::activeThreads_; 8 | std::mutex rs::jsapi::ContextThreadGuard::activeThreadsLock_; 9 | 10 | rs::jsapi::ContextThreadGuard::ContextThreadGuard(std::thread::id id) : id_(id) { 11 | if (!AddThread(id)) { 12 | throw ContextThreadInstanceException(); 13 | } 14 | } 15 | 16 | bool rs::jsapi::ContextThreadGuard::AddThread(std::thread::id id) { 17 | std::lock_guard lock(activeThreadsLock_); 18 | 19 | auto iter = std::find(activeThreads_.cbegin(), activeThreads_.cend(), id); 20 | bool add = iter == activeThreads_.cend(); 21 | if (add) { 22 | activeThreads_.push_back(id); 23 | } 24 | 25 | return add; 26 | } 27 | 28 | bool rs::jsapi::ContextThreadGuard::RemoveThread(std::thread::id id) { 29 | std::lock_guard lock(activeThreadsLock_); 30 | 31 | auto iter = std::find(activeThreads_.begin(), activeThreads_.end(), id); 32 | bool remove = iter != activeThreads_.end(); 33 | if (remove) { 34 | activeThreads_.erase(iter); 35 | } 36 | 37 | return remove; 38 | } 39 | 40 | bool rs::jsapi::ContextThreadGuard::RemoveThread() { 41 | return RemoveThread(std::this_thread::get_id()); 42 | } 43 | 44 | void rs::jsapi::ContextThreadGuard::CheckCallingThread() const { 45 | if (std::this_thread::get_id() != id_) { 46 | throw ContextWrongThreadException(); 47 | } 48 | } -------------------------------------------------------------------------------- /src/libjsapi/context_thread_guard.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef RS_JSAPI_CONTEXT_THREAD_GUARD_H 26 | #define RS_JSAPI_CONTEXT_THREAD_GUARD_H 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | namespace rs { 34 | namespace jsapi { 35 | 36 | class ContextThreadGuard { 37 | public: 38 | ContextThreadGuard() : ContextThreadGuard(std::this_thread::get_id()) {} 39 | ContextThreadGuard(std::thread::id id); 40 | 41 | ~ContextThreadGuard() { 42 | RemoveThread(); 43 | } 44 | 45 | static bool AddThread(std::thread::id id); 46 | static bool RemoveThread(std::thread::id id); 47 | static bool RemoveThread(); 48 | 49 | void CheckCallingThread() const; 50 | 51 | private: 52 | static std::vector activeThreads_; 53 | static std::mutex activeThreadsLock_; 54 | 55 | const std::thread::id id_; 56 | }; 57 | 58 | }} 59 | 60 | #endif /* RS_JSAPI_CONTEXT_THREAD_GUARD_H */ 61 | 62 | -------------------------------------------------------------------------------- /src/libjsapi/dynamic_array.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef RS_JSAPI_DYNAMIC_ARRAY_H 26 | #define RS_JSAPI_DYNAMIC_ARRAY_H 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | 34 | #include "value.h" 35 | 36 | namespace rs { 37 | namespace jsapi { 38 | 39 | class DynamicArray final { 40 | public: 41 | using GetCallback = std::function; 42 | using SetCallback = std::function; 43 | using LengthCallback = std::function; 44 | using FinalizeCallback = std::function; 45 | 46 | static bool Create(JSContext*, const GetCallback& getter, const SetCallback& setter, const LengthCallback& length, const FinalizeCallback& finalize, Value& array); 47 | 48 | static bool SetPrivate(Value&, uint64_t, void*); 49 | static bool GetPrivate(const Value&, uint64_t&, void*&); 50 | 51 | static bool IsDynamicArray(const Value&); 52 | 53 | private: 54 | struct DynamicArrayState { 55 | DynamicArrayState(const GetCallback& g, const SetCallback& s, 56 | const LengthCallback& l, const FinalizeCallback& f, 57 | uint64_t d = 0, void* p = nullptr) : 58 | getter(g), setter(s), length(l), finalizer(f), data(d), ptr(p) {} 59 | 60 | const GetCallback getter; 61 | const SetCallback setter; 62 | const LengthCallback length; 63 | const FinalizeCallback finalizer; 64 | uint64_t data; 65 | void* ptr; 66 | }; 67 | 68 | static bool Get(JSContext*, JS::HandleObject, JS::HandleId, JS::MutableHandleValue); 69 | static bool Set(JSContext*, JS::HandleObject, JS::HandleId, JS::MutableHandleValue, JS::ObjectOpResult&); 70 | static void Finalize(JSFreeOp* fop, JSObject* obj); 71 | static bool Length(JSContext*, unsigned, JS::Value*); 72 | 73 | static DynamicArrayState* GetState(JSContext* cx, JS::HandleObject obj); 74 | static DynamicArrayState* GetState(JSObject* obj); 75 | static void SetState(JSObject* obj, DynamicArrayState* state); 76 | 77 | static JSClassOps classOps_; 78 | static JSClass class_; 79 | }; 80 | 81 | }} 82 | 83 | #endif -------------------------------------------------------------------------------- /src/libjsapi/dynamic_object.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef RS_JSAPI_DYNAMIC_OBJECT_H 26 | #define RS_JSAPI_DYNAMIC_OBJECT_H 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | 34 | #include "value.h" 35 | 36 | namespace rs { 37 | namespace jsapi { 38 | 39 | class DynamicObject final { 40 | public: 41 | using GetCallback = std::function; 42 | using SetCallback = std::function; 43 | using EnumeratorCallback = std::function& props, std::vector>& funcs)>; 44 | using FinalizeCallback = std::function; 45 | 46 | static bool Create(JSContext*, const GetCallback& getter, const SetCallback& setter, const EnumeratorCallback& enumerator, const FinalizeCallback& finalize, Value& obj); 47 | 48 | static bool SetPrivate(Value&, uint64_t, void*); 49 | static bool GetPrivate(const Value&, uint64_t&, void*&); 50 | 51 | static bool IsDynamicObject(const Value&); 52 | 53 | private: 54 | struct DynamicObjectState { 55 | DynamicObjectState(const GetCallback& g, const SetCallback& s, 56 | const EnumeratorCallback& e, const FinalizeCallback& f, 57 | uint64_t d = 0, void* p = nullptr) : 58 | getter(g), setter(s), enumerator(e), finalizer(f), data(d), ptr(p) {} 59 | 60 | const GetCallback getter; 61 | const SetCallback setter; 62 | const EnumeratorCallback enumerator; 63 | const FinalizeCallback finalizer; 64 | uint64_t data; 65 | void* ptr; 66 | }; 67 | 68 | static bool Get(JSContext*, JS::HandleObject, JS::HandleId, JS::MutableHandleValue); 69 | static bool Set(JSContext*, JS::HandleObject, JS::HandleId, JS::MutableHandleValue, JS::ObjectOpResult&); 70 | static bool Enumerate(JSContext* cx, JS::HandleObject obj); 71 | static void Finalize(JSFreeOp* fop, JSObject* obj); 72 | 73 | static bool Get(JSContext*, JS::HandleObject, JSString*, JS::MutableHandleValue); 74 | static bool Set(JSContext*, JS::HandleObject, JSString*, JS::MutableHandleValue); 75 | 76 | static DynamicObjectState* GetState(JSContext* cx, JS::HandleObject obj); 77 | static DynamicObjectState* GetState(JSObject* obj); 78 | static void SetState(JSObject* obj, DynamicObjectState* state); 79 | 80 | static JSClassOps classOps_; 81 | static JSClass class_; 82 | }; 83 | 84 | }} 85 | 86 | #endif -------------------------------------------------------------------------------- /src/libjsapi/exceptions.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #include "exceptions.h" 26 | 27 | #include 28 | 29 | static std::map jsapiExceptionTypes = { 30 | { JSEXN_INTERNALERR, "InternalError: " }, 31 | { JSEXN_EVALERR, "EvaluationError: " }, 32 | { JSEXN_RANGEERR, "RangeError: " }, 33 | { JSEXN_REFERENCEERR, "ReferenceError: " }, 34 | { JSEXN_SYNTAXERR, "SyntaxError: " }, 35 | { JSEXN_TYPEERR, "TypeError: " }, 36 | { JSEXN_URIERR, "UriError: " }, 37 | { JSEXN_DEBUGGEEWOULDRUN, "DebuggeeWouldRun: " }, 38 | { JSEXN_WARN, "Warning: " } 39 | }; 40 | 41 | std::string rs::jsapi::ScriptException::U16toString(const char16_t* ptr) { 42 | std::string str; 43 | if (ptr) { 44 | while (*ptr != '\0') { 45 | str += *ptr < 128 ? (char) *ptr : '?'; 46 | ++ptr; 47 | } 48 | } 49 | return str; 50 | } 51 | 52 | std::string rs::jsapi::ScriptException::GetExceptionMessage(const JSErrorReport* error) { 53 | std::string str; 54 | 55 | if (error && error->ucmessage && jsapiExceptionTypes.find(error->exnType) != jsapiExceptionTypes.cend()) { 56 | str = jsapiExceptionTypes[error->exnType] + U16toString(error->ucmessage); 57 | } 58 | 59 | return str; 60 | } 61 | -------------------------------------------------------------------------------- /src/libjsapi/exceptions.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef RS_JSAPI_EXCEPTIONS_H 26 | #define RS_JSAPI_EXCEPTIONS_H 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | namespace rs { 34 | namespace jsapi { 35 | 36 | class Exception : public std::exception { 37 | public: 38 | virtual const char* what() const noexcept override { 39 | return "An exception in JSAPI occurred"; 40 | } 41 | }; 42 | 43 | class ContextWrongThreadException : public Exception { 44 | public: 45 | virtual const char* what() const noexcept override { 46 | return "Context call made from the wrong thread"; 47 | } 48 | }; 49 | 50 | class ContextThreadInstanceException : public Exception { 51 | public: 52 | virtual const char* what() const noexcept override { 53 | return "A context is already active on this thread"; 54 | } 55 | }; 56 | 57 | class ScriptException : public Exception { 58 | public: 59 | ScriptException(const char* message, const JSErrorReport* error) : 60 | message(message != nullptr ? message : ""), 61 | filename(error->filename != nullptr ? error->filename : ""), 62 | lineno(error->lineno), 63 | column(error->column), 64 | uclinebuf(error->linebuf() != nullptr ? error->linebuf() : u""), 65 | uctokenOffset(error->tokenOffset()), 66 | errorNumber(error->errorNumber), 67 | exnType(error->exnType) { 68 | } 69 | 70 | ScriptException(const JSErrorReport* error) : 71 | ScriptException(GetExceptionMessage(error).c_str(), error) {} 72 | 73 | virtual const char* what() const noexcept override { 74 | return message.length() > 0 ? message.c_str() : "An error or exception happened in JSAPI"; 75 | } 76 | 77 | const std::string message; 78 | const std::string filename; 79 | const unsigned lineno; 80 | const unsigned column; 81 | const std::u16string uclinebuf; 82 | const unsigned uctokenOffset; 83 | const unsigned errorNumber; 84 | const int16_t exnType; 85 | 86 | private: 87 | static std::string U16toString(const char16_t* ptr); 88 | static std::string GetExceptionMessage(const JSErrorReport* error); 89 | }; 90 | 91 | class ValueCastException : public Exception { 92 | public: 93 | virtual const char* what() const noexcept override { 94 | return "Invalid object or value cast"; 95 | } 96 | }; 97 | 98 | class FunctionArgumentsIndexException : public Exception { 99 | public: 100 | virtual const char* what() const noexcept override { 101 | return "The FunctionArguments index value was invalid"; 102 | } 103 | }; 104 | 105 | }} 106 | 107 | #endif /* RS_JSAPI_EXCEPTIONS_H */ 108 | 109 | -------------------------------------------------------------------------------- /src/libjsapi/function_arguments.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #include "function_arguments.h" 26 | #include "context.h" 27 | #include "object.h" 28 | 29 | rs::jsapi::FunctionArguments::FunctionArguments(Context& cx) : cx_(cx), args_(cx_) { 30 | 31 | } 32 | 33 | bool rs::jsapi::FunctionArguments::Append(signed value) { 34 | return args_.append(JS::Int32Value(value)); 35 | } 36 | 37 | bool rs::jsapi::FunctionArguments::Append(unsigned value) { 38 | return args_.append(JS::DoubleValue(value)); 39 | } 40 | 41 | bool rs::jsapi::FunctionArguments::Append(double value) { 42 | return args_.append(JS::DoubleValue(value)); 43 | } 44 | 45 | bool rs::jsapi::FunctionArguments::Append(const char* value) { 46 | JSAutoRequest ar(cx_); 47 | auto str = JS_NewStringCopyZ(cx_, value); 48 | auto strValue = JS::StringValue(str); 49 | return args_.append(strValue); 50 | } 51 | 52 | bool rs::jsapi::FunctionArguments::Append(const std::string& value) { 53 | return Append(value.c_str()); 54 | } 55 | 56 | bool rs::jsapi::FunctionArguments::Append(bool value) { 57 | return args_.append(JS::BooleanValue(value)); 58 | } 59 | 60 | bool rs::jsapi::FunctionArguments::Append(const Value& value) { 61 | return args_.append(value); 62 | } 63 | 64 | bool rs::jsapi::FunctionArguments::Append(const JS::RootedObject& obj) { 65 | return args_.append(JS::ObjectOrNullValue(obj)); 66 | } 67 | 68 | bool rs::jsapi::FunctionArguments::Append(const JS::RootedValue& value) { 69 | return args_.append(value); 70 | } 71 | 72 | void rs::jsapi::FunctionArguments::Clear() { 73 | args_.clear(); 74 | } 75 | 76 | bool rs::jsapi::FunctionArguments::Empty() { 77 | return args_.empty(); 78 | } 79 | 80 | int rs::jsapi::FunctionArguments::getLength() { 81 | return args_.length(); 82 | } 83 | 84 | JS::Value& rs::jsapi::FunctionArguments::operator [](int index) { 85 | if (index < 0 || index >= args_.length()) { 86 | throw FunctionArgumentsIndexException(); 87 | } 88 | 89 | return args_[index].get(); 90 | } -------------------------------------------------------------------------------- /src/libjsapi/function_arguments.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef RS_JSAPI_FUNCTION_ARGUMENTS_H 26 | #define RS_JSAPI_FUNCTION_ARGUMENTS_H 27 | 28 | #include 29 | 30 | #include 31 | 32 | #include "value.h" 33 | 34 | namespace rs { 35 | namespace jsapi { 36 | 37 | class Context; 38 | 39 | class FunctionArguments final { 40 | public: 41 | FunctionArguments(Context& cx); 42 | 43 | bool Append(signed value); 44 | bool Append(unsigned value); 45 | bool Append(double value); 46 | bool Append(const char* value); 47 | bool Append(const std::string& value); 48 | bool Append(bool value); 49 | bool Append(const Value& value); 50 | bool Append(const JS::RootedObject& obj); 51 | bool Append(const JS::RootedValue& value); 52 | 53 | void Clear(); 54 | bool Empty(); 55 | int getLength(); 56 | 57 | operator const JS::AutoValueVector&() const { return args_; } 58 | operator const JS::HandleValueArray() const { return args_; } 59 | JS::Value& operator [](int index); 60 | 61 | private: 62 | Context& cx_; 63 | JS::AutoValueVector args_; 64 | }; 65 | 66 | }} 67 | 68 | #endif /* RS_JSAPI_FUNCTION_ARGUMENTS_H */ 69 | 70 | -------------------------------------------------------------------------------- /src/libjsapi/global.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #include "global.h" 26 | #include "context.h" 27 | #include "vector_utils.h" 28 | 29 | JSClassOps rs::jsapi::Global::privateFunctionStateClassOps_ = { 30 | nullptr, nullptr, 31 | nullptr, nullptr, 32 | nullptr, nullptr, 33 | nullptr, Global::Finalize, 34 | nullptr, nullptr, 35 | nullptr, nullptr 36 | }; 37 | 38 | JSClass rs::jsapi::Global::privateFunctionStateClass_ = { 39 | "rs_jsapi_global_function_object", JSCLASS_HAS_PRIVATE, &privateFunctionStateClassOps_ 40 | }; 41 | 42 | const char* rs::jsapi::Global::privateFunctionStatePropertyName_ = "__rs_jsapi__private_global_function_state"; 43 | 44 | bool rs::jsapi::Global::DefineProperty(Context& cx, const char* name, const Value& value, unsigned attrs) { 45 | JSAutoRequest ar(cx); 46 | if (value.isObject()) { 47 | return JS_DefineProperty(cx, cx.getGlobal(), name, value.getHandleObject(), attrs); 48 | } else { 49 | return JS_DefineProperty(cx, cx.getGlobal(), name, value.getHandleValue(), attrs); 50 | } 51 | } 52 | 53 | bool rs::jsapi::Global::DefineProperty(Context& cx, const char* name, JSNative getter, JSNative setter, unsigned attrs) { 54 | JSAutoRequest ar(cx); 55 | return JS_DefineProperty(cx, cx.getGlobal(), name, JS::NullHandleValue, attrs, getter, setter); 56 | } 57 | 58 | bool rs::jsapi::Global::DefineFunction(Context& cx, const char* name, FunctionCallback callback, unsigned attrs) { 59 | JSAutoRequest ar(cx); 60 | JS::RootedFunction func(cx.getContext(), JS_NewFunction(cx, CallFunction, 0, 0, name)); 61 | JS::RootedObject funcObj(cx.getContext(), JS_GetFunctionObject(func)); 62 | 63 | JS::RootedObject privateFuncObj(cx.getContext(), JS_NewObject(cx, &privateFunctionStateClass_)); 64 | JS_SetPrivate(privateFuncObj, new GlobalFunctionState(callback)); 65 | JS_DefineProperty(cx, funcObj, privateFunctionStatePropertyName_, privateFuncObj, 0, nullptr, nullptr); 66 | 67 | return JS_DefineProperty(cx, cx.getGlobal(), name, funcObj, attrs, nullptr, nullptr); 68 | } 69 | 70 | bool rs::jsapi::Global::CallFunction(JSContext* cx, unsigned argc, JS::Value* vp) { 71 | JSAutoRequest ar(cx); 72 | auto args = JS::CallArgsFromVp(argc, vp); 73 | 74 | auto state = Global::GetFunctionState(&args.callee(), cx, privateFunctionStatePropertyName_); 75 | if (state) { 76 | try { 77 | #if (defined(__APPLE__) && __clang_major__ < 8) || (!defined(__APPLE__) && __clang_major < 4) 78 | std::vector vArgs; 79 | #else 80 | static thread_local std::vector vArgs; 81 | #endif 82 | 83 | VectorUtils::ScopedVectorCleaner clean(vArgs); 84 | for (int i = 0; i < argc; ++i) { 85 | vArgs.emplace_back(cx, args.get(i)); 86 | } 87 | 88 | Value result(cx); 89 | state->function_(vArgs, result); 90 | args.rval().set(result); 91 | return true; 92 | } catch (const std::exception& ex) { 93 | JS_ReportError(cx, ex.what()); 94 | return false; 95 | } 96 | } else { 97 | JS_ReportError(cx, "Unable to find function callback in libjsapi object"); 98 | return false; 99 | } 100 | } 101 | 102 | void rs::jsapi::Global::Finalize(JSFreeOp* fop, JSObject* obj) { 103 | auto data = GetFunctionState(obj); 104 | if (data) { 105 | delete data; 106 | SetFunctionState(obj, nullptr); 107 | } 108 | } 109 | 110 | rs::jsapi::Global::GlobalFunctionState* rs::jsapi::Global::GetFunctionState(JSObject* obj, JSContext* cx, const char* propName) { 111 | JSAutoRequest ar(cx); 112 | GlobalFunctionState* state = nullptr; 113 | 114 | if (obj) { 115 | rs::jsapi::Value result(cx); 116 | if (JS_GetProperty(cx, JS::RootedObject(cx, obj), privateFunctionStatePropertyName_, result) && result.isObject()) { 117 | state = GetFunctionState(result.toObject()); 118 | } 119 | } 120 | 121 | return state; 122 | } 123 | 124 | rs::jsapi::Global::GlobalFunctionState* rs::jsapi::Global::GetFunctionState(JSObject* obj) { 125 | auto state = obj != nullptr ? JS_GetPrivate(obj) : nullptr; 126 | return reinterpret_cast(state); 127 | } 128 | 129 | void rs::jsapi::Global::SetFunctionState(JSObject* obj, GlobalFunctionState* state) { 130 | JS_SetPrivate(obj, state); 131 | } 132 | 133 | bool rs::jsapi::Global::DeleteProperty(Context& cx, const char* name) { 134 | JSAutoRequest ar(cx); 135 | return JS_DeleteProperty(cx, cx.getGlobal(), name); 136 | } 137 | 138 | bool rs::jsapi::Global::DeleteFunction(Context& cx, const char* name) { 139 | JSAutoRequest ar(cx); 140 | return JS_DeleteProperty(cx, cx.getGlobal(), name); 141 | } 142 | -------------------------------------------------------------------------------- /src/libjsapi/global.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef RS_JSAPI_GLOBAL_H 26 | #define RS_JSAPI_GLOBAL_H 27 | 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | #include "value.h" 34 | 35 | namespace rs { 36 | namespace jsapi { 37 | 38 | class Context; 39 | 40 | class Global final { 41 | public: 42 | using FunctionCallback = std::function&, Value&)>; 43 | 44 | Global() = delete; 45 | Global(const Global& orig) = delete; 46 | 47 | static bool DefineProperty(Context& cx, const char* name, const Value& value, unsigned attrs = JSPROP_ENUMERATE); 48 | static bool DefineProperty(Context& cx, const char* name, JSNative getter, JSNative setter, unsigned attrs = JSPROP_ENUMERATE); 49 | static bool DefineFunction(Context& cx, const char* name, FunctionCallback callback, unsigned attrs = JSPROP_ENUMERATE); 50 | 51 | static bool DeleteProperty(Context& cx, const char* name); 52 | static bool DeleteFunction(Context& cx, const char* name); 53 | 54 | private: 55 | struct GlobalFunctionState { 56 | GlobalFunctionState(FunctionCallback function) : function_(function) {} 57 | 58 | const FunctionCallback function_; 59 | }; 60 | 61 | static bool CallFunction(JSContext*, unsigned, JS::Value*); 62 | static void Finalize(JSFreeOp* fop, JSObject* obj); 63 | 64 | static GlobalFunctionState* GetFunctionState(JSObject* obj, JSContext* cx, const char* propName); 65 | static GlobalFunctionState* GetFunctionState(JSObject* obj); 66 | static void SetFunctionState(JSObject* obj, GlobalFunctionState* state); 67 | 68 | static JSClassOps privateFunctionStateClassOps_; 69 | static JSClass privateFunctionStateClass_; 70 | static const char* privateFunctionStatePropertyName_; 71 | }; 72 | 73 | }} 74 | 75 | #endif /* RS_JSAPI_GLOBAL_H */ 76 | 77 | -------------------------------------------------------------------------------- /src/libjsapi/libjsapi.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef RS_LIBJSAPI_H 26 | #define RS_LIBJSAPI_H 27 | 28 | #include "context.h" 29 | #include "script.h" 30 | #include "value.h" 31 | #include "global.h" 32 | #include "object.h" 33 | #include "dynamic_object.h" 34 | #include "dynamic_array.h" 35 | #include "function_arguments.h" 36 | #include "exceptions.h" 37 | 38 | #endif /* RS_LIBJSAPI_H */ 39 | 40 | -------------------------------------------------------------------------------- /src/libjsapi/nbproject/Makefile-impl.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated Makefile - do not edit! 3 | # 4 | # Edit the Makefile in the project folder instead (../Makefile). Each target 5 | # has a pre- and a post- target defined where you can add customization code. 6 | # 7 | # This makefile implements macros and targets common to all configurations. 8 | # 9 | # NOCDDL 10 | 11 | 12 | # Building and Cleaning subprojects are done by default, but can be controlled with the SUB 13 | # macro. If SUB=no, subprojects will not be built or cleaned. The following macro 14 | # statements set BUILD_SUB-CONF and CLEAN_SUB-CONF to .build-reqprojects-conf 15 | # and .clean-reqprojects-conf unless SUB has the value 'no' 16 | SUB_no=NO 17 | SUBPROJECTS=${SUB_${SUB}} 18 | BUILD_SUBPROJECTS_=.build-subprojects 19 | BUILD_SUBPROJECTS_NO= 20 | BUILD_SUBPROJECTS=${BUILD_SUBPROJECTS_${SUBPROJECTS}} 21 | CLEAN_SUBPROJECTS_=.clean-subprojects 22 | CLEAN_SUBPROJECTS_NO= 23 | CLEAN_SUBPROJECTS=${CLEAN_SUBPROJECTS_${SUBPROJECTS}} 24 | 25 | 26 | # Project Name 27 | PROJECTNAME=libjsapi 28 | 29 | # Active Configuration 30 | DEFAULTCONF=Debug 31 | CONF=${DEFAULTCONF} 32 | 33 | # All Configurations 34 | ALLCONFS=Debug Release 35 | 36 | 37 | # build 38 | .build-impl: .build-pre .validate-impl .depcheck-impl 39 | @#echo "=> Running $@... Configuration=$(CONF)" 40 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .build-conf 41 | 42 | 43 | # clean 44 | .clean-impl: .clean-pre .validate-impl .depcheck-impl 45 | @#echo "=> Running $@... Configuration=$(CONF)" 46 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .clean-conf 47 | 48 | 49 | # clobber 50 | .clobber-impl: .clobber-pre .depcheck-impl 51 | @#echo "=> Running $@..." 52 | for CONF in ${ALLCONFS}; \ 53 | do \ 54 | "${MAKE}" -f nbproject/Makefile-$${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .clean-conf; \ 55 | done 56 | 57 | # all 58 | .all-impl: .all-pre .depcheck-impl 59 | @#echo "=> Running $@..." 60 | for CONF in ${ALLCONFS}; \ 61 | do \ 62 | "${MAKE}" -f nbproject/Makefile-$${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .build-conf; \ 63 | done 64 | 65 | # build tests 66 | .build-tests-impl: .build-impl .build-tests-pre 67 | @#echo "=> Running $@... Configuration=$(CONF)" 68 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .build-tests-conf 69 | 70 | # run tests 71 | .test-impl: .build-tests-impl .test-pre 72 | @#echo "=> Running $@... Configuration=$(CONF)" 73 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .test-conf 74 | 75 | # dependency checking support 76 | .depcheck-impl: 77 | @echo "# This code depends on make tool being used" >.dep.inc 78 | @if [ -n "${MAKE_VERSION}" ]; then \ 79 | echo "DEPFILES=\$$(wildcard \$$(addsuffix .d, \$${OBJECTFILES}))" >>.dep.inc; \ 80 | echo "ifneq (\$${DEPFILES},)" >>.dep.inc; \ 81 | echo "include \$${DEPFILES}" >>.dep.inc; \ 82 | echo "endif" >>.dep.inc; \ 83 | else \ 84 | echo ".KEEP_STATE:" >>.dep.inc; \ 85 | echo ".KEEP_STATE_FILE:.make.state.\$${CONF}" >>.dep.inc; \ 86 | fi 87 | 88 | # configuration validation 89 | .validate-impl: 90 | @if [ ! -f nbproject/Makefile-${CONF}.mk ]; \ 91 | then \ 92 | echo ""; \ 93 | echo "Error: can not find the makefile for configuration '${CONF}' in project ${PROJECTNAME}"; \ 94 | echo "See 'make help' for details."; \ 95 | echo "Current directory: " `pwd`; \ 96 | echo ""; \ 97 | fi 98 | @if [ ! -f nbproject/Makefile-${CONF}.mk ]; \ 99 | then \ 100 | exit 1; \ 101 | fi 102 | 103 | 104 | # help 105 | .help-impl: .help-pre 106 | @echo "This makefile supports the following configurations:" 107 | @echo " ${ALLCONFS}" 108 | @echo "" 109 | @echo "and the following targets:" 110 | @echo " build (default target)" 111 | @echo " clean" 112 | @echo " clobber" 113 | @echo " all" 114 | @echo " help" 115 | @echo "" 116 | @echo "Makefile Usage:" 117 | @echo " make [CONF=] [SUB=no] build" 118 | @echo " make [CONF=] [SUB=no] clean" 119 | @echo " make [SUB=no] clobber" 120 | @echo " make [SUB=no] all" 121 | @echo " make help" 122 | @echo "" 123 | @echo "Target 'build' will build a specific configuration and, unless 'SUB=no'," 124 | @echo " also build subprojects." 125 | @echo "Target 'clean' will clean a specific configuration and, unless 'SUB=no'," 126 | @echo " also clean subprojects." 127 | @echo "Target 'clobber' will remove all built files from all configurations and," 128 | @echo " unless 'SUB=no', also from subprojects." 129 | @echo "Target 'all' will will build all configurations and, unless 'SUB=no'," 130 | @echo " also build subprojects." 131 | @echo "Target 'help' prints this message." 132 | @echo "" 133 | 134 | -------------------------------------------------------------------------------- /src/libjsapi/nbproject/Makefile-variables.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated - do not edit! 3 | # 4 | # NOCDDL 5 | # 6 | CND_BASEDIR=`pwd` 7 | CND_BUILDDIR=build 8 | CND_DISTDIR=dist 9 | # Debug configuration 10 | CND_PLATFORM_Debug=GNU-Linux-x86 11 | CND_ARTIFACT_DIR_Debug=dist/Debug/GNU-Linux-x86 12 | CND_ARTIFACT_NAME_Debug=libjsapi.a 13 | CND_ARTIFACT_PATH_Debug=dist/Debug/GNU-Linux-x86/libjsapi.a 14 | CND_PACKAGE_DIR_Debug=dist/Debug/GNU-Linux-x86/package 15 | CND_PACKAGE_NAME_Debug=libjsapi.tar 16 | CND_PACKAGE_PATH_Debug=dist/Debug/GNU-Linux-x86/package/libjsapi.tar 17 | # Release configuration 18 | CND_PLATFORM_Release=GNU-Linux-x86 19 | CND_ARTIFACT_DIR_Release=dist/Release/GNU-Linux-x86 20 | CND_ARTIFACT_NAME_Release=libjsapi.a 21 | CND_ARTIFACT_PATH_Release=dist/Release/GNU-Linux-x86/libjsapi.a 22 | CND_PACKAGE_DIR_Release=dist/Release/GNU-Linux-x86/package 23 | CND_PACKAGE_NAME_Release=libjsapi.tar 24 | CND_PACKAGE_PATH_Release=dist/Release/GNU-Linux-x86/package/libjsapi.tar 25 | # 26 | # include compiler specific variables 27 | # 28 | # dmake command 29 | ROOT:sh = test -f nbproject/private/Makefile-variables.mk || \ 30 | (mkdir -p nbproject/private && touch nbproject/private/Makefile-variables.mk) 31 | # 32 | # gmake command 33 | .PHONY: $(shell test -f nbproject/private/Makefile-variables.mk || (mkdir -p nbproject/private && touch nbproject/private/Makefile-variables.mk)) 34 | # 35 | include nbproject/private/Makefile-variables.mk 36 | -------------------------------------------------------------------------------- /src/libjsapi/nbproject/Package-Debug.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | 3 | # 4 | # Generated - do not edit! 5 | # 6 | 7 | # Macros 8 | TOP=`pwd` 9 | CND_PLATFORM=GNU-Linux-x86 10 | CND_CONF=Debug 11 | CND_DISTDIR=dist 12 | CND_BUILDDIR=build 13 | CND_DLIB_EXT=so 14 | NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging 15 | TMPDIRNAME=tmp-packaging 16 | OUTPUT_PATH=${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libjsapi.a 17 | OUTPUT_BASENAME=libjsapi.a 18 | PACKAGE_TOP_DIR=libjsapi/ 19 | 20 | # Functions 21 | function checkReturnCode 22 | { 23 | rc=$? 24 | if [ $rc != 0 ] 25 | then 26 | exit $rc 27 | fi 28 | } 29 | function makeDirectory 30 | # $1 directory path 31 | # $2 permission (optional) 32 | { 33 | mkdir -p "$1" 34 | checkReturnCode 35 | if [ "$2" != "" ] 36 | then 37 | chmod $2 "$1" 38 | checkReturnCode 39 | fi 40 | } 41 | function copyFileToTmpDir 42 | # $1 from-file path 43 | # $2 to-file path 44 | # $3 permission 45 | { 46 | cp "$1" "$2" 47 | checkReturnCode 48 | if [ "$3" != "" ] 49 | then 50 | chmod $3 "$2" 51 | checkReturnCode 52 | fi 53 | } 54 | 55 | # Setup 56 | cd "${TOP}" 57 | mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package 58 | rm -rf ${NBTMPDIR} 59 | mkdir -p ${NBTMPDIR} 60 | 61 | # Copy files and create directories and links 62 | cd "${TOP}" 63 | makeDirectory "${NBTMPDIR}/libjsapi/lib" 64 | copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}lib/${OUTPUT_BASENAME}" 0644 65 | 66 | 67 | # Generate tar file 68 | cd "${TOP}" 69 | rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/libjsapi.tar 70 | cd ${NBTMPDIR} 71 | tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/libjsapi.tar * 72 | checkReturnCode 73 | 74 | # Cleanup 75 | cd "${TOP}" 76 | rm -rf ${NBTMPDIR} 77 | -------------------------------------------------------------------------------- /src/libjsapi/nbproject/Package-Release.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | 3 | # 4 | # Generated - do not edit! 5 | # 6 | 7 | # Macros 8 | TOP=`pwd` 9 | CND_PLATFORM=GNU-Linux-x86 10 | CND_CONF=Release 11 | CND_DISTDIR=dist 12 | CND_BUILDDIR=build 13 | CND_DLIB_EXT=so 14 | NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging 15 | TMPDIRNAME=tmp-packaging 16 | OUTPUT_PATH=${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libjsapi.a 17 | OUTPUT_BASENAME=libjsapi.a 18 | PACKAGE_TOP_DIR=libjsapi/ 19 | 20 | # Functions 21 | function checkReturnCode 22 | { 23 | rc=$? 24 | if [ $rc != 0 ] 25 | then 26 | exit $rc 27 | fi 28 | } 29 | function makeDirectory 30 | # $1 directory path 31 | # $2 permission (optional) 32 | { 33 | mkdir -p "$1" 34 | checkReturnCode 35 | if [ "$2" != "" ] 36 | then 37 | chmod $2 "$1" 38 | checkReturnCode 39 | fi 40 | } 41 | function copyFileToTmpDir 42 | # $1 from-file path 43 | # $2 to-file path 44 | # $3 permission 45 | { 46 | cp "$1" "$2" 47 | checkReturnCode 48 | if [ "$3" != "" ] 49 | then 50 | chmod $3 "$2" 51 | checkReturnCode 52 | fi 53 | } 54 | 55 | # Setup 56 | cd "${TOP}" 57 | mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package 58 | rm -rf ${NBTMPDIR} 59 | mkdir -p ${NBTMPDIR} 60 | 61 | # Copy files and create directories and links 62 | cd "${TOP}" 63 | makeDirectory "${NBTMPDIR}/libjsapi/lib" 64 | copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}lib/${OUTPUT_BASENAME}" 0644 65 | 66 | 67 | # Generate tar file 68 | cd "${TOP}" 69 | rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/libjsapi.tar 70 | cd ${NBTMPDIR} 71 | tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/libjsapi.tar * 72 | checkReturnCode 73 | 74 | # Cleanup 75 | cd "${TOP}" 76 | rm -rf ${NBTMPDIR} 77 | -------------------------------------------------------------------------------- /src/libjsapi/nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.cnd.makeproject 4 | 5 | 6 | libjsapi 7 | 8 | cpp 9 | h 10 | UTF-8 11 | 12 | 13 | 14 | 15 | Debug 16 | 3 17 | 18 | 19 | Release 20 | 3 21 | 22 | 23 | 24 | false 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/libjsapi/object.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef RS_JSAPI_OBJECT_H 26 | #define RS_JSAPI_OBJECT_H 27 | 28 | #include 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include "value.h" 36 | 37 | namespace rs { 38 | namespace jsapi { 39 | 40 | class Context; 41 | 42 | class Object final { 43 | public: 44 | using GetCallback = std::function; 45 | using SetCallback = std::function; 46 | using FinalizeCallback = std::function; 47 | using FunctionCallback = std::function&, Value&)>; 48 | using Functions = std::unordered_map; 49 | 50 | Object(Context& cx) = delete; 51 | Object(const Object&) = delete; 52 | 53 | static bool Create(JSContext*, 54 | const std::vector& properties, 55 | const GetCallback& getter, const SetCallback& setter, 56 | const std::vector>& functions, 57 | const FinalizeCallback& finalizer, 58 | Value& obj); 59 | 60 | static bool SetPrivate(Value&, uint64_t, void*); 61 | static bool GetPrivate(const Value&, uint64_t&, void*&); 62 | 63 | static bool IsObject(const Value&); 64 | 65 | private: 66 | struct ObjectState { 67 | ObjectState(const GetCallback& g, const SetCallback& s, 68 | const FinalizeCallback& f, uint64_t d = 0, void* p = nullptr) : 69 | getter(g), setter(s), finalizer(f), data(d), ptr(p) {} 70 | 71 | const GetCallback getter; 72 | const SetCallback setter; 73 | const FinalizeCallback finalizer; 74 | Functions functions; 75 | uint64_t data; 76 | void* ptr; 77 | }; 78 | 79 | static bool Get(JSContext*, JS::HandleObject, JS::HandleId, JS::MutableHandleValue); 80 | static bool Set(JSContext*, JS::HandleObject, JS::HandleId, JS::MutableHandleValue, JS::ObjectOpResult&); 81 | static bool CallFunction(JSContext*, unsigned, JS::Value*); 82 | static void Finalize(JSFreeOp* fop, JSObject* obj); 83 | 84 | static ObjectState* GetState(JSContext* cx, JS::HandleObject obj); 85 | static ObjectState* GetState(JSObject* obj); 86 | static void SetState(JSObject* obj, ObjectState* state); 87 | 88 | static JSClassOps classOps_; 89 | static JSClass class_; 90 | }; 91 | 92 | }} 93 | 94 | #endif /* RS_JSAPI_OBJECT_H */ 95 | 96 | -------------------------------------------------------------------------------- /src/libjsapi/script.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #include "script.h" 26 | 27 | #include "context.h" 28 | 29 | rs::jsapi::Script::Script(Context& cx, const char* code) : 30 | cx_(cx), code_(code), script_(cx.getContext()) { 31 | } 32 | 33 | rs::jsapi::Script::~Script() { 34 | } 35 | 36 | bool rs::jsapi::Script::Compile() { 37 | JSAutoRequest ar(cx_); 38 | JS::CompileOptions options(cx_); 39 | auto status = JS_CompileScript(cx_, code_.c_str(), code_.length(), options, &script_); 40 | 41 | auto error = cx_.GetError(); 42 | if (!!error) { 43 | throw *error; 44 | } 45 | 46 | return status; 47 | } 48 | 49 | bool rs::jsapi::Script::Execute() { 50 | JSAutoRequest ar(cx_); 51 | if (!script_) { 52 | Compile(); 53 | } 54 | 55 | auto status = JS_ExecuteScript(cx_, script_); 56 | 57 | auto error = cx_.GetError(); 58 | if (!!error) { 59 | throw *error; 60 | } 61 | 62 | return status; 63 | } 64 | 65 | bool rs::jsapi::Script::Execute(Value& result) { 66 | JSAutoRequest ar(cx_); 67 | if (!script_) { 68 | Compile(); 69 | } 70 | 71 | auto status = JS_ExecuteScript(cx_, script_, result); 72 | 73 | auto error = cx_.GetError(); 74 | if (!!error) { 75 | throw *error; 76 | } 77 | 78 | return status; 79 | } -------------------------------------------------------------------------------- /src/libjsapi/script.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef RS_JSAPI_SCRIPT_H 26 | #define RS_JSAPI_SCRIPT_H 27 | 28 | #include 29 | 30 | #include 31 | 32 | #include "value.h" 33 | 34 | namespace rs { 35 | namespace jsapi { 36 | 37 | class Context; 38 | 39 | class Script final { 40 | public: 41 | Script(Context& cx, const char* code); 42 | Script(const Script&) = delete; 43 | ~Script(); 44 | 45 | Script& operator=(const Script&) = delete; 46 | 47 | bool Compile(); 48 | bool Execute(); 49 | bool Execute(Value& result); 50 | 51 | private: 52 | Context& cx_; 53 | const std::string code_; 54 | 55 | JS::RootedScript script_; 56 | }; 57 | 58 | }} 59 | 60 | #endif /* RS_JSAPI_SCRIPT_H */ -------------------------------------------------------------------------------- /src/libjsapi/tests/call_js_function_tests.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #include 26 | 27 | #include "../libjsapi.h" 28 | 29 | class CallJSFunctionTests : public ::testing::Test { 30 | protected: 31 | virtual void SetUp() { 32 | 33 | } 34 | 35 | virtual void TearDown() { 36 | 37 | } 38 | }; 39 | 40 | TEST_F(CallJSFunctionTests, test1) { 41 | rs::jsapi::Context cx; 42 | cx.Evaluate("var myfunc=function(){return 42;};"); 43 | 44 | rs::jsapi::Value result(cx); 45 | cx.Call("myfunc", result); 46 | 47 | ASSERT_TRUE(result.isNumber()); 48 | ASSERT_EQ(42, result.toNumber()); 49 | } 50 | 51 | TEST_F(CallJSFunctionTests, test2) { 52 | rs::jsapi::Context cx; 53 | cx.Evaluate("var myfunc1=function(){return 42;};"); 54 | cx.Evaluate("var myfunc2=function(){return 69;};"); 55 | 56 | rs::jsapi::Value result1(cx); 57 | cx.Call("myfunc1", result1); 58 | 59 | ASSERT_TRUE(result1.isNumber()); 60 | ASSERT_EQ(42, result1.toNumber()); 61 | 62 | rs::jsapi::Value result2(cx); 63 | cx.Call("myfunc2", result2); 64 | 65 | ASSERT_TRUE(result2.isNumber()); 66 | ASSERT_EQ(69, result2.toNumber()); 67 | } 68 | 69 | TEST_F(CallJSFunctionTests, test3) { 70 | rs::jsapi::Context cx; 71 | 72 | rs::jsapi::Value result1(cx); 73 | cx.Evaluate("var myfunc=function(){return 42;};(function(){return myfunc()+1;})();", result1); 74 | 75 | ASSERT_TRUE(result1.isNumber()); 76 | ASSERT_EQ(43, result1.toNumber()); 77 | 78 | rs::jsapi::Value result2(cx); 79 | cx.Call("myfunc", result2); 80 | 81 | ASSERT_TRUE(result2.isNumber()); 82 | ASSERT_EQ(42, result2.toNumber()); 83 | } 84 | 85 | TEST_F(CallJSFunctionTests, test4) { 86 | rs::jsapi::Context cx; 87 | 88 | cx.Evaluate("var count=7;var inc=function(){count++;};var get=function(){return count;};"); 89 | 90 | rs::jsapi::Value result1(cx); 91 | cx.Call("get", result1); 92 | 93 | ASSERT_TRUE(result1.isNumber()); 94 | ASSERT_EQ(7, result1.toNumber()); 95 | 96 | cx.Call("inc"); 97 | 98 | rs::jsapi::Value result2(cx); 99 | cx.Call("get", result2); 100 | 101 | ASSERT_TRUE(result2.isNumber()); 102 | ASSERT_EQ(8, result2.toNumber()); 103 | } 104 | 105 | TEST_F(CallJSFunctionTests, test5) { 106 | rs::jsapi::Context cx; 107 | cx.Evaluate("var myfunc=function(n){return n;};"); 108 | 109 | rs::jsapi::FunctionArguments args(cx); 110 | args.Append(3.14159); 111 | 112 | rs::jsapi::Value result(cx); 113 | cx.Call("myfunc", args, result); 114 | 115 | ASSERT_TRUE(result.isNumber()); 116 | ASSERT_FLOAT_EQ(3.14159, result.toNumber()); 117 | } 118 | 119 | TEST_F(CallJSFunctionTests, test5b) { 120 | rs::jsapi::Context cx; 121 | cx.Evaluate("var myfunc=function(n){return n;};"); 122 | 123 | rs::jsapi::FunctionArguments args(cx); 124 | args.Append(3); 125 | 126 | rs::jsapi::Value result(cx); 127 | cx.Call("myfunc", args, result); 128 | 129 | ASSERT_TRUE(result.isInt32()); 130 | ASSERT_EQ(3, result.toInt32()); 131 | } 132 | 133 | TEST_F(CallJSFunctionTests, test5c) { 134 | rs::jsapi::Context cx; 135 | cx.Evaluate("var myfunc=function(n){return n;};"); 136 | 137 | rs::jsapi::FunctionArguments args(cx); 138 | args.Append(7u); 139 | 140 | rs::jsapi::Value result(cx); 141 | cx.Call("myfunc", args, result); 142 | 143 | ASSERT_TRUE(result.isNumber()); 144 | ASSERT_EQ(7, result.toNumber()); 145 | } 146 | 147 | TEST_F(CallJSFunctionTests, test6) { 148 | rs::jsapi::Context cx; 149 | cx.Evaluate("var myfunc=function(n){return n;};"); 150 | 151 | rs::jsapi::FunctionArguments args(cx); 152 | args.Append("hello"); 153 | 154 | rs::jsapi::Value result(cx); 155 | cx.Call("myfunc", args, result); 156 | 157 | ASSERT_TRUE(result.isString()); 158 | ASSERT_STRCASEEQ("hello", result.ToString().c_str()); 159 | } 160 | 161 | TEST_F(CallJSFunctionTests, test7) { 162 | rs::jsapi::Context cx; 163 | cx.Evaluate("var myfunc=function(n){return n;};"); 164 | 165 | rs::jsapi::FunctionArguments args(cx); 166 | args.Append(true); 167 | 168 | rs::jsapi::Value result(cx); 169 | cx.Call("myfunc", args, result); 170 | 171 | ASSERT_TRUE(result.isBoolean()); 172 | ASSERT_TRUE(result.toBoolean()); 173 | } 174 | 175 | TEST_F(CallJSFunctionTests, test8) { 176 | rs::jsapi::Context cx; 177 | cx.Evaluate("var myfunc=function(n){return n;};"); 178 | 179 | rs::jsapi::FunctionArguments args(cx); 180 | args.Append(false); 181 | 182 | rs::jsapi::Value result(cx); 183 | cx.Call("myfunc", args, result); 184 | 185 | ASSERT_TRUE(result.isBoolean()); 186 | ASSERT_FALSE(result.toBoolean()); 187 | 188 | args.Clear(); 189 | ASSERT_TRUE(args.Empty()); 190 | 191 | args.Append(true); 192 | cx.Call("myfunc", args, result); 193 | 194 | ASSERT_TRUE(result.isBoolean()); 195 | ASSERT_TRUE(result.toBoolean()); 196 | } -------------------------------------------------------------------------------- /src/libjsapi/tests/function_arguments_tests.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #include 26 | 27 | #include "../libjsapi.h" 28 | 29 | class FunctionArgumentsTests : public ::testing::Test { 30 | protected: 31 | virtual void SetUp() { 32 | 33 | } 34 | 35 | virtual void TearDown() { 36 | 37 | } 38 | }; 39 | 40 | TEST_F(FunctionArgumentsTests, test1) { 41 | rs::jsapi::Context cx; 42 | rs::jsapi::FunctionArguments args(cx); 43 | 44 | ASSERT_EQ(0, args.getLength()); 45 | args.Clear(); 46 | ASSERT_EQ(0, args.getLength()); 47 | ASSERT_TRUE(args.Empty()); 48 | } 49 | 50 | TEST_F(FunctionArgumentsTests, test2) { 51 | rs::jsapi::Context cx; 52 | rs::jsapi::FunctionArguments args(cx); 53 | 54 | args.Append(99); 55 | 56 | ASSERT_EQ(1, args.getLength()); 57 | ASSERT_FALSE(args.Empty()); 58 | args.Clear(); 59 | ASSERT_TRUE(args.Empty()); 60 | } 61 | 62 | TEST_F(FunctionArgumentsTests, test3) { 63 | rs::jsapi::Context cx; 64 | rs::jsapi::FunctionArguments args(cx); 65 | 66 | args.Append(99); 67 | 68 | ASSERT_EQ(1, args.getLength()); 69 | ASSERT_FALSE(args.Empty()); 70 | ASSERT_TRUE(args[0].isInt32()); 71 | ASSERT_EQ(99, args[0].toInt32()); 72 | } 73 | 74 | TEST_F(FunctionArgumentsTests, test4) { 75 | rs::jsapi::Context cx; 76 | rs::jsapi::FunctionArguments args(cx); 77 | 78 | args.Append(99); 79 | ASSERT_EQ(1, args.getLength()); 80 | ASSERT_FALSE(args.Empty()); 81 | 82 | args.Append("Hello world"); 83 | ASSERT_EQ(2, args.getLength()); 84 | ASSERT_FALSE(args.Empty()); 85 | 86 | ASSERT_TRUE(args[0].isInt32()); 87 | ASSERT_EQ(99, args[0].toInt32()); 88 | 89 | ASSERT_TRUE(args[1].isString()); 90 | rs::jsapi::Value result(cx, args[1].toString()); 91 | ASSERT_TRUE(result.isString()); 92 | ASSERT_STREQ("Hello world", result.ToString().c_str()); 93 | 94 | args.Clear(); 95 | ASSERT_TRUE(args.Empty()); 96 | } 97 | 98 | TEST_F(FunctionArgumentsTests, test5) { 99 | rs::jsapi::Context cx; 100 | rs::jsapi::FunctionArguments args(cx); 101 | 102 | JS::RootedObject obj(cx.getContext()); 103 | args.Append(obj); 104 | ASSERT_EQ(1, args.getLength()); 105 | ASSERT_FALSE(args.Empty()); 106 | 107 | JS::RootedValue value(cx.getContext()); 108 | args.Append(value); 109 | ASSERT_EQ(2, args.getLength()); 110 | ASSERT_FALSE(args.Empty()); 111 | } 112 | 113 | TEST_F(FunctionArgumentsTests, test6) { 114 | rs::jsapi::Context cx; 115 | rs::jsapi::FunctionArguments args(cx); 116 | 117 | ASSERT_THROW({ 118 | args[0].isString(); 119 | }, rs::jsapi::FunctionArgumentsIndexException); 120 | 121 | bool thrown = false; 122 | try { 123 | args[0].isString(); 124 | } catch (const rs::jsapi::FunctionArgumentsIndexException& ex) { 125 | ASSERT_NE(nullptr, ex.what()); 126 | thrown = true; 127 | } 128 | 129 | args.Append(true); 130 | ASSERT_EQ(1, args.getLength()); 131 | ASSERT_TRUE(args[0].isBoolean()); 132 | ASSERT_THROW({ args[1].isBoolean(); }, rs::jsapi::FunctionArgumentsIndexException); 133 | } -------------------------------------------------------------------------------- /src/libjsapi/tests/global_property_tests.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #include 26 | 27 | #include "../libjsapi.h" 28 | 29 | class GlobalPropertyTests : public ::testing::Test { 30 | protected: 31 | virtual void SetUp() { 32 | 33 | } 34 | 35 | virtual void TearDown() { 36 | 37 | } 38 | }; 39 | 40 | TEST_F(GlobalPropertyTests, test1) { 41 | rs::jsapi::Context cx; 42 | ASSERT_TRUE(rs::jsapi::Global::DefineProperty(cx, "test1", 43 | [](JSContext* cx, unsigned argc, JS::Value* vp) { 44 | auto args = JS::CallArgsFromVp(argc, vp); 45 | args.rval().setInt32(42); 46 | return true; 47 | }, 48 | nullptr) 49 | ); 50 | 51 | rs::jsapi::Value result(cx); 52 | cx.Evaluate("(function(){return test1;})();", result); 53 | ASSERT_TRUE(result.isInt32()); 54 | ASSERT_EQ(42, result.toInt32()); 55 | } 56 | 57 | TEST_F(GlobalPropertyTests, test2) { 58 | rs::jsapi::Context cx; 59 | ASSERT_TRUE(rs::jsapi::Global::DefineProperty(cx, "test2", 60 | [](JSContext* cx, unsigned argc, JS::Value* vp) { 61 | auto args = JS::CallArgsFromVp(argc, vp); 62 | args.rval().setString(JS_NewStringCopyZ(cx, "hello")); 63 | return true; 64 | }, 65 | nullptr) 66 | ); 67 | 68 | rs::jsapi::Value result(cx); 69 | cx.Evaluate("(function(){return test2;})();", result); 70 | ASSERT_TRUE(result.isString()); 71 | ASSERT_STREQ("hello", result.ToString().c_str()); 72 | } 73 | 74 | TEST_F(GlobalPropertyTests, test3) { 75 | rs::jsapi::Context cx; 76 | ASSERT_TRUE(rs::jsapi::Global::DefineProperty(cx, "test3", 77 | [](JSContext* cx, unsigned argc, JS::Value* vp) { 78 | auto args = JS::CallArgsFromVp(argc, vp); 79 | args.rval().setObject(*JS_NewObject(cx, nullptr)); 80 | return true; 81 | }, 82 | nullptr) 83 | ); 84 | 85 | rs::jsapi::Value result(cx); 86 | cx.Evaluate("(function(){return test3;})();", result); 87 | ASSERT_TRUE(result.isObject()); 88 | } 89 | 90 | TEST_F(GlobalPropertyTests, test4) { 91 | rs::jsapi::Context cx; 92 | rs::jsapi::Value value(cx, 99); 93 | rs::jsapi::Global::DefineProperty(cx, "test4", value); 94 | 95 | rs::jsapi::Value result(cx); 96 | cx.Evaluate("(function(){return test4;})();", result); 97 | ASSERT_TRUE(result.isInt32()); 98 | ASSERT_EQ(99, result.toInt32()); 99 | } 100 | 101 | TEST_F(GlobalPropertyTests, test5) { 102 | rs::jsapi::Context cx; 103 | rs::jsapi::Value obj(cx); 104 | rs::jsapi::Object::Create(cx, 105 | {}, 106 | nullptr, 107 | nullptr, 108 | {}, 109 | nullptr, 110 | obj); 111 | ASSERT_TRUE(!!obj); 112 | 113 | rs::jsapi::Global::DefineProperty(cx, "test5", obj); 114 | 115 | rs::jsapi::Value result(cx); 116 | cx.Evaluate("(function(){return test5;})();", result); 117 | ASSERT_TRUE(result.isObject()); 118 | } -------------------------------------------------------------------------------- /src/libjsapi/value.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef RS_JSAPI_VALUE_H 26 | #define RS_JSAPI_VALUE_H 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | #include "context_state.h" 34 | 35 | namespace rs { 36 | namespace jsapi { 37 | 38 | class FunctionArguments; 39 | 40 | class Value final { 41 | public: 42 | Value(JSContext* cx); 43 | Value(JSContext* cx, const char* str); 44 | Value(JSContext* cx, const std::string& str); 45 | Value(JSContext* cx, int value); 46 | Value(JSContext* cx, bool value); 47 | Value(JSContext* cx, double value); 48 | Value(JSContext* cx, const JS::HandleValue& obj); 49 | Value(JSContext* cx, const JS::HandleObject& obj); 50 | Value(JSContext* cx, const JS::RootedObject& obj); 51 | Value(JSContext* cx, JSObject* obj); 52 | Value(JSContext* cx, JSString* obj); 53 | 54 | ~Value(); 55 | 56 | Value(const Value& rhs); 57 | 58 | Value& operator=(const Value& rhs) { 59 | cx_ = rhs.cx_; 60 | if (rhs.isObject_) { 61 | InitObjectRef(); 62 | object_ = rhs.object_; 63 | } else { 64 | InitValueRef(); 65 | value_ = rhs.value_; 66 | } 67 | 68 | return *this; 69 | } 70 | 71 | Value& operator=(int value) { set(value); return *this; } 72 | Value& operator=(double value) { set(value); return *this; } 73 | Value& operator=(bool value) { set(value); return *this; } 74 | Value& operator=(const char* str) { set(str); return *this; } 75 | Value& operator=(const std::string& str) { set(str); return *this; } 76 | Value& operator=(const JS::HandleValue& value) { set(value); return *this; } 77 | Value& operator=(const JS::HandleObject& value) { set(value); return *this; } 78 | Value& operator=(const JS::RootedObject& value) { set(value); return *this; } 79 | Value& operator=(const JSObject* value) { set(value); return *this; } 80 | Value& operator=(const JSString* value) { set(value); return *this; } 81 | 82 | operator const JS::Value&() const { ToValueRef(); return value_.get(); } 83 | operator const JS::HandleValue() const { ToValueRef(); return value_; } 84 | operator const JS::HandleObject() const { ToObjectRef(); return object_; } 85 | operator JS::MutableHandleValue() { InitValueRef(); return &value_; } 86 | operator JS::MutableHandleObject() { InitObjectRef(); return &object_; } 87 | 88 | bool operator !() const { return isObject() ? toObject() == nullptr : false; } 89 | 90 | JSContext* getContext() const; 91 | const JS::HandleValue getHandleValue() const { ToValueRef(); return value_; } 92 | const JS::HandleObject getHandleObject() const { ToObjectRef(); return object_; } 93 | 94 | void set(const char* str); 95 | void set(const std::string& str); 96 | void set(int value); 97 | void set(bool value); 98 | void set(double value); 99 | void set(const JS::HandleValue& value); 100 | void set(const JS::HandleObject& value); 101 | void set(const JS::RootedObject& value); 102 | void set(JSObject* value); 103 | void set(JSString* value); 104 | void set(const Value& value); 105 | void setUndefined(); 106 | void setNull(); 107 | 108 | bool isString() const; 109 | bool isNumber() const; 110 | bool isInt32() const; 111 | bool isBoolean() const; 112 | bool isObject() const; 113 | bool isNull() const; 114 | bool isUndefined() const; 115 | bool isArray() const; 116 | bool isFunction() const; 117 | 118 | JSString* toString() const; 119 | double toNumber() const; 120 | int32_t toInt32() const; 121 | bool toBoolean() const; 122 | JSObject* toObject() const; 123 | const JS::HandleValue toFunction() const; 124 | 125 | bool CallFunction(const FunctionArguments&, bool throwOnError = true); 126 | bool CallFunction(const FunctionArguments&, Value&, bool throwOnError = true); 127 | 128 | std::string ToString() const; 129 | 130 | protected: 131 | mutable JSContext* cx_; 132 | 133 | mutable bool isObject_; 134 | mutable JS::PersistentRootedValue value_; 135 | mutable JS::PersistentRootedObject object_; 136 | 137 | void InitValueRef() const; 138 | void InitObjectRef() const; 139 | void ToValueRef() const; 140 | void ToObjectRef() const; 141 | }; 142 | 143 | }} 144 | 145 | std::ostream& operator<<(std::ostream& os, const rs::jsapi::Value& value); 146 | 147 | #endif /* RS_JSAPI_VALUE_H */ 148 | -------------------------------------------------------------------------------- /src/libjsapi/vector_utils.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef RS_JSAPI_VECTOR_UTILS_H 26 | #define RS_JSAPI_VECTOR_UTILS_H 27 | 28 | #include 29 | 30 | class VectorUtils final { 31 | public: 32 | VectorUtils() = delete; 33 | 34 | template 35 | class ScopedVectorCleaner final { 36 | public: 37 | ScopedVectorCleaner(std::vector& v) : v_(v) {} 38 | ~ScopedVectorCleaner() { v_.clear(); } 39 | 40 | private: 41 | std::vector& v_; 42 | }; 43 | }; 44 | 45 | #endif /* RS_JSAPI_VECTOR_UTILS_H */ 46 | 47 | -------------------------------------------------------------------------------- /src/make/coverage.mk: -------------------------------------------------------------------------------- 1 | COVERAGE_FLAGS:=$(shell cd /tmp; echo 'int main(){}' | $(CXX) -x c++ -o /dev/null --coverage - > /dev/null 2>&1 && echo '--coverage') 2 | -------------------------------------------------------------------------------- /src/make/externals.mk: -------------------------------------------------------------------------------- 1 | # installed vars 2 | EXT_INSTALLED:=$(abspath ../../externals/installed) 3 | EXT_INSTALLED_LIB:=$(EXT_INSTALLED)/lib 4 | 5 | -------------------------------------------------------------------------------- /src/make/ldlibs.mk: -------------------------------------------------------------------------------- 1 | LDLIBS:=$(shell echo 'int main(){}' | $(CXX) -x c++ -o /dev/null -ldl - > /dev/null 2>&1 && echo '-ldl') 2 | LDLIBS+=$(shell echo 'int main(){}' | $(CXX) -x c++ -o /dev/null -lrt - > /dev/null 2>&1 && echo '-lrt') 3 | -------------------------------------------------------------------------------- /src/make/libmozglue.mk: -------------------------------------------------------------------------------- 1 | LIBMOZGLUE_DYLIB=$(1)/libmozglue.dylib 2 | LIBMOZGLUE_A=$(1)/libmozglue.a 3 | 4 | LIBMOZGLUE=$(shell if [ -e $(call LIBMOZGLUE_DYLIB, $(1)) ]; then echo $(call LIBMOZGLUE_DYLIB, $(1)); else echo "-Wl,--whole-archive $(call LIBMOZGLUE_A, $(1)) -Wl,--no-whole-archive"; fi) 5 | -------------------------------------------------------------------------------- /src/testlibjsapi/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # There exist several targets which are by default empty and which can be 3 | # used for execution of your targets. These targets are usually executed 4 | # before and after some main targets. They are: 5 | # 6 | # .build-pre: called before 'build' target 7 | # .build-post: called after 'build' target 8 | # .clean-pre: called before 'clean' target 9 | # .clean-post: called after 'clean' target 10 | # .clobber-pre: called before 'clobber' target 11 | # .clobber-post: called after 'clobber' target 12 | # .all-pre: called before 'all' target 13 | # .all-post: called after 'all' target 14 | # .help-pre: called before 'help' target 15 | # .help-post: called after 'help' target 16 | # 17 | # Targets beginning with '.' are not intended to be called on their own. 18 | # 19 | # Main targets can be executed directly, and they are: 20 | # 21 | # build build a specific configuration 22 | # clean remove built files from a configuration 23 | # clobber remove all built files 24 | # all build all configurations 25 | # help print help mesage 26 | # 27 | # Targets .build-impl, .clean-impl, .clobber-impl, .all-impl, and 28 | # .help-impl are implemented in nbproject/makefile-impl.mk. 29 | # 30 | # Available make variables: 31 | # 32 | # CND_BASEDIR base directory for relative paths 33 | # CND_DISTDIR default top distribution directory (build artifacts) 34 | # CND_BUILDDIR default top build directory (object files, ...) 35 | # CONF name of current configuration 36 | # CND_PLATFORM_${CONF} platform name (current configuration) 37 | # CND_ARTIFACT_DIR_${CONF} directory of build artifact (current configuration) 38 | # CND_ARTIFACT_NAME_${CONF} name of build artifact (current configuration) 39 | # CND_ARTIFACT_PATH_${CONF} path to build artifact (current configuration) 40 | # CND_PACKAGE_DIR_${CONF} directory of package (current configuration) 41 | # CND_PACKAGE_NAME_${CONF} name of package (current configuration) 42 | # CND_PACKAGE_PATH_${CONF} path to package (current configuration) 43 | # 44 | # NOCDDL 45 | 46 | 47 | # Environment 48 | MKDIR=mkdir 49 | CP=cp 50 | CCADMIN=CCadmin 51 | 52 | include ../make/coverage.mk 53 | include ../make/ldlibs.mk 54 | 55 | include ../make/libmozglue.mk 56 | LDLIBS:=$(call LIBMOZGLUE, ../../externals/installed/lib) $(LDLIBS) 57 | 58 | # build 59 | build: .build-post 60 | 61 | .build-pre: 62 | # Add your pre 'build' code here... 63 | 64 | .build-post: .build-impl 65 | # Add your post 'build' code here... 66 | 67 | 68 | # clean 69 | clean: .clean-post 70 | 71 | .clean-pre: 72 | # Add your pre 'clean' code here... 73 | 74 | .clean-post: .clean-impl 75 | # Add your post 'clean' code here... 76 | 77 | 78 | # clobber 79 | clobber: .clobber-post 80 | 81 | .clobber-pre: 82 | # Add your pre 'clobber' code here... 83 | 84 | .clobber-post: .clobber-impl 85 | # Add your post 'clobber' code here... 86 | 87 | 88 | # all 89 | all: .all-post 90 | 91 | .all-pre: 92 | # Add your pre 'all' code here... 93 | 94 | .all-post: .all-impl 95 | # Add your post 'all' code here... 96 | 97 | 98 | # build tests 99 | build-tests: .build-tests-post 100 | 101 | .build-tests-pre: 102 | # Add your pre 'build-tests' code here... 103 | 104 | .build-tests-post: .build-tests-impl 105 | # Add your post 'build-tests' code here... 106 | 107 | 108 | # run tests 109 | test: .test-post 110 | 111 | .test-pre: build-tests 112 | # Add your pre 'test' code here... 113 | 114 | .test-post: .test-impl 115 | # Add your post 'test' code here... 116 | 117 | 118 | # help 119 | help: .help-post 120 | 121 | .help-pre: 122 | # Add your pre 'help' code here... 123 | 124 | .help-post: .help-impl 125 | # Add your post 'help' code here... 126 | 127 | 128 | 129 | # include project implementation makefile 130 | include nbproject/Makefile-impl.mk 131 | 132 | # include project make variables 133 | include nbproject/Makefile-variables.mk 134 | -------------------------------------------------------------------------------- /src/testlibjsapi/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include "libjsapi.h" 30 | 31 | int main() { 32 | std::vector threads; 33 | 34 | threads.emplace_back([] { 35 | rs::jsapi::Context cx1; 36 | 37 | rs::jsapi::Script script1(cx1, "(function(){return 42;})();"); 38 | script1.Compile(); 39 | 40 | rs::jsapi::Value result(cx1); 41 | script1.Execute(result); 42 | 43 | if (result.isNumber()) { 44 | auto val = result.toNumber(); 45 | std::cout << val << std::endl; 46 | } 47 | 48 | rs::jsapi::Value result2(cx1); 49 | script1.Execute(result2); 50 | 51 | if (result.isNumber()) { 52 | auto val = result.toNumber(); 53 | std::cout << val << std::endl; 54 | } 55 | }); 56 | 57 | threads.emplace_back([] { 58 | rs::jsapi::Context cx2; 59 | 60 | rs::jsapi::Script script2(cx2, "(function(){return 3.14159;})();"); 61 | script2.Compile(); 62 | 63 | rs::jsapi::Value result(cx2); 64 | script2.Execute(result); 65 | 66 | if (result.isNumber()) { 67 | auto val = result.toNumber(); 68 | std::cout << val << std::endl; 69 | } 70 | 71 | rs::jsapi::Value result2(cx2); 72 | script2.Execute(result2); 73 | 74 | if (result2.isNumber()) { 75 | auto val = result2.toNumber(); 76 | std::cout << val << std::endl; 77 | } 78 | }); 79 | 80 | for (auto& thread : threads) { 81 | thread.join(); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/testlibjsapi/nbproject/Makefile-Debug.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated Makefile - do not edit! 3 | # 4 | # Edit the Makefile in the project folder instead (../Makefile). Each target 5 | # has a -pre and a -post target defined where you can add customized code. 6 | # 7 | # This makefile implements configuration specific macros and targets. 8 | 9 | 10 | # Environment 11 | MKDIR=mkdir 12 | CP=cp 13 | GREP=grep 14 | NM=nm 15 | CCADMIN=CCadmin 16 | RANLIB=ranlib 17 | CC=gcc 18 | CCC=g++ 19 | CXX=g++ 20 | FC=gfortran 21 | AS=as 22 | 23 | # Macros 24 | CND_PLATFORM=GNU-Linux-x86 25 | CND_DLIB_EXT=so 26 | CND_CONF=Debug 27 | CND_DISTDIR=dist 28 | CND_BUILDDIR=build 29 | 30 | # Include project Makefile 31 | include Makefile 32 | 33 | # Object Directory 34 | OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM} 35 | 36 | # Object Files 37 | OBJECTFILES= \ 38 | ${OBJECTDIR}/main.o 39 | 40 | 41 | # C Compiler Flags 42 | CFLAGS= 43 | 44 | # CC Compiler Flags 45 | CCFLAGS=$(COVERAGE_FLAGS) 46 | CXXFLAGS=$(COVERAGE_FLAGS) 47 | 48 | # Fortran Compiler Flags 49 | FFLAGS= 50 | 51 | # Assembler Flags 52 | ASFLAGS= 53 | 54 | # Link Libraries and Options 55 | LDLIBSOPTIONS=../libjsapi/dist/Debug/GNU-Linux-x86/libjsapi.a ../../externals/installed/lib/libjs_static.ajs -lpthread -lz $(LDLIBS) 56 | 57 | # Build Targets 58 | .build-conf: ${BUILD_SUBPROJECTS} 59 | "${MAKE}" -f nbproject/Makefile-${CND_CONF}.mk ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi 60 | 61 | ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi: ../libjsapi/dist/Debug/GNU-Linux-x86/libjsapi.a 62 | 63 | ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi: ${OBJECTFILES} 64 | ${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM} 65 | ${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi ${OBJECTFILES} ${LDLIBSOPTIONS} 66 | 67 | ${OBJECTDIR}/main.o: main.cpp 68 | ${MKDIR} -p ${OBJECTDIR} 69 | ${RM} "$@.d" 70 | $(COMPILE.cc) -g -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/main.o main.cpp 71 | 72 | # Subprojects 73 | .build-subprojects: 74 | cd ../libjsapi && ${MAKE} -f Makefile CONF=Debug 75 | 76 | # Clean Targets 77 | .clean-conf: ${CLEAN_SUBPROJECTS} 78 | ${RM} -r ${CND_BUILDDIR}/${CND_CONF} 79 | ${RM} ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi 80 | 81 | # Subprojects 82 | .clean-subprojects: 83 | cd ../libjsapi && ${MAKE} -f Makefile CONF=Debug clean 84 | 85 | # Enable dependency checking 86 | .dep.inc: .depcheck-impl 87 | 88 | include .dep.inc 89 | -------------------------------------------------------------------------------- /src/testlibjsapi/nbproject/Makefile-Release.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated Makefile - do not edit! 3 | # 4 | # Edit the Makefile in the project folder instead (../Makefile). Each target 5 | # has a -pre and a -post target defined where you can add customized code. 6 | # 7 | # This makefile implements configuration specific macros and targets. 8 | 9 | 10 | # Environment 11 | MKDIR=mkdir 12 | CP=cp 13 | GREP=grep 14 | NM=nm 15 | CCADMIN=CCadmin 16 | RANLIB=ranlib 17 | CC=gcc 18 | CCC=g++ 19 | CXX=g++ 20 | FC=gfortran 21 | AS=as 22 | 23 | # Macros 24 | CND_PLATFORM=GNU-Linux-x86 25 | CND_DLIB_EXT=so 26 | CND_CONF=Release 27 | CND_DISTDIR=dist 28 | CND_BUILDDIR=build 29 | 30 | # Include project Makefile 31 | include Makefile 32 | 33 | # Object Directory 34 | OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM} 35 | 36 | # Object Files 37 | OBJECTFILES= \ 38 | ${OBJECTDIR}/main.o 39 | 40 | 41 | # C Compiler Flags 42 | CFLAGS= 43 | 44 | # CC Compiler Flags 45 | CCFLAGS= 46 | CXXFLAGS= 47 | 48 | # Fortran Compiler Flags 49 | FFLAGS= 50 | 51 | # Assembler Flags 52 | ASFLAGS= 53 | 54 | # Link Libraries and Options 55 | LDLIBSOPTIONS=../libjsapi/dist/Release/GNU-Linux-x86/libjsapi.a ../../externals/installed/lib/libjs_static.ajs -lpthread -lz $(LDLIBS) 56 | 57 | # Build Targets 58 | .build-conf: ${BUILD_SUBPROJECTS} 59 | "${MAKE}" -f nbproject/Makefile-${CND_CONF}.mk ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi 60 | 61 | ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi: ../libjsapi/dist/Release/GNU-Linux-x86/libjsapi.a 62 | 63 | ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi: ${OBJECTFILES} 64 | ${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM} 65 | ${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi ${OBJECTFILES} ${LDLIBSOPTIONS} 66 | 67 | ${OBJECTDIR}/main.o: main.cpp 68 | ${MKDIR} -p ${OBJECTDIR} 69 | ${RM} "$@.d" 70 | $(COMPILE.cc) -O2 -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/main.o main.cpp 71 | 72 | # Subprojects 73 | .build-subprojects: 74 | cd ../libjsapi && ${MAKE} -f Makefile CONF=Release 75 | 76 | # Clean Targets 77 | .clean-conf: ${CLEAN_SUBPROJECTS} 78 | ${RM} -r ${CND_BUILDDIR}/${CND_CONF} 79 | ${RM} ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi 80 | 81 | # Subprojects 82 | .clean-subprojects: 83 | cd ../libjsapi && ${MAKE} -f Makefile CONF=Release clean 84 | 85 | # Enable dependency checking 86 | .dep.inc: .depcheck-impl 87 | 88 | include .dep.inc 89 | -------------------------------------------------------------------------------- /src/testlibjsapi/nbproject/Makefile-impl.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated Makefile - do not edit! 3 | # 4 | # Edit the Makefile in the project folder instead (../Makefile). Each target 5 | # has a pre- and a post- target defined where you can add customization code. 6 | # 7 | # This makefile implements macros and targets common to all configurations. 8 | # 9 | # NOCDDL 10 | 11 | 12 | # Building and Cleaning subprojects are done by default, but can be controlled with the SUB 13 | # macro. If SUB=no, subprojects will not be built or cleaned. The following macro 14 | # statements set BUILD_SUB-CONF and CLEAN_SUB-CONF to .build-reqprojects-conf 15 | # and .clean-reqprojects-conf unless SUB has the value 'no' 16 | SUB_no=NO 17 | SUBPROJECTS=${SUB_${SUB}} 18 | BUILD_SUBPROJECTS_=.build-subprojects 19 | BUILD_SUBPROJECTS_NO= 20 | BUILD_SUBPROJECTS=${BUILD_SUBPROJECTS_${SUBPROJECTS}} 21 | CLEAN_SUBPROJECTS_=.clean-subprojects 22 | CLEAN_SUBPROJECTS_NO= 23 | CLEAN_SUBPROJECTS=${CLEAN_SUBPROJECTS_${SUBPROJECTS}} 24 | 25 | 26 | # Project Name 27 | PROJECTNAME=testlibjsapi 28 | 29 | # Active Configuration 30 | DEFAULTCONF=Debug 31 | CONF=${DEFAULTCONF} 32 | 33 | # All Configurations 34 | ALLCONFS=Debug Release 35 | 36 | 37 | # build 38 | .build-impl: .build-pre .validate-impl .depcheck-impl 39 | @#echo "=> Running $@... Configuration=$(CONF)" 40 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .build-conf 41 | 42 | 43 | # clean 44 | .clean-impl: .clean-pre .validate-impl .depcheck-impl 45 | @#echo "=> Running $@... Configuration=$(CONF)" 46 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .clean-conf 47 | 48 | 49 | # clobber 50 | .clobber-impl: .clobber-pre .depcheck-impl 51 | @#echo "=> Running $@..." 52 | for CONF in ${ALLCONFS}; \ 53 | do \ 54 | "${MAKE}" -f nbproject/Makefile-$${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .clean-conf; \ 55 | done 56 | 57 | # all 58 | .all-impl: .all-pre .depcheck-impl 59 | @#echo "=> Running $@..." 60 | for CONF in ${ALLCONFS}; \ 61 | do \ 62 | "${MAKE}" -f nbproject/Makefile-$${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .build-conf; \ 63 | done 64 | 65 | # build tests 66 | .build-tests-impl: .build-impl .build-tests-pre 67 | @#echo "=> Running $@... Configuration=$(CONF)" 68 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .build-tests-conf 69 | 70 | # run tests 71 | .test-impl: .build-tests-impl .test-pre 72 | @#echo "=> Running $@... Configuration=$(CONF)" 73 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .test-conf 74 | 75 | # dependency checking support 76 | .depcheck-impl: 77 | @echo "# This code depends on make tool being used" >.dep.inc 78 | @if [ -n "${MAKE_VERSION}" ]; then \ 79 | echo "DEPFILES=\$$(wildcard \$$(addsuffix .d, \$${OBJECTFILES}))" >>.dep.inc; \ 80 | echo "ifneq (\$${DEPFILES},)" >>.dep.inc; \ 81 | echo "include \$${DEPFILES}" >>.dep.inc; \ 82 | echo "endif" >>.dep.inc; \ 83 | else \ 84 | echo ".KEEP_STATE:" >>.dep.inc; \ 85 | echo ".KEEP_STATE_FILE:.make.state.\$${CONF}" >>.dep.inc; \ 86 | fi 87 | 88 | # configuration validation 89 | .validate-impl: 90 | @if [ ! -f nbproject/Makefile-${CONF}.mk ]; \ 91 | then \ 92 | echo ""; \ 93 | echo "Error: can not find the makefile for configuration '${CONF}' in project ${PROJECTNAME}"; \ 94 | echo "See 'make help' for details."; \ 95 | echo "Current directory: " `pwd`; \ 96 | echo ""; \ 97 | fi 98 | @if [ ! -f nbproject/Makefile-${CONF}.mk ]; \ 99 | then \ 100 | exit 1; \ 101 | fi 102 | 103 | 104 | # help 105 | .help-impl: .help-pre 106 | @echo "This makefile supports the following configurations:" 107 | @echo " ${ALLCONFS}" 108 | @echo "" 109 | @echo "and the following targets:" 110 | @echo " build (default target)" 111 | @echo " clean" 112 | @echo " clobber" 113 | @echo " all" 114 | @echo " help" 115 | @echo "" 116 | @echo "Makefile Usage:" 117 | @echo " make [CONF=] [SUB=no] build" 118 | @echo " make [CONF=] [SUB=no] clean" 119 | @echo " make [SUB=no] clobber" 120 | @echo " make [SUB=no] all" 121 | @echo " make help" 122 | @echo "" 123 | @echo "Target 'build' will build a specific configuration and, unless 'SUB=no'," 124 | @echo " also build subprojects." 125 | @echo "Target 'clean' will clean a specific configuration and, unless 'SUB=no'," 126 | @echo " also clean subprojects." 127 | @echo "Target 'clobber' will remove all built files from all configurations and," 128 | @echo " unless 'SUB=no', also from subprojects." 129 | @echo "Target 'all' will will build all configurations and, unless 'SUB=no'," 130 | @echo " also build subprojects." 131 | @echo "Target 'help' prints this message." 132 | @echo "" 133 | 134 | -------------------------------------------------------------------------------- /src/testlibjsapi/nbproject/Makefile-variables.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated - do not edit! 3 | # 4 | # NOCDDL 5 | # 6 | CND_BASEDIR=`pwd` 7 | CND_BUILDDIR=build 8 | CND_DISTDIR=dist 9 | # Debug configuration 10 | CND_PLATFORM_Debug=GNU-Linux-x86 11 | CND_ARTIFACT_DIR_Debug=dist/Debug/GNU-Linux-x86 12 | CND_ARTIFACT_NAME_Debug=testlibjsapi 13 | CND_ARTIFACT_PATH_Debug=dist/Debug/GNU-Linux-x86/testlibjsapi 14 | CND_PACKAGE_DIR_Debug=dist/Debug/GNU-Linux-x86/package 15 | CND_PACKAGE_NAME_Debug=testlibjsapi.tar 16 | CND_PACKAGE_PATH_Debug=dist/Debug/GNU-Linux-x86/package/testlibjsapi.tar 17 | # Release configuration 18 | CND_PLATFORM_Release=GNU-Linux-x86 19 | CND_ARTIFACT_DIR_Release=dist/Release/GNU-Linux-x86 20 | CND_ARTIFACT_NAME_Release=testlibjsapi 21 | CND_ARTIFACT_PATH_Release=dist/Release/GNU-Linux-x86/testlibjsapi 22 | CND_PACKAGE_DIR_Release=dist/Release/GNU-Linux-x86/package 23 | CND_PACKAGE_NAME_Release=testlibjsapi.tar 24 | CND_PACKAGE_PATH_Release=dist/Release/GNU-Linux-x86/package/testlibjsapi.tar 25 | # 26 | # include compiler specific variables 27 | # 28 | # dmake command 29 | ROOT:sh = test -f nbproject/private/Makefile-variables.mk || \ 30 | (mkdir -p nbproject/private && touch nbproject/private/Makefile-variables.mk) 31 | # 32 | # gmake command 33 | .PHONY: $(shell test -f nbproject/private/Makefile-variables.mk || (mkdir -p nbproject/private && touch nbproject/private/Makefile-variables.mk)) 34 | # 35 | include nbproject/private/Makefile-variables.mk 36 | -------------------------------------------------------------------------------- /src/testlibjsapi/nbproject/Package-Debug.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | 3 | # 4 | # Generated - do not edit! 5 | # 6 | 7 | # Macros 8 | TOP=`pwd` 9 | CND_PLATFORM=GNU-Linux-x86 10 | CND_CONF=Debug 11 | CND_DISTDIR=dist 12 | CND_BUILDDIR=build 13 | CND_DLIB_EXT=so 14 | NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging 15 | TMPDIRNAME=tmp-packaging 16 | OUTPUT_PATH=${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi 17 | OUTPUT_BASENAME=testlibjsapi 18 | PACKAGE_TOP_DIR=testlibjsapi/ 19 | 20 | # Functions 21 | function checkReturnCode 22 | { 23 | rc=$? 24 | if [ $rc != 0 ] 25 | then 26 | exit $rc 27 | fi 28 | } 29 | function makeDirectory 30 | # $1 directory path 31 | # $2 permission (optional) 32 | { 33 | mkdir -p "$1" 34 | checkReturnCode 35 | if [ "$2" != "" ] 36 | then 37 | chmod $2 "$1" 38 | checkReturnCode 39 | fi 40 | } 41 | function copyFileToTmpDir 42 | # $1 from-file path 43 | # $2 to-file path 44 | # $3 permission 45 | { 46 | cp "$1" "$2" 47 | checkReturnCode 48 | if [ "$3" != "" ] 49 | then 50 | chmod $3 "$2" 51 | checkReturnCode 52 | fi 53 | } 54 | 55 | # Setup 56 | cd "${TOP}" 57 | mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package 58 | rm -rf ${NBTMPDIR} 59 | mkdir -p ${NBTMPDIR} 60 | 61 | # Copy files and create directories and links 62 | cd "${TOP}" 63 | makeDirectory "${NBTMPDIR}/testlibjsapi/bin" 64 | copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755 65 | 66 | 67 | # Generate tar file 68 | cd "${TOP}" 69 | rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/testlibjsapi.tar 70 | cd ${NBTMPDIR} 71 | tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/testlibjsapi.tar * 72 | checkReturnCode 73 | 74 | # Cleanup 75 | cd "${TOP}" 76 | rm -rf ${NBTMPDIR} 77 | -------------------------------------------------------------------------------- /src/testlibjsapi/nbproject/Package-Release.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | 3 | # 4 | # Generated - do not edit! 5 | # 6 | 7 | # Macros 8 | TOP=`pwd` 9 | CND_PLATFORM=GNU-Linux-x86 10 | CND_CONF=Release 11 | CND_DISTDIR=dist 12 | CND_BUILDDIR=build 13 | CND_DLIB_EXT=so 14 | NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging 15 | TMPDIRNAME=tmp-packaging 16 | OUTPUT_PATH=${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi 17 | OUTPUT_BASENAME=testlibjsapi 18 | PACKAGE_TOP_DIR=testlibjsapi/ 19 | 20 | # Functions 21 | function checkReturnCode 22 | { 23 | rc=$? 24 | if [ $rc != 0 ] 25 | then 26 | exit $rc 27 | fi 28 | } 29 | function makeDirectory 30 | # $1 directory path 31 | # $2 permission (optional) 32 | { 33 | mkdir -p "$1" 34 | checkReturnCode 35 | if [ "$2" != "" ] 36 | then 37 | chmod $2 "$1" 38 | checkReturnCode 39 | fi 40 | } 41 | function copyFileToTmpDir 42 | # $1 from-file path 43 | # $2 to-file path 44 | # $3 permission 45 | { 46 | cp "$1" "$2" 47 | checkReturnCode 48 | if [ "$3" != "" ] 49 | then 50 | chmod $3 "$2" 51 | checkReturnCode 52 | fi 53 | } 54 | 55 | # Setup 56 | cd "${TOP}" 57 | mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package 58 | rm -rf ${NBTMPDIR} 59 | mkdir -p ${NBTMPDIR} 60 | 61 | # Copy files and create directories and links 62 | cd "${TOP}" 63 | makeDirectory "${NBTMPDIR}/testlibjsapi/bin" 64 | copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755 65 | 66 | 67 | # Generate tar file 68 | cd "${TOP}" 69 | rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/testlibjsapi.tar 70 | cd ${NBTMPDIR} 71 | tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/testlibjsapi.tar * 72 | checkReturnCode 73 | 74 | # Cleanup 75 | cd "${TOP}" 76 | rm -rf ${NBTMPDIR} 77 | -------------------------------------------------------------------------------- /src/testlibjsapi/nbproject/configurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 11 | 12 | 15 | main.cpp 16 | 17 | 21 | 22 | 26 | Makefile 27 | 28 | 29 | Makefile 30 | 31 | 32 | 33 | default 34 | true 35 | false 36 | 37 | 38 | 39 | 8 40 | 41 | ../libjsapi 42 | ../../externals/installed/include/mozjs 43 | 44 | $(COVERAGE_FLAGS) 45 | 46 | 47 | 48 | 49 | 58 | 59 | 60 | ../../externals/installed/lib/libjs_static.ajs 61 | PosixThreads 62 | DataCompression 63 | $(LDLIBS) 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | default 73 | true 74 | false 75 | 76 | 77 | 78 | 5 79 | 80 | 81 | 5 82 | 8 83 | 84 | ../libjsapi 85 | ../../externals/installed/include/mozjs 86 | 87 | 88 | 89 | 5 90 | 91 | 92 | 5 93 | 94 | 95 | 96 | 97 | 106 | 107 | 108 | ../../externals/installed/lib/libjs_static.ajs 109 | PosixThreads 110 | DataCompression 111 | $(LDLIBS) 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /src/testlibjsapi/nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.cnd.makeproject 4 | 5 | 6 | testlibjsapi 7 | 8 | cpp 9 | 10 | UTF-8 11 | 12 | ../libjsapi 13 | 14 | 15 | 16 | 17 | Debug 18 | 1 19 | 20 | 21 | Release 22 | 1 23 | 24 | 25 | 26 | false 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # There exist several targets which are by default empty and which can be 3 | # used for execution of your targets. These targets are usually executed 4 | # before and after some main targets. They are: 5 | # 6 | # .build-pre: called before 'build' target 7 | # .build-post: called after 'build' target 8 | # .clean-pre: called before 'clean' target 9 | # .clean-post: called after 'clean' target 10 | # .clobber-pre: called before 'clobber' target 11 | # .clobber-post: called after 'clobber' target 12 | # .all-pre: called before 'all' target 13 | # .all-post: called after 'all' target 14 | # .help-pre: called before 'help' target 15 | # .help-post: called after 'help' target 16 | # 17 | # Targets beginning with '.' are not intended to be called on their own. 18 | # 19 | # Main targets can be executed directly, and they are: 20 | # 21 | # build build a specific configuration 22 | # clean remove built files from a configuration 23 | # clobber remove all built files 24 | # all build all configurations 25 | # help print help mesage 26 | # 27 | # Targets .build-impl, .clean-impl, .clobber-impl, .all-impl, and 28 | # .help-impl are implemented in nbproject/makefile-impl.mk. 29 | # 30 | # Available make variables: 31 | # 32 | # CND_BASEDIR base directory for relative paths 33 | # CND_DISTDIR default top distribution directory (build artifacts) 34 | # CND_BUILDDIR default top build directory (object files, ...) 35 | # CONF name of current configuration 36 | # CND_PLATFORM_${CONF} platform name (current configuration) 37 | # CND_ARTIFACT_DIR_${CONF} directory of build artifact (current configuration) 38 | # CND_ARTIFACT_NAME_${CONF} name of build artifact (current configuration) 39 | # CND_ARTIFACT_PATH_${CONF} path to build artifact (current configuration) 40 | # CND_PACKAGE_DIR_${CONF} directory of package (current configuration) 41 | # CND_PACKAGE_NAME_${CONF} name of package (current configuration) 42 | # CND_PACKAGE_PATH_${CONF} path to package (current configuration) 43 | # 44 | # NOCDDL 45 | 46 | 47 | # Environment 48 | MKDIR=mkdir 49 | CP=cp 50 | CCADMIN=CCadmin 51 | 52 | include ../make/coverage.mk 53 | include ../make/ldlibs.mk 54 | 55 | include ../make/libmozglue.mk 56 | LDLIBS:=$(call LIBMOZGLUE, ../../externals/installed/lib) $(LDLIBS) 57 | 58 | # build 59 | build: .build-post 60 | 61 | .build-pre: 62 | # Add your pre 'build' code here... 63 | 64 | .build-post: .build-impl 65 | # Add your post 'build' code here... 66 | 67 | 68 | # clean 69 | clean: .clean-post 70 | 71 | .clean-pre: 72 | # Add your pre 'clean' code here... 73 | 74 | .clean-post: .clean-impl 75 | # Add your post 'clean' code here... 76 | 77 | 78 | # clobber 79 | clobber: .clobber-post 80 | 81 | .clobber-pre: 82 | # Add your pre 'clobber' code here... 83 | 84 | .clobber-post: .clobber-impl 85 | # Add your post 'clobber' code here... 86 | 87 | 88 | # all 89 | all: .all-post 90 | 91 | .all-pre: 92 | # Add your pre 'all' code here... 93 | 94 | .all-post: .all-impl 95 | # Add your post 'all' code here... 96 | 97 | 98 | # build tests 99 | build-tests: .build-tests-post 100 | 101 | .build-tests-pre: 102 | # Add your pre 'build-tests' code here... 103 | 104 | .build-tests-post: .build-tests-impl 105 | # Add your post 'build-tests' code here... 106 | 107 | 108 | # run tests 109 | test: .test-post 110 | 111 | .test-pre: build-tests 112 | # Add your pre 'test' code here... 113 | 114 | .test-post: .test-impl 115 | # Add your post 'test' code here... 116 | 117 | 118 | # help 119 | help: .help-post 120 | 121 | .help-pre: 122 | # Add your pre 'help' code here... 123 | 124 | .help-post: .help-impl 125 | # Add your post 'help' code here... 126 | 127 | 128 | 129 | # include project implementation makefile 130 | include nbproject/Makefile-impl.mk 131 | 132 | # include project make variables 133 | include nbproject/Makefile-variables.mk 134 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/Southport_Sunset.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RipcordSoftware/libjsapi/57369128d9da6eb84ff3b9c5b9791aee4ae34ce4/src/testlibjsapi_gtkmm/Southport_Sunset.PNG -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/application.cpp: -------------------------------------------------------------------------------- 1 | #include "application.h" 2 | 3 | #include "window.h" 4 | 5 | Application::Application(rs::jsapi::Context& cx, const char* appName, int argc, char** argv) : cx_(cx), obj_(cx) { 6 | app_ = Gtk::Application::create(argc, argv, appName); 7 | 8 | rs::jsapi::Object::Create(cx, {}, nullptr, nullptr, 9 | { { "run", std::bind(&Application::Run, this, std::placeholders::_1, std::placeholders::_2) } }, 10 | std::bind(&Application::Finalizer, this), obj_); 11 | } 12 | 13 | void Application::Run(const std::vector& args, rs::jsapi::Value& result) { 14 | if (args.size() > 0 && args[0].isObject()) { 15 | auto window = Window::getWindowFromValue(args[0]); 16 | if (window != nullptr) { 17 | app_->run(*window); 18 | result = true; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/application.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef APPLICATION_H 26 | #define APPLICATION_H 27 | 28 | #include 29 | 30 | #include "libjsapi.h" 31 | 32 | class Application { 33 | public: 34 | Application(rs::jsapi::Context& cx, const char* appName, int argc = 0, char** argv = nullptr); 35 | 36 | void Run(const std::vector& args, rs::jsapi::Value& result); 37 | 38 | operator rs::jsapi::Value&() { return obj_; } 39 | 40 | private: 41 | void Finalizer() { 42 | delete this; 43 | } 44 | 45 | rs::jsapi::Context& cx_; 46 | rs::jsapi::Value obj_; 47 | Glib::RefPtr app_; 48 | }; 49 | 50 | #endif /* APPLICATION_H */ 51 | 52 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/builder.cpp: -------------------------------------------------------------------------------- 1 | #include "builder.h" 2 | 3 | #include 4 | 5 | #include "window.h" 6 | #include "button.h" 7 | #include "check_button.h" 8 | #include "label.h" 9 | #include "entry.h" 10 | #include "drawing_area.h" 11 | 12 | class BuilderException : public std::exception { 13 | public: 14 | BuilderException(const char* msg) : msg_(msg) {} 15 | 16 | virtual const char* what() const noexcept override { return msg_.c_str(); } 17 | 18 | private: 19 | std::string msg_; 20 | }; 21 | 22 | Builder::Builder(rs::jsapi::Context& cx) : cx_(cx), obj_(cx), null_(cx) { 23 | rs::jsapi::Object::Create(cx, {}, nullptr, nullptr, { 24 | { "addFromFile", std::bind(&Builder::AddFromFile, this, std::placeholders::_1, std::placeholders::_2) }, 25 | { "getWidget", std::bind(&Builder::GetWidget, this, std::placeholders::_1, std::placeholders::_2) }, 26 | { "getWindow", std::bind(&Builder::GetWindow, this, std::placeholders::_1, std::placeholders::_2) }, 27 | { "getButton", std::bind(&Builder::GetButton, this, std::placeholders::_1, std::placeholders::_2) }, 28 | { "getCheckButton", std::bind(&Builder::GetCheckButton, this, std::placeholders::_1, std::placeholders::_2) }, 29 | { "getLabel", std::bind(&Builder::GetLabel, this, std::placeholders::_1, std::placeholders::_2) }, 30 | { "getEntry", std::bind(&Builder::GetEntry, this, std::placeholders::_1, std::placeholders::_2) }, 31 | { "getDrawingArea", std::bind(&Builder::GetDrawingArea, this, std::placeholders::_1, std::placeholders::_2) } 32 | }, std::bind(&Builder::Finalizer, this), obj_); 33 | } 34 | 35 | void Builder::AddFromFile(const std::vector& args, rs::jsapi::Value& result) { 36 | if (args.size() > 0 && args[0].isString()) { 37 | builder_ = Gtk::Builder::create(); 38 | 39 | try { 40 | builder_->add_from_file(args[0].ToString()); 41 | result = *this; 42 | } catch (const Glib::Exception& ex) { 43 | throw BuilderException(ex.what().c_str()); 44 | } 45 | } 46 | } 47 | 48 | void Builder::GetWindow(const std::vector& args, rs::jsapi::Value& result) { 49 | auto widget = FindWidget(args); 50 | if (widget && GTK_IS_WINDOW(widget->gobj())) { 51 | auto window = reinterpret_cast(widget); 52 | result = *(new Window(cx_, window)); 53 | } 54 | } 55 | 56 | void Builder::GetButton(const std::vector& args, rs::jsapi::Value& result) { 57 | auto widget = FindWidget(args); 58 | if (widget && GTK_IS_BUTTON(widget->gobj())) { 59 | auto button = reinterpret_cast(widget); 60 | result = *(new Button(cx_, button)); 61 | } 62 | } 63 | 64 | void Builder::GetCheckButton(const std::vector& args, rs::jsapi::Value& result) { 65 | auto widget = FindWidget(args); 66 | if (widget && GTK_IS_CHECK_BUTTON(widget->gobj())) { 67 | auto button = reinterpret_cast(widget); 68 | result = *(new CheckButton(cx_, button)); 69 | } 70 | } 71 | 72 | void Builder::GetLabel(const std::vector& args, rs::jsapi::Value& result) { 73 | auto widget = FindWidget(args); 74 | if (widget && GTK_IS_LABEL(widget->gobj())) { 75 | auto label = reinterpret_cast(widget); 76 | result = *(new Label(cx_, label)); 77 | } 78 | } 79 | 80 | void Builder::GetEntry(const std::vector& args, rs::jsapi::Value& result) { 81 | auto widget = FindWidget(args); 82 | if (widget && GTK_IS_ENTRY(widget->gobj())) { 83 | auto label = reinterpret_cast(widget); 84 | result = *(new Entry(cx_, label)); 85 | } 86 | } 87 | 88 | void Builder::GetDrawingArea(const std::vector& args, rs::jsapi::Value& result) { 89 | auto widget = FindWidget(args); 90 | if (widget && GTK_IS_DRAWING_AREA(widget->gobj())) { 91 | auto area = reinterpret_cast(widget); 92 | result = *(new DrawingArea(cx_, area)); 93 | } 94 | } 95 | 96 | Gtk::Widget* Builder::FindWidget(const std::vector& args) { 97 | Gtk::Widget* widget = nullptr; 98 | if (args.size() > 0 && args[0].isString()) { 99 | builder_->get_widget(args[0].ToString(), widget); 100 | } 101 | return widget; 102 | } 103 | 104 | void Builder::GetWidget(const std::vector& args, rs::jsapi::Value& result) { 105 | auto widget = FindWidget(args); 106 | if (widget && GTK_IS_CHECK_BUTTON(widget->gobj())) { 107 | GetCheckButton(args, result); 108 | } else if (widget && GTK_IS_BUTTON(widget->gobj())) { 109 | GetButton(args, result); 110 | } else if (widget && GTK_IS_ENTRY(widget->gobj())) { 111 | GetEntry(args, result); 112 | } else if (widget && GTK_IS_LABEL(widget->gobj())) { 113 | GetLabel(args, result); 114 | } else if (widget && GTK_IS_WINDOW(widget->gobj())) { 115 | GetWindow(args, result); 116 | } else if (widget && GTK_IS_DRAWING_AREA(widget->gobj())) { 117 | GetDrawingArea(args, result); 118 | } 119 | } -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/builder.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef BUILDER_H 26 | #define BUILDER_H 27 | 28 | #include 29 | 30 | #include "libjsapi.h" 31 | 32 | class Builder { 33 | public: 34 | Builder(rs::jsapi::Context& cx); 35 | 36 | void AddFromFile(const std::vector& args, rs::jsapi::Value& result); 37 | void GetWidget(const std::vector& args, rs::jsapi::Value& result); 38 | void GetWindow(const std::vector& args, rs::jsapi::Value& result); 39 | void GetButton(const std::vector& args, rs::jsapi::Value& result); 40 | void GetCheckButton(const std::vector& args, rs::jsapi::Value& result); 41 | void GetLabel(const std::vector& args, rs::jsapi::Value& result); 42 | void GetEntry(const std::vector& args, rs::jsapi::Value& result); 43 | void GetDrawingArea(const std::vector& args, rs::jsapi::Value& result); 44 | 45 | operator rs::jsapi::Value&() { return obj_; } 46 | 47 | private: 48 | void Finalizer() { 49 | delete this; 50 | } 51 | 52 | Gtk::Widget* FindWidget(const std::vector& args); 53 | 54 | rs::jsapi::Context& cx_; 55 | rs::jsapi::Value obj_; 56 | Glib::RefPtr builder_; 57 | 58 | rs::jsapi::Value null_; 59 | }; 60 | 61 | #endif /* BUILDER_H */ 62 | 63 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/button.cpp: -------------------------------------------------------------------------------- 1 | #include "button.h" 2 | 3 | Button::Button(rs::jsapi::Context& cx, Gtk::Button* button) : cx_(cx), button_(button), obj_(cx), widget_(button, obj_) { 4 | auto functions = widget_.GetFunctions(); 5 | 6 | functions.emplace_back("getLabel", std::bind(&Button::SetLabel, this, std::placeholders::_1, std::placeholders::_2)); 7 | functions.emplace_back("setLabel", std::bind(&Button::SetLabel, this, std::placeholders::_1, std::placeholders::_2)); 8 | functions.emplace_back("onClick", std::bind(&Button::OnClick, this, std::placeholders::_1, std::placeholders::_2)); 9 | functions.emplace_back("setFocus", std::bind(&Button::SetFocus, this, std::placeholders::_1, std::placeholders::_2)); 10 | functions.emplace_back("focus", std::bind(&Button::SetFocus, this, std::placeholders::_1, std::placeholders::_2)); 11 | 12 | rs::jsapi::Object::Create(cx, {}, nullptr, nullptr, 13 | functions, std::bind(&Button::Finalizer, this), obj_); 14 | } 15 | 16 | void Button::GetLabel(const std::vector& args, rs::jsapi::Value& result) { 17 | result = button_->get_label(); 18 | } 19 | 20 | void Button::SetLabel(const std::vector& args, rs::jsapi::Value& result) { 21 | button_->set_label(args[0].ToString()); 22 | result = *this; 23 | } 24 | 25 | void Button::SetFocus(const std::vector& args, rs::jsapi::Value& result) { 26 | button_->get_toplevel()->set_focus_child(*button_); 27 | result = *this; 28 | } 29 | 30 | void Button::OnClick(const std::vector& args, rs::jsapi::Value& result) { 31 | if (args.size() > 0) { 32 | if (args[0].isFunction()) { 33 | onClick_ = args[0].toFunction(); 34 | button_->signal_clicked().connect(sigc::mem_fun(*this, &Button::OnButtonClicked)); 35 | } else { 36 | onClick_.setUndefined(); 37 | } 38 | } 39 | result = *this; 40 | } 41 | 42 | void Button::OnButtonClicked() { 43 | if (onClick_.isFunction()) { 44 | rs::jsapi::FunctionArguments args(cx_); 45 | rs::jsapi::Value result(cx_); 46 | onClick_.CallFunction(args, result); 47 | } 48 | } -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/button.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef BUTTON_H 26 | #define BUTTON_H 27 | 28 | #include "libjsapi.h" 29 | 30 | #include "widget.h" 31 | 32 | class Button { 33 | public: 34 | 35 | Button(rs::jsapi::Context& cx, Gtk::Button* button); 36 | 37 | void GetLabel(const std::vector& args, rs::jsapi::Value& result); 38 | void SetLabel(const std::vector& args, rs::jsapi::Value& result); 39 | void OnClick(const std::vector& args, rs::jsapi::Value& result); 40 | void SetFocus(const std::vector& args, rs::jsapi::Value& result); 41 | 42 | operator rs::jsapi::Value&() { return obj_; } 43 | 44 | private: 45 | void Finalizer() { 46 | delete this; 47 | } 48 | 49 | void OnButtonClicked(); 50 | 51 | rs::jsapi::Context& cx_; 52 | rs::jsapi::Value obj_; 53 | Gtk::Button* button_; 54 | Widget widget_; 55 | 56 | rs::jsapi::Value onClick_ { cx_ }; 57 | }; 58 | 59 | #endif /* BUTTON_H */ 60 | 61 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/check_button.cpp: -------------------------------------------------------------------------------- 1 | #include "check_button.h" 2 | 3 | #include 4 | 5 | CheckButton::CheckButton(rs::jsapi::Context& cx, Gtk::CheckButton* button) : cx_(cx), button_(button), obj_(cx), widget_(button, obj_) { 6 | auto functions = widget_.GetFunctions(); 7 | 8 | functions.emplace_back("getActive", std::bind(&CheckButton::GetActive, this, std::placeholders::_1, std::placeholders::_2)); 9 | functions.emplace_back("setActive", std::bind(&CheckButton::SetActive, this, std::placeholders::_1, std::placeholders::_2)); 10 | functions.emplace_back("getLabel", std::bind(&CheckButton::SetLabel, this, std::placeholders::_1, std::placeholders::_2)); 11 | functions.emplace_back("setLabel", std::bind(&CheckButton::SetLabel, this, std::placeholders::_1, std::placeholders::_2)); 12 | functions.emplace_back("onClick", std::bind(&CheckButton::OnClick, this, std::placeholders::_1, std::placeholders::_2)); 13 | 14 | rs::jsapi::Object::Create(cx, { "checked" }, 15 | std::bind(&CheckButton::GetCallback, this, std::placeholders::_1, std::placeholders::_2), 16 | std::bind(&CheckButton::SetCallback, this, std::placeholders::_1, std::placeholders::_2), 17 | functions, std::bind(&CheckButton::Finalizer, this), obj_); 18 | } 19 | 20 | void CheckButton::GetCallback(const char* name, rs::jsapi::Value& value) { 21 | if (std::strcmp(name, "checked") == 0) { 22 | GetActive({}, value); 23 | } 24 | } 25 | 26 | void CheckButton::SetCallback(const char* name, const rs::jsapi::Value& value) { 27 | if (std::strcmp(name, "checked") == 0) { 28 | std::vector args; 29 | args.push_back(value); 30 | rs::jsapi::Value result(cx_); 31 | SetActive(args, result); 32 | } 33 | } 34 | 35 | void CheckButton::GetActive(const std::vector& args, rs::jsapi::Value& result) { 36 | result = button_->get_active(); 37 | } 38 | 39 | void CheckButton::SetActive(const std::vector& args, rs::jsapi::Value& result) { 40 | if (args.size() > 0) { 41 | button_->set_active(args[0].toBoolean()); 42 | } 43 | result = *this; 44 | } 45 | 46 | void CheckButton::GetLabel(const std::vector& args, rs::jsapi::Value& result) { 47 | result = button_->get_label(); 48 | } 49 | 50 | void CheckButton::SetLabel(const std::vector& args, rs::jsapi::Value& result) { 51 | if (args.size() > 0) { 52 | button_->set_label(args[0].ToString()); 53 | } 54 | result = *this; 55 | } 56 | 57 | void CheckButton::OnClick(const std::vector& args, rs::jsapi::Value& result) { 58 | if (args.size() > 0) { 59 | if (args[0].isFunction()) { 60 | onClick_ = args[0].toFunction(); 61 | button_->signal_clicked().connect(sigc::mem_fun(*this, &CheckButton::OnButtonClicked)); 62 | } else { 63 | onClick_.setUndefined(); 64 | } 65 | } 66 | result = *this; 67 | } 68 | 69 | void CheckButton::OnButtonClicked() { 70 | if (onClick_.isFunction()) { 71 | rs::jsapi::FunctionArguments args(cx_); 72 | rs::jsapi::Value result(cx_); 73 | onClick_.CallFunction(args, result); 74 | } 75 | } -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/check_button.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef CHECK_BUTTON_H 26 | #define CHECK_BUTTON_H 27 | 28 | #include "libjsapi.h" 29 | 30 | #include "widget.h" 31 | 32 | class CheckButton { 33 | public: 34 | CheckButton(rs::jsapi::Context& cx, Gtk::CheckButton* button); 35 | 36 | void GetActive(const std::vector& args, rs::jsapi::Value& result); 37 | void SetActive(const std::vector& args, rs::jsapi::Value& result); 38 | void GetLabel(const std::vector& args, rs::jsapi::Value& result); 39 | void SetLabel(const std::vector& args, rs::jsapi::Value& result); 40 | void OnClick(const std::vector& args, rs::jsapi::Value& result); 41 | 42 | operator rs::jsapi::Value&() { return obj_; } 43 | 44 | private: 45 | void Finalizer() { 46 | delete this; 47 | } 48 | 49 | void GetCallback(const char* name, rs::jsapi::Value& value); 50 | void SetCallback(const char* name, const rs::jsapi::Value& value); 51 | 52 | void OnButtonClicked(); 53 | 54 | rs::jsapi::Context& cx_; 55 | rs::jsapi::Value obj_; 56 | Gtk::CheckButton* button_; 57 | Widget widget_; 58 | 59 | rs::jsapi::Value onClick_ { cx_ }; 60 | 61 | }; 62 | 63 | #endif /* CHECK_BUTTON_H */ 64 | 65 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/dom.js: -------------------------------------------------------------------------------- 1 | var document = { 2 | getElementById: function(id) { return builder.getWidget(id); } 3 | } 4 | 5 | // pretend we are jQuery (just a little) 6 | var $ = document.getElementById; -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/drawing_area.glade: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | False 7 | 8 | 9 | 1024 10 | 768 11 | True 12 | False 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/drawing_area.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef DRAWING_AREA_H 26 | #define DRAWING_AREA_H 27 | 28 | #include "libjsapi.h" 29 | 30 | #include "widget.h" 31 | 32 | class DrawingArea { 33 | public: 34 | DrawingArea(rs::jsapi::Context& cx, Gtk::DrawingArea* entry); 35 | 36 | void SetSourceRgb(const std::vector& args, rs::jsapi::Value& result); 37 | void SetLineWidth(const std::vector& args, rs::jsapi::Value& result); 38 | void MoveTo(const std::vector& args, rs::jsapi::Value& result); 39 | void LineTo(const std::vector& args, rs::jsapi::Value& result); 40 | void Stroke(const std::vector& args, rs::jsapi::Value& result); 41 | void OnDraw(const std::vector& args, rs::jsapi::Value& result); 42 | void CreateImageSurface(const std::vector& args, rs::jsapi::Value& result); 43 | void CreateImageSurfaceFromPng(const std::vector& args, rs::jsapi::Value& result); 44 | void PutImageData(const std::vector& args, rs::jsapi::Value& result); 45 | 46 | operator rs::jsapi::Value&() { return obj_; } 47 | 48 | private: 49 | void Finalizer() { 50 | delete this; 51 | } 52 | 53 | bool OnGtkDraw(const Cairo::RefPtr& cr); 54 | 55 | rs::jsapi::Context& cx_; 56 | rs::jsapi::Value obj_; 57 | Gtk::DrawingArea* area_; 58 | Widget widget_; 59 | 60 | Cairo::RefPtr onDrawContext_; 61 | rs::jsapi::Value onDraw_ { cx_ }; 62 | 63 | Cairo::RefPtr image_; 64 | }; 65 | 66 | #endif /* DRAWING_AREA_H */ 67 | 68 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/drawing_area.js: -------------------------------------------------------------------------------- 1 | builder.addFromFile('drawing_area.glade'); 2 | 3 | var window = builder.getWindow('window1'); 4 | window.show(); 5 | 6 | var area = builder.getWidget('drawingarea1'); 7 | 8 | var img = area.createImageSurface(window.getWidth(), window.getHeight()); 9 | 10 | for (var i = 0, size = img.width * img.height; i < size; ++i) { 11 | var ix = i * 4; 12 | img.data[ix + 3] = 255; 13 | img.data[ix + 0] = 127; 14 | } 15 | 16 | area.putImageData(img, 0, 0); 17 | 18 | app.run(window); -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/entry.cpp: -------------------------------------------------------------------------------- 1 | #include "entry.h" 2 | 3 | #include 4 | 5 | Entry::Entry(rs::jsapi::Context& cx, Gtk::Entry* entry) : cx_(cx), obj_(cx), entry_(entry), widget_(entry, obj_) { 6 | auto functions = widget_.GetFunctions(); 7 | 8 | functions.emplace_back("setText", std::bind(&Entry::SetText, this, std::placeholders::_1, std::placeholders::_2)); 9 | functions.emplace_back("getText", std::bind(&Entry::GetText, this, std::placeholders::_1, std::placeholders::_2)); 10 | 11 | rs::jsapi::Object::Create(cx, { "value", "innerHTML" }, 12 | std::bind(&Entry::GetCallback, this, std::placeholders::_1, std::placeholders::_2), 13 | std::bind(&Entry::SetCallback, this, std::placeholders::_1, std::placeholders::_2), 14 | functions, std::bind(&Entry::Finalizer, this), obj_); 15 | } 16 | 17 | void Entry::GetCallback(const char* name, rs::jsapi::Value& value) { 18 | if (std::strcmp(name, "value") == 0 || std::strcmp(name, "innerHTML") == 0) { 19 | GetText({}, value); 20 | } 21 | } 22 | 23 | void Entry::SetCallback(const char* name, const rs::jsapi::Value& value) { 24 | if (std::strcmp(name, "value") == 0 || std::strcmp(name, "innerHTML") == 0) { 25 | std::vector args; 26 | args.push_back(value); 27 | rs::jsapi::Value result(cx_); 28 | SetText(args, result); 29 | } 30 | } 31 | 32 | void Entry::SetText(const std::vector& args, rs::jsapi::Value& result) { 33 | entry_->set_text(args[0].ToString()); 34 | result = *this; 35 | } 36 | 37 | void Entry::GetText(const std::vector& args, rs::jsapi::Value& result) { 38 | result = entry_->get_text(); 39 | } -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/entry.h: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * The MIT License (MIT) 4 | * 5 | * Copyright (c) 2015-2016 Ripcord Software Ltd 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | **/ 25 | #ifndef ENTRY_H 26 | #define ENTRY_H 27 | 28 | #include "libjsapi.h" 29 | 30 | #include "widget.h" 31 | 32 | class Entry { 33 | public: 34 | Entry(rs::jsapi::Context& rt, Gtk::Entry* entry); 35 | 36 | void GetText(const std::vector& args, rs::jsapi::Value& result); 37 | void SetText(const std::vector& args, rs::jsapi::Value& result); 38 | 39 | operator rs::jsapi::Value&() { return obj_; } 40 | 41 | private: 42 | void Finalizer() { 43 | delete this; 44 | } 45 | 46 | void GetCallback(const char* name, rs::jsapi::Value& value); 47 | void SetCallback(const char* name, const rs::jsapi::Value& value); 48 | 49 | rs::jsapi::Context& cx_; 50 | rs::jsapi::Value obj_; 51 | Gtk::Entry* entry_; 52 | Widget widget_; 53 | 54 | }; 55 | 56 | #endif /* ENTRY_H */ 57 | 58 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/image_surface.cpp: -------------------------------------------------------------------------------- 1 | #include "image_surface.h" 2 | 3 | #include 4 | #include 5 | 6 | ImageSurface::ImageSurface(rs::jsapi::Context& cx, unsigned width, unsigned height, unsigned depth) : 7 | cx_(cx), obj_(cx), width_(width), height_(height), depth_(depth), array_(cx), refCount_(2), 8 | data_(width_ * height_ * depth_, 0) { 9 | 10 | rs::jsapi::Object::Create(cx, { "data", "width", "height", "depth" }, 11 | std::bind(&ImageSurface::GetCallback, this, std::placeholders::_1, std::placeholders::_2), 12 | nullptr, 13 | {}, 14 | std::bind(&ImageSurface::Finalizer, this), obj_); 15 | 16 | rs::jsapi::Object::SetPrivate(obj_, typeid(ImageSurface).hash_code(), this); 17 | 18 | rs::jsapi::DynamicArray::Create(cx, 19 | [&](int index, rs::jsapi::Value& value) { value.set(data_[index]); }, 20 | [&](int index, const rs::jsapi::Value& value) { 21 | if (value.isInt32()) { 22 | data_[index] = value.toInt32(); 23 | } else if (value.isNumber()) { 24 | data_[index] = value.toNumber(); 25 | } 26 | }, 27 | [&]() { return data_.size(); }, 28 | std::bind(&ImageSurface::Finalizer, this), 29 | array_); 30 | } 31 | 32 | void ImageSurface::GetCallback(const char* name, rs::jsapi::Value& value) { 33 | if (std::strcmp(name, "data") == 0) { 34 | value = array_; 35 | } else if (std::strcmp(name, "width") == 0) { 36 | value = (int)width_; 37 | } else if (std::strcmp(name, "height") == 0) { 38 | value = (int)height_; 39 | } else if (std::strcmp(name, "depth") == 0) { 40 | value = (int)depth_; 41 | } 42 | } 43 | 44 | bool ImageSurface::GetData(const rs::jsapi::Value& obj, const unsigned char*& data, unsigned& width, unsigned& height, unsigned& depth) { 45 | uint64_t dataType = 0; 46 | void* ptr = nullptr; 47 | if (rs::jsapi::Object::GetPrivate(obj, dataType, ptr) && dataType == typeid(ImageSurface).hash_code()) { 48 | auto that = reinterpret_cast(ptr); 49 | data = that->data_.data(); 50 | width = that->width_; 51 | height = that->height_; 52 | depth = that->depth_; 53 | return true; 54 | } else { 55 | return false; 56 | } 57 | } -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/image_surface.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef IMAGE_SURFACE_H 26 | #define IMAGE_SURFACE_H 27 | 28 | #include 29 | #include 30 | 31 | #include "libjsapi.h" 32 | 33 | #include "widget.h" 34 | 35 | class ImageSurface { 36 | public: 37 | ImageSurface(rs::jsapi::Context& cx, unsigned width, unsigned height, unsigned depth = 4); 38 | 39 | operator rs::jsapi::Value&() { return obj_; } 40 | 41 | static bool GetData(const rs::jsapi::Value& obj, const unsigned char*& data, unsigned& width, unsigned& height, unsigned& depth); 42 | 43 | private: 44 | void Finalizer() { 45 | if (--refCount_ == 0) { 46 | delete this; 47 | } 48 | } 49 | 50 | void GetCallback(const char* name, rs::jsapi::Value& value); 51 | 52 | rs::jsapi::Context& cx_; 53 | rs::jsapi::Value obj_; 54 | unsigned width_; 55 | unsigned height_; 56 | unsigned depth_; 57 | 58 | rs::jsapi::Value array_; 59 | std::vector data_; 60 | 61 | std::atomic refCount_; 62 | }; 63 | 64 | #endif /* IMAGE_SURFACE_H */ 65 | 66 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/label.cpp: -------------------------------------------------------------------------------- 1 | #include "label.h" 2 | 3 | #include 4 | 5 | Label::Label(rs::jsapi::Context& cx, Gtk::Label* label) : cx_(cx), obj_(cx), label_(label), widget_(label, obj_) { 6 | auto functions = widget_.GetFunctions(); 7 | 8 | functions.emplace_back("setText", std::bind(&Label::SetText, this, std::placeholders::_1, std::placeholders::_2)); 9 | functions.emplace_back("getText", std::bind(&Label::GetText, this, std::placeholders::_1, std::placeholders::_2)); 10 | 11 | rs::jsapi::Object::Create(cx, { "value", "innerHTML" }, 12 | std::bind(&Label::GetCallback, this, std::placeholders::_1, std::placeholders::_2), 13 | std::bind(&Label::SetCallback, this, std::placeholders::_1, std::placeholders::_2), 14 | functions, std::bind(&Label::Finalizer, this), obj_); 15 | } 16 | 17 | void Label::GetCallback(const char* name, rs::jsapi::Value& value) { 18 | if (std::strcmp(name, "value") == 0 || std::strcmp(name, "innerHTML") == 0) { 19 | GetText({}, value); 20 | } 21 | } 22 | 23 | void Label::SetCallback(const char* name, const rs::jsapi::Value& value) { 24 | if (std::strcmp(name, "value") == 0 || std::strcmp(name, "innerHTML") == 0) { 25 | std::vector args; 26 | args.push_back(value); 27 | rs::jsapi::Value result(cx_); 28 | SetText(args, result); 29 | } 30 | } 31 | 32 | void Label::SetText(const std::vector& args, rs::jsapi::Value& result) { 33 | label_->set_text(args[0].ToString()); 34 | result = *this; 35 | } 36 | 37 | void Label::GetText(const std::vector& args, rs::jsapi::Value& result) { 38 | result = label_->get_text(); 39 | } -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/label.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef LABEL_H 26 | #define LABEL_H 27 | 28 | #include "libjsapi.h" 29 | 30 | #include "widget.h" 31 | 32 | class Label { 33 | public: 34 | 35 | Label(rs::jsapi::Context& cx, Gtk::Label* label); 36 | 37 | void GetText(const std::vector& args, rs::jsapi::Value& result); 38 | void SetText(const std::vector& args, rs::jsapi::Value& result); 39 | 40 | operator rs::jsapi::Value&() { return obj_; } 41 | 42 | private: 43 | void Finalizer() { 44 | delete this; 45 | } 46 | 47 | void GetCallback(const char* name, rs::jsapi::Value& value); 48 | void SetCallback(const char* name, const rs::jsapi::Value& value); 49 | 50 | rs::jsapi::Context& cx_; 51 | rs::jsapi::Value obj_; 52 | Gtk::Label* label_; 53 | Widget widget_; 54 | }; 55 | 56 | #endif /* LABEL_H */ 57 | 58 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include "libjsapi.h" 8 | 9 | #include "application.h" 10 | #include "window.h" 11 | #include "builder.h" 12 | 13 | std::string LoadScript(int argc, char** argv) { 14 | std::string script; 15 | 16 | for (int i = 1; i < argc; i++) { 17 | std::fstream file; 18 | file.open(argv[i], std::ios::in); 19 | while (!!file) { 20 | char buffer[4096]; 21 | file.read(buffer, sizeof(buffer)); 22 | script.append(buffer, file.gcount()); 23 | } 24 | 25 | script += ";\n"; 26 | } 27 | 28 | return script; 29 | } 30 | 31 | int main(int argc, char** argv) { 32 | 33 | try { 34 | std::string script = LoadScript(argc, argv); 35 | 36 | rs::jsapi::Context cx(JS::DefaultHeapMaxBytes * 4, JS::DefaultNurseryBytes * 4, true, true); 37 | 38 | auto app = new Application(cx, "com.ripcordsoftware.examples.gtk", 1, argv); 39 | rs::jsapi::Global::DefineProperty(cx, "app", *app); 40 | 41 | auto builder = new Builder(cx); 42 | rs::jsapi::Global::DefineProperty(cx, "builder", *builder); 43 | 44 | rs::jsapi::Global::DefineFunction(cx, "trace", 45 | [](const std::vector& args, rs::jsapi::Value& result){ 46 | for (auto arg : args) { 47 | std::cout << arg.ToString(); 48 | } 49 | 50 | if (args.size() > 0) { 51 | std::cout << std::endl; 52 | } 53 | 54 | return true; 55 | }); 56 | 57 | cx.Evaluate(script.c_str()); 58 | } catch (const rs::jsapi::ScriptException& ex) { 59 | std::cerr << 60 | "ERROR: line " << ex.lineno << std::endl << 61 | ex.what() << std::endl; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/nasa-9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RipcordSoftware/libjsapi/57369128d9da6eb84ff3b9c5b9791aee4ae34ce4/src/testlibjsapi_gtkmm/nasa-9.png -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/nbproject/Makefile-Debug.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated Makefile - do not edit! 3 | # 4 | # Edit the Makefile in the project folder instead (../Makefile). Each target 5 | # has a -pre and a -post target defined where you can add customized code. 6 | # 7 | # This makefile implements configuration specific macros and targets. 8 | 9 | 10 | # Environment 11 | MKDIR=mkdir 12 | CP=cp 13 | GREP=grep 14 | NM=nm 15 | CCADMIN=CCadmin 16 | RANLIB=ranlib 17 | CC=gcc 18 | CCC=g++ 19 | CXX=g++ 20 | FC=gfortran 21 | AS=as 22 | 23 | # Macros 24 | CND_PLATFORM=GNU-Linux-x86 25 | CND_DLIB_EXT=so 26 | CND_CONF=Debug 27 | CND_DISTDIR=dist 28 | CND_BUILDDIR=build 29 | 30 | # Include project Makefile 31 | include Makefile 32 | 33 | # Object Directory 34 | OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM} 35 | 36 | # Object Files 37 | OBJECTFILES= \ 38 | ${OBJECTDIR}/application.o \ 39 | ${OBJECTDIR}/builder.o \ 40 | ${OBJECTDIR}/button.o \ 41 | ${OBJECTDIR}/check_button.o \ 42 | ${OBJECTDIR}/drawing_area.o \ 43 | ${OBJECTDIR}/entry.o \ 44 | ${OBJECTDIR}/image_surface.o \ 45 | ${OBJECTDIR}/label.o \ 46 | ${OBJECTDIR}/main.o \ 47 | ${OBJECTDIR}/widget.o \ 48 | ${OBJECTDIR}/window.o 49 | 50 | 51 | # C Compiler Flags 52 | CFLAGS= 53 | 54 | # CC Compiler Flags 55 | CCFLAGS=`pkg-config gtkmm-3.0 --cflags` $(COVERAGE_FLAGS) 56 | CXXFLAGS=`pkg-config gtkmm-3.0 --cflags` $(COVERAGE_FLAGS) 57 | 58 | # Fortran Compiler Flags 59 | FFLAGS= 60 | 61 | # Assembler Flags 62 | ASFLAGS= 63 | 64 | # Link Libraries and Options 65 | LDLIBSOPTIONS=../libjsapi/dist/Debug/GNU-Linux-x86/libjsapi.a ../../externals/installed/lib/libjs_static.ajs `pkg-config gtkmm-3.0 --libs` -lz $(LDLIBS) 66 | 67 | # Build Targets 68 | .build-conf: ${BUILD_SUBPROJECTS} 69 | "${MAKE}" -f nbproject/Makefile-${CND_CONF}.mk ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi_gtkmm 70 | 71 | ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi_gtkmm: ../libjsapi/dist/Debug/GNU-Linux-x86/libjsapi.a 72 | 73 | ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi_gtkmm: ${OBJECTFILES} 74 | ${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM} 75 | ${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi_gtkmm ${OBJECTFILES} ${LDLIBSOPTIONS} 76 | 77 | ${OBJECTDIR}/application.o: application.cpp 78 | ${MKDIR} -p ${OBJECTDIR} 79 | ${RM} "$@.d" 80 | $(COMPILE.cc) -g -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/application.o application.cpp 81 | 82 | ${OBJECTDIR}/builder.o: builder.cpp 83 | ${MKDIR} -p ${OBJECTDIR} 84 | ${RM} "$@.d" 85 | $(COMPILE.cc) -g -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/builder.o builder.cpp 86 | 87 | ${OBJECTDIR}/button.o: button.cpp 88 | ${MKDIR} -p ${OBJECTDIR} 89 | ${RM} "$@.d" 90 | $(COMPILE.cc) -g -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/button.o button.cpp 91 | 92 | ${OBJECTDIR}/check_button.o: check_button.cpp 93 | ${MKDIR} -p ${OBJECTDIR} 94 | ${RM} "$@.d" 95 | $(COMPILE.cc) -g -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/check_button.o check_button.cpp 96 | 97 | ${OBJECTDIR}/drawing_area.o: drawing_area.cpp 98 | ${MKDIR} -p ${OBJECTDIR} 99 | ${RM} "$@.d" 100 | $(COMPILE.cc) -g -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/drawing_area.o drawing_area.cpp 101 | 102 | ${OBJECTDIR}/entry.o: entry.cpp 103 | ${MKDIR} -p ${OBJECTDIR} 104 | ${RM} "$@.d" 105 | $(COMPILE.cc) -g -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/entry.o entry.cpp 106 | 107 | ${OBJECTDIR}/image_surface.o: image_surface.cpp 108 | ${MKDIR} -p ${OBJECTDIR} 109 | ${RM} "$@.d" 110 | $(COMPILE.cc) -g -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/image_surface.o image_surface.cpp 111 | 112 | ${OBJECTDIR}/label.o: label.cpp 113 | ${MKDIR} -p ${OBJECTDIR} 114 | ${RM} "$@.d" 115 | $(COMPILE.cc) -g -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/label.o label.cpp 116 | 117 | ${OBJECTDIR}/main.o: main.cpp 118 | ${MKDIR} -p ${OBJECTDIR} 119 | ${RM} "$@.d" 120 | $(COMPILE.cc) -g -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/main.o main.cpp 121 | 122 | ${OBJECTDIR}/widget.o: widget.cpp 123 | ${MKDIR} -p ${OBJECTDIR} 124 | ${RM} "$@.d" 125 | $(COMPILE.cc) -g -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/widget.o widget.cpp 126 | 127 | ${OBJECTDIR}/window.o: window.cpp 128 | ${MKDIR} -p ${OBJECTDIR} 129 | ${RM} "$@.d" 130 | $(COMPILE.cc) -g -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/window.o window.cpp 131 | 132 | # Subprojects 133 | .build-subprojects: 134 | cd ../libjsapi && ${MAKE} -f Makefile CONF=Debug 135 | 136 | # Clean Targets 137 | .clean-conf: ${CLEAN_SUBPROJECTS} 138 | ${RM} -r ${CND_BUILDDIR}/${CND_CONF} 139 | ${RM} ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi_gtkmm 140 | 141 | # Subprojects 142 | .clean-subprojects: 143 | cd ../libjsapi && ${MAKE} -f Makefile CONF=Debug clean 144 | 145 | # Enable dependency checking 146 | .dep.inc: .depcheck-impl 147 | 148 | include .dep.inc 149 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/nbproject/Makefile-Release.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated Makefile - do not edit! 3 | # 4 | # Edit the Makefile in the project folder instead (../Makefile). Each target 5 | # has a -pre and a -post target defined where you can add customized code. 6 | # 7 | # This makefile implements configuration specific macros and targets. 8 | 9 | 10 | # Environment 11 | MKDIR=mkdir 12 | CP=cp 13 | GREP=grep 14 | NM=nm 15 | CCADMIN=CCadmin 16 | RANLIB=ranlib 17 | CC=gcc 18 | CCC=g++ 19 | CXX=g++ 20 | FC=gfortran 21 | AS=as 22 | 23 | # Macros 24 | CND_PLATFORM=GNU-Linux-x86 25 | CND_DLIB_EXT=so 26 | CND_CONF=Release 27 | CND_DISTDIR=dist 28 | CND_BUILDDIR=build 29 | 30 | # Include project Makefile 31 | include Makefile 32 | 33 | # Object Directory 34 | OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM} 35 | 36 | # Object Files 37 | OBJECTFILES= \ 38 | ${OBJECTDIR}/application.o \ 39 | ${OBJECTDIR}/builder.o \ 40 | ${OBJECTDIR}/button.o \ 41 | ${OBJECTDIR}/check_button.o \ 42 | ${OBJECTDIR}/drawing_area.o \ 43 | ${OBJECTDIR}/entry.o \ 44 | ${OBJECTDIR}/image_surface.o \ 45 | ${OBJECTDIR}/label.o \ 46 | ${OBJECTDIR}/main.o \ 47 | ${OBJECTDIR}/widget.o \ 48 | ${OBJECTDIR}/window.o 49 | 50 | 51 | # C Compiler Flags 52 | CFLAGS= 53 | 54 | # CC Compiler Flags 55 | CCFLAGS=`pkg-config gtkmm-3.0 --cflags` 56 | CXXFLAGS=`pkg-config gtkmm-3.0 --cflags` 57 | 58 | # Fortran Compiler Flags 59 | FFLAGS= 60 | 61 | # Assembler Flags 62 | ASFLAGS= 63 | 64 | # Link Libraries and Options 65 | LDLIBSOPTIONS=../libjsapi/dist/Release/GNU-Linux-x86/libjsapi.a ../../externals/installed/lib/libjs_static.ajs `pkg-config gtkmm-3.0 --libs` -lz $(LDLIBS) 66 | 67 | # Build Targets 68 | .build-conf: ${BUILD_SUBPROJECTS} 69 | "${MAKE}" -f nbproject/Makefile-${CND_CONF}.mk ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi_gtkmm 70 | 71 | ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi_gtkmm: ../libjsapi/dist/Release/GNU-Linux-x86/libjsapi.a 72 | 73 | ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi_gtkmm: ${OBJECTFILES} 74 | ${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM} 75 | ${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi_gtkmm ${OBJECTFILES} ${LDLIBSOPTIONS} 76 | 77 | ${OBJECTDIR}/application.o: application.cpp 78 | ${MKDIR} -p ${OBJECTDIR} 79 | ${RM} "$@.d" 80 | $(COMPILE.cc) -O2 -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/application.o application.cpp 81 | 82 | ${OBJECTDIR}/builder.o: builder.cpp 83 | ${MKDIR} -p ${OBJECTDIR} 84 | ${RM} "$@.d" 85 | $(COMPILE.cc) -O2 -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/builder.o builder.cpp 86 | 87 | ${OBJECTDIR}/button.o: button.cpp 88 | ${MKDIR} -p ${OBJECTDIR} 89 | ${RM} "$@.d" 90 | $(COMPILE.cc) -O2 -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/button.o button.cpp 91 | 92 | ${OBJECTDIR}/check_button.o: check_button.cpp 93 | ${MKDIR} -p ${OBJECTDIR} 94 | ${RM} "$@.d" 95 | $(COMPILE.cc) -O2 -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/check_button.o check_button.cpp 96 | 97 | ${OBJECTDIR}/drawing_area.o: drawing_area.cpp 98 | ${MKDIR} -p ${OBJECTDIR} 99 | ${RM} "$@.d" 100 | $(COMPILE.cc) -O2 -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/drawing_area.o drawing_area.cpp 101 | 102 | ${OBJECTDIR}/entry.o: entry.cpp 103 | ${MKDIR} -p ${OBJECTDIR} 104 | ${RM} "$@.d" 105 | $(COMPILE.cc) -O2 -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/entry.o entry.cpp 106 | 107 | ${OBJECTDIR}/image_surface.o: image_surface.cpp 108 | ${MKDIR} -p ${OBJECTDIR} 109 | ${RM} "$@.d" 110 | $(COMPILE.cc) -O2 -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/image_surface.o image_surface.cpp 111 | 112 | ${OBJECTDIR}/label.o: label.cpp 113 | ${MKDIR} -p ${OBJECTDIR} 114 | ${RM} "$@.d" 115 | $(COMPILE.cc) -O2 -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/label.o label.cpp 116 | 117 | ${OBJECTDIR}/main.o: main.cpp 118 | ${MKDIR} -p ${OBJECTDIR} 119 | ${RM} "$@.d" 120 | $(COMPILE.cc) -O2 -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/main.o main.cpp 121 | 122 | ${OBJECTDIR}/widget.o: widget.cpp 123 | ${MKDIR} -p ${OBJECTDIR} 124 | ${RM} "$@.d" 125 | $(COMPILE.cc) -O2 -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/widget.o widget.cpp 126 | 127 | ${OBJECTDIR}/window.o: window.cpp 128 | ${MKDIR} -p ${OBJECTDIR} 129 | ${RM} "$@.d" 130 | $(COMPILE.cc) -O2 -I../libjsapi -I../../externals/installed/include/mozjs -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/window.o window.cpp 131 | 132 | # Subprojects 133 | .build-subprojects: 134 | cd ../libjsapi && ${MAKE} -f Makefile CONF=Release 135 | 136 | # Clean Targets 137 | .clean-conf: ${CLEAN_SUBPROJECTS} 138 | ${RM} -r ${CND_BUILDDIR}/${CND_CONF} 139 | ${RM} ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi_gtkmm 140 | 141 | # Subprojects 142 | .clean-subprojects: 143 | cd ../libjsapi && ${MAKE} -f Makefile CONF=Release clean 144 | 145 | # Enable dependency checking 146 | .dep.inc: .depcheck-impl 147 | 148 | include .dep.inc 149 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/nbproject/Makefile-impl.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated Makefile - do not edit! 3 | # 4 | # Edit the Makefile in the project folder instead (../Makefile). Each target 5 | # has a pre- and a post- target defined where you can add customization code. 6 | # 7 | # This makefile implements macros and targets common to all configurations. 8 | # 9 | # NOCDDL 10 | 11 | 12 | # Building and Cleaning subprojects are done by default, but can be controlled with the SUB 13 | # macro. If SUB=no, subprojects will not be built or cleaned. The following macro 14 | # statements set BUILD_SUB-CONF and CLEAN_SUB-CONF to .build-reqprojects-conf 15 | # and .clean-reqprojects-conf unless SUB has the value 'no' 16 | SUB_no=NO 17 | SUBPROJECTS=${SUB_${SUB}} 18 | BUILD_SUBPROJECTS_=.build-subprojects 19 | BUILD_SUBPROJECTS_NO= 20 | BUILD_SUBPROJECTS=${BUILD_SUBPROJECTS_${SUBPROJECTS}} 21 | CLEAN_SUBPROJECTS_=.clean-subprojects 22 | CLEAN_SUBPROJECTS_NO= 23 | CLEAN_SUBPROJECTS=${CLEAN_SUBPROJECTS_${SUBPROJECTS}} 24 | 25 | 26 | # Project Name 27 | PROJECTNAME=testlibjsapi_gtkmm 28 | 29 | # Active Configuration 30 | DEFAULTCONF=Debug 31 | CONF=${DEFAULTCONF} 32 | 33 | # All Configurations 34 | ALLCONFS=Debug Release 35 | 36 | 37 | # build 38 | .build-impl: .build-pre .validate-impl .depcheck-impl 39 | @#echo "=> Running $@... Configuration=$(CONF)" 40 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .build-conf 41 | 42 | 43 | # clean 44 | .clean-impl: .clean-pre .validate-impl .depcheck-impl 45 | @#echo "=> Running $@... Configuration=$(CONF)" 46 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .clean-conf 47 | 48 | 49 | # clobber 50 | .clobber-impl: .clobber-pre .depcheck-impl 51 | @#echo "=> Running $@..." 52 | for CONF in ${ALLCONFS}; \ 53 | do \ 54 | "${MAKE}" -f nbproject/Makefile-$${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .clean-conf; \ 55 | done 56 | 57 | # all 58 | .all-impl: .all-pre .depcheck-impl 59 | @#echo "=> Running $@..." 60 | for CONF in ${ALLCONFS}; \ 61 | do \ 62 | "${MAKE}" -f nbproject/Makefile-$${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .build-conf; \ 63 | done 64 | 65 | # build tests 66 | .build-tests-impl: .build-impl .build-tests-pre 67 | @#echo "=> Running $@... Configuration=$(CONF)" 68 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .build-tests-conf 69 | 70 | # run tests 71 | .test-impl: .build-tests-impl .test-pre 72 | @#echo "=> Running $@... Configuration=$(CONF)" 73 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .test-conf 74 | 75 | # dependency checking support 76 | .depcheck-impl: 77 | @echo "# This code depends on make tool being used" >.dep.inc 78 | @if [ -n "${MAKE_VERSION}" ]; then \ 79 | echo "DEPFILES=\$$(wildcard \$$(addsuffix .d, \$${OBJECTFILES}))" >>.dep.inc; \ 80 | echo "ifneq (\$${DEPFILES},)" >>.dep.inc; \ 81 | echo "include \$${DEPFILES}" >>.dep.inc; \ 82 | echo "endif" >>.dep.inc; \ 83 | else \ 84 | echo ".KEEP_STATE:" >>.dep.inc; \ 85 | echo ".KEEP_STATE_FILE:.make.state.\$${CONF}" >>.dep.inc; \ 86 | fi 87 | 88 | # configuration validation 89 | .validate-impl: 90 | @if [ ! -f nbproject/Makefile-${CONF}.mk ]; \ 91 | then \ 92 | echo ""; \ 93 | echo "Error: can not find the makefile for configuration '${CONF}' in project ${PROJECTNAME}"; \ 94 | echo "See 'make help' for details."; \ 95 | echo "Current directory: " `pwd`; \ 96 | echo ""; \ 97 | fi 98 | @if [ ! -f nbproject/Makefile-${CONF}.mk ]; \ 99 | then \ 100 | exit 1; \ 101 | fi 102 | 103 | 104 | # help 105 | .help-impl: .help-pre 106 | @echo "This makefile supports the following configurations:" 107 | @echo " ${ALLCONFS}" 108 | @echo "" 109 | @echo "and the following targets:" 110 | @echo " build (default target)" 111 | @echo " clean" 112 | @echo " clobber" 113 | @echo " all" 114 | @echo " help" 115 | @echo "" 116 | @echo "Makefile Usage:" 117 | @echo " make [CONF=] [SUB=no] build" 118 | @echo " make [CONF=] [SUB=no] clean" 119 | @echo " make [SUB=no] clobber" 120 | @echo " make [SUB=no] all" 121 | @echo " make help" 122 | @echo "" 123 | @echo "Target 'build' will build a specific configuration and, unless 'SUB=no'," 124 | @echo " also build subprojects." 125 | @echo "Target 'clean' will clean a specific configuration and, unless 'SUB=no'," 126 | @echo " also clean subprojects." 127 | @echo "Target 'clobber' will remove all built files from all configurations and," 128 | @echo " unless 'SUB=no', also from subprojects." 129 | @echo "Target 'all' will will build all configurations and, unless 'SUB=no'," 130 | @echo " also build subprojects." 131 | @echo "Target 'help' prints this message." 132 | @echo "" 133 | 134 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/nbproject/Makefile-variables.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated - do not edit! 3 | # 4 | # NOCDDL 5 | # 6 | CND_BASEDIR=`pwd` 7 | CND_BUILDDIR=build 8 | CND_DISTDIR=dist 9 | # Debug configuration 10 | CND_PLATFORM_Debug=GNU-Linux-x86 11 | CND_ARTIFACT_DIR_Debug=dist/Debug/GNU-Linux-x86 12 | CND_ARTIFACT_NAME_Debug=testlibjsapi_gtkmm 13 | CND_ARTIFACT_PATH_Debug=dist/Debug/GNU-Linux-x86/testlibjsapi_gtkmm 14 | CND_PACKAGE_DIR_Debug=dist/Debug/GNU-Linux-x86/package 15 | CND_PACKAGE_NAME_Debug=testlibjsapigtkmm.tar 16 | CND_PACKAGE_PATH_Debug=dist/Debug/GNU-Linux-x86/package/testlibjsapigtkmm.tar 17 | # Release configuration 18 | CND_PLATFORM_Release=GNU-Linux-x86 19 | CND_ARTIFACT_DIR_Release=dist/Release/GNU-Linux-x86 20 | CND_ARTIFACT_NAME_Release=testlibjsapi_gtkmm 21 | CND_ARTIFACT_PATH_Release=dist/Release/GNU-Linux-x86/testlibjsapi_gtkmm 22 | CND_PACKAGE_DIR_Release=dist/Release/GNU-Linux-x86/package 23 | CND_PACKAGE_NAME_Release=testlibjsapigtkmm.tar 24 | CND_PACKAGE_PATH_Release=dist/Release/GNU-Linux-x86/package/testlibjsapigtkmm.tar 25 | # 26 | # include compiler specific variables 27 | # 28 | # dmake command 29 | ROOT:sh = test -f nbproject/private/Makefile-variables.mk || \ 30 | (mkdir -p nbproject/private && touch nbproject/private/Makefile-variables.mk) 31 | # 32 | # gmake command 33 | .PHONY: $(shell test -f nbproject/private/Makefile-variables.mk || (mkdir -p nbproject/private && touch nbproject/private/Makefile-variables.mk)) 34 | # 35 | include nbproject/private/Makefile-variables.mk 36 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/nbproject/Package-Debug.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | 3 | # 4 | # Generated - do not edit! 5 | # 6 | 7 | # Macros 8 | TOP=`pwd` 9 | CND_PLATFORM=GNU-Linux-x86 10 | CND_CONF=Debug 11 | CND_DISTDIR=dist 12 | CND_BUILDDIR=build 13 | CND_DLIB_EXT=so 14 | NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging 15 | TMPDIRNAME=tmp-packaging 16 | OUTPUT_PATH=${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi_gtkmm 17 | OUTPUT_BASENAME=testlibjsapi_gtkmm 18 | PACKAGE_TOP_DIR=testlibjsapigtkmm/ 19 | 20 | # Functions 21 | function checkReturnCode 22 | { 23 | rc=$? 24 | if [ $rc != 0 ] 25 | then 26 | exit $rc 27 | fi 28 | } 29 | function makeDirectory 30 | # $1 directory path 31 | # $2 permission (optional) 32 | { 33 | mkdir -p "$1" 34 | checkReturnCode 35 | if [ "$2" != "" ] 36 | then 37 | chmod $2 "$1" 38 | checkReturnCode 39 | fi 40 | } 41 | function copyFileToTmpDir 42 | # $1 from-file path 43 | # $2 to-file path 44 | # $3 permission 45 | { 46 | cp "$1" "$2" 47 | checkReturnCode 48 | if [ "$3" != "" ] 49 | then 50 | chmod $3 "$2" 51 | checkReturnCode 52 | fi 53 | } 54 | 55 | # Setup 56 | cd "${TOP}" 57 | mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package 58 | rm -rf ${NBTMPDIR} 59 | mkdir -p ${NBTMPDIR} 60 | 61 | # Copy files and create directories and links 62 | cd "${TOP}" 63 | makeDirectory "${NBTMPDIR}/testlibjsapigtkmm/bin" 64 | copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755 65 | 66 | 67 | # Generate tar file 68 | cd "${TOP}" 69 | rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/testlibjsapigtkmm.tar 70 | cd ${NBTMPDIR} 71 | tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/testlibjsapigtkmm.tar * 72 | checkReturnCode 73 | 74 | # Cleanup 75 | cd "${TOP}" 76 | rm -rf ${NBTMPDIR} 77 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/nbproject/Package-Release.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | 3 | # 4 | # Generated - do not edit! 5 | # 6 | 7 | # Macros 8 | TOP=`pwd` 9 | CND_PLATFORM=GNU-Linux-x86 10 | CND_CONF=Release 11 | CND_DISTDIR=dist 12 | CND_BUILDDIR=build 13 | CND_DLIB_EXT=so 14 | NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging 15 | TMPDIRNAME=tmp-packaging 16 | OUTPUT_PATH=${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testlibjsapi_gtkmm 17 | OUTPUT_BASENAME=testlibjsapi_gtkmm 18 | PACKAGE_TOP_DIR=testlibjsapigtkmm/ 19 | 20 | # Functions 21 | function checkReturnCode 22 | { 23 | rc=$? 24 | if [ $rc != 0 ] 25 | then 26 | exit $rc 27 | fi 28 | } 29 | function makeDirectory 30 | # $1 directory path 31 | # $2 permission (optional) 32 | { 33 | mkdir -p "$1" 34 | checkReturnCode 35 | if [ "$2" != "" ] 36 | then 37 | chmod $2 "$1" 38 | checkReturnCode 39 | fi 40 | } 41 | function copyFileToTmpDir 42 | # $1 from-file path 43 | # $2 to-file path 44 | # $3 permission 45 | { 46 | cp "$1" "$2" 47 | checkReturnCode 48 | if [ "$3" != "" ] 49 | then 50 | chmod $3 "$2" 51 | checkReturnCode 52 | fi 53 | } 54 | 55 | # Setup 56 | cd "${TOP}" 57 | mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package 58 | rm -rf ${NBTMPDIR} 59 | mkdir -p ${NBTMPDIR} 60 | 61 | # Copy files and create directories and links 62 | cd "${TOP}" 63 | makeDirectory "${NBTMPDIR}/testlibjsapigtkmm/bin" 64 | copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755 65 | 66 | 67 | # Generate tar file 68 | cd "${TOP}" 69 | rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/testlibjsapigtkmm.tar 70 | cd ${NBTMPDIR} 71 | tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/testlibjsapigtkmm.tar * 72 | checkReturnCode 73 | 74 | # Cleanup 75 | cd "${TOP}" 76 | rm -rf ${NBTMPDIR} 77 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.cnd.makeproject 4 | 5 | 6 | testlibjsapi_gtkmm 7 | 8 | cpp 9 | h 10 | UTF-8 11 | 12 | ../libjsapi 13 | 14 | 15 | 16 | 17 | Debug 18 | 1 19 | 20 | 21 | Release 22 | 1 23 | 24 | 25 | 26 | false 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/test.js: -------------------------------------------------------------------------------- 1 | builder.addFromFile('test.glade'); 2 | 3 | var msgs = [ 4 | 'you suck!!!', 5 | 'get outa here!!!', 6 | 'roger, over and out', 7 | 'dang....', 8 | 'zooooooooooooooooom....', 9 | 'you are now under my control!', 10 | 'buy more stuff!' 11 | ]; 12 | 13 | var label = $('label1'); 14 | var button = $('button1'); 15 | var entry = $('entry1'); 16 | if (label && button && entry) { 17 | button.onClick(function(){ 18 | var i = Math.random() * msgs.length; 19 | var msg = msgs[Math.floor(i)]; 20 | label.value = msg; 21 | entry.value = msg; 22 | }); 23 | } 24 | 25 | var checkButton = $('checkbutton1'); 26 | if (checkButton) { 27 | checkButton.onClick(function() { 28 | if (entry) { 29 | checkButton.getActive() ? entry.hide() : entry.show(); 30 | } 31 | }); 32 | } 33 | 34 | $('window2').show(); 35 | $('drawingarea1').onDraw(function(area, width, height) { 36 | area.setLineWidth(10.0); 37 | 38 | var xc = width / 2; 39 | var yc = height / 2; 40 | 41 | // draw red lines out from the center of the window 42 | area.setSourceRgb(0.8, 0.0, 0.0); 43 | area.moveTo(0, 0); 44 | area.lineTo(xc, yc); 45 | area.lineTo(0, height); 46 | area.moveTo(xc, yc); 47 | area.lineTo(width, yc); 48 | area.stroke(); 49 | }); 50 | 51 | var window = $('window1'); 52 | if (window) { 53 | window.setTitle('TEST!').show(); 54 | app.run(window); 55 | } -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/widget.cpp: -------------------------------------------------------------------------------- 1 | #include "widget.h" 2 | 3 | Widget::Widget(Gtk::Widget* widget, rs::jsapi::Value& parent) : widget_(widget), parent_(parent) {} 4 | 5 | void Widget::Show(const std::vector& args, rs::jsapi::Value& result) { 6 | widget_->show(); 7 | result = parent_; 8 | } 9 | 10 | void Widget::Hide(const std::vector& args, rs::jsapi::Value& result) { 11 | widget_->hide(); 12 | result = parent_; 13 | } 14 | 15 | void Widget::GetWidth(const std::vector& args, rs::jsapi::Value& result) { 16 | result = widget_->get_width(); 17 | } 18 | 19 | void Widget::GetHeight(const std::vector& args, rs::jsapi::Value& result) { 20 | result = widget_->get_height(); 21 | } 22 | 23 | void Widget::GetOpacity(const std::vector& args, rs::jsapi::Value& result) { 24 | result = widget_->get_opacity(); 25 | } 26 | 27 | void Widget::SetOpacity(const std::vector& args, rs::jsapi::Value& result) { 28 | widget_->set_opacity(args[0].toNumber()); 29 | result = parent_; 30 | } 31 | 32 | void Widget::GetName(const std::vector& args, rs::jsapi::Value& result) { 33 | result = widget_->get_name(); 34 | } 35 | 36 | void Widget::SetName(const std::vector& args, rs::jsapi::Value& result) { 37 | widget_->set_name(args[0].ToString()); 38 | result = parent_; 39 | } -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/widget.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef WIDGET_H 26 | #define WIDGET_H 27 | 28 | #include 29 | 30 | #include "libjsapi.h" 31 | 32 | class Widget { 33 | public: 34 | Widget(Gtk::Widget* widget, rs::jsapi::Value& parent); 35 | 36 | void Show(const std::vector& args, rs::jsapi::Value& result); 37 | void Hide(const std::vector& args, rs::jsapi::Value& result); 38 | void GetWidth(const std::vector& args, rs::jsapi::Value& result); 39 | void GetHeight(const std::vector& args, rs::jsapi::Value& result); 40 | void GetOpacity(const std::vector& args, rs::jsapi::Value& result); 41 | void SetOpacity(const std::vector& args, rs::jsapi::Value& result); 42 | void GetName(const std::vector& args, rs::jsapi::Value& result); 43 | void SetName(const std::vector& args, rs::jsapi::Value& result); 44 | 45 | std::vector> GetFunctions() { 46 | return std::vector> { 47 | { "show", std::bind(&Widget::Show, this, std::placeholders::_1, std::placeholders::_2) }, 48 | { "hide", std::bind(&Widget::Hide, this, std::placeholders::_1, std::placeholders::_2) }, 49 | { "getWidth", std::bind(&Widget::GetWidth, this, std::placeholders::_1, std::placeholders::_2) }, 50 | { "getHeight", std::bind(&Widget::GetHeight, this, std::placeholders::_1, std::placeholders::_2) }, 51 | { "getName", std::bind(&Widget::GetName, this, std::placeholders::_1, std::placeholders::_2) }, 52 | { "setName", std::bind(&Widget::SetName, this, std::placeholders::_1, std::placeholders::_2) }, 53 | { "getOpacity", std::bind(&Widget::GetOpacity, this, std::placeholders::_1, std::placeholders::_2) }, 54 | { "setOpacity", std::bind(&Widget::SetOpacity, this, std::placeholders::_1, std::placeholders::_2) }, 55 | }; 56 | } 57 | 58 | private: 59 | Gtk::Widget* widget_; 60 | rs::jsapi::Value& parent_; 61 | }; 62 | 63 | #endif /* WIDGET_H */ 64 | 65 | -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/window.cpp: -------------------------------------------------------------------------------- 1 | #include "window.h" 2 | 3 | #include 4 | #include 5 | 6 | #include "widget.h" 7 | #include "label.h" 8 | #include "button.h" 9 | 10 | Window::Window(rs::jsapi::Context& cx, Gtk::Window* window) : cx_(cx), obj_(cx), widget_(window_, obj_), window_(window) { 11 | 12 | auto functions = widget_.GetFunctions(); 13 | 14 | functions.emplace_back("setDefaultSize", std::bind(&Window::SetDefaultSize, this, std::placeholders::_1, std::placeholders::_2)); 15 | functions.emplace_back("setTitle", std::bind(&Window::SetTitle, this, std::placeholders::_1, std::placeholders::_2)); 16 | functions.emplace_back("setBorderWidth", std::bind(&Window::SetBorderWidth, this, std::placeholders::_1, std::placeholders::_2)); 17 | functions.emplace_back("getLabel", std::bind(&Window::GetLabel, this, std::placeholders::_1, std::placeholders::_2)); 18 | functions.emplace_back("addLabel", std::bind(&Window::AddLabel, this, std::placeholders::_1, std::placeholders::_2)); 19 | functions.emplace_back("addButton", std::bind(&Window::AddButton, this, std::placeholders::_1, std::placeholders::_2)); 20 | 21 | rs::jsapi::Object::Create(cx, 22 | { "width", "height" }, 23 | std::bind(&Window::GetCallback, this, std::placeholders::_1, std::placeholders::_2), 24 | nullptr, 25 | functions, std::bind(&Window::Finalizer, this), obj_); 26 | 27 | rs::jsapi::Object::SetPrivate(obj_, typeid(Window).hash_code(), this); 28 | } 29 | 30 | void Window::GetCallback(const char* name, rs::jsapi::Value& value) { 31 | if (std::strcmp(name, "width") == 0) { 32 | value = window_->get_width(); 33 | } else if (std::strcmp(name, "height") == 0) { 34 | value = window_->get_height(); 35 | } 36 | } 37 | 38 | void Window::SetDefaultSize(const std::vector& args, rs::jsapi::Value& result) { 39 | auto width = args[0].toInt32(); 40 | auto height = args[1].toInt32(); 41 | window_->set_default_size(width, height); 42 | result = *this; 43 | } 44 | 45 | void Window::SetTitle(const std::vector& args, rs::jsapi::Value& result) { 46 | window_->set_title(args[0].ToString()); 47 | result = *this; 48 | } 49 | 50 | void Window::SetBorderWidth(const std::vector& args, rs::jsapi::Value& result) { 51 | window_->set_border_width(args[0].toInt32()); 52 | result = *this; 53 | } 54 | 55 | void Window::GetLabel(const std::vector& args, rs::jsapi::Value& result) { 56 | auto name = args[0].ToString(); 57 | auto children = window_->get_children(); 58 | 59 | for (auto c : children) { 60 | if (c->get_name().compare(name) == 0) { 61 | auto label = new Label(cx_, reinterpret_cast(children[0])); 62 | result = *label; 63 | return; 64 | } 65 | } 66 | 67 | result.setNull(); 68 | } 69 | 70 | void Window::AddLabel(const std::vector& args, rs::jsapi::Value& result) { 71 | auto label = Gtk::manage(new Gtk::Label()); 72 | 73 | if (args.size() > 0 && args[0].isString()) { 74 | label->set_name(args[0].ToString()); 75 | } 76 | 77 | window_->add(*label); 78 | result = *(new Label(cx_, label)); 79 | } 80 | 81 | void Window::AddButton(const std::vector& args, rs::jsapi::Value& result) { 82 | auto button = Gtk::manage(new Gtk::Button()); 83 | 84 | if (args.size() > 0 && args[0].isString()) { 85 | button->set_name(args[0].ToString()); 86 | } 87 | 88 | window_->add(*button); 89 | result = *(new Button(cx_, button)); 90 | } 91 | 92 | Gtk::Window* Window::getWindowFromValue(const rs::jsapi::Value& value) { 93 | Gtk::Window* window = nullptr; 94 | 95 | uint64_t data = 0; 96 | void* ptr = nullptr; 97 | if (rs::jsapi::Object::GetPrivate(value, data, ptr) && data == typeid(Window).hash_code()) { 98 | auto that = reinterpret_cast(ptr); 99 | window = that->window_; 100 | } 101 | 102 | return window; 103 | } -------------------------------------------------------------------------------- /src/testlibjsapi_gtkmm/window.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2015-2016 Ripcord Software Ltd 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | **/ 24 | 25 | #ifndef WINDOW_H 26 | #define WINDOW_H 27 | 28 | #include 29 | 30 | #include "libjsapi.h" 31 | 32 | #include "widget.h" 33 | 34 | class Window { 35 | public: 36 | Window(rs::jsapi::Context& cx, Gtk::Window* window); 37 | 38 | void SetDefaultSize(const std::vector& args, rs::jsapi::Value& result); 39 | void SetTitle(const std::vector& args, rs::jsapi::Value& result); 40 | void SetBorderWidth(const std::vector& args, rs::jsapi::Value& result); 41 | void GetLabel(const std::vector& args, rs::jsapi::Value& result); 42 | void AddLabel(const std::vector& args, rs::jsapi::Value& result); 43 | void AddButton(const std::vector& args, rs::jsapi::Value& result); 44 | 45 | operator Gtk::Window&() { return *window_; } 46 | operator rs::jsapi::Value&() { return obj_; } 47 | 48 | static Gtk::Window* getWindowFromValue(const rs::jsapi::Value&); 49 | 50 | private: 51 | void Finalizer() { 52 | delete this; 53 | } 54 | 55 | void GetCallback(const char* name, rs::jsapi::Value& value); 56 | 57 | rs::jsapi::Context& cx_; 58 | rs::jsapi::Value obj_; 59 | Gtk::Window* window_; 60 | Widget widget_; 61 | }; 62 | 63 | #endif /* WINDOW_H */ 64 | -------------------------------------------------------------------------------- /strip_mozjs.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # strip the debug table from libmozjsi and js 3 | strip -g externals/installed/lib/libjs_static.ajs 4 | strip -g externals/installed/bin/js 5 | --------------------------------------------------------------------------------