From 19fcec84d8d7d21e796c7624e521b60d28ee21ed Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:45:59 +0200 Subject: Adding upstream version 16.2.11+ds. Signed-off-by: Daniel Baumann --- src/civetweb/.clang-format | 32 + src/civetweb/.gitattributes | 33 + src/civetweb/.gitignore | 268 + src/civetweb/.travis.yml | 694 + src/civetweb/CMakeLists.txt | 502 + src/civetweb/CREDITS.md | 167 + src/civetweb/LICENSE.md | 208 + src/civetweb/Makefile | 332 + src/civetweb/Makefile.deprecated | 208 + src/civetweb/Makefile.osx | 42 + src/civetweb/Qt/CivetWeb.pro | 34 + src/civetweb/README.md | 168 + src/civetweb/RELEASE_NOTES.md | 395 + src/civetweb/VisualStudio/buildRelease.pl | 71 + src/civetweb/VisualStudio/civetweb.sln | 75 + .../VisualStudio/civetweb/civetweb.vcxproj | 165 + .../VisualStudio/civetweb/civetweb.vcxproj.filters | 40 + .../VisualStudio/civetweb_lua/civetweb_lua.vcxproj | 216 + .../civetweb_lua/civetweb_lua.vcxproj.filters | 78 + .../VisualStudio/civetweb_yassl/civetweb_yassl.sln | 36 + .../civetweb_yassl/civetweb_yassl.vcxproj | 170 + .../civetweb_yassl/civetweb_yassl.vcxproj.filters | 40 + .../civetweb_yassl/yassl_lib/yassl_lib.vcxproj | 185 + .../yassl_lib/yassl_lib.vcxproj.filters | 124 + .../VisualStudio/duktape_lib/duktape_lib.vcxproj | 156 + .../duktape_lib/duktape_lib.vcxproj.filters | 30 + .../VisualStudio/ex_embed_cpp/ex_embed_cpp.vcxproj | 162 + .../ex_embed_cpp/ex_embed_cpp.vcxproj.filters | 36 + .../ex_embedded_c/ex_embedded_c.vcxproj | 161 + .../ex_embedded_c/ex_embedded_c.vcxproj.filters | 30 + .../VisualStudio/ex_websocket/ex_websocket.vcxproj | 162 + .../ex_websocket/ex_websocket.vcxproj.filters | 36 + .../ex_websocket_client.vcxproj | 160 + .../ex_websocket_client.vcxproj.filters | 30 + src/civetweb/VisualStudio/lua_lib/lua_lib.vcxproj | 191 + .../VisualStudio/lua_lib/lua_lib.vcxproj.filters | 135 + .../VisualStudio/unit_test/unit_test.vcxproj | 105 + .../unit_test/unit_test.vcxproj.filters | 66 + src/civetweb/VisualStudio/upload/upload.vcxproj | 160 + .../VisualStudio/upload/upload.vcxproj.filters | 30 + src/civetweb/_config.yml | 1 + src/civetweb/appveyor.yml | 386 + src/civetweb/build | 138 + src/civetweb/build.cmd | 866 + src/civetweb/ci/test/01_basic/basic_spec.lua | 35 + .../docroot/01_basic_test_dir/git_keep_empty_dir | 0 .../ci/test/01_basic/docroot/01_basic_test_file | 0 src/civetweb/ci/test/README.md | 34 + src/civetweb/ci/test/civet.lua | 42 + src/civetweb/ci/travis/install_rocks.sh | 14 + src/civetweb/ci/travis/lua_env.sh | 5 + src/civetweb/ci/travis/platform.sh | 15 + src/civetweb/ci/travis/run_ci_tests.sh | 7 + src/civetweb/ci/travis/setup_lua.sh | 61 + src/civetweb/cmake/AddCCompilerFlag.cmake | 38 + src/civetweb/cmake/AddCXXCompilerFlag.cmake | 38 + .../cmake/DetermineTargetArchitecture.cmake | 47 + src/civetweb/cmake/FindLibDl.cmake | 46 + src/civetweb/cmake/FindLibM.cmake | 47 + src/civetweb/cmake/FindLibRt.cmake | 46 + src/civetweb/cmake/FindLibSubunit.cmake | 46 + src/civetweb/cmake/FindWinSock.cmake | 102 + .../c82fe8888aacfe784476112edd3878256d2e30bc.patch | 31 + src/civetweb/cmake/check/patch.cmake | 12 + src/civetweb/contrib/buildroot/Config.in | 26 + src/civetweb/contrib/buildroot/civetweb.mk | 55 + .../distribution/arch/PKGBUILD.git.example | 42 + src/civetweb/distribution/arch/civetweb.service | 9 + src/civetweb/docs/APIReference.md | 134 + src/civetweb/docs/Building.md | 216 + src/civetweb/docs/Contribution.md | 23 + src/civetweb/docs/Embedding.md | 260 + src/civetweb/docs/Installing.md | 38 + src/civetweb/docs/Interface_Changes_1.10.md | 86 + src/civetweb/docs/OpenSSL.md | 153 + src/civetweb/docs/README.md | 42 + src/civetweb/docs/UserManual.md | 814 + src/civetweb/docs/_config.yml | 1 + src/civetweb/docs/api/mg_callbacks.md | 60 + .../api/mg_check_digest_access_authentication.md | 37 + src/civetweb/docs/api/mg_check_feature.md | 40 + src/civetweb/docs/api/mg_client_cert.md | 21 + src/civetweb/docs/api/mg_client_options.md | 21 + src/civetweb/docs/api/mg_close_connection.md | 21 + src/civetweb/docs/api/mg_connect_client.md | 29 + src/civetweb/docs/api/mg_connect_client_secure.md | 30 + .../docs/api/mg_connect_websocket_client.md | 36 + src/civetweb/docs/api/mg_cry.md | 23 + src/civetweb/docs/api/mg_download.md | 37 + src/civetweb/docs/api/mg_exit_library.md | 29 + src/civetweb/docs/api/mg_form_data_handler.md | 36 + src/civetweb/docs/api/mg_get_builtin_mime_type.md | 24 + src/civetweb/docs/api/mg_get_connection_info.md | 40 + src/civetweb/docs/api/mg_get_context.md | 24 + src/civetweb/docs/api/mg_get_context_info.md | 32 + src/civetweb/docs/api/mg_get_cookie.md | 29 + src/civetweb/docs/api/mg_get_header.md | 22 + src/civetweb/docs/api/mg_get_option.md | 24 + src/civetweb/docs/api/mg_get_ports.md | 31 + src/civetweb/docs/api/mg_get_request_info.md | 28 + src/civetweb/docs/api/mg_get_request_link.md | 29 + src/civetweb/docs/api/mg_get_response.md | 29 + src/civetweb/docs/api/mg_get_response_code_text.md | 25 + src/civetweb/docs/api/mg_get_response_info.md | 29 + src/civetweb/docs/api/mg_get_server_ports.md | 28 + src/civetweb/docs/api/mg_get_system_info.md | 30 + .../docs/api/mg_get_user_connection_data.md | 23 + src/civetweb/docs/api/mg_get_user_data.md | 23 + src/civetweb/docs/api/mg_get_valid_option_names.md | 24 + src/civetweb/docs/api/mg_get_valid_options.md | 22 + src/civetweb/docs/api/mg_get_var.md | 30 + src/civetweb/docs/api/mg_get_var2.md | 31 + src/civetweb/docs/api/mg_handle_form_request.md | 24 + src/civetweb/docs/api/mg_header.md | 18 + src/civetweb/docs/api/mg_init_library.md | 44 + src/civetweb/docs/api/mg_lock_connection.md | 26 + src/civetweb/docs/api/mg_lock_context.md | 23 + src/civetweb/docs/api/mg_md5.md | 24 + src/civetweb/docs/api/mg_modify_passwords_file.md | 33 + src/civetweb/docs/api/mg_option.md | 31 + src/civetweb/docs/api/mg_printf.md | 27 + src/civetweb/docs/api/mg_read.md | 26 + src/civetweb/docs/api/mg_request_info.md | 35 + src/civetweb/docs/api/mg_response_info.md | 38 + src/civetweb/docs/api/mg_send_chunk.md | 31 + ...mg_send_digest_access_authentication_request.md | 35 + src/civetweb/docs/api/mg_send_file.md | 25 + src/civetweb/docs/api/mg_send_http_error.md | 32 + src/civetweb/docs/api/mg_send_mime_file.md | 27 + src/civetweb/docs/api/mg_send_mime_file2.md | 30 + src/civetweb/docs/api/mg_server_ports.md | 24 + src/civetweb/docs/api/mg_set_auth_handler.md | 30 + src/civetweb/docs/api/mg_set_request_handler.md | 26 + .../docs/api/mg_set_user_connection_data.md | 44 + src/civetweb/docs/api/mg_set_websocket_handler.md | 30 + src/civetweb/docs/api/mg_start.md | 39 + src/civetweb/docs/api/mg_start_thread.md | 26 + src/civetweb/docs/api/mg_stop.md | 22 + src/civetweb/docs/api/mg_store_body.md | 24 + src/civetweb/docs/api/mg_strcasecmp.md | 24 + src/civetweb/docs/api/mg_strncasecmp.md | 25 + src/civetweb/docs/api/mg_unlock_connection.md | 27 + src/civetweb/docs/api/mg_unlock_context.md | 23 + src/civetweb/docs/api/mg_upload.md | 22 + src/civetweb/docs/api/mg_url_decode.md | 27 + src/civetweb/docs/api/mg_url_encode.md | 25 + src/civetweb/docs/api/mg_version.md | 19 + src/civetweb/docs/api/mg_websocket_client_write.md | 32 + src/civetweb/docs/api/mg_websocket_write.md | 34 + src/civetweb/docs/api/mg_write.md | 29 + src/civetweb/docs/yaSSL.md | 87 + src/civetweb/examples/README.md | 13 + src/civetweb/examples/_obsolete/chat/Makefile | 40 + src/civetweb/examples/_obsolete/chat/chat.c | 403 + .../examples/_obsolete/docroot/favicon.ico | Bin 0 -> 1406 bytes src/civetweb/examples/_obsolete/docroot/index.html | 73 + src/civetweb/examples/_obsolete/docroot/jquery.js | 154 + src/civetweb/examples/_obsolete/docroot/login.html | 43 + src/civetweb/examples/_obsolete/docroot/logo.png | Bin 0 -> 1601 bytes src/civetweb/examples/_obsolete/docroot/main.js | 107 + .../examples/_obsolete/docroot/prime_numbers.lp | 46 + src/civetweb/examples/_obsolete/docroot/style.css | 154 + src/civetweb/examples/_obsolete/hello/Makefile | 36 + src/civetweb/examples/_obsolete/hello/hello.c | 53 + src/civetweb/examples/_obsolete/lua/lua_dll.c | 21 + src/civetweb/examples/_obsolete/post/Makefile | 36 + src/civetweb/examples/_obsolete/post/post.c | 58 + src/civetweb/examples/_obsolete/upload/Makefile | 36 + src/civetweb/examples/_obsolete/upload/upload.c | 110 + src/civetweb/examples/_obsolete/websocket/Makefile | 36 + .../_obsolete/websocket/WebSockCallbacks.c | 225 + .../_obsolete/websocket/WebSockCallbacks.h | 44 + .../examples/_obsolete/websocket/websock.htm | 55 + .../examples/_obsolete/websocket/websocket.c | 65 + .../examples/_obsolete/websocket_client/Makefile | 37 + .../_obsolete/websocket_client/ssl/server.crt | 13 + .../_obsolete/websocket_client/ssl/server.csr | 11 + .../_obsolete/websocket_client/ssl/server.key | 15 + .../_obsolete/websocket_client/ssl/server.key.orig | 18 + .../_obsolete/websocket_client/ssl/server.pem | 28 + .../_obsolete/websocket_client/websocket_client.c | 421 + src/civetweb/examples/_obsolete/ws_server/Makefile | 36 + .../_obsolete/ws_server/docroot/index.html | 316 + .../examples/_obsolete/ws_server/ws_server.c | 271 + src/civetweb/examples/embedded_c/Makefile | 37 + src/civetweb/examples/embedded_c/embedded_c.c | 1089 + src/civetweb/examples/embedded_cpp/Makefile | 36 + .../examples/embedded_cpp/embedded_cpp.cpp | 432 + src/civetweb/examples/https/README.md | 15 + src/civetweb/examples/https/civetweb.conf | 86 + src/civetweb/format.bat | 33 + src/civetweb/include/CivetServer.h | 611 + src/civetweb/include/civetweb.h | 1372 + src/civetweb/mingw.cmd | 884 + src/civetweb/resources/Info.plist | 21 + src/civetweb/resources/Makefile.in-duktape | 77 + src/civetweb/resources/Makefile.in-lua | 149 + src/civetweb/resources/Makefile.in-os | 23 + src/civetweb/resources/cert/client.crt | 19 + src/civetweb/resources/cert/client.csr | 16 + src/civetweb/resources/cert/client.key | 27 + src/civetweb/resources/cert/client.key.orig | 30 + src/civetweb/resources/cert/client.pem | 46 + src/civetweb/resources/cert/client.pfx | Bin 0 -> 2450 bytes src/civetweb/resources/cert/make_certs.bat | 55 + src/civetweb/resources/cert/make_certs.sh | 64 + src/civetweb/resources/cert/server.crt | 19 + src/civetweb/resources/cert/server.csr | 16 + src/civetweb/resources/cert/server.key | 27 + src/civetweb/resources/cert/server.key.orig | 30 + src/civetweb/resources/cert/server.pem | 46 + src/civetweb/resources/cert/server.pin | 1 + src/civetweb/resources/cert/server_bkup.crt | 19 + src/civetweb/resources/cert/server_bkup.csr | 16 + src/civetweb/resources/cert/server_bkup.key | 27 + src/civetweb/resources/cert/server_bkup.key.orig | 30 + src/civetweb/resources/cert/server_bkup.pem | 46 + src/civetweb/resources/cert/server_bkup.pin | 1 + src/civetweb/resources/civetweb.conf | 32 + src/civetweb/resources/civetweb.icns | Bin 0 -> 13635 bytes src/civetweb/resources/civetweb.psd | Bin 0 -> 9365069 bytes src/civetweb/resources/civetweb_16x16.png | Bin 0 -> 1691 bytes src/civetweb/resources/civetweb_16x16@2.png | Bin 0 -> 1624 bytes src/civetweb/resources/civetweb_22x22.png | Bin 0 -> 1857 bytes src/civetweb/resources/civetweb_22x22@2.png | Bin 0 -> 2728 bytes src/civetweb/resources/civetweb_32x32.png | Bin 0 -> 2193 bytes src/civetweb/resources/civetweb_32x32@2.png | Bin 0 -> 3923 bytes src/civetweb/resources/civetweb_64x64.png | Bin 0 -> 3923 bytes src/civetweb/resources/civetweb_64x64@2.png | Bin 0 -> 10178 bytes src/civetweb/resources/coverity_check.sh | 60 + src/civetweb/resources/duktape-logo.png | Bin 0 -> 1741 bytes src/civetweb/resources/itworks.html | 23 + src/civetweb/resources/jni/Android.mk | 6 + src/civetweb/resources/lua-logo.jpg | Bin 0 -> 2799 bytes src/civetweb/resources/luafilesystem-logo.jpg | Bin 0 -> 8209 bytes src/civetweb/resources/luasqlite-logo.jpg | Bin 0 -> 6534 bytes src/civetweb/resources/luaxml-logo.jpg | Bin 0 -> 6741 bytes src/civetweb/resources/mingw.bat | 15 + src/civetweb/resources/res.rc | 1 + src/civetweb/resources/sqlite3-logo.jpg | Bin 0 -> 1673 bytes src/civetweb/resources/ssl_cert.pem | 50 + src/civetweb/resources/systray.ico | Bin 0 -> 15086 bytes src/civetweb/src/CMakeLists.txt | 297 + src/civetweb/src/CivetServer.cpp | 609 + src/civetweb/src/civetweb.c | 18073 ++ src/civetweb/src/civetweb_private_lua.h | 11 + src/civetweb/src/file_ops.inl | 1 + src/civetweb/src/handle_form.inl | 949 + src/civetweb/src/main.c | 2856 + src/civetweb/src/md5.inl | 471 + src/civetweb/src/mod_duktape.inl | 262 + src/civetweb/src/mod_lua.inl | 2371 + src/civetweb/src/sha1.inl | 322 + src/civetweb/src/third_party/LuaXML.lua | 116 + src/civetweb/src/third_party/LuaXML_lib.c | 476 + src/civetweb/src/third_party/civetweb_lua.h | 73 + .../src/third_party/duktape-1.5.2/AUTHORS.rst | 72 + .../src/third_party/duktape-1.5.2/LICENSE.txt | 25 + .../src/third_party/duktape-1.5.2/Makefile.cmdline | 34 + .../third_party/duktape-1.5.2/Makefile.codepage | 4 + .../src/third_party/duktape-1.5.2/Makefile.coffee | 4 + .../third_party/duktape-1.5.2/Makefile.dukdebug | 26 + .../src/third_party/duktape-1.5.2/Makefile.eval | 7 + .../third_party/duktape-1.5.2/Makefile.eventloop | 22 + .../src/third_party/duktape-1.5.2/Makefile.hello | 35 + .../third_party/duktape-1.5.2/Makefile.jxpretty | 8 + .../src/third_party/duktape-1.5.2/Makefile.sandbox | 7 + .../duktape-1.5.2/Makefile.sharedlibrary | 71 + .../src/third_party/duktape-1.5.2/README.rst | 110 + .../third_party/duktape-1.5.2/config/README.rst | 39 + .../duktape-1.5.2/config/duk_config.h-modular-dll | 3415 + .../config/duk_config.h-modular-static | 3415 + .../third_party/duktape-1.5.2/config/genconfig.py | 1537 + .../duktape-1.5.2/config/genconfig_metadata.tar.gz | Bin 0 -> 71815 bytes .../third_party/duktape-1.5.2/debugger/Makefile | 101 + .../third_party/duktape-1.5.2/debugger/README.rst | 384 + .../duktape-1.5.2/debugger/duk_classnames.yaml | 32 + .../duktape-1.5.2/debugger/duk_debug.js | 2473 + .../duktape-1.5.2/debugger/duk_debug_meta.json | 1497 + .../duktape-1.5.2/debugger/duk_debug_proxy.js | 1029 + .../duktape-1.5.2/debugger/duk_debugcommands.yaml | 52 + .../duktape-1.5.2/debugger/duk_debugerrors.yaml | 6 + .../duktape-1.5.2/debugger/duk_opcodes.yaml | 658 + .../duktape-1.5.2/debugger/merge_debug_meta.py | 32 + .../duktape-1.5.2/debugger/package.json | 27 + .../duktape-1.5.2/debugger/static/index.html | 96 + .../duktape-1.5.2/debugger/static/style.css | 516 + .../duktape-1.5.2/debugger/static/webui.js | 785 + .../third_party/duktape-1.5.2/duk_build_meta.json | 1349 + .../third_party/duktape-1.5.2/examples/README.rst | 10 + .../duktape-1.5.2/examples/alloc-hybrid/README.rst | 10 + .../examples/alloc-hybrid/duk_alloc_hybrid.c | 293 + .../examples/alloc-hybrid/duk_alloc_hybrid.h | 11 + .../examples/alloc-logging/README.rst | 7 + .../examples/alloc-logging/duk_alloc_logging.c | 138 + .../examples/alloc-logging/duk_alloc_logging.h | 10 + .../examples/alloc-logging/log2gnuplot.py | 41 + .../examples/alloc-torture/README.rst | 10 + .../examples/alloc-torture/duk_alloc_torture.c | 182 + .../examples/alloc-torture/duk_alloc_torture.h | 10 + .../duktape-1.5.2/examples/cmdline/README.rst | 6 + .../duktape-1.5.2/examples/cmdline/duk_cmdline.c | 1463 + .../examples/cmdline/duk_cmdline_ajduk.c | 1008 + .../examples/codepage-conv/README.rst | 8 + .../examples/codepage-conv/duk_codepage_conv.c | 54 + .../examples/codepage-conv/duk_codepage_conv.h | 8 + .../duktape-1.5.2/examples/codepage-conv/test.c | 286 + .../duktape-1.5.2/examples/coffee/README.rst | 10 + .../duktape-1.5.2/examples/coffee/globals.coffee | 7 + .../duktape-1.5.2/examples/coffee/hello.coffee | 2 + .../duktape-1.5.2/examples/coffee/mandel.coffee | 28 + .../examples/cpp-exceptions/README.rst | 29 + .../examples/cpp-exceptions/cpp_exceptions.cpp | 274 + .../examples/debug-trans-dvalue/Makefile | 17 + .../examples/debug-trans-dvalue/README.rst | 8 + .../examples/debug-trans-dvalue/duk_trans_dvalue.c | 1239 + .../examples/debug-trans-dvalue/duk_trans_dvalue.h | 113 + .../examples/debug-trans-dvalue/test.c | 236 + .../examples/debug-trans-socket/README.rst | 17 + .../examples/debug-trans-socket/duk_trans_socket.h | 15 + .../debug-trans-socket/duk_trans_socket_unix.c | 340 + .../debug-trans-socket/duk_trans_socket_windows.c | 414 + .../examples/dummy-date-provider/README.rst | 5 + .../dummy-date-provider/dummy_date_provider.c | 27 + .../duktape-1.5.2/examples/eval/README.rst | 5 + .../third_party/duktape-1.5.2/examples/eval/eval.c | 48 + .../duktape-1.5.2/examples/eventloop/README.rst | 76 + .../duktape-1.5.2/examples/eventloop/basic-test.js | 17 + .../duktape-1.5.2/examples/eventloop/c_eventloop.c | 618 + .../examples/eventloop/c_eventloop.js | 179 + .../examples/eventloop/client-socket-test.js | 24 + .../examples/eventloop/curses-timers.js | 79 + .../examples/eventloop/ecma_eventloop.js | 466 + .../duktape-1.5.2/examples/eventloop/fileio.c | 69 + .../duktape-1.5.2/examples/eventloop/main.c | 256 + .../duktape-1.5.2/examples/eventloop/ncurses.c | 105 + .../duktape-1.5.2/examples/eventloop/poll.c | 111 + .../examples/eventloop/server-socket-test.js | 34 + .../duktape-1.5.2/examples/eventloop/socket.c | 286 + .../duktape-1.5.2/examples/guide/README.rst | 5 + .../duktape-1.5.2/examples/guide/fib.js | 16 + .../duktape-1.5.2/examples/guide/prime.js | 32 + .../duktape-1.5.2/examples/guide/primecheck.c | 52 + .../duktape-1.5.2/examples/guide/process.js | 12 + .../duktape-1.5.2/examples/guide/processlines.c | 59 + .../duktape-1.5.2/examples/guide/uppercase.c | 42 + .../duktape-1.5.2/examples/hello/README.rst | 5 + .../duktape-1.5.2/examples/hello/hello.c | 38 + .../duktape-1.5.2/examples/jxpretty/README.rst | 5 + .../duktape-1.5.2/examples/jxpretty/jxpretty.c | 63 + .../duktape-1.5.2/examples/sandbox/README.rst | 5 + .../duktape-1.5.2/examples/sandbox/sandbox.c | 252 + .../third_party/duktape-1.5.2/extras/README.rst | 13 + .../src/third_party/duktape-1.5.2/license.spdx | 3933 + .../duktape-1.5.2/licenses/commonjs.txt | 2 + .../src/third_party/duktape-1.5.2/licenses/lua.txt | 1 + .../duktape-1.5.2/licenses/murmurhash2.txt | 21 + .../src/third_party/duktape-1.5.2/mandel.js | 53 + .../duktape-1.5.2/polyfills/console-minimal.js | 20 + .../polyfills/duktape-error-setter-nonwritable.js | 20 + .../polyfills/duktape-error-setter-writable.js | 19 + .../duktape-1.5.2/polyfills/duktape-isfastint.js | 38 + .../duktape-1.5.2/polyfills/object-assign.js | 45 + .../polyfills/object-prototype-definegetter.js | 11 + .../polyfills/object-prototype-definesetter.js | 11 + .../duktape-1.5.2/polyfills/performance-now.js | 25 + .../duktape-1.5.2/src-noline/duk_config.h | 3804 + .../third_party/duktape-1.5.2/src-noline/duktape.c | 86570 ++++++++ .../third_party/duktape-1.5.2/src-noline/duktape.h | 1567 + .../duktape-1.5.2/src-noline/metadata.json | 629 + .../duktape-1.5.2/src-separate/duk_alloc_default.c | 34 + .../duktape-1.5.2/src-separate/duk_api_buffer.c | 73 + .../duktape-1.5.2/src-separate/duk_api_bytecode.c | 727 + .../duktape-1.5.2/src-separate/duk_api_call.c | 555 + .../duktape-1.5.2/src-separate/duk_api_codec.c | 638 + .../duktape-1.5.2/src-separate/duk_api_compile.c | 194 + .../duktape-1.5.2/src-separate/duk_api_debug.c | 263 + .../duktape-1.5.2/src-separate/duk_api_heap.c | 134 + .../duktape-1.5.2/src-separate/duk_api_internal.h | 194 + .../duktape-1.5.2/src-separate/duk_api_logging.c | 52 + .../duktape-1.5.2/src-separate/duk_api_memory.c | 103 + .../duktape-1.5.2/src-separate/duk_api_object.c | 610 + .../duktape-1.5.2/src-separate/duk_api_stack.c | 4658 + .../duktape-1.5.2/src-separate/duk_api_string.c | 331 + .../duktape-1.5.2/src-separate/duk_api_var.c | 86 + .../duktape-1.5.2/src-separate/duk_bi_array.c | 1445 + .../duktape-1.5.2/src-separate/duk_bi_boolean.c | 68 + .../duktape-1.5.2/src-separate/duk_bi_buffer.c | 2882 + .../duktape-1.5.2/src-separate/duk_bi_date.c | 1728 + .../duktape-1.5.2/src-separate/duk_bi_date_unix.c | 309 + .../src-separate/duk_bi_date_windows.c | 98 + .../duktape-1.5.2/src-separate/duk_bi_duktape.c | 321 + .../duktape-1.5.2/src-separate/duk_bi_error.c | 383 + .../duktape-1.5.2/src-separate/duk_bi_function.c | 346 + .../duktape-1.5.2/src-separate/duk_bi_global.c | 1288 + .../duktape-1.5.2/src-separate/duk_bi_json.c | 3143 + .../duktape-1.5.2/src-separate/duk_bi_logger.c | 300 + .../duktape-1.5.2/src-separate/duk_bi_math.c | 348 + .../duktape-1.5.2/src-separate/duk_bi_number.c | 240 + .../duktape-1.5.2/src-separate/duk_bi_object.c | 577 + .../duktape-1.5.2/src-separate/duk_bi_pointer.c | 74 + .../duktape-1.5.2/src-separate/duk_bi_protos.h | 69 + .../duktape-1.5.2/src-separate/duk_bi_proxy.c | 66 + .../duktape-1.5.2/src-separate/duk_bi_regexp.c | 209 + .../duktape-1.5.2/src-separate/duk_bi_string.c | 1314 + .../duktape-1.5.2/src-separate/duk_bi_thread.c | 305 + .../duktape-1.5.2/src-separate/duk_bi_thrower.c | 10 + .../duktape-1.5.2/src-separate/duk_builtins.c | 776 + .../duktape-1.5.2/src-separate/duk_builtins.h | 854 + .../duktape-1.5.2/src-separate/duk_config.h | 3804 + .../duktape-1.5.2/src-separate/duk_debug.h | 185 + .../src-separate/duk_debug_fixedbuffer.c | 69 + .../duktape-1.5.2/src-separate/duk_debug_heap.c | 247 + .../duktape-1.5.2/src-separate/duk_debug_macros.c | 144 + .../src-separate/duk_debug_vsnprintf.c | 1049 + .../duktape-1.5.2/src-separate/duk_debugger.c | 2773 + .../duktape-1.5.2/src-separate/duk_debugger.h | 144 + .../duktape-1.5.2/src-separate/duk_error.h | 439 + .../duktape-1.5.2/src-separate/duk_error_augment.c | 567 + .../duktape-1.5.2/src-separate/duk_error_longjmp.c | 45 + .../duktape-1.5.2/src-separate/duk_error_macros.c | 163 + .../duktape-1.5.2/src-separate/duk_error_misc.c | 132 + .../duktape-1.5.2/src-separate/duk_error_throw.c | 181 + .../duktape-1.5.2/src-separate/duk_exception.h | 18 + .../duktape-1.5.2/src-separate/duk_forwdecl.h | 122 + .../duktape-1.5.2/src-separate/duk_hbuffer.h | 328 + .../duktape-1.5.2/src-separate/duk_hbuffer_alloc.c | 132 + .../duktape-1.5.2/src-separate/duk_hbuffer_ops.c | 82 + .../duktape-1.5.2/src-separate/duk_hbufferobject.h | 133 + .../src-separate/duk_hbufferobject_misc.c | 20 + .../src-separate/duk_hcompiledfunction.h | 239 + .../duktape-1.5.2/src-separate/duk_heap.h | 596 + .../duktape-1.5.2/src-separate/duk_heap_alloc.c | 1042 + .../src-separate/duk_heap_hashstring.c | 120 + .../src-separate/duk_heap_markandsweep.c | 1421 + .../duktape-1.5.2/src-separate/duk_heap_memory.c | 384 + .../duktape-1.5.2/src-separate/duk_heap_misc.c | 79 + .../duktape-1.5.2/src-separate/duk_heap_refcount.c | 614 + .../src-separate/duk_heap_stringcache.c | 298 + .../src-separate/duk_heap_stringtable.c | 1181 + .../duktape-1.5.2/src-separate/duk_heaphdr.h | 750 + .../src-separate/duk_hnativefunction.h | 32 + .../duktape-1.5.2/src-separate/duk_hobject.h | 926 + .../duktape-1.5.2/src-separate/duk_hobject_alloc.c | 192 + .../duktape-1.5.2/src-separate/duk_hobject_class.c | 132 + .../duktape-1.5.2/src-separate/duk_hobject_enum.c | 626 + .../src-separate/duk_hobject_finalizer.c | 110 + .../duktape-1.5.2/src-separate/duk_hobject_misc.c | 52 + .../src-separate/duk_hobject_pc2line.c | 250 + .../duktape-1.5.2/src-separate/duk_hobject_props.c | 5999 + .../duktape-1.5.2/src-separate/duk_hstring.h | 219 + .../duktape-1.5.2/src-separate/duk_hstring_misc.c | 45 + .../duktape-1.5.2/src-separate/duk_hthread.h | 380 + .../duktape-1.5.2/src-separate/duk_hthread_alloc.c | 95 + .../src-separate/duk_hthread_builtins.c | 836 + .../duktape-1.5.2/src-separate/duk_hthread_misc.c | 108 + .../src-separate/duk_hthread_stacks.c | 476 + .../duktape-1.5.2/src-separate/duk_initjs_min.js | 1 + .../duktape-1.5.2/src-separate/duk_internal.h | 79 + .../duktape-1.5.2/src-separate/duk_jmpbuf.h | 24 + .../duktape-1.5.2/src-separate/duk_js.h | 99 + .../duktape-1.5.2/src-separate/duk_js_bytecode.h | 205 + .../duktape-1.5.2/src-separate/duk_js_call.c | 2727 + .../duktape-1.5.2/src-separate/duk_js_compiler.c | 7865 + .../duktape-1.5.2/src-separate/duk_js_compiler.h | 227 + .../duktape-1.5.2/src-separate/duk_js_executor.c | 4507 + .../duktape-1.5.2/src-separate/duk_js_ops.c | 1370 + .../duktape-1.5.2/src-separate/duk_js_var.c | 1821 + .../duktape-1.5.2/src-separate/duk_json.h | 68 + .../duktape-1.5.2/src-separate/duk_lexer.c | 2067 + .../duktape-1.5.2/src-separate/duk_lexer.h | 432 + .../duktape-1.5.2/src-separate/duk_numconv.c | 2266 + .../duktape-1.5.2/src-separate/duk_numconv.h | 90 + .../duktape-1.5.2/src-separate/duk_regexp.h | 82 + .../src-separate/duk_regexp_compiler.c | 1072 + .../src-separate/duk_regexp_executor.c | 1008 + .../duktape-1.5.2/src-separate/duk_replacements.c | 82 + .../duktape-1.5.2/src-separate/duk_replacements.h | 28 + .../duktape-1.5.2/src-separate/duk_selftest.c | 373 + .../duktape-1.5.2/src-separate/duk_selftest.h | 12 + .../duktape-1.5.2/src-separate/duk_strings.c | 115 + .../duktape-1.5.2/src-separate/duk_strings.h | 252 + .../duktape-1.5.2/src-separate/duk_tval.c | 138 + .../duktape-1.5.2/src-separate/duk_tval.h | 518 + .../duktape-1.5.2/src-separate/duk_unicode.h | 267 + .../src-separate/duk_unicode_support.c | 1173 + .../src-separate/duk_unicode_tables.c | 6119 + .../duktape-1.5.2/src-separate/duk_util.h | 530 + .../src-separate/duk_util_bitdecoder.c | 70 + .../src-separate/duk_util_bitencoder.c | 43 + .../src-separate/duk_util_bufwriter.c | 360 + .../src-separate/duk_util_hashbytes.c | 57 + .../src-separate/duk_util_hashprime.c | 77 + .../duktape-1.5.2/src-separate/duk_util_misc.c | 222 + .../src-separate/duk_util_tinyrandom.c | 63 + .../duktape-1.5.2/src-separate/duktape.h | 1567 + .../src/third_party/duktape-1.5.2/src/duk_config.h | 3804 + .../src/third_party/duktape-1.5.2/src/duktape.c | 86695 ++++++++ .../src/third_party/duktape-1.5.2/src/duktape.h | 1567 + .../third_party/duktape-1.5.2/src/metadata.json | 629 + .../src/third_party/duktape-1.8.0/AUTHORS.rst | 72 + .../src/third_party/duktape-1.8.0/LICENSE.txt | 25 + .../src/third_party/duktape-1.8.0/Makefile.cmdline | 34 + .../third_party/duktape-1.8.0/Makefile.codepage | 4 + .../src/third_party/duktape-1.8.0/Makefile.coffee | 4 + .../third_party/duktape-1.8.0/Makefile.dukdebug | 26 + .../src/third_party/duktape-1.8.0/Makefile.eval | 7 + .../third_party/duktape-1.8.0/Makefile.eventloop | 22 + .../src/third_party/duktape-1.8.0/Makefile.hello | 35 + .../third_party/duktape-1.8.0/Makefile.jxpretty | 8 + .../src/third_party/duktape-1.8.0/Makefile.sandbox | 7 + .../duktape-1.8.0/Makefile.sharedlibrary | 71 + .../src/third_party/duktape-1.8.0/README.rst | 110 + .../third_party/duktape-1.8.0/config/README.rst | 39 + .../duktape-1.8.0/config/duk_config.h-modular-dll | 3415 + .../config/duk_config.h-modular-static | 3415 + .../third_party/duktape-1.8.0/config/genconfig.py | 1537 + .../duktape-1.8.0/config/genconfig_metadata.tar.gz | Bin 0 -> 71810 bytes .../third_party/duktape-1.8.0/debugger/Makefile | 101 + .../third_party/duktape-1.8.0/debugger/README.rst | 384 + .../duktape-1.8.0/debugger/duk_classnames.yaml | 32 + .../duktape-1.8.0/debugger/duk_debug.js | 2473 + .../duktape-1.8.0/debugger/duk_debug_meta.json | 1497 + .../duktape-1.8.0/debugger/duk_debug_proxy.js | 1029 + .../duktape-1.8.0/debugger/duk_debugcommands.yaml | 52 + .../duktape-1.8.0/debugger/duk_debugerrors.yaml | 6 + .../duktape-1.8.0/debugger/duk_opcodes.yaml | 658 + .../duktape-1.8.0/debugger/merge_debug_meta.py | 32 + .../duktape-1.8.0/debugger/package.json | 27 + .../duktape-1.8.0/debugger/static/index.html | 96 + .../duktape-1.8.0/debugger/static/style.css | 516 + .../duktape-1.8.0/debugger/static/webui.js | 785 + .../third_party/duktape-1.8.0/duk_build_meta.json | 1349 + .../third_party/duktape-1.8.0/examples/README.rst | 10 + .../duktape-1.8.0/examples/alloc-hybrid/README.rst | 10 + .../examples/alloc-hybrid/duk_alloc_hybrid.c | 293 + .../examples/alloc-hybrid/duk_alloc_hybrid.h | 11 + .../examples/alloc-logging/README.rst | 7 + .../examples/alloc-logging/duk_alloc_logging.c | 138 + .../examples/alloc-logging/duk_alloc_logging.h | 10 + .../examples/alloc-logging/log2gnuplot.py | 41 + .../examples/alloc-torture/README.rst | 10 + .../examples/alloc-torture/duk_alloc_torture.c | 182 + .../examples/alloc-torture/duk_alloc_torture.h | 10 + .../duktape-1.8.0/examples/cmdline/README.rst | 6 + .../duktape-1.8.0/examples/cmdline/duk_cmdline.c | 1463 + .../examples/cmdline/duk_cmdline_ajduk.c | 1008 + .../examples/codepage-conv/README.rst | 8 + .../examples/codepage-conv/duk_codepage_conv.c | 54 + .../examples/codepage-conv/duk_codepage_conv.h | 8 + .../duktape-1.8.0/examples/codepage-conv/test.c | 286 + .../duktape-1.8.0/examples/coffee/README.rst | 10 + .../duktape-1.8.0/examples/coffee/globals.coffee | 7 + .../duktape-1.8.0/examples/coffee/hello.coffee | 2 + .../duktape-1.8.0/examples/coffee/mandel.coffee | 28 + .../examples/cpp-exceptions/README.rst | 29 + .../examples/cpp-exceptions/cpp_exceptions.cpp | 274 + .../examples/debug-trans-dvalue/Makefile | 17 + .../examples/debug-trans-dvalue/README.rst | 8 + .../examples/debug-trans-dvalue/duk_trans_dvalue.c | 1239 + .../examples/debug-trans-dvalue/duk_trans_dvalue.h | 113 + .../examples/debug-trans-dvalue/test.c | 236 + .../examples/debug-trans-socket/README.rst | 17 + .../examples/debug-trans-socket/duk_trans_socket.h | 15 + .../debug-trans-socket/duk_trans_socket_unix.c | 340 + .../debug-trans-socket/duk_trans_socket_windows.c | 414 + .../examples/dummy-date-provider/README.rst | 5 + .../dummy-date-provider/dummy_date_provider.c | 27 + .../duktape-1.8.0/examples/eval/README.rst | 5 + .../third_party/duktape-1.8.0/examples/eval/eval.c | 48 + .../duktape-1.8.0/examples/eventloop/README.rst | 76 + .../duktape-1.8.0/examples/eventloop/basic-test.js | 17 + .../duktape-1.8.0/examples/eventloop/c_eventloop.c | 618 + .../examples/eventloop/c_eventloop.js | 179 + .../examples/eventloop/client-socket-test.js | 24 + .../examples/eventloop/curses-timers.js | 79 + .../examples/eventloop/ecma_eventloop.js | 466 + .../duktape-1.8.0/examples/eventloop/fileio.c | 69 + .../duktape-1.8.0/examples/eventloop/main.c | 256 + .../duktape-1.8.0/examples/eventloop/ncurses.c | 105 + .../duktape-1.8.0/examples/eventloop/poll.c | 111 + .../examples/eventloop/server-socket-test.js | 34 + .../duktape-1.8.0/examples/eventloop/socket.c | 286 + .../duktape-1.8.0/examples/guide/README.rst | 5 + .../duktape-1.8.0/examples/guide/fib.js | 16 + .../duktape-1.8.0/examples/guide/prime.js | 32 + .../duktape-1.8.0/examples/guide/primecheck.c | 52 + .../duktape-1.8.0/examples/guide/process.js | 12 + .../duktape-1.8.0/examples/guide/processlines.c | 59 + .../duktape-1.8.0/examples/guide/uppercase.c | 42 + .../duktape-1.8.0/examples/hello/README.rst | 5 + .../duktape-1.8.0/examples/hello/hello.c | 38 + .../duktape-1.8.0/examples/jxpretty/README.rst | 5 + .../duktape-1.8.0/examples/jxpretty/jxpretty.c | 63 + .../duktape-1.8.0/examples/sandbox/README.rst | 5 + .../duktape-1.8.0/examples/sandbox/sandbox.c | 252 + .../third_party/duktape-1.8.0/extras/README.rst | 13 + .../src/third_party/duktape-1.8.0/license.spdx | 3933 + .../duktape-1.8.0/licenses/commonjs.txt | 2 + .../src/third_party/duktape-1.8.0/licenses/lua.txt | 1 + .../duktape-1.8.0/licenses/murmurhash2.txt | 21 + .../src/third_party/duktape-1.8.0/mandel.js | 53 + .../duktape-1.8.0/polyfills/console-minimal.js | 20 + .../polyfills/duktape-error-setter-nonwritable.js | 20 + .../polyfills/duktape-error-setter-writable.js | 19 + .../duktape-1.8.0/polyfills/duktape-isfastint.js | 38 + .../duktape-1.8.0/polyfills/object-assign.js | 45 + .../polyfills/object-prototype-definegetter.js | 11 + .../polyfills/object-prototype-definesetter.js | 11 + .../duktape-1.8.0/polyfills/performance-now.js | 25 + .../duktape-1.8.0/src-noline/duk_config.h | 3804 + .../third_party/duktape-1.8.0/src-noline/duktape.c | 86857 ++++++++ .../third_party/duktape-1.8.0/src-noline/duktape.h | 1581 + .../duktape-1.8.0/src-noline/metadata.json | 629 + .../duktape-1.8.0/src-separate/duk_alloc_default.c | 34 + .../duktape-1.8.0/src-separate/duk_api_buffer.c | 73 + .../duktape-1.8.0/src-separate/duk_api_bytecode.c | 727 + .../duktape-1.8.0/src-separate/duk_api_call.c | 566 + .../duktape-1.8.0/src-separate/duk_api_codec.c | 638 + .../duktape-1.8.0/src-separate/duk_api_compile.c | 194 + .../duktape-1.8.0/src-separate/duk_api_debug.c | 263 + .../duktape-1.8.0/src-separate/duk_api_heap.c | 194 + .../duktape-1.8.0/src-separate/duk_api_internal.h | 194 + .../duktape-1.8.0/src-separate/duk_api_logging.c | 52 + .../duktape-1.8.0/src-separate/duk_api_memory.c | 103 + .../duktape-1.8.0/src-separate/duk_api_object.c | 610 + .../duktape-1.8.0/src-separate/duk_api_stack.c | 4788 + .../duktape-1.8.0/src-separate/duk_api_string.c | 331 + .../duktape-1.8.0/src-separate/duk_api_var.c | 86 + .../duktape-1.8.0/src-separate/duk_bi_array.c | 1445 + .../duktape-1.8.0/src-separate/duk_bi_boolean.c | 68 + .../duktape-1.8.0/src-separate/duk_bi_buffer.c | 2884 + .../duktape-1.8.0/src-separate/duk_bi_date.c | 1728 + .../duktape-1.8.0/src-separate/duk_bi_date_unix.c | 309 + .../src-separate/duk_bi_date_windows.c | 98 + .../duktape-1.8.0/src-separate/duk_bi_duktape.c | 321 + .../duktape-1.8.0/src-separate/duk_bi_error.c | 383 + .../duktape-1.8.0/src-separate/duk_bi_function.c | 346 + .../duktape-1.8.0/src-separate/duk_bi_global.c | 1287 + .../duktape-1.8.0/src-separate/duk_bi_json.c | 3143 + .../duktape-1.8.0/src-separate/duk_bi_logger.c | 300 + .../duktape-1.8.0/src-separate/duk_bi_math.c | 348 + .../duktape-1.8.0/src-separate/duk_bi_number.c | 240 + .../duktape-1.8.0/src-separate/duk_bi_object.c | 577 + .../duktape-1.8.0/src-separate/duk_bi_pointer.c | 74 + .../duktape-1.8.0/src-separate/duk_bi_protos.h | 69 + .../duktape-1.8.0/src-separate/duk_bi_proxy.c | 66 + .../duktape-1.8.0/src-separate/duk_bi_regexp.c | 209 + .../duktape-1.8.0/src-separate/duk_bi_string.c | 1314 + .../duktape-1.8.0/src-separate/duk_bi_thread.c | 305 + .../duktape-1.8.0/src-separate/duk_bi_thrower.c | 10 + .../duktape-1.8.0/src-separate/duk_builtins.c | 776 + .../duktape-1.8.0/src-separate/duk_builtins.h | 854 + .../duktape-1.8.0/src-separate/duk_config.h | 3804 + .../duktape-1.8.0/src-separate/duk_debug.h | 185 + .../src-separate/duk_debug_fixedbuffer.c | 69 + .../duktape-1.8.0/src-separate/duk_debug_heap.c | 247 + .../duktape-1.8.0/src-separate/duk_debug_macros.c | 144 + .../src-separate/duk_debug_vsnprintf.c | 1049 + .../duktape-1.8.0/src-separate/duk_debugger.c | 2785 + .../duktape-1.8.0/src-separate/duk_debugger.h | 144 + .../duktape-1.8.0/src-separate/duk_error.h | 439 + .../duktape-1.8.0/src-separate/duk_error_augment.c | 567 + .../duktape-1.8.0/src-separate/duk_error_longjmp.c | 45 + .../duktape-1.8.0/src-separate/duk_error_macros.c | 163 + .../duktape-1.8.0/src-separate/duk_error_misc.c | 132 + .../duktape-1.8.0/src-separate/duk_error_throw.c | 181 + .../duktape-1.8.0/src-separate/duk_exception.h | 18 + .../duktape-1.8.0/src-separate/duk_forwdecl.h | 122 + .../duktape-1.8.0/src-separate/duk_hbuffer.h | 328 + .../duktape-1.8.0/src-separate/duk_hbuffer_alloc.c | 132 + .../duktape-1.8.0/src-separate/duk_hbuffer_ops.c | 82 + .../duktape-1.8.0/src-separate/duk_hbufferobject.h | 133 + .../src-separate/duk_hbufferobject_misc.c | 20 + .../src-separate/duk_hcompiledfunction.h | 239 + .../duktape-1.8.0/src-separate/duk_heap.h | 596 + .../duktape-1.8.0/src-separate/duk_heap_alloc.c | 1045 + .../src-separate/duk_heap_hashstring.c | 120 + .../src-separate/duk_heap_markandsweep.c | 1440 + .../duktape-1.8.0/src-separate/duk_heap_memory.c | 384 + .../duktape-1.8.0/src-separate/duk_heap_misc.c | 79 + .../duktape-1.8.0/src-separate/duk_heap_refcount.c | 624 + .../src-separate/duk_heap_stringcache.c | 298 + .../src-separate/duk_heap_stringtable.c | 1181 + .../duktape-1.8.0/src-separate/duk_heaphdr.h | 755 + .../src-separate/duk_hnativefunction.h | 32 + .../duktape-1.8.0/src-separate/duk_hobject.h | 926 + .../duktape-1.8.0/src-separate/duk_hobject_alloc.c | 192 + .../duktape-1.8.0/src-separate/duk_hobject_class.c | 132 + .../duktape-1.8.0/src-separate/duk_hobject_enum.c | 626 + .../src-separate/duk_hobject_finalizer.c | 110 + .../duktape-1.8.0/src-separate/duk_hobject_misc.c | 52 + .../src-separate/duk_hobject_pc2line.c | 250 + .../duktape-1.8.0/src-separate/duk_hobject_props.c | 5995 + .../duktape-1.8.0/src-separate/duk_hstring.h | 219 + .../duktape-1.8.0/src-separate/duk_hstring_misc.c | 45 + .../duktape-1.8.0/src-separate/duk_hthread.h | 380 + .../duktape-1.8.0/src-separate/duk_hthread_alloc.c | 95 + .../src-separate/duk_hthread_builtins.c | 835 + .../duktape-1.8.0/src-separate/duk_hthread_misc.c | 108 + .../src-separate/duk_hthread_stacks.c | 476 + .../duktape-1.8.0/src-separate/duk_initjs_min.js | 1 + .../duktape-1.8.0/src-separate/duk_internal.h | 79 + .../duktape-1.8.0/src-separate/duk_jmpbuf.h | 24 + .../duktape-1.8.0/src-separate/duk_js.h | 99 + .../duktape-1.8.0/src-separate/duk_js_bytecode.h | 205 + .../duktape-1.8.0/src-separate/duk_js_call.c | 2732 + .../duktape-1.8.0/src-separate/duk_js_compiler.c | 7865 + .../duktape-1.8.0/src-separate/duk_js_compiler.h | 227 + .../duktape-1.8.0/src-separate/duk_js_executor.c | 4515 + .../duktape-1.8.0/src-separate/duk_js_ops.c | 1387 + .../duktape-1.8.0/src-separate/duk_js_var.c | 1830 + .../duktape-1.8.0/src-separate/duk_json.h | 68 + .../duktape-1.8.0/src-separate/duk_lexer.c | 2069 + .../duktape-1.8.0/src-separate/duk_lexer.h | 432 + .../duktape-1.8.0/src-separate/duk_numconv.c | 2266 + .../duktape-1.8.0/src-separate/duk_numconv.h | 90 + .../duktape-1.8.0/src-separate/duk_regexp.h | 82 + .../src-separate/duk_regexp_compiler.c | 1072 + .../src-separate/duk_regexp_executor.c | 1008 + .../duktape-1.8.0/src-separate/duk_replacements.c | 82 + .../duktape-1.8.0/src-separate/duk_replacements.h | 28 + .../duktape-1.8.0/src-separate/duk_selftest.c | 373 + .../duktape-1.8.0/src-separate/duk_selftest.h | 12 + .../duktape-1.8.0/src-separate/duk_strings.c | 115 + .../duktape-1.8.0/src-separate/duk_strings.h | 252 + .../duktape-1.8.0/src-separate/duk_tval.c | 138 + .../duktape-1.8.0/src-separate/duk_tval.h | 518 + .../duktape-1.8.0/src-separate/duk_unicode.h | 267 + .../src-separate/duk_unicode_support.c | 1173 + .../src-separate/duk_unicode_tables.c | 6119 + .../duktape-1.8.0/src-separate/duk_util.h | 530 + .../src-separate/duk_util_bitdecoder.c | 70 + .../src-separate/duk_util_bitencoder.c | 43 + .../src-separate/duk_util_bufwriter.c | 360 + .../src-separate/duk_util_hashbytes.c | 57 + .../src-separate/duk_util_hashprime.c | 77 + .../duktape-1.8.0/src-separate/duk_util_misc.c | 222 + .../src-separate/duk_util_tinyrandom.c | 63 + .../duktape-1.8.0/src-separate/duktape.h | 1581 + .../src/third_party/duktape-1.8.0/src/duk_config.h | 3804 + .../src/third_party/duktape-1.8.0/src/duktape.c | 86982 ++++++++ .../src/third_party/duktape-1.8.0/src/duktape.h | 1581 + .../third_party/duktape-1.8.0/src/metadata.json | 629 + src/civetweb/src/third_party/lfs.c | 900 + src/civetweb/src/third_party/lfs.h | 32 + src/civetweb/src/third_party/lsqlite3.c | 2404 + src/civetweb/src/third_party/lua-5.1.5/COPYRIGHT | 34 + src/civetweb/src/third_party/lua-5.1.5/HISTORY | 183 + src/civetweb/src/third_party/lua-5.1.5/INSTALL | 99 + src/civetweb/src/third_party/lua-5.1.5/Makefile | 128 + src/civetweb/src/third_party/lua-5.1.5/README | 37 + .../src/third_party/lua-5.1.5/doc/contents.html | 497 + .../src/third_party/lua-5.1.5/doc/cover.png | Bin 0 -> 3305 bytes .../src/third_party/lua-5.1.5/doc/logo.gif | Bin 0 -> 4232 bytes src/civetweb/src/third_party/lua-5.1.5/doc/lua.1 | 163 + src/civetweb/src/third_party/lua-5.1.5/doc/lua.css | 83 + .../src/third_party/lua-5.1.5/doc/lua.html | 172 + src/civetweb/src/third_party/lua-5.1.5/doc/luac.1 | 136 + .../src/third_party/lua-5.1.5/doc/luac.html | 145 + .../src/third_party/lua-5.1.5/doc/manual.css | 24 + .../src/third_party/lua-5.1.5/doc/manual.html | 8804 + .../src/third_party/lua-5.1.5/doc/readme.html | 40 + .../src/third_party/lua-5.1.5/etc/Makefile | 44 + src/civetweb/src/third_party/lua-5.1.5/etc/README | 37 + src/civetweb/src/third_party/lua-5.1.5/etc/all.c | 38 + src/civetweb/src/third_party/lua-5.1.5/etc/lua.hpp | 9 + src/civetweb/src/third_party/lua-5.1.5/etc/lua.ico | Bin 0 -> 1078 bytes src/civetweb/src/third_party/lua-5.1.5/etc/lua.pc | 31 + .../src/third_party/lua-5.1.5/etc/luavs.bat | 28 + src/civetweb/src/third_party/lua-5.1.5/etc/min.c | 39 + .../src/third_party/lua-5.1.5/etc/noparser.c | 50 + .../src/third_party/lua-5.1.5/etc/strict.lua | 41 + .../src/third_party/lua-5.1.5/src/Makefile | 182 + src/civetweb/src/third_party/lua-5.1.5/src/lapi.c | 1087 + src/civetweb/src/third_party/lua-5.1.5/src/lapi.h | 16 + .../src/third_party/lua-5.1.5/src/lauxlib.c | 652 + .../src/third_party/lua-5.1.5/src/lauxlib.h | 174 + .../src/third_party/lua-5.1.5/src/lbaselib.c | 653 + src/civetweb/src/third_party/lua-5.1.5/src/lcode.c | 831 + src/civetweb/src/third_party/lua-5.1.5/src/lcode.h | 76 + .../src/third_party/lua-5.1.5/src/ldblib.c | 398 + .../src/third_party/lua-5.1.5/src/ldebug.c | 638 + .../src/third_party/lua-5.1.5/src/ldebug.h | 33 + src/civetweb/src/third_party/lua-5.1.5/src/ldo.c | 519 + src/civetweb/src/third_party/lua-5.1.5/src/ldo.h | 57 + src/civetweb/src/third_party/lua-5.1.5/src/ldump.c | 164 + src/civetweb/src/third_party/lua-5.1.5/src/lfunc.c | 174 + src/civetweb/src/third_party/lua-5.1.5/src/lfunc.h | 34 + src/civetweb/src/third_party/lua-5.1.5/src/lgc.c | 710 + src/civetweb/src/third_party/lua-5.1.5/src/lgc.h | 110 + src/civetweb/src/third_party/lua-5.1.5/src/linit.c | 38 + .../src/third_party/lua-5.1.5/src/liolib.c | 556 + src/civetweb/src/third_party/lua-5.1.5/src/llex.c | 463 + src/civetweb/src/third_party/lua-5.1.5/src/llex.h | 81 + .../src/third_party/lua-5.1.5/src/llimits.h | 128 + .../src/third_party/lua-5.1.5/src/lmathlib.c | 263 + src/civetweb/src/third_party/lua-5.1.5/src/lmem.c | 86 + src/civetweb/src/third_party/lua-5.1.5/src/lmem.h | 49 + .../src/third_party/lua-5.1.5/src/loadlib.c | 666 + .../src/third_party/lua-5.1.5/src/lobject.c | 214 + .../src/third_party/lua-5.1.5/src/lobject.h | 381 + .../src/third_party/lua-5.1.5/src/lopcodes.c | 102 + .../src/third_party/lua-5.1.5/src/lopcodes.h | 268 + .../src/third_party/lua-5.1.5/src/loslib.c | 243 + .../src/third_party/lua-5.1.5/src/lparser.c | 1339 + .../src/third_party/lua-5.1.5/src/lparser.h | 82 + .../src/third_party/lua-5.1.5/src/lstate.c | 214 + .../src/third_party/lua-5.1.5/src/lstate.h | 169 + .../src/third_party/lua-5.1.5/src/lstring.c | 111 + .../src/third_party/lua-5.1.5/src/lstring.h | 31 + .../src/third_party/lua-5.1.5/src/lstrlib.c | 871 + .../src/third_party/lua-5.1.5/src/ltable.c | 588 + .../src/third_party/lua-5.1.5/src/ltable.h | 40 + .../src/third_party/lua-5.1.5/src/ltablib.c | 287 + src/civetweb/src/third_party/lua-5.1.5/src/ltm.c | 75 + src/civetweb/src/third_party/lua-5.1.5/src/ltm.h | 54 + src/civetweb/src/third_party/lua-5.1.5/src/lua.c | 392 + src/civetweb/src/third_party/lua-5.1.5/src/lua.h | 388 + src/civetweb/src/third_party/lua-5.1.5/src/luac.c | 200 + .../src/third_party/lua-5.1.5/src/luaconf.h | 763 + .../src/third_party/lua-5.1.5/src/lualib.h | 53 + .../src/third_party/lua-5.1.5/src/lundump.c | 227 + .../src/third_party/lua-5.1.5/src/lundump.h | 36 + src/civetweb/src/third_party/lua-5.1.5/src/lvm.c | 767 + src/civetweb/src/third_party/lua-5.1.5/src/lvm.h | 36 + src/civetweb/src/third_party/lua-5.1.5/src/lzio.c | 82 + src/civetweb/src/third_party/lua-5.1.5/src/lzio.h | 67 + src/civetweb/src/third_party/lua-5.1.5/src/print.c | 227 + src/civetweb/src/third_party/lua-5.1.5/test/README | 26 + .../src/third_party/lua-5.1.5/test/bisect.lua | 27 + src/civetweb/src/third_party/lua-5.1.5/test/cf.lua | 16 + .../src/third_party/lua-5.1.5/test/echo.lua | 5 + .../src/third_party/lua-5.1.5/test/env.lua | 7 + .../src/third_party/lua-5.1.5/test/factorial.lua | 32 + .../src/third_party/lua-5.1.5/test/fib.lua | 40 + .../src/third_party/lua-5.1.5/test/fibfor.lua | 13 + .../src/third_party/lua-5.1.5/test/globals.lua | 13 + .../src/third_party/lua-5.1.5/test/hello.lua | 3 + .../src/third_party/lua-5.1.5/test/life.lua | 111 + .../src/third_party/lua-5.1.5/test/luac.lua | 7 + .../src/third_party/lua-5.1.5/test/printf.lua | 7 + .../src/third_party/lua-5.1.5/test/readonly.lua | 12 + .../src/third_party/lua-5.1.5/test/sieve.lua | 29 + .../src/third_party/lua-5.1.5/test/sort.lua | 66 + .../src/third_party/lua-5.1.5/test/table.lua | 12 + .../src/third_party/lua-5.1.5/test/trace-calls.lua | 32 + .../third_party/lua-5.1.5/test/trace-globals.lua | 38 + src/civetweb/src/third_party/lua-5.1.5/test/xd.lua | 14 + src/civetweb/src/third_party/lua-5.2.4/Makefile | 114 + src/civetweb/src/third_party/lua-5.2.4/README | 6 + .../src/third_party/lua-5.2.4/doc/contents.html | 608 + .../src/third_party/lua-5.2.4/doc/logo.gif | Bin 0 -> 4232 bytes src/civetweb/src/third_party/lua-5.2.4/doc/lua.1 | 116 + src/civetweb/src/third_party/lua-5.2.4/doc/lua.css | 106 + src/civetweb/src/third_party/lua-5.2.4/doc/luac.1 | 118 + .../src/third_party/lua-5.2.4/doc/manual.css | 27 + .../src/third_party/lua-5.2.4/doc/manual.html | 10508 + .../lua-5.2.4/doc/osi-certified-72x60.png | Bin 0 -> 3774 bytes .../src/third_party/lua-5.2.4/doc/readme.html | 417 + .../src/third_party/lua-5.2.4/src/Makefile | 187 + src/civetweb/src/third_party/lua-5.2.4/src/lapi.c | 1284 + src/civetweb/src/third_party/lua-5.2.4/src/lapi.h | 24 + .../src/third_party/lua-5.2.4/src/lauxlib.c | 959 + .../src/third_party/lua-5.2.4/src/lauxlib.h | 212 + .../src/third_party/lua-5.2.4/src/lbaselib.c | 458 + .../src/third_party/lua-5.2.4/src/lbitlib.c | 212 + src/civetweb/src/third_party/lua-5.2.4/src/lcode.c | 881 + src/civetweb/src/third_party/lua-5.2.4/src/lcode.h | 83 + .../src/third_party/lua-5.2.4/src/lcorolib.c | 155 + .../src/third_party/lua-5.2.4/src/lctype.c | 52 + .../src/third_party/lua-5.2.4/src/lctype.h | 95 + .../src/third_party/lua-5.2.4/src/ldblib.c | 408 + .../src/third_party/lua-5.2.4/src/ldebug.c | 610 + .../src/third_party/lua-5.2.4/src/ldebug.h | 34 + src/civetweb/src/third_party/lua-5.2.4/src/ldo.c | 681 + src/civetweb/src/third_party/lua-5.2.4/src/ldo.h | 46 + src/civetweb/src/third_party/lua-5.2.4/src/ldump.c | 173 + src/civetweb/src/third_party/lua-5.2.4/src/lfunc.c | 161 + src/civetweb/src/third_party/lua-5.2.4/src/lfunc.h | 33 + src/civetweb/src/third_party/lua-5.2.4/src/lgc.c | 1220 + src/civetweb/src/third_party/lua-5.2.4/src/lgc.h | 157 + src/civetweb/src/third_party/lua-5.2.4/src/linit.c | 67 + .../src/third_party/lua-5.2.4/src/liolib.c | 666 + src/civetweb/src/third_party/lua-5.2.4/src/llex.c | 530 + src/civetweb/src/third_party/lua-5.2.4/src/llex.h | 78 + .../src/third_party/lua-5.2.4/src/llimits.h | 309 + .../src/third_party/lua-5.2.4/src/lmathlib.c | 279 + src/civetweb/src/third_party/lua-5.2.4/src/lmem.c | 99 + src/civetweb/src/third_party/lua-5.2.4/src/lmem.h | 57 + .../src/third_party/lua-5.2.4/src/loadlib.c | 725 + .../src/third_party/lua-5.2.4/src/lobject.c | 287 + .../src/third_party/lua-5.2.4/src/lobject.h | 607 + .../src/third_party/lua-5.2.4/src/lopcodes.c | 107 + .../src/third_party/lua-5.2.4/src/lopcodes.h | 288 + .../src/third_party/lua-5.2.4/src/loslib.c | 323 + .../src/third_party/lua-5.2.4/src/lparser.c | 1638 + .../src/third_party/lua-5.2.4/src/lparser.h | 119 + .../src/third_party/lua-5.2.4/src/lstate.c | 323 + .../src/third_party/lua-5.2.4/src/lstate.h | 228 + .../src/third_party/lua-5.2.4/src/lstring.c | 185 + .../src/third_party/lua-5.2.4/src/lstring.h | 46 + .../src/third_party/lua-5.2.4/src/lstrlib.c | 1019 + .../src/third_party/lua-5.2.4/src/ltable.c | 588 + .../src/third_party/lua-5.2.4/src/ltable.h | 45 + .../src/third_party/lua-5.2.4/src/ltablib.c | 285 + src/civetweb/src/third_party/lua-5.2.4/src/ltm.c | 77 + src/civetweb/src/third_party/lua-5.2.4/src/ltm.h | 57 + src/civetweb/src/third_party/lua-5.2.4/src/lua.c | 497 + src/civetweb/src/third_party/lua-5.2.4/src/lua.h | 444 + src/civetweb/src/third_party/lua-5.2.4/src/lua.hpp | 9 + src/civetweb/src/third_party/lua-5.2.4/src/luac.c | 432 + .../src/third_party/lua-5.2.4/src/luaconf.h | 551 + .../src/third_party/lua-5.2.4/src/lualib.h | 55 + .../src/third_party/lua-5.2.4/src/lundump.c | 258 + .../src/third_party/lua-5.2.4/src/lundump.h | 28 + src/civetweb/src/third_party/lua-5.2.4/src/lvm.c | 867 + src/civetweb/src/third_party/lua-5.2.4/src/lvm.h | 44 + src/civetweb/src/third_party/lua-5.2.4/src/lzio.c | 76 + src/civetweb/src/third_party/lua-5.2.4/src/lzio.h | 65 + src/civetweb/src/third_party/lua-5.3.3/Makefile | 114 + src/civetweb/src/third_party/lua-5.3.3/README | 6 + .../src/third_party/lua-5.3.3/doc/contents.html | 618 + .../src/third_party/lua-5.3.3/doc/index.css | 21 + .../src/third_party/lua-5.3.3/doc/logo.gif | Bin 0 -> 4232 bytes src/civetweb/src/third_party/lua-5.3.3/doc/lua.1 | 111 + src/civetweb/src/third_party/lua-5.3.3/doc/lua.css | 164 + src/civetweb/src/third_party/lua-5.3.3/doc/luac.1 | 118 + .../src/third_party/lua-5.3.3/doc/manual.css | 21 + .../src/third_party/lua-5.3.3/doc/manual.html | 10910 + .../lua-5.3.3/doc/osi-certified-72x60.png | Bin 0 -> 3774 bytes .../src/third_party/lua-5.3.3/doc/readme.html | 365 + .../src/third_party/lua-5.3.3/src/Makefile | 197 + src/civetweb/src/third_party/lua-5.3.3/src/lapi.c | 1298 + src/civetweb/src/third_party/lua-5.3.3/src/lapi.h | 24 + .../src/third_party/lua-5.3.3/src/lauxlib.c | 1035 + .../src/third_party/lua-5.3.3/src/lauxlib.h | 256 + .../src/third_party/lua-5.3.3/src/lbaselib.c | 498 + .../src/third_party/lua-5.3.3/src/lbitlib.c | 233 + src/civetweb/src/third_party/lua-5.3.3/src/lcode.c | 1199 + src/civetweb/src/third_party/lua-5.3.3/src/lcode.h | 88 + .../src/third_party/lua-5.3.3/src/lcorolib.c | 168 + .../src/third_party/lua-5.3.3/src/lctype.c | 55 + .../src/third_party/lua-5.3.3/src/lctype.h | 95 + .../src/third_party/lua-5.3.3/src/ldblib.c | 456 + .../src/third_party/lua-5.3.3/src/ldebug.c | 679 + .../src/third_party/lua-5.3.3/src/ldebug.h | 39 + src/civetweb/src/third_party/lua-5.3.3/src/ldo.c | 800 + src/civetweb/src/third_party/lua-5.3.3/src/ldo.h | 58 + src/civetweb/src/third_party/lua-5.3.3/src/ldump.c | 215 + src/civetweb/src/third_party/lua-5.3.3/src/lfunc.c | 151 + src/civetweb/src/third_party/lua-5.3.3/src/lfunc.h | 61 + src/civetweb/src/third_party/lua-5.3.3/src/lgc.c | 1176 + src/civetweb/src/third_party/lua-5.3.3/src/lgc.h | 147 + src/civetweb/src/third_party/lua-5.3.3/src/linit.c | 68 + .../src/third_party/lua-5.3.3/src/liolib.c | 768 + src/civetweb/src/third_party/lua-5.3.3/src/llex.c | 565 + src/civetweb/src/third_party/lua-5.3.3/src/llex.h | 85 + .../src/third_party/lua-5.3.3/src/llimits.h | 323 + .../src/third_party/lua-5.3.3/src/lmathlib.c | 407 + src/civetweb/src/third_party/lua-5.3.3/src/lmem.c | 100 + src/civetweb/src/third_party/lua-5.3.3/src/lmem.h | 69 + .../src/third_party/lua-5.3.3/src/loadlib.c | 787 + .../src/third_party/lua-5.3.3/src/lobject.c | 521 + .../src/third_party/lua-5.3.3/src/lobject.h | 549 + .../src/third_party/lua-5.3.3/src/lopcodes.c | 124 + .../src/third_party/lua-5.3.3/src/lopcodes.h | 295 + .../src/third_party/lua-5.3.3/src/loslib.c | 403 + .../src/third_party/lua-5.3.3/src/lparser.c | 1652 + .../src/third_party/lua-5.3.3/src/lparser.h | 133 + .../src/third_party/lua-5.3.3/src/lprefix.h | 45 + .../src/third_party/lua-5.3.3/src/lstate.c | 347 + .../src/third_party/lua-5.3.3/src/lstate.h | 234 + .../src/third_party/lua-5.3.3/src/lstring.c | 248 + .../src/third_party/lua-5.3.3/src/lstring.h | 49 + .../src/third_party/lua-5.3.3/src/lstrlib.c | 1582 + .../src/third_party/lua-5.3.3/src/ltable.c | 669 + .../src/third_party/lua-5.3.3/src/ltable.h | 58 + .../src/third_party/lua-5.3.3/src/ltablib.c | 450 + src/civetweb/src/third_party/lua-5.3.3/src/ltm.c | 165 + src/civetweb/src/third_party/lua-5.3.3/src/ltm.h | 76 + src/civetweb/src/third_party/lua-5.3.3/src/lua.c | 609 + src/civetweb/src/third_party/lua-5.3.3/src/lua.h | 486 + src/civetweb/src/third_party/lua-5.3.3/src/lua.hpp | 9 + src/civetweb/src/third_party/lua-5.3.3/src/luac.c | 449 + .../src/third_party/lua-5.3.3/src/luaconf.h | 767 + .../src/third_party/lua-5.3.3/src/lualib.h | 58 + .../src/third_party/lua-5.3.3/src/lundump.c | 279 + .../src/third_party/lua-5.3.3/src/lundump.h | 32 + .../src/third_party/lua-5.3.3/src/lutf8lib.c | 256 + src/civetweb/src/third_party/lua-5.3.3/src/lvm.c | 1322 + src/civetweb/src/third_party/lua-5.3.3/src/lvm.h | 113 + src/civetweb/src/third_party/lua-5.3.3/src/lzio.c | 68 + src/civetweb/src/third_party/lua-5.3.3/src/lzio.h | 66 + src/civetweb/src/third_party/sqlite3.c | 204433 ++++++++++++++++++ src/civetweb/src/third_party/sqlite3.h | 10694 + src/civetweb/src/timer.inl | 227 + src/civetweb/test/.leading.dot.txt | 1 + src/civetweb/test/1000images.lua | 199 + src/civetweb/test/100images.htm | 160 + src/civetweb/test/CMakeLists.txt | 250 + src/civetweb/test/HugeText.lua | 149 + src/civetweb/test/MakefileTest.mk | 88 + src/civetweb/test/MethodTest.xhtml | 200 + src/civetweb/test/README.md | 25 + src/civetweb/test/ajax/echo.cgi | 8 + src/civetweb/test/ajax/echo.cgi.old | 73 + src/civetweb/test/ajax/echo.lp | 9 + src/civetweb/test/ajax/echo.lua | 35 + src/civetweb/test/ajax/jquery.js | 4 + src/civetweb/test/ajax/test.html | 168 + src/civetweb/test/all_build_flags.pl | 28 + src/civetweb/test/bad.cgi | 6 + src/civetweb/test/bad2.cgi | 3 + src/civetweb/test/bad_page.lp | 1 + src/civetweb/test/bad_script.lua | 1 + src/civetweb/test/cgi_test.c | 40 + src/civetweb/test/cgi_test.html | 12 + src/civetweb/test/civetweb_check.h | 96 + src/civetweb/test/cors.html | 75 + src/civetweb/test/cors.reply.html | 7 + src/civetweb/test/cors.reply.lua | 86 + src/civetweb/test/cors.reply.shtml | 7 + src/civetweb/test/delayed.cgi | 20 + src/civetweb/test/dir with spaces/hello.cgi | 3 + src/civetweb/test/echo.lua | 41 + src/civetweb/test/embed.c | 182 + src/civetweb/test/env.cgi | 46 + src/civetweb/test/error.lua | 12 + src/civetweb/test/error404.htm | 10 + src/civetweb/test/exit.lua | 15 + src/civetweb/test/exploit.pl | 69 + src/civetweb/test/filehandler.lua | 93 + src/civetweb/test/form.html | 118 + src/civetweb/test/handle_form.lua | 123 + src/civetweb/test/hello.cgi | 7 + src/civetweb/test/hello.shtml | 5 + src/civetweb/test/hello.txt | 1 + src/civetweb/test/hello_gz.txt.gz | Bin 0 -> 66 bytes src/civetweb/test/hello_gz_unzipped.txt | 1 + src/civetweb/test/html_esc.lua | 60 + src/civetweb/test/imagetest/00.png | Bin 0 -> 566 bytes src/civetweb/test/imagetest/01.png | Bin 0 -> 587 bytes src/civetweb/test/imagetest/02.png | Bin 0 -> 701 bytes src/civetweb/test/imagetest/03.png | Bin 0 -> 741 bytes src/civetweb/test/imagetest/04.png | Bin 0 -> 622 bytes src/civetweb/test/imagetest/05.png | Bin 0 -> 711 bytes src/civetweb/test/imagetest/06.png | Bin 0 -> 748 bytes src/civetweb/test/imagetest/07.png | Bin 0 -> 645 bytes src/civetweb/test/imagetest/08.png | Bin 0 -> 759 bytes src/civetweb/test/imagetest/09.png | Bin 0 -> 760 bytes src/civetweb/test/imagetest/10.png | Bin 0 -> 584 bytes src/civetweb/test/imagetest/11.png | Bin 0 -> 386 bytes src/civetweb/test/imagetest/12.png | Bin 0 -> 529 bytes src/civetweb/test/imagetest/13.png | Bin 0 -> 575 bytes src/civetweb/test/imagetest/14.png | Bin 0 -> 451 bytes src/civetweb/test/imagetest/15.png | Bin 0 -> 547 bytes src/civetweb/test/imagetest/16.png | Bin 0 -> 600 bytes src/civetweb/test/imagetest/17.png | Bin 0 -> 477 bytes src/civetweb/test/imagetest/18.png | Bin 0 -> 597 bytes src/civetweb/test/imagetest/19.png | Bin 0 -> 600 bytes src/civetweb/test/imagetest/20.png | Bin 0 -> 700 bytes src/civetweb/test/imagetest/21.png | Bin 0 -> 533 bytes src/civetweb/test/imagetest/22.png | Bin 0 -> 514 bytes src/civetweb/test/imagetest/23.png | Bin 0 -> 679 bytes src/civetweb/test/imagetest/24.png | Bin 0 -> 564 bytes src/civetweb/test/imagetest/25.png | Bin 0 -> 656 bytes src/civetweb/test/imagetest/26.png | Bin 0 -> 694 bytes src/civetweb/test/imagetest/27.png | Bin 0 -> 586 bytes src/civetweb/test/imagetest/28.png | Bin 0 -> 695 bytes src/civetweb/test/imagetest/29.png | Bin 0 -> 712 bytes src/civetweb/test/imagetest/30.png | Bin 0 -> 736 bytes src/civetweb/test/imagetest/31.png | Bin 0 -> 572 bytes src/civetweb/test/imagetest/32.png | Bin 0 -> 682 bytes src/civetweb/test/imagetest/33.png | Bin 0 -> 556 bytes src/civetweb/test/imagetest/34.png | Bin 0 -> 612 bytes src/civetweb/test/imagetest/35.png | Bin 0 -> 695 bytes src/civetweb/test/imagetest/36.png | Bin 0 -> 739 bytes src/civetweb/test/imagetest/37.png | Bin 0 -> 636 bytes src/civetweb/test/imagetest/38.png | Bin 0 -> 747 bytes src/civetweb/test/imagetest/39.png | Bin 0 -> 736 bytes src/civetweb/test/imagetest/40.png | Bin 0 -> 623 bytes src/civetweb/test/imagetest/41.png | Bin 0 -> 448 bytes src/civetweb/test/imagetest/42.png | Bin 0 -> 560 bytes src/civetweb/test/imagetest/43.png | Bin 0 -> 610 bytes src/civetweb/test/imagetest/44.png | Bin 0 -> 441 bytes src/civetweb/test/imagetest/45.png | Bin 0 -> 584 bytes src/civetweb/test/imagetest/46.png | Bin 0 -> 632 bytes src/civetweb/test/imagetest/47.png | Bin 0 -> 522 bytes src/civetweb/test/imagetest/48.png | Bin 0 -> 634 bytes src/civetweb/test/imagetest/49.png | Bin 0 -> 637 bytes src/civetweb/test/imagetest/50.png | Bin 0 -> 709 bytes src/civetweb/test/imagetest/51.png | Bin 0 -> 550 bytes src/civetweb/test/imagetest/52.png | Bin 0 -> 661 bytes src/civetweb/test/imagetest/53.png | Bin 0 -> 696 bytes src/civetweb/test/imagetest/54.png | Bin 0 -> 587 bytes src/civetweb/test/imagetest/55.png | Bin 0 -> 544 bytes src/civetweb/test/imagetest/56.png | Bin 0 -> 718 bytes src/civetweb/test/imagetest/57.png | Bin 0 -> 610 bytes src/civetweb/test/imagetest/58.png | Bin 0 -> 721 bytes src/civetweb/test/imagetest/59.png | Bin 0 -> 725 bytes src/civetweb/test/imagetest/60.png | Bin 0 -> 751 bytes src/civetweb/test/imagetest/61.png | Bin 0 -> 600 bytes src/civetweb/test/imagetest/62.png | Bin 0 -> 710 bytes src/civetweb/test/imagetest/63.png | Bin 0 -> 744 bytes src/civetweb/test/imagetest/64.png | Bin 0 -> 631 bytes src/civetweb/test/imagetest/65.png | Bin 0 -> 726 bytes src/civetweb/test/imagetest/66.png | Bin 0 -> 584 bytes src/civetweb/test/imagetest/67.png | Bin 0 -> 659 bytes src/civetweb/test/imagetest/68.png | Bin 0 -> 766 bytes src/civetweb/test/imagetest/69.png | Bin 0 -> 766 bytes src/civetweb/test/imagetest/70.png | Bin 0 -> 640 bytes src/civetweb/test/imagetest/71.png | Bin 0 -> 474 bytes src/civetweb/test/imagetest/72.png | Bin 0 -> 584 bytes src/civetweb/test/imagetest/73.png | Bin 0 -> 631 bytes src/civetweb/test/imagetest/74.png | Bin 0 -> 517 bytes src/civetweb/test/imagetest/75.png | Bin 0 -> 603 bytes src/civetweb/test/imagetest/76.png | Bin 0 -> 650 bytes src/civetweb/test/imagetest/77.png | Bin 0 -> 453 bytes src/civetweb/test/imagetest/78.png | Bin 0 -> 653 bytes src/civetweb/test/imagetest/79.png | Bin 0 -> 655 bytes src/civetweb/test/imagetest/80.png | Bin 0 -> 759 bytes src/civetweb/test/imagetest/81.png | Bin 0 -> 596 bytes src/civetweb/test/imagetest/82.png | Bin 0 -> 714 bytes src/civetweb/test/imagetest/83.png | Bin 0 -> 751 bytes src/civetweb/test/imagetest/84.png | Bin 0 -> 634 bytes src/civetweb/test/imagetest/85.png | Bin 0 -> 724 bytes src/civetweb/test/imagetest/86.png | Bin 0 -> 766 bytes src/civetweb/test/imagetest/87.png | Bin 0 -> 660 bytes src/civetweb/test/imagetest/88.png | Bin 0 -> 579 bytes src/civetweb/test/imagetest/89.png | Bin 0 -> 778 bytes src/civetweb/test/imagetest/90.png | Bin 0 -> 756 bytes src/civetweb/test/imagetest/91.png | Bin 0 -> 604 bytes src/civetweb/test/imagetest/92.png | Bin 0 -> 716 bytes src/civetweb/test/imagetest/93.png | Bin 0 -> 735 bytes src/civetweb/test/imagetest/94.png | Bin 0 -> 634 bytes src/civetweb/test/imagetest/95.png | Bin 0 -> 726 bytes src/civetweb/test/imagetest/96.png | Bin 0 -> 763 bytes src/civetweb/test/imagetest/97.png | Bin 0 -> 660 bytes src/civetweb/test/imagetest/98.png | Bin 0 -> 776 bytes src/civetweb/test/imagetest/99.png | Bin 0 -> 590 bytes src/civetweb/test/linux.cgi | 9 + src/civetweb/test/linux_fail.cgi | 4 + src/civetweb/test/linux_fail_silent.cgi | 5 + src/civetweb/test/lua_preload_file.lua | 7 + src/civetweb/test/main.c | 103 + src/civetweb/test/page.lp | 71 + src/civetweb/test/page.lua | 71 + src/civetweb/test/page.ssjs | 19 + src/civetweb/test/page2.lp | 72 + src/civetweb/test/page2.lua | 91 + src/civetweb/test/page2.ssjs | 30 + src/civetweb/test/page3.lp | 20 + src/civetweb/test/page3.lua | 34 + src/civetweb/test/page3.ssjs | 61 + src/civetweb/test/page3a.lp | 20 + src/civetweb/test/page3r.lp | 20 + src/civetweb/test/page3v.lp | 20 + src/civetweb/test/page4.lua | 180 + src/civetweb/test/page5.lua | 8 + src/civetweb/test/page6.lua | 16 + src/civetweb/test/page_keep_alive.lua | 34 + src/civetweb/test/page_keep_alive_chunked.lua | 66 + src/civetweb/test/page_status.lua | 38 + src/civetweb/test/passfile | 3 + src/civetweb/test/prime.ssjs | 36 + src/civetweb/test/private.c | 1018 + src/civetweb/test/private.h | 28 + src/civetweb/test/private_exe.c | 82 + src/civetweb/test/private_exe.h | 31 + src/civetweb/test/protected/.htpasswd | 1 + src/civetweb/test/protected/content.txt | 5 + src/civetweb/test/public_func.c | 546 + src/civetweb/test/public_func.h | 28 + src/civetweb/test/public_server.c | 4953 + src/civetweb/test/public_server.h | 28 + src/civetweb/test/require_test.lua | 2 + src/civetweb/test/resource_script_demo.lua | 124 + src/civetweb/test/shared.c | 48 + src/civetweb/test/shared.h | 27 + src/civetweb/test/ssi_test.shtml | 37 + src/civetweb/test/syntax_error.ssjs | 7 + src/civetweb/test/test.ico | Bin 0 -> 1406 bytes src/civetweb/test/test.pl | 461 + src/civetweb/test/testclient.c | 151 + src/civetweb/test/timeout.cgi | 12 + src/civetweb/test/timertest.c | 350 + src/civetweb/test/timertest.h | 28 + src/civetweb/test/websocket.lua | 118 + src/civetweb/test/websocket.xhtml | 117 + src/civetweb/test/windows.cgi | 2 + src/civetweb/test/windows.cgi.cmd | 7 + src/civetweb/test/windows_fail.cgi | 2 + src/civetweb/test/windows_fail.cgi.cmd | 2 + src/civetweb/test/windows_fail_silent.cgi | 2 + src/civetweb/test/windows_fail_silent.cgi.cmd | 3 + src/civetweb/test/ws_status.lua | 137 + src/civetweb/test/x.php | 9 + 1198 files changed, 993718 insertions(+) create mode 100644 src/civetweb/.clang-format create mode 100644 src/civetweb/.gitattributes create mode 100644 src/civetweb/.gitignore create mode 100644 src/civetweb/.travis.yml create mode 100644 src/civetweb/CMakeLists.txt create mode 100644 src/civetweb/CREDITS.md create mode 100644 src/civetweb/LICENSE.md create mode 100644 src/civetweb/Makefile create mode 100644 src/civetweb/Makefile.deprecated create mode 100644 src/civetweb/Makefile.osx create mode 100644 src/civetweb/Qt/CivetWeb.pro create mode 100644 src/civetweb/README.md create mode 100644 src/civetweb/RELEASE_NOTES.md create mode 100644 src/civetweb/VisualStudio/buildRelease.pl create mode 100644 src/civetweb/VisualStudio/civetweb.sln create mode 100644 src/civetweb/VisualStudio/civetweb/civetweb.vcxproj create mode 100644 src/civetweb/VisualStudio/civetweb/civetweb.vcxproj.filters create mode 100644 src/civetweb/VisualStudio/civetweb_lua/civetweb_lua.vcxproj create mode 100644 src/civetweb/VisualStudio/civetweb_lua/civetweb_lua.vcxproj.filters create mode 100644 src/civetweb/VisualStudio/civetweb_yassl/civetweb_yassl.sln create mode 100644 src/civetweb/VisualStudio/civetweb_yassl/civetweb_yassl/civetweb_yassl.vcxproj create mode 100644 src/civetweb/VisualStudio/civetweb_yassl/civetweb_yassl/civetweb_yassl.vcxproj.filters create mode 100644 src/civetweb/VisualStudio/civetweb_yassl/yassl_lib/yassl_lib.vcxproj create mode 100644 src/civetweb/VisualStudio/civetweb_yassl/yassl_lib/yassl_lib.vcxproj.filters create mode 100644 src/civetweb/VisualStudio/duktape_lib/duktape_lib.vcxproj create mode 100644 src/civetweb/VisualStudio/duktape_lib/duktape_lib.vcxproj.filters create mode 100644 src/civetweb/VisualStudio/ex_embed_cpp/ex_embed_cpp.vcxproj create mode 100644 src/civetweb/VisualStudio/ex_embed_cpp/ex_embed_cpp.vcxproj.filters create mode 100644 src/civetweb/VisualStudio/ex_embedded_c/ex_embedded_c.vcxproj create mode 100644 src/civetweb/VisualStudio/ex_embedded_c/ex_embedded_c.vcxproj.filters create mode 100644 src/civetweb/VisualStudio/ex_websocket/ex_websocket.vcxproj create mode 100644 src/civetweb/VisualStudio/ex_websocket/ex_websocket.vcxproj.filters create mode 100644 src/civetweb/VisualStudio/ex_websocket_client/ex_websocket_client.vcxproj create mode 100644 src/civetweb/VisualStudio/ex_websocket_client/ex_websocket_client.vcxproj.filters create mode 100644 src/civetweb/VisualStudio/lua_lib/lua_lib.vcxproj create mode 100644 src/civetweb/VisualStudio/lua_lib/lua_lib.vcxproj.filters create mode 100644 src/civetweb/VisualStudio/unit_test/unit_test.vcxproj create mode 100644 src/civetweb/VisualStudio/unit_test/unit_test.vcxproj.filters create mode 100644 src/civetweb/VisualStudio/upload/upload.vcxproj create mode 100644 src/civetweb/VisualStudio/upload/upload.vcxproj.filters create mode 100644 src/civetweb/_config.yml create mode 100644 src/civetweb/appveyor.yml create mode 100755 src/civetweb/build create mode 100644 src/civetweb/build.cmd create mode 100644 src/civetweb/ci/test/01_basic/basic_spec.lua create mode 100644 src/civetweb/ci/test/01_basic/docroot/01_basic_test_dir/git_keep_empty_dir create mode 100644 src/civetweb/ci/test/01_basic/docroot/01_basic_test_file create mode 100644 src/civetweb/ci/test/README.md create mode 100644 src/civetweb/ci/test/civet.lua create mode 100755 src/civetweb/ci/travis/install_rocks.sh create mode 100755 src/civetweb/ci/travis/lua_env.sh create mode 100755 src/civetweb/ci/travis/platform.sh create mode 100755 src/civetweb/ci/travis/run_ci_tests.sh create mode 100755 src/civetweb/ci/travis/setup_lua.sh create mode 100644 src/civetweb/cmake/AddCCompilerFlag.cmake create mode 100644 src/civetweb/cmake/AddCXXCompilerFlag.cmake create mode 100644 src/civetweb/cmake/DetermineTargetArchitecture.cmake create mode 100644 src/civetweb/cmake/FindLibDl.cmake create mode 100644 src/civetweb/cmake/FindLibM.cmake create mode 100644 src/civetweb/cmake/FindLibRt.cmake create mode 100644 src/civetweb/cmake/FindLibSubunit.cmake create mode 100644 src/civetweb/cmake/FindWinSock.cmake create mode 100644 src/civetweb/cmake/check/c82fe8888aacfe784476112edd3878256d2e30bc.patch create mode 100644 src/civetweb/cmake/check/patch.cmake create mode 100644 src/civetweb/contrib/buildroot/Config.in create mode 100644 src/civetweb/contrib/buildroot/civetweb.mk create mode 100644 src/civetweb/distribution/arch/PKGBUILD.git.example create mode 100644 src/civetweb/distribution/arch/civetweb.service create mode 100644 src/civetweb/docs/APIReference.md create mode 100644 src/civetweb/docs/Building.md create mode 100644 src/civetweb/docs/Contribution.md create mode 100644 src/civetweb/docs/Embedding.md create mode 100644 src/civetweb/docs/Installing.md create mode 100644 src/civetweb/docs/Interface_Changes_1.10.md create mode 100644 src/civetweb/docs/OpenSSL.md create mode 100644 src/civetweb/docs/README.md create mode 100644 src/civetweb/docs/UserManual.md create mode 100644 src/civetweb/docs/_config.yml create mode 100644 src/civetweb/docs/api/mg_callbacks.md create mode 100644 src/civetweb/docs/api/mg_check_digest_access_authentication.md create mode 100644 src/civetweb/docs/api/mg_check_feature.md create mode 100644 src/civetweb/docs/api/mg_client_cert.md create mode 100644 src/civetweb/docs/api/mg_client_options.md create mode 100644 src/civetweb/docs/api/mg_close_connection.md create mode 100644 src/civetweb/docs/api/mg_connect_client.md create mode 100644 src/civetweb/docs/api/mg_connect_client_secure.md create mode 100644 src/civetweb/docs/api/mg_connect_websocket_client.md create mode 100644 src/civetweb/docs/api/mg_cry.md create mode 100644 src/civetweb/docs/api/mg_download.md create mode 100644 src/civetweb/docs/api/mg_exit_library.md create mode 100644 src/civetweb/docs/api/mg_form_data_handler.md create mode 100644 src/civetweb/docs/api/mg_get_builtin_mime_type.md create mode 100644 src/civetweb/docs/api/mg_get_connection_info.md create mode 100644 src/civetweb/docs/api/mg_get_context.md create mode 100644 src/civetweb/docs/api/mg_get_context_info.md create mode 100644 src/civetweb/docs/api/mg_get_cookie.md create mode 100644 src/civetweb/docs/api/mg_get_header.md create mode 100644 src/civetweb/docs/api/mg_get_option.md create mode 100644 src/civetweb/docs/api/mg_get_ports.md create mode 100644 src/civetweb/docs/api/mg_get_request_info.md create mode 100644 src/civetweb/docs/api/mg_get_request_link.md create mode 100644 src/civetweb/docs/api/mg_get_response.md create mode 100644 src/civetweb/docs/api/mg_get_response_code_text.md create mode 100644 src/civetweb/docs/api/mg_get_response_info.md create mode 100644 src/civetweb/docs/api/mg_get_server_ports.md create mode 100644 src/civetweb/docs/api/mg_get_system_info.md create mode 100644 src/civetweb/docs/api/mg_get_user_connection_data.md create mode 100644 src/civetweb/docs/api/mg_get_user_data.md create mode 100644 src/civetweb/docs/api/mg_get_valid_option_names.md create mode 100644 src/civetweb/docs/api/mg_get_valid_options.md create mode 100644 src/civetweb/docs/api/mg_get_var.md create mode 100644 src/civetweb/docs/api/mg_get_var2.md create mode 100644 src/civetweb/docs/api/mg_handle_form_request.md create mode 100644 src/civetweb/docs/api/mg_header.md create mode 100644 src/civetweb/docs/api/mg_init_library.md create mode 100644 src/civetweb/docs/api/mg_lock_connection.md create mode 100644 src/civetweb/docs/api/mg_lock_context.md create mode 100644 src/civetweb/docs/api/mg_md5.md create mode 100644 src/civetweb/docs/api/mg_modify_passwords_file.md create mode 100644 src/civetweb/docs/api/mg_option.md create mode 100644 src/civetweb/docs/api/mg_printf.md create mode 100644 src/civetweb/docs/api/mg_read.md create mode 100644 src/civetweb/docs/api/mg_request_info.md create mode 100644 src/civetweb/docs/api/mg_response_info.md create mode 100644 src/civetweb/docs/api/mg_send_chunk.md create mode 100644 src/civetweb/docs/api/mg_send_digest_access_authentication_request.md create mode 100644 src/civetweb/docs/api/mg_send_file.md create mode 100644 src/civetweb/docs/api/mg_send_http_error.md create mode 100644 src/civetweb/docs/api/mg_send_mime_file.md create mode 100644 src/civetweb/docs/api/mg_send_mime_file2.md create mode 100644 src/civetweb/docs/api/mg_server_ports.md create mode 100644 src/civetweb/docs/api/mg_set_auth_handler.md create mode 100644 src/civetweb/docs/api/mg_set_request_handler.md create mode 100644 src/civetweb/docs/api/mg_set_user_connection_data.md create mode 100644 src/civetweb/docs/api/mg_set_websocket_handler.md create mode 100644 src/civetweb/docs/api/mg_start.md create mode 100644 src/civetweb/docs/api/mg_start_thread.md create mode 100644 src/civetweb/docs/api/mg_stop.md create mode 100644 src/civetweb/docs/api/mg_store_body.md create mode 100644 src/civetweb/docs/api/mg_strcasecmp.md create mode 100644 src/civetweb/docs/api/mg_strncasecmp.md create mode 100644 src/civetweb/docs/api/mg_unlock_connection.md create mode 100644 src/civetweb/docs/api/mg_unlock_context.md create mode 100644 src/civetweb/docs/api/mg_upload.md create mode 100644 src/civetweb/docs/api/mg_url_decode.md create mode 100644 src/civetweb/docs/api/mg_url_encode.md create mode 100644 src/civetweb/docs/api/mg_version.md create mode 100644 src/civetweb/docs/api/mg_websocket_client_write.md create mode 100644 src/civetweb/docs/api/mg_websocket_write.md create mode 100644 src/civetweb/docs/api/mg_write.md create mode 100644 src/civetweb/docs/yaSSL.md create mode 100644 src/civetweb/examples/README.md create mode 100644 src/civetweb/examples/_obsolete/chat/Makefile create mode 100644 src/civetweb/examples/_obsolete/chat/chat.c create mode 100644 src/civetweb/examples/_obsolete/docroot/favicon.ico create mode 100644 src/civetweb/examples/_obsolete/docroot/index.html create mode 100644 src/civetweb/examples/_obsolete/docroot/jquery.js create mode 100644 src/civetweb/examples/_obsolete/docroot/login.html create mode 100644 src/civetweb/examples/_obsolete/docroot/logo.png create mode 100644 src/civetweb/examples/_obsolete/docroot/main.js create mode 100644 src/civetweb/examples/_obsolete/docroot/prime_numbers.lp create mode 100644 src/civetweb/examples/_obsolete/docroot/style.css create mode 100644 src/civetweb/examples/_obsolete/hello/Makefile create mode 100644 src/civetweb/examples/_obsolete/hello/hello.c create mode 100644 src/civetweb/examples/_obsolete/lua/lua_dll.c create mode 100644 src/civetweb/examples/_obsolete/post/Makefile create mode 100644 src/civetweb/examples/_obsolete/post/post.c create mode 100644 src/civetweb/examples/_obsolete/upload/Makefile create mode 100644 src/civetweb/examples/_obsolete/upload/upload.c create mode 100644 src/civetweb/examples/_obsolete/websocket/Makefile create mode 100644 src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.c create mode 100644 src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.h create mode 100644 src/civetweb/examples/_obsolete/websocket/websock.htm create mode 100644 src/civetweb/examples/_obsolete/websocket/websocket.c create mode 100644 src/civetweb/examples/_obsolete/websocket_client/Makefile create mode 100644 src/civetweb/examples/_obsolete/websocket_client/ssl/server.crt create mode 100644 src/civetweb/examples/_obsolete/websocket_client/ssl/server.csr create mode 100644 src/civetweb/examples/_obsolete/websocket_client/ssl/server.key create mode 100644 src/civetweb/examples/_obsolete/websocket_client/ssl/server.key.orig create mode 100644 src/civetweb/examples/_obsolete/websocket_client/ssl/server.pem create mode 100644 src/civetweb/examples/_obsolete/websocket_client/websocket_client.c create mode 100644 src/civetweb/examples/_obsolete/ws_server/Makefile create mode 100644 src/civetweb/examples/_obsolete/ws_server/docroot/index.html create mode 100644 src/civetweb/examples/_obsolete/ws_server/ws_server.c create mode 100644 src/civetweb/examples/embedded_c/Makefile create mode 100644 src/civetweb/examples/embedded_c/embedded_c.c create mode 100644 src/civetweb/examples/embedded_cpp/Makefile create mode 100644 src/civetweb/examples/embedded_cpp/embedded_cpp.cpp create mode 100644 src/civetweb/examples/https/README.md create mode 100644 src/civetweb/examples/https/civetweb.conf create mode 100755 src/civetweb/format.bat create mode 100644 src/civetweb/include/CivetServer.h create mode 100644 src/civetweb/include/civetweb.h create mode 100644 src/civetweb/mingw.cmd create mode 100644 src/civetweb/resources/Info.plist create mode 100644 src/civetweb/resources/Makefile.in-duktape create mode 100644 src/civetweb/resources/Makefile.in-lua create mode 100644 src/civetweb/resources/Makefile.in-os create mode 100644 src/civetweb/resources/cert/client.crt create mode 100644 src/civetweb/resources/cert/client.csr create mode 100644 src/civetweb/resources/cert/client.key create mode 100644 src/civetweb/resources/cert/client.key.orig create mode 100644 src/civetweb/resources/cert/client.pem create mode 100644 src/civetweb/resources/cert/client.pfx create mode 100644 src/civetweb/resources/cert/make_certs.bat create mode 100644 src/civetweb/resources/cert/make_certs.sh create mode 100644 src/civetweb/resources/cert/server.crt create mode 100644 src/civetweb/resources/cert/server.csr create mode 100644 src/civetweb/resources/cert/server.key create mode 100644 src/civetweb/resources/cert/server.key.orig create mode 100644 src/civetweb/resources/cert/server.pem create mode 100644 src/civetweb/resources/cert/server.pin create mode 100644 src/civetweb/resources/cert/server_bkup.crt create mode 100644 src/civetweb/resources/cert/server_bkup.csr create mode 100644 src/civetweb/resources/cert/server_bkup.key create mode 100644 src/civetweb/resources/cert/server_bkup.key.orig create mode 100644 src/civetweb/resources/cert/server_bkup.pem create mode 100644 src/civetweb/resources/cert/server_bkup.pin create mode 100644 src/civetweb/resources/civetweb.conf create mode 100644 src/civetweb/resources/civetweb.icns create mode 100644 src/civetweb/resources/civetweb.psd create mode 100644 src/civetweb/resources/civetweb_16x16.png create mode 100644 src/civetweb/resources/civetweb_16x16@2.png create mode 100644 src/civetweb/resources/civetweb_22x22.png create mode 100644 src/civetweb/resources/civetweb_22x22@2.png create mode 100644 src/civetweb/resources/civetweb_32x32.png create mode 100644 src/civetweb/resources/civetweb_32x32@2.png create mode 100644 src/civetweb/resources/civetweb_64x64.png create mode 100644 src/civetweb/resources/civetweb_64x64@2.png create mode 100755 src/civetweb/resources/coverity_check.sh create mode 100644 src/civetweb/resources/duktape-logo.png create mode 100644 src/civetweb/resources/itworks.html create mode 100644 src/civetweb/resources/jni/Android.mk create mode 100644 src/civetweb/resources/lua-logo.jpg create mode 100644 src/civetweb/resources/luafilesystem-logo.jpg create mode 100644 src/civetweb/resources/luasqlite-logo.jpg create mode 100644 src/civetweb/resources/luaxml-logo.jpg create mode 100644 src/civetweb/resources/mingw.bat create mode 100644 src/civetweb/resources/res.rc create mode 100644 src/civetweb/resources/sqlite3-logo.jpg create mode 100644 src/civetweb/resources/ssl_cert.pem create mode 100644 src/civetweb/resources/systray.ico create mode 100644 src/civetweb/src/CMakeLists.txt create mode 100644 src/civetweb/src/CivetServer.cpp create mode 100644 src/civetweb/src/civetweb.c create mode 100644 src/civetweb/src/civetweb_private_lua.h create mode 100644 src/civetweb/src/file_ops.inl create mode 100644 src/civetweb/src/handle_form.inl create mode 100644 src/civetweb/src/main.c create mode 100644 src/civetweb/src/md5.inl create mode 100644 src/civetweb/src/mod_duktape.inl create mode 100644 src/civetweb/src/mod_lua.inl create mode 100644 src/civetweb/src/sha1.inl create mode 100644 src/civetweb/src/third_party/LuaXML.lua create mode 100644 src/civetweb/src/third_party/LuaXML_lib.c create mode 100644 src/civetweb/src/third_party/civetweb_lua.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/AUTHORS.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/LICENSE.txt create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/Makefile.cmdline create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/Makefile.codepage create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/Makefile.coffee create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/Makefile.dukdebug create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/Makefile.eval create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/Makefile.eventloop create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/Makefile.hello create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/Makefile.jxpretty create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/Makefile.sandbox create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/Makefile.sharedlibrary create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/config/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/config/duk_config.h-modular-dll create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/config/duk_config.h-modular-static create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/config/genconfig.py create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/config/genconfig_metadata.tar.gz create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/debugger/Makefile create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/debugger/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_classnames.yaml create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debug.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debug_meta.json create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debug_proxy.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debugcommands.yaml create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debugerrors.yaml create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_opcodes.yaml create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/debugger/merge_debug_meta.py create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/debugger/package.json create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/debugger/static/index.html create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/debugger/static/style.css create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/debugger/static/webui.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/duk_build_meta.json create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-hybrid/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-hybrid/duk_alloc_hybrid.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-hybrid/duk_alloc_hybrid.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/duk_alloc_logging.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/duk_alloc_logging.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/log2gnuplot.py create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-torture/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-torture/duk_alloc_torture.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-torture/duk_alloc_torture.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/cmdline/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/cmdline/duk_cmdline.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/cmdline/duk_cmdline_ajduk.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/duk_codepage_conv.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/duk_codepage_conv.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/test.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/globals.coffee create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/hello.coffee create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/mandel.coffee create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/cpp-exceptions/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/cpp-exceptions/cpp_exceptions.cpp create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/Makefile create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/duk_trans_dvalue.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/duk_trans_dvalue.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/test.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/duk_trans_socket.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/duk_trans_socket_unix.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/duk_trans_socket_windows.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/dummy-date-provider/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/dummy-date-provider/dummy_date_provider.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/eval/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/eval/eval.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/basic-test.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/c_eventloop.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/c_eventloop.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/client-socket-test.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/curses-timers.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/ecma_eventloop.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/fileio.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/main.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/ncurses.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/poll.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/server-socket-test.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/socket.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/guide/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/guide/fib.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/guide/prime.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/guide/primecheck.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/guide/process.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/guide/processlines.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/guide/uppercase.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/hello/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/hello/hello.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/jxpretty/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/jxpretty/jxpretty.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/sandbox/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/examples/sandbox/sandbox.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/extras/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/license.spdx create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/licenses/commonjs.txt create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/licenses/lua.txt create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/licenses/murmurhash2.txt create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/mandel.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/polyfills/console-minimal.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/polyfills/duktape-error-setter-nonwritable.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/polyfills/duktape-error-setter-writable.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/polyfills/duktape-isfastint.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/polyfills/object-assign.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/polyfills/object-prototype-definegetter.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/polyfills/object-prototype-definesetter.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/polyfills/performance-now.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-noline/duk_config.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-noline/duktape.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-noline/duktape.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-noline/metadata.json create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_alloc_default.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_api_buffer.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_api_bytecode.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_api_call.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_api_codec.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_api_compile.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_api_debug.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_api_heap.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_api_internal.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_api_logging.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_api_memory.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_api_object.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_api_stack.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_api_string.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_api_var.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_array.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_boolean.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_buffer.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_date.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_date_unix.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_date_windows.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_duktape.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_error.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_function.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_global.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_json.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_logger.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_math.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_number.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_object.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_pointer.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_protos.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_proxy.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_regexp.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_string.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_thread.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_thrower.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_builtins.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_builtins.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_config.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_debug.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_debug_fixedbuffer.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_debug_heap.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_debug_macros.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_debug_vsnprintf.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_debugger.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_debugger.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_error.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_error_augment.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_error_longjmp.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_error_macros.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_error_misc.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_error_throw.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_exception.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_forwdecl.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hbuffer.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hbuffer_alloc.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hbuffer_ops.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hbufferobject.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hbufferobject_misc.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hcompiledfunction.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_heap.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_heap_alloc.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_heap_hashstring.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_heap_markandsweep.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_heap_memory.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_heap_misc.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_heap_refcount.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_heap_stringcache.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_heap_stringtable.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_heaphdr.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hnativefunction.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hobject.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hobject_alloc.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hobject_class.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hobject_enum.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hobject_finalizer.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hobject_misc.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hobject_pc2line.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hobject_props.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hstring.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hstring_misc.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hthread.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hthread_alloc.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hthread_builtins.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hthread_misc.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_hthread_stacks.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_initjs_min.js create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_internal.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_jmpbuf.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_js.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_js_bytecode.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_js_call.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_js_compiler.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_js_compiler.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_js_executor.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_js_ops.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_js_var.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_json.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_lexer.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_lexer.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_numconv.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_numconv.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_regexp.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_regexp_compiler.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_regexp_executor.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_replacements.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_replacements.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_selftest.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_selftest.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_strings.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_strings.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_tval.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_tval.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_unicode.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_unicode_support.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_unicode_tables.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_util.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_util_bitdecoder.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_util_bitencoder.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_util_bufwriter.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_util_hashbytes.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_util_hashprime.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_util_misc.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_util_tinyrandom.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src-separate/duktape.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src/duk_config.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src/duktape.c create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src/duktape.h create mode 100644 src/civetweb/src/third_party/duktape-1.5.2/src/metadata.json create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/AUTHORS.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/LICENSE.txt create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/Makefile.cmdline create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/Makefile.codepage create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/Makefile.coffee create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/Makefile.dukdebug create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/Makefile.eval create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/Makefile.eventloop create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/Makefile.hello create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/Makefile.jxpretty create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/Makefile.sandbox create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/Makefile.sharedlibrary create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/config/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/config/duk_config.h-modular-dll create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/config/duk_config.h-modular-static create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/config/genconfig.py create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/config/genconfig_metadata.tar.gz create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/debugger/Makefile create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/debugger/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/debugger/duk_classnames.yaml create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/debugger/duk_debug.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/debugger/duk_debug_meta.json create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/debugger/duk_debug_proxy.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/debugger/duk_debugcommands.yaml create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/debugger/duk_debugerrors.yaml create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/debugger/duk_opcodes.yaml create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/debugger/merge_debug_meta.py create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/debugger/package.json create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/debugger/static/index.html create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/debugger/static/style.css create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/debugger/static/webui.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/duk_build_meta.json create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/alloc-hybrid/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/alloc-hybrid/duk_alloc_hybrid.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/alloc-hybrid/duk_alloc_hybrid.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/alloc-logging/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/alloc-logging/duk_alloc_logging.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/alloc-logging/duk_alloc_logging.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/alloc-logging/log2gnuplot.py create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/alloc-torture/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/alloc-torture/duk_alloc_torture.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/alloc-torture/duk_alloc_torture.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/cmdline/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/cmdline/duk_cmdline.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/cmdline/duk_cmdline_ajduk.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/codepage-conv/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/codepage-conv/duk_codepage_conv.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/codepage-conv/duk_codepage_conv.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/codepage-conv/test.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/coffee/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/coffee/globals.coffee create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/coffee/hello.coffee create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/coffee/mandel.coffee create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/cpp-exceptions/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/cpp-exceptions/cpp_exceptions.cpp create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/debug-trans-dvalue/Makefile create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/debug-trans-dvalue/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/debug-trans-dvalue/duk_trans_dvalue.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/debug-trans-dvalue/duk_trans_dvalue.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/debug-trans-dvalue/test.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/debug-trans-socket/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/debug-trans-socket/duk_trans_socket.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/debug-trans-socket/duk_trans_socket_unix.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/debug-trans-socket/duk_trans_socket_windows.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/dummy-date-provider/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/dummy-date-provider/dummy_date_provider.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/eval/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/eval/eval.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/basic-test.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/c_eventloop.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/c_eventloop.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/client-socket-test.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/curses-timers.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/ecma_eventloop.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/fileio.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/main.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/ncurses.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/poll.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/server-socket-test.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/socket.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/guide/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/guide/fib.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/guide/prime.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/guide/primecheck.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/guide/process.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/guide/processlines.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/guide/uppercase.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/hello/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/hello/hello.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/jxpretty/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/jxpretty/jxpretty.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/sandbox/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/examples/sandbox/sandbox.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/extras/README.rst create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/license.spdx create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/licenses/commonjs.txt create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/licenses/lua.txt create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/licenses/murmurhash2.txt create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/mandel.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/polyfills/console-minimal.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/polyfills/duktape-error-setter-nonwritable.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/polyfills/duktape-error-setter-writable.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/polyfills/duktape-isfastint.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/polyfills/object-assign.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/polyfills/object-prototype-definegetter.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/polyfills/object-prototype-definesetter.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/polyfills/performance-now.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-noline/duk_config.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-noline/duktape.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-noline/duktape.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-noline/metadata.json create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_alloc_default.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_api_buffer.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_api_bytecode.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_api_call.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_api_codec.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_api_compile.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_api_debug.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_api_heap.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_api_internal.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_api_logging.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_api_memory.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_api_object.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_api_stack.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_api_string.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_api_var.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_array.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_boolean.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_buffer.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_date.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_date_unix.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_date_windows.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_duktape.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_error.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_function.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_global.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_json.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_logger.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_math.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_number.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_object.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_pointer.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_protos.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_proxy.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_regexp.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_string.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_thread.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_bi_thrower.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_builtins.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_builtins.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_config.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_debug.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_debug_fixedbuffer.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_debug_heap.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_debug_macros.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_debug_vsnprintf.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_debugger.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_debugger.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_error.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_error_augment.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_error_longjmp.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_error_macros.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_error_misc.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_error_throw.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_exception.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_forwdecl.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hbuffer.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hbuffer_alloc.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hbuffer_ops.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hbufferobject.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hbufferobject_misc.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hcompiledfunction.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_heap.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_heap_alloc.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_heap_hashstring.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_heap_markandsweep.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_heap_memory.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_heap_misc.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_heap_refcount.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_heap_stringcache.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_heap_stringtable.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_heaphdr.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hnativefunction.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hobject.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hobject_alloc.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hobject_class.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hobject_enum.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hobject_finalizer.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hobject_misc.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hobject_pc2line.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hobject_props.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hstring.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hstring_misc.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hthread.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hthread_alloc.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hthread_builtins.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hthread_misc.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_hthread_stacks.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_initjs_min.js create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_internal.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_jmpbuf.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_js.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_js_bytecode.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_js_call.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_js_compiler.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_js_compiler.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_js_executor.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_js_ops.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_js_var.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_json.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_lexer.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_lexer.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_numconv.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_numconv.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_regexp.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_regexp_compiler.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_regexp_executor.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_replacements.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_replacements.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_selftest.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_selftest.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_strings.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_strings.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_tval.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_tval.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_unicode.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_unicode_support.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_unicode_tables.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_util.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_util_bitdecoder.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_util_bitencoder.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_util_bufwriter.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_util_hashbytes.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_util_hashprime.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_util_misc.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_util_tinyrandom.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src-separate/duktape.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src/duk_config.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src/duktape.c create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src/duktape.h create mode 100644 src/civetweb/src/third_party/duktape-1.8.0/src/metadata.json create mode 100644 src/civetweb/src/third_party/lfs.c create mode 100644 src/civetweb/src/third_party/lfs.h create mode 100644 src/civetweb/src/third_party/lsqlite3.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/COPYRIGHT create mode 100644 src/civetweb/src/third_party/lua-5.1.5/HISTORY create mode 100644 src/civetweb/src/third_party/lua-5.1.5/INSTALL create mode 100644 src/civetweb/src/third_party/lua-5.1.5/Makefile create mode 100644 src/civetweb/src/third_party/lua-5.1.5/README create mode 100644 src/civetweb/src/third_party/lua-5.1.5/doc/contents.html create mode 100644 src/civetweb/src/third_party/lua-5.1.5/doc/cover.png create mode 100644 src/civetweb/src/third_party/lua-5.1.5/doc/logo.gif create mode 100644 src/civetweb/src/third_party/lua-5.1.5/doc/lua.1 create mode 100644 src/civetweb/src/third_party/lua-5.1.5/doc/lua.css create mode 100644 src/civetweb/src/third_party/lua-5.1.5/doc/lua.html create mode 100644 src/civetweb/src/third_party/lua-5.1.5/doc/luac.1 create mode 100644 src/civetweb/src/third_party/lua-5.1.5/doc/luac.html create mode 100644 src/civetweb/src/third_party/lua-5.1.5/doc/manual.css create mode 100644 src/civetweb/src/third_party/lua-5.1.5/doc/manual.html create mode 100644 src/civetweb/src/third_party/lua-5.1.5/doc/readme.html create mode 100644 src/civetweb/src/third_party/lua-5.1.5/etc/Makefile create mode 100644 src/civetweb/src/third_party/lua-5.1.5/etc/README create mode 100644 src/civetweb/src/third_party/lua-5.1.5/etc/all.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/etc/lua.hpp create mode 100644 src/civetweb/src/third_party/lua-5.1.5/etc/lua.ico create mode 100644 src/civetweb/src/third_party/lua-5.1.5/etc/lua.pc create mode 100644 src/civetweb/src/third_party/lua-5.1.5/etc/luavs.bat create mode 100644 src/civetweb/src/third_party/lua-5.1.5/etc/min.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/etc/noparser.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/etc/strict.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/Makefile create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lapi.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lapi.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lauxlib.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lauxlib.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lbaselib.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lcode.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lcode.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/ldblib.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/ldebug.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/ldebug.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/ldo.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/ldo.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/ldump.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lfunc.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lfunc.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lgc.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lgc.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/linit.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/liolib.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/llex.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/llex.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/llimits.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lmathlib.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lmem.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lmem.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/loadlib.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lobject.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lobject.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lopcodes.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lopcodes.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/loslib.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lparser.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lparser.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lstate.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lstate.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lstring.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lstring.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lstrlib.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/ltable.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/ltable.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/ltablib.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/ltm.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/ltm.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lua.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lua.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/luac.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/luaconf.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lualib.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lundump.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lundump.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lvm.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lvm.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lzio.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/lzio.h create mode 100644 src/civetweb/src/third_party/lua-5.1.5/src/print.c create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/README create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/bisect.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/cf.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/echo.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/env.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/factorial.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/fib.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/fibfor.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/globals.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/hello.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/life.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/luac.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/printf.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/readonly.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/sieve.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/sort.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/table.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/trace-calls.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/trace-globals.lua create mode 100644 src/civetweb/src/third_party/lua-5.1.5/test/xd.lua create mode 100644 src/civetweb/src/third_party/lua-5.2.4/Makefile create mode 100644 src/civetweb/src/third_party/lua-5.2.4/README create mode 100644 src/civetweb/src/third_party/lua-5.2.4/doc/contents.html create mode 100644 src/civetweb/src/third_party/lua-5.2.4/doc/logo.gif create mode 100644 src/civetweb/src/third_party/lua-5.2.4/doc/lua.1 create mode 100644 src/civetweb/src/third_party/lua-5.2.4/doc/lua.css create mode 100644 src/civetweb/src/third_party/lua-5.2.4/doc/luac.1 create mode 100644 src/civetweb/src/third_party/lua-5.2.4/doc/manual.css create mode 100644 src/civetweb/src/third_party/lua-5.2.4/doc/manual.html create mode 100644 src/civetweb/src/third_party/lua-5.2.4/doc/osi-certified-72x60.png create mode 100644 src/civetweb/src/third_party/lua-5.2.4/doc/readme.html create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/Makefile create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lapi.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lapi.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lauxlib.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lauxlib.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lbaselib.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lbitlib.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lcode.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lcode.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lcorolib.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lctype.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lctype.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/ldblib.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/ldebug.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/ldebug.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/ldo.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/ldo.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/ldump.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lfunc.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lfunc.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lgc.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lgc.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/linit.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/liolib.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/llex.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/llex.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/llimits.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lmathlib.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lmem.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lmem.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/loadlib.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lobject.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lobject.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lopcodes.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lopcodes.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/loslib.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lparser.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lparser.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lstate.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lstate.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lstring.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lstring.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lstrlib.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/ltable.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/ltable.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/ltablib.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/ltm.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/ltm.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lua.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lua.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lua.hpp create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/luac.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/luaconf.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lualib.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lundump.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lundump.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lvm.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lvm.h create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lzio.c create mode 100644 src/civetweb/src/third_party/lua-5.2.4/src/lzio.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/Makefile create mode 100644 src/civetweb/src/third_party/lua-5.3.3/README create mode 100644 src/civetweb/src/third_party/lua-5.3.3/doc/contents.html create mode 100644 src/civetweb/src/third_party/lua-5.3.3/doc/index.css create mode 100644 src/civetweb/src/third_party/lua-5.3.3/doc/logo.gif create mode 100644 src/civetweb/src/third_party/lua-5.3.3/doc/lua.1 create mode 100644 src/civetweb/src/third_party/lua-5.3.3/doc/lua.css create mode 100644 src/civetweb/src/third_party/lua-5.3.3/doc/luac.1 create mode 100644 src/civetweb/src/third_party/lua-5.3.3/doc/manual.css create mode 100644 src/civetweb/src/third_party/lua-5.3.3/doc/manual.html create mode 100644 src/civetweb/src/third_party/lua-5.3.3/doc/osi-certified-72x60.png create mode 100644 src/civetweb/src/third_party/lua-5.3.3/doc/readme.html create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/Makefile create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lapi.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lapi.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lauxlib.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lauxlib.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lbaselib.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lbitlib.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lcode.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lcode.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lcorolib.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lctype.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lctype.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/ldblib.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/ldebug.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/ldebug.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/ldo.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/ldo.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/ldump.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lfunc.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lfunc.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lgc.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lgc.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/linit.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/liolib.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/llex.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/llex.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/llimits.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lmathlib.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lmem.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lmem.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/loadlib.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lobject.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lobject.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lopcodes.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lopcodes.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/loslib.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lparser.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lparser.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lprefix.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lstate.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lstate.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lstring.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lstring.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lstrlib.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/ltable.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/ltable.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/ltablib.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/ltm.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/ltm.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lua.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lua.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lua.hpp create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/luac.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/luaconf.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lualib.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lundump.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lundump.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lutf8lib.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lvm.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lvm.h create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lzio.c create mode 100644 src/civetweb/src/third_party/lua-5.3.3/src/lzio.h create mode 100644 src/civetweb/src/third_party/sqlite3.c create mode 100644 src/civetweb/src/third_party/sqlite3.h create mode 100644 src/civetweb/src/timer.inl create mode 100644 src/civetweb/test/.leading.dot.txt create mode 100644 src/civetweb/test/1000images.lua create mode 100644 src/civetweb/test/100images.htm create mode 100644 src/civetweb/test/CMakeLists.txt create mode 100644 src/civetweb/test/HugeText.lua create mode 100644 src/civetweb/test/MakefileTest.mk create mode 100644 src/civetweb/test/MethodTest.xhtml create mode 100644 src/civetweb/test/README.md create mode 100644 src/civetweb/test/ajax/echo.cgi create mode 100644 src/civetweb/test/ajax/echo.cgi.old create mode 100644 src/civetweb/test/ajax/echo.lp create mode 100644 src/civetweb/test/ajax/echo.lua create mode 100644 src/civetweb/test/ajax/jquery.js create mode 100644 src/civetweb/test/ajax/test.html create mode 100755 src/civetweb/test/all_build_flags.pl create mode 100755 src/civetweb/test/bad.cgi create mode 100755 src/civetweb/test/bad2.cgi create mode 100644 src/civetweb/test/bad_page.lp create mode 100644 src/civetweb/test/bad_script.lua create mode 100644 src/civetweb/test/cgi_test.c create mode 100644 src/civetweb/test/cgi_test.html create mode 100644 src/civetweb/test/civetweb_check.h create mode 100644 src/civetweb/test/cors.html create mode 100644 src/civetweb/test/cors.reply.html create mode 100644 src/civetweb/test/cors.reply.lua create mode 100644 src/civetweb/test/cors.reply.shtml create mode 100644 src/civetweb/test/delayed.cgi create mode 100755 src/civetweb/test/dir with spaces/hello.cgi create mode 100644 src/civetweb/test/echo.lua create mode 100644 src/civetweb/test/embed.c create mode 100755 src/civetweb/test/env.cgi create mode 100644 src/civetweb/test/error.lua create mode 100644 src/civetweb/test/error404.htm create mode 100644 src/civetweb/test/exit.lua create mode 100755 src/civetweb/test/exploit.pl create mode 100644 src/civetweb/test/filehandler.lua create mode 100644 src/civetweb/test/form.html create mode 100644 src/civetweb/test/handle_form.lua create mode 100755 src/civetweb/test/hello.cgi create mode 100644 src/civetweb/test/hello.shtml create mode 100644 src/civetweb/test/hello.txt create mode 100644 src/civetweb/test/hello_gz.txt.gz create mode 100644 src/civetweb/test/hello_gz_unzipped.txt create mode 100644 src/civetweb/test/html_esc.lua create mode 100644 src/civetweb/test/imagetest/00.png create mode 100644 src/civetweb/test/imagetest/01.png create mode 100644 src/civetweb/test/imagetest/02.png create mode 100644 src/civetweb/test/imagetest/03.png create mode 100644 src/civetweb/test/imagetest/04.png create mode 100644 src/civetweb/test/imagetest/05.png create mode 100644 src/civetweb/test/imagetest/06.png create mode 100644 src/civetweb/test/imagetest/07.png create mode 100644 src/civetweb/test/imagetest/08.png create mode 100644 src/civetweb/test/imagetest/09.png create mode 100644 src/civetweb/test/imagetest/10.png create mode 100644 src/civetweb/test/imagetest/11.png create mode 100644 src/civetweb/test/imagetest/12.png create mode 100644 src/civetweb/test/imagetest/13.png create mode 100644 src/civetweb/test/imagetest/14.png create mode 100644 src/civetweb/test/imagetest/15.png create mode 100644 src/civetweb/test/imagetest/16.png create mode 100644 src/civetweb/test/imagetest/17.png create mode 100644 src/civetweb/test/imagetest/18.png create mode 100644 src/civetweb/test/imagetest/19.png create mode 100644 src/civetweb/test/imagetest/20.png create mode 100644 src/civetweb/test/imagetest/21.png create mode 100644 src/civetweb/test/imagetest/22.png create mode 100644 src/civetweb/test/imagetest/23.png create mode 100644 src/civetweb/test/imagetest/24.png create mode 100644 src/civetweb/test/imagetest/25.png create mode 100644 src/civetweb/test/imagetest/26.png create mode 100644 src/civetweb/test/imagetest/27.png create mode 100644 src/civetweb/test/imagetest/28.png create mode 100644 src/civetweb/test/imagetest/29.png create mode 100644 src/civetweb/test/imagetest/30.png create mode 100644 src/civetweb/test/imagetest/31.png create mode 100644 src/civetweb/test/imagetest/32.png create mode 100644 src/civetweb/test/imagetest/33.png create mode 100644 src/civetweb/test/imagetest/34.png create mode 100644 src/civetweb/test/imagetest/35.png create mode 100644 src/civetweb/test/imagetest/36.png create mode 100644 src/civetweb/test/imagetest/37.png create mode 100644 src/civetweb/test/imagetest/38.png create mode 100644 src/civetweb/test/imagetest/39.png create mode 100644 src/civetweb/test/imagetest/40.png create mode 100644 src/civetweb/test/imagetest/41.png create mode 100644 src/civetweb/test/imagetest/42.png create mode 100644 src/civetweb/test/imagetest/43.png create mode 100644 src/civetweb/test/imagetest/44.png create mode 100644 src/civetweb/test/imagetest/45.png create mode 100644 src/civetweb/test/imagetest/46.png create mode 100644 src/civetweb/test/imagetest/47.png create mode 100644 src/civetweb/test/imagetest/48.png create mode 100644 src/civetweb/test/imagetest/49.png create mode 100644 src/civetweb/test/imagetest/50.png create mode 100644 src/civetweb/test/imagetest/51.png create mode 100644 src/civetweb/test/imagetest/52.png create mode 100644 src/civetweb/test/imagetest/53.png create mode 100644 src/civetweb/test/imagetest/54.png create mode 100644 src/civetweb/test/imagetest/55.png create mode 100644 src/civetweb/test/imagetest/56.png create mode 100644 src/civetweb/test/imagetest/57.png create mode 100644 src/civetweb/test/imagetest/58.png create mode 100644 src/civetweb/test/imagetest/59.png create mode 100644 src/civetweb/test/imagetest/60.png create mode 100644 src/civetweb/test/imagetest/61.png create mode 100644 src/civetweb/test/imagetest/62.png create mode 100644 src/civetweb/test/imagetest/63.png create mode 100644 src/civetweb/test/imagetest/64.png create mode 100644 src/civetweb/test/imagetest/65.png create mode 100644 src/civetweb/test/imagetest/66.png create mode 100644 src/civetweb/test/imagetest/67.png create mode 100644 src/civetweb/test/imagetest/68.png create mode 100644 src/civetweb/test/imagetest/69.png create mode 100644 src/civetweb/test/imagetest/70.png create mode 100644 src/civetweb/test/imagetest/71.png create mode 100644 src/civetweb/test/imagetest/72.png create mode 100644 src/civetweb/test/imagetest/73.png create mode 100644 src/civetweb/test/imagetest/74.png create mode 100644 src/civetweb/test/imagetest/75.png create mode 100644 src/civetweb/test/imagetest/76.png create mode 100644 src/civetweb/test/imagetest/77.png create mode 100644 src/civetweb/test/imagetest/78.png create mode 100644 src/civetweb/test/imagetest/79.png create mode 100644 src/civetweb/test/imagetest/80.png create mode 100644 src/civetweb/test/imagetest/81.png create mode 100644 src/civetweb/test/imagetest/82.png create mode 100644 src/civetweb/test/imagetest/83.png create mode 100644 src/civetweb/test/imagetest/84.png create mode 100644 src/civetweb/test/imagetest/85.png create mode 100644 src/civetweb/test/imagetest/86.png create mode 100644 src/civetweb/test/imagetest/87.png create mode 100644 src/civetweb/test/imagetest/88.png create mode 100644 src/civetweb/test/imagetest/89.png create mode 100644 src/civetweb/test/imagetest/90.png create mode 100644 src/civetweb/test/imagetest/91.png create mode 100644 src/civetweb/test/imagetest/92.png create mode 100644 src/civetweb/test/imagetest/93.png create mode 100644 src/civetweb/test/imagetest/94.png create mode 100644 src/civetweb/test/imagetest/95.png create mode 100644 src/civetweb/test/imagetest/96.png create mode 100644 src/civetweb/test/imagetest/97.png create mode 100644 src/civetweb/test/imagetest/98.png create mode 100644 src/civetweb/test/imagetest/99.png create mode 100755 src/civetweb/test/linux.cgi create mode 100644 src/civetweb/test/linux_fail.cgi create mode 100644 src/civetweb/test/linux_fail_silent.cgi create mode 100644 src/civetweb/test/lua_preload_file.lua create mode 100644 src/civetweb/test/main.c create mode 100644 src/civetweb/test/page.lp create mode 100644 src/civetweb/test/page.lua create mode 100644 src/civetweb/test/page.ssjs create mode 100644 src/civetweb/test/page2.lp create mode 100644 src/civetweb/test/page2.lua create mode 100644 src/civetweb/test/page2.ssjs create mode 100644 src/civetweb/test/page3.lp create mode 100644 src/civetweb/test/page3.lua create mode 100644 src/civetweb/test/page3.ssjs create mode 100644 src/civetweb/test/page3a.lp create mode 100644 src/civetweb/test/page3r.lp create mode 100644 src/civetweb/test/page3v.lp create mode 100644 src/civetweb/test/page4.lua create mode 100644 src/civetweb/test/page5.lua create mode 100644 src/civetweb/test/page6.lua create mode 100644 src/civetweb/test/page_keep_alive.lua create mode 100644 src/civetweb/test/page_keep_alive_chunked.lua create mode 100644 src/civetweb/test/page_status.lua create mode 100644 src/civetweb/test/passfile create mode 100644 src/civetweb/test/prime.ssjs create mode 100644 src/civetweb/test/private.c create mode 100644 src/civetweb/test/private.h create mode 100644 src/civetweb/test/private_exe.c create mode 100644 src/civetweb/test/private_exe.h create mode 100644 src/civetweb/test/protected/.htpasswd create mode 100644 src/civetweb/test/protected/content.txt create mode 100644 src/civetweb/test/public_func.c create mode 100644 src/civetweb/test/public_func.h create mode 100644 src/civetweb/test/public_server.c create mode 100644 src/civetweb/test/public_server.h create mode 100644 src/civetweb/test/require_test.lua create mode 100644 src/civetweb/test/resource_script_demo.lua create mode 100644 src/civetweb/test/shared.c create mode 100644 src/civetweb/test/shared.h create mode 100644 src/civetweb/test/ssi_test.shtml create mode 100644 src/civetweb/test/syntax_error.ssjs create mode 100644 src/civetweb/test/test.ico create mode 100755 src/civetweb/test/test.pl create mode 100644 src/civetweb/test/testclient.c create mode 100755 src/civetweb/test/timeout.cgi create mode 100644 src/civetweb/test/timertest.c create mode 100644 src/civetweb/test/timertest.h create mode 100644 src/civetweb/test/websocket.lua create mode 100644 src/civetweb/test/websocket.xhtml create mode 100644 src/civetweb/test/windows.cgi create mode 100644 src/civetweb/test/windows.cgi.cmd create mode 100644 src/civetweb/test/windows_fail.cgi create mode 100644 src/civetweb/test/windows_fail.cgi.cmd create mode 100644 src/civetweb/test/windows_fail_silent.cgi create mode 100644 src/civetweb/test/windows_fail_silent.cgi.cmd create mode 100644 src/civetweb/test/ws_status.lua create mode 100644 src/civetweb/test/x.php (limited to 'src/civetweb') diff --git a/src/civetweb/.clang-format b/src/civetweb/.clang-format new file mode 100644 index 000000000..37a80d92c --- /dev/null +++ b/src/civetweb/.clang-format @@ -0,0 +1,32 @@ +# http://clang.llvm.org/docs/ClangFormatStyleOptions.html + +BasedOnStyle: LLVM + +IndentWidth: 4 +TabWidth: 4 +UseTab: ForIndentation +ColumnLimit: 80 + +Language: Cpp + +AlignAfterOpenBracket: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: true +BinPackArguments: false +BinPackParameters: false +BreakBeforeBinaryOperators: NonAssignment +BreakBeforeBraces: Linux +DerivePointerAlignment: false +MaxEmptyLinesToKeep: 2 +PenaltyBreakBeforeFirstCallParameter: 100 + +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesInSquareBrackets: false + +DisableFormat: false diff --git a/src/civetweb/.gitattributes b/src/civetweb/.gitattributes new file mode 100644 index 000000000..5c528676b --- /dev/null +++ b/src/civetweb/.gitattributes @@ -0,0 +1,33 @@ +# Auto detect text files and perform LF normalization +* -text + +# Custom for Visual Studio +*.cs diff=csharp +*.sln merge=union +*.csproj merge=union +*.vbproj merge=union +*.fsproj merge=union +*.dbproj merge=union + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain + +# Preserver Windows specfic lines endings +*.cmd text eol=crlf + + +# Settings for github syntax highlighting +# see https://github.com/github/linguist/blob/master/README.md +docs/* linguist-documentation +*.inl linguist-language=C +*.h linguist-language=C +include/CivetServer.h linguist-language=C++ diff --git a/src/civetweb/.gitignore b/src/civetweb/.gitignore new file mode 100644 index 000000000..f10aa01e7 --- /dev/null +++ b/src/civetweb/.gitignore @@ -0,0 +1,268 @@ + +civetweb +civetweb_test +libcivetweb.a +libcivetweb.so +*-cache +out +*.dmg +*.msi +*.exe +*.zip +[oO]utput +[tT]esting + +*.o + +################# +## CMake +################# +/CMakeCache.txt +/CMakeFiles +/mingw-builds + +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Text-mode IDE tools +cscope.out +tags + +# Build results + +[Dd]ebug/ +[Dd]ebug CONSOLE/ +[Rr]elease/ +x64/ +[Bb]in/ +[Oo]bj/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +#packages/ + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml +*.pfx +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +############# +## Windows detritus +############# + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac crap +.DS_Store + + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist/ +eggs/ +parts/ +var/ +sdist/ +develop-eggs/ +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg + + +########################## +## Files created by tests +########################## +requests.db + +########################## +## Files created by ctags +########################## +?tags +?tags? + +########################## +## Files created by autotools +########################## +*.lo +.libs + +########################## +## Travis Build Dir +########################## +ci/lua + + diff --git a/src/civetweb/.travis.yml b/src/civetweb/.travis.yml new file mode 100644 index 000000000..2dd926cff --- /dev/null +++ b/src/civetweb/.travis.yml @@ -0,0 +1,694 @@ +############################################################################## +# Travis version specific build environment specification +############################################################################## + +# The "precise" build environment on Travis is in the process of being decommissioned +# see https://blog.travis-ci.com/2017-08-31-trusty-as-default-status +# The "precise=true"+"sudo=required" environment seems to lack IPv6 support. +# According to some tests, all "sudo=required" environments do not support IPv6, see +# https://github.com/travis-ci/travis-ci/issues/8361#issuecomment-328263113 +# The container environments for "sudo=false" support IPv6 localhost [::1] +# connections for server/client test. Thus, all tests with ENABLE_IPV6=YES +# + + +############################################################################## +# Project specific settings +############################################################################## + +language: c + +cache: + directories: + - $HOME/third-party + +osx_image: xcode8 + +addons: + apt: + packages: + - cmake + - openssl + - libssl-dev + sources: + - kubuntu-backports + + +before_install: + - if [ "${TRAVIS_OS_NAME}" == "linux" ]; then + mkdir $HOME/usr; + export PATH="$HOME/usr/bin:$PATH"; + wget https://cmake.org/files/v3.7/cmake-3.7.2-Linux-x86_64.sh --no-check-certificate; + chmod +x cmake-3.7.2-Linux-x86_64.sh; + ./cmake-3.7.2-Linux-x86_64.sh --prefix=$HOME/usr --exclude-subdir --skip-license; + fi + - cmake --version + + +install: + - if [ "${BUILD_TYPE}" == "Coverage" -a "${TRAVIS_OS_NAME}" == "linux" ]; then + PATH=~/.local/bin:${PATH}; + pip install --user --upgrade pip; + pip install --user cpp-coveralls; + fi + +before_script: + # Check some settings of the build server (operating system, IPv6 availability, directory) + - uname -a + - ifconfig + - pwd + - ls -la + - if [ "${TRAVIS_OS_NAME}" == "linux" ]; then + apt-cache search gcc | grep "GNU C compiler"; + apt-cache search clang | grep compiler; + fi + - if [[ "${BUILD_TYPE}" == "OSX_OPENSSL_1_1" ]]; then brew install openssl@1.1 ;fi + # Generate the build scripts with CMake + - mkdir output + - gcc test/cgi_test.c -o output/cgi_test.cgi + - cd output + - cmake --version + - cmake + -G "Unix Makefiles" + -DCMAKE_BUILD_TYPE=${BUILD_TYPE} + -DBUILD_SHARED_LIBS=${BUILD_SHARED} + "-DCIVETWEB_THIRD_PARTY_DIR=${HOME}/third-party" + -DCIVETWEB_ENABLE_THIRD_PARTY_OUTPUT=YES + -DCIVETWEB_ENABLE_SSL=${ENABLE_SSL} + -DCIVETWEB_DISABLE_CGI=${NO_CGI} + -DCIVETWEB_SERVE_NO_FILES=${NO_FILES} + -DCIVETWEB_ENABLE_SSL_DYNAMIC_LOADING=${ENABLE_SSL_DYNAMIC_LOADING} + -DCIVETWEB_SSL_OPENSSL_API_1_1=${OPENSSL_1_1} + -DCIVETWEB_ENABLE_WEBSOCKETS=${ENABLE_WEBSOCKETS} + -DCIVETWEB_ENABLE_CXX=${ENABLE_CXX} + -DCIVETWEB_ENABLE_IPV6=${ENABLE_IPV6} + -DCIVETWEB_ENABLE_SERVER_STATS=${ENABLE_SERVER_STATS} + -DCIVETWEB_ENABLE_LUA=${ENABLE_LUA} + -DCIVETWEB_ENABLE_LUA_SHARED=${ENABLE_LUA_SHARED} + -DCIVETWEB_ENABLE_DUKTAPE=${ENABLE_DUKTAPE} + -DCIVETWEB_DISABLE_CACHING=${NO_CACHING} + -DCIVETWEB_C_STANDARD=${C_STANDARD} + -DCIVETWEB_CXX_STANDARD=${CXX_STANDARD} + -DCIVETWEB_ALLOW_WARNINGS=${ALLOW_WARNINGS} + ${ADDITIONAL_CMAKE_ARGS} + .. + - ls -la + +script: + - if [ "${MACOSX_PACKAGE}" == "1" ]; then + cd "${TRAVIS_BUILD_DIR}"; + make -f Makefile.osx package; + else + CTEST_OUTPUT_ON_FAILURE=1 make all test; + fi + +# Coveralls options: https://github.com/eddyxu/cpp-coveralls/blob/master/README.md +after_success: + - if [ "${BUILD_TYPE}" == "Coverage" -a "${TRAVIS_OS_NAME}" == "linux" ]; then + coveralls --include src --exclude src/main.c --exclude src/third_party --include include --gcov-options '\-lp' --root .. --build-root .; + bash <(curl -s https://codecov.io/bash); + fi + + +############################################################################## +# build matrix (auto generated) +############################################################################## + + +matrix: + fast_finish: false + include: + + - dist: trusty + sudo: false + os: linux + compiler: clang + addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-precise-3.8 + packages: + - clang-3.8 + env: + idx=1 + N=Clang3.8-Linux-Minimal-Debug + MATRIX_EVAL="CC=clang-3.8 && CXX=clang++-3.8" + BUILD_TYPE=Debug + ENABLE_SSL_DYNAMIC_LOADING=YES + OPENSSL_1_1=NO + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=0 + BUILD_SHARED=NO + NO_FILES=YES + ENABLE_SSL=NO + NO_CGI=YES + ENABLE_IPV6=NO + ENABLE_WEBSOCKETS=NO + ENABLE_SERVER_STATS=NO + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=NO + ALLOW_WARNINGS=YES + + - dist: trusty + sudo: false + os: linux + compiler: clang + addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-precise-3.8 + packages: + - clang-3.8 + env: + idx=2 + N=Clang3.8-Linux-Default-Release + MATRIX_EVAL="CC=clang-3.8 && CXX=clang++-3.8" + BUILD_TYPE=Release + ENABLE_SSL_DYNAMIC_LOADING=YES + OPENSSL_1_1=NO + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=7 + BUILD_SHARED=NO + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=NO + ENABLE_WEBSOCKETS=NO + ENABLE_SERVER_STATS=NO + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=NO + NO_CACHING=NO + ALLOW_WARNINGS=YES + + - dist: trusty + sudo: false + os: linux + compiler: clang + addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-precise-3.8 + packages: + - clang-3.8 + env: + idx=3 + N=Clang3.8-Linux-Default-Release + MATRIX_EVAL="CC=clang-3.8 && CXX=clang++-3.8" + BUILD_TYPE=Release + ENABLE_SSL_DYNAMIC_LOADING=YES + OPENSSL_1_1=NO + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=7 + BUILD_SHARED=NO + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=NO + ENABLE_WEBSOCKETS=NO + ENABLE_SERVER_STATS=NO + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=NO + ALLOW_WARNINGS=YES + + - dist: trusty + sudo: false + os: linux + compiler: clang + addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-precise-3.8 + packages: + - clang-3.8 + env: + idx=4 + N=Clang3.8-Linux-Complete-NoLua-Release + MATRIX_EVAL="CC=clang-3.8 && CXX=clang++-3.8" + BUILD_TYPE=Release + ENABLE_SSL_DYNAMIC_LOADING=YES + OPENSSL_1_1=NO + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=31 + BUILD_SHARED=NO + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=YES + ENABLE_WEBSOCKETS=YES + ENABLE_SERVER_STATS=YES + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=YES + ALLOW_WARNINGS=YES + + + - dist: trusty + sudo: false + os: linux + compiler: gcc + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-5 + env: + idx=5 + N=GCC5-Linux-Complete-NoLua-Release + MATRIX_EVAL="CC=gcc-5 && CXX=g++-5" + BUILD_TYPE=Release + ENABLE_SSL_DYNAMIC_LOADING=YES + OPENSSL_1_1=NO + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=31 + BUILD_SHARED=NO + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=YES + ENABLE_WEBSOCKETS=YES + ENABLE_SERVER_STATS=YES + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=YES + ALLOW_WARNINGS=YES + + - os: linux + compiler: gcc + env: + idx=6 + N=GCCAnyVersion-Linux-Coverage + BUILD_TYPE=Coverage + ENABLE_SSL_DYNAMIC_LOADING=YES + OPENSSL_1_1=NO + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=31 + BUILD_SHARED=NO + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=YES + ENABLE_WEBSOCKETS=YES + ENABLE_SERVER_STATS=YES + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=NO + ALLOW_WARNINGS=YES + + - os: osx + compiler: clang + env: + idx=7 + N=Clang-OSX-Complete-NoLua-Release + BUILD_TYPE=Release + ENABLE_SSL_DYNAMIC_LOADING=YES + OPENSSL_1_1=NO + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=31 + BUILD_SHARED=NO + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=YES + ENABLE_WEBSOCKETS=YES + ENABLE_SERVER_STATS=YES + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=YES + ALLOW_WARNINGS=YES + + - + os: osx + compiler: clang + env: + idx=8 + N=Clang-OSX-Complete-NoLua-Release-OpenSSL_1_1_NoDynLoad + BUILD_TYPE=OSX_OPENSSL_1_1 + ENABLE_SSL_DYNAMIC_LOADING=NO + OPENSSL_1_1=YES + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=31 + BUILD_SHARED=NO + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=YES + ENABLE_WEBSOCKETS=YES + ENABLE_SERVER_STATS=YES + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=YES + ALLOW_WARNINGS=YES + OPENSSL_ROOT_DIR="/usr/local/opt/openssl@1.1" + LDFLAGS="-L${OPENSSL_ROOT_DIR}/lib" + CFLAGS="-I${OPENSSL_ROOT_DIR}/include" + ADDITIONAL_CMAKE_ARGS="-DCMAKE_SHARED_LINKER_FLAGS=${LDFLAGS} -DCMAKE_C_FLAGS=${CFLAGS}" + PATH="${OPENSSL_ROOT_DIR}/bin:$PATH" + DYLD_LIBRARY_PATH="${OPENSSL_ROOT_DIR}/lib:${DYLD_LIBRARY_PATH}" + + - dist: trusty + sudo: false + os: linux + compiler: clang + addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-trusty-5.0 + packages: + - clang-5.0 + env: + idx=9 + N=Clang50-Linux-Default-Shared + BUILD_TYPE=Debug + ENABLE_SSL_DYNAMIC_LOADING=YES + OPENSSL_1_1=NO + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=7 + BUILD_SHARED=YES + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=NO + ENABLE_WEBSOCKETS=NO + ENABLE_SERVER_STATS=NO + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=NO + ALLOW_WARNINGS=YES + MATRIX_EVAL="CC=clang-3.8 && CXX=clang++-3.8" + + - + os: linux + dist: precise + sudo: required + compiler: clang + env: + idx=10 + N=Precise-Clang-Linux-Default + BUILD_TYPE=Debug + ENABLE_SSL_DYNAMIC_LOADING=YES + OPENSSL_1_1=NO + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=7 + BUILD_SHARED=NO + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=NO + ENABLE_WEBSOCKETS=NO + ENABLE_SERVER_STATS=NO + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=NO + ALLOW_WARNINGS=YES + + - + os: osx + compiler: clang + env: + idx=11 + N=OSX-Package + BUILD_TYPE=Release + ENABLE_SSL_DYNAMIC_LOADING=YES + OPENSSL_1_1=NO + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=31 + BUILD_SHARED=NO + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=YES + ENABLE_WEBSOCKETS=YES + ENABLE_SERVER_STATS=NO + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=NO + ALLOW_WARNINGS=YES + MACOSX_PACKAGE=1 + + - dist: trusty + sudo: false + os: linux + compiler: clang + addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-trusty-3.8 + packages: + - clang-3.8 + env: + idx=12 + N=Clang-Linux-32bit-Complete-NoLua-Release + ARCH=x86 + BUILD_TYPE=Release + ENABLE_SSL_DYNAMIC_LOADING=YES + OPENSSL_1_1=NO + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=31 + BUILD_SHARED=NO + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=YES + ENABLE_WEBSOCKETS=YES + ENABLE_SERVER_STATS=YES + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=YES + ALLOW_WARNINGS=YES + MATRIX_EVAL="CC=clang-3.8 && CXX=clang++-3.8" + + +### Test all build types: +# According to CMakeLists, options are: +# None Debug Release RelWithDebInfo MinSizeRel Coverage + + - + os: linux + compiler: clang + env: + idx=13 + N=NoSslDynamicLoading + BUILD_TYPE=Release + ENABLE_SSL_DYNAMIC_LOADING=NO + OPENSSL_1_1=NO + ENABLE_CXX=NO + C_STANDARD=auto + CXX_STANDARD=auto + ENABLE_LUA_SHARED=NO + FEATURES=31 + BUILD_SHARED=NO + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=YES + ENABLE_WEBSOCKETS=YES + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=NO + ALLOW_WARNINGS=YES + MATRIX_EVAL="CC=clang-3.8 && CXX=clang++-3.8" + + - + os: linux + compiler: gcc + env: + idx=14 + N=GCCLinuxDefault_Debug + BUILD_TYPE=Debug + ENABLE_SSL_DYNAMIC_LOADING=YES + OPENSSL_1_1=NO + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=7 + BUILD_SHARED=NO + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=NO + ENABLE_WEBSOCKETS=NO + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=NO + ALLOW_WARNINGS=YES + + - + os: linux + compiler: gcc + env: + idx=15 + N=GCCLinuxDefault_RelWithDebInfo + BUILD_TYPE=RelWithDebInfo + ENABLE_SSL_DYNAMIC_LOADING=YES + OPENSSL_1_1=NO + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=7 + BUILD_SHARED=NO + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=NO + ENABLE_WEBSOCKETS=NO + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=NO + ALLOW_WARNINGS=YES + + - + os: linux + compiler: gcc + env: + idx=16 + N=GCCLinuxDefault_MinSizeRel + BUILD_TYPE=MinSizeRel + ENABLE_SSL_DYNAMIC_LOADING=YES + OPENSSL_1_1=NO + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=7 + BUILD_SHARED=NO + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=NO + ENABLE_WEBSOCKETS=NO + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=NO + ALLOW_WARNINGS=YES + + - + os: linux + compiler: gcc + env: + idx=17 + N=GCCLinuxDefault_None + BUILD_TYPE=None + ENABLE_SSL_DYNAMIC_LOADING=YES + OPENSSL_1_1=NO + ENABLE_CXX=NO + ENABLE_LUA_SHARED=NO + C_STANDARD=auto + CXX_STANDARD=auto + FEATURES=7 + BUILD_SHARED=NO + NO_FILES=NO + ENABLE_SSL=YES + NO_CGI=NO + ENABLE_IPV6=NO + ENABLE_WEBSOCKETS=NO + ENABLE_LUA=NO + ENABLE_DUKTAPE=NO + NO_CACHING=NO + ALLOW_WARNINGS=YES + + +#### Now all define combinations, but only for Linux clang +##### Generated with Lua: +# +# function YN(i,b) +# local bits = {} +# while (i > 0.5) do +# i = math.floor(i) +# bits[#bits+1] = (math.mod(i, 2) == 1) +# i = i/2 +# end +# if (bits[b]) then +# return "YES" +# end +# return "NO" +# end +# function INV(t) +# if t=="YES" then +# return "NO" +# elseif t=="NO" then +# return "YES" +# else +# assert("ERROR in INV!") +# end +# end +# for i=0,511 do +# if (YN(i, 6)=="NO") and (YN(i, 7)=="NO") then +# print(" -") +# print(" os: linux") +# print(" compiler: clang") +# print(" env:") +# print(" N=C" .. tostring(i) .. "_") +# print(" BUILD_TYPE=Release") +# print(" ENABLE_SSL_DYNAMIC_LOADING=YES") +# print(" OPENSSL_1_1=NO") +# print(" ENABLE_CXX=NO") +# print(" C_STANDARD=auto") +# print(" CXX_STANDARD=auto") +# print(" ENABLE_LUA_SHARED=NO") +# print(" FEATURES=" .. tostring(i)) +# print(" BUILD_SHARED=NO") +# print(" NO_FILES=" .. INV(YN(i, 1))) +# print(" ENABLE_SSL=" .. YN(i, 2)) +# print(" NO_CGI=" .. INV(YN(i, 3))) +# print(" ENABLE_IPV6=" .. YN(i, 4)) +# print(" ENABLE_WEBSOCKETS=" .. YN(i, 5)) +# print(" ENABLE_LUA=" .. YN(i, 6)) +# print(" ENABLE_DUKTAPE=" .. YN(i, 7)) +# print(" NO_CACHING=" .. INV(YN(i, 8))) +# print(" ENABLE_SERVER_STATS=" .. YN(i, 9)) +# print("") +# end +# end +# + +# TODO: Regenerate this matrix, once a stable Travis build is re-established + + diff --git a/src/civetweb/CMakeLists.txt b/src/civetweb/CMakeLists.txt new file mode 100644 index 000000000..2c08bd28e --- /dev/null +++ b/src/civetweb/CMakeLists.txt @@ -0,0 +1,502 @@ +# Determines what CMake APIs we can rely on +cmake_minimum_required (VERSION 2.8.11) +if (${CMAKE_VERSION} VERSION_GREATER 3.2.2) + cmake_policy(VERSION 3.2.2) +endif() +if (${CMAKE_VERSION} VERSION_GREATER 3.1 OR + ${CMAKE_VERSION} VERSION_EQUAL 3.1) + cmake_policy(SET CMP0054 NEW) +endif() + +# Do not allow in source builds +set(CMAKE_DISABLE_SOURCE_CHANGES ON) +set(CMAKE_DISABLE_IN_SOURCE_BUILD ON) + +# Make sure we can import out CMake functions +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") + +# Load in the needed CMake modules +include(CheckIncludeFiles) +include(CheckCCompilerFlag) +include(CheckCXXCompilerFlag) +include(AddCCompilerFlag) +include(AddCXXCompilerFlag) +include(DetermineTargetArchitecture) +include(CMakeDependentOption) + +# Set up the project +project (civetweb) +set(CIVETWEB_VERSION "1.10.0" CACHE STRING "The version of the civetweb library") +string(REGEX MATCH "([0-9]+)\\.([0-9]+)\\.([0-9]+)" CIVETWEB_VERSION_MATCH "${CIVETWEB_VERSION}") +if ("${CIVETWEB_VERSION_MATCH}" STREQUAL "") + message(FATAL_ERROR "Must specify a semantic version: major.minor.patch") +endif() +set(CIVETWEB_VERSION_MAJOR "${CMAKE_MATCH_1}") +set(CIVETWEB_VERSION_MINOR "${CMAKE_MATCH_2}") +set(CIVETWEB_VERSION_PATCH "${CMAKE_MATCH_3}") +determine_target_architecture(CIVETWEB_ARCHITECTURE) + +# Detect the platform reliably +if(NOT MACOSX AND ${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + SET(DARWIN YES) +elseif(NOT BSD AND ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") + SET(FREEBSD YES) +elseif(NOT LINUX AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux") + SET(LINUX YES) +endif() + +# C++ wrappers +option(CIVETWEB_ENABLE_THIRD_PARTY_OUTPUT "Shows the output of third party dependency processing" OFF) + +# Thread Stack Size +set(CIVETWEB_THREAD_STACK_SIZE 102400 CACHE STRING + "The stack size in bytes for each thread created") +set_property(CACHE CIVETWEB_THREAD_STACK_SIZE PROPERTY VALUE ${CIVETWEB_THREAD_STACK_SIZE}) +message(STATUS "Thread Stack Size - ${CIVETWEB_THREAD_STACK_SIZE}") + +# Serve no files from the web server +option(CIVETWEB_SERVE_NO_FILES "Configures the server to serve no static files" OFF) +message(STATUS "Serve no static files - ${CIVETWEB_SERVE_NO_FILES}") + +# Serve no files from the web server +option(CIVETWEB_DISABLE_CGI "Disables CGI, so theserver will not execute CGI scripts" OFF) +message(STATUS "Disable CGI support - ${CIVETWEB_DISABLE_CGI}") + +# Disable caching +option(CIVETWEB_DISABLE_CACHING "Disables caching, so that no timegm is used." OFF) +message(STATUS "Disable caching support - ${CIVETWEB_DISABLE_CACHING}") + +# C++ wrappers +option(CIVETWEB_ENABLE_CXX "Enables the C++ wrapper library" OFF) +message(STATUS "C++ wrappers - ${CIVETWEB_ENABLE_CXX}") + +# IP Version 6 +option(CIVETWEB_ENABLE_IPV6 "Enables the IP version 6 support" OFF) +message(STATUS "IP Version 6 - ${CIVETWEB_ENABLE_IPV6}") + +# Websocket support +option(CIVETWEB_ENABLE_WEBSOCKETS "Enable websockets connections" OFF) +message(STATUS "Websockets support - ${CIVETWEB_ENABLE_WEBSOCKETS}") + +# Server statistics support +option(CIVETWEB_ENABLE_SERVER_STATS "Enable server statistics" OFF) +message(STATUS "Server statistics support - ${CIVETWEB_ENABLE_SERVER_STATS}") + +# Memory debugging +option(CIVETWEB_ENABLE_MEMORY_DEBUGGING "Enable the memory debugging features" OFF) +message(STATUS "Memory Debugging - ${CIVETWEB_ENABLE_MEMORY_DEBUGGING}") + +# ASAN in debug mode (-fsanitize=address, etc) +option(CIVETWEB_ENABLE_ASAN "Enable ASAN in debug mode" ON) +message(STATUS "ASAN in debug mode - ${CIVETWEB_ENABLE_ASAN}") + +# ARCH flag +option(CIVETWEB_ARCH "Force 32/64 bit architecture" OFF) +message(STATUS "Force x32 / x64 architecture - ${CIVETWEB_ARCH}") + +# LUA CGI support +option(CIVETWEB_ENABLE_LUA "Enable Lua CGIs" OFF) +message(STATUS "Lua CGI support - ${CIVETWEB_ENABLE_LUA}") + +# Enable installing CivetWeb executables +option(CIVETWEB_INSTALL_EXECUTABLE "Enable installing CivetWeb executable" ON) +mark_as_advanced(FORCE CIVETWEB_INSTALL_EXECUTABLE) # Advanced users can disable +message(STATUS "Executable installation - ${CIVETWEB_INSTALL_EXECUTABLE}") + +# Allow builds to complete with warnings (do not set -Werror) +# CivetWeb Linux support is stable: +# Builds for GCC 4.6 and clang 3.4 are free from warnings. +# However, GCC introduced a couple of new, partially idiotic warnings, +# that can not be disabled using a #pragma directive. +# It seems unreasonable to have all GCC versions warning free, but only +# some selected ones. +option(CIVETWEB_ALLOW_WARNINGS "Do not stop build if there are warnings" ON) +message(STATUS "Build if there are warnings - ${CIVETWEB_ALLOW_WARNINGS}") + +# Link to the shared LUA library +cmake_dependent_option( + CIVETWEB_ENABLE_LUA_SHARED "Link to the shared LUA system library" OFF + CIVETWEB_ENABLE_LUA OFF) +if (CIVETWEB_ENABLE_LUA) + message(STATUS "Linking shared Lua library - ${CIVETWEB_ENABLE_LUA_SHARED}") +endif() + +# Lua Third Party Settings +if (CIVETWEB_ENABLE_LUA) + if (NOT CIVETWEB_ENABLE_LUA_SHARED) + # Lua Version + set(CIVETWEB_LUA_VERSION 5.2.4 CACHE STRING + "The version of Lua to build and include statically") + set_property(CACHE CIVETWEB_LUA_VERSION PROPERTY VALUE ${CIVETWEB_LUA_VERSION}) + message(STATUS "Lua Version - ${CIVETWEB_LUA_VERSION}") + mark_as_advanced(CIVETWEB_LUA_VERSION) + + # Lua Verification Hash + set(CIVETWEB_LUA_MD5_HASH 913fdb32207046b273fdb17aad70be13 CACHE STRING + "The hash of Lua archive to be downloaded") + set_property(CACHE CIVETWEB_LUA_MD5_HASH PROPERTY VALUE ${CIVETWEB_LUA_MD5_HASH}) + mark_as_advanced(CIVETWEB_LUA_MD5_HASH) + endif() + + # Lua Filesystem Version + set(CIVETWEB_LUA_FILESYSTEM_VERSION 1.6.3 CACHE STRING + "The version of Lua Filesystem to build and include statically") + set_property(CACHE CIVETWEB_LUA_FILESYSTEM_VERSION PROPERTY VALUE ${CIVETWEB_LUA_FILESYSTEM_VERSION}) + message(STATUS "Lua Filesystem Version - ${CIVETWEB_LUA_FILESYSTEM_VERSION}") + mark_as_advanced(CIVETWEB_LUA_FILESYSTEM_VERSION) + + # Lua Filesystem Verification Hash + set(CIVETWEB_LUA_FILESYSTEM_MD5_HASH d0552c7e5a082f5bb2865af63fb9dc95 CACHE STRING + "The hash of Lua Filesystem archive to be downloaded") + set_property(CACHE CIVETWEB_LUA_FILESYSTEM_MD5_HASH PROPERTY VALUE ${CIVETWEB_LUA_FILESYSTEM_MD5_HASH}) + mark_as_advanced(CIVETWEB_LUA_FILESYSTEM_MD5_HASH) + + # Lua SQLite Version + set(CIVETWEB_LUA_SQLITE_VERSION 0.9.3 CACHE STRING + "The version of Lua SQLite to build and include statically") + set_property(CACHE CIVETWEB_LUA_SQLITE_VERSION PROPERTY VALUE ${CIVETWEB_LUA_SQLITE_VERSION}) + message(STATUS "Lua SQLite Version - ${CIVETWEB_LUA_SQLITE_VERSION}") + mark_as_advanced(CIVETWEB_LUA_SQLITE_VERSION) + + # Lua SQLite Verification Hash + set(CIVETWEB_LUA_SQLITE_MD5_HASH 43234ae08197dfce6da02482ed14ec92 CACHE STRING + "The hash of Lua SQLite archive to be downloaded") + set_property(CACHE CIVETWEB_LUA_SQLITE_MD5_HASH PROPERTY VALUE ${CIVETWEB_LUA_SQLITE_MD5_HASH}) + mark_as_advanced(CIVETWEB_LUA_SQLITE_MD5_HASH) + + # Lua XML Version + set(CIVETWEB_LUA_XML_VERSION 1.8.0 CACHE STRING + "The version of Lua XML to build and include statically") + set_property(CACHE CIVETWEB_LUA_XML_VERSION PROPERTY VALUE ${CIVETWEB_LUA_XML_VERSION}) + message(STATUS "Lua XML Version - ${CIVETWEB_LUA_XML_VERSION}") + mark_as_advanced(CIVETWEB_LUA_XML_VERSION) + + # Lua XML Verification Hash + set(CIVETWEB_LUA_XML_MD5_HASH 25e4c276c5d8716af1de0c7853aec2b4 CACHE STRING + "The hash of Lua XML archive to be downloaded") + set_property(CACHE CIVETWEB_LUA_XML_MD5_HASH PROPERTY VALUE ${CIVETWEB_LUA_XML_MD5_HASH}) + mark_as_advanced(CIVETWEB_LUA_XML_MD5_HASH) + + # SQLite Version + set(CIVETWEB_SQLITE_VERSION 3.8.9 CACHE STRING + "The version of SQLite to build and include statically") + set_property(CACHE CIVETWEB_SQLITE_VERSION PROPERTY VALUE ${CIVETWEB_SQLITE_VERSION}) + message(STATUS "SQLite Version - ${CIVETWEB_SQLITE_VERSION}") + mark_as_advanced(CIVETWEB_SQLITE_VERSION) + + # SQLite Verification Hash + set(CIVETWEB_SQLITE_MD5_HASH 02e9c3a6daa8b8587cf6bef828c2e33f CACHE STRING + "The hash of SQLite archive to be downloaded") + set_property(CACHE CIVETWEB_SQLITE_MD5_HASH PROPERTY VALUE ${CIVETWEB_SQLITE_MD5_HASH}) + mark_as_advanced(CIVETWEB_SQLITE_MD5_HASH) +endif() + +# Duktape CGI support +option(CIVETWEB_ENABLE_DUKTAPE "Enable Duktape CGIs" OFF) +message(STATUS "Duktape CGI support - ${CIVETWEB_ENABLE_DUKTAPE}") + +# SSL support +option(CIVETWEB_ENABLE_SSL "Enables the secure socket layer" ON) +message(STATUS "SSL support - ${CIVETWEB_ENABLE_SSL}") + +# OpenSSL 1.1 API +option(CIVETWEB_SSL_OPENSSL_API_1_1 "Use the OpenSSL 1.1 API" OFF) +message(STATUS "Compile for OpenSSL 1.1 API - ${CIVETWEB_SSL_OPENSSL_API_1_1}") + +# Dynamically load or link the SSL libraries +cmake_dependent_option( + CIVETWEB_ENABLE_SSL_DYNAMIC_LOADING "Dynamically loads the SSL library rather than linking it" ON + CIVETWEB_ENABLE_SSL OFF) +if (CIVETWEB_ENABLE_SSL) + message(STATUS "Dynamically load SSL libraries - ${CIVETWEB_ENABLE_SSL_DYNAMIC_LOADING}") +endif() + +# Third Party Download location +set(CIVETWEB_THIRD_PARTY_DIR "${CMAKE_BINARY_DIR}/third_party" CACHE STRING + "The location that third party code is downloaded, built and installed") +set_property(CACHE CIVETWEB_THIRD_PARTY_DIR PROPERTY VALUE ${CIVETWEB_THIRD_PARTY_DIR}) + +# Unix systems can define the dynamic library names to load +if (CIVETWEB_ENABLE_SSL_DYNAMIC_LOADING AND NOT DARWIN AND UNIX) + # SSL library name + set(CIVETWEB_SSL_SSL_LIB "libssl.so" CACHE STRING + "The name of the SSL library to load") + set_property(CACHE CIVETWEB_SSL_SSL_LIB PROPERTY VALUE ${CIVETWEB_SSL_SSL_LIB}) + message(STATUS "SSL Library Name - ${CIVETWEB_SSL_SSL_LIB}") + + # Crytography library name + set(CIVETWEB_SSL_CRYPTO_LIB "libcrypto.so" CACHE STRING + "The name of the SSL Cryptography library to load") + set_property(CACHE CIVETWEB_SSL_CRYPTO_LIB PROPERTY VALUE ${CIVETWEB_SSL_CRYPTO_LIB}) + message(STATUS "SSL Cryptography Library Name - ${CIVETWEB_SSL_CRYPTO_LIB}") +endif() + +# Allow warnings in 3rd party components +if (CIVETWEB_ENABLE_LUA OR CIVETWEB_ENABLE_DUKTAPE) +SET(CIVETWEB_ALLOW_WARNINGS YES) +endif() + +# The C and C++ standards to use +set(CIVETWEB_C_STANDARD auto CACHE STRING + "The C standard to use; auto determines the latest supported by the compiler") +set_property(CACHE CIVETWEB_C_STANDARD PROPERTY STRINGS auto c11 c99 c89) +set(CIVETWEB_CXX_STANDARD auto CACHE STRING + "The C++ standard to use; auto determines the latest supported by the compiler") +set_property(CACHE CIVETWEB_CXX_STANDARD PROPERTY STRINGS auto c++14 c++11 c++98) + +# Configure the linker +if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") + find_program(GCC_AR gcc-ar) + if (GCC_AR) + set(CMAKE_AR ${GCC_AR}) + endif() + find_program(GCC_RANLIB gcc-ranlib) + if (GCC_RANLIB) + set(CMAKE_RANLIB ${GCC_RANLIB}) + endif() +endif() + +# Configure the C compiler +message(STATUS "Configuring C Compiler") +if ("${CIVETWEB_C_STANDARD}" STREQUAL "auto") + add_c_compiler_flag(-std=c11) + if (NOT HAVE_C_FLAG_STD_C11) + add_c_compiler_flag(-std=c99) + if (NOT HAVE_C_FLAG_STD_C99) + add_c_compiler_flag(-std=c89) + endif() + endif() +else() + add_c_compiler_flag(-std=${CIVETWEB_C_STANDARD}) +endif() + +#Warnings: enable everything +add_c_compiler_flag(-Wall) +add_c_compiler_flag(-Wextra) +add_c_compiler_flag(-Wshadow) +add_c_compiler_flag(-Wconversion) +add_c_compiler_flag(-Wmissing-prototypes) +add_c_compiler_flag(-Weverything) +add_c_compiler_flag(-Wparentheses) +add_c_compiler_flag(/W4) # VisualStudio highest warning level + +#Warnings: Disable some warnings +add_c_compiler_flag(-Wno-padded) # padding in structures by compiler +add_c_compiler_flag(-Wno-unused-macros) # so what? +add_c_compiler_flag(-Wno-reserved-id-macros) # for system headers +add_c_compiler_flag(-Wno-format-nonliteral) # printf(myFormatStringVar, ...) +add_c_compiler_flag(-Wno-date-time) # using __DATE__ once +add_c_compiler_flag(-Wno-cast-qual) # const cast +add_c_compiler_flag(/Wd4820) # padding + +if (MINGW) + add_c_compiler_flag(-Wno-format) +endif() +if (NOT CIVETWEB_ALLOW_WARNINGS) + add_c_compiler_flag(-Werror) +endif() +add_c_compiler_flag(/WX) +add_c_compiler_flag(-pedantic-errors) +add_c_compiler_flag(-fvisibility=hidden) +add_c_compiler_flag(-fstack-protector-strong RELEASE) +add_c_compiler_flag(-flto RELEASE) +if (${CIVETWEB_ENABLE_ASAN}) +add_c_compiler_flag(-fsanitize=undefined DEBUG) +add_c_compiler_flag(-fsanitize=address DEBUG) +if (HAVE_C_FLAG_FSANITIZE_ADDRESS) + add_c_compiler_flag(-static-asan DEBUG) +endif() +endif() +add_c_compiler_flag(-fstack-protector-all DEBUG) +if (MINGW) + add_c_compiler_flag(-mwindows) +endif() + +# Coverage build type +set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_DEBUG}" CACHE STRING + "Flags used by the C compiler during coverage builds." + FORCE) +set(CMAKE_EXE_LINKER_FLAGS_COVERAGE + "${CMAKE_EXE_LINKER_FLAGS_DEBUG}" CACHE STRING + "Flags used for linking binaries during coverage builds." + FORCE) +set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE + "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE STRING + "Flags used by the shared libraries linker during coverage builds." + FORCE) +mark_as_advanced( + CMAKE_CXX_FLAGS_COVERAGE + CMAKE_C_FLAGS_COVERAGE + CMAKE_EXE_LINKER_FLAGS_COVERAGE + CMAKE_SHARED_LINKER_FLAGS_COVERAGE) +set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING + "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage." + FORCE) +add_c_compiler_flag(--coverage COVERAGE) + +# Configure the C++ compiler +if (CIVETWEB_ENABLE_CXX) + message(STATUS "Configuring C++ Compiler") + if ("${CIVETWEB_CXX_STANDARD}" STREQUAL "auto") + add_cxx_compiler_flag(-std=c++14) + if (NOT HAVE_CXX_FLAG_STD_CXX14) + add_cxx_compiler_flag(-std=c++11) + if (NOT HAVE_CXX_FLAG_STD_CXX11) + add_cxx_compiler_flag(-std=c++98) + endif() + endif() + else() + add_cxx_compiler_flag(-std=${CIVETWEB_CXX_STANDARD}) + endif() + add_cxx_compiler_flag(-Wall) + add_cxx_compiler_flag(-Wextra) + add_cxx_compiler_flag(-Wshadow) + add_cxx_compiler_flag(-Wmissing-prototypes) + add_cxx_compiler_flag(-Weverything) + add_cxx_compiler_flag(/W4) + add_cxx_compiler_flag(-Wno-padded) + add_cxx_compiler_flag(/Wd4820) # padding + add_cxx_compiler_flag(-Wno-unused-macros) + add_cxx_compiler_flag(-Wno-format-nonliteral) + if (MINGW) + add_cxx_compiler_flag(-Wno-format) + endif() + if (NOT CIVETWEB_ALLOW_WARNINGS) + add_cxx_compiler_flag(-Werror) + endif() + add_cxx_compiler_flag(/WX) + add_cxx_compiler_flag(-pedantic-errors) + add_cxx_compiler_flag(-fvisibility=hidden) + add_cxx_compiler_flag(-fstack-protector-strong RELEASE) + add_cxx_compiler_flag(-flto RELEASE) + if (${CIVETWEB_ENABLE_ASAN}) + add_cxx_compiler_flag(-fsanitize=undefined DEBUG) + add_cxx_compiler_flag(-fsanitize=address DEBUG) + if (HAVE_CXX_FLAG_FSANITIZE_ADDRESS) + add_cxx_compiler_flag(-static-asan DEBUG) + endif() + endif() + add_cxx_compiler_flag(-fstack-protector-all DEBUG) + if (MINGW) + add_cxx_compiler_flag(-mwindows) + endif() + set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}" CACHE STRING + "Flags used by the C++ compiler during coverage builds." + FORCE) + add_cxx_compiler_flag(--coverage COVERAGE) +endif() + +# Set up the definitions +if (${CMAKE_BUILD_TYPE} MATCHES "[Dd]ebug") + add_definitions(-DDEBUG) +endif() +if (CIVETWEB_ENABLE_IPV6) + add_definitions(-DUSE_IPV6) +endif() +if (CIVETWEB_ENABLE_WEBSOCKETS) + add_definitions(-DUSE_WEBSOCKET) +endif() +if (CIVETWEB_ENABLE_SERVER_STATS) + add_definitions(-DUSE_SERVER_STATS) +endif() +if (CIVETWEB_SERVE_NO_FILES) + add_definitions(-DNO_FILES) +endif() +if (CIVETWEB_DISABLE_CGI) + add_definitions(-DNO_CGI) +endif() +if (CIVETWEB_DISABLE_CACHING) + add_definitions(-DNO_CACHING) +endif() +if (CIVETWEB_ENABLE_LUA) + add_definitions(-DUSE_LUA) +endif() +if (CIVETWEB_ENABLE_DUKTAPE) + add_definitions(-DUSE_DUKTAPE) +endif() +if (CIVETWEB_ENABLE_MEMORY_DEBUGGING) + add_definitions(-DMEMORY_DEBUGGING) +endif() +if (NOT CIVETWEB_ENABLE_SSL) + add_definitions(-DNO_SSL) +elseif (NOT CIVETWEB_ENABLE_SSL_DYNAMIC_LOADING) + add_definitions(-DNO_SSL_DL) +else() + if(CIVETWEB_SSL_SSL_LIB) + add_definitions(-DSSL_LIB="${CIVETWEB_SSL_SSL_LIB}") + endif() + if(CIVETWEB_SSL_CRYPTO_LIB) + add_definitions(-DCRYPTO_LIB="${CIVETWEB_SSL_CRYPTO_LIB}") + endif() +endif() +if(CIVETWEB_SSL_OPENSSL_API_1_1) + add_definitions(-DOPENSSL_API_1_1) +endif() +add_definitions(-DUSE_STACK_SIZE=${CIVETWEB_THREAD_STACK_SIZE}) + +# Set 32 or 64 bit environment +if (${CMAKE_ARCH} MATCHES "[Xx]86") +add_c_compiler_flag(-m32) +endif() +if (${CMAKE_ARCH} MATCHES "[Xx]64") +add_c_compiler_flag(-m64) +endif() +# TODO: add support for -march + +# Build the targets +add_subdirectory(src) + +# Enable the testing of the library/executable +include(CTest) +if (BUILD_TESTING) + # Check unit testing framework Version + set(CIVETWEB_CHECK_VERSION 0.11.0 CACHE STRING + "The version of Check unit testing framework to build and include statically") + set_property(CACHE CIVETWEB_CHECK_VERSION PROPERTY VALUE ${CIVETWEB_CHECK_VERSION}) + message(STATUS "Check Unit Testing Framework Version - ${CIVETWEB_CHECK_VERSION}") + mark_as_advanced(CIVETWEB_CHECK_VERSION) + + # Check unit testing framework Verification Hash + # Hash for Check 0.10.0: 67a34c40b5bc888737f4e5ae82e9939f + # Hash for Check 0.11.0: 1b14ee307dca8e954a8219c34484d7c4 + set(CIVETWEB_CHECK_MD5_HASH 1b14ee307dca8e954a8219c34484d7c4 CACHE STRING + "The hash of Check unit testing framework archive to be downloaded") + set_property(CACHE CIVETWEB_CHECK_MD5_HASH PROPERTY VALUE ${CIVETWEB_CHECK_MD5_HASH}) + mark_as_advanced(CIVETWEB_CHECK_MD5_HASH) + + # Build the testing + add_subdirectory(test) +endif() + +# Set up CPack +include(InstallRequiredSystemLibraries) +set(CPACK_PACKAGE_VENDOR "civetweb Contributors") +set(CPACK_PACKAGE_CONTACT "civetweb@github.com") +set(CPACK_PACKAGE_VERSION_MAJOR "${CIVETWEB_VERSION_MAJOR}") +set(CPACK_PACKAGE_VERSION_MINOR "${CIVETWEB_VERSION_MINOR}") +set(CPACK_PACKAGE_VERSION_PATCH "${CIVETWEB_VERSION_PATCH}") +set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "A HTTP library and server") +set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README.md") +set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.md") +set(CPACK_STRIP_FILES TRUE) +set(CPACK_PACKAGE_DEPENDS "openssl") +if (CIVETWEB_ENABLE_LUA_SHARED) + set(CPACK_PACKAGE_DEPENDS "lua, ${CPACK_PACKAGE_DEPENDS}") +endif() + +# RPM Packaging +set(CPACK_RPM_PACKAGE_GROUP "Development/Libraries") +set(CPACK_RPM_PACKAGE_LICENSE "MIT") +set(CPACK_RPM_PACKAGE_ARCHITECTURE "${CIVETWEB_ARCHITECTURE}") +set(CPACK_RPM_PACKAGE_REQUIRES "${CPACK_PACKAGE_DEPENDS}") + +# Debian Packaging +set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "${CIVETWEB_ARCHITECTURE}") +set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://github.com/civetweb/civetweb") +set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_PACKAGE_DEPENDS}") + +# WiX Packaging +# TODO: www.cmake.org/cmake/help/v3.0/module/CPackWIX.html + +# Finalize CPack settings +include(CPack) diff --git a/src/civetweb/CREDITS.md b/src/civetweb/CREDITS.md new file mode 100644 index 000000000..4e4c98745 --- /dev/null +++ b/src/civetweb/CREDITS.md @@ -0,0 +1,167 @@ +# Civetweb Contributors + +* Abhishek Lekshmanan +* Adam Bailey +* Alan Somers +* Alex Kozlov +* bel2125 +* Ben M. Ward +* BigJoe +* Bjoern Petri +* Braedy Kuzma +* brett +* Brian Lambert +* Brian Spratke +* cdbishop +* celeron55 +* Charles Olivi +* Christian Mauderer +* Christopher Galas +* cjh +* Daniel Oaks +* Daniel Rempel +* Danny Al-Gaaf +* Dave Brower +* David Arnold +* David Loffredo +* Dialga +* ehlertjd +* Eric Tsau +* Erik Beran +* extergnoto +* F-Secure Corporation +* feneuilflo +* Fernando G. Aranda +* Grahack +* grenclave +* grunk +* hansipie +* HariKamath Kamath +* Henry Chang +* Jack +* Jacob Skillin +* Jan Willem Janssen +* Jeremy Lin +* Jim Evans +* jmc- +* Jochen Scheib +* Joe Mucchiello +* Joel Gallant +* Johan De Taeye +* Jordan +* Jordan Shelley +* Joshua Boyd +* Joshua D. Boyd +* kakwa +* kalphamon +* Keith Kyzivat +* Kevin Branigan +* Kevin Wojniak +* Kimmo Mustonen +* Lammert Bies +* Lawrence +* Li Peng +* Lianghui +* Maarten Fremouw +* makrsmark +* Mark Lakata +* Martin Gaida +* Mateusz Gralka +* Matt Clarkson +* mingodad +* Morgan McGuire +* mrdvlpr.xnu +* Neil Jensen +* Nick Hildebrant +* Nigel Stewart +* nihildeb +* No Face Press +* palortoff +* Patrick Drechsler +* Patrick Trinkle +* Paul Sokolovsky +* Paulo Brizolara +* pavel.pimenov +* PavelVozenilek +* Perttu Ahola +* Peter Foerster +* Philipp Friedenberger +* Philipp Hasper +* Red54 +* Richard Screene +* pkvamme +* Sage Weil +* Sangwhan Moon +* Saumitra Vikram +* Scott Nations +* sgmesservey +* shantanugadgil +* Simon Hailes +* slidertom +* SpaceLord +* sunfch +* thewaterymoon +* THILMANT, Bernard +* Thomas Davis +* tnoho +* Toni Wilk +* Ulrich Hertlein +* Walt Steverson +* webxer +* William Greathouse +* xeoshow +* xtne6f +* Yehuda Sadeh + +# Mongoose Contributors +CivetWeb is based on the Mongoose code. The following users contributed to the original Mongoose release between 2010 and 2013. This list was generated from the Mongoose GIT logs. It does not contain contributions from the Mongoose mailing list. There is no record for contributors prior to 2010. + +* Sergey Lyubka +* Arnout Vandecappelle (Essensium/Mind) +* Benoît Amiaux +* Cody Hanson +* Colin Leitner +* Daniel Oaks +* Eric Bakan +* Erik Oomen +* Filipp Kovalev +* Ger Hobbelt +* Hendrik Polczynski +* Henrique Mendonça +* Igor Okulist +* Jay +* Joe Mucchiello +* John Safranek +* Joseph Mainwaring +* José Miguel Gonçalves +* KIU Shueng Chuan +* Katerina Blinova +* Konstantin Sorokin +* Marin Atanasov Nikolov +* Matt Healy +* Miguel Morales +* Mikhail Nikalyukin +* MikieMorales +* Mitch Hendrickson +* Nigel Stewart +* Pavel +* Pavel Khlebovich +* Rogerz Zhang +* Sebastian Reinhard +* Stefan Doehla +* Thileepan +* abadc0de +* arvidn +* bick +* ff.feng +* jmucchiello +* jwang +* lsm +* migal +* mlamb +* nullable.type +* shantanugadgil +* tayS +* test +* valenok + diff --git a/src/civetweb/LICENSE.md b/src/civetweb/LICENSE.md new file mode 100644 index 000000000..be6ae19b9 --- /dev/null +++ b/src/civetweb/LICENSE.md @@ -0,0 +1,208 @@ +ALL LICENSES +===== + +This document includes several copyright licenses for different +aspects of the software. Not all licenses may apply depending +on the features chosen. + + +Civetweb License +----- + +### Included with all features. + +> Copyright (c) 2013-2017 The CivetWeb developers ([CREDITS.md](https://github.com/civetweb/civetweb/blob/master/CREDITS.md)) +> +> Copyright (c) 2004-2013 Sergey Lyubka +> +> Copyright (c) 2013 No Face Press, LLC (Thomas Davis) +> +> Copyright (c) 2013 F-Secure Corporation +> +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. + + +Lua License +------ + +### Included only if built with Lua support. + +http://www.lua.org/license.html + +> Copyright (C) 1994-2015 Lua.org, PUC-Rio. +> +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. + + +SQLite3 License +------ + +### Included only if built with Lua and SQLite support. + +http://www.sqlite.org/copyright.html + +> 2001 September 15 +> +> The author disclaims copyright to this source code. In place of +> a legal notice, here is a blessing: +> +> May you do good and not evil. +> May you find forgiveness for yourself and forgive others. +> May you share freely, never taking more than you give. + + +lsqlite3 License +------ + +### Included only if built with Lua and SQLite support. + +> Copyright (C) 2002-2013 Tiago Dionizio, Doug Currie +> All rights reserved. +> Author : Tiago Dionizio +> Author : Doug Currie +> Library : lsqlite3 - a SQLite 3 database binding for Lua 5 +> +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. + + +Lua File System License +------ + +### Included only if built with Lua support. + +http://keplerproject.github.io/luafilesystem/license.html + +> Copyright © 2003 Kepler Project. +> +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. + + +LuaXML License +------ + +### Included only if built with Lua and LuaXML support. + +> LuaXML License +> +> LuaXml is licensed under the terms of the MIT license reproduced below, +> the same as Lua itself. This means that LuaXml is free software and can be +> used for both academic and commercial purposes at absolutely no cost. +> +> Copyright (C) 2007-2013 Gerald Franz, eludi.net +> +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. + + +Duktape License +------ + +### Included only if built with Duktape support. + +https://github.com/svaarala/duktape/blob/master/LICENSE.txt + +> =============== +> Duktape license +> =============== +> +> (http://opensource.org/licenses/MIT) +> +> Copyright (c) 2013-2015 by Duktape authors (see AUTHORS.rst) +> +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. + diff --git a/src/civetweb/Makefile b/src/civetweb/Makefile new file mode 100644 index 000000000..fdc015f9e --- /dev/null +++ b/src/civetweb/Makefile @@ -0,0 +1,332 @@ +# +# Copyright (c) 2013 No Face Press, LLC +# License http://opensource.org/licenses/mit-license.php MIT License +# + +# +# For help try, "make help" +# + +include resources/Makefile.in-os + +CPROG = civetweb +#CXXPROG = civetweb +UNIT_TEST_PROG = civetweb_test + +BUILD_DIR = out + +# Installation directories by convention +# http://www.gnu.org/prep/standards/html_node/Directory-Variables.html +PREFIX = /usr/local +EXEC_PREFIX = $(PREFIX) +BINDIR = $(EXEC_PREFIX)/bin +DATAROOTDIR = $(PREFIX)/share +DOCDIR = $(DATAROOTDIR)/doc/$(CPROG) +SYSCONFDIR = $(PREFIX)/etc +HTMLDIR = $(DOCDIR) + +# build tools +MKDIR = mkdir -p +RMF = rm -f +RMRF = rm -rf + +# desired configuration of the document root +# never assume that the document_root actually +# exists on the build machine. When building +# a chroot, PREFIX if just a directory which +# later becomes /. +DOCUMENT_ROOT = $(HTMLDIR) +PORTS = 8080 + +BUILD_DIRS = $(BUILD_DIR) $(BUILD_DIR)/src $(BUILD_DIR)/resources + +LIB_SOURCES = src/civetweb.c +LIB_INLINE = src/mod_lua.inl src/md5.inl +APP_SOURCES = src/main.c +WINDOWS_RESOURCES = resources/res.rc +UNIT_TEST_SOURCES = test/unit_test.c +SOURCE_DIRS = + +OBJECTS = $(LIB_SOURCES:.c=.o) $(APP_SOURCES:.c=.o) +BUILD_RESOURCES = + +# The unit tests include the source files directly to get visibility to the +# static functions. So we clear OBJECTS so that we don't try to build or link +# with any external object. Later if we find WITH_LUA=1, we'll add lua objects +# to this variable so we can run lua-specific unit tests. +ifeq ($(MAKECMDGOALS), unit_test) +OBJECTS = +BUILD_DIRS += $(BUILD_DIR)/test +endif + +# only set main compile options if none were chosen +CFLAGS += -Wall -Wextra -Wshadow -Wformat-security -Winit-self -Wmissing-prototypes -D$(TARGET_OS) -Iinclude $(COPT) -DUSE_STACK_SIZE=102400 + +LIBS = -lpthread -lm + +ifdef WITH_DEBUG + CFLAGS += -g -DDEBUG +else + CFLAGS += -O2 -DNDEBUG +endif + +ifdef WITH_CPP + OBJECTS += src/CivetServer.o + LCC = $(CXX) +else + LCC = $(CC) +endif + +ifdef WITH_ALL + WITH_WEBSOCKET = 1 + WITH_IPV6 = 1 + WITH_LUA = 1 + WITH_DUKTAPE = 1 + WITH_SERVER_STATS = 1 + #WITH_CPP is not defined, ALL means only real features, not wrappers +endif + +ifdef WITH_LUA_SHARED + WITH_LUA = 1 +endif + +ifdef WITH_LUAJIT_SHARED + WITH_LUA_SHARED = 1 + WITH_LUA = 1 + WITH_LUA_VERSION = 501 +endif + +ifdef WITH_LUA + include resources/Makefile.in-lua +endif + +ifdef WITH_SSJS + WITH_DUKTAPE = 1 +endif + +ifdef WITH_DUKTAPE_SHARED + WITH_DUKTAPE = 1 +endif + +ifdef WITH_DUKTAPE + include resources/Makefile.in-duktape +endif + +ifdef WITH_IPV6 + CFLAGS += -DUSE_IPV6 +endif + +ifdef WITH_WEBSOCKET + CFLAGS += -DUSE_WEBSOCKET +endif +ifdef WITH_WEBSOCKETS + CFLAGS += -DUSE_WEBSOCKET +endif + +ifdef WITH_SERVER_STAT + CFLAGS += -DUSE_SERVER_STATS +endif +ifdef WITH_SERVER_STATS + CFLAGS += -DUSE_SERVER_STATS +endif + +ifdef CONFIG_FILE + CFLAGS += -DCONFIG_FILE=\"$(CONFIG_FILE)\" +endif + +ifdef CONFIG_FILE2 + CFLAGS += -DCONFIG_FILE2=\"$(CONFIG_FILE2)\" +endif + +ifdef SSL_LIB + CFLAGS += -DSSL_LIB=\"$(SSL_LIB)\" +endif + +ifdef CRYPTO_LIB + CFLAGS += -DCRYPTO_LIB=\"$(CRYPTO_LIB)\" +endif + +BUILD_DIRS += $(addprefix $(BUILD_DIR)/, $(SOURCE_DIRS)) +BUILD_OBJECTS = $(addprefix $(BUILD_DIR)/, $(OBJECTS)) +MAIN_OBJECTS = $(addprefix $(BUILD_DIR)/, $(APP_SOURCES:.c=.o)) +LIB_OBJECTS = $(filter-out $(MAIN_OBJECTS), $(BUILD_OBJECTS)) + +ifeq ($(TARGET_OS),LINUX) + LIBS += -lrt -ldl + CAN_INSTALL = 1 +endif + +ifeq ($(TARGET_OS),WIN32) + MKDIR = mkdir + RMF = del /q + RMRF = rmdir /s /q +endif + +ifdef WITH_LUAJIT_SHARED + LIBS += -lluajit-5.1 +else +ifdef WITH_LUA_SHARED + LIBS += $(LUA_SHARED_LIB_FLAG) +endif +endif + +ifneq (, $(findstring mingw32, $(shell $(CC) -dumpmachine))) + BUILD_RESOURCES = $(BUILD_DIR)/$(WINDOWS_RESOURCES:.rc=.o) + LIBS += -lws2_32 -mwindows + SHARED_LIB = dll +else + SHARED_LIB = so +endif + +all: build + +help: + @echo "make help show this message" + @echo "make build compile" + @echo "make install install on the system" + @echo "make clean clean up the mess" + @echo "make lib build a static library" + @echo "make slib build a shared library" + @echo "make unit_test build unit tests executable" + @echo "" + @echo " Make Options" + @echo " WITH_LUA=1 build with Lua support; include Lua as static library" + @echo " WITH_LUA_SHARED=1 build with Lua support; use dynamic linking to liblua5.2.so" + @echo " WITH_LUA_VERSION=502 build with Lua 5.2.x (501 for Lua 5.1.x to 503 for 5.3.x)" + @echo " WITH_DUKTAPE=1 build with Duktape support; include as static library" + @echo " WITH_DUKTAPE_SHARED=1 build with Duktape support; use libduktape1.3.so" +# @echo " WITH_DUKTAPE_VERSION=103 build with Duktape 1.3.x" + @echo " WITH_DEBUG=1 build with GDB debug support" + @echo " WITH_IPV6=1 with IPV6 support" + @echo " WITH_WEBSOCKET=1 build with web socket support" + @echo " WITH_SERVER_STATS=1 build includes support for server statistics" + @echo " WITH_CPP=1 build library with c++ classes" + @echo " CONFIG_FILE=file use 'file' as the config file" + @echo " CONFIG_FILE2=file use 'file' as the backup config file" + @echo " DOCUMENT_ROOT=/path document root override when installing" + @echo " PORTS=8080 listening ports override when installing" + @echo " SSL_LIB=libssl.so.0 use versioned SSL library" + @echo " CRYPTO_LIB=libcrypto.so.0 system versioned CRYPTO library" + @echo " PREFIX=/usr/local sets the install directory" + @echo " COPT='-DNO_SSL' method to insert compile flags" + @echo "" + @echo " Compile Flags" + @echo " NDEBUG strip off all debug code" + @echo " DEBUG build debug version (very noisy)" + @echo " NO_CGI disable CGI support" + @echo " NO_SSL disable SSL functionality" + @echo " NO_SSL_DL link against system libssl library" + @echo " NO_FILES do not serve files from a directory" + @echo " NO_CACHING disable caching (usefull for systems without timegm())" + @echo "" + @echo " Variables" + @echo " TARGET_OS='$(TARGET_OS)'" + @echo " CFLAGS='$(CFLAGS)'" + @echo " CXXFLAGS='$(CXXFLAGS)'" + @echo " LDFLAGS='$(LDFLAGS)'" + @echo " CC='$(CC)'" + @echo " CXX='$(CXX)'" + +build: $(CPROG) $(CXXPROG) + +unit_test: $(UNIT_TEST_PROG) + +ifeq ($(CAN_INSTALL),1) +install: $(HTMLDIR)/index.html $(SYSCONFDIR)/civetweb.conf + install -d -m 755 "$(DOCDIR)" + install -m 644 *.md "$(DOCDIR)" + install -d -m 755 "$(BINDIR)" + install -m 755 $(CPROG) "$(BINDIR)/" + +# Install target we do not want to overwrite +# as it may be an upgrade +$(HTMLDIR)/index.html: + install -d -m 755 "$(HTMLDIR)" + install -m 644 resources/itworks.html $(HTMLDIR)/index.html + install -m 644 resources/civetweb_64x64.png $(HTMLDIR)/ + +# Install target we do not want to overwrite +# as it may be an upgrade +$(SYSCONFDIR)/civetweb.conf: + install -d -m 755 "$(SYSCONFDIR)" + install -m 644 resources/civetweb.conf "$(SYSCONFDIR)/" + @sed -i 's#^document_root.*$$#document_root $(DOCUMENT_ROOT)#' "$(SYSCONFDIR)/civetweb.conf" + @sed -i 's#^listening_ports.*$$#listening_ports $(PORTS)#' "$(SYSCONFDIR)/civetweb.conf" + +else +install: + @echo "Target not flagged for installation. Use CAN_INSTALL=1 to force" + @echo "As a precaution only LINUX targets are set as installable." + @echo "If the target is linux-like, use CAN_INSTALL=1 option." +endif + +lib: lib$(CPROG).a + +slib: lib$(CPROG).$(SHARED_LIB) + +clean: + $(RMRF) $(BUILD_DIR) + $(eval version=$(shell grep -w "define CIVETWEB_VERSION" include/civetweb.h | sed 's|.*VERSION "\(.*\)"|\1|g')) + $(eval major=$(shell echo $(version) | cut -d'.' -f1)) + $(RMRF) lib$(CPROG).a + $(RMRF) lib$(CPROG).so + $(RMRF) lib$(CPROG).so.$(major) + $(RMRF) lib$(CPROG).so.$(version).0 + $(RMRF) $(CPROG) + $(RMF) $(UNIT_TEST_PROG) + +distclean: clean + @$(RMRF) VS2012/Debug VS2012/*/Debug VS2012/*/*/Debug + @$(RMRF) VS2012/Release VS2012/*/Release VS2012/*/*/Release + $(RMF) $(CPROG) lib$(CPROG).so lib$(CPROG).a *.dmg *.msi *.exe lib$(CPROG).dll lib$(CPROG).dll.a + $(RMF) $(UNIT_TEST_PROG) + +lib$(CPROG).a: CFLAGS += -fPIC +lib$(CPROG).a: $(LIB_OBJECTS) + @$(RMF) $@ + ar cq $@ $(LIB_OBJECTS) + +lib$(CPROG).so: CFLAGS += -fPIC +lib$(CPROG).so: $(LIB_OBJECTS) + $(eval version=$(shell grep -w "define CIVETWEB_VERSION" include/civetweb.h | sed 's|.*VERSION "\(.*\)"|\1|g')) + $(eval major=$(shell echo $(version) | cut -d'.' -f1)) + $(LCC) -shared -Wl,-soname,$@.$(major) -o $@.$(version).0 $(CFLAGS) $(LDFLAGS) $(LIB_OBJECTS) + ln -s -f $@.$(major) $@ + ln -s -f $@.$(version).0 $@.$(major) + +lib$(CPROG).dll: CFLAGS += -fPIC +lib$(CPROG).dll: $(LIB_OBJECTS) + $(LCC) -shared -o $@ $(CFLAGS) $(LDFLAGS) $(LIB_OBJECTS) $(LIBS) -Wl,--out-implib,lib$(CPROG).dll.a + +$(UNIT_TEST_PROG): CFLAGS += -Isrc -g +$(UNIT_TEST_PROG): $(LIB_SOURCES) $(LIB_INLINE) $(UNIT_TEST_SOURCES) $(BUILD_OBJECTS) + $(LCC) -o $@ $(CFLAGS) $(LDFLAGS) $(UNIT_TEST_SOURCES) $(BUILD_OBJECTS) $(LIBS) + +$(CPROG): $(BUILD_OBJECTS) $(BUILD_RESOURCES) + $(LCC) -o $@ $(CFLAGS) $(LDFLAGS) $(BUILD_OBJECTS) $(BUILD_RESOURCES) $(LIBS) + +$(CXXPROG): $(BUILD_OBJECTS) + $(CXX) -o $@ $(CFLAGS) $(LDFLAGS) $(BUILD_OBJECTS) $(LIBS) + +$(BUILD_OBJECTS): $(BUILD_DIRS) + +$(BUILD_DIRS): + -@$(MKDIR) "$@" + +$(BUILD_DIR)/%.o : %.cpp + $(CXX) -c $(CFLAGS) $(CXXFLAGS) $< -o $@ + +$(BUILD_DIR)/%.o : %.c + $(CC) -c $(CFLAGS) $< -o $@ + +$(BUILD_RESOURCES) : $(WINDOWS_RESOURCES) + windres $(WINDRES_FLAGS) $< $@ + +# This rules is used to keep the code formatted in a reasonable manor +# For this to work astyle must be installed and in the path +# http://sourceforge.net/projects/astyle +indent: + astyle --suffix=none --style=linux --indent=spaces=4 --lineend=linux include/*.h src/*.c src/*.cpp src/*.inl examples/*/*.c examples/*/*.cpp + +.PHONY: all help build install clean lib so + diff --git a/src/civetweb/Makefile.deprecated b/src/civetweb/Makefile.deprecated new file mode 100644 index 000000000..288c15ba8 --- /dev/null +++ b/src/civetweb/Makefile.deprecated @@ -0,0 +1,208 @@ +# This Makefile is part of Civetweb web server project, +# https://github.com/valenok/civetweb +# +# Example custom build: +# COPT="-g -O0 -DNO_SSL_DL -DUSE_LUA -llua -lcrypto -lssl" make linux +# +# Flags are: +# -DHAVE_MD5 - use system md5 library (-2kb) +# -DNDEBUG - strip off all debug code (-5kb) +# -DDEBUG - build debug version (very noisy) (+7kb) +# -DNO_CGI - disable CGI support (-5kb) +# -DNO_SSL - disable SSL functionality (-2kb) +# -DNO_SSL_DL - link against system libssl library (-1kb) +# -DCONFIG_FILE=\"file\" - use `file' as the default config file +# -DSSL_LIB=\"libssl.so.\" - use system versioned SSL shared object +# -DCRYPTO_LIB=\"libcrypto.so.\" - use system versioned CRYPTO so +# -DUSE_LUA - embed Lua in Civetweb (+100kb) + +PROG = civetweb +CFLAGS = -std=c99 -O2 -W -Wall -pedantic -pthread -pipe -Iinclude $(COPT) + +# To build with Lua, download and unzip Lua 5.2.3 source code into the +# civetweb directory, and then add $(LUA_SOURCES) to CFLAGS +LUA = src/third_party/lua-5.2.3/src +LUA_FLAGS = -I$(LUA) -DLUA_COMPAT_ALL +LUA_SOURCES = $(LUA)/lapi.c $(LUA)/lcode.c $(LUA)/lctype.c \ + $(LUA)/ldebug.c $(LUA)/ldo.c $(LUA)/ldump.c \ + $(LUA)/lfunc.c $(LUA)/lgc.c $(LUA)/llex.c \ + $(LUA)/lmem.c $(LUA)/lobject.c $(LUA)/lopcodes.c \ + $(LUA)/lparser.c $(LUA)/lstate.c $(LUA)/lstring.c \ + $(LUA)/ltable.c $(LUA)/ltm.c $(LUA)/lundump.c \ + $(LUA)/lvm.c $(LUA)/lzio.c $(LUA)/lauxlib.c \ + $(LUA)/lbaselib.c $(LUA)/lbitlib.c $(LUA)/lcorolib.c \ + $(LUA)/ldblib.c $(LUA)/liolib.c $(LUA)/lmathlib.c \ + $(LUA)/loslib.c $(LUA)/lstrlib.c $(LUA)/ltablib.c \ + $(LUA)/loadlib.c $(LUA)/linit.c +LUA_WINOBJS = $(LUA_SOURCES:%.c=%.obj) + +ifneq ($(OS), Windows_NT) + LUA_FLAGS += -DLUA_USE_DLOPEN +endif + +LIB_SOURCES = src/civetweb.c + +ALL_SOURCES = src/main.c $(LIB_SOURCES) src/third_party/sqlite3.c src/third_party/lsqlite3.c src/third_party/lfs.c \ + $(LUA_SOURCES) $(YASSL_SOURCES) + +SQLITE_FLAGS = -DTHREADSAFE=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS +CIVETWEB_FLAGS = -DUSE_LUA -DUSE_LUA_SQLITE3 -DUSE_LUA_FILE_SYSTEM $(COPT) +FLAGS = $(CIVETWEB_FLAGS) $(SQLITE_FLAGS) $(LUA_FLAGS) + + +# Stock windows binary builds with Lua. +# Yassl has a GPL license, so we will leave it out by default. + +ifeq ($(WITH_YASSL), 1) +YASSL = ../cyassl-2.4.6 +YASSL_FLAGS = -I $(YASSL) -I $(YASSL)/cyassl \ + -D _LIB -D OPENSSL_EXTRA -D HAVE_ERRNO_H \ + -D HAVE_GETHOSTBYNAME -D HAVE_INET_NTOA -D HAVE_LIMITS_H \ + -D HAVE_MEMSET -D HAVE_SOCKET -D HAVE_STDDEF_H -D HAVE_STDLIB_H \ + -D HAVE_STRING_H -D HAVE_SYS_STAT_H -D HAVE_SYS_TYPES_H +YASSL_SOURCES = \ + $(YASSL)/src/internal.c $(YASSL)/src/io.c $(YASSL)/src/keys.c \ + $(YASSL)/src/ssl.c $(YASSL)/src/tls.c $(YASSL)/ctaocrypt/src/hmac.c \ + $(YASSL)/ctaocrypt/src/random.c $(YASSL)/ctaocrypt/src/sha.c \ + $(YASSL)/ctaocrypt/src/sha256.c $(YASSL)/ctaocrypt/src/logging.c \ + $(YASSL)/ctaocrypt/src/error.c $(YASSL)/ctaocrypt/src/rsa.c \ + $(YASSL)/ctaocrypt/src/des3.c $(YASSL)/ctaocrypt/src/asn.c \ + $(YASSL)/ctaocrypt/src/coding.c $(YASSL)/ctaocrypt/src/arc4.c \ + $(YASSL)/ctaocrypt/src/md4.c $(YASSL)/ctaocrypt/src/md5.c \ + $(YASSL)/ctaocrypt/src/dh.c $(YASSL)/ctaocrypt/src/dsa.c \ + $(YASSL)/ctaocrypt/src/pwdbased.c $(YASSL)/ctaocrypt/src/aes.c \ + $(YASSL)/ctaocrypt/src/md2.c $(YASSL)/ctaocrypt/src/ripemd.c \ + $(YASSL)/ctaocrypt/src/sha512.c $(YASSL)/src/sniffer.c \ + $(YASSL)/ctaocrypt/src/rabbit.c $(YASSL)/ctaocrypt/src/misc.c \ + $(YASSL)/ctaocrypt/src/tfm.c $(YASSL)/ctaocrypt/src/integer.c \ + $(YASSL)/ctaocrypt/src/ecc.c $(YASSL)/src/ocsp.c $(YASSL)/src/crl.c \ + $(YASSL)/ctaocrypt/src/hc128.c $(YASSL)/ctaocrypt/src/memory.c + + ALL_SOURCES += $(YASSL_SOURCES) + FLAGS += $(YASSL_FLAGS) -DNO_SSL_DL + CIVETWEB_FLAGS += -DNO_SSL_DL + +else +# FLAGS += -DNO_SSL +# CIVETWEB_FLAGS += -DNO_SSL +endif + +ALL_OBJECTS = $(ALL_SOURCES:%.c=%.o) +ALL_WINOBJS = $(ALL_SOURCES:%.c=%.obj) + + +# Using Visual Studio 6.0. To build Civetweb: +# Set MSVC variable below to where VS 6.0 is installed on your system +# Run "PATH_TO_VC6\bin\nmake windows" +MSVC = ../vc6 +#DBG = /Zi /Od +DBG = /DNDEBUG /O1 +CL = $(MSVC)/bin/cl /MD /TC /nologo $(DBG) /W3 /GA /I$(MSVC)/include +LINK = $(MSVC)/bin/link /incremental:no /libpath:$(MSVC)/lib /machine:IX86 \ + user32.lib shell32.lib comdlg32.lib ws2_32.lib advapi32.lib + +all: + @echo "make (linux|bsd|solaris|mac|windows|mingw|cygwin)" + +%.obj: %.c + $(CL) /c $(FLAGS) /Fo$@ $< + +%.o: %.c + $(CC) -o $@ $< -c $(FLAGS) $(CFLAGS) + +# Lua library for Windows +lua.lib: $(LUA_WINOBJS) + $(MSVC)/bin/lib /out:$@ $(LUA_WINOBJS) + +# To build with Lua, make sure you have Lua unpacked into src/third_party/lua-5.2.3 directory +linux_lua: $(ALL_OBJECTS) + $(CC) $(ALL_OBJECTS) -o $(PROG) -ldl + +civetweb.o: src/mod_lua.inl + +# Make sure that the compiler flags come last in the compilation string. +# If not so, this can break some on some Linux distros which use +# "-Wl,--as-needed" turned on by default in cc command. +# Also, this is turned in many other distros in static linkage builds. +linux: + $(CC) $(LIB_SOURCES) src/main.c -o $(PROG) -ldl $(CFLAGS) + +mac: bsd +bsd: + $(CC) $(LIB_SOURCES) src/main.c -o $(PROG) $(CFLAGS) + +bsd_lua: $(ALL_OBJECTS) + $(CC) $(ALL_OBJECTS) -o $@ + +solaris: + $(CC) $(LIB_SOURCES) src/main.c -lnsl -lsocket -o $(PROG) $(CFLAGS) + +lib$(PROG).a: $(ALL_OBJECTS) + ar cr $@ $(ALL_OBJECTS) + +$(PROG).lib: $(ALL_WINOBJS) + $(MSVC)/bin/lib /out:$@ $(ALL_WINOBJS) + +# For codesign to work in non-interactive mode, unlock login keychain: +# security unlock ~/Library/Keychains/login.keychain +# See e.g. http://lists.apple.com/archives/apple-cdsa/2008/Jan/msg00027.html +Civetweb: $(LIB_SOURCES) src/main.c + $(CC) $(LIB_SOURCES) src/main.c src/third_party/lsqlite3.c src/third_party/sqlite3.c src/third_party/lfs.c \ + -DUSE_COCOA $(CFLAGS) $(FLAGS) -mmacosx-version-min=10.4 \ + $(YASSL_SOURCES) $(LUA_SOURCES) \ + -framework Cocoa -ObjC -arch i386 -arch x86_64 -o Civetweb + +cocoa: Civetweb + V=`perl -lne '/define\s+CIVETWEB_VERSION\s+"(\S+)"/ and print $$1' include/civetweb.h`; DIR=dmg/Civetweb.app && rm -rf $$DIR && mkdir -p $$DIR/Contents/{MacOS,Resources} && install -m 644 resources/civetweb_*.png resources/civetweb.icns $$DIR/Contents/Resources/ && install -m 644 resources/Info.plist $$DIR/Contents/ && install -m 755 Civetweb $$DIR/Contents/MacOS/ && ln -fs /Applications dmg/ ; hdiutil create Civetweb_$$V.dmg -volname "Civetweb $$V" -srcfolder dmg -ov #; rm -rf dmg + +un: + $(CC) test/unit_test.c -o unit_test -I. -I$(LUA) $(LUA_SOURCES) \ + $(CFLAGS) -g -O0 + ./unit_test + +wi: + $(CL) test/unit_test.c $(LUA_SOURCES) $(LUA_FLAGS) \ + $(YASSL_SOURCES) $(YASSL_FLAGS) /I. /DNO_SSL_DL \ + /link /libpath:$(MSVC)/lib advapi32.lib /out:unit_test.exe + ./unit_test.exe + +windows: $(ALL_WINOBJS) + $(MSVC)/bin/rc resources/res.rc + $(LINK) /nologo $(ALL_WINOBJS) resources/res.res /out:$(PROG).exe + +# Build for Windows under MinGW +#MINGWDBG= -DDEBUG -O0 -ggdb +MINGWDBG= -DNDEBUG -Os +MINGWOPT= -W -Wall -mthreads -Wl,--subsystem,console $(MINGWDBG) -DHAVE_STDINT $(GCC_WARNINGS) $(COPT) +mingw: + windres resources\res.rc resources\res.o + $(CC) $(MINGWOPT) $(LIB_SOURCES) -lws2_32 \ + -shared -Wl,--out-implib=$(PROG).lib -o $(PROG).dll + $(CC) $(MINGWOPT) $(LIB_SOURCES) src/main.c resources\res.o \ + -lws2_32 -ladvapi32 -lcomdlg32 -o $(PROG).exe + +# Build for Windows under Cygwin +#CYGWINDBG= -DDEBUG -O0 -ggdb +CYGWINDBG= -DNDEBUG -Os +CYGWINOPT= -W -Wall -mthreads -Wl,--subsystem,console $(CYGWINDBG) -DHAVE_STDINT $(GCC_WARNINGS) $(COPT) +cygwin: + windres ./resources/res.rc ./resources/res.o + $(CC) $(CYGWINOPT) $(LIB_SOURCES) -lws2_32 \ + -shared -Wl,--out-implib=$(PROG).lib -o $(PROG).dll + $(CC) $(CYGWINOPT) -Iinclude $(LIB_SOURCES) src/main.c ./resources/res.o \ + -lws2_32 -ladvapi32 -o $(PROG).exe + +tests: + perl test/test.pl $(TEST) + +tarball: clean + F=civetweb-`perl -lne '/define\s+CIVETWEB_VERSION\s+"(\S+)"/ and print $$1' include/civetweb.h`.tgz ; cd .. && tar -czf x civetweb/{LICENSE.md,Makefile,examples,test,resources,*.[ch],*.md} && mv x civetweb/$$F + +release: tarball cocoa + wine make windows + V=`perl -lne '/define\s+CIVETWEB_VERSION\s+"(\S+)"/ and print $$1' include/civetweb.h`; upx civetweb.exe; cp civetweb.exe civetweb-$$V.exe; cp civetweb.exe civetweb_php_bundle/; zip -r civetweb_php_bundle_$$V.zip civetweb_php_bundle/ + +clean: + rm -rf *.o *.core $(PROG) *.obj *.so $(PROG).txt *.dSYM *.tgz \ + $(PROG).exe *.dll *.lib resources/res.o resources/res.RES *.dSYM *.zip *.pdb \ + *.exe *.dmg $(ALL_OBJECTS) $(ALL_WINOBJS) diff --git a/src/civetweb/Makefile.osx b/src/civetweb/Makefile.osx new file mode 100644 index 000000000..44260e84c --- /dev/null +++ b/src/civetweb/Makefile.osx @@ -0,0 +1,42 @@ +# +# Copyright (c) 2013 No Face Press, LLC +# License http://opensource.org/licenses/mit-license.php MIT License +# + +# For codesign to work in non-interactive mode, unlock login keychain: +# security unlock ~/Library/Keychains/login.keychain +# See e.g. http://lists.apple.com/archives/apple-cdsa/2008/Jan/msg00027.html + +# Civetweb features +WITH_LUA = 1 + +PACKAGE = Civetweb +BUILD_DIR = out + +CFLAGS += -DUSE_COCOA -DENABLE_CREATE_CONFIG_FILE -mmacosx-version-min=10.4 -ObjC -arch i386 -arch x86_64 +LDFLAGS += -framework Cocoa + +DMG_DIR = $(BUILD_DIR)/dmg +CONTENTS_DIR = $(DMG_DIR)/$(PACKAGE).app/Contents +RESOURCES_DIR = $(CONTENTS_DIR)/Resources +OSXBIN_DIR = $(CONTENTS_DIR)/MacOS + +CIVETWEB_VERSION = $(shell perl -lne '/define\s+CIVETWEB_VERSION\s+"(\S+)"/ and print $$1' include/civetweb.h) +ZIPFILENAME = $(PACKAGE)-$(CIVETWEB_VERSION).zip + +include Makefile + +package: build + @rm -rf $(DMG_DIR) + install -d -m 755 $(CONTENTS_DIR) $(RESOURCES_DIR) $(OSXBIN_DIR) + install -m 644 resources/Info.plist $(CONTENTS_DIR)/ + install -m 644 resources/civetweb_*.png resources/civetweb.icns $(RESOURCES_DIR)/ + install -m 644 resources/itworks.html $(OSXBIN_DIR)/index.html + install -m 644 resources/civetweb_64x64.png $(OSXBIN_DIR)/ + install -m 755 $(CPROG) $(OSXBIN_DIR)/$(PACKAGE) + install -m 644 docs/Installing.md $(DMG_DIR)/Installing.txt + install -m 644 LICENSE.md $(DMG_DIR)/License.txt + rm -rf $(ZIPFILENAME) + cd $(DMG_DIR) && zip -r ../../$(ZIPFILENAME) . + +.PHONY: package diff --git a/src/civetweb/Qt/CivetWeb.pro b/src/civetweb/Qt/CivetWeb.pro new file mode 100644 index 000000000..6d5e1a281 --- /dev/null +++ b/src/civetweb/Qt/CivetWeb.pro @@ -0,0 +1,34 @@ +TEMPLATE = app +CONFIG += console +CONFIG -= app_bundle +CONFIG -= qt + +SOURCES += \ + ../src/md5.inl \ + ../src/sha1.inl \ + ../src/handle_form.inl \ + ../src/mod_lua.inl \ + ../src/mod_duktape.inl \ + ../src/timer.inl \ + ../src/civetweb.c \ + ../src/main.c + +#include(deployment.pri) +#qtcAddDeployment() + +HEADERS += \ + ../include/civetweb.h + +INCLUDEPATH += \ + ../include/ + +win32 { +LIBS += -lws2_32 -lComdlg32 -lUser32 -lShell32 -lAdvapi32 +} else { +LIBS += -lpthread -ldl -lm +} + + +DEFINES += USE_IPV6 +DEFINES += USE_WEBSOCKET +DEFINES += USE_SERVER_STATS diff --git a/src/civetweb/README.md b/src/civetweb/README.md new file mode 100644 index 000000000..4a334ca26 --- /dev/null +++ b/src/civetweb/README.md @@ -0,0 +1,168 @@ +![CivetWeb](https://raw.githubusercontent.com/civetweb/civetweb/master/resources/civetweb_64x64.png "CivetWeb") CivetWeb +======= + +**The official home of CivetWeb is [https://github.com/civetweb/civetweb](https://github.com/civetweb/civetweb)** + +[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT) +[![GitHub contributors](https://img.shields.io/github/contributors/civetweb/civetweb.svg)](https://github.com/civetweb/civetweb/blob/master/CREDITS.md) + +Continuous integration for Linux and OSX ([Travis CI](https://travis-ci.org/civetweb/civetweb)): + +[![Travis Build Status](https://travis-ci.org/civetweb/civetweb.svg?branch=master)](https://travis-ci.org/civetweb/civetweb) + +Continuous integration for Windows ([AppVeyor](https://ci.appveyor.com/project/civetweb/civetweb)): + +[![Appveyor Build Status](https://ci.appveyor.com/api/projects/status/github/civetweb/civetweb?svg=true)](https://ci.appveyor.com/project/civetweb/civetweb/branch/master) + +Test coverage check ([coveralls](https://coveralls.io/github/civetweb/civetweb), [codecov](https://codecov.io/gh/civetweb/civetweb/branch/master)) (currently in a setup and evaluation phase): + +[![Coveralls](https://img.shields.io/coveralls/civetweb/civetweb.svg?maxAge=3600)]() +[![Coverage Status](https://coveralls.io/repos/github/civetweb/civetweb/badge.svg?branch=master)](https://coveralls.io/github/civetweb/civetweb?branch=master) + +[![codecov](https://codecov.io/gh/civetweb/civetweb/branch/master/graph/badge.svg)](https://codecov.io/gh/civetweb/civetweb) + + + +Static source code analysis ([Coverity](https://scan.coverity.com/projects/5784)): + +[![Coverity Scan Build Status](https://scan.coverity.com/projects/5784/badge.svg)](https://scan.coverity.com/projects/5784) + + + +Project Mission +----------------- + +Project mission is to provide easy to use, powerful, C/C++ embeddable web +server with optional CGI, SSL and Lua support. +CivetWeb has a MIT license so you can innovate without restrictions. + +CivetWeb can be used by developers as a library, to add web server functionality to an existing application. +It can also be used by end users as a stand-alone web server. It is available as single executable, no installation is required. + + +Where to find the official version? +----------------------------------- + +End users can download CivetWeb releases at SourceForge +[https://sourceforge.net/projects/civetweb/](https://sourceforge.net/projects/civetweb/) + +Developers can contribute to CivetWeb via GitHub +[https://github.com/civetweb/civetweb](https://github.com/civetweb/civetweb) + +Trouble tickets should be filed on GitHub +[https://github.com/civetweb/civetweb/issues](https://github.com/civetweb/civetweb/issues) + +Announcements are at Google Groups +[https://groups.google.com/d/forum/civetweb](https://groups.google.com/d/forum/civetweb). Some older support and discussion threads are there as well. However, recently support questions and discussions are usually [GitHub issues](https://github.com/civetweb/civetweb/issues). + +Source releases can be found on GitHub +[https://github.com/civetweb/civetweb/releases](https://github.com/civetweb/civetweb/releases) + +A very brief overview can be found on GitHub Pages +[http://civetweb.github.io/civetweb/](http://civetweb.github.io/civetweb/) + + +Quick start documentation +-------------------------- + +- [docs/Installing.md](https://github.com/civetweb/civetweb/blob/master/docs/Installing.md) - Install Guide (for end users using pre-built binaries) +- [docs/UserManual.md](https://github.com/civetweb/civetweb/blob/master/docs/UserManual.md) - End User Guide +- [docs/Building.md](https://github.com/civetweb/civetweb/blob/master/docs/Building.md) - Building the Server (quick start guide) +- [docs/Embedding.md](https://github.com/civetweb/civetweb/blob/master/docs/Embedding.md) - Embedding (how to add HTTP support to an existing application) +- [docs/OpenSSL.md](https://github.com/civetweb/civetweb/blob/master/docs/OpenSSL.md) - Adding HTTPS (SSL/TLS) support using OpenSSL. +- [API documentation](https://github.com/civetweb/civetweb/tree/master/docs/api) - Additional documentation on the civetweb application programming interface ([civetweb.h](https://github.com/civetweb/civetweb/blob/master/include/civetweb.h)). +- [RELEASE_NOTES.md](https://github.com/civetweb/civetweb/blob/master/RELEASE_NOTES.md) - Release Notes +- [LICENSE.md](https://github.com/civetweb/civetweb/blob/master/LICENSE.md) - Copyright License + + +Overview +-------- + +CivetWeb keeps the balance between functionality and +simplicity by a carefully selected list of features: + +- Liberal, commercial-friendly, permissive, + [MIT license](http://en.wikipedia.org/wiki/MIT_License) +- Free from copy-left licenses, like GPL, because you should innovate without + restrictions. +- Forked from [Mongoose](https://code.google.com/p/mongoose/) in 2013, before + it changed the licence from MIT to commercial + GPL. A lot of enchancements + have been added since that time, see + [RELEASE_NOTES.md](https://github.com/civetweb/civetweb/blob/master/RELEASE_NOTES.md). +- Works on Windows, Mac, Linux, UNIX, iPhone, Android, Buildroot, and many + other platforms. +- Scripting and database support (Lua scipts, Lua Server Pages, CGI + SQLite + database, Server side javascript). + This provides a ready to go, powerful web development platform in a one + single-click executable with **no dependencies**. +- Support for CGI, HTTPS (SSL/TLS), SSI, HTTP digest (MD5) authorization, Websocket, + WEbDAV. +- Optional support for authentication using client side X.509 certificates. +- Resumed download, URL rewrite, file blacklist, IP-based ACL, Windows service. +- Download speed limit based on client subnet or URI pattern. +- Simple and clean embedding API. +- The source is in single file to make things easy. +- Embedding examples included. +- HTTP client capable of sending arbitrary HTTP/HTTPS requests. +- Websocket client functionality available (WS/WSS). + + +### Optionally included software + +[![Lua](https://raw.githubusercontent.com/civetweb/civetweb/master/resources/lua-logo.jpg "Lua Logo")](http://lua.org) + +[![Sqlite3](https://raw.githubusercontent.com/civetweb/civetweb/master/resources/sqlite3-logo.jpg "Sqlite3 Logo")](http://sqlite.org) + +[![LuaFileSystem](https://raw.githubusercontent.com/civetweb/civetweb/master/resources/luafilesystem-logo.jpg "LuaFileSystem Logo")](http://keplerproject.github.io/luafilesystem/) + +[![LuaSQLite3](https://raw.githubusercontent.com/civetweb/civetweb/master/resources/luasqlite-logo.jpg "LuaSQLite3 Logo")](http://lua.sqlite.org/index.cgi/index) + +[![LuaXML](https://raw.githubusercontent.com/civetweb/civetweb/master/resources/luaxml-logo.jpg "LuaXML Logo")](http://viremo.eludi.net/LuaXML/index.html) + +[![Duktape](https://raw.githubusercontent.com/civetweb/civetweb/master/resources/duktape-logo.png "Duktape Logo")](http://duktape.org) + + +Support +------- + +This project is very easy to install and use. +Please read the [documentation](https://github.com/civetweb/civetweb/blob/master/docs/) +and have a look at the [examples](https://github.com/civetweb/civetweb/blob/master/examples/). +More information may be found on the [mailing list](https://groups.google.com/d/forum/civetweb). + +Note: I do not take any liability or warranty for any linked contents. Visit these pages and try the community support suggestions at your own risk. + + +Contributions +--------------- + +Contributions are welcome provided all contributions carry the MIT license. + +DO NOT APPLY fixes copied from Mongoose to this project to prevent GPL tainting. +Since 2013, CivetWeb and Mongoose are developed independently. +By now the code base differs, so patches cannot be safely transfered in either direction. + +Some guidelines can be found in [docs/Contribution.md](https://github.com/civetweb/civetweb/blob/master/docs/Contribution.md). + + +### Authors + +CivetWeb is based on the Mongoose project. The original author of Mongoose was +Sergey Lyubka (Copyright (c) 2004-2013 Sergey Lyubka, MIT license). + +However, in August 16, 2013, the [license of Mongoose has been changed](https://groups.google.com/forum/#!topic/mongoose-users/aafbOnHonkI) +after writing and distributing the original code this project is based on. +The license change and CivetWeb used to be mentioned on the Mongoose +[Wikipedia](https://en.wikipedia.org/wiki/Mongoose_(web_server)) +page as well, but it's getting deleted (and added again) there every +now and then. + +CivetWeb has been forked from the last MIT version of Mongoose. +Since 2013, CivetWeb has seen many improvements from various authors +(Copyright (c) 2013-2017 the CivetWeb developers, MIT license). +A list of authors can be found in [CREDITS.md](https://github.com/civetweb/civetweb/blob/master/CREDITS.md). + +Using the CivetWeb project ensures the MIT licenses terms are applied and +GPL cannot be imposed on any of this code, as long as it is sourced from +here. This code will remain free with the MIT license protection. + diff --git a/src/civetweb/RELEASE_NOTES.md b/src/civetweb/RELEASE_NOTES.md new file mode 100644 index 000000000..dfae89f16 --- /dev/null +++ b/src/civetweb/RELEASE_NOTES.md @@ -0,0 +1,395 @@ +Release Notes v1.10 +=== +### Objectives: *OpenSSL 1.1 support, add server statistics and diagnostic data* + +Changes +------- + +- Add missing `mg_` or `MG_` to symbols in civetweb.h. Symbols without will be removed a future version. +- Add HTTPS server configuration example +- Lua Pages: mg.include should support absolute, relative and virtual path types +- Add API function for HTTP digest authentication +- Improved interface documentation +- Support parameters for Lua background scripts +- Use new connection queue implementation (previously ALTERNATIVE\_QUEUE) as default +- Add USE\_SERVER\_STATS define, so the server collects statistics data +- Convert system\_info text output and all other diagnostic strings to JSON format +- Add experimental function to query the connection status (may be dropped again) +- Add document on proposed future interface changes (for comments) +- Officially drop Symbian support +- Ignore leading blank lines in multipart messages (for Android upload service) +- Rewrite some functions, in particular request parsing +- CORS preflight directly in the server, with additional config options +- Solve some warnings from different static source code analysis tools +- Collect server status data +- Allow hostname in listening\_ports +- Make maximum request size configurable +- Allow multiple Sec-Websocket-Protocol +- Add configuration option to send additional headers +- Add configuration option for Strict-Transport-Security +- Mark "file in memory" feature is a candidate for deletion +- Improve examples +- Fix timeout error when sending larger files +- Add mg\_send\_chunk interface function +- Allow to separate server private key and certificate chain in two different files +- Support for multipart requests without quotes (for some C# clients) +- Initialize SSL in mg\_init\_library, so https client functions can be used when no server is running +- Allow "REPORT" HTTP method for REST calls to scripts +- Allow to compile civetweb.c wih a C++ compiler +- Lua: Remove internal length limits of encode/decode functions +- Allow sub-resources of index script files +- Add config parameter allow\_index\_script\_resource the aforementioned feature +- Remove deprecated "uri" member of the request from the interface +- Improve documentation +- Make auth domain check optional (configuration) +- Update unit test framework to check 0.11.0 (C89/C90 compilers still need a patched version) +- Limit depth of mg.include for Lua server pages +- Additional unit tests +- OpenSSL 1.1 support +- Update version number + + +Release Notes v1.9.1 +=== +### Objectives: *Bug fix* + +Changes +------- + +- Add "open website" button for pre-built Windows binaries +- Fix for connections closed prematurely +- Update to a new check unit test framework and remove patches required for previous version +- Update version number + + +Release Notes v1.9 +=== +### Objectives: *Read SSI client certificate information, improve windows usability, use non-blocking sockets, bug fixes* + +Changes +------- + +- Add library init/exit functions (call is now optional, but will be required in V1.10) +- Windows: Show system information from the tray icon +- Windows: Bring overlaid windows to top from the tray icon +- Add Lua background script, running independent from server state +- Move obsolete examples into separated directory +- Change name of CMake generated C++ library to civetweb-cpp +- Add option to set linger timeout +- Update Duktape and Lua (third-party code) +- Add continuous integration tests +- Add API documentation +- Limit recursions in .htpasswd files +- Fix SCRIPT_NAME for CGI directory index files (index.php) +- Use non-blocking sockets +- stdint.h is now required and no longer optional +- Rewrite connection close handling +- Rewrite mg_fopen/mg_stat +- Enhanced tray icon menu for Windows +- Add subprotocol management for websocket connections +- Partially rewrite timeout handling +- Add option keep_alive_timeout_ms +- Improve support for absolute URIs +- Allow some additional compiler checks (higher warning level) +- Add option for case sensitive file names for Windows +- Short notation for listening_ports option when using IPv4 and IPv6 ports +- Make usage of Linux sendfile configurable +- Optimize build matrix for Travis CI +- Retry failing TLS/HTTPS read/write operations +- Read client certificate information +- Do not tolerate URIs with invalid characters +- Fix mg_get_cookie to ignore substrings +- Fix memory leak in form handling +- Fix bug in timer logic (for Lua Websockets) +- Updated version number + +Release Notes v1.8 +=== +### Objectives: *CMake integration and continuous integration tests, Support client certificates, bug fixes* + +Changes +------- + +- Replace mg_upload by mg_handle_form_request +- CGI-scripts must receive EOF if all POST data is read +- Add API function to handle all kinds of HTML form data +- Do not allow short file names in Windows +- Callback when a new thread is initialized +- Support for short lived certificates +- Add NO_CACHING compile option +- Update Visual Studio project files to VS2015; rename directory VS2012 to VS +- Sec-Wesocket-Protocol must only return one protocol +- Mark some examples and tests as obsolete +- Remove no longer maintained test utils +- Add some default MIME types and the mg_send_mime_file API function. +- Client API using SSL certificates +- Send "Cache-Control" headers +- Add alternative to mg_upload +- Additional configuration options +- Fix memory leaks +- Add API function to check available features +- Add new interface to get listening ports +- Add websocket client interface and encode websocket data with a simple random number +- Support SSL client certificates +- Add configuration options for SSL client certificates +- Stand-alone server: Add command line option -I to display information about the system +- Redirect stderr of CGI process to error log +- Support absolute URI; split uri in mg_request_info to request_uri and local_uri +- Some source code refactoring, to improve maintainability +- Use recursive mutex for Linux +- Allow CGI environment to grow dynamically +- Support build for Lua 5.1 (including LuaJIT), Lua 5.2 and Lua 5.3 +- Improve examples and documentation +- Build option CIVETWEB_SERVE_NO_FILES to disable serving static files +- Add Server side JavaScript support (Duktape library) +- Created a "civetweb" organization at GitHub. +- Repository moved from https://github.com/bel2125/civetweb to https://github.com/civetweb/civetweb +- Improved continuous integration +- CMake support, continuous integration with Travis CI and Appveyor +- Adapt/port unit tests to CMake/Travis/Appveyor +- Bug fixes, including issues from static code analysis +- Add status badges to the GitHub project main page +- Updated version number + +Release Notes v1.7 +=== +### Objectives: *Examples, documentation, additional API functions, some functions rewritten, bug fixes and updates* + +Changes +------- + +- Format source with clang_format +- Use function 'sendfile' for Linux +- Fix for CRAMFS in Linux +- Fix for file modification times in Windows +- Use SO_EXCLUSIVEADDRUSE instead of SO_REUSEADDR for Windows +- Rewrite push/pull functions +- Allow to use Lua as shared objects (WITH_LUA_SHARED) +- Fixes for many warnings +- URI specific callbacks and different timeouts for websockets +- Add chunked transfer support +- Update LuaFileSystem +- Update Lua to 5.2.4 +- Fix build for MinGW-x64, TDM-GCC and clang +- Update SQLite to 3.8.10.2 +- Fix CGI variables SCRIPT_NAME and PATH_TRANSLATED +- Set TCP_USER_TIMEOUT to deal faster with broken connections +- Add a Lua form handling example +- Return more differentiated HTTP error codes +- Add log_access callback +- Rewrite and comment request handling function +- Specify in detail and document return values of callback functions +- Set names for all threads (unless NO_THREAD_NAME is defined) +- New API functions for TCP/HTTP clients +- Fix upload of huge files +- Allow multiple SSL instances within one application +- Improve API and user documentation +- Allow to choose between static and dynamic Lua library +- Improve unit test +- Use temporary file name for partially uploaded files +- Additional API functions exported to C++ +- Add a websocket client example +- Add a websocket client API +- Update websocket example +- Make content length available in request_info +- New API functions: access context, callback for create/delete, access user data +- Upgraded Lua from 5.2.2 to 5.2.3 and finally 5.2.4 +- Integrate LuaXML (for testing purposes) +- Fix compiler warnings +- Updated version number + +Release Notes v1.6 +=== +### Objectives: *Enhance Lua support, configuration dialog for windows, new examples, bug fixes and updates* + +Changes +------- + +- Add examples of Lua pages, scripts and websockets to the test directory (bel) +- Add dialog to change htpasswd files for the Windows standalone server (bel) +- Fix compiler warnings and warnings from static code analysis (Danny Al-Gaaf, jmc-, Thomas, bel, ...) +- Add new unit tests (bel) +- Support includes in htpasswd files (bel) +- Add a basic option check for the standalone executable (bel) +- Support user defined error pages (bel) +- Method to get POST request parameters via C++ interface (bel) +- Re-Add unit tests for Linux and Windows (jmc-, bel) +- Allow to specify title and tray icon for the Windows standalone server (bel) +- Fix minor memory leaks (bel) +- Redirect all memory allocation/deallocation through mg functions which may be overwritten (bel) +- Support Cross-Origin Resource Sharing (CORS) for static files and scripts (bel) +- Win32: Replace dll.def file by export macros in civetweb.h (CSTAJ) +- Base64 encode and decode functions for Lua (bel) +- Support pre-loaded files for the Lua environment (bel) +- Server should check the nonce for http digest access authentication (bel) +- Hide read-only flag in file dialogs opened by the Edit Settings dialog for the Windows executable (bel) +- Add all functions to dll.def, that are in the header (bel) +- Added Lua extensions: send_file, get_var, get_mime_type, get_cookie, url_decode, url_encode (bel) +- mg_set_request_handler() mod to use pattern (bel, Patch from Toni Wilk) +- Solved, tested and documented SSL support for Windows (bel) +- Fixed: select for Linux needs the nfds parameter set correctly (bel) +- Add methods for returning the ports civetweb is listening on (keithel) +- Fixes for Lua Server Pages, as described within the google groups thread. (bel) +- Added support for plain Lua Scripts, and an example script. (bel) +- A completely new, and more illustrative websocket example for C. (bel) +- Websocket for Lua (bel) +- An optional websocket_root directory, including URL rewriting (bel) +- Update of SQLite3 to 3.8.1. (bel) +- Add "date" header field to replies, according to the requirements of RFC 2616 (the HTTP standard), Section 14.18 (bel) +- Fix websocket long pull (celeron55) +- Updated API documentation (Alex Kozlov) +- Fixed Posix locking functions for Windows (bel2125) +- Updated version number + +Release Notes v1.5 +=== +### Objectives: *Bug fixes and updates, repository restoration* + +Changes +------- + +- Corrected bad mask flag/opcode passing to websocket callback (William Greathouse) +- Moved CEVITWEB_VERSION define into civetweb.h +- Added new simple zip deployment build for Windows. +- Removed windows install package build. +- Fixes page violation in mod_lua.inl (apkbox) +- Use C style comments to enable compiling most of civetweb with -ansi. (F-Secure Corporation) +- Allow directories with non ASCII characters in Windows in UTF-8 encoded (bel2125) +- Added Lua File System support (bel2125) +- Added mongoose history back in repository thanks to (Paul Sokolovsky) +- Fixed keep alive (bel2125) +- Updated of MIME types (bel2125) +- Updated lsqlite (bel2125) +- Fixed master thread priority (bel2125) +- Fixed IPV6 defines under Windowe (grenclave) +- Fixed potential dead lock in connection_close() (Morgan McGuire) +- Added WebSocket example using asynchronous server messages (William Greathouse) +- Fixed the getcwd() warning (William Greathouse) +- Implemented the connection_close() callback (William Greathouse) +- Fixed support URL's in civetweb.c (Daniel Oaks) +- Allow port number to be zero to use a random free port (F-Secure Corporation) +- Wait for threads to finish when stopping for a clean shutdown (F-Secure Corporation) +- More static analysis fixes against Coverity tool (F-Secure Corporation) +- Travis automated build testing support added (Daniel Oaks) +- Updated version numbers. +- Added contributor credits file. + +Release Notes v1.4 +=== +### Objectives: *New URI handler interface, feature enhancements, C++ extensions* +The main idea behind this release is to bring about API consistency. All changes +are backward compatible and have been kept to a minimum. + +Changes +------- + +- Added mg_set_request_handler() which provides a URI mapping for callbacks. + This is a new alternative to overriding callbacks.begin_request. +- Externalized mg_url_encode() +- Externalized mg_strncasecmp() for utiliy +- Added CivetServer::getParam methods +- Added CivetServer::urlDecode methods +- Added CivetServer::urlEncode methods +- Dealt with compiler warnings and some static analysis hits. +- Added mg_get_var2() to parse repeated query variables +- Externalized logging function cry() as mg_cry() +- Added CivetServer::getCookie method (Hariprasad Kamath) +- Added CivetServer::getHeader method (Hariprasad Kamath) +- Added new basic C embedding example +- Conformed source files to UNIX line endings for consistency. +- Unified the coding style to improve reability. + +Release Notes v1.3 +=== +### Objectives: *Buildroot Integration* + +Changes +------- + +- Made option to put initial HTMLDIR in a different place +- Validated build without SQLITE3 large file support +- Updated documentation +- Updated Buildroot config example + +Release Notes v1.2 +=== +### Objectives: *Installation Improvements, buildroot, cross compile support* +The objective of this release is to make installation seamless. + +Changes +------- + +- Create an installation guide +- Created both 32 and 64 bit windows installations +- Added install for windows distribution +- Added 64 bit build profiles for VS 2012. +- Created a buildroot patch +- Updated makefile to better support buildroot +- Made doc root and ports configurable during the make install. +- Updated Linux Install +- Updated OS X Package +- Improved install scheme with welcome web page + +Known Issues +----- + +- The prebuilt Window's version requires [Visual C++ Redistributable for Visual Studio 2012](http://www.microsoft.com/en-us/download/details.aspx?id=30679) + +Release Notes v1.1 +=== +### Objectives: *Build, Documentation, License Improvements* +The objective of this release is to establish a maintable code base, ensure MIT license rights and improve usability and documentation. + +Changes +------- + +- Reorangized build directories to make them more intuitive +- Added new build rules for lib and slib with option to include C++ class +- Upgraded Lua from 5.2.1 to 5.2.2 +- Added fallback configuration file path for Linux systems. + + Good for having a system wide default configuration /usr/local/etc/civetweb.conf +- Added new C++ abstraction class CivetServer +- Added thread safety for and fixed websocket defects (Morgan McGuire) +- Created PKGBUILD to use Arch distribution (Daniel Oaks) +- Created new documentation on Embeddeding, Building and yaSSL (see docs/). +- Updated License file to include all licenses. +- Replaced MD5 implementation due to questionable license. + + This requires new source file md5.inl +- Changed UNIX/OSX build to conform to common practices. + + Supports build, install and clean rules. + + Supports cross compiling + + Features can be chosen in make options +- Moved Cocoa/OSX build and packaging to a separate file. + + This actually a second build variant for OSX. + + Removed yaSSL from the OSX build, not needed. +- Added new Visual Studio projects for Windows builds. + + Removed Windows support from Makefiles + + Provided additional, examples with Lua, and another with yaSSL. +- Changed Zombie Reaping policy to not ignore SIGCHLD. + + The previous method caused trouble in applciations that spawn children. + +Known Issues +----- + +- Build support for VS6 and some other has been deprecated. + + This does not impact embedded programs, just the stand-alone build. + + The old Makefile was renamed to Makefile.deprecated. + + This is partcially do to lack fo testing. + + Need to find out what is actually in demand. +- Build changes may impact current users. + + As with any change of this type, changes may impact some users. + +Release Notes v1.0 +=== + +### Objectives: *MIT License Preservation, Rebranding* +The objective of this release is to establish a version of the Mongoose software distribution that still retains the MIT license. + +Changes +------- + +- Renamed Mongoose to Civetweb in the code and documentation. +- Replaced copyrighted images with new images +- Created a new code respository at https://github.com/civetweb/civetweb +- Created a distribution site at https://sourceforge.net/projects/civetweb/ +- Basic build testing diff --git a/src/civetweb/VisualStudio/buildRelease.pl b/src/civetweb/VisualStudio/buildRelease.pl new file mode 100644 index 000000000..df8db0924 --- /dev/null +++ b/src/civetweb/VisualStudio/buildRelease.pl @@ -0,0 +1,71 @@ +#!/usr/bin/perl +# +# Copyright (c) 2013 No Face Press, LLC +# License http://opensource.org/licenses/mit-license.php MIT License +# + +# This script builds and packages a Windows release. +# It requires ActiveState Perl to use and is intended +# to be run from the its directory under the +# VS Developer Command Prompt. + +# Create a Zip file +use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); +my $zip = Archive::Zip->new(); + +my $src = ".."; + +sub getCivetwebVersion { + print "Fetching CivetWeb version...\n"; + open HEADER, "${src}/include/civetweb.h"; + while (
) { + if (m/define\s+CIVETWEB_VERSION\s+"(.+)"/) { + close HEADER; + return $1; + } + } + close HEADER; + return "UNKNOWN_VERSION"; +} + +my $CIVETWEB_VERSION = getCivetwebVersion(); +my $basename = "civetweb-$CIVETWEB_VERSION"; +my $dir = "${basename}"; + +sub build32() { + print "\nBuilding Win32 Release version...\n"; + system("msbuild /p:Configuration=Release /p:Platform=Win32 civetweb.sln"); +} + +sub build64() { + print "\nBuilding x64 Release version...\n"; + system("msbuild /p:Configuration=Release /p:Platform=x64 civetweb.sln"); +} + +sub writeArchive() { + my $archive = "${basename}-win.zip"; + print "Creating archive $archive ...\n"; + + $zip->addDirectory("${dir}/"); + + $zip->addFile( "${src}/LICENSE.md", "${dir}/LICENSE.md" ); + $zip->addFile( "${src}/README.md", "${dir}/README.md" ); + $zip->addFile( "${src}/resources/systray.ico", "${dir}/systray.ico" ); + $zip->addFile( "${src}/resources/civetweb_64x64.png", + "${dir}/civetweb_64x64.png" ); + $zip->addFile( "${src}/resources/itworks.html", "${dir}/index.html" ); + $zip->addFile( "${src}/VS2012/Release/Win32/civetweb_lua.exe", + "${dir}/civetweb32.exe" ); + $zip->addFile( "${src}/VS2012/Release/x64/civetweb_lua.exe", + "${dir}/civetweb64.exe" ); + + unless ( $zip->writeToFileNamed($archive) == AZ_OK ) { + die 'write error'; + } + +} + +build32(); +build64(); +writeArchive(); +exit 0; diff --git a/src/civetweb/VisualStudio/civetweb.sln b/src/civetweb/VisualStudio/civetweb.sln new file mode 100644 index 000000000..0e8672b99 --- /dev/null +++ b/src/civetweb/VisualStudio/civetweb.sln @@ -0,0 +1,75 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "civetweb_lua", "civetweb_lua\civetweb_lua.vcxproj", "{9BE9C008-E851-42B1-A034-BD4630AE4CD6}" + ProjectSection(ProjectDependencies) = postProject + {0A11689C-DB6A-4BF6-97B2-AD32DB863FBD} = {0A11689C-DB6A-4BF6-97B2-AD32DB863FBD} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lua_lib", "lua_lib\lua_lib.vcxproj", "{8F5E5D77-D269-4665-9E27-1045DA6CF0D8}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ex_embedded_c", "ex_embedded_c\ex_embedded_c.vcxproj", "{882EC43C-2EEE-434B-A711-C844108D29C6}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "unit_test", "unit_test\unit_test.vcxproj", "{1AC4A7A6-0100-4287-97F4-B95807BE5607}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "duktape_lib", "duktape_lib\duktape_lib.vcxproj", "{0A11689C-DB6A-4BF6-97B2-AD32DB863FBD}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ex_embed_cpp", "ex_embed_cpp\ex_embed_cpp.vcxproj", "{4308C5EE-45E4-45D8-9D73-6C4E2587AD78}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9BE9C008-E851-42B1-A034-BD4630AE4CD6}.Debug|Win32.ActiveCfg = Debug|Win32 + {9BE9C008-E851-42B1-A034-BD4630AE4CD6}.Debug|Win32.Build.0 = Debug|Win32 + {9BE9C008-E851-42B1-A034-BD4630AE4CD6}.Debug|x64.ActiveCfg = Debug|x64 + {9BE9C008-E851-42B1-A034-BD4630AE4CD6}.Debug|x64.Build.0 = Debug|x64 + {9BE9C008-E851-42B1-A034-BD4630AE4CD6}.Release|Win32.ActiveCfg = Release|Win32 + {9BE9C008-E851-42B1-A034-BD4630AE4CD6}.Release|Win32.Build.0 = Release|Win32 + {9BE9C008-E851-42B1-A034-BD4630AE4CD6}.Release|x64.ActiveCfg = Release|x64 + {9BE9C008-E851-42B1-A034-BD4630AE4CD6}.Release|x64.Build.0 = Release|x64 + {8F5E5D77-D269-4665-9E27-1045DA6CF0D8}.Debug|Win32.ActiveCfg = Debug|Win32 + {8F5E5D77-D269-4665-9E27-1045DA6CF0D8}.Debug|Win32.Build.0 = Debug|Win32 + {8F5E5D77-D269-4665-9E27-1045DA6CF0D8}.Debug|x64.ActiveCfg = Debug|x64 + {8F5E5D77-D269-4665-9E27-1045DA6CF0D8}.Debug|x64.Build.0 = Debug|x64 + {8F5E5D77-D269-4665-9E27-1045DA6CF0D8}.Release|Win32.ActiveCfg = Release|Win32 + {8F5E5D77-D269-4665-9E27-1045DA6CF0D8}.Release|Win32.Build.0 = Release|Win32 + {8F5E5D77-D269-4665-9E27-1045DA6CF0D8}.Release|x64.ActiveCfg = Release|x64 + {8F5E5D77-D269-4665-9E27-1045DA6CF0D8}.Release|x64.Build.0 = Release|x64 + {882EC43C-2EEE-434B-A711-C844108D29C6}.Debug|Win32.ActiveCfg = Debug|Win32 + {882EC43C-2EEE-434B-A711-C844108D29C6}.Debug|Win32.Build.0 = Debug|Win32 + {882EC43C-2EEE-434B-A711-C844108D29C6}.Debug|x64.ActiveCfg = Debug|x64 + {882EC43C-2EEE-434B-A711-C844108D29C6}.Release|Win32.ActiveCfg = Release|Win32 + {882EC43C-2EEE-434B-A711-C844108D29C6}.Release|Win32.Build.0 = Release|Win32 + {882EC43C-2EEE-434B-A711-C844108D29C6}.Release|x64.ActiveCfg = Release|x64 + {1AC4A7A6-0100-4287-97F4-B95807BE5607}.Debug|Win32.ActiveCfg = Debug|Win32 + {1AC4A7A6-0100-4287-97F4-B95807BE5607}.Debug|Win32.Build.0 = Debug|Win32 + {1AC4A7A6-0100-4287-97F4-B95807BE5607}.Debug|x64.ActiveCfg = Debug|Win32 + {1AC4A7A6-0100-4287-97F4-B95807BE5607}.Release|Win32.ActiveCfg = Release|Win32 + {1AC4A7A6-0100-4287-97F4-B95807BE5607}.Release|Win32.Build.0 = Release|Win32 + {1AC4A7A6-0100-4287-97F4-B95807BE5607}.Release|x64.ActiveCfg = Release|Win32 + {0A11689C-DB6A-4BF6-97B2-AD32DB863FBD}.Debug|Win32.ActiveCfg = Debug|Win32 + {0A11689C-DB6A-4BF6-97B2-AD32DB863FBD}.Debug|Win32.Build.0 = Debug|Win32 + {0A11689C-DB6A-4BF6-97B2-AD32DB863FBD}.Debug|x64.ActiveCfg = Debug|x64 + {0A11689C-DB6A-4BF6-97B2-AD32DB863FBD}.Debug|x64.Build.0 = Debug|x64 + {0A11689C-DB6A-4BF6-97B2-AD32DB863FBD}.Release|Win32.ActiveCfg = Release|Win32 + {0A11689C-DB6A-4BF6-97B2-AD32DB863FBD}.Release|Win32.Build.0 = Release|Win32 + {0A11689C-DB6A-4BF6-97B2-AD32DB863FBD}.Release|x64.ActiveCfg = Release|x64 + {0A11689C-DB6A-4BF6-97B2-AD32DB863FBD}.Release|x64.Build.0 = Release|x64 + {4308C5EE-45E4-45D8-9D73-6C4E2587AD78}.Debug|Win32.ActiveCfg = Debug|Win32 + {4308C5EE-45E4-45D8-9D73-6C4E2587AD78}.Debug|Win32.Build.0 = Debug|Win32 + {4308C5EE-45E4-45D8-9D73-6C4E2587AD78}.Debug|x64.ActiveCfg = Debug|x64 + {4308C5EE-45E4-45D8-9D73-6C4E2587AD78}.Release|Win32.ActiveCfg = Release|Win32 + {4308C5EE-45E4-45D8-9D73-6C4E2587AD78}.Release|Win32.Build.0 = Release|Win32 + {4308C5EE-45E4-45D8-9D73-6C4E2587AD78}.Release|x64.ActiveCfg = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/src/civetweb/VisualStudio/civetweb/civetweb.vcxproj b/src/civetweb/VisualStudio/civetweb/civetweb.vcxproj new file mode 100644 index 000000000..8dfcea711 --- /dev/null +++ b/src/civetweb/VisualStudio/civetweb/civetweb.vcxproj @@ -0,0 +1,165 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {E23B0040-F485-43D6-AA24-B34F9FED8484} + Win32Proj + civetweb + + + + Application + true + v100 + MultiByte + + + Application + true + v110 + MultiByte + + + Application + false + v100 + true + MultiByte + + + Application + false + v110 + true + MultiByte + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + false + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + false + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Windows + true + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Windows + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/civetweb/civetweb.vcxproj.filters b/src/civetweb/VisualStudio/civetweb/civetweb.vcxproj.filters new file mode 100644 index 000000000..253988b33 --- /dev/null +++ b/src/civetweb/VisualStudio/civetweb/civetweb.vcxproj.filters @@ -0,0 +1,40 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Resource Files + + + + + Resource Files + + + + + Header Files + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/civetweb_lua/civetweb_lua.vcxproj b/src/civetweb/VisualStudio/civetweb_lua/civetweb_lua.vcxproj new file mode 100644 index 000000000..45f8bbee9 --- /dev/null +++ b/src/civetweb/VisualStudio/civetweb_lua/civetweb_lua.vcxproj @@ -0,0 +1,216 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {9BE9C008-E851-42B1-A034-BD4630AE4CD6} + Win32Proj + civetweb_lua + 8.1 + + + + Application + true + MultiByte + v140_xp + + + Application + true + v140 + MultiByte + + + Application + true + v110 + MultiByte + + + Application + false + true + MultiByte + v140_xp + + + Application + false + v140 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + false + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + false + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + + + + Level3 + Disabled + USE_SERVER_STATS;USE_DUKTAPE;USE_IPV6;LUA_COMPAT_ALL;USE_LUA;USE_LUA_SQLITE3;USE_LUA_FILE_SYSTEM;USE_WEBSOCKET;WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;$(ProjectDir)..\..\src\third_party;$(ProjectDir)..\..\src\third_party\lua-5.2.4\src;$(ProjectDir)..\..\src\third_party\duktape-1.5.2\src;%(AdditionalIncludeDirectories) + + + Windows + true + + + + + + + Level3 + Disabled + USE_SERVER_STATS;USE_DUKTAPE;USE_IPV6;LUA_COMPAT_ALL;USE_LUA;USE_LUA_SQLITE3;USE_LUA_FILE_SYSTEM;USE_WEBSOCKET;WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;$(ProjectDir)..\..\src\third_party;$(ProjectDir)..\..\src\third_party\lua-5.2.4\src;$(ProjectDir)..\..\src\third_party\duktape-1.5.2\src;%(AdditionalIncludeDirectories) + + + Windows + true + + + + + + + Level3 + Disabled + LUA_COMPAT_ALL;USE_LUA;USE_LUA_SQLITE3;USE_LUA_FILE_SYSTEM;USE_WEBSOCKET;WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;$(ProjectDir)..\..\src\third_party\lua-5.2.4\src;%(AdditionalIncludeDirectories) + + + Windows + true + + + + + Level3 + + + MaxSpeed + true + true + USE_SERVER_STATS;USE_DUKTAPE;USE_IPV6;LUA_COMPAT_ALL;USE_LUA;USE_LUA_SQLITE3;USE_LUA_FILE_SYSTEM;USE_WEBSOCKET;WIN32;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;$(ProjectDir)..\..\src\third_party;$(ProjectDir)..\..\src\third_party\lua-5.2.4\src;$(ProjectDir)..\..\src\third_party\duktape-1.5.2\src;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + USE_SERVER_STATS;USE_DUKTAPE;USE_IPV6;LUA_COMPAT_ALL;USE_LUA;USE_LUA_SQLITE3;USE_LUA_FILE_SYSTEM;USE_WEBSOCKET;WIN32;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;$(ProjectDir)..\..\src\third_party;$(ProjectDir)..\..\src\third_party\lua-5.2.4\src;$(ProjectDir)..\..\src\third_party\duktape-1.5.2\src;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + {8f5e5d77-d269-4665-9e27-1045da6cf0d8} + + + {0A11689C-DB6A-4BF6-97B2-AD32DB863FBD} + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/civetweb_lua/civetweb_lua.vcxproj.filters b/src/civetweb/VisualStudio/civetweb_lua/civetweb_lua.vcxproj.filters new file mode 100644 index 000000000..caf951c48 --- /dev/null +++ b/src/civetweb/VisualStudio/civetweb_lua/civetweb_lua.vcxproj.filters @@ -0,0 +1,78 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + {1ef3413b-2315-48f2-ad22-57af6b4f7aca} + + + + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + + + Resource Files + + + + + Resource Files + + + + + inl files + + + inl files + + + inl files + + + inl files + + + inl files + + + inl files + + + inl files + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/civetweb_yassl/civetweb_yassl.sln b/src/civetweb/VisualStudio/civetweb_yassl/civetweb_yassl.sln new file mode 100644 index 000000000..49e36ce50 --- /dev/null +++ b/src/civetweb/VisualStudio/civetweb_yassl/civetweb_yassl.sln @@ -0,0 +1,36 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "civetweb_yassl", "civetweb_yassl\civetweb_yassl.vcxproj", "{F02517CC-F896-41A2-86E4-509E55C70059}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "yassl_lib", "yassl_lib\yassl_lib.vcxproj", "{8C0C878B-BBD6-4241-BCA6-61753FFCC7F1}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F02517CC-F896-41A2-86E4-509E55C70059}.Debug|Win32.ActiveCfg = Debug|Win32 + {F02517CC-F896-41A2-86E4-509E55C70059}.Debug|Win32.Build.0 = Debug|Win32 + {F02517CC-F896-41A2-86E4-509E55C70059}.Debug|x64.ActiveCfg = Debug|x64 + {F02517CC-F896-41A2-86E4-509E55C70059}.Debug|x64.Build.0 = Debug|x64 + {F02517CC-F896-41A2-86E4-509E55C70059}.Release|Win32.ActiveCfg = Release|Win32 + {F02517CC-F896-41A2-86E4-509E55C70059}.Release|Win32.Build.0 = Release|Win32 + {F02517CC-F896-41A2-86E4-509E55C70059}.Release|x64.ActiveCfg = Release|x64 + {F02517CC-F896-41A2-86E4-509E55C70059}.Release|x64.Build.0 = Release|x64 + {8C0C878B-BBD6-4241-BCA6-61753FFCC7F1}.Debug|Win32.ActiveCfg = Debug|Win32 + {8C0C878B-BBD6-4241-BCA6-61753FFCC7F1}.Debug|Win32.Build.0 = Debug|Win32 + {8C0C878B-BBD6-4241-BCA6-61753FFCC7F1}.Debug|x64.ActiveCfg = Debug|x64 + {8C0C878B-BBD6-4241-BCA6-61753FFCC7F1}.Debug|x64.Build.0 = Debug|x64 + {8C0C878B-BBD6-4241-BCA6-61753FFCC7F1}.Release|Win32.ActiveCfg = Release|Win32 + {8C0C878B-BBD6-4241-BCA6-61753FFCC7F1}.Release|Win32.Build.0 = Release|Win32 + {8C0C878B-BBD6-4241-BCA6-61753FFCC7F1}.Release|x64.ActiveCfg = Release|x64 + {8C0C878B-BBD6-4241-BCA6-61753FFCC7F1}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/src/civetweb/VisualStudio/civetweb_yassl/civetweb_yassl/civetweb_yassl.vcxproj b/src/civetweb/VisualStudio/civetweb_yassl/civetweb_yassl/civetweb_yassl.vcxproj new file mode 100644 index 000000000..b9a048ef0 --- /dev/null +++ b/src/civetweb/VisualStudio/civetweb_yassl/civetweb_yassl/civetweb_yassl.vcxproj @@ -0,0 +1,170 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {F02517CC-F896-41A2-86E4-509E55C70059} + Win32Proj + civetweb_yassl + + + + Application + true + v110 + MultiByte + + + Application + true + v110 + MultiByte + + + Application + false + v110 + true + MultiByte + + + Application + false + v110 + true + MultiByte + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)\$(Configuration)\$(Platform) + + + true + $(SolutionDir)\$(Configuration)\$(Platform) + + + false + $(SolutionDir)\$(Configuration)\$(Platform) + + + false + $(SolutionDir)\$(Configuration)\$(Platform) + + + + + + Level3 + Disabled + USE_YASSL;NO_SSL_DL;WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\..\include;$(ProjectDir)..\..\..\..\cyassl-2.7.0\;$(ProjectDir)..\..\..\..\cyassl-2.7.0\cyassl\ + + + Windows + true + + + + + + + Level3 + Disabled + USE_YASSL;NO_SSL_DL;WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\..\include;$(ProjectDir)..\..\..\..\cyassl-2.7.0\;$(ProjectDir)..\..\..\..\cyassl-2.7.0\cyassl\ + + + Windows + true + + + + + Level3 + + + MaxSpeed + true + true + USE_YASSL;NO_SSL_DL;WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\..\include;$(ProjectDir)..\..\..\..\cyassl-2.7.0\;$(ProjectDir)..\..\..\..\cyassl-2.7.0\cyassl\ + + + Windows + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + USE_YASSL;NO_SSL_DL;WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\..\include;$(ProjectDir)..\..\..\..\cyassl-2.7.0\;$(ProjectDir)..\..\..\..\cyassl-2.7.0\cyassl\ + + + Windows + true + true + true + + + + + + + + + + + + + + + + + + {8c0c878b-bbd6-4241-bca6-61753ffcc7f1} + + + + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/civetweb_yassl/civetweb_yassl/civetweb_yassl.vcxproj.filters b/src/civetweb/VisualStudio/civetweb_yassl/civetweb_yassl/civetweb_yassl.vcxproj.filters new file mode 100644 index 000000000..1c5ee4092 --- /dev/null +++ b/src/civetweb/VisualStudio/civetweb_yassl/civetweb_yassl/civetweb_yassl.vcxproj.filters @@ -0,0 +1,40 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + + + Source Files + + + Source Files + + + + + Resource Files + + + + + Resource Files + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/civetweb_yassl/yassl_lib/yassl_lib.vcxproj b/src/civetweb/VisualStudio/civetweb_yassl/yassl_lib/yassl_lib.vcxproj new file mode 100644 index 000000000..033b82dfa --- /dev/null +++ b/src/civetweb/VisualStudio/civetweb_yassl/yassl_lib/yassl_lib.vcxproj @@ -0,0 +1,185 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {8C0C878B-BBD6-4241-BCA6-61753FFCC7F1} + Win32Proj + yassl_lib + + + + StaticLibrary + true + v110 + MultiByte + + + StaticLibrary + true + v110 + MultiByte + + + StaticLibrary + false + v110 + true + MultiByte + + + StaticLibrary + false + v110 + true + MultiByte + + + + + + + + + + + + + + + + + + + $(SolutionDir)\$(Configuration)\$(Platform) + + + $(SolutionDir)\$(Configuration)\$(Platform) + + + $(SolutionDir)\$(Configuration)\$(Platform) + + + $(SolutionDir)\$(Configuration)\$(Platform) + + + + + + Level3 + Disabled + OPENSSL_EXTRA;HAVE_ERRNO_H;HAVE_GETHOSTBYNAME;HAVE_INET_NTOA;HAVE_LIMITS_H;HAVE_MEMSET;HAVE_SOCKET;HAVE_STDDEF_H;HAVE_STDLIB_H;HAVE_STRING_H;HAVE_SYS_STAT_H;HAVE_SYS_TYPES_H;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + $(ProjectDir)..\..\..\..\cyassl-2.7.0\;$(ProjectDir)..\..\..\..\cyassl-2.7.0\cyassl\ + + + Windows + true + + + + + + + Level3 + Disabled + OPENSSL_EXTRA;HAVE_ERRNO_H;HAVE_GETHOSTBYNAME;HAVE_INET_NTOA;HAVE_LIMITS_H;HAVE_MEMSET;HAVE_SOCKET;HAVE_STDDEF_H;HAVE_STDLIB_H;HAVE_STRING_H;HAVE_SYS_STAT_H;HAVE_SYS_TYPES_H;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + $(ProjectDir)..\..\..\..\cyassl-2.7.0\;$(ProjectDir)..\..\..\..\cyassl-2.7.0\cyassl\ + + + Windows + true + + + + + Level3 + + + MaxSpeed + true + true + OPENSSL_EXTRA;HAVE_ERRNO_H;HAVE_GETHOSTBYNAME;HAVE_INET_NTOA;HAVE_LIMITS_H;HAVE_MEMSET;HAVE_SOCKET;HAVE_STDDEF_H;HAVE_STDLIB_H;HAVE_STRING_H;HAVE_SYS_STAT_H;HAVE_SYS_TYPES_H;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + $(ProjectDir)..\..\..\..\cyassl-2.7.0\;$(ProjectDir)..\..\..\..\cyassl-2.7.0\cyassl\ + + + Windows + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + OPENSSL_EXTRA;HAVE_ERRNO_H;HAVE_GETHOSTBYNAME;HAVE_INET_NTOA;HAVE_LIMITS_H;HAVE_MEMSET;HAVE_SOCKET;HAVE_STDDEF_H;HAVE_STDLIB_H;HAVE_STRING_H;HAVE_SYS_STAT_H;HAVE_SYS_TYPES_H;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + $(ProjectDir)..\..\..\..\cyassl-2.7.0\;$(ProjectDir)..\..\..\..\cyassl-2.7.0\cyassl\ + + + Windows + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/civetweb_yassl/yassl_lib/yassl_lib.vcxproj.filters b/src/civetweb/VisualStudio/civetweb_yassl/yassl_lib/yassl_lib.vcxproj.filters new file mode 100644 index 000000000..566b892b4 --- /dev/null +++ b/src/civetweb/VisualStudio/civetweb_yassl/yassl_lib/yassl_lib.vcxproj.filters @@ -0,0 +1,124 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/duktape_lib/duktape_lib.vcxproj b/src/civetweb/VisualStudio/duktape_lib/duktape_lib.vcxproj new file mode 100644 index 000000000..12fa214d2 --- /dev/null +++ b/src/civetweb/VisualStudio/duktape_lib/duktape_lib.vcxproj @@ -0,0 +1,156 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {0A11689C-DB6A-4BF6-97B2-AD32DB863FBD} + Win32Proj + duktape_lib + 8.1 + + + + StaticLibrary + true + MultiByte + v140_xp + + + StaticLibrary + true + v140 + MultiByte + + + StaticLibrary + false + true + MultiByte + v140_xp + + + StaticLibrary + false + v140 + true + MultiByte + + + + + + + + + + + + + + + + + + + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + + + + TurnOffAllWarnings + Disabled + duktape_COMPAT_ALL;THREADSAFE=1;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_FTS3_PARENTHESIS;WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\src\third_party\lua-5.2.4\src;%(AdditionalIncludeDirectories) + + + Windows + true + + + + + + + Level3 + Disabled + duktape_COMPAT_ALL;THREADSAFE=1;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_FTS3_PARENTHESIS;WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\src\third_party\lua-5.2.4\src;%(AdditionalIncludeDirectories) + + + Windows + true + + + + + Level3 + + + MaxSpeed + true + true + duktape_COMPAT_ALL;THREADSAFE=1;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_FTS3_PARENTHESIS;WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\src\third_party\lua-5.2.4\src;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + duktape_COMPAT_ALL;THREADSAFE=1;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_FTS3_PARENTHESIS;WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\src\third_party\lua-5.2.4\src;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/duktape_lib/duktape_lib.vcxproj.filters b/src/civetweb/VisualStudio/duktape_lib/duktape_lib.vcxproj.filters new file mode 100644 index 000000000..1aab8c811 --- /dev/null +++ b/src/civetweb/VisualStudio/duktape_lib/duktape_lib.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + Header Files + + + + + Source Files + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/ex_embed_cpp/ex_embed_cpp.vcxproj b/src/civetweb/VisualStudio/ex_embed_cpp/ex_embed_cpp.vcxproj new file mode 100644 index 000000000..a195fb698 --- /dev/null +++ b/src/civetweb/VisualStudio/ex_embed_cpp/ex_embed_cpp.vcxproj @@ -0,0 +1,162 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {4308C5EE-45E4-45D8-9D73-6C4E2587AD78} + Win32Proj + ex_embed_cpp + 8.1 + + + + Application + true + v140_xp + MultiByte + + + Application + true + v140 + MultiByte + + + Application + false + v140_xp + true + MultiByte + + + Application + false + v140 + true + MultiByte + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + false + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + false + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/ex_embed_cpp/ex_embed_cpp.vcxproj.filters b/src/civetweb/VisualStudio/ex_embed_cpp/ex_embed_cpp.vcxproj.filters new file mode 100644 index 000000000..68671e67a --- /dev/null +++ b/src/civetweb/VisualStudio/ex_embed_cpp/ex_embed_cpp.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + Source Files + + + diff --git a/src/civetweb/VisualStudio/ex_embedded_c/ex_embedded_c.vcxproj b/src/civetweb/VisualStudio/ex_embedded_c/ex_embedded_c.vcxproj new file mode 100644 index 000000000..c3a429f14 --- /dev/null +++ b/src/civetweb/VisualStudio/ex_embedded_c/ex_embedded_c.vcxproj @@ -0,0 +1,161 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + + + + + + + + {882EC43C-2EEE-434B-A711-C844108D29C6} + Win32Proj + ex_embedded_c + 8.1 + + + + Application + true + v140_xp + MultiByte + + + Application + true + v140 + MultiByte + + + Application + false + v140_xp + true + MultiByte + + + Application + false + v140 + true + MultiByte + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + false + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + false + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + + + + Level3 + Disabled + USE_IPV6;USE_WEBSOCKET;WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + + + Level3 + Disabled + USE_IPV6;USE_WEBSOCKET;WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + USE_IPV6;USE_WEBSOCKET;WIN32;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + USE_IPV6;USE_WEBSOCKET;WIN32;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/ex_embedded_c/ex_embedded_c.vcxproj.filters b/src/civetweb/VisualStudio/ex_embedded_c/ex_embedded_c.vcxproj.filters new file mode 100644 index 000000000..cf2e4d052 --- /dev/null +++ b/src/civetweb/VisualStudio/ex_embedded_c/ex_embedded_c.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/ex_websocket/ex_websocket.vcxproj b/src/civetweb/VisualStudio/ex_websocket/ex_websocket.vcxproj new file mode 100644 index 000000000..8076f89b9 --- /dev/null +++ b/src/civetweb/VisualStudio/ex_websocket/ex_websocket.vcxproj @@ -0,0 +1,162 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {58B93E94-7766-435E-93AE-42A2FB5D99B1} + Win32Proj + ex_websocket + 8.1 + + + + Application + true + MultiByte + v140 + + + Application + true + v140 + MultiByte + + + Application + false + true + MultiByte + v140 + + + Application + false + v140 + true + MultiByte + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + false + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + false + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + + + + Level3 + Disabled + USE_WEBSOCKET;WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + + + + + + + Level3 + Disabled + USE_WEBSOCKET;WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + USE_WEBSOCKET;WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + USE_WEBSOCKET;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/ex_websocket/ex_websocket.vcxproj.filters b/src/civetweb/VisualStudio/ex_websocket/ex_websocket.vcxproj.filters new file mode 100644 index 000000000..9adb8fb6c --- /dev/null +++ b/src/civetweb/VisualStudio/ex_websocket/ex_websocket.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/ex_websocket_client/ex_websocket_client.vcxproj b/src/civetweb/VisualStudio/ex_websocket_client/ex_websocket_client.vcxproj new file mode 100644 index 000000000..013c887c1 --- /dev/null +++ b/src/civetweb/VisualStudio/ex_websocket_client/ex_websocket_client.vcxproj @@ -0,0 +1,160 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {58B93E94-7766-435E-93AE-42A2FB5D99B2} + Win32Proj + ex_websocket_client + 8.1 + + + + Application + true + MultiByte + v140 + + + Application + true + v140 + MultiByte + + + Application + false + true + MultiByte + v140 + + + Application + false + v140 + true + MultiByte + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + false + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + false + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + + + + Level3 + Disabled + USE_WEBSOCKET;WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + + + + + + + Level3 + Disabled + USE_WEBSOCKET;WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + USE_WEBSOCKET;WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + USE_WEBSOCKET;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/ex_websocket_client/ex_websocket_client.vcxproj.filters b/src/civetweb/VisualStudio/ex_websocket_client/ex_websocket_client.vcxproj.filters new file mode 100644 index 000000000..c91ce3eb2 --- /dev/null +++ b/src/civetweb/VisualStudio/ex_websocket_client/ex_websocket_client.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/lua_lib/lua_lib.vcxproj b/src/civetweb/VisualStudio/lua_lib/lua_lib.vcxproj new file mode 100644 index 000000000..a92e92ad8 --- /dev/null +++ b/src/civetweb/VisualStudio/lua_lib/lua_lib.vcxproj @@ -0,0 +1,191 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {8F5E5D77-D269-4665-9E27-1045DA6CF0D8} + Win32Proj + lua_lib + 8.1 + + + + StaticLibrary + true + MultiByte + v140_xp + + + StaticLibrary + true + v140 + MultiByte + + + StaticLibrary + false + true + MultiByte + v140_xp + + + StaticLibrary + false + v140 + true + MultiByte + + + + + + + + + + + + + + + + + + + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + + + + TurnOffAllWarnings + Disabled + LUA_COMPAT_ALL;THREADSAFE=1;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_FTS3_PARENTHESIS;WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\src\third_party\lua-5.2.4\src;%(AdditionalIncludeDirectories) + + + Windows + true + + + + + + + Level3 + Disabled + LUA_COMPAT_ALL;THREADSAFE=1;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_FTS3_PARENTHESIS;WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\src\third_party\lua-5.2.4\src;%(AdditionalIncludeDirectories) + + + Windows + true + + + + + Level3 + + + MaxSpeed + true + true + LUA_COMPAT_ALL;THREADSAFE=1;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_FTS3_PARENTHESIS;WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\src\third_party\lua-5.2.4\src;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + LUA_COMPAT_ALL;THREADSAFE=1;SQLITE_ENABLE_FTS3;SQLITE_ENABLE_FTS3_PARENTHESIS;WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\src\third_party\lua-5.2.4\src;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/lua_lib/lua_lib.vcxproj.filters b/src/civetweb/VisualStudio/lua_lib/lua_lib.vcxproj.filters new file mode 100644 index 000000000..657c5f255 --- /dev/null +++ b/src/civetweb/VisualStudio/lua_lib/lua_lib.vcxproj.filters @@ -0,0 +1,135 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + diff --git a/src/civetweb/VisualStudio/unit_test/unit_test.vcxproj b/src/civetweb/VisualStudio/unit_test/unit_test.vcxproj new file mode 100644 index 000000000..d3e02cd61 --- /dev/null +++ b/src/civetweb/VisualStudio/unit_test/unit_test.vcxproj @@ -0,0 +1,105 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + + + + + + + + + + + + + + + + + + + + {1AC4A7A6-0100-4287-97F4-B95807BE5607} + Win32Proj + unit_test + 8.1 + + + + Application + true + MultiByte + v140_xp + + + Application + false + true + MultiByte + v140_xp + + + + + + + + + + + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + false + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + + + + Level3 + Disabled + REPLACE_CHECK_FOR_LOCAL_DEBUGGING;LOCAL_TEST;USE_IPV6;USE_WEBSOCKET;MEMORY_DEBUGGING;WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\src;$(ProjectDir)..\..\include;$(ProjectDir)..\..\src\third_party\lua-5.2.4\src;$(ProjectDir)..\..\..\check-0.10.0\;$(ProjectDir)..\..\..\check-0.10.0\src\;%(AdditionalIncludeDirectories) + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + REPLACE_CHECK_FOR_LOCAL_DEBUGGING;WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\src;$(ProjectDir)..\..\include;$(ProjectDir)..\..\src\third_party\lua-5.2.4\src;$(ProjectDir)..\..\..\check-0.10.0\;$(ProjectDir)..\..\..\check-0.10.0\src\;%(AdditionalIncludeDirectories) + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/unit_test/unit_test.vcxproj.filters b/src/civetweb/VisualStudio/unit_test/unit_test.vcxproj.filters new file mode 100644 index 000000000..bf33419b5 --- /dev/null +++ b/src/civetweb/VisualStudio/unit_test/unit_test.vcxproj.filters @@ -0,0 +1,66 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Headerdateien + + + Headerdateien + + + Headerdateien + + + Headerdateien + + + Headerdateien + + + Headerdateien + + + Headerdateien + + + Headerdateien + + + Quelldateien + + + + + Quelldateien + + + Quelldateien + + + Quelldateien + + + Quelldateien + + + Quelldateien + + + Quelldateien + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/upload/upload.vcxproj b/src/civetweb/VisualStudio/upload/upload.vcxproj new file mode 100644 index 000000000..4aec14c46 --- /dev/null +++ b/src/civetweb/VisualStudio/upload/upload.vcxproj @@ -0,0 +1,160 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + + + + + + + + {882EC43C-2EEE-434B-A711-C845678D29C6} + Win32Proj + upload + 8.1 + + + + Application + true + v140 + MultiByte + + + Application + true + v140 + MultiByte + + + Application + false + v140 + true + MultiByte + + + Application + false + v140 + true + MultiByte + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + true + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + false + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + false + $(SolutionDir)\$(Configuration)\$(Platform)\ + + + + + + Level3 + Disabled + NO_FILES;WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + + + + + + + Level3 + Disabled + NO_FILES;WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + NO_FILES;WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + NO_FILES;WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + $(ProjectDir)..\..\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/src/civetweb/VisualStudio/upload/upload.vcxproj.filters b/src/civetweb/VisualStudio/upload/upload.vcxproj.filters new file mode 100644 index 000000000..fb2d2476e --- /dev/null +++ b/src/civetweb/VisualStudio/upload/upload.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A322342A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52E765} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AA145} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff --git a/src/civetweb/_config.yml b/src/civetweb/_config.yml new file mode 100644 index 000000000..259a24e4d --- /dev/null +++ b/src/civetweb/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-tactile \ No newline at end of file diff --git a/src/civetweb/appveyor.yml b/src/civetweb/appveyor.yml new file mode 100644 index 000000000..87455a2bb --- /dev/null +++ b/src/civetweb/appveyor.yml @@ -0,0 +1,386 @@ +version: '{build}' + + +build: +# no automatic build in script mode + + +skip_commits: + # Builds just testing something on Travis CI don't need to be + # done on AppVeyor + message: /\[Travis\]/ + # Dont build, if only documentation was changed + files: + - '**/*.md' + + +environment: + enable_cxx: NO + enable_ssl_dynamic_loading: YES + enable_lua: NO + enable_lua_shared: NO + c_standard: auto + cxx_standard: auto + matrix: + # Use default values + - id: Default-x86 + compiler: msvc-19-seh + build_shared: NO + no_files: NO + enable_ipv6: NO + enable_ssl: YES + enable_websockets: NO + no_cgi: NO + no_caching: NO + configuration: Release + platform: x86 + - id: Default-x64 + compiler: msvc-19-seh + build_shared: NO + no_files: NO + enable_ipv6: NO + enable_ssl: YES + enable_websockets: NO + no_cgi: NO + no_caching: NO + configuration: Release + platform: x64 + # Use default values + - id: Full-x86 + compiler: msvc-19-seh + build_shared: NO + no_files: NO + enable_ipv6: YES + enable_ssl: YES + enable_websockets: YES + no_cgi: NO + no_caching: NO + configuration: Release + platform: x86 + - id: Full-x64 + compiler: msvc-19-seh + build_shared: NO + no_files: NO + enable_ipv6: YES + enable_ssl: YES + enable_websockets: YES + no_cgi: NO + no_caching: NO + configuration: Release + platform: x64 + # Debug builds + - id: Full-x86-Debug + compiler: msvc-19-seh + build_shared: NO + no_files: NO + enable_ipv6: YES + enable_ssl: YES + enable_websockets: YES + no_cgi: NO + no_caching: NO + configuration: Debug + platform: x86 + - id: Full-x64-Debug + compiler: msvc-19-seh + build_shared: NO + no_files: NO + enable_ipv6: YES + enable_ssl: YES + enable_websockets: YES + no_cgi: NO + no_caching: NO + configuration: Debug + platform: x64 + # Minimum settings + - id: Minimal-x86 + compiler: msvc-19-seh + build_shared: NO + no_files: YES + enable_ipv6: NO + enable_ssl: NO + enable_websockets: NO + no_cgi: YES + no_caching: YeS + configuration: Release + platform: x86 + - id: Minimal-x64 + compiler: msvc-19-seh + build_shared: NO + no_files: YES + enable_ipv6: NO + enable_ssl: NO + enable_websockets: NO + no_cgi: YES + no_caching: YeS + configuration: Release + platform: x64 + # Test shared and debug build + - id: Shared-default-x86 + compiler: msvc-19-seh + build_shared: YES + no_files: NO + enable_ipv6: NO + enable_ssl: YES + enable_websockets: NO + no_cgi: NO + no_caching: NO + configuration: Release + platform: x86 + - id: Shared-default-x64 + compiler: msvc-19-seh + build_shared: YES + no_files: NO + enable_ipv6: NO + enable_ssl: YES + enable_websockets: NO + no_cgi: NO + no_caching: NO + configuration: Release + platform: x64 + # MinGW + - id: Full-GCC-x64 + compiler: gcc-5.1.0-posix + build_shared: NO + no_files: NO + enable_ipv6: YES + enable_ssl: YES + enable_websockets: YES + no_cgi: NO + no_caching: NO + configuration: Release + platform: x64 + # Visual Studio 2010 + - id: Full-VS2010-x86 + compiler: msvc-16-seh + build_shared: NO + no_files: NO + enable_ipv6: YES + enable_ssl: YES + enable_websockets: YES + no_cgi: NO + no_caching: NO + configuration: Release + platform: x86 + # Visual Studio 2012 + - id: Full-VS2012-x86 + compiler: msvc-17-seh + build_shared: NO + no_files: NO + enable_ipv6: YES + enable_ssl: YES + enable_websockets: YES + no_cgi: NO + no_caching: NO + configuration: Release + platform: x86 + # Visual Studio 2013 + - id: Full-VS2013-x86 + compiler: msvc-18-seh + build_shared: NO + no_files: NO + enable_ipv6: YES + enable_ssl: YES + enable_websockets: YES + no_cgi: NO + no_caching: NO + configuration: Release + platform: x86 + - id: Full-VS2013-x64 + compiler: msvc-18-seh + build_shared: NO + no_files: NO + enable_ipv6: YES + enable_ssl: YES + enable_websockets: YES + no_cgi: NO + no_caching: NO + configuration: Release + platform: x64 + # Visual Studio 2015 is default + # Visual Studio 2017 is not yet default + - id: Full-VS2017-x86 + compiler: msvc-20-seh + build_shared: NO + no_files: NO + enable_ipv6: YES + enable_ssl: YES + enable_websockets: YES + no_cgi: NO + no_caching: NO + configuration: Release + platform: x86 + APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 + - id: Full-VS2017-x64 + compiler: msvc-20-seh + build_shared: NO + no_files: NO + enable_ipv6: YES + enable_ssl: YES + enable_websockets: YES + no_cgi: NO + no_caching: NO + configuration: Release + platform: x64 + APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 + + +install: + # Derive some extra information + - set build_type=%configuration% + - for /f "tokens=1-3 delims=-" %%a in ("%compiler%") do (@set "compiler_name=%%a") + - for /f "tokens=1-3 delims=-" %%a in ("%compiler%") do (@set "compiler_version=%%b") + - for /f "tokens=1-3 delims=-" %%a in ("%compiler%") do (@set "compiler_threading=%%c") + - if "%platform%"=="x64" (set arch=x86_64) + - if "%platform%"=="x86" (set arch=i686) + # Download the specific version of MinGW + - if "%compiler_name%"=="gcc" (@set "mingw_output_folder=C:\mingw-builds") + - if "%compiler_name%"=="gcc" ( + @for /f %%a in ( + 'call mingw.cmd + /version "%compiler_version%" + /arch "%arch%" + /threading "%compiler_threading%" + "%mingw_output_folder%"' + ) do @set "compiler_path=%%a" + ) + - if "%compiler_name%"=="gcc" (@set "mingw_log_folder=%mingw_output_folder%\logs") + - if exist "%mingw_log_folder%" @for /f %%f in ('dir /b /oD /tc "%mingw_log_folder%"') do @set "mingw_log_file=%mingw_log_folder%\%%f" + - if exist "%mingw_log_file%" powershell Push-AppveyorArtifact "%mingw_log_file%" -FileName mingw-download.log + # Get OpenSSL + # + # OpenSSL should already be installed, according to + # - http://help.appveyor.com/discussions/questions/1132-openssl-installation-issues + # - https://github.com/appveyor/ci/issues/576 + # + - cmd: set PATH=%PATH%;C:\OpenSSL-Win32;C:\OpenSSL-Win64 + - dir C:\OpenSSL-Win32 + - dir C:\OpenSSL-Win64 + - path + + +before_build: + # Remove sh.exe from the path otherwise CMake will complain: + # "sh.exe was found in your PATH, here: C:/Program Files/Git/usr/bin/sh.exe" + # and the MinGW build will not work (the Visual Studio build does not care). + # See http://help.appveyor.com/discussions/problems/3193-cmake-building-for-mingw-issue-with-git-shexe + # The entire directory containing sh.exe could be removed from the PATH environment: + # - set PATH=%PATH:C:\Program Files\Git\usr\bin;=% + # However, this will also remove all other programs in this directory from the PATH. + # In particular "patch" is still required. + # So, just rename sh.exe: + - ren "C:\Program Files\Git\usr\bin\sh.exe" _sh.exe + # Set up mingw commands + - if "%compiler_name%"=="gcc" (set "generator=MinGW Makefiles") + - if "%compiler_name%"=="gcc" (set "build=mingw32-make -j4") + - if "%compiler_name%"=="gcc" (set "test=mingw32-make test") + # MSVC specific commands + # Note: The minimum version officially supported for CivetWeb is VS2010. Older ones might work or not. + - if "%compiler_version%"=="14" (set "vs_version=8" & set "vs_year=2005") + - if "%compiler_version%"=="15" (set "vs_version=9" & set "vs_year=2008") + - if "%compiler_version%"=="16" (set "vs_version=10" & set "vs_year=2010") + - if "%compiler_version%"=="17" (set "vs_version=11" & set "vs_year=2012") + - if "%compiler_version%"=="18" (set "vs_version=12" & set "vs_year=2013") + - if "%compiler_version%"=="19" (set "vs_version=14" & set "vs_year=2015") + - if "%compiler_version%"=="20" (set "vs_version=15" & set "vs_year=2017") + - if "%compiler_name%"=="msvc" (set "generator=Visual Studio %vs_version% %vs_year%") + - if "%compiler_name%"=="msvc" ( + if "%platform%"=="x64" ( + set "generator=%generator% Win64" + ) + ) + - if %compiler_version% gtr 9 (set platform=%platform:x86=Win32%) + - if "%compiler_name%"=="msvc" (set "msbuild_opts=/clp:OnlyErrors;OnlyWarnings /nologo /m /v:m") + - if "%compiler_name%"=="msvc" (set "build=msbuild %msbuild_opts% /p:Configuration=%configuration% /p:Platform=%platform% civetweb.sln") + - if "%compiler_name%"=="msvc" (set "test=msbuild %msbuild_opts% RUN_TESTS.vcxproj") + # Add the compiler path if needed + - if not "%compiler_path%"=="" (set "PATH=%PATH%;%compiler_path%") + # git bash conflicts with MinGW makefiles + - if "%generator%"=="MinGW Makefiles" (set "PATH=%PATH:C:\Program Files (x86)\Git\bin=%") + # Useful locations + - set "source_path=%cd%" + - set "output_path=%source_path%\output" + - set "build_path=%output_path%\build" + - set "install_path=%output_path%\install" + - set "third_party_dir=C:\third-party" + # Check some settings of the build server + - ver + - cd + - dir + - ipconfig /all + # Generate the build scripts with CMake + - mkdir "%build_path%" + - cd "%build_path%" + - cmake --version + - appveyor AddMessage -Category Information "Generating '%generator%'" + - cmake + -G "%generator%" + -DCMAKE_BUILD_TYPE=%build_type% + -DBUILD_SHARED_LIBS=%build_shared% + -DCIVETWEB_SERVE_NO_FILES=%no_files% + "-DCIVETWEB_THIRD_PARTY_DIR=%third_party_dir:\=\\%" + -DCIVETWEB_ENABLE_THIRD_PARTY_OUTPUT=YES + -DCIVETWEB_ENABLE_SSL=%enable_ssl% + -DCIVETWEB_DISABLE_CGI=%no_cgi% + -DCIVETWEB_ENABLE_SSL_DYNAMIC_LOADING=%enable_ssl_dynamic_loading% + -DCIVETWEB_ENABLE_WEBSOCKETS=%enable_websockets% + -DCIVETWEB_ENABLE_CXX=%enable_cxx% + -DCIVETWEB_ENABLE_LUA=%enable_lua% + -DCIVETWEB_ENABLE_LUA_SHARED=%enable_lua_shared% + -DCIVETWEB_DISABLE_CACHING=%no_caching% + -DCIVETWEB_C_STANDARD=%c_standard% + -DCIVETWEB_CXX_STANDARD=%cxx_standard% + "%source_path%" + - powershell Push-AppveyorArtifact CMakeCache.txt + - cd "%source_path%" + +build_script: + - cd + - cd "%build_path%" + - appveyor AddMessage -Category Information "Build command '%build%'" + - cmd /c "%build%" + - cd "%source_path%" + +test_script: + - cd "%build_path%" + - appveyor AddMessage -Category Information "Test command '%build%'" + - set CTEST_OUTPUT_ON_FAILURE=1 + - cmd /c "%test%" + - cd "%source_path%" + + - set "output_path=%source_path%\output" + - set "build_path=%output_path%\build" + - set "install_path=%output_path%\install" + - set "third_party_dir=C:\third-party" + +after_test: + - echo "Current directory:" + - cd + - dir + - md dist + - if "%build_type%"=="Release" (cmake "-DCMAKE_INSTALL_PREFIX=%install_path%" -P "%build_path%/cmake_install.cmake") + - dir dist\ + - echo "Output directory:" + - dir %output_path% + - echo "Build directory:" + - dir %build_path% + - if "%build_type%"=="Release" (echo "Install directory:") + - if "%build_type%"=="Release" (dir %install_path%) + - if "%build_type%"=="Release" (dir %install_path%\bin) + - if "%build_type%"=="Release" (dir %install_path%\include) + - if "%build_type%"=="Release" (dir %install_path%\lib) + - if "%build_type%"=="Release" (copy "%install_path%"\include dist\) + - if "%build_type%"=="Release" (copy "%install_path%"\bin\*.exe dist\) + - echo "Dist directory:" + - dir dist\ + +matrix: + fast_finish: false + +cache: + - C:\mingw-builds -> mingw.cmd + - C:\third-party -> **\CMakeLists.txt + - C:\ssl + +artifacts: + - path: dist\* + diff --git a/src/civetweb/build b/src/civetweb/build new file mode 100755 index 000000000..2b423108f --- /dev/null +++ b/src/civetweb/build @@ -0,0 +1,138 @@ +#!/bin/sh +set -euo pipefail +IFS=$'\n\t' + +stdout() { + cat <<< "$@" +} + +stderr() { + cat <<< "$@" 1>&2 +} + +prereqs () { + local E_BADARGS=65 + if [ $# -eq 0 ]; then + stderr "Usage: $(basename $0) [prerequisite_program] [another_program...]" + return $E_BADARGS + fi + for prog in $@; do + hash $prog 2>&- + if [ $? -ne 0 ]; then + return 1 + fi + done +} + +usage() { + if [ $# -ne 0 ]; then + stdout $@ + fi + stdout "Usage: $(basename $0) [options]" + stdout + stdout "A convenience script to quickly build the library with CMake." + stdout + stdout "Options:" + stdout " [--shared|(--static)] Builds either a static or a shared library" + stdout " [--debug|(--release)] Builds a certain variant of the library" + stdout " -g,--generator name The CMake generator to use ('Unix Makefiles')" + stdout " -o,--output folder The place to output the build files (./output)" + stdout + stdout "Examples:" + stdout " ./build" + stdout " ./build --shared --debug" + stdout " ./build --static --release -o ~/my-output-folder" +} + +check() { + local E_BADARGS=65 + if [ $# -ne 1 ]; then + stderr "Usage: check prerequisite_program" + return $E_BADARGS + fi + prereqs $1 + if [ $? -ne 0 ]; then + stderr "Failed to find `$1` on the command line:" + stderr "Please install it with your package manager" + return 1 + fi +} + +sanitize() { + local E_BADARGS=65 + if [ $# -ne 1 ]; then + stderr "Usage: sanitize string_to_clean" + return $E_BADARGS + fi + echo $(echo "$1" | sed "s|[^A-Za-z]\+|-|g" | tr '[:upper:]' '[:lower:]') + return 0 +} + +build () { + # Get the build locations + local src_dir=$(cd $(dirname $0); pwd -P) + + # Arguments + local E_BADARGS=65 + local generator="Unix Makefiles" + local shared=NO + local build_type=Release + local output_dir="${src_dir}/output" + while (( "$#" )); do + case "$1" in + --debug) build_type=Release;; + --release) build_type=Debug;; + --shared) shared=YES;; + --static) shared=NO;; + --output) shift; out="$1";; + -o) shift; output_dir="$1";; + --generator) shift; generator="$1";; + -g) shift; generator="$1";; + --help) usage; return 0;; + --) shift; break;; + -*) usage "Bad argument $1"; return ${E_BADARGS};; + *) break;; + esac + shift + done + + # Update the build folder + local build_dir=${output_dir}/build + local install_dir=${output_dir}/install + + # Create the build folder + mkdir -p ${build_dir} + + # Enter the build folder + cd ${build_dir} + trap 'cd ${src_dir}' INT TERM EXIT + + # Do the CMake configuration + check cmake + cmake -G ${generator} -DCMAKE_BUILD_TYPE=${build_type} -DBUILD_SHARED_LIBS:BOOL=${shared} ${src_dir} + + # Do the build + if [ "${generator}" = "Unix Makefiles" ]; then + check make + make all test + else + stderr "Unknown build system for ${generator}, go to ${build_dir} and run the correct build program" + fi + + # Do the install + cmake -DCMAKE_INSTALL_PREFIX="${install_dir}" -P "${build_dir}/cmake_install.cmake" + + # Return to the correct folder + trap - INT TERM EXIT + cd ${src_dir} + + # Notify the user + stdout "Built files are available at ${install_dir}" +} + +# If the script was not sourced we need to run the function +case "$0" in + *"build") + build "$@" + ;; +esac diff --git a/src/civetweb/build.cmd b/src/civetweb/build.cmd new file mode 100644 index 000000000..8ccf0e4bc --- /dev/null +++ b/src/civetweb/build.cmd @@ -0,0 +1,866 @@ +:: Make sure the extensions are enabled +@verify other 2>nul +@setlocal EnableDelayedExpansion +@if errorlevel 1 ( + call :print_usage "Failed to enable extensions" + exit /b 1 +) + +::Change the code page to unicode +@chcp 65001 1>nul 2>nul +@if errorlevel 1 ( + call :print_usage "Failed to change the code page to unicode" + exit /b 1 +) + +:: Set up some global variables +@set project=civetweb +@set "script_name=%~nx0" +@set "script_folder=%~dp0" +@set "script_folder=%script_folder:~0,-1%" +@set "output_path=%script_folder%\output" +@set "build_path=%output_path%\build" +@set "install_path=%output_path%\install" +@set build_shared=OFF +@set build_type=Release +@set dependency_path=%TEMP%\%project%-build-dependencies + +:: Check the command line parameters +@set logging_level=1 +@set "options=%* " +@if not "!options!"=="!options:/? =!" set usage="Convenience script to build %project% with CMake" +@for %%a in (%options%) do @( + @set arg=%%~a + @set arg=!arg: =! + @set one=!arg:~0,1! + @set two=!arg:~0,2! + @if /i [!arg!] == [/q] set quiet=true + @if /i [!two!] == [/v] call :verbosity "!arg!" + @if /i [!arg!] == [/s] set build_shared=ON + @if /i [!arg!] == [/d] set build_type=Debug + @if /i not [!one!] == [/] ( + if not defined generator ( + set generator=!arg! + ) else ( + set usage="Too many generators: !method! !arg!" ^ + "There should only be one generator parameter" + ) + ) +) +@if defined quiet ( + set logging_level=0 +) +@if not defined generator ( + set generator=MSVC +) +@if /i not [%generator%] == [MinGW] ( + if /i not [%generator%] == [MSVC] ( + call :print_usage "Invalid argument: %generator%" + exit /b 1 + ) +) + +:: Set up the logging +@set log_folder=%output_path%\logs +@call :iso8601 timestamp +@set log_path=%log_folder%\%timestamp%.log +@set log_keep=10 + +:: Only keep a certain amount of logs +@set /a "log_keep=log_keep-1" +@if not exist %log_folder% @mkdir %log_folder% +@for /f "skip=%log_keep%" %%f in ('dir /b /o-D /tc %log_folder%') do @( + call :log 4 "Removing old log file %log_folder%\%%f" + del %log_folder%\%%f +) + +:: Set up some more global variables +@call :architecture arch +@call :windows_version win_ver win_ver_major win_ver_minor win_ver_rev +@call :script_source script_source +@if [%script_source%] == [explorer] ( + set /a "logging_level=logging_level+1" +) + +:: Print the usage or start the script +@set exit_code=0 +@if defined usage ( + call :print_usage %usage% +) else ( + call :main + @if errorlevel 1 ( + @call :log 0 "Failed to build the %project% project" + @set exit_code=1 + ) +) + +:: Tell the user where the built files are +@call :log 5 +@call :log 0 "The built files are available in %install_path%" + +:: Stop the script if the user double clicked +@if [%script_source%] == [explorer] ( + pause +) + +@exit /b %exit_code% +@endlocal +@goto :eof + +:: -------------------------- Functions start here ---------------------------- + +:main - Main function that performs the build +@setlocal +@call :log 6 +@call :log 2 "Welcome to the %project% build script" +@call :log 6 "------------------------------------" +@call :log 6 +@call :log 2 "This script builds the project using CMake" +@call :log 6 +@call :log 2 "Generating %generator%..." +@call :log 6 +@set methods=dependencies ^ + generate ^ + build ^ + install +@for %%m in (%methods%) do @( + call :log 3 "Excuting the '%%m' method" + call :log 8 + call :%%~m + if errorlevel 1 ( + call :log 0 "Failed to complete the '%%~m' dependency routine" + call :log 0 "View the log at %log_path%" + exit /b 1 + ) +) +@call :log 6 "------------------------------------" +@call :log 2 "Build complete" +@call :log 6 +@endlocal +@goto :eof + +:print_usage - Prints the usage of the script +:: %* - message to print, each argument on it's own line +@setlocal +@for %%a in (%*) do @echo.%%~a +@echo. +@echo.build [/?][/v[v...]^|/q][MinGW^|MSVC] +@echo. +@echo. [MinGW^|(MSVC)] +@echo. Builds the library with one of the compilers +@echo. /s Builds shared libraries +@echo. /d Builds a debug variant of the project +@echo. /v Sets the output to be more verbose +@echo. /v[v...] Extra verbosity, /vv, /vvv, etc +@echo. /q Quiets the output +@echo. /? Shows this usage message +@echo. +@endlocal +@goto :eof + +:dependencies - Installs any prerequisites for the build +@setlocal EnableDelayedExpansion +@if errorlevel 1 ( + call :log 0 "Failed to enable extensions" + exit /b 1 +) +@call :log 5 +@call :log 0 "Installing dependencies for %generator%" +@if /i [%generator%] == [MinGW] ( + call :mingw compiler_path + @if errorlevel 1 ( + @call :log 5 + @call :log 0 "Failed to find MinGW" + @exit /b 1 + ) + set "PATH=!compiler_path!;%PATH%" + @call :find_in_path gcc_executable gcc.exe + @if errorlevel 1 ( + @call :log 5 + @call :log 0 "Failed to find gcc.exe" + @exit /b 1 + ) +) +@if [%reboot_required%] equ [1] call :reboot +@endlocal & set "PATH=%PATH%" +@goto :eof + +:generate - Uses CMake to generate the build files +@setlocal EnableDelayedExpansion +@if errorlevel 1 ( + call :log 0 "Failed to enable extensions" + exit /b 1 +) +@call :log 5 +@call :log 0 "Generating CMake files for %generator%" +@call :cmake cmake_executable +@if errorlevel 1 ( + @call :log 5 + @call :log 0 "Need CMake to create the build files" + @exit /b 1 +) +@if /i [%generator%] == [MinGW] @( + @set "generator_var=-G "MinGW Makefiles^"" +) +@if /i [%generator%] == [MSVC] @( + rem We could figure out the correct MSVS generator here +) +@call :iso8601 iso8601 +@set output=%temp%\cmake-%iso8601%.log +@if not exist %build_path% mkdir %build_path% +@cd %build_path% +@"%cmake_executable%" ^ + !generator_var! ^ + -DCMAKE_BUILD_TYPE=!build_type! ^ + -DBUILD_SHARED_LIBS=!build_shared! ^ + "%script_folder%" > "%output%" +@if errorlevel 1 ( + @call :log 5 + @call :log 0 "Failed to generate build files with CMake" + @call :log_append "%output%" + @cd %script_folder% + @exit /b 1 +) +@cd %script_folder% +@endlocal +@goto :eof + +:build - Builds the library +@setlocal EnableDelayedExpansion +@if errorlevel 1 ( + call :log 0 "Failed to enable extensions" + exit /b 1 +) +@call :log 5 +@call :log 0 "Building %project% with %generator%" +@if /i [%generator%] == [MinGW] @( + @call :find_in_path mingw32_make_executable mingw32-make.exe + @if errorlevel 1 ( + @call :log 5 + @call :log 0 "Failed to find mingw32-make" + @exit /b 1 + ) + @set "build_command=^"!mingw32_make_executable!^" all test" +) +@if /i [%generator%] == [MSVC] @( + @call :msbuild msbuild_executable + @if errorlevel 1 ( + @call :log 5 + @call :log 0 "Failed to find MSBuild" + @exit /b 1 + ) + @set "build_command=^"!msbuild_executable!^" /m:4 /p:Configuration=%build_type% %project%.sln" +) +@if not defined build_command ( + @call :log 5 + @call :log 0 "No build command for %generator%" + @exit /b 1 +) +@cd %build_path% +@call :iso8601 iso8601 +@set output=%temp%\build-%iso8601%.log +@call :log 7 +@call :log 2 "Build command: %build_command:"=%" +@%build_command% > "%output%" +@if errorlevel 1 ( + @call :log_append "%output%" + @call :log 5 + @call :log 0 "Failed to complete the build" + @exit /b 1 +) +@call :log_append "%output%" +@cd %script_folder% +@endlocal +@goto :eof + +:install - Installs the built files +@setlocal +@call :log 5 +@call :log 0 "Installing built files" +@call :cmake cmake_executable +@if errorlevel 1 ( + @call :log 5 + @call :log 0 "Need CMake to install the built files" + @exit /b 1 +) +@call :iso8601 iso8601 +@set output=%temp%\install-%iso8601%.log +@"%cmake_executable%" ^ + "-DCMAKE_INSTALL_PREFIX=%install_path%" ^ + -P "%build_path%/cmake_install.cmake" ^ + > "%output%" +@if errorlevel 1 ( + @call :log_append "%output%" + @call :log 5 + @call :log 0 "Failed to install the files" + @exit /b 1 +) +@call :log_append "%output%" +@endlocal +@goto :eof + +:script_source - Determines if the script was ran from the cli or explorer +:: %1 - The return variable [cli|explorer] +@verify other 2>nul +@setlocal EnableDelayedExpansion +@if errorlevel 1 ( + call :log 0 "Failed to enable extensions" + exit /b 1 +) +@call :log 3 "Attempting to detect the script source" +@echo "The invocation command was: '%cmdcmdline%'" >> %log_path% +@for /f "tokens=1-3,*" %%a in ("%cmdcmdline%") do @( + set cmd=%%~a + set arg1=%%~b + set arg2=%%~c + set rest=%%~d +) +@set quote=" +@if "!arg2:~0,1!" equ "!quote!" ( + if "!arg2:~-1!" neq "!quote!" ( + set "arg2=!arg2:~1!" + ) +) +@call :log 4 "cmd = %cmd%" +@call :log 4 "arg1 = %arg1%" +@call :log 4 "arg2 = %arg2%" +@call :log 4 "rest = %rest%" +@call :log 4 "src = %~f0" +@if /i "%arg2%" == "call" ( + set script_source=cli +) else ( + @if /i "%arg1%" == "/c" ( + set script_source=explorer + ) else ( + set script_source=cli + ) +) +@call :log 3 "The script was invoked from %script_source%" +@endlocal & set "%~1=%script_source%" +@goto :eof + +:architecture - Finds the system architecture +:: %1 - The return variable [x86|x86_64] +@setlocal +@call :log 3 "Determining the processor architecture" +@set "key=HKLM\System\CurrentControlSet\Control\Session Manager\Environment" +@set "var=PROCESSOR_ARCHITECTURE" +@for /f "skip=2 tokens=2,*" %%a in ('reg query "%key%" /v "%var%"') do @set "arch=%%b" +@if "%arch%" == "AMD64" set arch=x86_64 +@call :log 4 "arch = %arch%" +@endlocal & set "%~1=%arch%" +@goto :eof + +:md5 - Gets the MD5 checksum for a file +:: %1 - The hash +:: %2 - The file path +@setlocal +@set var=%~1 +@set file_path=%~2 +@if [%var%] == [] exit /b 1 +@if "%file_path%" == "" exit /b 1 +@if not exist "%file_path%" exit /b 1 +@for /f "skip=3 tokens=1,*" %%a in ('powershell Get-FileHash -Algorithm MD5 "'%file_path%'"') do @set hash=%%b +@if not defined hash ( + call :log 6 + call :log 0 "Failed to get MD5 hash for %file_path%" + exit /b 1 +) +@endlocal & set "%var%=%hash: =%" +@goto :eof + +:windows_version - Checks the windows version +:: %1 - The windows version +:: %2 - The major version number return variable +:: %3 - The minor version number return variable +:: %4 - The revision version number return variable +@setlocal +@call :log 3 "Retrieving the Windows version" +@for /f "tokens=2 delims=[]" %%x in ('ver') do @set win_ver=%%x +@set win_ver=%win_ver:Version =% +@set win_ver_major=%win_ver:~0,1% +@set win_ver_minor=%win_ver:~2,1% +@set win_ver_rev=%win_ver:~4% +@call :log 4 "win_ver = %win_ver%" +@endlocal & set "%~1=%win_ver%" ^ + & set "%~2=%win_ver_major%" ^ + & set "%~3=%win_ver_minor%" ^ + & set "%~4=%win_ver_rev%" +@goto :eof + +:find_in_path - Finds a program of file in the PATH +@setlocal +@set var=%~1 +@set file=%~2 +@if [%var%] == [] exit /b 1 +@if [%file%] == [] exit /b 1 +@call :log 3 "Searching PATH for %file%" +@for %%x in ("%file%") do @set "file_path=%%~f$PATH:x" +@if not defined file_path exit /b 1 +@endlocal & set "%var%=%file_path%" +@goto :eof + +:administrator_check - Checks for administrator priviledges +@setlocal +@call :log 2 "Checking for administrator priviledges" +@set "key=HKLM\Software\VCA\Tool Chain\Admin Check" +@reg add "%key%" /v Elevated /t REG_DWORD /d 1 /f > nul 2>&1 +@if errorlevel 1 exit /b 1 +@reg delete "%key%" /va /f > nul 2>&1 +@endlocal +@goto :eof + +:log_append - Appends another file into the current logging file +:: %1 - the file_path to the file to concatenate +@setlocal +@set "file_path=%~1" +@if [%file_path%] == [] exit /b 1 +@call :log 3 "Appending to log: %file_path%" +@call :iso8601 iso8601 +@set "temp_log=%temp%\append-%iso8601%.log" +@call :log 4 "Using temp file %temp_log%" +@type "%log_path%" "%file_path%" > "%temp_log%" 2>nul +@move /y "%temp_log%" "%log_path%" 1>nul +@del "%file_path%" 2>nul +@del "%temp_log%" 2>nul +@endlocal +@goto :eof + +:iso8601 - Returns the current time in ISO8601 format +:: %1 - the return variable +:: %2 - format [extended|basic*] +:: iso8601 - contains the resulting timestamp +@setlocal +@wmic Alias /? >NUL 2>&1 || @exit /b 1 +@set "var=%~1" +@if "%var%" == "" @exit /b 1 +@set "format=%~2" +@if "%format%" == "" set format=basic +@for /F "skip=1 tokens=1-6" %%g IN ('wmic Path Win32_UTCTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') do @( + @if "%%~l"=="" goto :iso8601_done + @set "yyyy=%%l" + @set "mm=00%%j" + @set "dd=00%%g" + @set "hour=00%%h" + @set "minute=00%%i" + @set "seconds=00%%k" +) +:iso8601_done +@set mm=%mm:~-2% +@set dd=%dd:~-2% +@set hour=%hour:~-2% +@set minute=%minute:~-2% +@set seconds=%seconds:~-2% +@if /i [%format%] == [extended] ( + set iso8601=%yyyy%-%mm%-%dd%T%hour%:%minute%:%seconds%Z +) else ( + if /i [%format%] == [basic] ( + set iso8601=%yyyy%%mm%%dd%T%hour%%minute%%seconds%Z + ) else ( + @exit /b 1 + ) +) +@set iso8601=%iso8601: =0% +@endlocal & set %var%=%iso8601% +@goto :eof + +:verbosity - Processes the verbosity parameter '/v[v...] +:: %1 - verbosity given on the command line +:: logging_level - set to the number of v's +@setlocal +@set logging_level=0 +@set verbosity=%~1 +:verbosity_loop +@set verbosity=%verbosity:~1% +@if not [%verbosity%] == [] @( + set /a "logging_level=logging_level+1" + goto verbosity_loop +) +@endlocal & set logging_level=%logging_level% +@goto :eof + +:log - Logs a message, depending on verbosity +:: %1 - level +:: [0-4] for CLI logging +:: [5-9] for GUI logging +:: %2 - message to print +@setlocal +@set "level=%~1" +@set "msg=%~2" +@if "%log_folder%" == "" ( + echo Logging was used to early in the script, log_folder isn't set yet + goto :eof +) +@if "%log_path%" == "" ( + echo Logging was used to early in the script, log_path isn't set yet + goto :eof +) +@if not exist "%log_folder%" mkdir "%log_folder%" +@if not exist "%log_path%" echo. 1>nul 2>"%log_path%" +@echo.%msg% >> "%log_path%" +@if %level% geq 5 ( + @if [%script_source%] == [explorer] ( + set /a "level=level-5" + ) else ( + @goto :eof + ) +) +@if "%logging_level%" == "" ( + echo Logging was used to early in the script, logging_level isn't set yet + goto :eof +) +@if %logging_level% geq %level% echo.%msg% 1>&2 +@endlocal +@goto :eof + + +:start_browser - Opens the default browser to a URL +:: %1 - the url to open +@setlocal +@set url=%~1 +@call :log 4 "Opening default browser: %url%" +@start %url% +@endlocal +@goto :eof + +:find_cmake - Finds cmake on the command line or in the registry +:: %1 - the cmake file path +@setlocal +@set var=%~1 +@if [%var%] == [] exit /b 1 +@call :log 6 +@call :log 6 "Finding CMake" +@call :log 6 "--------------" +@call :find_in_path cmake_executable cmake.exe +@if not errorlevel 1 goto found_cmake +@for /l %%i in (5,-1,0) do @( +@for /l %%j in (9,-1,0) do @( +@for /l %%k in (9,-1,0) do @( +@for %%l in (HKCU HKLM) do @( +@for %%m in (SOFTWARE SOFTWARE\Wow6432Node) do @( + @reg query "%%l\%%m\Kitware\CMake %%i.%%j.%%k" /ve > nul 2>nul + @if not errorlevel 1 ( + @for /f "skip=2 tokens=2,*" %%a in ('reg query "%%l\%%m\Kitware\CMake %%i.%%j.%%k" /ve') do @( + @if exist "%%b\bin\cmake.exe" ( + @set "cmake_executable=%%b\bin\cmake.exe" + goto found_cmake + ) + ) + ) +))))) +@call :log 5 +@call :log 0 "Failed to find cmake" +@exit /b 1 +:found_cmake +@endlocal & set "%var%=%cmake_executable%" +@goto :eof + +:cmake - Finds cmake and installs it if necessary +:: %1 - the cmake file path +@setlocal +@set var=%~1 +@if [%var%] == [] exit /b 1 +@call :log 6 +@call :log 6 "Checking for CMake" +@call :log 6 "------------------" +@call :find_cmake cmake_executable cmake.exe +@if not errorlevel 1 goto got_cmake +@set checksum=C00267A3D3D9619A7A2E8FA4F46D7698 +@set version=3.2.2 +@call :install_nsis cmake http://www.cmake.org/files/v%version:~0,3%/cmake-%version%-win32-x86.exe %checksum% +@if errorlevel 1 ( + call :log 5 + call :log 0 "Failed to install cmake" + @exit /b 1 +) +@call :find_cmake cmake_executable cmake.exe +@if not errorlevel 1 goto got_cmake +@call :log 5 +@call :log 0 "Failed to check for cmake" +@exit /b 1 +:got_cmake +@endlocal & set "%var%=%cmake_executable%" +@goto :eof + +:mingw - Finds MinGW, installing it if needed +:: %1 - the compiler path that should be added to PATH +@setlocal EnableDelayedExpansion +@if errorlevel 1 ( + @call :log 5 + @call :log 0 "Failed to enable extensions" + @exit /b 1 +) +@set var=%~1 +@if [%var%] == [] exit /b 1 +@call :log 6 +@call :log 6 "Checking for MinGW" +@call :log 6 "------------------" +@call :find_in_path gcc_executable gcc.exe +@if not errorlevel 1 ( + @for %%a in ("%gcc_executable%") do @set "compiler_path=%%~dpa" + goto got_mingw +) +@call :log 7 +@call :log 2 "Downloading MinGW" +@if %logging_level% leq 1 set "logging=/q" +@if %logging_level% gtr 1 set "logging=/v" +@set output_path= +@for /f %%a in ('call + "%script_folder%\mingw.cmd" + %logging% + /arch "%arch%" + "%dependency_path%"' +) do @set "compiler_path=%%a\" +@if not defined compiler_path ( + @call :log_append "%output%" + @call :log 5 + @call :log 0 "Failed to download MinGW" + @exit /b 1 +) +:got_mingw +@call :log 5 +@call :log 0 "Found MinGW: %compiler_path%gcc.exe" +@endlocal & set "%var%=%compiler_path%" +@goto :eof + +:msbuild - Finds MSBuild +:: %1 - the path to MSBuild executable +@setlocal +@set var=%~1 +@if [%var%] == [] exit /b 1 +@call :find_in_path msbuild_executable msbuild.exe +@if not errorlevel 1 goto got_msbuild +@for /l %%i in (20,-1,4) do @( +@for /l %%j in (9,-1,0) do @( +@for %%k in (HKCU HKLM) do @( +@for %%l in (SOFTWARE SOFTWARE\Wow6432Node) do @( + @reg query "%%k\%%l\Microsoft\MSBuild\%%i.%%j" /v MSBuildOverrideTasksPath > nul 2>nul + @if not errorlevel 1 ( + @for /f "skip=2 tokens=2,*" %%a in ('reg query "%%k\%%l\Microsoft\MSBuild\%%i.%%j" /v MSBuildOverrideTasksPath') do @( + @if exist "%%bmsbuild.exe" ( + @set "msbuild_executable=%%bmsbuild.exe" + goto got_msbuild + ) + ) + ) +)))) +@call :log 5 +@call :log 0 "Failed to check for MSBuild" +@exit /b 1 +:got_msbuild +@endlocal & set "%var%=%msbuild_executable%" +@goto :eof + +:download - Downloads a file from the internet +:: %1 - the url of the file to download +:: %2 - the file to download to +:: %3 - the MD5 checksum of the file (optional) +@setlocal EnableDelayedExpansion +@if errorlevel 1 ( + call :print_usage "Failed to enable extensions" + exit /b 1 +) +@set url=%~1 +@set file_path=%~2 +@set checksum=%~3 +@for %%a in (%file_path%) do @set dir_path=%%~dpa +@for %%a in (%file_path%) do @set file_name=%%~nxa +@if [%url%] == [] exit /b 1 +@if [%file_path%] == [] exit /b 1 +@if [%dir_path%] == [] exit /b 1 +@if [%file_name%] == [] exit /b 1 +@if not exist "%dir_path%" mkdir "%dir_path%" +@call :log 1 "Downloading %url%" +@call :iso8601 iso8601 +@set temp_path=%temp%\download-%iso8601%-%file_name% +@call :log 3 "Using temp file %temp_path%" +@powershell Invoke-WebRequest "%url%" -OutFile %temp_path% +@if errorlevel 1 ( + call :log 0 "Failed to download %url%" + exit /b 1 +) +@if [%checksum%] neq [] ( + @call :log 4 "Checking %checksum% against %temp_path%" + @call :md5 hash "%temp_path%" + if "!hash!" neq "%checksum%" ( + call :log 0 "Failed to match checksum: %temp_path%" + call :log 0 "Hash : !hash!" + call :log 0 "Checksum: %checksum%" + exit /b 1 + ) else ( + call :log 3 "Checksum matched: %temp_path%" + call :log 3 "Hash : !hash!" + call :log 3 "Checksum: %checksum%" + ) +) +@call :log 4 "Renaming %temp_path% to %file_path%" +@move /y "%temp_path%" "%file_path%" 1>nul +@endlocal +@goto :eof + +:install_msi - Installs a dependency from an Microsoft Installer package (.msi) +:: %1 - [string] name of the project to install +:: %2 - The location of the .msi, a url must start with 'http://' or file_path +:: %3 - The checksum of the msi (optional) +@setlocal +@set name=%~1 +@set file_path=%~2 +@set checksum=%~3 +@set msi=%~nx2 +@set msi_path=%dependency_path%\%msi% +@if [%name%] == [] exit /b 1 +@if [%file_path%] == [] exit /b 1 +@if [%msi%] == [] exit /b 1 +@if [%msi_path%] == [] exit /b 1 +@for %%x in (msiexec.exe) do @set "msiexec_path=%%~f$PATH:x" +@if "msiexec_path" == "" ( + call :log 0 "Failed to find the Microsoft package installer (msiexec.exe)" + call :log 6 + call :log 0 "Please install it from the Microsoft Download center" + call :log 6 + choice /C YN /T 60 /D N /M "Would you like to go there now?" + if !errorlevel! equ 1 call :start_browser ^ + "http://search.microsoft.com/DownloadResults.aspx?q=Windows+Installer" + exit /b 1 +) +@call :log 6 +@call :log 1 "Installing the '%name%' dependency" +@call :log 6 "-------------------------------------" +@call :administrator_check +@if errorlevel 1 ( + call :log 0 "You must run %~nx0 in elevated mode to install '%name%'" + call :log 5 "Right-Click and select 'Run as Administrator' + call :log 0 "Install the dependency manually by running %file_path%" + @exit /b 740 +) +@if [%file_path:~0,4%] == [http] ( + if not exist "%msi_path%" ( + call :download "%file_path%" "%msi_path%" %checksum% + if errorlevel 1 ( + call :log 0 "Failed to download the %name% dependency" + exit /b 1 + ) + ) +) else ( + call :log 2 "Copying MSI %file_path% to %msi_path%" + call :log 7 + if not exist "%msi_path%" ( + xcopy /q /y /z "%file_path%" "%msi_path%" 1>nul + if errorlevel 1 ( + call :log 0 "Failed to copy the Microsoft Installer" + exit /b 1 + ) + ) +) +@call :log 1 "Running the %msi%" +@call :log 6 +@set msi_log=%temp%\msiexec-%timestamp%.log +@call :log 3 "Logging to: %msi_log%" +@msiexec /i "%msi_path%" /passive /log "%msi_log%" ALLUSERS=1 +@set msi_errorlevel=%errorlevel% +@call :log_append "%msi_log%" +@if %msi_errorlevel% equ 0 goto install_msi_success +@if %msi_errorlevel% equ 3010 goto install_msi_success_reboot +@if %msi_errorlevel% equ 1641 goto install_msi_success_reboot +@if %msi_errorlevel% equ 3015 goto install_msi_in_progress_reboot +@if %msi_errorlevel% equ 1615 goto install_msi_in_progress_reboot +@call :log 0 "Microsoft Installer failed: %msi_errorlevel%" +@call :log 0 "Install the dependency manually by running %msi_path%" +@exit /b 1 +:install_msi_in_progress_reboot +@call :log 0 "The installation requires a reboot to continue" +@call :log 5 +@call :reboot +@exit /b 1 +:install_msi_success_reboot +@call :log 3 "The installation requires a reboot to be fully functional" +@set reboot_required=1 +:install_msi_success +@call :log 2 "Successfully installed %name%" +@call :log 7 +@endlocal & set reboot_required=%reboot_required% +@goto :eof + +:install_nsis - Installs a dependency from an Nullsoft Installer package (.exe) +:: %1 - [string] name of the project to install +:: %2 - The location of the .exe, a url must start with 'http://' or file_path +:: %3 - The checksum of the exe (optional) +@setlocal +@set name=%~1 +@set file_path=%~2 +@set checksum=%~3 +@set exe=%~nx2 +@set exe_path=%dependency_path%\%exe% +@if [%name%] == [] exit /b 1 +@if [%file_path%] == [] exit /b 1 +@if [%exe%] == [] exit /b 1 +@if [%exe_path%] == [] exit /b 1 +@call :log 6 +@call :log 1 "Installing the '%name%' dependency" +@call :log 6 "-------------------------------------" +@call :administrator_check +@if errorlevel 1 ( + call :log 0 "You must run %~nx0 in elevated mode to install '%name%'" + call :log 5 "Right-Click and select 'Run as Administrator' + call :log 0 "Install the dependency manually by running %file_path%" + @exit /b 740 +) +@if [%file_path:~0,4%] == [http] ( + if not exist "%exe_path%" ( + call :download "%file_path%" "%exe_path%" %checksum% + if errorlevel 1 ( + call :log 0 "Failed to download the %name% dependency" + exit /b 1 + ) + ) +) else ( + call :log 2 "Copying installer %file_path% to %exe_path%" + call :log 7 + if not exist "%exe_path%" ( + xcopy /q /y /z "%file_path%" "%exe_path%" 1>nul + if errorlevel 1 ( + call :log 0 "Failed to copy the Nullsoft Installer" + exit /b 1 + ) + ) +) +@call :log 1 "Running the %exe%" +@call :log 6 +@"%exe_path%" /S +@set nsis_errorlevel=%errorlevel% +@if %nsis_errorlevel% equ 0 goto install_nsis_success +@if %nsis_errorlevel% equ 3010 goto install_nsis_success_reboot +@if %nsis_errorlevel% equ 1641 goto install_nsis_success_reboot +@if %nsis_errorlevel% equ 3015 goto install_nsis_in_progress_reboot +@if %nsis_errorlevel% equ 1615 goto install_nsis_in_progress_reboot +@call :log 0 "Nullsoft Installer failed: %nsis_errorlevel%" +@call :log 0 "Install the dependency manually by running %exe_path%" +@exit /b 1 +:install_nsis_in_progress_reboot +@call :log 0 "The installation requires a reboot to continue" +@call :log 5 +@call :reboot +@exit /b 1 +:install_nsis_success_reboot +@call :log 3 "The installation requires a reboot to be fully functional" +@set reboot_required=1 +:install_nsis_success +@call :log 2 "Successfully installed %name%" +@call :log 7 +@endlocal & set reboot_required=%reboot_required% +@goto :eof + +:reboot - Asks the user if they would like to reboot then stops the script +@setlocal +@call :log 6 "-------------------------------------------" +@choice /C YN /T 60 /D N /M "The %method% requires a reboot, reboot now?" +@set ret=%errorlevel% +@call :log 6 +@if %ret% equ 1 ( + @shutdown /r +) else ( + @call :log 0 "You will need to reboot to complete the %method%" + @call :log 5 +) +@endlocal +@goto :eof diff --git a/src/civetweb/ci/test/01_basic/basic_spec.lua b/src/civetweb/ci/test/01_basic/basic_spec.lua new file mode 100644 index 000000000..cf3b3007b --- /dev/null +++ b/src/civetweb/ci/test/01_basic/basic_spec.lua @@ -0,0 +1,35 @@ +civet = require "ci/test/civet" +local curl = require "cURL" + +describe("civetweb basic", function() + + setup(function() + civet.start() + end) + + teardown(function() + civet.stop() + end) + + + it("should serve a simple get request", function() + + local out = "" + function capture(str) + out = out .. str + end + + local c = curl.easy() + :setopt_url('http://localhost:' .. civet.port .. "/") + :setopt_writefunction(capture) + :perform() + :close() + + --print('rescode:' .. c.getinfo(curl.INFO_RESPONSE_CODE)) + + assert.are.equal('Index of', string.match(out, 'Index of')) + assert.are.equal('01_basic_test_dir', string.match(out, '01_basic_test_dir')) + assert.are.equal('01_basic_test_file', string.match(out, '01_basic_test_file')) + end) + +end) diff --git a/src/civetweb/ci/test/01_basic/docroot/01_basic_test_dir/git_keep_empty_dir b/src/civetweb/ci/test/01_basic/docroot/01_basic_test_dir/git_keep_empty_dir new file mode 100644 index 000000000..e69de29bb diff --git a/src/civetweb/ci/test/01_basic/docroot/01_basic_test_file b/src/civetweb/ci/test/01_basic/docroot/01_basic_test_file new file mode 100644 index 000000000..e69de29bb diff --git a/src/civetweb/ci/test/README.md b/src/civetweb/ci/test/README.md new file mode 100644 index 000000000..fdbecbe0e --- /dev/null +++ b/src/civetweb/ci/test/README.md @@ -0,0 +1,34 @@ +== Travis CI Tests + +Travis is a service which will build your project when you commit or get pull requests on Github. + +I have fixed and extended the travis configuration to build on the new sudo-less docker infrastructure. + +=== CI Process + +* On Check-in or Pull Requests clone the repo +* Run make WITH_LUA=1 WITH_DEBUG=1 WITH_IPV6=1 WITH_WEBSOCKET=1 +* Build a standalone lua installation (seperate from civetweb or the OS) +* Build LuaRocks in standalone installation +* Install a few rocks into the standalone installation +* Start the test script + +=== test/ci_tests/01_basic/basic_spec.lua + +On the initial checkin, there is only one test which demonstrates: + +* reliably starting civetweb server on travis infrastructure +* waiting (polling) with lua.socket to establish the server is up and running +* using libcurl via lua to test that files in the specified docroot are available +* kill the civetweb server process +* waiting (polling) the server port to see that the server has freed it + +=== Adding Tests + +* Create a directory under ci_tests +* Add a spec file, so now we have ci_tests/02_my_awesome_test/awesome_spec.lua +* Any file under ci_tests which ends in _spec.lua will be automatically run +* Check out the 'busted' and lua-curl3 docs for more info +* https://github.com/Lua-cURL/Lua-cURLv3 +* http://olivinelabs.com/busted/ + diff --git a/src/civetweb/ci/test/civet.lua b/src/civetweb/ci/test/civet.lua new file mode 100644 index 000000000..19a6848f5 --- /dev/null +++ b/src/civetweb/ci/test/civet.lua @@ -0,0 +1,42 @@ +socket = require "socket" + +local civet = {} + +-- default params +civet.port=12345 +civet.max_retry=100 +civet.start_delay=0.1 + +function civet.start(docroot) + -- TODO: use a property + docroot = docroot or 'ci/test/01_basic/docroot' + assert(io.popen('./civetweb' + .. " -listening_ports " .. civet.port + .. " -document_root " .. docroot + .. " > /dev/null 2>&1 &" + )) + -- wait until the server answers + for i=1,civet.max_retry do + local s = socket.connect('127.0.0.1', civet.port) + if s then + s:close() + break + end + socket.select(nil, nil, civet.start_delay) -- sleep + end +end + +function civet.stop() + os.execute('killall civetweb') + -- wait until the server port closes + for i=1,civet.max_retry do + local s = socket.connect('127.0.0.1', civet.port) + if not s then + break + end + s:close() + socket.select(nil, nil, civet.start_delay) -- sleep + end +end + +return civet diff --git a/src/civetweb/ci/travis/install_rocks.sh b/src/civetweb/ci/travis/install_rocks.sh new file mode 100755 index 000000000..739248b84 --- /dev/null +++ b/src/civetweb/ci/travis/install_rocks.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +set -ev + +source ci/travis/lua_env.sh + +# add any rocks required for ci_tests to this list +# lua-curl depends on a libcurl development package (i.e. libcurl4-openssl-dev) +ROCKS=(lua-curl busted) + +for ROCK in ${ROCKS[*]} +do + $LUAROCKS install $ROCK +done + diff --git a/src/civetweb/ci/travis/lua_env.sh b/src/civetweb/ci/travis/lua_env.sh new file mode 100755 index 000000000..dd742e911 --- /dev/null +++ b/src/civetweb/ci/travis/lua_env.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +LUAROCKS=ci/lua/bin/luarocks +eval $($LUAROCKS path --bin) + diff --git a/src/civetweb/ci/travis/platform.sh b/src/civetweb/ci/travis/platform.sh new file mode 100755 index 000000000..4a3af0d48 --- /dev/null +++ b/src/civetweb/ci/travis/platform.sh @@ -0,0 +1,15 @@ +if [ -z "$PLATFORM" ]; then + PLATFORM=$TRAVIS_OS_NAME; +fi + +if [ "$PLATFORM" == "osx" ]; then + PLATFORM="macosx"; +fi + +if [ -z "$PLATFORM" ]; then + if [ "$(uname)" == "Linux" ]; then + PLATFORM="linux"; + else + PLATFORM="macosx"; + fi; +fi diff --git a/src/civetweb/ci/travis/run_ci_tests.sh b/src/civetweb/ci/travis/run_ci_tests.sh new file mode 100755 index 000000000..16c2cc067 --- /dev/null +++ b/src/civetweb/ci/travis/run_ci_tests.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +set -ev + +source ci/travis/lua_env.sh +busted -o TAP ci/test/ + + diff --git a/src/civetweb/ci/travis/setup_lua.sh b/src/civetweb/ci/travis/setup_lua.sh new file mode 100755 index 000000000..8e1b324ea --- /dev/null +++ b/src/civetweb/ci/travis/setup_lua.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env /bash +set -ev + +# this script installs a lua / luarocks environment in .travis/lua +# this is necessary because travis docker architecture (the fast way) +# does not permit sudo, and does not contain a useful lua installation + +# After this script is finished, you can configure your environment to +# use it by sourcing lua_env.sh + +source ci/travis/platform.sh + +# The current versions when this script was written +LUA_VERSION=5.2.4 +LUAROCKS_VERSION=2.2.2 + +# directory where this script is located +SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) + +# civetweb base dir +PROJECT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )"/../.. && pwd ) + +# fetch and unpack lua src +cd $SCRIPT_DIR +LUA_BASE=lua-$LUA_VERSION +rm -rf $LUA_BASE +curl http://www.lua.org/ftp/$LUA_BASE.tar.gz | tar zx + +# build lua +cd $LUA_BASE +make $PLATFORM +make local + +# mv built lua install to target Lua dir +LUA_DIR=$PROJECT_DIR/ci/lua +rm -rf $LUA_DIR +mv $SCRIPT_DIR/$LUA_BASE/install $LUA_DIR + +# add to path required by luarocks installer +export PATH=$LUA_DIR/bin:$PATH + + +# fetch and unpack luarocks +cd $SCRIPT_DIR +LUAROCKS_BASE=luarocks-$LUAROCKS_VERSION +rm -rf ${LUAROCKS_BASE} +LUAROCKS_URL=http://luarocks.org/releases/${LUAROCKS_BASE}.tar.gz +# -L because it's a 302 redirect +curl -L $LUAROCKS_URL | tar xzp +cd $LUAROCKS_BASE + +# build luarocks +./configure --prefix=$LUA_DIR +make build +make install + +# cleanup source dirs +cd $SCRIPT_DIR +rm -rf $LUAROCKS_BASE +rm -rf $LUA_BASE + diff --git a/src/civetweb/cmake/AddCCompilerFlag.cmake b/src/civetweb/cmake/AddCCompilerFlag.cmake new file mode 100644 index 000000000..f5550fa74 --- /dev/null +++ b/src/civetweb/cmake/AddCCompilerFlag.cmake @@ -0,0 +1,38 @@ +# - Adds a compiler flag if it is supported by the compiler +# +# This function checks that the supplied compiler flag is supported and then +# adds it to the corresponding compiler flags +# +# add_c_compiler_flag( []) +# +# - Example +# +# include(AddCCompilerFlag) +# add_c_compiler_flag(-Wall) +# add_c_compiler_flag(-no-strict-aliasing RELEASE) +# Requires CMake 2.6+ + +if(__add_c_compiler_flag) + return() +endif() +set(__add_c_compiler_flag INCLUDED) + +include(CheckCCompilerFlag) + +function(add_c_compiler_flag FLAG) + string(TOUPPER "HAVE_C_FLAG_${FLAG}" SANITIZED_FLAG) + string(REPLACE "+" "X" SANITIZED_FLAG ${SANITIZED_FLAG}) + string(REGEX REPLACE "[^A-Za-z_0-9]" "_" SANITIZED_FLAG ${SANITIZED_FLAG}) + string(REGEX REPLACE "_+" "_" SANITIZED_FLAG ${SANITIZED_FLAG}) + set(CMAKE_REQUIRED_FLAGS "${FLAG}") + check_c_compiler_flag("" ${SANITIZED_FLAG}) + if(${SANITIZED_FLAG}) + set(VARIANT ${ARGV1}) + if(ARGV1) + string(REGEX REPLACE "[^A-Za-z_0-9]" "_" VARIANT "${VARIANT}") + string(TOUPPER "_${VARIANT}" VARIANT) + endif() + set(CMAKE_C_FLAGS${VARIANT} "${CMAKE_C_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE) + endif() +endfunction() + diff --git a/src/civetweb/cmake/AddCXXCompilerFlag.cmake b/src/civetweb/cmake/AddCXXCompilerFlag.cmake new file mode 100644 index 000000000..5e58c6de4 --- /dev/null +++ b/src/civetweb/cmake/AddCXXCompilerFlag.cmake @@ -0,0 +1,38 @@ +# - Adds a compiler flag if it is supported by the compiler +# +# This function checks that the supplied compiler flag is supported and then +# adds it to the corresponding compiler flags +# +# add_cxx_compiler_flag( []) +# +# - Example +# +# include(AddCXXCompilerFlag) +# add_cxx_compiler_flag(-Wall) +# add_cxx_compiler_flag(-no-strict-aliasing RELEASE) +# Requires CMake 2.6+ + +if(__add_cxx_compiler_flag) + return() +endif() +set(__add_cxx_compiler_flag INCLUDED) + +include(CheckCXXCompilerFlag) + +function(add_cxx_compiler_flag FLAG) + string(TOUPPER "HAVE_CXX_FLAG_${FLAG}" SANITIZED_FLAG) + string(REPLACE "+" "X" SANITIZED_FLAG ${SANITIZED_FLAG}) + string(REGEX REPLACE "[^A-Za-z_0-9]" "_" SANITIZED_FLAG ${SANITIZED_FLAG}) + string(REGEX REPLACE "_+" "_" SANITIZED_FLAG ${SANITIZED_FLAG}) + set(CMAKE_REQUIRED_FLAGS "${FLAG}") + check_cxx_compiler_flag("" ${SANITIZED_FLAG}) + if(${SANITIZED_FLAG}) + set(VARIANT ${ARGV1}) + if(ARGV1) + string(REGEX REPLACE "[^A-Za-z_0-9]" "_" VARIANT "${VARIANT}") + string(TOUPPER "_${VARIANT}" VARIANT) + endif() + set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE) + endif() +endfunction() + diff --git a/src/civetweb/cmake/DetermineTargetArchitecture.cmake b/src/civetweb/cmake/DetermineTargetArchitecture.cmake new file mode 100644 index 000000000..7d18213f7 --- /dev/null +++ b/src/civetweb/cmake/DetermineTargetArchitecture.cmake @@ -0,0 +1,47 @@ +# - Determines the target architecture of the compilation +# +# This function checks the architecture that will be built by the compiler +# and sets a variable to the architecture +# +# determine_target_architecture() +# +# - Example +# +# include(DetermineTargetArchitecture) +# determine_target_architecture(PROJECT_NAME_ARCHITECTURE) + +if(__determine_target_architecture) + return() +endif() +set(__determine_target_architecture INCLUDED) + +function(determine_target_architecture FLAG) + if (MSVC) + if("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "X86") + set(ARCH "i686") + elseif("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "x64") + set(ARCH "x86_64") + elseif("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "ARM") + set(ARCH "arm") + else() + message(FATAL_ERROR "Failed to determine the MSVC target architecture: ${MSVC_C_ARCHITECTURE_ID}") + endif() + else() + execute_process( + COMMAND ${CMAKE_C_COMPILER} -dumpmachine + RESULT_VARIABLE RESULT + OUTPUT_VARIABLE ARCH + ERROR_QUIET + ) + if (RESULT) + message(FATAL_ERROR "Failed to determine target architecture triplet: ${RESULT}") + endif() + string(REGEX MATCH "([^-]+).*" ARCH_MATCH ${ARCH}) + if (NOT CMAKE_MATCH_1 OR NOT ARCH_MATCH) + message(FATAL_ERROR "Failed to match the target architecture triplet: ${ARCH}") + endif() + set(ARCH ${CMAKE_MATCH_1}) + endif() + message(STATUS "Target architecture - ${ARCH}") + set(FLAG ${ARCH} PARENT_SCOPE) +endfunction() diff --git a/src/civetweb/cmake/FindLibDl.cmake b/src/civetweb/cmake/FindLibDl.cmake new file mode 100644 index 000000000..c018d9239 --- /dev/null +++ b/src/civetweb/cmake/FindLibDl.cmake @@ -0,0 +1,46 @@ +#.rst: +# FindLibDl +# -------- +# +# Find the native realtime includes and library. +# +# IMPORTED Targets +# ^^^^^^^^^^^^^^^^ +# +# This module defines :prop_tgt:`IMPORTED` target ``LIBDL::LIBDL``, if +# LIBDL has been found. +# +# Result Variables +# ^^^^^^^^^^^^^^^^ +# +# This module defines the following variables: +# +# :: +# +# LIBDL_INCLUDE_DIRS - where to find dlfcn.h, etc. +# LIBDL_LIBRARIES - List of libraries when using libdl. +# LIBDL_FOUND - True if dynamic linking library found. +# +# Hints +# ^^^^^ +# +# A user may set ``LIBDL_ROOT`` to a library installation root to tell this +# module where to look. + +find_path(LIBDL_INCLUDE_DIRS + NAMES dlfcn.h + PATHS ${LIBDL_ROOT}/include/ +) +find_library(LIBDL_LIBRARIES dl) +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(LibDl DEFAULT_MSG LIBDL_LIBRARIES LIBDL_INCLUDE_DIRS) +mark_as_advanced(LIBDL_INCLUDE_DIRS LIBDL_LIBRARIES) + +if(LIBDL_FOUND) + if(NOT TARGET LIBDL::LIBDL) + add_library(LIBDL::LIBDL UNKNOWN IMPORTED) + set_target_properties(LIBDL::LIBDL PROPERTIES + IMPORTED_LOCATION "${LIBDL_LIBRARIES}" + INTERFACE_INCLUDE_DIRECTORIES "${LIBDL_INCLUDE_DIRS}") + endif() +endif() diff --git a/src/civetweb/cmake/FindLibM.cmake b/src/civetweb/cmake/FindLibM.cmake new file mode 100644 index 000000000..9f42aa493 --- /dev/null +++ b/src/civetweb/cmake/FindLibM.cmake @@ -0,0 +1,47 @@ +#.rst: +# FindLibM +# -------- +# +# Find the native realtime includes and library. +# +# IMPORTED Targets +# ^^^^^^^^^^^^^^^^ +# +# This module defines :prop_tgt:`IMPORTED` target ``LIBM::LIBM``, if +# LIBM has been found. +# +# Result Variables +# ^^^^^^^^^^^^^^^^ +# +# This module defines the following variables: +# +# :: +# +# LIBM_INCLUDE_DIRS - where to find math.h, etc. +# LIBM_LIBRARIES - List of libraries when using libm. +# LIBM_FOUND - True if math library found. +# +# Hints +# ^^^^^ +# +# A user may set ``LIBM_ROOT`` to a math library installation root to tell this +# module where to look. + +find_path(LIBM_INCLUDE_DIRS + NAMES math.h + PATHS /usr/include /usr/local/include /usr/local/bic/include + NO_DEFAULT_PATH +) +find_library(LIBM_LIBRARIES m) +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(LibM DEFAULT_MSG LIBM_LIBRARIES LIBM_INCLUDE_DIRS) +mark_as_advanced(LIBM_INCLUDE_DIRS LIBM_LIBRARIES) + +if(LIBM_FOUND) + if(NOT TARGET LIBM::LIBM) + add_library(LIBM::LIBM UNKNOWN IMPORTED) + set_target_properties(LIBM::LIBM PROPERTIES + IMPORTED_LOCATION "${LIBM_LIBRARIES}" + INTERFACE_INCLUDE_DIRECTORIES "${LIBM_INCLUDE_DIRS}") + endif() +endif() diff --git a/src/civetweb/cmake/FindLibRt.cmake b/src/civetweb/cmake/FindLibRt.cmake new file mode 100644 index 000000000..c496edf86 --- /dev/null +++ b/src/civetweb/cmake/FindLibRt.cmake @@ -0,0 +1,46 @@ +#.rst: +# FindLibRt +# -------- +# +# Find the native realtime includes and library. +# +# IMPORTED Targets +# ^^^^^^^^^^^^^^^^ +# +# This module defines :prop_tgt:`IMPORTED` target ``LIBRT::LIBRT``, if +# LIBRT has been found. +# +# Result Variables +# ^^^^^^^^^^^^^^^^ +# +# This module defines the following variables: +# +# :: +# +# LIBRT_INCLUDE_DIRS - where to find time.h, etc. +# LIBRT_LIBRARIES - List of libraries when using librt. +# LIBRT_FOUND - True if realtime library found. +# +# Hints +# ^^^^^ +# +# A user may set ``LIBRT_ROOT`` to a realtime installation root to tell this +# module where to look. + +find_path(LIBRT_INCLUDE_DIRS + NAMES time.h + PATHS ${LIBRT_ROOT}/include/ +) +find_library(LIBRT_LIBRARIES rt) +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(LibRt DEFAULT_MSG LIBRT_LIBRARIES LIBRT_INCLUDE_DIRS) +mark_as_advanced(LIBRT_INCLUDE_DIRS LIBRT_LIBRARIES) + +if(LIBRT_FOUND) + if(NOT TARGET LIBRT::LIBRT) + add_library(LIBRT::LIBRT UNKNOWN IMPORTED) + set_target_properties(LIBRT::LIBRT PROPERTIES + IMPORTED_LOCATION "${LIBRT_LIBRARIES}" + INTERFACE_INCLUDE_DIRECTORIES "${LIBRT_INCLUDE_DIRS}") + endif() +endif() diff --git a/src/civetweb/cmake/FindLibSubunit.cmake b/src/civetweb/cmake/FindLibSubunit.cmake new file mode 100644 index 000000000..b45344fbe --- /dev/null +++ b/src/civetweb/cmake/FindLibSubunit.cmake @@ -0,0 +1,46 @@ +#.rst: +# FindLibSubunit +# -------- +# +# Find the native realtime includes and library. +# +# IMPORTED Targets +# ^^^^^^^^^^^^^^^^ +# +# This module defines :prop_tgt:`IMPORTED` target ``LIBSUBUNIT::LIBSUBUNIT``, if +# LIBSUBUNIT has been found. +# +# Result Variables +# ^^^^^^^^^^^^^^^^ +# +# This module defines the following variables: +# +# :: +# +# LIBSUBUNIT_INCLUDE_DIRS - where to find subunit/child.h +# LIBSUBUNIT_LIBRARIES - List of libraries when using libsubunit. +# LIBSUBUNIT_FOUND - True if subunit library found. +# +# Hints +# ^^^^^ +# +# A user may set ``LIBSUBUNIT_ROOT`` to a subunit library installation root to tell this +# module where to look. + +find_path(LIBSUBUNIT_INCLUDE_DIRS + NAMES subunit/child.h + PATHS ${LIBSUBUNIT_ROOT}/include/ +) +find_library(LIBSUBUNIT_LIBRARIES subunit) +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(LibSubunit DEFAULT_MSG LIBSUBUNIT_LIBRARIES LIBSUBUNIT_INCLUDE_DIRS) +mark_as_advanced(LIBSUBUNIT_INCLUDE_DIRS LIBSUBUNIT_LIBRARIES) + +if(LIBSUBUNIT_FOUND) + if(NOT TARGET LIBSUBUNIT::LIBSUBUNIT) + add_library(LIBSUBUNIT::LIBSUBUNIT UNKNOWN IMPORTED) + set_target_properties(LIBSUBUNIT::LIBSUBUNIT PROPERTIES + IMPORTED_LOCATION "${LIBSUBUNIT_LIBRARIES}" + INTERFACE_INCLUDE_DIRECTORIES "${LIBSUBUNIT_INCLUDE_DIRS}") + endif() +endif() diff --git a/src/civetweb/cmake/FindWinSock.cmake b/src/civetweb/cmake/FindWinSock.cmake new file mode 100644 index 000000000..0bf355dec --- /dev/null +++ b/src/civetweb/cmake/FindWinSock.cmake @@ -0,0 +1,102 @@ +#.rst: +# FindWinSock +# -------- +# +# Find the native realtime includes and library. +# +# IMPORTED Targets +# ^^^^^^^^^^^^^^^^ +# +# This module defines :prop_tgt:`IMPORTED` target ``WINSOCK::WINSOCK``, if +# WINSOCK has been found. +# +# Result Variables +# ^^^^^^^^^^^^^^^^ +# +# This module defines the following variables: +# +# :: +# +# WINSOCK_INCLUDE_DIRS - where to find winsock.h, etc. +# WINSOCK_LIBRARIES - List of libraries when using librt. +# WINSOCK_FOUND - True if realtime library found. +# +# Hints +# ^^^^^ +# +# A user may set ``WINSOCK_ROOT`` to a realtime installation root to tell this +# module where to look. + +macro(REMOVE_DUPLICATE_PATHS LIST_VAR) + set(WINSOCK_LIST "") + foreach(PATH IN LISTS ${LIST_VAR}) + get_filename_component(PATH "${PATH}" REALPATH) + list(APPEND WINSOCK_LIST "${PATH}") + endforeach(PATH) + set(${LIST_VAR} ${WINSOCK_LIST}) + list(REMOVE_DUPLICATES ${LIST_VAR}) +endmacro(REMOVE_DUPLICATE_PATHS) + +set(WINSOCK_INCLUDE_PATHS "${WINSOCK_ROOT}/include/") +if(MINGW) + execute_process( + COMMAND ${CMAKE_C_COMPILER} -xc -E -v - + RESULT_VARIABLE RESULT + INPUT_FILE nul + ERROR_VARIABLE ERR + OUTPUT_QUIET + ) + if (NOT RESULT) + string(FIND "${ERR}" "#include <...> search starts here:" START) + string(FIND "${ERR}" "End of search list." END) + if (NOT ${START} EQUAL -1 AND NOT ${END} EQUAL -1) + math(EXPR START "${START} + 36") + math(EXPR END "${END} - 1") + math(EXPR LENGTH "${END} - ${START}") + string(SUBSTRING "${ERR}" ${START} ${LENGTH} WINSOCK_INCLUDE_PATHS) + string(REPLACE "\n " ";" WINSOCK_INCLUDE_PATHS "${WINSOCK_INCLUDE_PATHS}") + list(REVERSE WINSOCK_INCLUDE_PATHS) + endif() + endif() +endif() +remove_duplicate_paths(WINSOCK_INCLUDE_PATHS) + +set(WINSOCK_LIBRARY_PATHS "${WINSOCK_ROOT}/lib/") +if(MINGW) + execute_process( + COMMAND ${CMAKE_C_COMPILER} -print-search-dirs + RESULT_VARIABLE RESULT + OUTPUT_VARIABLE OUT + ERROR_QUIET + ) + if (NOT RESULT) + string(REGEX MATCH "libraries: =([^\r\n]*)" OUT "${OUT}") + list(APPEND WINSOCK_LIBRARY_PATHS "${CMAKE_MATCH_1}") + endif() +endif() +if (${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "AMD64" AND ${CMAKE_SIZEOF_VOID_P} EQUAL 4) + list(APPEND WINSOCK_LIBRARY_PATHS "C:/Windows/SysWOW64") +endif() +list(APPEND WINSOCK_LIBRARY_PATHS "C:/Windows/System32") +remove_duplicate_paths(WINSOCK_LIBRARY_PATHS) + +find_path(WINSOCK_INCLUDE_DIRS + NAMES winsock2.h + PATHS ${WINSOCK_INCLUDE_PATHS} +) +find_library(WINSOCK_LIBRARIES ws2_32 + PATHS ${WINSOCK_LIBRARY_PATHS} + NO_DEFAULT_PATH +) +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(WinSock DEFAULT_MSG WINSOCK_LIBRARIES WINSOCK_INCLUDE_DIRS) +mark_as_advanced(WINSOCK_INCLUDE_DIRS WINSOCK_LIBRARIES) + +if(WINSOCK_FOUND) + if(NOT TARGET WINSOCK::WINSOCK) + add_library(WINSOCK::WINSOCK UNKNOWN IMPORTED) + set_target_properties(WINSOCK::WINSOCK PROPERTIES + IMPORTED_LOCATION "${WINSOCK_LIBRARIES}" + INTERFACE_INCLUDE_DIRECTORIES "${WINSOCK_INCLUDE_DIRS}") + endif() +endif() diff --git a/src/civetweb/cmake/check/c82fe8888aacfe784476112edd3878256d2e30bc.patch b/src/civetweb/cmake/check/c82fe8888aacfe784476112edd3878256d2e30bc.patch new file mode 100644 index 000000000..e16ea1f5f --- /dev/null +++ b/src/civetweb/cmake/check/c82fe8888aacfe784476112edd3878256d2e30bc.patch @@ -0,0 +1,31 @@ +From c82fe8888aacfe784476112edd3878256d2e30bc Mon Sep 17 00:00:00 2001 +From: Joshua Boyd +Date: Wed, 23 Mar 2016 17:54:41 -0400 +Subject: [PATCH] Detect missing itimerspec on OSX. + +Set define to compiler accordingly. + +This fixes cmake on osx support. +--- + CMakeLists.txt | 8 ++++++++ + 1 file changed, 8 insertions(+) + +diff --git a/CMakeLists.txt b/CMakeLists.txt +index e271e31..1d413e8 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -193,6 +193,14 @@ if(NOT HAVE_SYS_TIME_H) + endif(MSVC) + endif(NOT HAVE_SYS_TIME_H) + ++# OSX has sys/time.h, but it still lacks itimerspec ++if(HAVE_SYS_TIME_H) ++ check_struct_member("struct itimerspec" it_value "sys/time.h" HAVE_STRUCT_ITIMERSPEC_IT_VALUE) ++ if(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE) ++ add_definitions(-DSTRUCT_ITIMERSPEC_DEFINITION_MISSING=1) ++ set(STRUCT_ITIMERSPEC_DEFINITION_MISSING 1) ++ endif(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE) ++endif(HAVE_SYS_TIME_H) + + ############################################################################### + # Check for integer types diff --git a/src/civetweb/cmake/check/patch.cmake b/src/civetweb/cmake/check/patch.cmake new file mode 100644 index 000000000..472d392b5 --- /dev/null +++ b/src/civetweb/cmake/check/patch.cmake @@ -0,0 +1,12 @@ +message(STATUS "Patching check ${VERSION} ${SOURCE_DIR}") + +# Patch checks for MinGW +# https://sourceforge.net/p/check/patches/53/ +set(CMAKE_LISTS_LOCATION "${SOURCE_DIR}/CMakeLists.txt") +file(READ ${CMAKE_LISTS_LOCATION} CMAKE_LISTS) +string(REGEX REPLACE + "(check_type_size\\((clock|clockid|timer)_t [A-Z_]+\\)[\r\n]+[^\r\n]+[\r\n]+[^\r\n]+[\r\n]+endif\\(NOT HAVE[A-Z_]+\\))" + "set(CMAKE_EXTRA_INCLUDE_FILES time.h)\n\\1\nunset(CMAKE_EXTRA_INCLUDE_FILES)" + CMAKE_LISTS "${CMAKE_LISTS}") +file(WRITE ${CMAKE_LISTS_LOCATION} "${CMAKE_LISTS}") +message(STATUS "Patched ${CMAKE_LISTS_LOCATION}") diff --git a/src/civetweb/contrib/buildroot/Config.in b/src/civetweb/contrib/buildroot/Config.in new file mode 100644 index 000000000..2334fdfe8 --- /dev/null +++ b/src/civetweb/contrib/buildroot/Config.in @@ -0,0 +1,26 @@ +config BR2_PACKAGE_CIVETWEB + bool "civetweb" + depends on BR2_TOOLCHAIN_HAS_THREADS + help + Full featured embedded web server with Lua support. + + https://sourceforge.net/projects/civetweb + +if BR2_PACKAGE_CIVETWEB + +config BR2_CIVETWEB_WITH_LUA + bool "enable Lua support" + # required by the bundled Sqlite3 and Lua code + depends on BR2_LARGEFILE + help + Enable Lua support in Civetweb. Note that this will use a + version of Lua and Sqlite bundled within the Civetweb + sources, and not the packages from Buildroot. + +comment "Lua support requires largefile support in toolchain" + depends on !BR2_LARGEFILE + +endif + +comment "civetweb requires a toolchain with PTHREAD support" + depends on !BR2_TOOLCHAIN_HAS_THREADS diff --git a/src/civetweb/contrib/buildroot/civetweb.mk b/src/civetweb/contrib/buildroot/civetweb.mk new file mode 100644 index 000000000..8b9b7de0b --- /dev/null +++ b/src/civetweb/contrib/buildroot/civetweb.mk @@ -0,0 +1,55 @@ +################################################################################ +# +# civetweb +# +################################################################################ + +CIVETWEB_VERSION = 1.10 +CIVETWEB_SITE = http://github.com/civetweb/civetweb/tarball/v$(CIVETWEB_VERSION) +CIVETWEB_LICENSE = MIT +CIVETWEB_LICENSE_FILES = LICENSE.md + +CIVETWEB_CONF_OPT = TARGET_OS=LINUX +CIVETWEB_COPT = $(TARGET_CFLAGS) -DHAVE_POSIX_FALLOCATE=0 +CIVETWEB_LDFLAGS = $(TARGET_LDFLAGS) +CIVETWEB_SYSCONFDIR = /etc +CIVETWEB_HTMLDIR = /var/www + +ifneq ($(BR2_LARGEFILE),y) + CIVETWEB_COPT += -DSQLITE_DISABLE_LFS +endif + +ifeq ($(BR2_INET_IPV6),y) + CIVETWEB_CONF_OPT += WITH_IPV6=1 +endif + +ifeq ($(BR2_CIVETWEB_WITH_LUA),y) + CIVETWEB_CONF_OPT += WITH_LUA=1 +endif + +ifeq ($(BR2_PACKAGE_OPENSSL),y) + CIVETWEB_COPT += -DNO_SSL_DL -lcrypt -lssl + CIVETWEB_DEPENDENCIES += openssl +else + CIVETWEB_COPT += -DNO_SSL +endif + +define CIVETWEB_BUILD_CMDS + $(MAKE) CC="$(TARGET_CC)" -C $(@D) build \ + $(CIVETWEB_CONF_OPT) \ + COPT="$(CIVETWEB_COPT)" +endef + +define CIVETWEB_INSTALL_TARGET_CMDS + $(MAKE) CC="$(TARGET_CC)" -C $(@D) install \ + DOCUMENT_ROOT="$(CIVETWEB_HTMLDIR)" \ + CONFIG_FILE2="$(CIVETWEB_SYSCONFDIR)/civetweb.conf" \ + HTMLDIR="$(TARGET_DIR)$(CIVETWEB_HTMLDIR)" \ + SYSCONFDIR="$(TARGET_DIR)$(CIVETWEB_SYSCONFDIR)" \ + PREFIX="$(TARGET_DIR)/usr" \ + $(CIVETWEB_CONF_OPT) \ + COPT='$(CIVETWEB_COPT)' +endef + +$(eval $(generic-package)) + diff --git a/src/civetweb/distribution/arch/PKGBUILD.git.example b/src/civetweb/distribution/arch/PKGBUILD.git.example new file mode 100644 index 000000000..7102bcb34 --- /dev/null +++ b/src/civetweb/distribution/arch/PKGBUILD.git.example @@ -0,0 +1,42 @@ +# An example PKGBUILD script for Civetweb upstream, git version +# Rename to PKGBUILD to build via makepkg +_pkgname=civetweb +pkgname=$_pkgname-git +pkgver=v1.4.24.g73c40b6 +pkgrel=1 +pkgdesc="Small and quick-to-use web server; https/php/cgi support; MIT license - git development version" +arch=('i686' 'x86_64') +url="http://sourceforge.net/p/civetweb/" +license=('MIT') +groups=() +depends=() +makedepends=('git sed') +optdepends=('php-cgi: for php support') +provides=("$_pkgname") +conflicts=("$_pkgname") +backup=("etc/$_pkgname/$_pkgname.conf") +source=("$_pkgname::git+https://github.com/civetweb/civetweb.git") +md5sums=('SKIP') + +pkgver() { + cd "$srcdir/$_pkgname" + git describe --tags | sed 's|-|.|g' +} + +build() { + cd "$srcdir/$_pkgname" + make build WITH_IPV6=1 +} + +package() { + cd "$srcdir/$_pkgname" + make install PREFIX="$pkgdir/usr" SYSCONFDIR="$pkgdir/etc/local/$_pkgname" + + install -Dm644 "$srcdir/$_pkgname/distribution/arch/$_pkgname.service" "$pkgdir/usr/lib/systemd/system/$_pkgname.service" + + sed -i "s/^document_root [^\n]*/document_root \/srv\/http/g" "$pkgdir/etc/local/$_pkgname/$_pkgname.conf" + sed -i "s/^# access_log_file/access_log_file \/var\/log\/$_pkgname\/access.log/g" "$pkgdir/etc/local/$_pkgname/$_pkgname.conf" + sed -i "s/^# error_log_file/access_log_file \/var\/log\/$_pkgname\/error.log/g" "$pkgdir/etc/local/$_pkgname/$_pkgname.conf" +} + +# vim:set ts=2 sw=2 et: diff --git a/src/civetweb/distribution/arch/civetweb.service b/src/civetweb/distribution/arch/civetweb.service new file mode 100644 index 000000000..5327b6caa --- /dev/null +++ b/src/civetweb/distribution/arch/civetweb.service @@ -0,0 +1,9 @@ +[Unit] +Description=Civetweb httpd +After=syslog.target network.target remote-fs.target nss-lookup.target + +[Service] +ExecStart=/usr/local/bin/civetweb /usr/local/etc/civetweb/civetweb.conf + +[Install] +WantedBy=multi-user.target diff --git a/src/civetweb/docs/APIReference.md b/src/civetweb/docs/APIReference.md new file mode 100644 index 000000000..58c2faf38 --- /dev/null +++ b/src/civetweb/docs/APIReference.md @@ -0,0 +1,134 @@ +# CivetWeb API Reference + +CivetWeb is often used as HTTP and HTTPS library inside a larger application. +A C API is available to integrate the CivetWeb functionality in a larger +codebase. A C++ wrapper is also available, although it is not guaranteed +that all functionality available through the C API can also be accessed +from C++. This document describes the public C API. Basic usage examples of +the API can be found in [Embedding.md](Embedding.md), as well as in the +examples directory. + +## Macros + +| Macro | Description | +| :--- | :--- | +| **`CIVETWEB_VERSION`** | The current version of the software as a string with the major and minor version number seperated with a dot. For version 1.9, this string will have the value "1.9", for thw first patch of this version "1.9.1". | +| **`CIVETWEB_VERSION_MAJOR`** | The current major version as number, e.g., (1) for version 1.9. | +| **`CIVETWEB_VERSION_MINOR`** | The current minor version as number, e.g., (9) for version 1.9. | +| **`CIVETWEB_VERSION_PATCH`** | The current patch version as number, e.g., (0) for version 1.9 or (1) for version 1.9.1. | + +## Handles + +* `struct mg_context *` +Handle for one instance of the HTTP(S) server. +All functions using `const struct mg_context *` as an argument do not modify a running server instance, but just query information. Functions using a non-const `struct mg_context *` as an argument may modify a server instance (e.g., register a new URI, stop the server, ...). + +* `struct mg_connection *` +Handle for one individual client-server connection. +Functions working with `const struct mg_connection *` operate on data already known to the server without reading data from or sending data to the client. Callbacks using a `const struct mg_connection *` argument are supposed to not call functions from the `mg_read()` and `mg_write()` family. To support a correct application, reading and writing functions require a non-const `struct mg_connection *` connection handle. + +The content of both structures is not defined in the interface - they are only used as opaque pointers (handles). + +## Structures + +* [`struct mg_client_cert;`](api/mg_client_cert.md) +* [`struct mg_client_options;`](api/mg_client_options.md) +* [`struct mg_callbacks;`](api/mg_callbacks.md) +* [`struct mg_form_data_handler;`](api/mg_form_data_handler.md) +* [`struct mg_header;`](api/mg_header.md) +* [`struct mg_option;`](api/mg_option.md) +* [`struct mg_request_info;`](api/mg_request_info.md) +* [`struct mg_server_ports;`](api/mg_server_ports.md) + + +## Library API Functions + +* [`mg_init_library( feature );`](api/mg_init_library.md) +* [`mg_exit_library( feature );`](api/mg_exit_library.md) + +* [`mg_check_feature( feature );`](api/mg_check_feature.md) +* [`mg_version();`](api/mg_version.md) + + +## Server API Functions + +* [`mg_start( callbacks, user_data, options );`](api/mg_start.md) +* [`mg_stop( ctx );`](api/mg_stop.md) + +* [`mg_get_builtin_mime_type( file_name );`](api/mg_get_builtin_mime_type.md) +* [`mg_get_option( ctx, name );`](api/mg_get_option.md) +* [`mg_get_server_ports( ctx, size, ports );`](api/mg_get_server_ports.md) +* [`mg_get_user_data( ctx );`](api/mg_get_user_data.md) +* [`mg_set_auth_handler( ctx, uri, handler, cbdata );`](api/mg_set_auth_handler.md) +* [`mg_set_request_handler( ctx, uri, handler, cbdata );`](api/mg_set_request_handler.md) +* [`mg_set_websocket_handler( ctx, uri, connect_handler, ready_handler, data_handler, close_handler, cbdata );`](api/mg_set_websocket_handler.md) + +* [`mg_lock_context( ctx );`](api/mg_lock_context.md) +* [`mg_unlock_context( ctx );`](api/mg_unlock_context.md) + +* [`mg_get_context( conn );`](api/mg_get_context.md) + +* [`mg_send_http_error( conn, status_code, fmt, ... );`](api/mg_send_http_error.md) + +* [`mg_send_digest_access_authentication_request( conn, realm );`](api/mg_send_digest_access_authentication_request.md) +* [`mg_check_digest_access_authentication( conn, realm, filename );`](api/mg_check_digest_access_authentication.md) +* [`mg_modify_passwords_file( passwords_file_name, realm, user, password );`](api/mg_modify_passwords_file.md) + + +## Client API Functions + +* [`mg_connect_client( host, port, use_ssl, error_buffer, error_buffer_size );`](api/mg_connect_client.md) +* [`mg_connect_websocket_client( host, port, use_ssl, error_buffer, error_buffer_size, path, origin, data_func, close_func, user_data);`](api/mg_connect_websocket_client.md) +* [`mg_websocket_client_write( conn, opcode, data, data_len );`](api/mg_websocket_client_write.md) + +* [`mg_download( host, port, use_ssl, error_buffer, error_buffer_size, fmt, ... );`](api/mg_download.md) + + +## Common API Functions + +* [`mg_close_connection( conn );`](api/mg_close_connection.md) +* [`mg_cry( conn, fmt, ... );`](api/mg_cry.md) + +* [`mg_get_cookie( cookie, var_name, buf, buf_len );`](api/mg_get_cookie.md) +* [`mg_get_header( conn, name );`](api/mg_get_header.md) +* [`mg_get_request_info( conn );`](api/mg_get_request_info.md) +* [`mg_get_response( conn, ebuf, ebuf_len, timeout );`](api/mg_get_response.md) +* [`mg_get_response_code_text( conn, response_code );`](api/mg_get_response_code_text.md) +* [`mg_get_user_connection_data( conn );`](api/mg_get_user_connection_data.md) +* [`mg_get_valid_options();`](api/mg_get_valid_options.md) +* [`mg_get_var( data, data_len, var_name, dst, dst_len );`](api/mg_get_var.md) +* [`mg_get_var2( data, data_len, var_name, dst, dst_len, occurrence );`](api/mg_get_var2.md) +* [`mg_handle_form_request( conn, fdh );`](api/mg_handle_form_request.md) +* [`mg_lock_connection( conn );`](api/mg_lock_connection.md) +* [`mg_md5( buf, ... );`](api/mg_md5.md) +* [`mg_printf( conn, fmt, ... );`](api/mg_printf.md) +* [`mg_read( conn, buf, len );`](api/mg_read.md) +* [`mg_send_chunk( conn, buf, len );`](api/mg_send_chunk.md) +* [`mg_send_file( conn, path );`](api/mg_send_file.md) +* [`mg_send_mime_file( conn, path, mime_type );`](api/mg_send_mime_file.md) +* [`mg_send_mime_file2( conn, path, mime_type, additional_headers );`](api/mg_send_mime_file2.md) +* [`mg_set_user_connection_data( conn, data );`](api/mg_set_user_connection_data.md) +* [`mg_start_thread( f, p );`](api/mg_start_thread.md) +* [`mg_store_body( conn, path );`](api/mg_store_body.md) +* [`mg_strcasecmp( s1, s2 );`](api/mg_strcasecmp.md) +* [`mg_strncasecmp( s1, s2, len );`](api/mg_strncasecmp.md) +* [`mg_unlock_connection( conn );`](api/mg_unlock_connection.md) +* [`mg_url_decode( src, src_len, dst, dst_len, is_form_url_encoded );`](api/mg_url_decode.md) +* [`mg_url_encode( src, dst, dst_len );`](api/mg_url_encode.md) +* [`mg_websocket_write( conn, opcode, data, data_len );`](api/mg_websocket_write.md) +* [`mg_write( conn, buf, len );`](api/mg_write.md) + + +## Diagnosis Functions + +* [`mg_get_system_info( buffer, buf_len );`](api/mg_get_system_info.md) +* [`mg_get_context_info( ctx, buffer, buf_len );`](api/mg_get_context_info.md) +* [`mg_get_connection_info( ctx, idx, buffer, buf_len );`](api/mg_get_context_info.md) + + +## Deprecated: + +* [~~`mg_get_valid_option_names();`~~](api/mg_get_valid_option_names.md) +* [~~`mg_upload( conn, destination_dir );`~~](api/mg_upload.md) + + diff --git a/src/civetweb/docs/Building.md b/src/civetweb/docs/Building.md new file mode 100644 index 000000000..9d7b56ff7 --- /dev/null +++ b/src/civetweb/docs/Building.md @@ -0,0 +1,216 @@ +Building CivetWeb +========= + +This guide covers the build instructions for the stand-alone web server. +See [Embedding.md](https://github.com/civetweb/civetweb/blob/master/docs/Embedding.md) for information on extending an existing C or C++ application. A brief overview of the source code files can be found in [Embedding.md](https://github.com/civetweb/civetweb/blob/master/docs/Embedding.md) as well. + +#### Where to get the source code? + +The latest version can be found at +https://github.com/civetweb/civetweb + +Released versions can be found at +https://github.com/civetweb/civetweb/releases + + +Building for Windows +--------- + +#### Using Visual Studio + +Open the *VS/civetweb.sln* in Visual Studio. +To include SSL support, you may have to add an extra library for the cryptography support. You might wish to use yaSSL. However, it is GPL licensed or uses a commercial license. See [yaSSL.md](https://github.com/civetweb/civetweb/blob/master/docs/yaSSL.md) for more information. +Alternatively, you might wish to use OpenSSL. See [OpenSSL.md](https://github.com/civetweb/civetweb/blob/master/docs/OpenSSL.md) for more information. + +#### Using MinGW-w64 or TDM-GCC +In the start menu locate and run the "Run terminal" batch file. For TDM-GCC this is named "MinGW Command Prompt". +Navigate to the civetweb sources directory and run: +``` +mingw32-make CC=gcc +``` + +#### Using Qt Creator +Open the Qt Designer project in the Qt folder + +#### Using CMake +Except for the components in the `third_party` folder (e.g., Lua and Duktape), CivetWeb can also be built with CMake. +CMake can be used for all supported operating systems. + + +Building for Linux, BSD, and OSX +--------- + +## Using Make + +``` +make help +``` +Get a list of all supported make option + +``` +make build +``` +compile the code + +``` +make install +``` +Install on the system, Linux only. + +``` +make lib WITH_CPP=1 WITH_IPV6=1 +make clean slib WITH_CPP=1 WITH_LUA=1 WITH_WEBSOCKET=1 +``` +Build the static and shared libraries. +The *WITH_CPP* make option is to include the CivetServer class. +The additional make options configure the library just as it would the application. + +The *slib* option should be done on a separate clean build as position +independent code (PIC) is required for it. Trying to run it after +building the static library or the server will result in a link error. + +``` +make clean +``` +Clean up files generated during the build + +## Setting build options + +Make options can be set on the command line with the make command like so. +``` +make build WITH_LUA=1 +``` + + +| Make Options | Description | +| ------------------------- | ----------------------------------------- | +| WITH_LUA=1 | build with Lua support | +| WITH_DUKTAPE=1 | build with server-side JavaScript support | +| WITH_DEBUG=1 | build with GDB debug support | +| WITH_IPV6=1 | with IPV6 support | +| WITH_WEBSOCKET=1 | build with web socket support | +| WITH_SERVER_STATS=1 | build with support for server statistics | +| WITH_CPP=1 | build libraries with c++ classes | +| CONFIG_FILE=file | use 'file' as the config file | +| CONFIG_FILE2=file | use 'file' as the backup config file | +| HTMLDIR=/path | place to install initial web pages | +| DOCUMENT_ROOT=/path | HTMLDIR override, config option, install | +| | nothing is installed here. | +| PORTS=8080 | listening ports override when installing | +| SSL_LIB=libssl.so.0 | use versioned SSL library | +| CRYPTO_LIB=libcrypto.so.0 | system versioned CRYPTO library | +| PREFIX=/usr/local | sets the install directory | +| COPT='-DNO_SSL' | method to insert compile flags | + +Note that the WITH_* options used for *make* are not identical to the +preprocessor defines in the source code - usually USE_* is used there. + +## Changing PREFIX + +To change the target destination pass the `PREFIX` option to the command `make install` (not `make build`). Example usage: + +``` +$ make build +$ make -n install PREFIX=/opt/civetweb +``` +Note: The `-n` corresponds to the `--dry-run` option (it does not make any changes): You can see where `make install` would install. Example output of the above command: + +``` +$ make -n install PREFIX=/opt/civetweb +install -d -m 755 "/opt/civetweb/share/doc/civetweb" +install -m 644 resources/itworks.html /opt/civetweb/share/doc/civetweb/index.html +install -m 644 resources/civetweb_64x64.png /opt/civetweb/share/doc/civetweb/ +install -d -m 755 "/opt/civetweb/etc" +install -m 644 resources/civetweb.conf "/opt/civetweb/etc/" +sed -i 's#^document_root.*$#document_root /opt/civetweb/share/doc/civetweb#' "/opt/civetweb/etc/civetweb.conf" +sed -i 's#^listening_ports.*$#listening_ports 8080#' "/opt/civetweb/etc/civetweb.conf" +install -d -m 755 "/opt/civetweb/share/doc/civetweb" +install -m 644 *.md "/opt/civetweb/share/doc/civetweb" +install -d -m 755 "/opt/civetweb/bin" +install -m 755 civetweb "/opt/civetweb/bin/" +``` + +If the output looks good: Just remove the `-n` option to actually install the software on your system. + +## Setting compile flags + +Compile flags can be set using the *COPT* make option like so. +``` +make build COPT="-DNDEBUG -DNO_CGI" +``` + +| Compile Flags | Description | +| ------------------------- | ------------------------------------ | +| NDEBUG | strip off all debug code | +| DEBUG | build debug version (very noisy) | +| NO_CGI | disable CGI support | +| NO_CACHING | disable caching functionality | +| NO_SSL | disable SSL functionality | +| NO_SSL_DL | link against system libssl library | +| NO_FILES | do not serve files from a directory | +| SQLITE_DISABLE_LFS | disables large files (Lua only) | +| SSL_ALREADY_INITIALIZED | do not initialize libcrypto | + +## Cross Compiling + +Take total control with *CC*, *COPT* and *TARGET_OS* as make options. +TARGET_OS is used to determine some compile details as will as code function. +TARGET_OS values should be be one found in *resources/Makefile.in-os*. + +``` +make CC=arm-none-linux-gnueabi-gcc COPT="-march=armv7-a -mfpu=vfp -mfloat-abi=softfp" TARGET_OS=FROG +``` + +## Cocoa DMG Packaging (OSX Only) + +Use the alternate *Makefile.osx* to do the build. The entire build has +to be done using *Makefile.osx* because additional compile and link options +are required. This Makefile has all the same options as the other one plus +one additional *package* rule. + +``` +make -f Makefile.osx package +``` + +Building with Buildroot +--------- + +[Buildroot](http://buildroot.uclibc.org/) is a tool for creating cross compiled file systems. Including Civetweb in buildroot is fairly easy. There is even support for various build options. + +1. First, check if it already there. + - In buildroot, make menuconfig + - Package Selection for the target ---> + - Networking applications ---> + - civetweb +2. If not there, just add it + - copy *Config.in* and *civetweb.mk* from Civetweb's *contrib/buildroot/* to Buildroot's *package/civetweb/* directory. + - In Buildroot's *package/Config.in, insert the following line in were you will know how to find it in the menu. + > ``` source "package/civetweb/Config.in" ``` + + +Building on Android +--------- + +This is a small guide to help you run civetweb on Android, originally +tested on the HTC Wildfire. +Note: You do not need root access to run civetweb on Android. + +- Download the source from the Downloads page. +- Download the Android NDK from [http://developer.android.com/tools/sdk/ndk/index.html](http://developer.android.com/tools/sdk/ndk/index.html) +- Run `/path-to-ndk/ndk-build -C /path-to-civetweb/resources` + That should generate civetweb/lib/armeabi/civetweb +- Using the adb tool (you need to have Android SDK installed for that), + push the generated civetweb binary to `/data/local` folder on device. +- From adb shell, navigate to `/data/local` and execute `./civetweb`. +- To test if the server is running fine, visit your web-browser and + navigate to `http://127.0.0.1:8080` You should see the `Index of /` page. + + +Notes: + +- `jni` stands for Java Native Interface. Read up on Android NDK if you want + to know how to interact with the native C functions of civetweb in Android + Java applications. + + + diff --git a/src/civetweb/docs/Contribution.md b/src/civetweb/docs/Contribution.md new file mode 100644 index 000000000..aa88c14ff --- /dev/null +++ b/src/civetweb/docs/Contribution.md @@ -0,0 +1,23 @@ +Contributing to CivetWeb +==== + +Contributions to CivetWeb are welcome, provided all contributions carry the MIT license. + +- Please report issues on GitHub. If the issue you want to report is already reported there, add a note with your specific details to that issue. In case of doubt, please create a new issue. +- If you know how to fix the issue, please create a pull request on GitHub. Please take care your modifications pass the continuous integration checks. These checks are performed automatically when you create a pull request, but it may take some hours until all tests are completed. Please provide a description for every pull request. +- Alternatively, you can post a patch or describe the required modifications in a GitHub issue. +However, a pull request would be preferred. +- Contributor names are listed in CREDITS.md, unless you explicitly state you don't want your name to be listed there. This file is occasionally updated, adding new contributors, using author names from git commits and GitHub comments. + + +- In case your modifications either + 1. modify or extend the API, + 2. affect multi-threading, + 3. imply structural changes, + or + 4. have significant influence on maintenance, + + please first create an issue on GitHub or create a thread on the CivetWeb discussion group, to discuss the planned changed. + +- In case you think you found a security issue that should be evaluated and fixed before public disclosure, feel free to write an email. Although CivetWeb is a fork from Mongoose from 2013, the code bases are different now, so security vulnerabilities of Mongoose usually do not affect CivetWeb. Open an issue for Mongoose vulnerabilities you want to have checked if CivetWeb is affected. + diff --git a/src/civetweb/docs/Embedding.md b/src/civetweb/docs/Embedding.md new file mode 100644 index 000000000..0ffbc230d --- /dev/null +++ b/src/civetweb/docs/Embedding.md @@ -0,0 +1,260 @@ +Embedding CivetWeb +========= + +CivetWeb is primarily designed so applications can easily add HTTP and HTTPS server as well as WebSocket functionality. For example, an application server could use CivetWeb to enable a web service interface for automation or remote control. + +However, it can also be used as a stand-alone executable. It can deliver static files and offers built-in server side Lua, JavaScript and CGI support. Some instructions how to build the stand-alone server can be found in [Building.md](https://github.com/civetweb/civetweb/blob/master/docs/Building.md). + +Files +------ + +There is just a small set of files to compile in to the application, +but if a library is desired, see [Building.md](https://github.com/CivetWeb/CivetWeb/blob/master/docs/Building.md) + +#### Regarding the INL file extension +The *INL* file extension represents code that is statically included inline in a source file. Slightly different from C++ where it means "inline" code which is technically not the same as static code. CivetWeb overloads this extension for the sake of clarity as opposed to having .c extensions on files that should not be directly compiled. + +#### HTTP Server Source Files + +These files constitute the CivetWeb library. They do not contain a `main` function, +but all functions required to run a HTTP server. + + - HTTP server API + - include/civetweb.h + - C implementation + - src/civetweb.c + - src/md5.inl (MD5 calculation) + - src/sha1.inl (SHA calculation) + - src/handle\_form.inl (HTML form handling functions) + - src/timer.inl (optional timer support) + - Optional: C++ wrapper + - include/CivetServer.h (C++ interface) + - src/CivetServer.cpp (C++ wrapper implementation) + - Optional: Third party components + - src/third\_party/* (third party components, mainly used for the standalone server) + - src/mod\_*.inl (modules to access third party components from civetweb) + + +Note: The C++ wrapper uses the official C interface (civetweb.h) and does not add new features to the server. Several features available in the C interface are missing in the C++ interface. While all features should be accessible using the C interface, this is not a design goal of the C++ interface. + + +#### Additional Source Files for Executables + +These files can be used to build a server executable. They contain a `main` function +starting the HTTP server. + + - Stand-alone C Server + - src/main.c + - Reference embedded C Server + - examples/embedded\_c/embedded\_c.c + - Reference embedded C++ Server + - examples/embedded\_cpp/embedded\_cpp.cpp + +Note: The "embedded" example is actively maintained, updated, extended and tested. Other examples in the examples/ folder might be outdated and remain there for reference. + + +Quick Start +------ + +By default, the server will automatically serve up files like a normal HTTP server. An embedded server is most likely going to overload this functionality. + +### C + - Include the C interface ```civetweb.h```. + - Use `mg_start()` to start the server. + - Use *options* to select the port and document root among other things. + - Use *callbacks* to add your own hooks. + - Use `mg_set_request_handler()` to easily add your own request handlers. + - Use `mg_stop()` to stop the server. + +### C++ + - Note that CivetWeb is Clean C, and C++ interface ```CivetServer.h``` is only a wrapper layer around the C interface. + Not all CivetWeb features available in C are also available in C++. + - Create CivetHandlers for each URI. + - Register the handlers with `CivetServer::addHandler()` + - `CivetServer` starts on contruction and stops on destruction. + - Use contructor *options* to select the port and document root among other things. + - Use constructor *callbacks* to add your own hooks. + +Alternative quick start: Have a look at the examples embedded\_c and embedded\_cpp + + +Lua Support +------ + +Lua is a server side include functionality. Files ending in .lua will be processed with Lua. + +##### Add the following CFLAGS + + - `-DLUA_COMPAT_ALL` + - `-DUSE_LUA` + - `-DUSE_LUA_SQLITE3` + - `-DUSE_LUA_FILE_SYSTEM` + +##### Add the following sources + + - src/mod\_lua.inl + - src/third\_party/lua-5.2.4/src + + lapi.c + + lauxlib.c + + lbaselib.c + + lbitlib.c + + lcode.c + + lcorolib.c + + lctype.c + + ldblib.c + + ldebug.c + + ldo.c + + ldump.c + + lfunc.c + + lgc.c + + linit.c + + liolib.c + + llex.c + + lmathlib.c + + lmem.c + + loadlib.c + + lobject.c + + lopcodes.c + + loslib.c + + lparser.c + + lstate.c + + lstring.c + + lstrlib.c + + ltable.c + + ltablib.c + + ltm.c + + lundump.c + + lvm.c + + lzio.c + - src/third\_party/sqlite3.c + - src/third\_party/sqlite3.h + - src/third\_party/lsqlite3.c + - src/third\_party/lfs.c + - src/third\_party/lfs.h + +This build is valid for Lua version Lua 5.2. It is also possible to build with Lua 5.1 (including LuaJIT) or Lua 5.3. + + +JavaScript Support +------ + +CivetWeb can be built with server side JavaScript support by including the Duktape library. + + +CivetWeb internals +------ + +CivetWeb is multithreaded web server. `mg_start()` function allocates +web server context (`struct mg_context`), which holds all information +about web server instance: + +- configuration options. Note that CivetWeb makes internal copies of + passed options. +- SSL context, if any +- user-defined callbacks +- opened listening sockets +- a queue for accepted sockets +- mutexes and condition variables for inter-thread synchronization + +When `mg_start()` returns, all initialization is guaranteed to be complete +(e.g. listening ports are opened, SSL is initialized, etc). `mg_start()` starts +some threads: a master thread, that accepts new connections, and several +worker threads, that process accepted connections. The number of worker threads +is configurable via `num_threads` configuration option. That number puts a +limit on number of simultaneous requests that can be handled by CivetWeb. +If you embed CivetWeb into a program that uses SSL outside CivetWeb as well, +you may need to initialize SSL before calling `mg_start()`, and set the pre- +processor define `SSL_ALREADY_INITIALIZED`. This is not required if SSL is +used only within CivetWeb. + +When master thread accepts new a connection, a new accepted socket (described +by `struct socket`) it placed into the accepted sockets queue, +which has size of `MGSQLEN` (default 20). +Any idle worker thread can grab accepted sockets from that queue. +If all worker threads are busy, master thread can accept and queue up to +20 more TCP connections, filling up the queue. +In the attempt to queue even more accepted connection, the master thread blocks +until there is space in the queue. When the master thread is blocked on a +full queue, the operating system can also queue incoming connection. +The number is limited by the `listen()` call parameter, +which is `SOMAXCONN` and depends on the platform. + +Worker threads are running in an infinite loop, which in a simplified form +looks something like this: + +```C + static void *worker_thread() { + while (consume_socket()) { + process_new_connection(); + } + } +``` + +Function `consume_socket()` gets a new accepted socket from the CivetWeb socket +queue, atomically removing it from the queue. If the queue is empty, +`consume_socket()` blocks and waits until a new socket is placed in the queue +by the master thread. + +`process_new_connection()` actually processes the +connection, i.e. reads the request, parses it, and performs appropriate action +depending on the parsed request. + +Master thread uses `poll()` and `accept()` to accept new connections on +listening sockets. `poll()` is used to avoid `FD_SETSIZE` limitation of +`select()`. Since there are only a few listening sockets, there is no reason +to use hi-performance alternatives like `epoll()` or `kqueue()`. Worker +threads use blocking IO on accepted sockets for reading and writing data. +All accepted sockets have `SO_RCVTIMEO` and `SO_SNDTIMEO` socket options set +(controlled by the `request_timeout_ms` CivetWeb option, 30 seconds default) +which specifies a read/write timeout on client connections. + + +A minimal example +------ + +Initializing a HTTP server +```C +{ + /* Server context handle */ + struct mg_context *ctx; + + /* Initialize the library */ + mg_init_library(0); + + /* Start the server */ + ctx = mg_start(NULL, 0, NULL); + + /* Add some handler */ + mg_set_request_handler(ctx, "/hello", handler, "Hello world"); + + ... Run the application ... + + /* Stop the server */ + mg_stop(ctx); + + /* Un-initialize the library */ + mg_exit_library(); +} +``` + +A simple callback +```C +static int +handler(struct mg_connection *conn, void *ignored) +{ + const char *msg = "Hello world"; + unsigned long len = (unsigned long)strlen(msg); + + mg_printf(conn, + "HTTP/1.1 200 OK\r\n" + "Content-Length: %lu\r\n" + "Content-Type: text/plain\r\n" + "Connection: close\r\n\r\n", + len); + + mg_write(conn, msg, len); + + return 200; +} +``` + diff --git a/src/civetweb/docs/Installing.md b/src/civetweb/docs/Installing.md new file mode 100644 index 000000000..2476b94fd --- /dev/null +++ b/src/civetweb/docs/Installing.md @@ -0,0 +1,38 @@ +Civetweb Install Guide +==== + +This guide covers the distributions for CivetWeb. The latest source code is available at [https://github.com/civetweb/civetweb](https://github.com/civetweb/civetweb). + +Windows +--- + +This pre-built version comes pre-built wit Lua support. Libraries for SSL support are not included due to licensing restrictions; +however, users may add an SSL library themselves. +Instructions for adding SSL support can be found in [https://github.com/civetweb/civetweb/tree/master/docs](https://github.com/civetweb/civetweb/tree/master/docs) + +1. In case the Visual C++ Redistributable are not already installed: + 32 Bit Version: Install the [Redistributable for Visual Studio 2010](http://www.microsoft.com/en-us/download/details.aspx?id=8328) + 64 Bit Version: Install the [Redistributable for Visual Studio 2015](http://www.microsoft.com/en-us/download/details.aspx?id=48145) + Note: The required version of the Redistributables may vary, depending on the CivetWeb version. +2. Download latest *civetweb-win.zip* from [SourceForge](https://sourceforge.net/projects/civetweb/files/) +3. When started, Civetweb puts itself into the tray. + +OS X +--- + +This pre-built version comes with Lua, IPV6 and SSL support. + +1. Download the latest *Civetweb.dmg* from [SourceForge](https://sourceforge.net/projects/civetweb/files/) +2. Click on the it and look for the attachment in the finder. +4. Drag Civetweb to the Applications folder. +5. When started, Civetweb puts itself into top menu. + +Linux +--- + +1. Download the latest *civetweb.tar.gz* from [SourceForge](https://sourceforge.net/projects/civetweb/files/) +2. Open archive and change to the new directory. +3. make help +4. make +5. make install +6. Run the program ```/usr/local/bin/civetweb```, it will use the configuration file */usr/local/etc/civetweb.conf*. diff --git a/src/civetweb/docs/Interface_Changes_1.10.md b/src/civetweb/docs/Interface_Changes_1.10.md new file mode 100644 index 000000000..16bc7dde5 --- /dev/null +++ b/src/civetweb/docs/Interface_Changes_1.10.md @@ -0,0 +1,86 @@ +# Interface changes + +## Proposed interface changes for 1.10 + +Status: To be discussed + +### Server interface + +#### mg\_start / mg\_init\_library + +Calling mg\_init\_library is recommended before calling mg\_start. + +Compatibility: +Initially, mg\_init\_library will be called implicitly if it has +not been called before mg\_start. +If mg\_init\_library was not called, mg\_stop may leave memory leaks. + +#### mg\_websocket\_write functions + +Calling mg\_lock\_connection is no longer called implicitly +in mg\_websocket\_write functions. +If you use websocket write functions them from two threads, +you must call mg\_lock\_connection explicitly, just like for any +other connection. + +This is an API harmonization issue. + +Compatibility: +If a websocket connection was used in only one thread, there is +no incompatibility. If a websocket connection was used in multiple +threads, the user has to add the mg\_lock\_connection before and +the mg\_unlock\_connection after the websocket write call. + +#### open\_file member of mg\_callbacks + +This member is going to be removed. +It is superseeded by mg\_add\_request\_handler. + +Compatibility: +Current code using open\_file needs to be changed. +Instructions how to do this will be provided. + + +### Client interface + + +#### mg\_init\_library + +Calling mg\_init\_library is required before calling any client +function. In particular, the TLS initialization must be done +before using mg\_connect\_client\_secure. + +Compatibility: +Some parts of the client interface did not work, if mg\_start +was not called before. Now server and client become independent. + +#### mg\_connect\_client (family) + +mg_connect_client needs several new parameters (options). + +Details are to be defined. + +mg_connect_client and mg_download should return a different kind of +mg_connection than used in server callbacks. At least, there should +be a function mg_get_response_info, instead of using +mg_get_request_info, and getting the HTTP response code from the +server by looking into the uri member of struct mg_request_info. + + +### `size_t` in all interface + +Having `size_t` in interfaces while building for 32 and 64 bit +complicates maintenance in an unnecessary way +(see [498](https://github.com/civetweb/civetweb/issues/498)). + +Replace all data sizes by 64 bit integers. + + +### Pattern definition + +The current definition of pattern matching is problematic +(see [499](https://github.com/civetweb/civetweb/issues/499)). + +Find and implement a new definition. + + diff --git a/src/civetweb/docs/OpenSSL.md b/src/civetweb/docs/OpenSSL.md new file mode 100644 index 000000000..1f01cca79 --- /dev/null +++ b/src/civetweb/docs/OpenSSL.md @@ -0,0 +1,153 @@ +Adding OpenSSL Support +===== + +Civetweb supports *HTTPS* connections using the OpenSSL transport layer +security (TLS) library. OpenSSL is a free, open source library (see +http://www.openssl.org/). + + +Getting Started +---- + +- Install OpenSSL on your system. There are OpenSSL install packages for all + major Linux distributions as well as a setup for Windows. +- The default build configuration of the civetweb web server will load the + required OpenSSL libraries, if a HTTPS certificate has been configured. + + +Civetweb Configuration +---- + +The configuration file must contain an https port, identified by a letter 's' +attached to the port number. +To serve http and https from their standard ports use the following line in +the configuration file 'civetweb.conf': +
+  listening_ports 80, 443s
+
+To serve only https use: +
+  listening_ports 443s
+
+ +Furthermore the SSL certificate file must be set: +
+  ssl_certificate d:\civetweb\certificate\server.pem
+
+ + +Creating a self signed certificate +---- + +OpenSSL provides a command line interface, that can be used to create the +certificate file required by civetweb (server.pem). + +One can use the following steps in Windows (in Linux replace "copy" by "cp" +and "type" by "cat"): + +
+  openssl genrsa -des3 -out server.key 1024
+
+  openssl req -new -key server.key -out server.csr
+
+  copy server.key server.key.orig
+
+  openssl rsa -in server.key.orig -out server.key
+
+  openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
+
+  copy server.crt server.pem
+
+  type server.key >> server.pem
+
+ +The server.pem file created must contain a 'CERTIFICATE' section as well as a +'RSA PRIVATE KEY' section. It should look like this (x represents BASE64 +encoded data): + +
+-----BEGIN CERTIFICATE-----
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxx
+-----END CERTIFICATE-----
+-----BEGIN RSA PRIVATE KEY-----
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+-----END RSA PRIVATE KEY-----
+
+ + +Including a certificate from a certificate authority +---- + +CivetWeb requires one certificate file in PEM format. +If you got multiple files from your certificate authority, +you need to copy their content together into one file. +Make sure the file has one section BEGIN RSA PRIVATE KEY / +END RSA PRIVATE KEY, and at least one section +BEGIN CERTIFICATE / END CERTIFICATE. +In case you received a file with a section +BEGIN PRIVATE KEY / END PRIVATE KEY, +you may get a suitable file by adding the letters RSA manually. + +Set the "ssl_certificate" configuration parameter to the +file name (including path) of the resulting *.pem file. + +The file must look like the file in the section +"Creating a self signed certificate", but it will have several +BEGIN CERTIFICATE / END CERTIFICATE sections. + + +Common Problems +---- + +In case the OpenSSL configuration is not set up correctly, the server will not +start. Configure an error log file in 'civetweb.conf' to get more information: +
+  error_log_file error.log
+
+ +Check the content of 'error.log': + +
+load_dll: cannot load libeay32.*/libcrypto.*/ssleay32.*/libssl.*
+
+This error message means, the SSL library has not been installed (correctly). +For Windows you might use the pre-built binaries. A link is available at the +OpenSSL project home page (http://www.openssl.org/related/binaries.html). +Choose the windows system folder as installation directory - this is the +default location. + +
+set_ssl_option: cannot open server.pem: error:PEM routines:*:PEM_read_bio:no start line
+set_ssl_option: cannot open server.pem: error:PEM routines:*:PEM_read_bio:bad end line
+
+These error messages indicate, that the format of the ssl_certificate file does +not match the expectations of the SSL library. The PEM file must contain both, +a 'CERTIFICATE' and a 'RSA PRIVATE KEY' section. It should be a strict ASCII +file without byte-order marks. +The instructions above may be used to create a valid ssl_certificate file. + + diff --git a/src/civetweb/docs/README.md b/src/civetweb/docs/README.md new file mode 100644 index 000000000..9494409f9 --- /dev/null +++ b/src/civetweb/docs/README.md @@ -0,0 +1,42 @@ +![CivetWeb](https://raw.github.com/civetweb/civetweb/master/resources/civetweb_64x64.png "CivetWeb") CivetWeb +======= + +CivetWeb is an easy to use, powerful, C/C++ embeddable web server with optional CGI, SSL and Lua support. + +CivetWeb can be used by developers as a library, to add web server functionality to an existing application. +CivetWeb uses an [MIT license](https://github.com/civetweb/civetweb/blob/master/LICENSE.md). + +It can also be used by end users as a stand-alone web server. It is available as single executable, no installation is required. + +The current stable version is 1.9.1 - [release notes](https://github.com/civetweb/civetweb/blob/master/RELEASE_NOTES.md) + + +End users can download CivetWeb at SourceForge +[https://sourceforge.net/projects/civetweb/](https://sourceforge.net/projects/civetweb/) + +Developers can contribute to CivetWeb via GitHub +[https://github.com/civetweb/civetweb](https://github.com/civetweb/civetweb) + +Trouble tickets should be filed on GitHub +[https://github.com/civetweb/civetweb/issues](https://github.com/civetweb/civetweb/issues) + +Announcements are at Google Groups +[https://groups.google.com/d/forum/civetweb](https://groups.google.com/d/forum/civetweb) + +While older support question and discussion threads have been at [Google groups](https://groups.google.com/d/forum/civetweb), most newer ones are [GitHub issues](https://github.com/civetweb/civetweb/issues). + +Source releases can be found on GitHub +[https://github.com/civetweb/civetweb/releases](https://github.com/civetweb/civetweb/releases) + + +Documentation +--------------- + +- [Installing.md](Installing.md) - Install Guide (for end users using pre-built binaries) +- [UserManual.md](UserManual.md) - End User Guide +- [Building.md](Building.md) - Building the Server (quick start guide) +- [Embedding.md](Embedding.md) - Embedding (how to add HTTP support to an existing application) +- [OpenSSL.md](OpenSSL.md) - Adding HTTPS (SSL/TLS) support using OpenSSL. +- [API documentation](api) - Additional documentation on the civetweb application programming interface ([civetweb.h](https://github.com/civetweb/civetweb/blob/master/include/civetweb.h)). + +[Authors](https://github.com/civetweb/civetweb/blob/master/CREDITS.md) diff --git a/src/civetweb/docs/UserManual.md b/src/civetweb/docs/UserManual.md new file mode 100644 index 000000000..3acf49272 --- /dev/null +++ b/src/civetweb/docs/UserManual.md @@ -0,0 +1,814 @@ + +Overview +===== + +Civetweb is small and easy to use web server. +It may be embedded into C/C++ host applications or used as a stand-alone +server. See `Embedding.md` for information on embedding civetweb into +host applications. + +The stand-alone server is self-contained, and does not require any external +software to run. Some Windows users may need to install the +[Visual C++ Redistributable](http://www.microsoft.com/en-us/download/details.aspx?id=30679). + +Installation +---- + +On Windows, UNIX and Mac, the civetweb stand-alone executable may be started +from the command line. +Running `civetweb` in a terminal, optionally followed by configuration parameters +(`civetweb [OPTIONS]`) or a configuration file name (`civetweb [config_file_name]`), +starts the web server. + +For UNIX and Mac, civetweb does not detach from the terminal. +Pressing `Ctrl-C` keys will stop the server. + +On Windows, civetweb iconifies itself to the system tray icon when started. +Right-click on the icon pops up a menu, where it is possible to stop +civetweb, or configure it, or install it as Windows service. + +When started without options, the server exposes the local directory at +[http](http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) port 8080. +Thus, the easiest way to share a folder on Windows is to copy `civetweb.exe` +to this folder, double-click the exe, and launch a browser at +[http://localhost:8080](http://localhost:8080). Note that 'localhost' should +be changed to a machine's name if a folder is accessed from other computer. + +When started, civetweb first searches for the configuration file. +If configuration file is specified explicitly in the command line, i.e. +`civetweb path_to_config_file`, then specified configuration file is used. +Otherwise, civetweb would search for file `civetweb.conf` in the same directory +the executable is located, and use it. This configuration file is optional. + +The configuration file is a sequence of lines, each line containing one +command line argument name and the corresponding value. +Empty lines, and lines beginning with `#`, are ignored. +Here is the example of `civetweb.conf` file: + + document_root c:\www + listening_ports 80,443s + ssl_certificate c:\civetweb\ssl_cert.pem + +When a configuration file is used, additional command line arguments may +override the configuration file settings. +All command line arguments must start with `-`. + +For example: The above `civetweb.conf` file is used, and civetweb started as +`civetweb -document_root D:\web`. Then the `D:\web` directory will be served +as document root, because command line options take priority over the +configuration file. The configuration options section below provides a good +overview of the Civetweb features. + +Note that configuration options on the command line must start with `-`, +but their names are the same as in the config file. All option names are +listed in the next section. Thus, the following two setups are equivalent: + + # Using command line arguments + $ civetweb -listening_ports 1234 -document_root /var/www + + # Using config file + $ cat civetweb.conf + listening_ports 1234 + document_root /var/www + $ civetweb + +Civetweb can also be used to modify `.htpasswd` passwords files: + + civetweb -A + +Unlike other web servers, civetweb does not require CGI scripts to be located +in a special directory. CGI scripts can be anywhere. CGI (and SSI) files are +recognized by the file name pattern. Civetweb uses shell-like glob +patterns. Pattern match starts at the beginning of the string, so essentially +patterns are prefix patterns. Syntax is as follows: + + ** Matches everything + * Matches everything but slash character, '/' + ? Matches any character + $ Matches the end of the string + | Matches if pattern on the left side or the right side matches. + +All other characters in the pattern match themselves. Examples: + + **.cgi$ Any string that ends with .cgi + /foo Any string that begins with /foo + **a$|**b$ Any string that ends with a or b + +# Configuration Options + +Below is a list of configuration options understood by Civetweb. +Every option is followed by it's default value. If a default value is not +present, then the default is empty. + +### cgi\_pattern `**.cgi$|**.pl$|**.php$` +All files that match `cgi_pattern` are treated as CGI files. Default pattern +allows CGI files be anywhere. To restrict CGIs to a certain directory, +use `/path/to/cgi-bin/**.cgi` as pattern. Note that the full file path is +matched against the pattern, not the URI. + +### cgi\_environment +Extra environment variables to be passed to the CGI script in +addition to standard ones. The list must be comma-separated list +of name=value pairs, like this: `VARIABLE1=VALUE1,VARIABLE2=VALUE2`. + +### put\_delete\_auth\_file +Passwords file for PUT and DELETE requests. Without password file, it will not +be possible to, PUT new files to the server or DELETE existing ones. PUT and +DELETE requests might still be handled by Lua scripts and CGI paged. + +### cgi\_interpreter +Path to an executable to use as CGI interpreter for __all__ CGI scripts +regardless of the script file extension. If this option is not set (which is +the default), Civetweb looks at first line of a CGI script, +[shebang line](http://en.wikipedia.org/wiki/Shebang_(Unix\)), for an +interpreter (not only on Linux and Mac but also for Windows). + +For example, if both PHP and Perl CGIs are used, then +`#!/path/to/php-cgi.exe` and `#!/path/to/perl.exe` must be first lines of the +respective CGI scripts. Note that paths should be either full file paths, +or file paths relative to the current working directory of the civetweb +server. If civetweb is started by mouse double-click on Windows, the current +working directory is the directory where the civetweb executable is located. + +If all CGIs use the same interpreter, for example they are all PHP, it is +more efficient to set `cgi_interpreter` to the path to `php-cgi.exe`. +The shebang line in the CGI scripts can be omitted in this case. +Note that PHP scripts must use `php-cgi.exe` as executable, not `php.exe`. + +### protect\_uri +Comma separated list of URI=PATH pairs, specifying that given +URIs must be protected with password files specified by PATH. +All Paths must be full file paths. + +### authentication\_domain `mydomain.com` +Authorization realm used for HTTP digest authentication. This domain is +used in the encoding of the `.htpasswd` authorization files as well. +Changing the domain retroactively will render the existing passwords useless. + +### enable\_auth\_domain\_check `yes` +When using absolute URLs, verify the host is identical to the authentication\_domain. If enabled, requests to absolute URLs will only be processed +if they are directed to the domain. If disabled, absolute URLs to any host +will be accepted. + +### ssi\_pattern `**.shtml$|**.shtm$` +All files that match `ssi_pattern` are treated as Server Side Includes (SSI). + +SSI is a simple interpreted server-side scripting language which is most +commonly used to include the contents of another file into a web page. +It can be useful when it is desirable to include a common piece +of code throughout a website, for example, headers and footers. + +In order for a webpage to recognize an SSI-enabled HTML file, the filename +should end with a special extension, by default the extension should be +either `.shtml` or `.shtm`. These extentions may be changed using the +`ssi_pattern` option. + +Unknown SSI directives are silently ignored by civetweb. Currently, two SSI +directives are supported, ` + +For more information on Server Side Includes, take a look at the Wikipedia: +[Server Side Includes](http://en.wikipedia.org/wiki/Server_Side_Includes) + +### throttle +Limit download speed for clients. `throttle` is a comma-separated +list of key=value pairs, where key could be: + + * limit speed for all connections + x.x.x.x/mask limit speed for specified subnet + uri_prefix_pattern limit speed for given URIs + +The value is a floating-point number of bytes per second, optionally +followed by a `k` or `m` character, meaning kilobytes and +megabytes respectively. A limit of 0 means unlimited rate. The +last matching rule wins. Examples: + + *=1k,10.0.0.0/8=0 limit all accesses to 1 kilobyte per second, + but give connections the from 10.0.0.0/8 subnet + unlimited speed + + /downloads/=5k limit accesses to all URIs in `/downloads/` to + 5 kilobytes per second. All other accesses are unlimited + +### access\_log\_file +Path to a file for access logs. Either full path, or relative to the current +working directory. If absent (default), then accesses are not logged. + +### enable\_directory\_listing `yes` +Enable directory listing, either `yes` or `no`. + +### error\_log\_file +Path to a file for error logs. Either full path, or relative to the current +working directory. If absent (default), then errors are not logged. + +### global\_auth\_file +Path to a global passwords file, either full path or relative to the current +working directory. If set, per-directory `.htpasswd` files are ignored, +and all requests are authorized against that file. + +The file has to include the realm set through `authentication_domain` and the +password in digest format: + + user:realm:digest + test:test.com:ce0220efc2dd2fad6185e1f1af5a4327 + +Password files may be generated using `civetweb -A` as explained above, or +online tools e.g. [this generator](http://www.askapache.com/online-tools/htpasswd-generator). + +### index\_files `index.xhtml,index.html,index.htm,index.cgi,index.shtml,index.php` +Comma-separated list of files to be treated as directory index files. +If more than one matching file is present in a directory, the one listed to the left +is used as a directory index. + +In case built-in Lua support has been enabled, `index.lp,index.lsp,index.lua` +are additional default index files, ordered before `index.cgi`. + +### enable\_keep\_alive `no` +Enable connection keep alive, either `yes` or `no`. + +Allows clients to reuse TCP connection for subsequent HTTP requests, +which improves performance. +For this to work when using request handlers it is important to add the +correct Content-Length HTTP header for each request. If this is forgotten the +client will time out. + +Note: If you set keep\_alive to `yes`, you should set keep\_alive\_timeout\_ms +to some value > 0 (e.g. 500). If you set keep\_alive to `no`, you should set +keep\_alive\_timeout\_ms to 0. Currently, this is done as a default value, +but this configuration is redundant. In a future version, the keep\_alive +configuration option might be removed and automatically set to `yes` if +a timeout > 0 is set. + +### access\_control\_list +An Access Control List (ACL) allows restrictions to be put on the list of IP +addresses which have access to the web server. In the case of the Civetweb +web server, the ACL is a comma separated list of IP subnets, where each +subnet is pre-pended by either a `-` or a `+` sign. A plus sign means allow, +where a minus sign means deny. If a subnet mask is omitted, such as `-1.2.3.4`, +this means to deny only that single IP address. + +Subnet masks may vary from 0 to 32, inclusive. The default setting is to allow +all accesses. On each request the full list is traversed, and +the last match wins. Examples: + + -0.0.0.0/0,+192.168/16 deny all accesses, only allow 192.168/16 subnet + +To learn more about subnet masks, see the +[Wikipedia page on Subnetwork](http://en.wikipedia.org/wiki/Subnetwork). + +### extra\_mime\_types +Extra mime types, in tha form `extension1=type1,exten-sion2=type2,...`. +See the [Wikipedia page on Internet media types](http://en.wikipedia.org/wiki/Internet_media_type). +Extension must include a leading dot. Example: +`.cpp=plain/text,.java=plain/text` + +### listening\_ports `8080` +Comma-separated list of ports to listen on. If the port is SSL, a +letter `s` must be appended, for example, `80,443s` will open +port 80 and port 443, and connections on port 443 will be SSL-ed. +For non-SSL ports, it is allowed to append letter `r`, meaning 'redirect'. +Redirect ports will redirect all their traffic to the first configured +SSL port. For example, if `listening_ports` is `80r,443s`, then all +HTTP traffic coming at port 80 will be redirected to HTTPS port 443. + +It is possible to specify an IP address to bind to. In this case, +an IP address and a colon must be pre-pended to the port number. +For example, to bind to a loopback interface on port 80 and to +all interfaces on HTTPS port 443, use `127.0.0.1:80,443s`. + +If the server is built with IPv6 support, `[::]:8080` can be used to +listen to IPv6 connections to port 8080. IPv6 addresses of network +interfaces can be specified as well, +e.g. `[::1]:8080` for the IPv6 loopback interface. + +[::]:80 will bind to port 80 IPv6 only. In order to use port 80 for +all interfaces, both IPv4 and IPv6, use either the configuration +`80,[::]:80` (create one socket for IPv4 and one for IPv6 only), +or `+80` (create one socket for both, IPv4 and IPv6). +The `+`-notation to use IPv4 and IPv6 will only work in no network +interface is specified. Depending on your operating system version +and IPv6 network environment, some configurations might not work +as expected, so you have to test to find the configuration most +suitable for your needs. In case `+80` does not work for your +environment, you need to use `80,[::]:80`. + +It is possible to use network interface addresses (e.g., `192.0.2.3:80`, +`[2001:0db8::1234]:80`). To get a list of available network interface +addresses, use `ipconfig` (in a `cmd` window in Windows) or `ifconfig` +(in a Linux shell). +Alternatively, you could use the hostname for an interface. Check the +hosts file of your operating system for a proper hostname +(for Windows, usually found in C:\Windows\System32\drivers\etc\, +for most Linux distributions: /etc/hosts). E.g., to bind the IPv6 +local host, you could use `ip6-localhost:80`. This translates to +`[::1]:80`. Beside the hosts file, there are several other name +resolution services. Using your hostname might bind you to the +localhost or an external interface. You could also try `hostname.local`, +if the proper network services are installed (Zeroconf, mDNS, Bonjour, +Avahi). When using a hostname, you need to test in your particular network +environment - in some cases, you might need to resort to a fixed IP address. + +### document\_root `.` +A directory to serve. By default, the current working directory is served. +The current directory is commonly referenced as dot (`.`). +It is recommended to use an absolute path for document\_root, in order to +avoid accidentally serving the wrong directory. + +### ssl\_certificate +Path to the SSL certificate file. This option is only required when at least +one of the `listening\_ports` is SSL. The file must be in PEM format, +and it must have both, private key and certificate, see for example +[ssl_cert.pem](https://github.com/civetweb/civetweb/blob/master/resources/ssl_cert.pem) +A description how to create a certificate can be found in doc/OpenSSL.md + +### num\_threads `50` +Number of worker threads. Civetweb handles each incoming connection in a +separate thread. Therefore, the value of this option is effectively the number +of concurrent HTTP connections Civetweb can handle. + +### run\_as\_user +Switch to given user credentials after startup. Usually, this option is +required when civetweb needs to bind on privileged ports on UNIX. To do +that, civetweb needs to be started as root. From a security point of view, +running as root is not advisable, therefore this option can be used to drop +privileges. Example: + + civetweb -listening_ports 80 -run_as_user webserver + +### url\_rewrite\_patterns +Comma-separated list of URL rewrites in the form of +`uri_pattern=file_or_directory_path`. When Civetweb receives any request, +it constructs the file name to show by combining `document_root` and the URI. +However, if the rewrite option is used and `uri_pattern` matches the +requested URI, then `document_root` is ignored. Instead, +`file_or_directory_path` is used, which should be a full path name or +a path relative to the web server's current working directory. Note that +`uri_pattern`, as all civetweb patterns, is a prefix pattern. + +This makes it possible to serve many directories outside from `document_root`, +redirect all requests to scripts, and do other tricky things. For example, +to redirect all accesses to `.doc` files to a special script, do: + + civetweb -url_rewrite_patterns **.doc$=/path/to/cgi-bin/handle_doc.cgi + +Or, to imitate support for user home directories, do: + + civetweb -url_rewrite_patterns /~joe/=/home/joe/,/~bill=/home/bill/ + +### hide\_files\_patterns +A pattern for the files to hide. Files that match the pattern will not +show up in directory listing and return `404 Not Found` if requested. Pattern +must be for a file name only, not including directory names. Example: + + civetweb -hide_files_patterns secret.txt|**.hide + +Note: hide\_file\_patterns uses the pattern described above. If you want to +hide all files with a certain extension, make sure to use **.extension +(not just *.extension). + +### request\_timeout\_ms `30000` +Timeout for network read and network write operations, in milliseconds. +If a client intends to keep long-running connection, either increase this +value or (better) use keep-alive messages. + +### keep\_alive\_timeout\_ms `500` or `0` +Idle timeout between two requests in one keep-alive connection. +If keep alive is enabled, multiple requests using the same connection +are possible. This reduces the overhead for opening and closing connections +when loading several resources from one server, but it also blocks one port +and one thread at the server during the lifetime of this connection. +Unfortunately, browsers do not close the keep-alive connection after loading +all resources required to show a website. +The server closes a keep-alive connection, if there is no additional request +from the client during this timeout. + +Note: if enable\_keep\_alive is set to `no` the value of +keep\_alive\_timeout\_ms should be set to `0`, if enable\_keep\_alive is set +to `yes`, the value of keep\_alive\_timeout\_ms must be >0. +Currently keep\_alive\_timeout\_ms is ignored if enable\_keep\_alive is no, +but future versions my drop the enable\_keep\_alive configuration value and +automatically use keep-alive if keep\_alive\_timeout\_ms is not 0. + +### linger\_timeout\_ms +Set TCP socket linger timeout before closing sockets (SO\_LINGER option). +The configured value is a timeout in milliseconds. Setting the value to 0 +will yield in abortive close (if the socket is closed from the server side). +Setting the value to -1 will turn off linger. +If the value is not set (or set to -2), CivetWeb will not set the linger +option at all. + +Note: For consistency with other timeouts, the value is configured in +milliseconds. However, the TCP socket layer usually only offers a timeout in +seconds, so the value should be an integer multiple of 1000. + +### lua\_preload\_file +This configuration option can be used to specify a Lua script file, which +is executed before the actual web page script (Lua script, Lua server page +or Lua websocket). It can be used to modify the Lua environment of all web +page scripts, e.g., by loading additional libraries or defining functions +required by all scripts. +It may be used to achieve backward compatibility by defining obsolete +functions as well. + +### lua\_script\_pattern `"**.lua$` +A pattern for files that are interpreted as Lua scripts by the server. +In contrast to Lua server pages, Lua scripts use plain Lua syntax. +An example can be found in the test directory. + +### lua\_server\_page\_pattern `**.lp$|**.lsp$` +Files matching this pattern are treated as Lua server pages. +In contrast to Lua scripts, the content of a Lua server pages is delivered +directly to the client. Lua script parts are delimited from the standard +content by including them between tags. +An example can be found in the test directory. + +### lua\_background\_script +Experimental feature, and subject to change. +Run a Lua script in the background, independent from any connection. +The script is started before network access to the server is available. +It can be used to prepare the document root (e.g., update files, compress +files, ...), check for external resources, remove old log files, etc. + +The Lua state remains open until the server is stopped. +In the future, some callback functions will be available to notify the +script on changes of the server state. See example lua script : +[background.lua](https://github.com/civetweb/civetweb/blob/master/test/background.lua). + +Additional functions available in background script : +sleep, root path, script name, isterminated + +### lua\_background\_script\_params `param1=1,param2=2` +Can add dynamic parameters to background script. +Parameters mapped to global 'mg' table 'params' field. + +### websocket\_root +In case civetweb is built with Lua and websocket support, Lua scripts may +be used for websockets as well. Since websockets use a different URL scheme +(ws, wss) than other http pages (http, https), the Lua scripts used for +websockets may also be served from a different directory. By default, +the document_root is used as websocket_root as well. + + +### access\_control\_allow\_origin `*` +Access-Control-Allow-Origin header field, used for cross-origin resource +sharing (CORS). +See the [Wikipedia page on CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing). + + +### access\_control\_allow\_methods `*` +Access-Control-Allow-Methods header field, used for cross-origin resource +sharing (CORS) pre-flight requests. +See the [Wikipedia page on CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing). + +If set to an empty string, pre-flights will not be supported directly by the server, +but scripts may still support pre-flights by handling the OPTIONS method properly. +If set to "*", the pre-flight will allow whatever method has been requested. +If set to a comma separated list of valid HTTP methods, the pre-flight will return +exactly this list as allowed method. +If set in any other way, the result is unspecified. + + +### access\_control\_allow\_headers `*` +Access-Control-Allow-Headers header field, used for cross-origin resource +sharing (CORS) pre-flight requests. +See the [Wikipedia page on CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing). + +If set to an empty string, pre-flights will not allow additional headers. +If set to "*", the pre-flight will allow whatever headers have been requested. +If set to a comma separated list of valid HTTP headers, the pre-flight will return +exactly this list as allowed headers. +If set in any other way, the result is unspecified. + + +### error\_pages +This option may be used to specify a directory for user defined error pages. +The error pages may be specified for an individual http status code (e.g., +404 - page requested by the client not found), a group of http status codes +(e.g., 4xx - all client errors) or all errors. The corresponding error pages +must be called error404.ext, error4xx.ext or error.ext, whereas the file +extention may be one of the extentions specified for the index_files option. +See the [Wikipedia page on HTTP status codes](http://en.wikipedia.org/wiki/HTTP_status_code). + +### tcp\_nodelay `0` +Enable TCP_NODELAY socket option on client connections. + +If set the socket option will disable Nagle's algorithm on the connection +which means that packets will be sent as soon as possible instead of waiting +for a full buffer or timeout to occur. + + 0 Keep the default: Nagel's algorithm enabled + 1 Disable Nagel's algorithm for all sockets + +### static\_file\_max\_age `3600` +Set the maximum time (in seconds) a cache may store a static files. + +This option will set the `Cache-Control: max-age` value for static files. +Dynamically generated content, i.e., content created by a script or callback, +must send cache control headers by themselfes. + +A value >0 corresponds to a maximum allowed caching time in seconds. +This value should not exceed one year (RFC 2616, Section 14.21). +A value of 0 will send "do not cache" headers for all static files. +For values <0 and values >31622400, the behavior is undefined. + +### strict\_transport\_security\_max\_age + +Set the `Strict-Transport-Security` header, and set the `max-age` value. +This instructs web browsers to interact with the server only using HTTPS, +never by HTTP. If set, it will be sent for every request handled directly +by the server, except scripts (CGI, Lua, ..) and callbacks. They must +send HTTP headers on their own. + +The time is specified in seconds. If this configuration is not set, +or set to -1, no `Strict-Transport-Security` header will be sent. +For values <-1 and values >31622400, the behavior is undefined. + +### decode\_url `yes` +URL encoded request strings are decoded in the server, unless it is disabled +by setting this option to `no`. + +### ssl\_verify\_peer `no` +Enable client's certificate verification by the server. + +### ssl\_ca\_path +Name of a directory containing trusted CA certificates. Each file in the +directory must contain only a single CA certificate. The files must be named +by the subject name’s hash and an extension of “.0”. If there is more than one +certificate with the same subject name they should have extensions ".0", ".1", +".2" and so on respectively. + +### ssl\_ca\_file +Path to a .pem file containing trusted certificates. The file may contain +more than one certificate. + +### ssl\_verify\_depth `9` +Sets maximum depth of certificate chain. If client's certificate chain is longer +than the depth set here connection is refused. + +### ssl\_default\_verify\_paths `yes` +Loads default trusted certificates locations set at openssl compile time. + +### ssl\_cipher\_list +List of ciphers to present to the client. Entries should be separated by +colons, commas or spaces. + + ALL All available ciphers + ALL:!eNULL All ciphers excluding NULL ciphers + AES128:!MD5 AES 128 with digests other than MD5 + +See [this entry](https://www.openssl.org/docs/manmaster/apps/ciphers.html) in +OpenSSL documentation for full list of options and additional examples. + +### ssl\_protocol\_version `0` +Sets the minimal accepted version of SSL/TLS protocol according to the table: + +Protocols | Value +------------ | ------------- +SSL2+SSL3+TLS1.0+TLS1.1+TLS1.2 | 0 +SSL3+TLS1.0+TLS1.1+TLS1.2 | 1 +TLS1.0+TLS1.1+TLS1.2 | 2 +TLS1.1+TLS1.2 | 3 +TLS1.2 | 4 + +### ssl\_short\_trust `no` +Enables the use of short lived certificates. This will allow for the certificates +and keys specified in `ssl_certificate`, `ssl_ca_file` and `ssl_ca_path` to be +exchanged and reloaded while the server is running. + +In an automated environment it is advised to first write the new pem file to +a different filename and then to rename it to the configured pem file name to +increase performance while swapping the certificate. + +Disk IO performance can be improved when keeping the certificates and keys stored +on a tmpfs (linux) on a system with very high throughput. + +### allow\_sendfile\_call `yes` +This option can be used to enable or disable the use of the Linux `sendfile` system call. It is only available for Linux systems and only affecting HTTP (not HTTPS) connections if `throttle` is not enabled. While using the `sendfile` call will lead to a performance boost for HTTP connections, this call may be broken for some file systems and some operating system versions. + +### case\_sensitive `no` +This option can be uset to enable case URLs for Windows servers. It is only available for Windows systems. Windows file systems are not case sensitive, but they still store the file name including case. If this option is set to `yes`, the comparison for URIs and Windows file names will be case sensitive. + +### allow\_index\_script\_resource `no` +Index scripts (like `index.cgi` or `index.lua`) may have script handled resources. + +It this feature is activated, that /some/path/file.ext might be handled by: + 1. /some/path/file.ext (with PATH\_INFO='/', if ext = cgi) + 2. /some/path/index.lua with mg.request\_info.path\_info='/file.ext' + 3. /some/path/index.cgi with PATH\_INFO='/file.ext' + 4. /some/path/index.php with PATH\_INFO='/file.ext' + 5. /some/index.lua with mg.request\_info.path\_info=='/path/file.ext' + 6. /some/index.cgi with PATH\_INFO='/path/file.ext' + 7. /some/index.php with PATH\_INFO='/path/file.ext' + 8. /index.lua with mg.request\_info.path\_info=='/some/path/file.ext' + 9. /index.cgi with PATH\_INFO='/some/path/file.ext' + 10. /index.php with PATH\_INFO='/some/path/file.ext' + +Note: This example is valid, if the default configuration values for `index_files`, `cgi_pattern` and `lua_script_pattern` are used, and the server is built with CGI and Lua support enabled. + +If this feature is not activated, only the first file (/some/path/file.cgi) will be accepted. + +Note: This parameter affects only index scripts. A path like /here/script.cgi/handle/this.ext will call /here/script.cgi with PATH\_INFO='/handle/this.ext', no matter if this option is set to `yes` or `no`. + +This feature can be used to completely hide the script extension from the URL. + +### additional\_header +Send additional HTTP response header line for every request. +The full header line including key and value must be specified, excluding the carriage return line feed. + +Example (used as command line option): +`-additional_header "X-Frame-Options: SAMEORIGIN"` + +This option can be specified multiple times. All specified header lines will be sent. + +# Lua Scripts and Lua Server Pages +Pre-built Windows and Mac civetweb binaries have built-in Lua scripting +support as well as support for Lua Server Pages. + +Lua scripts (default extension: *.lua) use plain Lua syntax. +The body of the script file is not sent directly to the client, +the Lua script must send header and content of the web page by calling +the function mg.write(text). + +Lua Server Pages (default extensions: *.lsp, *.lp) are html pages containing +script elements similar to PHP, using the Lua programming language instead of +PHP. Lua script elements must be enclosed in `` blocks, and can appear +anywhere on the page. Furthermore, Lua Server Pages offer the opportunity to +insert the content of a variable by enclosing the Lua variable name in +`` blocks, similar to PHP. +For example, to print the current weekday name and the URI of the current +page, one can write: + +

+ Today is: + +

+

+ URI is +

+ +Lua is known for it's speed and small size. Civetweb currently uses Lua +version 5.2.4. The documentation for it can be found in the +[Lua 5.2 reference manual](http://www.lua.org/manual/5.2/). + + +Note that this example uses function `mg.write()`, which sends data to the +web client. Using `mg.write()` is the way to generate web content from inside +Lua code. In addition to `mg.write()`, all standard Lua library functions +are accessible from the Lua code (please check the reference manual for +details). Lua functions working on files (e.g., `io.open`) use a path +relative to the working path of the civetweb process. The web server content +is located in the path `mg.document_root`. +Information on the request is available in the `mg.request_info` +object, like the request method, all HTTP headers, etcetera. + +[page2.lua](https://github.com/civetweb/civetweb/blob/master/test/page2.lua) +is an example for a plain Lua script. + +[page2.lp](https://github.com/civetweb/civetweb/blob/master/test/page2.lp) +is an example for a Lua Server Page. + +Both examples show the content of the `mg.request_info` object as the page +content. Please refer to `struct mg_request_info` definition in +[civetweb.h](https://github.com/civetweb/civetweb/blob/master/include/civetweb.h) +to see additional information on the elements of the `mg.request_info` object. + +Civetweb also provides access to the [SQlite3 database](http://www.sqlite.org/) +through the [LuaSQLite3 interface](http://lua.sqlite.org/index.cgi/doc/tip/doc/lsqlite3.wiki) +in Lua. Examples are given in +[page.lua](https://github.com/civetweb/civetweb/blob/master/test/page.lua) and +[page.lp](https://github.com/civetweb/civetweb/blob/master/test/page.lp). + + +Civetweb exports the following functions to Lua: + +mg (table): + + mg.read() -- reads a chunk from POST data, returns it as a string + mg.write(str) -- writes string to the client + mg.include(filename, [pathtype]) -- include another Lua Page file (Lua Pages only) + -- pathtype can be "abs", "rel"/"file" or "virt[ual]" + -- like defined for SSI #include + mg.redirect(uri) -- internal redirect to a given URI + mg.onerror(msg) -- error handler, can be overridden + mg.version -- a string that holds Civetweb version + mg.document_root -- a string that holds the document root directory + mg.auth_domain -- a string that holds the HTTP authentication domain + mg.get_var(str, varname) -- extract variable from (query) string + mg.get_cookie(str, cookie) -- extract cookie from a string + mg.get_mime_type(filename) -- get MIME type of a file + mg.get_info(infotype) -- get server status information + mg.send_file(filename) -- send a file, including MIME type + mg.url_encode(str) -- URL encode a string + mg.url_decode(str, [form]) -- URL decode a string. If form=true, replace + by space. + mg.base64_encode(str) -- BASE64 encode a string + mg.base64_decode(str) -- BASE64 decode a string + mg.md5(str) -- return the MD5 hash of a string + mg.keep_alive(bool) -- allow/forbid to use http keep-alive for this request + mg.request_info -- a table with the following request information + .remote_addr -- IP address of the client as string + .remote_port -- remote port number + .server_port -- server port number + .request_method -- HTTP method (e.g.: GET, POST) + .http_version -- HTTP protocol version (e.g.: 1.1) + .uri -- resource name + .query_string -- query string if present, nil otherwise + .script_name -- name of the Lua script + .https -- true if accessed by https://, false otherwise + .remote_user -- user name if authenticated, nil otherwise + +connect (function): + + -- Connect to the remote TCP server. This function is an implementation + -- of simple socket interface. It returns a socket object with three + -- methods: send, recv, close, which are synchronous (blocking). + -- connect() throws an exception on connection error. + connect(host, port, use_ssl) + + -- Example of using connect() interface: + local host = 'code.google.com' -- IP address or domain name + local ok, sock = pcall(connect, host, 80, 1) + if ok then + sock:send('GET /p/civetweb/ HTTP/1.0\r\n' .. + 'Host: ' .. host .. '\r\n\r\n') + local reply = sock:recv() + sock:close() + -- reply now contains the web page https://code.google.com/p/civetweb + end + + +All filename arguments are either absolute or relative to the civetweb working +directory (not the document root or the Lua script/page file). + +**IMPORTANT: Civetweb does not send HTTP headers for Lua pages. Therefore, +every Lua Page must begin with a HTTP reply line and headers**, like this: + + + + ... the rest of the web page ... + +To serve a Lua Page, civetweb creates a Lua context. That context is used for +all Lua blocks within the page. That means, all Lua blocks on the same page +share the same context. If one block defines a variable, for example, that +variable is visible in all block that follow. + +## Websockets for Lua +Civetweb offers support for websockets in Lua as well. In contrast to plain +Lua scripts and Lua server pages, Lua websocket scripts are shared by all clients. + +Lua websocket scripts must define a few functions: + open(arg) -- callback to accept or reject a connection + ready(arg) -- called after a connection has been established + data(arg) -- called when the server receives data from the client + close(arg) -- called when a websocket connection is closed +All function are called with one argument of type table with at least one field +"client" to identify the client. When "open" is called, the argument table additionally +contains the "request_info" table as defined above. For the "data" handler, an +additional field "data" is available. The functions "open", "ready" and "data" +must return true in order to keep the connetion open. + +Lua websocket pages do support single shot (timeout) and interval timers. + +An example is shown in +[websocket.lua](https://github.com/civetweb/civetweb/blob/master/test/websocket.lua). + + +# Common Problems +- PHP doesn't work - getting empty page, or 'File not found' error. The + reason for that is wrong paths to the interpreter. Remember that with PHP, + the correct interpreter is `php-cgi.exe` (`php-cgi` on UNIX). + Solution: specify the full path to the PHP interpreter, e.g.: + `civetweb -cgi_interpreter /full/path/to/php-cgi` + +- `php-cgi` is unavailable, for example on Mac OS X. As long as the `php` binary is installed, you can run CGI programs in command line mode (see the example below). Note that in this mode, `$_GET` and friends will be unavailable, and you'll have to parse the query string manually using [parse_str](http://php.net/manual/en/function.parse-str.php) and the `QUERY_STRING` environmental variable. + + #!/usr/bin/php + + +- Civetweb fails to start. If Civetweb exits immediately when started, this + usually indicates a syntax error in the configuration file + (named `civetweb.conf` by default) or the command-line arguments. + Syntax checking is omitted from Civetweb to keep its size low. However, + the Manual should be of help. Note: the syntax changes from time to time, + so updating the config file might be necessary after executable update. + +- Embedding with OpenSSL on Windows might fail because of calling convention. + To force Civetweb to use `__stdcall` convention, add `/Gz` compilation + flag in Visual Studio compiler. + diff --git a/src/civetweb/docs/_config.yml b/src/civetweb/docs/_config.yml new file mode 100644 index 000000000..259a24e4d --- /dev/null +++ b/src/civetweb/docs/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-tactile \ No newline at end of file diff --git a/src/civetweb/docs/api/mg_callbacks.md b/src/civetweb/docs/api/mg_callbacks.md new file mode 100644 index 000000000..c7d69a134 --- /dev/null +++ b/src/civetweb/docs/api/mg_callbacks.md @@ -0,0 +1,60 @@ +# Civetweb API Reference + +### `struct mg_callbacks;` + +### Fields + +| Field | Description | +| :--- | :--- | +|**`begin_request`**|**`int (*begin_request)( struct mg_connection *conn );`**| +| |The `begin_request()` callback function is called when CivetWeb has received a new HTTP request. If the callback function does not process the request, it should return 0. In that case CivetWeb will handle the request with the default callback routine. If the callback function returns a value between 1 and 999, CivetWeb does nothing and the callback function should do all the processing, including sending the proper HTTP headers etc. Starting at CivetWeb version 1.7, the function `begin_request()` is called before any authorization is done. If an authorization check is required, `request_handler()` should be used instead. The return value of the callback function is not only used to signal CivetWeb to not further process the request. The returned value is also stored as HTTP status code in the access log. | +|**`connection_close`**|**`void (*connection_close)( const struct mg_connection *conn );`**| +| |The callback function `connection_close()` is called when CivetWeb is closing a connection. The per-context mutex is locked when the callback function is invoked. The function is primarly useful for noting when a websocket is closing and removing it from any application-maintained list of clients. *Using this callback for websocket connections is deprecated. Use* `mg_set_websocket_handler()` *instead.*| +|**`end_request`**|**`void (*end_request)(const struct mg_connection *conn, int reply_status_code);`**| +| |The callback function `end_request()` is called by CivetWeb when a request has been completely processed. It sends the reply status code which was sent to the client to the application.| +|**`exit_context`**|**`void (*exit_context)( const struct mg_context *ctx );`**| +| |The callback function `exit_context()` is called by CivetWeb when the server is stopped. It allows the application to do some cleanup on the application side.| +|**`http_error`**|**`int (*http_error)( struct mg_connection *conn, int status );`**| +| |The callback function `http_error()` is called by CivetWeb just before an HTTP error is to be sent to the client. The function allows the application to send a custom error page. The status code of the error is provided as a parameter. If the application sends their own error page, it must return 1 to signal CivetWeb that no further processing is needed. If the returned value is 0, CivetWeb will send a built-in error page to the client.| +|**`init_context`**|**`void (*init_context)( const struct mg_context *ctx );`**| +| |The callback function `init_context()` is called after the CivetWeb server has been started and initialized, but before any requests are served. This allowes the application to perform some initialization activities before the first requests are handled.| +|**`init_lua`**|**`void (*init_lua)( const struct mg_connection *conn, void *lua_context );`**| +| |The callback function `init_lua()` is called just before a Lua server page is to be served. Lua page serving must have been enabled at compile time for this callback function to be called. The parameter `lua_context` is a `lua_State *` pointer.| +|**`init_ssl`**|**`int (*init_ssl)( void *ssl_context, void *user_data );`**| +| |The callback function `init_ssl()` is called when CivetWeb initializes the SSL library. The parameter `user_data` contains a pointer to the data which was provided to `mg_start()` when the server was started. The callback function can return 0 to signal that CivetWeb should setup the SSL certificate. With a return value of 1 the callback function signals CivetWeb that the certificate has already been setup and no further processing is necessary. The value -1 should be returned when the SSL initialization fails.| +|**`init_thread`**|**`void (*init_thread)( const struct mg_context *ctx, int thread_type );`**| +| |The callback function `init_thread()` is called when a new thread is created by CivetWeb. The `thread_type` parameter indicates which type of thread has been created. following thread types are recognized:| +| |**0** - The master thread is created | +| |**1** - A worker thread which handles client connections has been created| +| |**2** - An internal helper thread (timer thread) has been created| +|**`log_access`**|**`int (*log_access)( const struct mg_connection *conn, const char *message );`**| +| |The callback function `log_access()` is called when CivetWeb is about to log a message. If the callback function returns 0, CivetWeb will use the default internal access log routines to log the access. If a non-zero value is returned, CivetWeb assumes that access logging has already been done and no further action is performed.| +|**`log_message`**|**`int (*log_message)( const struct mg_connection *conn, const char *message );`**| +| |The callback function `log_message()` is called when CivetWeb is about to log a message. If the callback function returns 0, CivetWeb will use the default internal log routines to log the message. If a non-zero value is returned CivetWeb assumes that logging has already been done and no further action is performed.| +|**`open_file`**|**`const char *(*open_file)( const struct mg_connection *conn, const char *path, size_t *data_len );`**| +| |The callback function `open_file()` is called when a file is to be opened by CivetWeb. The callback can return a pointer to a memory location and set the memory block size in the variable pointed to by `data_len` to signal CivetWeb that the file should not be loaded from disk, but that instead a stored version in memory should be used. If the callback function returns NULL, CivetWeb will open the file from disk. This callback allows caching to be implemented at the application side, or to serve specific files from static memory instead of from disk.| +|~~`upload`~~|**`void (*upload)( struct mg_connection * conn, const char *file_name );`**| +| |*Deprecated. Use* `mg_handle_form_request()` *instead.* The callback function `upload()` is called when CivetWeb has uploaded a file to a temporary directory as result of a call to `mg_upload()`. The parameter `file_name` contains the full file name including path to the uploaded file.| +|~~`websocket_connect`~~|**`int (*websocket_connect)( const struct mg_connection *conn );`**| +| |*Deprecated. Use* `mg_set_websocket_handler()` *instead.* The callback function `websocket_connect()` is called when a websocket request is received, before the actual websocket handshake has taken place. The callback function can signal to CivetWeb if it should accept or deny the incoming request with one of the following return values: | +| |**0** - CivetWeb can proceed with the handshake to accept the connection | +| |**1** - CivetWeb must close the connection immediately without performing a handshake | +|~~`websocket_data`~~|**`int (*websocket_data)( struct mg_connection *conn, int bits, char *data, size_t data_len );`**| +| |*Deprecated. Use* `mg_set_websocket_handler()` *instead.* The callback function `websocket_data()` is called when a data frame has been received from the client. The parameters contain the following information: | +| | **`bits`** - The first byte of the websocket frame. See [RFC-6455](http://tools.ietf.org/html/rfc6455) at section 5.2 for more information. | +| | **`data`** - The pointer to the received data block. Masks--if any--have already been applied. | +| | **`data_len`** - The length of the received data block | +| | If the application wants to keep the websocket open to receive more data, the callback function should return the value **1**. If the value **0** is returned by the callback function, CivetWeb will close the websocket connection and no more frames will be received.| +|~~`websocket_ready`~~|**`int (*websocket_ready)( struct mg_connection *conn );`**| +| |*Deprecated. Use* `mg_set_websocket_handler()` *instead.* The callback function `websocket_ready()` is called after the handshake of a websocket connection has succeeded succesfully to signal the application that the connection is ready for use. | + +### Description + +Much of the functionality in the Civetweb library is provided through callback functions. The application registers their own processing functions with the Civetweb library and when an event happens, the appropriate callback function is called. In this way an application is able to have their processing code right at the heart of the webserver, without the need to change the code of the webserver itself. A number of callback functions are registered when the civetweb subsystem is started. Other may be added or changed at runtime with helper functions. + +A pointer to a `mg_callbacks` structure is passed as parameter to the [`mg_start()`](mg_start.md) function to provide links to callback functions which the webserver will call at specific events. If a specific callback function is not supplied, CivetWeb will fallback to default internal callback routines. Callback functions give the application detailed control over how specific events should be handled. + +### See Also + +* [`mg_start();`](mg_start.md) +* [`mg_stop();`](mg_stop.md) diff --git a/src/civetweb/docs/api/mg_check_digest_access_authentication.md b/src/civetweb/docs/api/mg_check_digest_access_authentication.md new file mode 100644 index 000000000..fec3ad3ba --- /dev/null +++ b/src/civetweb/docs/api/mg_check_digest_access_authentication.md @@ -0,0 +1,37 @@ +# Civetweb API Reference + +### `mg_check_digest_access_authentication( conn, realm, filename );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`| A pointer to the connection to be used to send data | +|**`realm`**|`const char *`| The requested authentication realm or NULL | +|**`filename`**|`const char *`| The path to the passwords file | + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`| An integer indicating success or failure | + +### Description + +This function can be used to check if a request header contains HTTP digest authentication +information, matching user and password encoded within the password file. +If the authentication realm (also called authentication domain) is NULL, the parameter +`authentication_domain` as specified in the server configuration (`mg_start()`) is used. + +A positive return value means, the user name, realm and a correct password hash have been +found in the passwords file. +A return of 0 means, reading the password file succeeded, but there was no matching user, +realm and password. +The function returns a negative number on errors. + +### See Also + +* [`mg_send_digest_access_authentication_request();`](mg_send_digest_access_authentication_request.md) +* [`mg_modify_passwords_file();`](mg_modify_passwords_file.md) +* [`mg_start();`](mg_start.md) + diff --git a/src/civetweb/docs/api/mg_check_feature.md b/src/civetweb/docs/api/mg_check_feature.md new file mode 100644 index 000000000..1da1b0721 --- /dev/null +++ b/src/civetweb/docs/api/mg_check_feature.md @@ -0,0 +1,40 @@ +# Civetweb API Reference + +### `mg_check_feature( feature );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`feature`**|`unsigned`| A value indicating the feature to be checked | + +### Return Value + +| Type | Description | +| :--- | :--- | +|`unsigned`| A value indicating if a feature is available. A positive value indicates available, while **0** is returned for an unavailable feature | + +### Description + +The function `mg_check_feature()` can be called from an application program to check of specific features have been compiled in the civetweb version which the application has been linked to. The feature to check is provided as an unsigned integer parameter. If the function is available in the currently linked library version, a value **> 0** is returned. Otherwise the function `mg_check_feature()` returns the value **0**. + +The following parameter values can be used: + +| Value | Compilation option | Description | +| :---: | :---: | :--- | +| **1** | NO_FILES | *Able to serve files*. If this feature is available, the webserver is able to serve files directly from a directory tree. | +| **2** | NO_SSL | *Support for HTTPS*. If this feature is available, the webserver van use encryption in the client-server connection. SSLv2, SSLv3, TLSv1.0, TLSv1.1 and TLSv1.2 are supported depending on the SSL library CivetWeb has been compiled with, but which protocols are used effectively when the server is running is dependent on the options used when the server is started. | +| **4** | NO_CGI | *Support for CGI*. If this feature is available, external CGI scripts can be called by the webserver. | +| **8** | USE_IPV6 | *Support IPv6*. The CivetWeb library is capable of communicating over both IPv4 and IPv6, but IPv6 support is only available if it has been enabled at compile time. | +| **16** | USE_WEBSOCKET | Support for web sockets. WebSockets support is available in the CivetWeb library if the proper options has been used during cimpile time. | +| **32** | USE_LUA | *Support for Lua scripts and Lua server pages*. CivetWeb supports server side scripting through the Lua language, if that has been enabled at compile time. Lua is an efficient scripting language which is less resource heavy than for example PHP. | +| **64** | USE_DUKTAPE | *Support for server side JavaScript*. Server side JavaScript can be used for dynamic page generation if the proper options have been set at compile time. Please note that client side JavaScript execution is always available if it has been enabled in the connecting browser. | +| **128** | NO_CACHING | *Support for caching*. The webserver will support caching, if it has not been disabled while compiling the library. | + +Parameter values other than the values mentioned above will give undefined results. Therefore—although the parameter values for the `mg_check_feature()` function are effectively bitmasks, you should't assume that combining two of those values with an OR to a new value will give any meaningful results when the function returns. + +### See Also + +* [`mg_get_option();`](mg_get_option.md) +* [~~`mg_get_valid_option_names();`~~](mg_get_valid_option_names.md) +* [`mg_get_valid_options();`](mg_get_valid_options.md) diff --git a/src/civetweb/docs/api/mg_client_cert.md b/src/civetweb/docs/api/mg_client_cert.md new file mode 100644 index 000000000..c81fbd080 --- /dev/null +++ b/src/civetweb/docs/api/mg_client_cert.md @@ -0,0 +1,21 @@ +# Civetweb API Reference + +### `struct mg_client_cert;` + +### Fields + +| Field | Type | Description | +| :--- | :--- | :--- | +|**`subject`**|`const char *`| The subject of the certificate | +|**`issuer`**|`const char *`| The issuer of the certificate | +|**`serial`**|`const char *`| The serial number of the certificate | +|**`finger`**|`const char *`| The fingerprint of the certificate | + +### Description + +The structure `client_cert` is used as a sub-structure in the [`mg_request_info`](mg_request_info.md) structure to store information of an optional client supplied certificate. + +### See Also + +* [`struct mg_request_info;`](mg_request_info.md) +* [`mg_get_request_info();`](mg_get_request_info.md) diff --git a/src/civetweb/docs/api/mg_client_options.md b/src/civetweb/docs/api/mg_client_options.md new file mode 100644 index 000000000..f3b9b6823 --- /dev/null +++ b/src/civetweb/docs/api/mg_client_options.md @@ -0,0 +1,21 @@ +# Civetweb API Reference + +### `struct mg_client_options;` + +### Fields + +| Field | Type | Description | +| :--- | :--- | :--- | +|**`host`**|`const char *`|The hostname or IP address to connect to| +|**`port`**|`int`|The port on the server| +|**`client_cert`**|`const char *`|Pointer to client certificate| +|**`server_cert`**|`const char *`|Pointer to a server certificate| + +### Description + +The the `mgclient_options` structure contains host and security information to connect as a client to another host. A parameter of this type is used in the call to the function [`mg_connect_client_secure();`](mg_connect_client_secure.md). Please note that IPv6 addresses are only permitted if IPv6 support was enabled during compilation. You can use the function [`mg_check_feature()`](mg_check_feature.md) with the parameter `USE_IPV6` while running your application to check if IPv6 is supported. + +### See Also + +* [`mg_check_feature();`](mg_check_feature.md) +* [`mg_connect_client_secure();`](mg_connect_client_secure.md) diff --git a/src/civetweb/docs/api/mg_close_connection.md b/src/civetweb/docs/api/mg_close_connection.md new file mode 100644 index 000000000..ca8d2a109 --- /dev/null +++ b/src/civetweb/docs/api/mg_close_connection.md @@ -0,0 +1,21 @@ +# Civetweb API Reference + +### `mg_close_connection( conn );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`|The connection which must be closed| + +### Return Value + +*none* + +### Description + +The function `mg_close_connection()` is used to close a connection which was opened with the [`mg_download()`](mg_download.md) function. Use of this function to close a connection which was opened in another way is undocumented and may give unexpected results. + +### See Also + +* [`mg_download();`](mg_download.md) diff --git a/src/civetweb/docs/api/mg_connect_client.md b/src/civetweb/docs/api/mg_connect_client.md new file mode 100644 index 000000000..e2aa77398 --- /dev/null +++ b/src/civetweb/docs/api/mg_connect_client.md @@ -0,0 +1,29 @@ +# Civetweb API Reference + +### `mg_connect_client( host, port, use_ssl, error_buffer, error_buffer_size );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`host`**|`const char *`|hostname or IP address of the server| +|**`port`**|`int`|The port to connect to on the server| +|**`use_ssl`**|`int`|Connects using SSL of this value is not zero| +|**`error_buffer`**|`char *`|Buffer to store an error message| +|**`error_buffer_size`**|`size_t`|Maximum size of the error buffer including the NUL terminator| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`struct mg_connection *`|| + +### Description + +The function `mg_connect_client()` connects to a TCP server as a client. This server can be a HTTP server but this is not necessary. The function returns a pointer to a connection structure when the connection is established and NULL otherwise. The host may be on IPv4 or IPv6, but IPv6 is not enabled in every Civetweb installation. Specifically the use of IPv6 communications has to be enabled when the library is compiled. At runtime you can use the [`mg_check_feature()`](mg_check_feature.md) function with the parameter `USE_IPV6` to check if IPv6 communication is supported. + +### See Also + +* [`mg_check_feature();`](mg_check_feature.md) +* [`mg_connect_client_secure();`](mg_connect_client_secure.md) +* [`mg_connect_websocket_client();`](mg_connect_websocket_client.md) diff --git a/src/civetweb/docs/api/mg_connect_client_secure.md b/src/civetweb/docs/api/mg_connect_client_secure.md new file mode 100644 index 000000000..a87949b6a --- /dev/null +++ b/src/civetweb/docs/api/mg_connect_client_secure.md @@ -0,0 +1,30 @@ +# Civetweb API Reference + +### `mg_connect_client_secure( client_options, error_buffer, error_buffer_size );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`client_options`**|`const struct mg_client_options *`|Settings about the server connection| +|**`error_buffer`**|`char *`|Buffer to store an error message| +|**`error_buffer_size`**|`size_t`|Size of the error message buffer including the NUL terminator| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`struct mg_connection *`|| + +### Description + +The function `mg_connect_client_secure()` creates a secure connection with a server. The information about the connection and server is passed in a structure and an error message may be returned in a local buffer. The function returns a pointer to a `struct mg_connection` structure when successful and NULL otherwise. + +Please note that IPv6 communication is supported by Civetweb, but only if the use of IPv6 was enabled at compile time. The check while running a program if IPv6 communication is possible you can call [`mg_check_feature()`](mg_check_feature.md) with the `USE_IPV6` parameter to check if IPv6 communications can be used. + +### See Also + +* [`struct mg_client_options;`](mg_client_options.md) +* [`mg_check_feature();`](mg_check_feature.md) +* [`mg_connect_client();`](mg_connect_client.md) +* [`mg_connect_websocket_client();`](mg_connect_websocket_client.md) diff --git a/src/civetweb/docs/api/mg_connect_websocket_client.md b/src/civetweb/docs/api/mg_connect_websocket_client.md new file mode 100644 index 000000000..8562b25a7 --- /dev/null +++ b/src/civetweb/docs/api/mg_connect_websocket_client.md @@ -0,0 +1,36 @@ +# Civetweb API Reference + +### `mg_connect_websocket_client( host, port, use_ssl, error_buffer, error_buffer_size, path, origin, data_func, close_func, user-data);` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`host`**|`const char *`|The hostname or IP address of the server| +|**`port`**|`int`|The port on the server| +|**`use_ssl`**|`int`|Use SSL if this parameter is not equal to zero| +|**`error_buffer`**|`char *`|Buffer to store an error message| +|**`error_buffer_size`**|`size_t`|Size of the error message buffer including the NUL terminator| +|**`path`**|`const char *`|The server path to connect to, for example `/app` if you want to connect to `localhost/app`| +|**`origin`**|`const char *`|The value of the `Origin` HTTP header| +|**`data_func`**|`mg_websocket_data_handler`|Callback which is used to process data coming back from the server| +|**`close_func`**|`mg_websocket_close_handler`|Callback which is called when the connection is to be closed| +|**`user_data`**|`void *`|User supplied argument| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`struct mg_connection *`|A pointer to the connection structure, or NULL if connecting failed| + +### Description + +The function `mg_connect_websocket_client()` connects to a websocket on a server as a client. Data and close events are processed with callback functions which must be provided in the call. + +Civetweb supports both IPv4 and IPv6 communication, but only if the use if IPv6 has been enabled at compile time. When running an application it is possible to check if IPv6 addressing is available by calling the [`mg_check_feature()`](mg_check_feature.md) function with the `USE_IPV6` parameter. + +### See Also + +* [`mg_check_feature();`](mg_check_feature.md) +* [`mg_connect_client();`](mg_connect_client.md) +* [`mg_connect_client_secure();`](mg_connect_client_secure.md) diff --git a/src/civetweb/docs/api/mg_cry.md b/src/civetweb/docs/api/mg_cry.md new file mode 100644 index 000000000..0cf45c9b1 --- /dev/null +++ b/src/civetweb/docs/api/mg_cry.md @@ -0,0 +1,23 @@ +# Civetweb API Reference + +### `mg_cry( conn, fmt, ... );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`const struct mg_connection *`|The connection on which a problem occured| +|**`fmt`**|`const char *`|Format string without a line return| +|**`...`**|*various*|Parameters depending on the format string| + +### Return Value + +*none* + +### Description + +The function `mg_cry()` is called when something happens on a connection. The function takes a format string similar to the `printf()` series of functions with parameters and creates a text string which can then be used for logging. The `mg_cry()` function prints the output to the opened error log stream. Log messages can be processed with the `log_message()` callback function specified in the `struct mg_callbacks` structure. + +### See Also + +* [`struct mg_callbacks;`](mg_callbacks.md) diff --git a/src/civetweb/docs/api/mg_download.md b/src/civetweb/docs/api/mg_download.md new file mode 100644 index 000000000..1dbded0c7 --- /dev/null +++ b/src/civetweb/docs/api/mg_download.md @@ -0,0 +1,37 @@ +# Civetweb API Reference + +### `mg_download( host, port, use_ssl, error_buffer, error_buffer_size, fmt, ... );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`host`**|`const char *`|The hostname or IP address of the server| +|**`port`**|`int`|The port number on the server| +|**`use_ssl`**|`int`|Use SSL if this value is not equal zero| +|**`error_buffer`**|`char *`|Buffer to store an error message| +|**`error_buffer_size`**|`size_t`|Size of the error message buffer including the terminating NUL| +|**`fmt`**|`const char *`|Format string specifying the remote command to execute| +|**`...`**|*various*|Parameters used in the format string| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`struct mg_connection *`|A pointer to the connection structure if successful and NULL otherwise| + +### Description + +The `mg_download()` function is used to download data from a remote webserver. The server address can either be specified as a hostname or IP address and SSL can be used if needed. If the function succeeds, a pointer is returned to a connection structure. The connection must be closed with a call to the [`mg_close_connection()`](mg_close_connection.md) function. + +The format string is a format string from the `printf()` series of functions to specify the remote command. An example to get the main index page from Google is the following call: + +`conn = mg_download( "google.com", 80, 0, ebuf, sizeof(ebuf), + "%s", "GET / HTTP/1.0\r\nHost: google.com\r\n\r\n" );` + +Please note that although Civetweb supports both IPv4 and IPv6 communication that IPv6 addressing is only available if it was enabled at compile time. When running an application it is possible to check if IPv6 support has been compiled in by using the [`mg_check_feature()`](md_check_feature.md) function with the parameter `USE_IPV6`. + +### See Also + +* [`mg_check_feature();`](mg_check_feature.md) +* [`mg_close_connection();`](mg_close_connection.md) diff --git a/src/civetweb/docs/api/mg_exit_library.md b/src/civetweb/docs/api/mg_exit_library.md new file mode 100644 index 000000000..264340368 --- /dev/null +++ b/src/civetweb/docs/api/mg_exit_library.md @@ -0,0 +1,29 @@ +# Civetweb API Reference + +### `mg_exit_library( );` + +### Parameters + +none + +### Return Value + +| Type | Description | +| :--- | :--- | +|`unsigned`| **0** is returned or error | + +### Description + +The function `mg_exit_library()` should be called from an application program, when the library should be unloaded. +It must be called only from one thread (it is not guaranteed to be thread safe). + +Only use `mg_exit_library( );` when you used [`mg_init_library( feature );`](api/mg_init_library.md) before. + +The library init and exit functions are new in version 1.9 (as dummy implementation) and effective only from version 1.10. +For compatibility reasons, other functions (such as [`mg_start();`](mg_start.md)) will initialize the required features as well, +but they will no longer do a de-initialization, leaving a memory leak when the library is unloaded. + +### See Also + +* [`mg_init_library( feature );`](mg_init_library.md) +* [`mg_check_feature( feature );`](mg_check_feature.md) diff --git a/src/civetweb/docs/api/mg_form_data_handler.md b/src/civetweb/docs/api/mg_form_data_handler.md new file mode 100644 index 000000000..2338b72a7 --- /dev/null +++ b/src/civetweb/docs/api/mg_form_data_handler.md @@ -0,0 +1,36 @@ +# Civetweb API Reference + +### `struct mg_form_data_handler;` + +### Fields + +|Field|Description| +|:---|:---| +|**`field_found`**|**`int field_found( const char *key, const char *filename, char *path, size_t pathlen, void *user_data )`**;| +||The callback function `field_found()` is called when a new field has been found. The return value of this callback is used to define how the field should be processed. The parameters contain the following information:| +||**`key`** - The name of the field as it was named with the `name` tag in the HTML source.| +||**`filename`** - The name of the file to upload. Please not that this parameter is only valid when the input type was set to `file`. Otherwise this parameter has the value `NULL`.| +||**`path`** - This is an output parameter used to store the full name of the file including the path to store an incoming file at the computer. This parameter must be provided by the application to Civetweb when a form field of type `file` is found. Please not that together with setting this parameter, the callback function must return `FORM_FIELD_STORAGE_STORE`.i With any other return value the contents of the `path` buffer is ignored by Civetweb.| +||**`pathlen`** - The length of the buffer where the output path can be stored.| +||**`user_data`** - A pointer to the value of the field `user_data` of the structure `struct mg_form_data_handler`.| +||The callback function `field_found()` can return the following values back to Civetweb:| +||**`FORM_FIELD_STORAGE_SKIP`** - Ignore the field and continue with processing the next field| +||**`FORM_FIELD_STORAGE_GET`** - Call the callback function `field_get()` to receive the form data| +||**`FORM_FIELD_STORAGE_STORE`** - Store a file as `path` and overwrite that file if it already exists| +||**`FORM_FIELD_STORAGE_ABORT`** - Stop parsing the request and ignore all remaining form fields| +|**`field_get`**|**`int field_get( const char *key, const char *value, size_t valuelen, void *user_data );`**| +|**`field_store`**|**`int field_store( const char *path, long long file_size, void *user_data );`**| +||If the callback function `field_found()` returned `FORM_FIELD_STORAGE_STORE`, Civetweb will try to store the received data in a file. If writing the file is successful, the callback function `field_store()` is called. This function is only called after completion of a full upload, not if a file has only partly been uploaded. When only part of a file is received, Civetweb will delete that partly upload in the background and not inform the main application through this callback. The following parameters are provided in the function call:| +||**`path`** -| +||**`file_size`** - The path on the server where the file was stored| +||**`user_data`** - The size of the stored file in bytes| +|**`user_data`**|**`void *`** The value of the field `user_data` when the callback functions were registered with a call to `mg_handle_form_request();`| +||The `user_data` field is a user supplied argument that will be passed as parameter to each of callback functions| + +### Description + +The structure `struct mg_form_data_handler` contains callback functions for handling form fields. Form fields give additional information back from a web page to the server which can be processed by these callback functions. + +### See Also + +* [`mg_handle_form_request();`](mg_handle_form_request.md) diff --git a/src/civetweb/docs/api/mg_get_builtin_mime_type.md b/src/civetweb/docs/api/mg_get_builtin_mime_type.md new file mode 100644 index 000000000..c9044ae1f --- /dev/null +++ b/src/civetweb/docs/api/mg_get_builtin_mime_type.md @@ -0,0 +1,24 @@ +# Civetweb API Reference + +### `mg_get_builtin_mime_type( file_name );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`file_name`**|`const char *`|The name of the file for which the MIME type has to be determined| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`const char *`|A text string describing the MIME type| + +### Description + +The function `mg_get_builtin_mime_type()` tries to determine the MIME type of a given file. If the MIME type cannot be determined, the value `text/plain` is returned. Please note that this function does not an intelligent check of the file contents. The MIME type is solely determined based on the file name extension. + +### See Also + +* [`mg_send_mime_file();`](mg_send_mime_file.md) +* [`mg_send_mime_file2();`](mg_send_mime_file2.md) diff --git a/src/civetweb/docs/api/mg_get_connection_info.md b/src/civetweb/docs/api/mg_get_connection_info.md new file mode 100644 index 000000000..14a2f68cb --- /dev/null +++ b/src/civetweb/docs/api/mg_get_connection_info.md @@ -0,0 +1,40 @@ +# Civetweb API Reference + +### `mg_get_connection_info( ctx, idx, buffer, buflen );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`ctx`**|`struct mg_context *`|The server context handle| +|**`idx`**|`int`|Connection index within the context| +|**`buffer**|`char *`|A string buffer to store the information| +|**`buflen**|`int`|Size of the string buffer (including space for a terminating 0)| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Available context information in bytes (excluding the terminating 0)| + +### Description + +The function `mg_get_connection_info()` returns statistics information collected for +a server connection index. This may be empty if the server has not been built with +statistics support (`#define USE_SERVER_STATS`). +If data is available, the returned string is in JSON format. The exact content may +vary, depending on the connection state and server version. + +### Note + +This is an experimental interface and may be changed, replaced +or even removed in the future. Currently the index `idx` must be +between `0` and `num_threads-1`. The thread is not locked for +performance reasons, so the information may be inconsistent +in rare cases. + +### See Also + +* [`mg_get_system_info();`](mg_get_system_info.md) +* [`mg_get_context_info();`](mg_get_context_info.md) + diff --git a/src/civetweb/docs/api/mg_get_context.md b/src/civetweb/docs/api/mg_get_context.md new file mode 100644 index 000000000..ce6cb2be2 --- /dev/null +++ b/src/civetweb/docs/api/mg_get_context.md @@ -0,0 +1,24 @@ +# Civetweb API Reference + +### `mg_get_context( conn );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`const struct mg_connection *`|The connection for which the context has to be returned| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`struct mg_context *`|A pointer to the context of the given connection| + +### Description + +The function `mg_get_context()` returns the context associated with a connection. + +### See Also + +* [`mg_start();`](mg_start.md) +* [`mg_stop();`](mg_stop.md) diff --git a/src/civetweb/docs/api/mg_get_context_info.md b/src/civetweb/docs/api/mg_get_context_info.md new file mode 100644 index 000000000..88ffcf742 --- /dev/null +++ b/src/civetweb/docs/api/mg_get_context_info.md @@ -0,0 +1,32 @@ +# Civetweb API Reference + +### `mg_get_context_info( ctx, buffer, buflen );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`ctx`**|`struct mg_context *`|The server context handle| +|**`buffer**|`char *`|A string buffer to store the information| +|**`buflen**|`int`|Size of the string buffer (including space for a terminating 0)| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Available context information in bytes (excluding the terminating 0)| + +### Description + +The function `mg_get_context_info()` returns statistics information collected for +the server context. This may be empty if the server has not been built with +statistics support (`#define USE_SERVER_STATS`). +If data is available, the returned string is in JSON format. The exact content may +vary, depending on the server state and server version. + +### See Also + +* [`mg_get_system_info();`](mg_get_system_info.md) +* [`mg_get_connection_info();`](mg_get_connection_info.md) + + diff --git a/src/civetweb/docs/api/mg_get_cookie.md b/src/civetweb/docs/api/mg_get_cookie.md new file mode 100644 index 000000000..a738bc07d --- /dev/null +++ b/src/civetweb/docs/api/mg_get_cookie.md @@ -0,0 +1,29 @@ +# Civetweb API Reference + +### `mg_get_cookie( cookie, var_name, buf, buf_len );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`cookie`**|`const char *`|The cookie name| +|**`var_name`**|`const char *`|The variable name| +|**`buf`**|`char *`|The buffer where to store the contents of the cookie| +|**`buf_len`**|`size_t`|The length of the cookie buffer, including the terminating NUL| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|The length of the cookie or an error code| + +### Description + +The function `mg_get_cookie()` tries to fetch the value of a certain cookie variable. The contents will either be stored in an application provided buffer, or an error code will be returned. The destination buffer is guaranteed to be NUL terminated if the pointer of the buffer is not a NULL pointer and the size of the buffer is at least one byte. + +If the function succeeds, the return value of the function is the length in bytes of the cookie. The value **`-1`** is returned if the requested cookie could not be found and **`-2`** if the destination buffer is represented by a NULL pointer, is zero length or too short to store the whole cookie. + +### See Also + +* [`mg_get_var();`](mg_get_var.md) +* [`mg_get_var2();`](mg_get_var2.md) diff --git a/src/civetweb/docs/api/mg_get_header.md b/src/civetweb/docs/api/mg_get_header.md new file mode 100644 index 000000000..8ad1810ac --- /dev/null +++ b/src/civetweb/docs/api/mg_get_header.md @@ -0,0 +1,22 @@ +# Civetweb API Reference + +### `mg_get_header( conn, name );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`| A pointer referencing the connection | +|**`name`**|`const char *`| The name of the request header | + +### Return Value + +| Type | Description | +| :--- | :--- | +|`const char *`| A pointer to the value of the request header, or NULL of no matching header count be found | + +### Description + +HTTP and HTTPS clients can send request headers to the server to provide details about the communication. These request headers can for example specify the preferred language in which the server should respond and the supported compression algorithms. The function `mg_get_header()` can be called to return the contents of a specific request header. The function will return a pointer to the value text of the header when succesful, and NULL of no matching request header from the client could be found. + +### See Also diff --git a/src/civetweb/docs/api/mg_get_option.md b/src/civetweb/docs/api/mg_get_option.md new file mode 100644 index 000000000..b731dd74f --- /dev/null +++ b/src/civetweb/docs/api/mg_get_option.md @@ -0,0 +1,24 @@ +# Civetweb API Reference + +### `mg_get_option( ctx, name );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`ctx`**|`const struct mg_context *`| A pointer to the webserver context | +|**`name`**|`const char *`| The name of the option to query | + +### Return Value + +| Type | Description | +| :--- | :--- | +|`const char *`| A pointer to the option value in text, or NULL if an error occured | + +### Description + +When starting the CivetWeb webserver, options are provided to set the wanted behaviour of the server. The options which were used during startup can be queried through the `mg_get_option()` function. Options are read-only and cannot be changed while the webserver is running. The function returns a pointer to a text string containing the value of the queried option, or NULL if an error occured. It is guaranteed however that if a valid option name is provided as a parameter to this function, that a pointer to a string is returned and not NULL. In case an option was empty or NULL during initialisation, `mg_get_option()` will return a pointer to an empty string. + +### See Also + +* [`mg_start();`](mg_start.md) diff --git a/src/civetweb/docs/api/mg_get_ports.md b/src/civetweb/docs/api/mg_get_ports.md new file mode 100644 index 000000000..ba492ca36 --- /dev/null +++ b/src/civetweb/docs/api/mg_get_ports.md @@ -0,0 +1,31 @@ +# Civetweb API Reference + +### ~~`mg_get_ports( ctx, size, ports, ssl );`~~ + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`ctx`**|`const struct mg_context *`|| +|**`size`**|`size_t`|The number of ports which can be stored in the buffer| +|**`ports`**|`int *`|Buffer for storage of the port numbers| +|**`ssl`**|`int *`|Buffer used to store if SSL is used for the ports| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`size_t`|The number of ports stored in the buffer| + +### Description + +This function is deprecated. Use [`mg_get_server_ports()`](mg_get_server_ports.md) instead. + +The function `mg_get_ports()` returns a list of ports the Civetweb server is listening on. The port numbers are stored in a buffer of integers which is supplied by the calling party. The function also stores information if SSL is used on the ports. This information is stored in a second buffer which should be capable of storing the same amount of items as the ports buffer. + +The function returns the number of ports actually stored in the buffer. + +### See Also + +* [`struct mg_server_ports;`](mg_server_ports.md) +* [`mg_get_server_ports();`](mg_get_server_ports.md) diff --git a/src/civetweb/docs/api/mg_get_request_info.md b/src/civetweb/docs/api/mg_get_request_info.md new file mode 100644 index 000000000..b7ddd07aa --- /dev/null +++ b/src/civetweb/docs/api/mg_get_request_info.md @@ -0,0 +1,28 @@ +# Civetweb API Reference + +### `mg_get_request_info( conn );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`const struct mg_connection *`|The connection for which the request info is needed| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`const struct mg_request_info *`|Pointer to the requested info, or NULL if an error occured| + +### Description + +The function `mg_get_request_info()` returns information about the request on a given connection. This information is returned as a pointer to a [`mg_request_info`](mg_request_info.md) structure. If an error occurs, a NULL pointer is returned instead. + +Use this function when implementing a server. + +### See Also + +* [`struct mg_request_info;`](mg_request_info.md) +* [`mg_get_response_info();`](mg_get_response_info.md) +* [`struct mg_response_info;`](mg_response_info.md) + diff --git a/src/civetweb/docs/api/mg_get_request_link.md b/src/civetweb/docs/api/mg_get_request_link.md new file mode 100644 index 000000000..d58a59285 --- /dev/null +++ b/src/civetweb/docs/api/mg_get_request_link.md @@ -0,0 +1,29 @@ +# Civetweb API Reference + +### `mg_get_request_link( conn, buf, buflen );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`| A pointer referencing the connection | +|**`buf`**|`char *`| A buffer to store the link | +|**`buflen`**|`size_t`| Size of the buffer | + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`| Return code: <0 for error, >=0 for success | + +### Description + +Store a formatted link corresponding to the current request. + +E.g., returns +`http://mydomain.com:8080/path/to/callback.ext` +or +`http://127.0.0.1:8080/path/to/callback.ext` +depending on the auth check settings. + +### See Also diff --git a/src/civetweb/docs/api/mg_get_response.md b/src/civetweb/docs/api/mg_get_response.md new file mode 100644 index 000000000..3e7b72711 --- /dev/null +++ b/src/civetweb/docs/api/mg_get_response.md @@ -0,0 +1,29 @@ +# Civetweb API Reference + +### `mg_get_response( conn, ebuf, ebuf_len, timeout );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`|The connection to listen on| +|**`ebuf`**|`char *`|Buffer to store an error message| +|**`ebuf_len`**|`size_t`|Size of the error message buffer including the terminating NUL| +|**`timeout`**|`int`|Time to wait for a response in milliseconds| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Success value of the wait| + +### Description + +The function `mg_get_reponse()` wait for a response from a remote server. A return value equal or greater than zero is an indication for success, a negative value us used to signal an error condition. A timeout can be specified which lets the function return after a specified number of milliseconds, even if no data is received from the remote party. If the timeout value is negative, the function will not return until data has been read or an unrecoverable error occurs. + +Error messages are stored in a caller supplied error message buffer. + +### See Also + +* [`mg_connect_client();`](mg_connect_client.md) +* [`mg_connect_client_secure();`](mg_connect_client_secure.md) diff --git a/src/civetweb/docs/api/mg_get_response_code_text.md b/src/civetweb/docs/api/mg_get_response_code_text.md new file mode 100644 index 000000000..79ffa13ce --- /dev/null +++ b/src/civetweb/docs/api/mg_get_response_code_text.md @@ -0,0 +1,25 @@ +# Civetweb API Reference + +### `mg_get_response_code_text( conn, response_code );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`| A pointer referencing the connection | +|**`response_code`**|`int`| Response code for which the text is queried | + +### Return Value + +| Type | Description | +| :--- | :--- | +|`const char *`| A pointer to a human readable text explaining the response code. | + +### Description + +The function `mg_get_response_code_text()` returns a pointer to a human readable text describing the HTTP response code which was provided as a parameter. + +### See Also + +* [`mg_get_builtin_mime_type();`](mg_get_builtin_mime_type.md) +* [`mg_version();`](mg_version.md) diff --git a/src/civetweb/docs/api/mg_get_response_info.md b/src/civetweb/docs/api/mg_get_response_info.md new file mode 100644 index 000000000..40e7ab88a --- /dev/null +++ b/src/civetweb/docs/api/mg_get_response_info.md @@ -0,0 +1,29 @@ +# Civetweb API Reference + +### `mg_get_response_info( conn );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`const struct mg_connection *`|The connection for which the response info is needed| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`const struct mg_request_info *`|Pointer to the requested info, or NULL if an error occured| + +### Description + +The function `mg_response_info()` returns information about a response on a client connection opened by `mg_connect_client()`. If an error occurs, a NULL pointer is returned instead. + +Use this function when implementing a client. + +### See Also + +* [`struct mg_response_info;`](mg_response_info.md) +* [`mg_connect_client();`](mg_connect_client.md) +* [`mg_get_request_info();`](mg_get_request_info.md) +* [`struct mg_request_info;`](mg_request_info.md) + diff --git a/src/civetweb/docs/api/mg_get_server_ports.md b/src/civetweb/docs/api/mg_get_server_ports.md new file mode 100644 index 000000000..323b47011 --- /dev/null +++ b/src/civetweb/docs/api/mg_get_server_ports.md @@ -0,0 +1,28 @@ +# Civetweb API Reference + +### `mg_get_server_ports( ctx, size, ports );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`ctx`**|`const struct mg_context *`|The context for which the server ports are requested| +|**`size`**|`int`|The size of the buffer to store the port information| +|**`ports`**|`struct mg_server_ports *`|Buffer to store the port information| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|The actual number of ports returned, or an error condition| + +### Description + +The `mg_get_server_ports()` returns a list with server ports on which the Civetweb server is listening. The ports are returned for a given context and stored with additional information like the SSL and redirection state in a list of structures. The list of structures must be allocated by the calling routine. The size of the structure is also passed to `mg_get_server_ports()`. + +The function returns the number of items in the list, or a negative value if an error occured. + +### See Also + +* [~~`mg_get_ports();`~~](mg_get_ports.md) +* [`struct mg_server_ports;`](mg_server_ports.md) diff --git a/src/civetweb/docs/api/mg_get_system_info.md b/src/civetweb/docs/api/mg_get_system_info.md new file mode 100644 index 000000000..6add98d5a --- /dev/null +++ b/src/civetweb/docs/api/mg_get_system_info.md @@ -0,0 +1,30 @@ +# Civetweb API Reference + +### `mg_get_system_info( buffer, buflen );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`buffer**|`char *`|A string buffer to store the information| +|**`buflen**|`int`|Size of the string buffer (including space for a terminating 0)| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Available system information in bytes (excluding the terminating 0)| + +### Description + +The function `mg_get_system_info()` returns information collected for the system +(operating system, compiler, version, ...). +Currently this data is returned as string is in JSON format, but changes to the +format are possible in future versions. The exact content of the JSON object may vary, +depending on the operating system and server version. +This string should be included for support requests. + +### See Also + +* [`mg_get_context_info();`](mg_get_context_info.md) + diff --git a/src/civetweb/docs/api/mg_get_user_connection_data.md b/src/civetweb/docs/api/mg_get_user_connection_data.md new file mode 100644 index 000000000..2e24c1340 --- /dev/null +++ b/src/civetweb/docs/api/mg_get_user_connection_data.md @@ -0,0 +1,23 @@ +# Civetweb API Reference + +### `mg_get_user_connection_data( conn );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`const struct mg_connection *`|The connection for which to return the user data| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`void *`|A pointer to the user data, or NULL if no user data was registered with the connection| + +### Description + +The function `mg_get_user_connection_data()` returns the user data associated with a connection. This user data is represented with a pointer which has been prevously registered with a call to [`mg_set_user_connection_data();`](mg_set_user_connection_data.md). With this function it is possible to pass state information between callback functions refering to a specific connection. + +### See Also + +* [`mg_set_user_connection_data();`](mg_set_user_connection_data.md) diff --git a/src/civetweb/docs/api/mg_get_user_data.md b/src/civetweb/docs/api/mg_get_user_data.md new file mode 100644 index 000000000..87bbe04fa --- /dev/null +++ b/src/civetweb/docs/api/mg_get_user_data.md @@ -0,0 +1,23 @@ +# Civetweb API Reference + +### `mg_get_user_data( ctx );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`ctx`**|`const struct mg_context *`|The context for which the user data is requested| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`void *`|| + +### Description + +The function `mg_get_user_data()` returns the user data associated with a Civetweb context. This is a pointer value which has previously been used in the call to [`mg_start()`](mg_start.md) to initialize the server context. + +### See Also + +* [`mg_start();`](mg_start.md) diff --git a/src/civetweb/docs/api/mg_get_valid_option_names.md b/src/civetweb/docs/api/mg_get_valid_option_names.md new file mode 100644 index 000000000..c6b43ad7c --- /dev/null +++ b/src/civetweb/docs/api/mg_get_valid_option_names.md @@ -0,0 +1,24 @@ +# Civetweb API Reference + +### ~~`mg_get_valid_option_names();`~~ + +### Parameters + +*none* + +### Return Value + +| Type | Description | +| :--- | :--- | +|`const char **`|An array with strings where the even elements represent the option names, and the odd element the option values The array is NULL terminated.| + +### Description + +The function `mg_get_valid_option_names()` is depricated. Use [`mg_get_valid_options()`](mg_get_valid_options.md) instead. + +This function returns an array with option/value pairs describing the valid configuration options for Civetweb. En element value of NULL signals the end of the list. + +### See Also + +* [`struct mg_option;`](mg_option.md) +* [`mg_get_valid_options();`](mg_get_valid_options.md) diff --git a/src/civetweb/docs/api/mg_get_valid_options.md b/src/civetweb/docs/api/mg_get_valid_options.md new file mode 100644 index 000000000..fd6755eb5 --- /dev/null +++ b/src/civetweb/docs/api/mg_get_valid_options.md @@ -0,0 +1,22 @@ +# Civetweb API Reference + +### `mg_get_valid_options();` + +### Parameters + +*none* + +### Return Value + +| Type | Description | +| :--- | :--- | +|`const struct mg_option *`|An array with all valid configuration options| + +### Description + +The function `mg_get_valid_options()` returns an array with all valid configuration options of Civetweb. Each element in the array is a structure with three fields which represent the name of the option, the value of the option and the type of the value. The array is terminated with an element for which the name is `NULL`. See for more details about this structure the documentation of [`struct mg_option`](mg_option.md). + +### See Also + +* [`struct mg_option;`](mg_option.md) +* [`mg_start();`](mg_start.md) diff --git a/src/civetweb/docs/api/mg_get_var.md b/src/civetweb/docs/api/mg_get_var.md new file mode 100644 index 000000000..c21c7bbd3 --- /dev/null +++ b/src/civetweb/docs/api/mg_get_var.md @@ -0,0 +1,30 @@ +# Civetweb API Reference + +### `mg_get_var( data, data_len, var_name, dst, dst_len );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`data`**|`const char *`|Encoded buffer from either POST data or the URI of a GET call| +|**`data_len`**|`size_t`|Size of the encode buffer including the terminating NULL| +|**`var_name`**|`const char *`|Name of the variable to search for| +|**`dst`**|`char *`|Output buffer to store the content of the variable| +|**`dst_len`**|`size_t`|Length of the output buffer| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|The length of the variable or an error code| + +### Description + +The function `mg_get_var()` returns the value of a variable which is passed to the server with either a POST method, or as a parameter in the URI of a GET call. The data pointer passed to the function points to a form-URI encoded buffer. This can either be POST data or the `request_info.query_string`. The name of the searched variable and a buffer to store the results are also parameters to the function. + +The function either returns the length of the variable when successful, **`-1`** if the variable could not be found and **`-2`** if the destination buffer is NULL, has size zero or is too small to store the resulting variable value. + +### See Also + +* [`mg_get_cookie();`](mg_get_cookie.md) +* [`mg_get_var2();`](mg_get_var2.md) diff --git a/src/civetweb/docs/api/mg_get_var2.md b/src/civetweb/docs/api/mg_get_var2.md new file mode 100644 index 000000000..3a47d91a9 --- /dev/null +++ b/src/civetweb/docs/api/mg_get_var2.md @@ -0,0 +1,31 @@ +# Civetweb API Reference + +### `mg_get_var2( data, data_len, var_name, dst, dst_len, occurrence );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`data`**|`const char *`|Encoded data buffer from either POST data or a GET URI| +|**`data_len`**|`size_t`|The size of the encoded data buffer| +|**`var_name`**|`const char *`|The name of the variable to search for| +|**`dst`**|`char *`|Destination buffer to store the variable content| +|**`dst_len`**|`size_t`|The size of the destination buffer including the terminating NUL| +|**`occurrence`**|`size_t`|The instance index of the wanted variable| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Length of the variable contents, or an error code| + +### Description + +The function `mg_get_var2()` can be used to return the contents of a variable passed to the server as either POST data, or in the URI in a GET call. The function is somilar to [`mg_get_var()`](mg_get_var.md) but the difference is that `mg_get_var2()` can be used if the same variable is present multiple times in the data. The `occurence` parameter is used to identify which instance of the variable must be returned where **`0`** is used for the first variable with the specified name, **`1`** for the second and so on. + +The function returns the length of the variable content in the return buffer, **`-1`** if a variable with the specified name could not be found and **`-2`** if the pointer to the result buffer is NULL, the size of the result buffer is zero or when the result buffer is too small to contain the variable content and terminating NUL. + +### See Also + +* [`mg_get_cookie();`](mg_get_cookie.md) +* [`mg_get_var();`](mg_get_var.md) diff --git a/src/civetweb/docs/api/mg_handle_form_request.md b/src/civetweb/docs/api/mg_handle_form_request.md new file mode 100644 index 000000000..4a1d888e3 --- /dev/null +++ b/src/civetweb/docs/api/mg_handle_form_request.md @@ -0,0 +1,24 @@ +# Civetweb API Reference + +### `mg_handle_form_request( conn, fdh );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`|The connection on which form data must be processed| +|**`fdh`**|`struct mg_form_data_handler`|Structure with callback functions to to the heavy work| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|The number of fields processed, or an error code| + +### Description + +The function `mg_handle_form_request()` processes form data on a connection. The function uses callback functions for the heavy lifting which are passed to the function as fields in a [`struct mg_form_data_handler`](mg_form_data_handler.md) structure. The number of processed fields is returned by the function, or a negative value when an error occured. I nthe situation where some fields are processed successfully (for example file downloads) and an error occurs later in the form processing, the function still returns a negative value. It is the responsibility of the calling party to do the necessary cleanup. The calling party should also do the cleanup of any files which are created, but not required anymore later. + +### See Also + +* [`struct mg_form_data_handler;`](mg_form_data_handler.md) diff --git a/src/civetweb/docs/api/mg_header.md b/src/civetweb/docs/api/mg_header.md new file mode 100644 index 000000000..403dc885f --- /dev/null +++ b/src/civetweb/docs/api/mg_header.md @@ -0,0 +1,18 @@ +# Civetweb API Reference + +### `struct mg_header;` + +### Fields + +| Field | Type | Description | +| :--- | :--- | :--- | +|**`name`**|`const char *`| The name of the client request header | +|**`value`**|`const char *`| The value of the client request header | + +### Description + +The structure `mg_header` is used as a sub-structure in the [`struct mg_request_info;`](mg_request_info.md) structure to store the name and value of one HTTP request header as sent by the client. + +### See Also + +* [`struct mg_request_info;`](mg_request_info.md) diff --git a/src/civetweb/docs/api/mg_init_library.md b/src/civetweb/docs/api/mg_init_library.md new file mode 100644 index 000000000..f0d2c4691 --- /dev/null +++ b/src/civetweb/docs/api/mg_init_library.md @@ -0,0 +1,44 @@ +# Civetweb API Reference + +### `mg_init_library( feature );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`feature`**|`unsigned`| A bitmask indicating the features to be ininialized | + +### Return Value + +| Type | Description | +| :--- | :--- | +|`unsigned`| A value indicating the initialized features is available. **0** is returned or error | + +### Description + +The function `mg_init_library()` should be called from an application program before using any other function. +It must be called only from one thread (it is not guaranteed to be thread safe). + +This function is new in version 1.9 (as dummy implementation) and effective only from version 1.10. +For compatibility reasons, other functions (such as [`mg_start();`](mg_start.md)) will initialize the required features as well, +but they will no longer do a de-initialization, leaving a memory leak when the library is unloaded. + +The following parameter values can be used: + +| Value | Compilation option | Description | +| :---: | :---: | :--- | +| **1** | NO_FILES | *Able to serve files*. If this feature is available, the webserver is able to serve files directly from a directory tree. | +| **2** | NO_SSL | *Support for HTTPS*. If this feature is available, the webserver van use encryption in the client-server connection. SSLv2, SSLv3, TLSv1.0, TLSv1.1 and TLSv1.2 are supported depending on the SSL library CivetWeb has been compiled with, but which protocols are used effectively when the server is running is dependent on the options used when the server is started. | +| **4** | NO_CGI | *Support for CGI*. If this feature is available, external CGI scripts can be called by the webserver. | +| **8** | USE_IPV6 | *Support IPv6*. The CivetWeb library is capable of communicating over both IPv4 and IPv6, but IPv6 support is only available if it has been enabled at compile time. | +| **16** | USE_WEBSOCKET | Support for web sockets. WebSockets support is available in the CivetWeb library if the proper options has been used during cimpile time. | +| **32** | USE_LUA | *Support for Lua scripts and Lua server pages*. CivetWeb supports server side scripting through the Lua language, if that has been enabled at compile time. Lua is an efficient scripting language which is less resource heavy than for example PHP. | +| **64** | USE_DUKTAPE | *Support for server side JavaScript*. Server side JavaScript can be used for dynamic page generation if the proper options have been set at compile time. Please note that client side JavaScript execution is always available if it has been enabled in the connecting browser. | +| **128** | NO_CACHING | *Support for caching*. The webserver will support caching, if it has not been disabled while compiling the library. | + +The parameters can added using bitwise or. Values above 255 are reserved, the behavior of the function is undefined if any unknown bit is set. + +### See Also + +* [`mg_check_feature( feature );`](api/mg_check_feature.md) +* [`mg_exit_library( feature );`](api/mg_exit_library.md) diff --git a/src/civetweb/docs/api/mg_lock_connection.md b/src/civetweb/docs/api/mg_lock_connection.md new file mode 100644 index 000000000..167f9228c --- /dev/null +++ b/src/civetweb/docs/api/mg_lock_connection.md @@ -0,0 +1,26 @@ +# Civetweb API Reference + +### `mg_lock_connection( conn );` + +### Parameters + +| Parameter | Type | Description | +|**`conn`**|`struct mg_connection *`|The connection to retrieve a lock| + +### Return Value + +*none* + +### Description + +The function `mg_lock_connection()` is specifically for websocket connections to lock connection. Using this function in combination with [`mg_unlock_connection();`](mg_unlock_connection.md) is necessary around [`mg_write()`](mg_write.md) and [`mg_printf()`](mg_printf.md) calls if the code has server-initiated communication, as well as with communication in direct response to a message. + +### See Also + +* [`mg_lock_context();`](mg_lock_context.md) +* [`mg_printf();`](mg_printf.md) +* [`mg_unlock_connection();`](mg_unlock_connection.md) +* [`mg_unlock_context();`](mg_unlock_context.md) +* [`mg_websocket_client_write();`](mg_websocket_client_write.md) +* [`mg_websocket_write();`](mg_websocket_write.md) +* [`mg_write();`](mg_write.md) diff --git a/src/civetweb/docs/api/mg_lock_context.md b/src/civetweb/docs/api/mg_lock_context.md new file mode 100644 index 000000000..8be6bac72 --- /dev/null +++ b/src/civetweb/docs/api/mg_lock_context.md @@ -0,0 +1,23 @@ +# Civetweb API Reference + +### `mg_lock_context( ctx );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`ctx`**|`struct mg_context *`|The context to put the lock on| + +### Return Value + +*none* + +### Description + +The function `mg_lock_context()` can be used to acquire a lock for exclusive access to resources which are shared between connection of threads. The lock is context wide. The lock must be released with a call to [`mg_unlock_context()`](mg_unlock_context.md). + +### See Also + +* [`mg_lock_connection();`](mg_lock_connection.md) +* [`mg_unlock_connection();`](mg_unlock_connection.md) +* [`mg_unlock_context();`](mg_unlock_context.md) diff --git a/src/civetweb/docs/api/mg_md5.md b/src/civetweb/docs/api/mg_md5.md new file mode 100644 index 000000000..067a6a2f8 --- /dev/null +++ b/src/civetweb/docs/api/mg_md5.md @@ -0,0 +1,24 @@ +# Civetweb API Reference + +### `mg_md5( buf, ... );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`buf`**|`char[33]`|Storage buffer for the calculated MD5 sum| +|**`...`**|`char *, ...`|NULL terminated list of pointers to strings with data| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`char *`|Pointer to the resulting MD5 string| + +### Description + +The function `mg_md5()` caluclates the MD5 checksum of a NULL terminated list of NUL terminated ASCII strings. The MD5 checksum is returned in human readable format as an MD5 string in a caller supplied buffer. + +The function returns a pointer to the supplied result buffer. + +### See Also diff --git a/src/civetweb/docs/api/mg_modify_passwords_file.md b/src/civetweb/docs/api/mg_modify_passwords_file.md new file mode 100644 index 000000000..edb5f0214 --- /dev/null +++ b/src/civetweb/docs/api/mg_modify_passwords_file.md @@ -0,0 +1,33 @@ +# Civetweb API Reference + +### `mg_modify_passwords_file( passwords_file_name, domain, user, password );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`passwords_file_name`**|`const char *`|The path to the passwords file| +|**`realm`**|`const char *`|The authentication realm (domain) of the user record| +|**`user`**|`const char *`|Username of the record to be added, changed or deleted| +|**`password`**|`const char *`|Password associated with the user or NULL if the record must be deleted| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Success or error code| + +### Description + +The function `mg_modify_passwords_file()` allows an application to manipulate .htpasswd files on the fly by adding, deleting and changing user records. This is one of the several ways to implement authentication on the server side. + +If the password parameter is not `NULL` an entry is added to the password file. An existing records is modified in that case. If `NULL` is used as the password the enrty is removed from the file. + +The function returns 1 when successful and 0 if an error occurs. + +### See Also + +* [`mg_check_digest_access_authentication();`](mg_check_digest_access_authentication.md) +* [`mg_send_digest_access_authentication_request();`](mg_send_digest_access_authentication_request.md) + + diff --git a/src/civetweb/docs/api/mg_option.md b/src/civetweb/docs/api/mg_option.md new file mode 100644 index 000000000..dd7f090f8 --- /dev/null +++ b/src/civetweb/docs/api/mg_option.md @@ -0,0 +1,31 @@ +# Civetweb API Reference + +### `struct mg_option;` + +### Fields + +| Field | Type | Description | +| :--- | :--- | :--- | +|**`name`**|`const char *`|Name of the option| +|**`type`**|`int`|Type of the option| +|**`default_value`**|`const char *`|Value of the option| + +### Description + +A list of valid configuration options of the Civetweb instance can be retrieved with a call to [`mg_get_valid_options()`](mg_get_valid_options.md). This function fills a list of `struct mg_option` structures where the content of each structure represents a configuration option. Each structure contains three fields. One field contains the name of the option, the second contains the value of the option and the third is an identifier used to define the type of the option and how the value contents should be interpreted. + +The field `type` can be one of the following values: + +|Value|Description| +| :--- | :--- | +|**`CONFIG_TYPE_UNKNOWN`**|The type of the option value is unknown| +|**`CONFIG_TYPE_NUMBER`**|The option value is an integer| +|**`CONFIG_TYPE_STRING`**|The option value is a number| +|**`CONFIG_TYPE_FILE`**|The option value is a file name| +|**`CONFIG_TYPE_DIRECTORY`**|The option value is a directory name| +|**`CONFIG_TYPE_BOOLEAN`**|The option value is a boolean| +|**`CONFIG_TYPE_EXT_PATTERN`**|The option value is a list of regular expression patterns| + +### See Also + +* [`mg_get_valid_options();`](mg_get_valid_options.md) diff --git a/src/civetweb/docs/api/mg_printf.md b/src/civetweb/docs/api/mg_printf.md new file mode 100644 index 000000000..8beb9c076 --- /dev/null +++ b/src/civetweb/docs/api/mg_printf.md @@ -0,0 +1,27 @@ +# Civetweb API Reference + +### `mg_printf( conn, fmt, ... );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`|The connection over which the data must be sent| +|**`fmt`**|`const char *`|Format string| +|**`...`**|*various*|Parameters as specified in the format string| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Number of bytes written or an error code| + +### Description + +The function `mg_printf()` can be used to send formatted strings over a connection. The functionality is comparable to the `printf()` family of functions in the standard C library. The function returns **0** when the connection has been closed, **-1** if an error occurred and otherwise the number of bytes written over the connection. Except for the formatting part, the `mg_printf()` function is identical to the function [`mg_write()`](mg_write.md). + +### See Also + +* [`mg_websocket_client_write();`](mg_websocket_client_write.md) +* [`mg_websocket_write();`](mg_websocket_write.md) +* [`mg_write();`](mg_write.md) diff --git a/src/civetweb/docs/api/mg_read.md b/src/civetweb/docs/api/mg_read.md new file mode 100644 index 000000000..c7ec6c3df --- /dev/null +++ b/src/civetweb/docs/api/mg_read.md @@ -0,0 +1,26 @@ +# Civetweb API Reference + +### `mg_read( conn, buf, len );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`| A pointer referencing the connection | +|**`buf`**|`void *`| A pointer to the location where the received data can be stored | +|**`len`**|`size_t`| The maximum number of bytes to be stored in the buffer | + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`| The number of read bytes, or a status indication | + +### Description + +The function `mg_read()` receives data over an existing connection. The data is handled as binary and is stored in a buffer whose address has been provided as a parameter. The function returns the number of read bytes when successful, the value **0** when the connection has been closed by peer and a negative value when no more data could be read from the connection. + +### See Also + +* [`mg_printf();`](mg_printf.md) +* [`mg_write();`](mg_write.md) diff --git a/src/civetweb/docs/api/mg_request_info.md b/src/civetweb/docs/api/mg_request_info.md new file mode 100644 index 000000000..cad6bbc3c --- /dev/null +++ b/src/civetweb/docs/api/mg_request_info.md @@ -0,0 +1,35 @@ +# Civetweb API Reference + +### `struct mg_request_info;` + +### Fields + +| Field | Type | Description | +| :--- | :--- | :--- | +|**`request_method`**|`const char *`| The request method used by the client for the connection this can be **GET**, **POST** or one of the other common HTTP request methods | +|**`request_uri`**|`const char *`| The absolute, relative or URL-encoded URI as it was sent in the request. Example: "http://mydomain.com:8080/path/to/file.ext" or "/path/to/file.ext", depending on the client. | +|**`local_uri`**|`const char *`| The relative URL-encoded URI as it references the local resource. If the request URI does not reference a resource on the local server, this field is NULL. Example: "/path/to/file.ext" (even if the client used "http://mydomain.com:8080/path/to/file.ext" in the request) | +|~~`uri`~~|`const char *`| *Deprecated. Use* `local_uri` *instead* | +|**`http_version`**|`const char *`| The HTTP version as mentioned in the client request. This can be "1.0", "1.1", etc. | +|**`query_string`**|`const char *`| The HTTP query string, defined as URL part after the first '?' character, not including '?'. NULL if there is no '?'. | +|**`remote_user`**|`const char *`| The name of the authenticated remote user, or NULL if no authentication was used. Only used for HTTP (digest) authentication, not for cookie based authentication. | +|**`remote addr`**|`char[48]`| The IP address of the remote client as a string. This can either represent an IPv4 or an IPv6 address. Example: "127.0.0.1" | +|~~`remote_ip`~~|`long`| *Deprecated. Use* `remote_addr` *instead* | +|**`content_length`**|`long long`| The content length of the request body. This value can be -1 if no content length was provided. The request may still have body data, but the server cannot determine the length until all data has arrived (e.g. when the client closes the connection, or the final chunk of a chunked request has been received). | +|**`remote_port`**|`int`| The port number at the client's side (an integer number between 1 and 65535). | +|**`is_ssl`**|`int`| 1 if the connection is over SSL (https), and 0 if it is a plain connection (http) | +|**`user_data`**|`void *`| A pointer to the `user_data` information which was provided as a parameter to `mg_start()`. | +|**`conn_data`**|`void *`| A pointer to connection specific user data | +|**`num_headers`**|`int`| The number of HTTP request headers sent by the client (see http_headers) | +|**`http_headers`**|`struct mg_header[64]`| Array of structures with the HTTP request headers sent by the client. For the number of filled header fields, ee num_headers. | +|**`client_cert`**|`struct mg_client_cert *`| Pointer to the client certificate information, when available. This field is only filled for https connections using client certificates. | + +### Description + +The `mg_request_info` structure contains the client information of an existing connection. + +### See Also + +* [`struct mg_client_cert;`](mg_client_cert.md) +* [`struct mg_header;`](mg_header.md) +* [`mg_get_request_info();`](mg_get_request_info.md) diff --git a/src/civetweb/docs/api/mg_response_info.md b/src/civetweb/docs/api/mg_response_info.md new file mode 100644 index 000000000..a348dfcd9 --- /dev/null +++ b/src/civetweb/docs/api/mg_response_info.md @@ -0,0 +1,38 @@ +# Civetweb API Reference + +### `struct mg_response_info;` + +### Fields + +struct mg_response_info { + int status_code; /* E.g. 200 */ + const char *status_text; /* E.g. "OK" */ + const char *http_version; /* E.g. "1.0", "1.1" */ + + long long content_length; /* Length (in bytes) of the request body, + can be -1 if no length was given. */ + + int num_headers; /* Number of HTTP headers */ + struct mg_header + http_headers[MG_MAX_HEADERS]; /* Allocate maximum headers */ +}; + +| Field | Type | Description | +| :--- | :--- | :--- | +|**`status code`**|`int`| The HTTP response code received by the client. | +|**`status_text`**|`const char *`| The textual representation of the HTTP status code. | +|**`http_version`**|`const char *`| The HTTP version as mentioned in the client request. This can be "1.0", "1.1", etc. | +|**`content_length`**|`long long`| The content length of the request body. This value can be -1 if no content length was provided. The request may still have body data, but the server cannot determine the length until all data has arrived (e.g. when the client closes the connection, or the final chunk of a chunked request has been received). | +|**`num_headers`**|`int`| The number of HTTP request headers sent by the client (see http_headers) | +|**`http_headers`**|`struct mg_header[64]`| Array of structures with the HTTP request headers sent by the client. For the number of filled header fields, ee num_headers. | + +Note: This structure is not yet feature complete and will be extended in future versions. + +### Description + +The `mg_response_info` structure contains information on a completed request from a client. + +### See Also + +* [`struct mg_header;`](mg_header.md) +* [`mg_get_response_info();`](mg_get_response_info.md) diff --git a/src/civetweb/docs/api/mg_send_chunk.md b/src/civetweb/docs/api/mg_send_chunk.md new file mode 100644 index 000000000..895ffdc30 --- /dev/null +++ b/src/civetweb/docs/api/mg_send_chunk.md @@ -0,0 +1,31 @@ +# Civetweb API Reference + +### `mg_send_chunk( conn, buf, len );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`| A pointer to the connection to be used to send data | +|**`chunk`**|`const void *`| A pointer to the blob of information to be sent | +|**`chunk_len`**|`size_t`| The amount of bytes to be sent | + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`| An integer indicating the amount of bytes sent, or failure | + +### Description + +The function `mg_send_chunk()` can be used to send a blob of arbitrary data over a connection. +Only use this function after sending a complete HTTP request or response header with "Transfer-Encoding: chunked" set. Otherwise: use `mg_write()`. +The function returns a number **>0** if data was sent, the value **0** when the connection has been closed, and **-1** in case of an error. + +### See Also + +* [`mg_write();`](mg_write.md) +* [`mg_printf();`](mg_print.md) +* [`mg_lock_connection();`](mg_lock_connection.md) +* [`mg_unlock_connection();`](mg_unlock_connection.md) + diff --git a/src/civetweb/docs/api/mg_send_digest_access_authentication_request.md b/src/civetweb/docs/api/mg_send_digest_access_authentication_request.md new file mode 100644 index 000000000..d5af9dad1 --- /dev/null +++ b/src/civetweb/docs/api/mg_send_digest_access_authentication_request.md @@ -0,0 +1,35 @@ +# Civetweb API Reference + +### `mg_send_digest_access_authentication_request( conn, realm );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`| A pointer to the connection to be used to send data | +|**`realm`**|`const char *`| The requested authentication realm or NULL | + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`| An integer indicating success or failure | + +### Description + +This function can be used to send a HTTP Digest Authentication request to the client. +Browsers will react with repeating the request with user authentication data. +If they do not yet know the user authentication for the requested realm, they will show +a dialog to query username and password. +In case the authentication realm (also called domain) is NULL, the parameter +`authentication_domain` from the server configuration is used. +The function returns a negative number on errors. + +### See Also + +* [`mg_check_digest_access_authentication();`](mg_check_digest_access_authentication.md) +* [`mg_modify_passwords_file();`](mg_modify_passwords_file.md) +* [`mg_send_http_error();`](mg_send_http_error.md) +* [`mg_write();`](mg_write.md) +* [`mg_printf();`](mg_print.md) + diff --git a/src/civetweb/docs/api/mg_send_file.md b/src/civetweb/docs/api/mg_send_file.md new file mode 100644 index 000000000..bffe075da --- /dev/null +++ b/src/civetweb/docs/api/mg_send_file.md @@ -0,0 +1,25 @@ +# Civetweb API Reference + +### `mg_send_file( conn, path );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`|The connection over which the file must be sent| +|**`path`**|`const char *`|The full path and filename of the file| + +### Return Value + +*none* + +### Description + +The function `mg_send_file()` sends the contents of a file over a connection to the remote peer. The function also adds the necessary HTTP headers. + +### See Also + +* [`mg_printf();`](mg_printf.md) +* [`mg_send_mime_file();`](mg_send_mime_file.md) +* [`mg_send_mime_file2();`](mg_send_mime_file2.md) +* [`mg_write();`](mg_write.md) diff --git a/src/civetweb/docs/api/mg_send_http_error.md b/src/civetweb/docs/api/mg_send_http_error.md new file mode 100644 index 000000000..0c18d3e9f --- /dev/null +++ b/src/civetweb/docs/api/mg_send_http_error.md @@ -0,0 +1,32 @@ +# Civetweb API Reference + +### `mg_send_http_error( conn, status_code, fmt, ... );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`|The connection over which the data must be sent| +|**`status_code`**|`int`|The HTTP status code (see HTTP standard)| +|**`fmt`**|`const char *`|Format string for an error message| +|**`...`**|*various*|Parameters as specified in the format string| + +### Return Value + +| Type | Description | +| :--- | :--- | + + +### Description + +The function `mg_send_http_error()` can be used to send HTTP error messages from a server to a client. +The `status_code` must be one of the predefined HTTP standard error codes (e.g., "404" for "Not Found"). +The status text (e.g., "Not Found") for standard error codes is known by this function. +A body of the error message, to explain the error in more detail, can be specified using the `fmt` format specifier and additional arguments. The `fmt` format specifier works like for the `printf()` function in the standard C library. + + +### See Also + +* [`mg_printf();`](mg_printf.md) +* [`mg_write();`](mg_write.md) + diff --git a/src/civetweb/docs/api/mg_send_mime_file.md b/src/civetweb/docs/api/mg_send_mime_file.md new file mode 100644 index 000000000..424b0dc18 --- /dev/null +++ b/src/civetweb/docs/api/mg_send_mime_file.md @@ -0,0 +1,27 @@ +# Civetweb API Reference + +### `mg_send_mime_file( conn, path, mime_type );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`|The connection over which the file must be sent| +|**`path`**|`const char *`|The full path and filename of the file| +|**`mime_type`**|`const char *`|The mime type of the file, or NULL for automatic detection| + +### Return Value + +*none* + +### Description + +The function `mg_send_mime_file()` sends a file over a connection including the HTTP headers. The function is similar to the [`mg_send_file()`](mg_send_file.md) with the additional functionality that the MIME type of the file can be specified. If the `mime_type` parameter is NULL, the routine will try to determine the MIME type based on the extension of the filename. + +### See Also + +* [`mg_get_builtin_mime_type();`](mg_get_builtin_mime_type.md) +* [`mg_printf();`](mg_printf.md) +* [`mg_send_file();`](mg_send_file.md) +* [`mg_send_mime_file2();`](mg_send_mime_file2.md) +* [`mg_write();`](mg_write.md) diff --git a/src/civetweb/docs/api/mg_send_mime_file2.md b/src/civetweb/docs/api/mg_send_mime_file2.md new file mode 100644 index 000000000..1350df8d6 --- /dev/null +++ b/src/civetweb/docs/api/mg_send_mime_file2.md @@ -0,0 +1,30 @@ +# Civetweb API Reference + +### `mg_send_mime_file2( conn, path, mime_type, additional_headers );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`|The connection over which the file must be sent| +|**`path`**|`const char *`|The full path and filename of the file| +|**`mime_type`**|`const char *`|The mime type or NULL for automatic detection| +|**`additional_headers`**|`const char *`|Additional headers to be sent| + +### Return Value + +*none* + +### Description + +The function `mg_send_mime_file2()` can be used to send a file over a connection. The function is similar to [`mg_send_mime_file()`](mg_send_mime_file.md) with the additional functionality that user specified headers can be sent. The MIME type of the file can be specified in the function call, or will be automatically determined based on the extension of the filename if the `mime_type` parameter has the value NULL. + +Additional custom header fields can be added as a parameter. Please make sure that these header names begin with `X-` to prevent name clashes with other headers. If the `additional_headers` parameter is NULL, no custom headers will be added. + +### See Also + +* [`mg_get_builtin_mime_type();`](mg_get_builtin_mime_type.md) +* [`mg_printf();`](mg_printf.md) +* [`mg_send_file();`](mg_send_file.md) +* [`mg_send_mime_file();`](mg_send_mime_file.md) +* [`mg_write();`](mg_write.md) diff --git a/src/civetweb/docs/api/mg_server_ports.md b/src/civetweb/docs/api/mg_server_ports.md new file mode 100644 index 000000000..e969f0324 --- /dev/null +++ b/src/civetweb/docs/api/mg_server_ports.md @@ -0,0 +1,24 @@ +# Civetweb API Reference + +### `struct mg_server_ports;` + +### Fields + +| Field | Type | Description | +| :--- | :--- | :--- | +|**`protocol`**|`int`|The protocol mask where `IPv4` is **1**, `IPv6` is **2** and both `IPv4` and `IPv6` is **3**| +|**`port`**|`int`|The port number on which the service listens| +|**`is_ssl`**|`int`|**0** for `HTTP` communication, **1** for `HTTPS`| +|**`is_redirect`**|`int`|**1** if all requests are redirected, otherwise **0**| +|**`_reserved1`**|`int`|Reserved for internal use| +|**`_reserved2`**|`int`|Reserved for internal use| +|**`_reserved3`**|`int`|Reserved for internal use| +|**`_reserved4`**|`int`|Reserved for internal use| + +### Description + +A call to the function [`mg_get_server_ports()`](mg_get_server_ports.md) returns a list of structures with information about each running Civetweb service. These structures are of type `struct mg_server_ports` and contain the base information of each service. + +### See Also + +* [`mg_get_server_ports();`](mg_get_server_ports.md) diff --git a/src/civetweb/docs/api/mg_set_auth_handler.md b/src/civetweb/docs/api/mg_set_auth_handler.md new file mode 100644 index 000000000..302324adb --- /dev/null +++ b/src/civetweb/docs/api/mg_set_auth_handler.md @@ -0,0 +1,30 @@ +# Civetweb API Reference + +### `mg_set_auth_handler( ctx, uri, handler, cbdata );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`ctx`**|`struct mg_context *`|The context on which the handler must be set| +|**`uri`**|`const char *`|The URI for the authorization handler| +|**`handler`**|`mg_authorization_handler`|Callback function doing the actual authorization| +|**`cbdata`**|`void *`|Optional user data| + +`int mg_authorization_handler( struct mg_connection *conn, void *cbdata );` + +### Return Value + +*none* + +### Description + +The function `mg_set_auth_handler()` hooks an authorization function to an URI to check if a user is authorized to visit that URI. The check is performed by a callback function of type `mg_authorization_handler`. The callback function is passed two parameters: the current connection and a pointer to optional user defined data which was passed to `mg_set_auth_handler()` when the callback was hooked to the URI. + +The callback function can return **0** to deny access, and **1** to allow access. + +The `mg_set_auth_handler()` function is very similar in use to [`mg_set_request_handler()`](mg_set_request_handler.md). + +### See Also + +* [`mg_set_request_handler();`](mg_set_request_handler.md) diff --git a/src/civetweb/docs/api/mg_set_request_handler.md b/src/civetweb/docs/api/mg_set_request_handler.md new file mode 100644 index 000000000..95a09a08d --- /dev/null +++ b/src/civetweb/docs/api/mg_set_request_handler.md @@ -0,0 +1,26 @@ +# Civetweb API Reference + +### `mg_set_request_handler( ctx, uri, handler, cbdata );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`ctx`**|`struct mg_context *`|The context where the handler must be active| +|**`uri`**|`const char *`|The URI to hook the handler on| +|**`handler`**|`mg_request_handler`|Callback function doing the heavy lifting| +|**`cbdata`**|`void *`|Optional user supplied data| + +`int mg_request_handler( struct mg_connection *conn, void *cbdata );` + +### Return Value + +*none* + +### Description + +The function `mg_set_request_handler()` hooks a callback function on a URI. That callback function is called whenever a client requests the specific URI. The callback function receives the connection information and optional user supplied data as parameters and can serve information back to the client. When the callback function does not send any information back to the client, it should return **0** to signal Civetweb that the Civetweb core should handle the request. A return value between 1 and 999 is used to tell Civetweb that the request has been handled and no further processing is necessary. The returned code is stored as the status code in the access log, it is therefore recommended, although not mandatory to return a status code which matches the state of the request. + +### See Also + +* [`mg_set_auth_handler();`](mg_set_auth_handler.md) diff --git a/src/civetweb/docs/api/mg_set_user_connection_data.md b/src/civetweb/docs/api/mg_set_user_connection_data.md new file mode 100644 index 000000000..adafc2290 --- /dev/null +++ b/src/civetweb/docs/api/mg_set_user_connection_data.md @@ -0,0 +1,44 @@ +# Civetweb API Reference + +### `mg_set_user_connection_data( conn, data );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`|connection to add the user data| +|**`data`**|`void *`|Pointer to the user data| + +### Return Value + +*none* + +### Description + +The function `mg_set_user_connection_data()` can be used to set a user defined +data pointer attached to a connection. This value can be read using +`mg_get_user_connection_data()`. +Any call to `mg_set_user_connection_data()` will overwrite a previously +assigned user data pointer. + +`mg_set_user_connection_data()` requires a non-const +`struct mg_connection *` to set the user data pointer. It is save to use the +`const struct mg_connection *` passed to a websocket connect handler (with a +const cast), since `const` just means you must not use `mg_read()` or +`mg_write()` in this context. + +Alternatively, you can use the `init_connection` callback in +`struct mg_callbacks` to set the user data pointer. +In this case, typically `init_connection` is used to allocate memory for +a user defined `struct`, while `connection_close` is used to free this +memory again. + + +### See Also + +* [`mg_get_user_connection_data();`](mg_get_user_connection_data.md) +* [`struct mg_callbacks`](mg_callbacks.md) +* [`mg_set_websocket_handler();`](mg_set_websocket_handler.md) +* [`mg_read();`](mg_read.md) +* [`mg_write();`](mg_write.md) + diff --git a/src/civetweb/docs/api/mg_set_websocket_handler.md b/src/civetweb/docs/api/mg_set_websocket_handler.md new file mode 100644 index 000000000..f838c81d0 --- /dev/null +++ b/src/civetweb/docs/api/mg_set_websocket_handler.md @@ -0,0 +1,30 @@ +# Civetweb API Reference + +### `mg_set_websocket_handler( ctx, uri, connect_handler, ready_handler, data_handler, close_handler, cbdata );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`ctx`**|`mg_context *`|The context in which to add the handlers| +|**`uri`**|`const char *`|The URI for which the handlers should be activated| +|**`connect_handler`**|`mg_websocket_connect_handler`|Handler called when a connect is signalled| +|**`ready_handler`**|`mg_websocket_ready_handler`|Handler called when the connection is ready| +|**`data_handler`**|`mg_websocket_data_handler`|Handler called when data is received| +|**`close_handler`**|`mg_websocket_close_handler`|Handler called when the connection closes| +|**`cbdata`**|`void *`|User defined data| + +`int mg_websocket_connect_handler( const struct mg_connection *conn, void *cbdata );` +`int mg_websocket_ready_handler( struct mg_connection *conn, void *cbdata );` +`int mg_websocket_data_handler( struct mg_connection *conn, int opcode, char * buf, size_t buf_len, void *cbdata );` +`int mg_websocket_close_handler( const struct mg_connection *conn, void *cbdata );` + +### Return Value + +*none* + +### Description + +The function `mg_set_websocket_handler()` connects callback functions to a websocket URI. The callback functions are called when a state change is detected on the URI like an incomming connection or data received from a remote peer. + +### See Also diff --git a/src/civetweb/docs/api/mg_start.md b/src/civetweb/docs/api/mg_start.md new file mode 100644 index 000000000..c597d5514 --- /dev/null +++ b/src/civetweb/docs/api/mg_start.md @@ -0,0 +1,39 @@ +# Civetweb API Reference + +### `mg_start( callbacks, user_data, options );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`callbacks`**|`const struct mg_callbacks *`| A structure with optional callback functions to process requests from the web server | +|**`user_data`**|`void *`| A pointer to optional user data | +|**`options`**|`char **`| A list of options used to initialize the web server. The list consists of an NULL terminated list of option-value string pairs. | + +The option list can be used to set the following options: + +| Option | Default | Description | +| :--- | :--- | :--- | +| **`cgi_environment`** | *empty* | The option `cgi_environment` can contain extra variables to be passed to the CGI script in addition to the standard environment variables. The lust must be a comma separated list of name=value pairs like this: `VARIABLE1=VALUE1,VARIABLE2=VALUE2`.| +| **`cgi_interpreter`**| *empty* | The option `cgi_interpreter` can contain a path to an executable which will be used as a CGI interpreter for **all** CGI scripts regardless of the script file extension. If this option is not set (which is the default), CivetWeb looks at the first line of a CGI script to see if an interpreter is defined there. This first line is formatted as a shebang line as common in unix style shell scripts, but this will also work in Windows. For more information about the syntax, please see the Wikipedia page about the [shebang line](http://en.wikipedia.org/wiki/Shebang_(Unix\)).| +| | |For example on a Windows system where both PHP and Perl CGI scripts are used, `#!/path/to/php-cgi.exe` and `#!/path/to/perl.exe` must be the first line of the respective CGI scripts. Note that the paths should be either full file paths, or file paths relative to the current working directory of the CivetWeb server. The current working directory may be dependent on the way the application is started. When started from the command line it is the directory from where the executable was called, but when starting it from a shortcut in a graphical desktop environment, it will be the directory where the executable is located, the default directory of the user or a directory mentioned in the shortcut, depending on the operating system and graphical user interface used.| +| | |If all CGIs use the same interpreter, it is more efficient to set the option `cgi_interpreter` to the path to that executable because in that case no processing of the shebang line is necessary. When using PHP, be sure to point tot php-cgi(.exe) and not the php(.exe) executable, as the latter is a stand alone interpreter which doesn't interface over CGI with CivetWeb. +| **`cgi_pattern`** | `**.cgi$|**.pl$|**.php$` | All files that match `cgi_pattern` are treated as CGI files. The default pattern allows CGI files to be anywhere. To restrict CGIs to a certain directory, use `/path/to/cgi-bin/**.cgi` as a pattern. Note that the full path of the local file is matched against the pattern, not the URI provided in the client request.| +|**`put_delete_auth_file`**| *empty* | The option `put_delete_auth_file` defines the password file to be used for PUT and DELETE requests. Without a password file it is not possible to put new files to the server, or to delete existing ones. This only applies to direct HTTP requests which use the PUT and DELETE methods without server side scripting. PUT and DELETE requests might still be handled by Lua scripts and CGI pages. | + +### Return Value + +| Type | Description | +| :--- | :--- | +|`struct mg_context *`| A pointer to a context structure when successful, or NULL in case of failure | + +### Description + +The function `mg_start()` is the only function needed to call to initialize the webserver. After the function returns and a pointer to a context structure is provided, it is guaranteed that the server has started and is listening on the designated ports. In case of failure a NULL pointer is returned. The behaviour of the web server is controlled by a list of callback functions and a list of options. The callback functions can do application specific processing of events which are encountered by the webserver. If a specific callback function is set to NULL, the webserver uses their default callback routine. The options list controls how the webserver should be started and contains settings for for example the ports to listen on, the maximum number of threads created to handle requests in parallel and if settings for SSL encryption. + +As a side effect on Unix systems, SIGCHLD and SIGPIPE signals will be ignored. If custom processing is needed for these signals, signal handlers must be setup after the call to `mg_start()` has completed. + +### See Also + +* [`struct mg_callbacks;`](mg_callbacks.md) +* [`mg_stop();`](mg_stop.md) diff --git a/src/civetweb/docs/api/mg_start_thread.md b/src/civetweb/docs/api/mg_start_thread.md new file mode 100644 index 000000000..e81722650 --- /dev/null +++ b/src/civetweb/docs/api/mg_start_thread.md @@ -0,0 +1,26 @@ +# Civetweb API Reference + +### `mg_start_thread( func, cbdata );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`func`**|`mg_thread_func_t`|Function to start as a separate thread| +|**`cbdata`**|`void *`|User defined data to be passed to the thread as parameter| + +`void mg_thread_func_t( void *cbdata );` + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Success or error code| + +### Description + +The function `mg_start_thread()` is a convenience function to create a detached thread. The function returns **0** when successful and another value if an error occured. A pointer to user supplied data can be passed which is then passed further on to the thread function as parameter. + +### See Also + +* [`mg_start();`](mg_start.md) diff --git a/src/civetweb/docs/api/mg_stop.md b/src/civetweb/docs/api/mg_stop.md new file mode 100644 index 000000000..ea0d24666 --- /dev/null +++ b/src/civetweb/docs/api/mg_stop.md @@ -0,0 +1,22 @@ +# Civetweb API Reference + +### `mg_stop( ctx );` + +#### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`ctx`**|**`struct mg_context *`**| A pointer to the current webserver context | + +### Return Value + +*none* + +### Description + +The function `mg_stop()` is used to stop and cleanup a running webserver. A pointer to the context of the running webserver is provided as a parameter. The execution of this function may take some time because it waits until all threads have stopped and returns all memory to the heap. After the function returns, the location the context pointer points to is invalid. The function does not return a return value and it is therefore not possible to know if stopping the webserver succeeded or not. + +### See Also + +* [`mg_start();`](mg_start.md) +* [`mg_start_thread();`](mg_start_thread.md) diff --git a/src/civetweb/docs/api/mg_store_body.md b/src/civetweb/docs/api/mg_store_body.md new file mode 100644 index 000000000..4cde44a92 --- /dev/null +++ b/src/civetweb/docs/api/mg_store_body.md @@ -0,0 +1,24 @@ +# Civetweb API Reference + +### `mg_store_body( conn, path );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`|connection on which to read the data| +|**`path`**|`const char *`|file to store the request body| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`long long`|Number of bytes written to the file, or an error code| + +### Description + +The function `mg_store_body()` stores the body of an incoming request to a data file. The function returns the number of bytes stored in the file, or a negative value to indicate an error. + +### See Also + +* [`mg_read();`](mg_read.md) diff --git a/src/civetweb/docs/api/mg_strcasecmp.md b/src/civetweb/docs/api/mg_strcasecmp.md new file mode 100644 index 000000000..cba6b922b --- /dev/null +++ b/src/civetweb/docs/api/mg_strcasecmp.md @@ -0,0 +1,24 @@ +# Civetweb API Reference + +### `mg_strcasecmp( s1, s2 );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`s1`**|`const char *`|First string to compare| +|**`s2`**|`const char *`|Second string to compare| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Integer value with the result of the comparison| + +### Description + +The function `mg_strcasecmp()` is a helper function to compare two strings. The comparison is case insensitive. The return value is **0** if both strings are equal, less then zero if the first string is less than the second in a lexical comparison, and greater than zero if the first string is greater than the second. + +### See Also + +* [`mg_strncasecmp();`](mg_strncasecmp.md) diff --git a/src/civetweb/docs/api/mg_strncasecmp.md b/src/civetweb/docs/api/mg_strncasecmp.md new file mode 100644 index 000000000..b4affd66b --- /dev/null +++ b/src/civetweb/docs/api/mg_strncasecmp.md @@ -0,0 +1,25 @@ +# Civetweb API Reference + +### `mg_strncasecmp( s1, s2, len );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`s1`**|`const char *`|First string in the comparison| +|**`s2`**|`const char *`|Second string in the comparison| +|**`len`**|`size_t`|The maximum number of characters to compare| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|The result of the comparison| + +### Description + +The function `mg_strncasecmp()` is a helper function to compare two strings. The comparison is case insensitive and only a limited number of characters are compared. This limit is provided as third parameter in the function call. The return value is **0** if both strings are equal, less then zero if the first string is less than the second in a lexical comparison, and greater than zero if the first string is greater than the second. + +### See Also + +* [`mg_strcasecmp();`](mg_strcasecmp.md) diff --git a/src/civetweb/docs/api/mg_unlock_connection.md b/src/civetweb/docs/api/mg_unlock_connection.md new file mode 100644 index 000000000..b74138fde --- /dev/null +++ b/src/civetweb/docs/api/mg_unlock_connection.md @@ -0,0 +1,27 @@ +# Civetweb API Reference + +### `mg_unlock_connection( conn );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`|Connection to remove the lock from| + +### Return Value + +*none* + +### Description + +The function `mg_unlock_connection()` removes the lock on a connection which was previously set with a call to [`mg_lock_connection()`](mg_lock_connection.md). Locking may be necessary when using [`mg_write()`](mg_write.md) or [`mg_printf()`](mg_printf.md) on websocket connections to prevent data corruption. + +### See Also + +* [`mg_lock_connection();`](mg_lock_connection.md) +* [`mg_lock_context();`](mg_lock_context.md) +* [`mg_printf();`](mg_printf.md) +* [`mg_unlock_context();`](mg_unlock_context.md) +* [`mg_websocket_client_write();`](mg_websocket_client_write.md) +* [`mg_websocket_write();`](mg_websocket_write.md) +* [`mg_write();`](mg_write.md) diff --git a/src/civetweb/docs/api/mg_unlock_context.md b/src/civetweb/docs/api/mg_unlock_context.md new file mode 100644 index 000000000..a630acd7e --- /dev/null +++ b/src/civetweb/docs/api/mg_unlock_context.md @@ -0,0 +1,23 @@ +# Civetweb API Reference + +### `mg_unlock_context( ctx );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`ctx`**|`struct mg_context *`|The context to remove the lock from| + +### Return Value + +*none* + +### Description + +The function `mg_unlock_contect()` removes a lock put previously on a context with a call to [`mg_lock_context()`](mg_lock_context.md). Locking a context may be necessary when accessing shared resources. + +### See Also + +* [`mg_lock_connection();`](mg_lock_connection.md) +* [`mg_lock_context();`](mg_lock_context.md) +* [`mg_unlock_connection();`](mg_unlock_connection.md) diff --git a/src/civetweb/docs/api/mg_upload.md b/src/civetweb/docs/api/mg_upload.md new file mode 100644 index 000000000..de7f1c8dd --- /dev/null +++ b/src/civetweb/docs/api/mg_upload.md @@ -0,0 +1,22 @@ +# Civetweb API Reference + +### ~~`mg_upload( conn, destination_dir );`~~ + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`|Connection on which files to upload| +|**`destination_dir`**|`const char *`|The destination directory to upload to| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Success or error code| + +### Description + +The function `mg_upload()` is deprecated and may be removed from future releases. Use of this function is therefore highly discouraged. + +### See Also diff --git a/src/civetweb/docs/api/mg_url_decode.md b/src/civetweb/docs/api/mg_url_decode.md new file mode 100644 index 000000000..679408848 --- /dev/null +++ b/src/civetweb/docs/api/mg_url_decode.md @@ -0,0 +1,27 @@ +# Civetweb API Reference + +### `mg_url_decode( src, src_len, dst, dst_len, is_form_url_encoded );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`src`**|`const char *`|Source data to convert| +|**`src_len`**|`int`|Length of the source buffer| +|**`dst`**|`char *`|Destination buffer to store the result| +|**`dst_len`**|`int`|Length of the destination buffer| +|**`is_form_url_encoded`**|`int`|Not equal zero when form decoding must be used| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|The number of bytes stored in the destination buffer, or **-1** if the buffer doesn't exist or is too small| + +### Description + +The function `mg_url_decode()` Decodes a in input buffer. Both normal URIs and form URIs can be decoded. In the latter case the space character is converted to a `+` as defined in [RFC 1866](http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt) in section 8.2.1. + +### See Also + +* [`mg_url_encode();`](mg_url_encode.md) diff --git a/src/civetweb/docs/api/mg_url_encode.md b/src/civetweb/docs/api/mg_url_encode.md new file mode 100644 index 000000000..465af1226 --- /dev/null +++ b/src/civetweb/docs/api/mg_url_encode.md @@ -0,0 +1,25 @@ +# Civetweb API Reference + +### `mg_url_encode( src, dst, des_len );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`src`**|`const char *`|Input string to encode| +|**`dst`**|`char *`|Destination buffer to store the encoded result| +|**`dst_len`**|`size_t`|Length of the destination buffer including the terminating NUL| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|The number of characters written in the destination buffer| + +### Description + +The function `mg_url_encode()` encodes a in input buffer. Both normal URIs and form URIs can be encoded. In the latter case the space character is converted to a `+` as defined in [RFC 1866](http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt) in section 8.2.1. + +### See Also + +* [`mg_url_decode();`](mg_url_decode.md) diff --git a/src/civetweb/docs/api/mg_version.md b/src/civetweb/docs/api/mg_version.md new file mode 100644 index 000000000..a31128b52 --- /dev/null +++ b/src/civetweb/docs/api/mg_version.md @@ -0,0 +1,19 @@ +# Civetweb API Reference + +### `mg_version();` + +### Parameters + +*none* + +### Return Value + +| Type | Description | +| :--- | :--- | +|`const char *`| A pointer to a text with the current CivetWeb version | + +### Description + +The function `mg_version()` can be used to return the current CivetWeb version. The function returns a pointer to a string with the current major and minor version number separated with a dot, for example "1.9". + +### See Also diff --git a/src/civetweb/docs/api/mg_websocket_client_write.md b/src/civetweb/docs/api/mg_websocket_client_write.md new file mode 100644 index 000000000..f792f4c7a --- /dev/null +++ b/src/civetweb/docs/api/mg_websocket_client_write.md @@ -0,0 +1,32 @@ +# Civetweb API Reference + +### `mg_websocket_client_write( conn, opcode, data, data_len );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`|Connection on which to send data| +|**`opcode`**|`int`|Opcode| +|**`data const`**|`char *`|The data to be written| +|**`data_len`**|`size_t`|Length of the data buffer| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Number of bytes written or an error code| + +### Description + +The function `mg_websocket_client_write()` sends data to a websocket server wrapped in a masked websocket frame. The function issues calls to [`mg_lock_connection()`](mg_lock_connection.md) and [`mg_unlock_connection()`](mg_unlock_connection.md) to ensure that the transmission is not interrupted. Interruption can happen the the application is proactively communicating and responding to a request simultaneously. This function is available only, if Civetweb is compiled with the option `-DUSE_WEBSOCKET`. + +The return value is the number of bytes written on success, **0** when the connection has been closed and **-1** if an error occured. + +### See Also + +* [`mg_lock_connection();`](mg_lock_connection.md) +* [`mg_printf();`](mg_printf.md) +* [`mg_unlock_connection();`](mg_unlock_connection.md) +* [`mg_websocket_write();`](mg_websocket_write.md) +* [`mg_write();`](mg_write.md) diff --git a/src/civetweb/docs/api/mg_websocket_write.md b/src/civetweb/docs/api/mg_websocket_write.md new file mode 100644 index 000000000..3c7eadc77 --- /dev/null +++ b/src/civetweb/docs/api/mg_websocket_write.md @@ -0,0 +1,34 @@ +# Civetweb API Reference + +### `mg_websocket_write( conn, opcode, data, data_len );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`|Connection on which the data must be written| +|**`opcode`**|`int`|Opcode| +|**`data`**|`const char *`|Data to be written to the client| +|**`data_len`**|`size_t`|Length of the data| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`|Number of bytes written or an error code| + +### Description + +The function `mg_websocket_write()` sends data to a websocket client wrapped in a websocket frame. The function issues calls to [`mg_lock_connection()`](mg_lock_connection.md) and [`mg_unlock_connection()`](mg_unlock_connection.md) to ensure that the transmission is not interrupted. Data corruption can otherwise happen if the application is proactively communicating and responding to a request simultaneously. + +The function is available only when Civetweb is compiled with the `-DUSE_WEBSOCKET` option. + +The function returns the number of bytes written, **0** when the connection has been closed and **-1** if an error occurred. + +### See Also + +* [`mg_lock_connection();`](mg_lock_connection.md) +* [`mg_printf();`](mg_printf.md) +* [`mg_unlock_connection();`](mg_unlock_connection.md) +* [`mg_websocket_client_write();`](mg_websocket_client_write.md) +* [`mg_write();`](mg_write.md) diff --git a/src/civetweb/docs/api/mg_write.md b/src/civetweb/docs/api/mg_write.md new file mode 100644 index 000000000..d501857bb --- /dev/null +++ b/src/civetweb/docs/api/mg_write.md @@ -0,0 +1,29 @@ +# Civetweb API Reference + +### `mg_write( conn, buf, len );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`conn`**|`struct mg_connection *`| A pointer to the connection to be used to send data | +|**`buf`**|`const void *`| A pointer to the blob of information to be sent | +|**`len`**|`size_t`| The amount of bytes to be sent | + +### Return Value + +| Type | Description | +| :--- | :--- | +|`int`| An integer indicating the amount of bytes sent, or failure | + +### Description + +The function `mg_write()` can be used to send a blob of arbitrary data over a connection. The size of the data is provided as a parameter. The only length limitation on this function is `MAX_INT`, because the return value of this function will turn negative with larger blocks of data, although they may have been sent correctly. The function returns the amount of bytes sent in case of success, the value **0** when the connection has been closed, and **-1** in case of an error. + +### See Also + +* [`mg_lock_connection();`](mg_lock_connection.md) +* [`mg_printf();`](mg_print.md) +* [`mg_unlock_connection();`](mg_unlock_connection.md) +* [`mg_websocket_client_write();`](mg_websocket_client_write.md) +* [`mg_websocket_write();`](mg_websocket_write.md) diff --git a/src/civetweb/docs/yaSSL.md b/src/civetweb/docs/yaSSL.md new file mode 100644 index 000000000..7f2e85f8e --- /dev/null +++ b/src/civetweb/docs/yaSSL.md @@ -0,0 +1,87 @@ +Adding wolfSSL (formerly CyaSSL) support +===== + +In order to support SSL *HTTPS* connections in Civetweb, +you may wish to use the GPLv2 licensed CyaSSL library. By using this +library, the resulting binary may have to have the GPL license unless +you buy a commercial license from [wolfSSL](http://www.yassl.com/). + +*Note: The following instructions have not been checked for the most recent versions of CivetWeb and wolfSSL. Some information might be outdated.* + + +Getting Started +---- + +- Download Cayssl at https://www.wolfssl.com (formerly http://www.yassl.com/) +- Extract the zip file + - To make this seemless, extract to a directory parallel to with Civetweb is + +### Example Project + +If you download cyaSSL to cyassl-2.7.0 in a directory parallel to Civetweb, you can open the *VS/civetweb_yassl* solution in Visual Studio. + +Build Configuration +---- + +#### Required include paths for both civetweb and cyassl + - *cyassl_directory*\ + - *cyassl_directory*\cyassl\ + +#### Required civetweb preprocessor defines + - USE_YASSL + - NO_SSL_DL + +#### Required cySSL preprocessor defines + - OPENSSL_EXTRA + - HAVE_ERRNO_H + - HAVE_GETHOSTBYNAME + - HAVE_INET_NTOA + - HAVE_LIMITS_H + - HAVE_MEMSET + - HAVE_SOCKET + - HAVE_STDDEF_H + - HAVE_STDLIB_H + - HAVE_STRING_H + - HAVE_SYS_STAT_H + - HAVE_SYS_TYPES_H + +#### Required CyaSSL source files + + - ctaocrypt/src/aes.c + - ctaocrypt/src/arc4.c + - ctaocrypt/src/asn.c + - ctaocrypt/src/coding.c + - ctaocrypt/src/des3.c + - ctaocrypt/src/dh.c + - ctaocrypt/src/dsa.c + - ctaocrypt/src/ecc.c + - ctaocrypt/src/error.c + - ctaocrypt/src/hc128.c + - ctaocrypt/src/hmac.c + - ctaocrypt/src/integer.c + - ctaocrypt/src/logging.c + - ctaocrypt/src/md2.c + - ctaocrypt/src/md4.c + - ctaocrypt/src/md5.c + - ctaocrypt/src/memory.c + - ctaocrypt/src/misc.c + - ctaocrypt/src/pwdbased.c + - ctaocrypt/src/rabbit.c + - ctaocrypt/src/random.c + - ctaocrypt/src/ripemd.c + - ctaocrypt/src/rsa.c + - ctaocrypt/src/sha.c + - ctaocrypt/src/sha256.c + - ctaocrypt/src/sha512.c + - ctaocrypt/src/tfm.c + - src/crl.c + - src/internal.c + - src/io.c + - src/keys.c + - src/ocsp.c + - src/sniffer.c + - src/ssl.c + - src/tls.c + + + diff --git a/src/civetweb/examples/README.md b/src/civetweb/examples/README.md new file mode 100644 index 000000000..cf5495977 --- /dev/null +++ b/src/civetweb/examples/README.md @@ -0,0 +1,13 @@ + +Examples +===== + +Two examples show how to embed civetweb into a C ([embedded_c](https://github.com/civetweb/civetweb/tree/master/examples/embedded_c)) or a C++ ([embedded_cpp](https://github.com/civetweb/civetweb/tree/master/examples/embedded_cpp)) application. +The C++ wrapper only offers a subset of the full C API, thus the C example is more complete than the C++ example. These examples were not designed with security in mind, but to show how the API can be used in principle. For more information, see the [documentation](https://github.com/civetweb/civetweb/tree/master/docs). Some examples can also be found in the [test](https://github.com/civetweb/civetweb/tree/master/test) folder (but they are less documented and adapted to the needs of the test framework). + +In addition, there is one example how to configure a HTTPS server, to comply with modern security standards ([https](https://github.com/civetweb/civetweb/tree/master/examples/https)). It does not hold any source, but only a configuration file and some documentation how to use it. + +Some no longer maintained examples can be found in the ["obsolete"](https://github.com/civetweb/civetweb/tree/master/examples/_obsolete) folder. It is not guaranteed that they work in the current version - they are kept for reference, but might be removed in the future. + +All examples are subject to the MIT license (unless noted otherwise) - they come without warranty of any kind. + diff --git a/src/civetweb/examples/_obsolete/chat/Makefile b/src/civetweb/examples/_obsolete/chat/Makefile new file mode 100644 index 000000000..e8d12be69 --- /dev/null +++ b/src/civetweb/examples/_obsolete/chat/Makefile @@ -0,0 +1,40 @@ +# +# Copyright (c) 2013 No Face Press, LLC +# License http://opensource.org/licenses/mit-license.php MIT License +# + +#This makefile is used to test the other Makefiles + + +PROG = chat +SRC = chat.c + +TOP = ../.. +CIVETWEB_LIB = libcivetweb.a +SSL_CERT = ssl_cert.pem + +CFLAGS = -I$(TOP)/include $(COPT) +LIBS = -lpthread + +include $(TOP)/resources/Makefile.in-os + +ifeq ($(TARGET_OS),LINUX) + LIBS += -ldl +endif + +all: $(PROG) $(SSL_CERT) + +$(PROG): $(CIVETWEB_LIB) $(SRC) + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS) + +$(CIVETWEB_LIB): + $(MAKE) -C $(TOP) clean lib + cp $(TOP)/$(CIVETWEB_LIB) . + +$(SSL_CERT): + cp $(TOP)/resources/$(SSL_CERT) . + +clean: + rm -f $(CIVETWEB_LIB) $(PROG) $(SSL_CERT) + +.PHONY: all clean diff --git a/src/civetweb/examples/_obsolete/chat/chat.c b/src/civetweb/examples/_obsolete/chat/chat.c new file mode 100644 index 000000000..ad146183b --- /dev/null +++ b/src/civetweb/examples/_obsolete/chat/chat.c @@ -0,0 +1,403 @@ +// This file is part of the Civetweb project, http://code.google.com/p/civetweb +// It implements an online chat server. For more details, +// see the documentation on the project web site. +// To test the application, +// 1. type "make" in the directory where this file lives +// 2. point your browser to http://127.0.0.1:8081 + +#include +#include +#include +#include +#include +#include +#include + +#include "civetweb.h" + +#define MAX_USER_LEN 20 +#define MAX_MESSAGE_LEN 100 +#define MAX_MESSAGES 5 +#define MAX_SESSIONS 2 +#define SESSION_TTL 120 + +static const char *authorize_url = "/authorize"; +static const char *login_url = "/login.html"; +static const char *ajax_reply_start = + "HTTP/1.1 200 OK\r\n" + "Cache: no-cache\r\n" + "Content-Type: application/x-javascript\r\n" + "\r\n"; + +// Describes single message sent to a chat. If user is empty (0 length), +// the message is then originated from the server itself. +struct message { + long id; // Message ID + char user[MAX_USER_LEN]; // User that have sent the message + char text[MAX_MESSAGE_LEN]; // Message text + time_t timestamp; // Message timestamp, UTC +}; + +// Describes web session. +struct session { + char session_id[33]; // Session ID, must be unique + char random[20]; // Random data used for extra user validation + char user[MAX_USER_LEN]; // Authenticated user + time_t expire; // Expiration timestamp, UTC +}; + +static struct message messages[MAX_MESSAGES]; // Ringbuffer for messages +static struct session sessions[MAX_SESSIONS]; // Current sessions +static long last_message_id; + +// Protects messages, sessions, last_message_id +static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; + +// Get session object for the connection. Caller must hold the lock. +static struct session *get_session(const struct mg_connection *conn) +{ + int i; + const char *cookie = mg_get_header(conn, "Cookie"); + char session_id[33]; + time_t now = time(NULL); + mg_get_cookie(cookie, "session", session_id, sizeof(session_id)); + for (i = 0; i < MAX_SESSIONS; i++) { + if (sessions[i].expire != 0 && + sessions[i].expire > now && + strcmp(sessions[i].session_id, session_id) == 0) { + break; + } + } + return i == MAX_SESSIONS ? NULL : &sessions[i]; +} + +static void get_qsvar(const struct mg_request_info *request_info, + const char *name, char *dst, size_t dst_len) +{ + const char *qs = request_info->query_string; + mg_get_var(qs, strlen(qs == NULL ? "" : qs), name, dst, dst_len); +} + +// Get a get of messages with IDs greater than last_id and transform them +// into a JSON string. Return that string to the caller. The string is +// dynamically allocated, caller must free it. If there are no messages, +// NULL is returned. +static char *messages_to_json(long last_id) +{ + const struct message *message; + int max_msgs, len; + char buf[sizeof(messages)]; // Large enough to hold all messages + + // Read-lock the ringbuffer. Loop over all messages, making a JSON string. + pthread_rwlock_rdlock(&rwlock); + len = 0; + max_msgs = sizeof(messages) / sizeof(messages[0]); + // If client is too far behind, return all messages. + if (last_message_id - last_id > max_msgs) { + last_id = last_message_id - max_msgs; + } + for (; last_id < last_message_id; last_id++) { + message = &messages[last_id % max_msgs]; + if (message->timestamp == 0) { + break; + } + // buf is allocated on stack and hopefully is large enough to hold all + // messages (it may be too small if the ringbuffer is full and all + // messages are large. in this case asserts will trigger). + len += snprintf(buf + len, sizeof(buf) - len, + "{user: '%s', text: '%s', timestamp: %lu, id: %ld},", + message->user, message->text, message->timestamp, message->id); + assert(len > 0); + assert((size_t) len < sizeof(buf)); + } + pthread_rwlock_unlock(&rwlock); + + return len == 0 ? NULL : strdup(buf); +} + +// If "callback" param is present in query string, this is JSONP call. +// Return 1 in this case, or 0 if "callback" is not specified. +// Wrap an output in Javascript function call. +static int handle_jsonp(struct mg_connection *conn, + const struct mg_request_info *request_info) +{ + char cb[64]; + + get_qsvar(request_info, "callback", cb, sizeof(cb)); + if (cb[0] != '\0') { + mg_printf(conn, "%s(", cb); + } + + return cb[0] == '\0' ? 0 : 1; +} + +// A handler for the /ajax/get_messages endpoint. +// Return a list of messages with ID greater than requested. +static void ajax_get_messages(struct mg_connection *conn, + const struct mg_request_info *request_info) +{ + char last_id[32], *json; + int is_jsonp; + + mg_printf(conn, "%s", ajax_reply_start); + is_jsonp = handle_jsonp(conn, request_info); + + get_qsvar(request_info, "last_id", last_id, sizeof(last_id)); + if ((json = messages_to_json(strtoul(last_id, NULL, 10))) != NULL) { + mg_printf(conn, "[%s]", json); + free(json); + } + + if (is_jsonp) { + mg_printf(conn, "%s", ")"); + } +} + +// Allocate new message. Caller must hold the lock. +static struct message *new_message(void) +{ + static int size = sizeof(messages) / sizeof(messages[0]); + struct message *message = &messages[last_message_id % size]; + message->id = last_message_id++; + message->timestamp = time(0); + return message; +} + +static void my_strlcpy(char *dst, const char *src, size_t len) +{ + strncpy(dst, src, len); + dst[len - 1] = '\0'; +} + +// A handler for the /ajax/send_message endpoint. +static void ajax_send_message(struct mg_connection *conn, + const struct mg_request_info *request_info) +{ + struct message *message; + struct session *session; + char text[sizeof(message->text) - 1]; + int is_jsonp; + + mg_printf(conn, "%s", ajax_reply_start); + is_jsonp = handle_jsonp(conn, request_info); + + get_qsvar(request_info, "text", text, sizeof(text)); + if (text[0] != '\0') { + // We have a message to store. Write-lock the ringbuffer, + // grab the next message and copy data into it. + pthread_rwlock_wrlock(&rwlock); + message = new_message(); + // TODO(lsm): JSON-encode all text strings + session = get_session(conn); + assert(session != NULL); + my_strlcpy(message->text, text, sizeof(text)); + my_strlcpy(message->user, session->user, sizeof(message->user)); + pthread_rwlock_unlock(&rwlock); + } + + mg_printf(conn, "%s", text[0] == '\0' ? "false" : "true"); + + if (is_jsonp) { + mg_printf(conn, "%s", ")"); + } +} + +// Redirect user to the login form. In the cookie, store the original URL +// we came from, so that after the authorization we could redirect back. +static void redirect_to_login(struct mg_connection *conn, + const struct mg_request_info *request_info) +{ + mg_printf(conn, "HTTP/1.1 302 Found\r\n" + "Set-Cookie: original_url=%s\r\n" + "Location: %s\r\n\r\n", + request_info->uri, login_url); +} + +// Return 1 if username/password is allowed, 0 otherwise. +static int check_password(const char *user, const char *password) +{ + // In production environment we should ask an authentication system + // to authenticate the user. + // Here however we do trivial check that user and password are not empty + return (user[0] && password[0]); +} + +// Allocate new session object +static struct session *new_session(void) +{ + int i; + time_t now = time(NULL); + pthread_rwlock_wrlock(&rwlock); + for (i = 0; i < MAX_SESSIONS; i++) { + if (sessions[i].expire == 0 || sessions[i].expire < now) { + sessions[i].expire = time(0) + SESSION_TTL; + break; + } + } + pthread_rwlock_unlock(&rwlock); + return i == MAX_SESSIONS ? NULL : &sessions[i]; +} + +// Generate session ID. buf must be 33 bytes in size. +// Note that it is easy to steal session cookies by sniffing traffic. +// This is why all communication must be SSL-ed. +static void generate_session_id(char *buf, const char *random, + const char *user) +{ + mg_md5(buf, random, user, NULL); +} + +static void send_server_message(const char *fmt, ...) +{ + va_list ap; + struct message *message; + + pthread_rwlock_wrlock(&rwlock); + message = new_message(); + message->user[0] = '\0'; // Empty user indicates server message + va_start(ap, fmt); + vsnprintf(message->text, sizeof(message->text), fmt, ap); + va_end(ap); + + pthread_rwlock_unlock(&rwlock); +} + +// A handler for the /authorize endpoint. +// Login page form sends user name and password to this endpoint. +static void authorize(struct mg_connection *conn, + const struct mg_request_info *request_info) +{ + char user[MAX_USER_LEN], password[MAX_USER_LEN]; + struct session *session; + + // Fetch user name and password. + get_qsvar(request_info, "user", user, sizeof(user)); + get_qsvar(request_info, "password", password, sizeof(password)); + + if (check_password(user, password) && (session = new_session()) != NULL) { + // Authentication success: + // 1. create new session + // 2. set session ID token in the cookie + // 3. remove original_url from the cookie - not needed anymore + // 4. redirect client back to the original URL + // + // The most secure way is to stay HTTPS all the time. However, just to + // show the technique, we redirect to HTTP after the successful + // authentication. The danger of doing this is that session cookie can + // be stolen and an attacker may impersonate the user. + // Secure application must use HTTPS all the time. + my_strlcpy(session->user, user, sizeof(session->user)); + snprintf(session->random, sizeof(session->random), "%d", rand()); + generate_session_id(session->session_id, session->random, session->user); + send_server_message("<%s> joined", session->user); + mg_printf(conn, "HTTP/1.1 302 Found\r\n" + "Set-Cookie: session=%s; max-age=3600; http-only\r\n" // Session ID + "Set-Cookie: user=%s\r\n" // Set user, needed by Javascript code + "Set-Cookie: original_url=/; max-age=0\r\n" // Delete original_url + "Location: /\r\n\r\n", + session->session_id, session->user); + } else { + // Authentication failure, redirect to login. + redirect_to_login(conn, request_info); + } +} + +// Return 1 if request is authorized, 0 otherwise. +static int is_authorized(const struct mg_connection *conn, + const struct mg_request_info *request_info) +{ + struct session *session; + char valid_id[33]; + int authorized = 0; + + // Always authorize accesses to login page and to authorize URI + if (!strcmp(request_info->uri, login_url) || + !strcmp(request_info->uri, authorize_url)) { + return 1; + } + + pthread_rwlock_rdlock(&rwlock); + if ((session = get_session(conn)) != NULL) { + generate_session_id(valid_id, session->random, session->user); + if (strcmp(valid_id, session->session_id) == 0) { + session->expire = time(0) + SESSION_TTL; + authorized = 1; + } + } + pthread_rwlock_unlock(&rwlock); + + return authorized; +} + +static void redirect_to_ssl(struct mg_connection *conn, + const struct mg_request_info *request_info) +{ + const char *p, *host = mg_get_header(conn, "Host"); + if (host != NULL && (p = strchr(host, ':')) != NULL) { + mg_printf(conn, "HTTP/1.1 302 Found\r\n" + "Location: https://%.*s:8082/%s:8082\r\n\r\n", + (int) (p - host), host, request_info->uri); + } else { + mg_printf(conn, "%s", "HTTP/1.1 500 Error\r\n\r\nHost: header is not set"); + } +} + +static int begin_request_handler(struct mg_connection *conn) +{ + const struct mg_request_info *request_info = mg_get_request_info(conn); + int processed = 1; + + if (!request_info->is_ssl) { + redirect_to_ssl(conn, request_info); + } else if (!is_authorized(conn, request_info)) { + redirect_to_login(conn, request_info); + } else if (strcmp(request_info->uri, authorize_url) == 0) { + authorize(conn, request_info); + } else if (strcmp(request_info->uri, "/ajax/get_messages") == 0) { + ajax_get_messages(conn, request_info); + } else if (strcmp(request_info->uri, "/ajax/send_message") == 0) { + ajax_send_message(conn, request_info); + } else { + // No suitable handler found, mark as not processed. Civetweb will + // try to serve the request. + processed = 0; + } + return processed; +} + +static const char *options[] = { + "document_root", "html", + "listening_ports", "8081,8082s", + "ssl_certificate", "ssl_cert.pem", + "num_threads", "5", + NULL +}; + +int main(void) +{ + struct mg_callbacks callbacks; + struct mg_context *ctx; + + // Initialize random number generator. It will be used later on for + // the session identifier creation. + srand((unsigned) time(0)); + + // Setup and start Civetweb + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.begin_request = begin_request_handler; + if ((ctx = mg_start(&callbacks, NULL, options)) == NULL) { + printf("%s\n", "Cannot start chat server, fatal exit"); + exit(EXIT_FAILURE); + } + + // Wait until enter is pressed, then exit + printf("Chat server started on ports %s, press enter to quit.\n", + mg_get_option(ctx, "listening_ports")); + getchar(); + mg_stop(ctx); + printf("%s\n", "Chat server stopped."); + + return EXIT_SUCCESS; +} + +// vim:ts=2:sw=2:et diff --git a/src/civetweb/examples/_obsolete/docroot/favicon.ico b/src/civetweb/examples/_obsolete/docroot/favicon.ico new file mode 100644 index 000000000..2179aba89 Binary files /dev/null and b/src/civetweb/examples/_obsolete/docroot/favicon.ico differ diff --git a/src/civetweb/examples/_obsolete/docroot/index.html b/src/civetweb/examples/_obsolete/docroot/index.html new file mode 100644 index 000000000..c85bec992 --- /dev/null +++ b/src/civetweb/examples/_obsolete/docroot/index.html @@ -0,0 +1,73 @@ + + + + + Civetweb chat server + + + + + + + + +
+ +
+
+ + + +
+ +
+
+ Main room +
+
+
+ + + Type your message here and press enter +
+
+
+ + + +
+
+ + + + + diff --git a/src/civetweb/examples/_obsolete/docroot/jquery.js b/src/civetweb/examples/_obsolete/docroot/jquery.js new file mode 100644 index 000000000..7c2430802 --- /dev/null +++ b/src/civetweb/examples/_obsolete/docroot/jquery.js @@ -0,0 +1,154 @@ +/*! + * jQuery JavaScript Library v1.4.2 + * http://jquery.com/ + * + * Copyright 2010, John Resig + * Dual licensed under the MIT or GPL Version 2 licenses. + * http://jquery.org/license + * + * Includes Sizzle.js + * http://sizzlejs.com/ + * Copyright 2010, The Dojo Foundation + * Released under the MIT, BSD, and GPL Licenses. + * + * Date: Sat Feb 13 22:33:48 2010 -0500 + */ +(function(A,w){function ma(){if(!c.isReady){try{s.documentElement.doScroll("left")}catch(a){setTimeout(ma,1);return}c.ready()}}function Qa(a,b){b.src?c.ajax({url:b.src,async:false,dataType:"script"}):c.globalEval(b.text||b.textContent||b.innerHTML||"");b.parentNode&&b.parentNode.removeChild(b)}function X(a,b,d,f,e,j){var i=a.length;if(typeof b==="object"){for(var o in b)X(a,o,b[o],f,e,d);return a}if(d!==w){f=!j&&f&&c.isFunction(d);for(o=0;o)[^>]*$|^#([\w-]+)$/,Ua=/^.[^:#\[\.,]*$/,Va=/\S/, +Wa=/^(\s|\u00A0)+|(\s|\u00A0)+$/g,Xa=/^<(\w+)\s*\/?>(?:<\/\1>)?$/,P=navigator.userAgent,xa=false,Q=[],L,$=Object.prototype.toString,aa=Object.prototype.hasOwnProperty,ba=Array.prototype.push,R=Array.prototype.slice,ya=Array.prototype.indexOf;c.fn=c.prototype={init:function(a,b){var d,f;if(!a)return this;if(a.nodeType){this.context=this[0]=a;this.length=1;return this}if(a==="body"&&!b){this.context=s;this[0]=s.body;this.selector="body";this.length=1;return this}if(typeof a==="string")if((d=Ta.exec(a))&& +(d[1]||!b))if(d[1]){f=b?b.ownerDocument||b:s;if(a=Xa.exec(a))if(c.isPlainObject(b)){a=[s.createElement(a[1])];c.fn.attr.call(a,b,true)}else a=[f.createElement(a[1])];else{a=sa([d[1]],[f]);a=(a.cacheable?a.fragment.cloneNode(true):a.fragment).childNodes}return c.merge(this,a)}else{if(b=s.getElementById(d[2])){if(b.id!==d[2])return T.find(a);this.length=1;this[0]=b}this.context=s;this.selector=a;return this}else if(!b&&/^\w+$/.test(a)){this.selector=a;this.context=s;a=s.getElementsByTagName(a);return c.merge(this, +a)}else return!b||b.jquery?(b||T).find(a):c(b).find(a);else if(c.isFunction(a))return T.ready(a);if(a.selector!==w){this.selector=a.selector;this.context=a.context}return c.makeArray(a,this)},selector:"",jquery:"1.4.2",length:0,size:function(){return this.length},toArray:function(){return R.call(this,0)},get:function(a){return a==null?this.toArray():a<0?this.slice(a)[0]:this[a]},pushStack:function(a,b,d){var f=c();c.isArray(a)?ba.apply(f,a):c.merge(f,a);f.prevObject=this;f.context=this.context;if(b=== +"find")f.selector=this.selector+(this.selector?" ":"")+d;else if(b)f.selector=this.selector+"."+b+"("+d+")";return f},each:function(a,b){return c.each(this,a,b)},ready:function(a){c.bindReady();if(c.isReady)a.call(s,c);else Q&&Q.push(a);return this},eq:function(a){return a===-1?this.slice(a):this.slice(a,+a+1)},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},slice:function(){return this.pushStack(R.apply(this,arguments),"slice",R.call(arguments).join(","))},map:function(a){return this.pushStack(c.map(this, +function(b,d){return a.call(b,d,b)}))},end:function(){return this.prevObject||c(null)},push:ba,sort:[].sort,splice:[].splice};c.fn.init.prototype=c.fn;c.extend=c.fn.extend=function(){var a=arguments[0]||{},b=1,d=arguments.length,f=false,e,j,i,o;if(typeof a==="boolean"){f=a;a=arguments[1]||{};b=2}if(typeof a!=="object"&&!c.isFunction(a))a={};if(d===b){a=this;--b}for(;b
a"; +var e=d.getElementsByTagName("*"),j=d.getElementsByTagName("a")[0];if(!(!e||!e.length||!j)){c.support={leadingWhitespace:d.firstChild.nodeType===3,tbody:!d.getElementsByTagName("tbody").length,htmlSerialize:!!d.getElementsByTagName("link").length,style:/red/.test(j.getAttribute("style")),hrefNormalized:j.getAttribute("href")==="/a",opacity:/^0.55$/.test(j.style.opacity),cssFloat:!!j.style.cssFloat,checkOn:d.getElementsByTagName("input")[0].value==="on",optSelected:s.createElement("select").appendChild(s.createElement("option")).selected, +parentNode:d.removeChild(d.appendChild(s.createElement("div"))).parentNode===null,deleteExpando:true,checkClone:false,scriptEval:false,noCloneEvent:true,boxModel:null};b.type="text/javascript";try{b.appendChild(s.createTextNode("window."+f+"=1;"))}catch(i){}a.insertBefore(b,a.firstChild);if(A[f]){c.support.scriptEval=true;delete A[f]}try{delete b.test}catch(o){c.support.deleteExpando=false}a.removeChild(b);if(d.attachEvent&&d.fireEvent){d.attachEvent("onclick",function k(){c.support.noCloneEvent= +false;d.detachEvent("onclick",k)});d.cloneNode(true).fireEvent("onclick")}d=s.createElement("div");d.innerHTML="";a=s.createDocumentFragment();a.appendChild(d.firstChild);c.support.checkClone=a.cloneNode(true).cloneNode(true).lastChild.checked;c(function(){var k=s.createElement("div");k.style.width=k.style.paddingLeft="1px";s.body.appendChild(k);c.boxModel=c.support.boxModel=k.offsetWidth===2;s.body.removeChild(k).style.display="none"});a=function(k){var n= +s.createElement("div");k="on"+k;var r=k in n;if(!r){n.setAttribute(k,"return;");r=typeof n[k]==="function"}return r};c.support.submitBubbles=a("submit");c.support.changeBubbles=a("change");a=b=d=e=j=null}})();c.props={"for":"htmlFor","class":"className",readonly:"readOnly",maxlength:"maxLength",cellspacing:"cellSpacing",rowspan:"rowSpan",colspan:"colSpan",tabindex:"tabIndex",usemap:"useMap",frameborder:"frameBorder"};var G="jQuery"+J(),Ya=0,za={};c.extend({cache:{},expando:G,noData:{embed:true,object:true, +applet:true},data:function(a,b,d){if(!(a.nodeName&&c.noData[a.nodeName.toLowerCase()])){a=a==A?za:a;var f=a[G],e=c.cache;if(!f&&typeof b==="string"&&d===w)return null;f||(f=++Ya);if(typeof b==="object"){a[G]=f;e[f]=c.extend(true,{},b)}else if(!e[f]){a[G]=f;e[f]={}}a=e[f];if(d!==w)a[b]=d;return typeof b==="string"?a[b]:a}},removeData:function(a,b){if(!(a.nodeName&&c.noData[a.nodeName.toLowerCase()])){a=a==A?za:a;var d=a[G],f=c.cache,e=f[d];if(b){if(e){delete e[b];c.isEmptyObject(e)&&c.removeData(a)}}else{if(c.support.deleteExpando)delete a[c.expando]; +else a.removeAttribute&&a.removeAttribute(c.expando);delete f[d]}}}});c.fn.extend({data:function(a,b){if(typeof a==="undefined"&&this.length)return c.data(this[0]);else if(typeof a==="object")return this.each(function(){c.data(this,a)});var d=a.split(".");d[1]=d[1]?"."+d[1]:"";if(b===w){var f=this.triggerHandler("getData"+d[1]+"!",[d[0]]);if(f===w&&this.length)f=c.data(this[0],a);return f===w&&d[1]?this.data(d[0]):f}else return this.trigger("setData"+d[1]+"!",[d[0],b]).each(function(){c.data(this, +a,b)})},removeData:function(a){return this.each(function(){c.removeData(this,a)})}});c.extend({queue:function(a,b,d){if(a){b=(b||"fx")+"queue";var f=c.data(a,b);if(!d)return f||[];if(!f||c.isArray(d))f=c.data(a,b,c.makeArray(d));else f.push(d);return f}},dequeue:function(a,b){b=b||"fx";var d=c.queue(a,b),f=d.shift();if(f==="inprogress")f=d.shift();if(f){b==="fx"&&d.unshift("inprogress");f.call(a,function(){c.dequeue(a,b)})}}});c.fn.extend({queue:function(a,b){if(typeof a!=="string"){b=a;a="fx"}if(b=== +w)return c.queue(this[0],a);return this.each(function(){var d=c.queue(this,a,b);a==="fx"&&d[0]!=="inprogress"&&c.dequeue(this,a)})},dequeue:function(a){return this.each(function(){c.dequeue(this,a)})},delay:function(a,b){a=c.fx?c.fx.speeds[a]||a:a;b=b||"fx";return this.queue(b,function(){var d=this;setTimeout(function(){c.dequeue(d,b)},a)})},clearQueue:function(a){return this.queue(a||"fx",[])}});var Aa=/[\n\t]/g,ca=/\s+/,Za=/\r/g,$a=/href|src|style/,ab=/(button|input)/i,bb=/(button|input|object|select|textarea)/i, +cb=/^(a|area)$/i,Ba=/radio|checkbox/;c.fn.extend({attr:function(a,b){return X(this,a,b,true,c.attr)},removeAttr:function(a){return this.each(function(){c.attr(this,a,"");this.nodeType===1&&this.removeAttribute(a)})},addClass:function(a){if(c.isFunction(a))return this.each(function(n){var r=c(this);r.addClass(a.call(this,n,r.attr("class")))});if(a&&typeof a==="string")for(var b=(a||"").split(ca),d=0,f=this.length;d-1)return true;return false},val:function(a){if(a===w){var b=this[0];if(b){if(c.nodeName(b,"option"))return(b.attributes.value||{}).specified?b.value:b.text;if(c.nodeName(b,"select")){var d=b.selectedIndex,f=[],e=b.options;b=b.type==="select-one";if(d<0)return null;var j=b?d:0;for(d=b?d+1:e.length;j=0;else if(c.nodeName(this,"select")){var u=c.makeArray(r);c("option",this).each(function(){this.selected= +c.inArray(c(this).val(),u)>=0});if(!u.length)this.selectedIndex=-1}else this.value=r}})}});c.extend({attrFn:{val:true,css:true,html:true,text:true,data:true,width:true,height:true,offset:true},attr:function(a,b,d,f){if(!a||a.nodeType===3||a.nodeType===8)return w;if(f&&b in c.attrFn)return c(a)[b](d);f=a.nodeType!==1||!c.isXMLDoc(a);var e=d!==w;b=f&&c.props[b]||b;if(a.nodeType===1){var j=$a.test(b);if(b in a&&f&&!j){if(e){b==="type"&&ab.test(a.nodeName)&&a.parentNode&&c.error("type property can't be changed"); +a[b]=d}if(c.nodeName(a,"form")&&a.getAttributeNode(b))return a.getAttributeNode(b).nodeValue;if(b==="tabIndex")return(b=a.getAttributeNode("tabIndex"))&&b.specified?b.value:bb.test(a.nodeName)||cb.test(a.nodeName)&&a.href?0:w;return a[b]}if(!c.support.style&&f&&b==="style"){if(e)a.style.cssText=""+d;return a.style.cssText}e&&a.setAttribute(b,""+d);a=!c.support.hrefNormalized&&f&&j?a.getAttribute(b,2):a.getAttribute(b);return a===null?w:a}return c.style(a,b,d)}});var O=/\.(.*)$/,db=function(a){return a.replace(/[^\w\s\.\|`]/g, +function(b){return"\\"+b})};c.event={add:function(a,b,d,f){if(!(a.nodeType===3||a.nodeType===8)){if(a.setInterval&&a!==A&&!a.frameElement)a=A;var e,j;if(d.handler){e=d;d=e.handler}if(!d.guid)d.guid=c.guid++;if(j=c.data(a)){var i=j.events=j.events||{},o=j.handle;if(!o)j.handle=o=function(){return typeof c!=="undefined"&&!c.event.triggered?c.event.handle.apply(o.elem,arguments):w};o.elem=a;b=b.split(" ");for(var k,n=0,r;k=b[n++];){j=e?c.extend({},e):{handler:d,data:f};if(k.indexOf(".")>-1){r=k.split("."); +k=r.shift();j.namespace=r.slice(0).sort().join(".")}else{r=[];j.namespace=""}j.type=k;j.guid=d.guid;var u=i[k],z=c.event.special[k]||{};if(!u){u=i[k]=[];if(!z.setup||z.setup.call(a,f,r,o)===false)if(a.addEventListener)a.addEventListener(k,o,false);else a.attachEvent&&a.attachEvent("on"+k,o)}if(z.add){z.add.call(a,j);if(!j.handler.guid)j.handler.guid=d.guid}u.push(j);c.event.global[k]=true}a=null}}},global:{},remove:function(a,b,d,f){if(!(a.nodeType===3||a.nodeType===8)){var e,j=0,i,o,k,n,r,u,z=c.data(a), +C=z&&z.events;if(z&&C){if(b&&b.type){d=b.handler;b=b.type}if(!b||typeof b==="string"&&b.charAt(0)==="."){b=b||"";for(e in C)c.event.remove(a,e+b)}else{for(b=b.split(" ");e=b[j++];){n=e;i=e.indexOf(".")<0;o=[];if(!i){o=e.split(".");e=o.shift();k=new RegExp("(^|\\.)"+c.map(o.slice(0).sort(),db).join("\\.(?:.*\\.)?")+"(\\.|$)")}if(r=C[e])if(d){n=c.event.special[e]||{};for(B=f||0;B=0){a.type= +e=e.slice(0,-1);a.exclusive=true}if(!d){a.stopPropagation();c.event.global[e]&&c.each(c.cache,function(){this.events&&this.events[e]&&c.event.trigger(a,b,this.handle.elem)})}if(!d||d.nodeType===3||d.nodeType===8)return w;a.result=w;a.target=d;b=c.makeArray(b);b.unshift(a)}a.currentTarget=d;(f=c.data(d,"handle"))&&f.apply(d,b);f=d.parentNode||d.ownerDocument;try{if(!(d&&d.nodeName&&c.noData[d.nodeName.toLowerCase()]))if(d["on"+e]&&d["on"+e].apply(d,b)===false)a.result=false}catch(j){}if(!a.isPropagationStopped()&& +f)c.event.trigger(a,b,f,true);else if(!a.isDefaultPrevented()){f=a.target;var i,o=c.nodeName(f,"a")&&e==="click",k=c.event.special[e]||{};if((!k._default||k._default.call(d,a)===false)&&!o&&!(f&&f.nodeName&&c.noData[f.nodeName.toLowerCase()])){try{if(f[e]){if(i=f["on"+e])f["on"+e]=null;c.event.triggered=true;f[e]()}}catch(n){}if(i)f["on"+e]=i;c.event.triggered=false}}},handle:function(a){var b,d,f,e;a=arguments[0]=c.event.fix(a||A.event);a.currentTarget=this;b=a.type.indexOf(".")<0&&!a.exclusive; +if(!b){d=a.type.split(".");a.type=d.shift();f=new RegExp("(^|\\.)"+d.slice(0).sort().join("\\.(?:.*\\.)?")+"(\\.|$)")}e=c.data(this,"events");d=e[a.type];if(e&&d){d=d.slice(0);e=0;for(var j=d.length;e-1?c.map(a.options,function(f){return f.selected}).join("-"):"";else if(a.nodeName.toLowerCase()==="select")d=a.selectedIndex;return d},fa=function(a,b){var d=a.target,f,e;if(!(!da.test(d.nodeName)||d.readOnly)){f=c.data(d,"_change_data");e=Fa(d);if(a.type!=="focusout"||d.type!=="radio")c.data(d,"_change_data", +e);if(!(f===w||e===f))if(f!=null||e){a.type="change";return c.event.trigger(a,b,d)}}};c.event.special.change={filters:{focusout:fa,click:function(a){var b=a.target,d=b.type;if(d==="radio"||d==="checkbox"||b.nodeName.toLowerCase()==="select")return fa.call(this,a)},keydown:function(a){var b=a.target,d=b.type;if(a.keyCode===13&&b.nodeName.toLowerCase()!=="textarea"||a.keyCode===32&&(d==="checkbox"||d==="radio")||d==="select-multiple")return fa.call(this,a)},beforeactivate:function(a){a=a.target;c.data(a, +"_change_data",Fa(a))}},setup:function(){if(this.type==="file")return false;for(var a in ea)c.event.add(this,a+".specialChange",ea[a]);return da.test(this.nodeName)},teardown:function(){c.event.remove(this,".specialChange");return da.test(this.nodeName)}};ea=c.event.special.change.filters}s.addEventListener&&c.each({focus:"focusin",blur:"focusout"},function(a,b){function d(f){f=c.event.fix(f);f.type=b;return c.event.handle.call(this,f)}c.event.special[b]={setup:function(){this.addEventListener(a, +d,true)},teardown:function(){this.removeEventListener(a,d,true)}}});c.each(["bind","one"],function(a,b){c.fn[b]=function(d,f,e){if(typeof d==="object"){for(var j in d)this[b](j,f,d[j],e);return this}if(c.isFunction(f)){e=f;f=w}var i=b==="one"?c.proxy(e,function(k){c(this).unbind(k,i);return e.apply(this,arguments)}):e;if(d==="unload"&&b!=="one")this.one(d,f,e);else{j=0;for(var o=this.length;j0){y=t;break}}t=t[g]}m[q]=y}}}var f=/((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g, +e=0,j=Object.prototype.toString,i=false,o=true;[0,0].sort(function(){o=false;return 0});var k=function(g,h,l,m){l=l||[];var q=h=h||s;if(h.nodeType!==1&&h.nodeType!==9)return[];if(!g||typeof g!=="string")return l;for(var p=[],v,t,y,S,H=true,M=x(h),I=g;(f.exec(""),v=f.exec(I))!==null;){I=v[3];p.push(v[1]);if(v[2]){S=v[3];break}}if(p.length>1&&r.exec(g))if(p.length===2&&n.relative[p[0]])t=ga(p[0]+p[1],h);else for(t=n.relative[p[0]]?[h]:k(p.shift(),h);p.length;){g=p.shift();if(n.relative[g])g+=p.shift(); +t=ga(g,t)}else{if(!m&&p.length>1&&h.nodeType===9&&!M&&n.match.ID.test(p[0])&&!n.match.ID.test(p[p.length-1])){v=k.find(p.shift(),h,M);h=v.expr?k.filter(v.expr,v.set)[0]:v.set[0]}if(h){v=m?{expr:p.pop(),set:z(m)}:k.find(p.pop(),p.length===1&&(p[0]==="~"||p[0]==="+")&&h.parentNode?h.parentNode:h,M);t=v.expr?k.filter(v.expr,v.set):v.set;if(p.length>0)y=z(t);else H=false;for(;p.length;){var D=p.pop();v=D;if(n.relative[D])v=p.pop();else D="";if(v==null)v=h;n.relative[D](y,v,M)}}else y=[]}y||(y=t);y||k.error(D|| +g);if(j.call(y)==="[object Array]")if(H)if(h&&h.nodeType===1)for(g=0;y[g]!=null;g++){if(y[g]&&(y[g]===true||y[g].nodeType===1&&E(h,y[g])))l.push(t[g])}else for(g=0;y[g]!=null;g++)y[g]&&y[g].nodeType===1&&l.push(t[g]);else l.push.apply(l,y);else z(y,l);if(S){k(S,q,l,m);k.uniqueSort(l)}return l};k.uniqueSort=function(g){if(B){i=o;g.sort(B);if(i)for(var h=1;h":function(g,h){var l=typeof h==="string";if(l&&!/\W/.test(h)){h=h.toLowerCase();for(var m=0,q=g.length;m=0))l||m.push(v);else if(l)h[p]=false;return false},ID:function(g){return g[1].replace(/\\/g,"")},TAG:function(g){return g[1].toLowerCase()}, +CHILD:function(g){if(g[1]==="nth"){var h=/(-?)(\d*)n((?:\+|-)?\d*)/.exec(g[2]==="even"&&"2n"||g[2]==="odd"&&"2n+1"||!/\D/.test(g[2])&&"0n+"+g[2]||g[2]);g[2]=h[1]+(h[2]||1)-0;g[3]=h[3]-0}g[0]=e++;return g},ATTR:function(g,h,l,m,q,p){h=g[1].replace(/\\/g,"");if(!p&&n.attrMap[h])g[1]=n.attrMap[h];if(g[2]==="~=")g[4]=" "+g[4]+" ";return g},PSEUDO:function(g,h,l,m,q){if(g[1]==="not")if((f.exec(g[3])||"").length>1||/^\w/.test(g[3]))g[3]=k(g[3],null,null,h);else{g=k.filter(g[3],h,l,true^q);l||m.push.apply(m, +g);return false}else if(n.match.POS.test(g[0])||n.match.CHILD.test(g[0]))return true;return g},POS:function(g){g.unshift(true);return g}},filters:{enabled:function(g){return g.disabled===false&&g.type!=="hidden"},disabled:function(g){return g.disabled===true},checked:function(g){return g.checked===true},selected:function(g){return g.selected===true},parent:function(g){return!!g.firstChild},empty:function(g){return!g.firstChild},has:function(g,h,l){return!!k(l[3],g).length},header:function(g){return/h\d/i.test(g.nodeName)}, +text:function(g){return"text"===g.type},radio:function(g){return"radio"===g.type},checkbox:function(g){return"checkbox"===g.type},file:function(g){return"file"===g.type},password:function(g){return"password"===g.type},submit:function(g){return"submit"===g.type},image:function(g){return"image"===g.type},reset:function(g){return"reset"===g.type},button:function(g){return"button"===g.type||g.nodeName.toLowerCase()==="button"},input:function(g){return/input|select|textarea|button/i.test(g.nodeName)}}, +setFilters:{first:function(g,h){return h===0},last:function(g,h,l,m){return h===m.length-1},even:function(g,h){return h%2===0},odd:function(g,h){return h%2===1},lt:function(g,h,l){return hl[3]-0},nth:function(g,h,l){return l[3]-0===h},eq:function(g,h,l){return l[3]-0===h}},filter:{PSEUDO:function(g,h,l,m){var q=h[1],p=n.filters[q];if(p)return p(g,l,h,m);else if(q==="contains")return(g.textContent||g.innerText||a([g])||"").indexOf(h[3])>=0;else if(q==="not"){h= +h[3];l=0;for(m=h.length;l=0}},ID:function(g,h){return g.nodeType===1&&g.getAttribute("id")===h},TAG:function(g,h){return h==="*"&&g.nodeType===1||g.nodeName.toLowerCase()===h},CLASS:function(g,h){return(" "+(g.className||g.getAttribute("class"))+" ").indexOf(h)>-1},ATTR:function(g,h){var l=h[1];g=n.attrHandle[l]?n.attrHandle[l](g):g[l]!=null?g[l]:g.getAttribute(l);l=g+"";var m=h[2];h=h[4];return g==null?m==="!=":m=== +"="?l===h:m==="*="?l.indexOf(h)>=0:m==="~="?(" "+l+" ").indexOf(h)>=0:!h?l&&g!==false:m==="!="?l!==h:m==="^="?l.indexOf(h)===0:m==="$="?l.substr(l.length-h.length)===h:m==="|="?l===h||l.substr(0,h.length+1)===h+"-":false},POS:function(g,h,l,m){var q=n.setFilters[h[2]];if(q)return q(g,l,h,m)}}},r=n.match.POS;for(var u in n.match){n.match[u]=new RegExp(n.match[u].source+/(?![^\[]*\])(?![^\(]*\))/.source);n.leftMatch[u]=new RegExp(/(^(?:.|\r|\n)*?)/.source+n.match[u].source.replace(/\\(\d+)/g,function(g, +h){return"\\"+(h-0+1)}))}var z=function(g,h){g=Array.prototype.slice.call(g,0);if(h){h.push.apply(h,g);return h}return g};try{Array.prototype.slice.call(s.documentElement.childNodes,0)}catch(C){z=function(g,h){h=h||[];if(j.call(g)==="[object Array]")Array.prototype.push.apply(h,g);else if(typeof g.length==="number")for(var l=0,m=g.length;l";var l=s.documentElement;l.insertBefore(g,l.firstChild);if(s.getElementById(h)){n.find.ID=function(m,q,p){if(typeof q.getElementById!=="undefined"&&!p)return(q=q.getElementById(m[1]))?q.id===m[1]||typeof q.getAttributeNode!=="undefined"&& +q.getAttributeNode("id").nodeValue===m[1]?[q]:w:[]};n.filter.ID=function(m,q){var p=typeof m.getAttributeNode!=="undefined"&&m.getAttributeNode("id");return m.nodeType===1&&p&&p.nodeValue===q}}l.removeChild(g);l=g=null})();(function(){var g=s.createElement("div");g.appendChild(s.createComment(""));if(g.getElementsByTagName("*").length>0)n.find.TAG=function(h,l){l=l.getElementsByTagName(h[1]);if(h[1]==="*"){h=[];for(var m=0;l[m];m++)l[m].nodeType===1&&h.push(l[m]);l=h}return l};g.innerHTML=""; +if(g.firstChild&&typeof g.firstChild.getAttribute!=="undefined"&&g.firstChild.getAttribute("href")!=="#")n.attrHandle.href=function(h){return h.getAttribute("href",2)};g=null})();s.querySelectorAll&&function(){var g=k,h=s.createElement("div");h.innerHTML="

";if(!(h.querySelectorAll&&h.querySelectorAll(".TEST").length===0)){k=function(m,q,p,v){q=q||s;if(!v&&q.nodeType===9&&!x(q))try{return z(q.querySelectorAll(m),p)}catch(t){}return g(m,q,p,v)};for(var l in g)k[l]=g[l];h=null}}(); +(function(){var g=s.createElement("div");g.innerHTML="
";if(!(!g.getElementsByClassName||g.getElementsByClassName("e").length===0)){g.lastChild.className="e";if(g.getElementsByClassName("e").length!==1){n.order.splice(1,0,"CLASS");n.find.CLASS=function(h,l,m){if(typeof l.getElementsByClassName!=="undefined"&&!m)return l.getElementsByClassName(h[1])};g=null}}})();var E=s.compareDocumentPosition?function(g,h){return!!(g.compareDocumentPosition(h)&16)}: +function(g,h){return g!==h&&(g.contains?g.contains(h):true)},x=function(g){return(g=(g?g.ownerDocument||g:0).documentElement)?g.nodeName!=="HTML":false},ga=function(g,h){var l=[],m="",q;for(h=h.nodeType?[h]:h;q=n.match.PSEUDO.exec(g);){m+=q[0];g=g.replace(n.match.PSEUDO,"")}g=n.relative[g]?g+"*":g;q=0;for(var p=h.length;q=0===d})};c.fn.extend({find:function(a){for(var b=this.pushStack("","find",a),d=0,f=0,e=this.length;f0)for(var j=d;j0},closest:function(a,b){if(c.isArray(a)){var d=[],f=this[0],e,j= +{},i;if(f&&a.length){e=0;for(var o=a.length;e-1:c(f).is(e)){d.push({selector:i,elem:f});delete j[i]}}f=f.parentNode}}return d}var k=c.expr.match.POS.test(a)?c(a,b||this.context):null;return this.map(function(n,r){for(;r&&r.ownerDocument&&r!==b;){if(k?k.index(r)>-1:c(r).is(a))return r;r=r.parentNode}return null})},index:function(a){if(!a||typeof a=== +"string")return c.inArray(this[0],a?c(a):this.parent().children());return c.inArray(a.jquery?a[0]:a,this)},add:function(a,b){a=typeof a==="string"?c(a,b||this.context):c.makeArray(a);b=c.merge(this.get(),a);return this.pushStack(qa(a[0])||qa(b[0])?b:c.unique(b))},andSelf:function(){return this.add(this.prevObject)}});c.each({parent:function(a){return(a=a.parentNode)&&a.nodeType!==11?a:null},parents:function(a){return c.dir(a,"parentNode")},parentsUntil:function(a,b,d){return c.dir(a,"parentNode", +d)},next:function(a){return c.nth(a,2,"nextSibling")},prev:function(a){return c.nth(a,2,"previousSibling")},nextAll:function(a){return c.dir(a,"nextSibling")},prevAll:function(a){return c.dir(a,"previousSibling")},nextUntil:function(a,b,d){return c.dir(a,"nextSibling",d)},prevUntil:function(a,b,d){return c.dir(a,"previousSibling",d)},siblings:function(a){return c.sibling(a.parentNode.firstChild,a)},children:function(a){return c.sibling(a.firstChild)},contents:function(a){return c.nodeName(a,"iframe")? +a.contentDocument||a.contentWindow.document:c.makeArray(a.childNodes)}},function(a,b){c.fn[a]=function(d,f){var e=c.map(this,b,d);eb.test(a)||(f=d);if(f&&typeof f==="string")e=c.filter(f,e);e=this.length>1?c.unique(e):e;if((this.length>1||gb.test(f))&&fb.test(a))e=e.reverse();return this.pushStack(e,a,R.call(arguments).join(","))}});c.extend({filter:function(a,b,d){if(d)a=":not("+a+")";return c.find.matches(a,b)},dir:function(a,b,d){var f=[];for(a=a[b];a&&a.nodeType!==9&&(d===w||a.nodeType!==1||!c(a).is(d));){a.nodeType=== +1&&f.push(a);a=a[b]}return f},nth:function(a,b,d){b=b||1;for(var f=0;a;a=a[d])if(a.nodeType===1&&++f===b)break;return a},sibling:function(a,b){for(var d=[];a;a=a.nextSibling)a.nodeType===1&&a!==b&&d.push(a);return d}});var Ja=/ jQuery\d+="(?:\d+|null)"/g,V=/^\s+/,Ka=/(<([\w:]+)[^>]*?)\/>/g,hb=/^(?:area|br|col|embed|hr|img|input|link|meta|param)$/i,La=/<([\w:]+)/,ib=/"},F={option:[1,""],legend:[1,"
","
"],thead:[1,"","
"],tr:[2,"","
"],td:[3,"","
"],col:[2,"","
"],area:[1,"",""],_default:[0,"",""]};F.optgroup=F.option;F.tbody=F.tfoot=F.colgroup=F.caption=F.thead;F.th=F.td;if(!c.support.htmlSerialize)F._default=[1,"div
","
"];c.fn.extend({text:function(a){if(c.isFunction(a))return this.each(function(b){var d= +c(this);d.text(a.call(this,b,d.text()))});if(typeof a!=="object"&&a!==w)return this.empty().append((this[0]&&this[0].ownerDocument||s).createTextNode(a));return c.text(this)},wrapAll:function(a){if(c.isFunction(a))return this.each(function(d){c(this).wrapAll(a.call(this,d))});if(this[0]){var b=c(a,this[0].ownerDocument).eq(0).clone(true);this[0].parentNode&&b.insertBefore(this[0]);b.map(function(){for(var d=this;d.firstChild&&d.firstChild.nodeType===1;)d=d.firstChild;return d}).append(this)}return this}, +wrapInner:function(a){if(c.isFunction(a))return this.each(function(b){c(this).wrapInner(a.call(this,b))});return this.each(function(){var b=c(this),d=b.contents();d.length?d.wrapAll(a):b.append(a)})},wrap:function(a){return this.each(function(){c(this).wrapAll(a)})},unwrap:function(){return this.parent().each(function(){c.nodeName(this,"body")||c(this).replaceWith(this.childNodes)}).end()},append:function(){return this.domManip(arguments,true,function(a){this.nodeType===1&&this.appendChild(a)})}, +prepend:function(){return this.domManip(arguments,true,function(a){this.nodeType===1&&this.insertBefore(a,this.firstChild)})},before:function(){if(this[0]&&this[0].parentNode)return this.domManip(arguments,false,function(b){this.parentNode.insertBefore(b,this)});else if(arguments.length){var a=c(arguments[0]);a.push.apply(a,this.toArray());return this.pushStack(a,"before",arguments)}},after:function(){if(this[0]&&this[0].parentNode)return this.domManip(arguments,false,function(b){this.parentNode.insertBefore(b, +this.nextSibling)});else if(arguments.length){var a=this.pushStack(this,"after",arguments);a.push.apply(a,c(arguments[0]).toArray());return a}},remove:function(a,b){for(var d=0,f;(f=this[d])!=null;d++)if(!a||c.filter(a,[f]).length){if(!b&&f.nodeType===1){c.cleanData(f.getElementsByTagName("*"));c.cleanData([f])}f.parentNode&&f.parentNode.removeChild(f)}return this},empty:function(){for(var a=0,b;(b=this[a])!=null;a++)for(b.nodeType===1&&c.cleanData(b.getElementsByTagName("*"));b.firstChild;)b.removeChild(b.firstChild); +return this},clone:function(a){var b=this.map(function(){if(!c.support.noCloneEvent&&!c.isXMLDoc(this)){var d=this.outerHTML,f=this.ownerDocument;if(!d){d=f.createElement("div");d.appendChild(this.cloneNode(true));d=d.innerHTML}return c.clean([d.replace(Ja,"").replace(/=([^="'>\s]+\/)>/g,'="$1">').replace(V,"")],f)[0]}else return this.cloneNode(true)});if(a===true){ra(this,b);ra(this.find("*"),b.find("*"))}return b},html:function(a){if(a===w)return this[0]&&this[0].nodeType===1?this[0].innerHTML.replace(Ja, +""):null;else if(typeof a==="string"&&!ta.test(a)&&(c.support.leadingWhitespace||!V.test(a))&&!F[(La.exec(a)||["",""])[1].toLowerCase()]){a=a.replace(Ka,Ma);try{for(var b=0,d=this.length;b0||e.cacheable||this.length>1?k.cloneNode(true):k)}o.length&&c.each(o,Qa)}return this}});c.fragments={};c.each({appendTo:"append",prependTo:"prepend",insertBefore:"before",insertAfter:"after",replaceAll:"replaceWith"},function(a,b){c.fn[a]=function(d){var f=[];d=c(d);var e=this.length===1&&this[0].parentNode;if(e&&e.nodeType===11&&e.childNodes.length===1&&d.length===1){d[b](this[0]); +return this}else{e=0;for(var j=d.length;e0?this.clone(true):this).get();c.fn[b].apply(c(d[e]),i);f=f.concat(i)}return this.pushStack(f,a,d.selector)}}});c.extend({clean:function(a,b,d,f){b=b||s;if(typeof b.createElement==="undefined")b=b.ownerDocument||b[0]&&b[0].ownerDocument||s;for(var e=[],j=0,i;(i=a[j])!=null;j++){if(typeof i==="number")i+="";if(i){if(typeof i==="string"&&!jb.test(i))i=b.createTextNode(i);else if(typeof i==="string"){i=i.replace(Ka,Ma);var o=(La.exec(i)||["", +""])[1].toLowerCase(),k=F[o]||F._default,n=k[0],r=b.createElement("div");for(r.innerHTML=k[1]+i+k[2];n--;)r=r.lastChild;if(!c.support.tbody){n=ib.test(i);o=o==="table"&&!n?r.firstChild&&r.firstChild.childNodes:k[1]===""&&!n?r.childNodes:[];for(k=o.length-1;k>=0;--k)c.nodeName(o[k],"tbody")&&!o[k].childNodes.length&&o[k].parentNode.removeChild(o[k])}!c.support.leadingWhitespace&&V.test(i)&&r.insertBefore(b.createTextNode(V.exec(i)[0]),r.firstChild);i=r.childNodes}if(i.nodeType)e.push(i);else e= +c.merge(e,i)}}if(d)for(j=0;e[j];j++)if(f&&c.nodeName(e[j],"script")&&(!e[j].type||e[j].type.toLowerCase()==="text/javascript"))f.push(e[j].parentNode?e[j].parentNode.removeChild(e[j]):e[j]);else{e[j].nodeType===1&&e.splice.apply(e,[j+1,0].concat(c.makeArray(e[j].getElementsByTagName("script"))));d.appendChild(e[j])}return e},cleanData:function(a){for(var b,d,f=c.cache,e=c.event.special,j=c.support.deleteExpando,i=0,o;(o=a[i])!=null;i++)if(d=o[c.expando]){b=f[d];if(b.events)for(var k in b.events)e[k]? +c.event.remove(o,k):Ca(o,k,b.handle);if(j)delete o[c.expando];else o.removeAttribute&&o.removeAttribute(c.expando);delete f[d]}}});var kb=/z-?index|font-?weight|opacity|zoom|line-?height/i,Na=/alpha\([^)]*\)/,Oa=/opacity=([^)]*)/,ha=/float/i,ia=/-([a-z])/ig,lb=/([A-Z])/g,mb=/^-?\d+(?:px)?$/i,nb=/^-?\d/,ob={position:"absolute",visibility:"hidden",display:"block"},pb=["Left","Right"],qb=["Top","Bottom"],rb=s.defaultView&&s.defaultView.getComputedStyle,Pa=c.support.cssFloat?"cssFloat":"styleFloat",ja= +function(a,b){return b.toUpperCase()};c.fn.css=function(a,b){return X(this,a,b,true,function(d,f,e){if(e===w)return c.curCSS(d,f);if(typeof e==="number"&&!kb.test(f))e+="px";c.style(d,f,e)})};c.extend({style:function(a,b,d){if(!a||a.nodeType===3||a.nodeType===8)return w;if((b==="width"||b==="height")&&parseFloat(d)<0)d=w;var f=a.style||a,e=d!==w;if(!c.support.opacity&&b==="opacity"){if(e){f.zoom=1;b=parseInt(d,10)+""==="NaN"?"":"alpha(opacity="+d*100+")";a=f.filter||c.curCSS(a,"filter")||"";f.filter= +Na.test(a)?a.replace(Na,b):b}return f.filter&&f.filter.indexOf("opacity=")>=0?parseFloat(Oa.exec(f.filter)[1])/100+"":""}if(ha.test(b))b=Pa;b=b.replace(ia,ja);if(e)f[b]=d;return f[b]},css:function(a,b,d,f){if(b==="width"||b==="height"){var e,j=b==="width"?pb:qb;function i(){e=b==="width"?a.offsetWidth:a.offsetHeight;f!=="border"&&c.each(j,function(){f||(e-=parseFloat(c.curCSS(a,"padding"+this,true))||0);if(f==="margin")e+=parseFloat(c.curCSS(a,"margin"+this,true))||0;else e-=parseFloat(c.curCSS(a, +"border"+this+"Width",true))||0})}a.offsetWidth!==0?i():c.swap(a,ob,i);return Math.max(0,Math.round(e))}return c.curCSS(a,b,d)},curCSS:function(a,b,d){var f,e=a.style;if(!c.support.opacity&&b==="opacity"&&a.currentStyle){f=Oa.test(a.currentStyle.filter||"")?parseFloat(RegExp.$1)/100+"":"";return f===""?"1":f}if(ha.test(b))b=Pa;if(!d&&e&&e[b])f=e[b];else if(rb){if(ha.test(b))b="float";b=b.replace(lb,"-$1").toLowerCase();e=a.ownerDocument.defaultView;if(!e)return null;if(a=e.getComputedStyle(a,null))f= +a.getPropertyValue(b);if(b==="opacity"&&f==="")f="1"}else if(a.currentStyle){d=b.replace(ia,ja);f=a.currentStyle[b]||a.currentStyle[d];if(!mb.test(f)&&nb.test(f)){b=e.left;var j=a.runtimeStyle.left;a.runtimeStyle.left=a.currentStyle.left;e.left=d==="fontSize"?"1em":f||0;f=e.pixelLeft+"px";e.left=b;a.runtimeStyle.left=j}}return f},swap:function(a,b,d){var f={};for(var e in b){f[e]=a.style[e];a.style[e]=b[e]}d.call(a);for(e in b)a.style[e]=f[e]}});if(c.expr&&c.expr.filters){c.expr.filters.hidden=function(a){var b= +a.offsetWidth,d=a.offsetHeight,f=a.nodeName.toLowerCase()==="tr";return b===0&&d===0&&!f?true:b>0&&d>0&&!f?false:c.curCSS(a,"display")==="none"};c.expr.filters.visible=function(a){return!c.expr.filters.hidden(a)}}var sb=J(),tb=//gi,ub=/select|textarea/i,vb=/color|date|datetime|email|hidden|month|number|password|range|search|tel|text|time|url|week/i,N=/=\?(&|$)/,ka=/\?/,wb=/(\?|&)_=.*?(&|$)/,xb=/^(\w+:)?\/\/([^\/?#]+)/,yb=/%20/g,zb=c.fn.load;c.fn.extend({load:function(a,b,d){if(typeof a!== +"string")return zb.call(this,a);else if(!this.length)return this;var f=a.indexOf(" ");if(f>=0){var e=a.slice(f,a.length);a=a.slice(0,f)}f="GET";if(b)if(c.isFunction(b)){d=b;b=null}else if(typeof b==="object"){b=c.param(b,c.ajaxSettings.traditional);f="POST"}var j=this;c.ajax({url:a,type:f,dataType:"html",data:b,complete:function(i,o){if(o==="success"||o==="notmodified")j.html(e?c("
").append(i.responseText.replace(tb,"")).find(e):i.responseText);d&&j.each(d,[i.responseText,o,i])}});return this}, +serialize:function(){return c.param(this.serializeArray())},serializeArray:function(){return this.map(function(){return this.elements?c.makeArray(this.elements):this}).filter(function(){return this.name&&!this.disabled&&(this.checked||ub.test(this.nodeName)||vb.test(this.type))}).map(function(a,b){a=c(this).val();return a==null?null:c.isArray(a)?c.map(a,function(d){return{name:b.name,value:d}}):{name:b.name,value:a}}).get()}});c.each("ajaxStart ajaxStop ajaxComplete ajaxError ajaxSuccess ajaxSend".split(" "), +function(a,b){c.fn[b]=function(d){return this.bind(b,d)}});c.extend({get:function(a,b,d,f){if(c.isFunction(b)){f=f||d;d=b;b=null}return c.ajax({type:"GET",url:a,data:b,success:d,dataType:f})},getScript:function(a,b){return c.get(a,null,b,"script")},getJSON:function(a,b,d){return c.get(a,b,d,"json")},post:function(a,b,d,f){if(c.isFunction(b)){f=f||d;d=b;b={}}return c.ajax({type:"POST",url:a,data:b,success:d,dataType:f})},ajaxSetup:function(a){c.extend(c.ajaxSettings,a)},ajaxSettings:{url:location.href, +global:true,type:"GET",contentType:"application/x-www-form-urlencoded",processData:true,async:true,xhr:A.XMLHttpRequest&&(A.location.protocol!=="file:"||!A.ActiveXObject)?function(){return new A.XMLHttpRequest}:function(){try{return new A.ActiveXObject("Microsoft.XMLHTTP")}catch(a){}},accepts:{xml:"application/xml, text/xml",html:"text/html",script:"text/javascript, application/javascript",json:"application/json, text/javascript",text:"text/plain",_default:"*/*"}},lastModified:{},etag:{},ajax:function(a){function b(){e.success&& +e.success.call(k,o,i,x);e.global&&f("ajaxSuccess",[x,e])}function d(){e.complete&&e.complete.call(k,x,i);e.global&&f("ajaxComplete",[x,e]);e.global&&!--c.active&&c.event.trigger("ajaxStop")}function f(q,p){(e.context?c(e.context):c.event).trigger(q,p)}var e=c.extend(true,{},c.ajaxSettings,a),j,i,o,k=a&&a.context||e,n=e.type.toUpperCase();if(e.data&&e.processData&&typeof e.data!=="string")e.data=c.param(e.data,e.traditional);if(e.dataType==="jsonp"){if(n==="GET")N.test(e.url)||(e.url+=(ka.test(e.url)? +"&":"?")+(e.jsonp||"callback")+"=?");else if(!e.data||!N.test(e.data))e.data=(e.data?e.data+"&":"")+(e.jsonp||"callback")+"=?";e.dataType="json"}if(e.dataType==="json"&&(e.data&&N.test(e.data)||N.test(e.url))){j=e.jsonpCallback||"jsonp"+sb++;if(e.data)e.data=(e.data+"").replace(N,"="+j+"$1");e.url=e.url.replace(N,"="+j+"$1");e.dataType="script";A[j]=A[j]||function(q){o=q;b();d();A[j]=w;try{delete A[j]}catch(p){}z&&z.removeChild(C)}}if(e.dataType==="script"&&e.cache===null)e.cache=false;if(e.cache=== +false&&n==="GET"){var r=J(),u=e.url.replace(wb,"$1_="+r+"$2");e.url=u+(u===e.url?(ka.test(e.url)?"&":"?")+"_="+r:"")}if(e.data&&n==="GET")e.url+=(ka.test(e.url)?"&":"?")+e.data;e.global&&!c.active++&&c.event.trigger("ajaxStart");r=(r=xb.exec(e.url))&&(r[1]&&r[1]!==location.protocol||r[2]!==location.host);if(e.dataType==="script"&&n==="GET"&&r){var z=s.getElementsByTagName("head")[0]||s.documentElement,C=s.createElement("script");C.src=e.url;if(e.scriptCharset)C.charset=e.scriptCharset;if(!j){var B= +false;C.onload=C.onreadystatechange=function(){if(!B&&(!this.readyState||this.readyState==="loaded"||this.readyState==="complete")){B=true;b();d();C.onload=C.onreadystatechange=null;z&&C.parentNode&&z.removeChild(C)}}}z.insertBefore(C,z.firstChild);return w}var E=false,x=e.xhr();if(x){e.username?x.open(n,e.url,e.async,e.username,e.password):x.open(n,e.url,e.async);try{if(e.data||a&&a.contentType)x.setRequestHeader("Content-Type",e.contentType);if(e.ifModified){c.lastModified[e.url]&&x.setRequestHeader("If-Modified-Since", +c.lastModified[e.url]);c.etag[e.url]&&x.setRequestHeader("If-None-Match",c.etag[e.url])}r||x.setRequestHeader("X-Requested-With","XMLHttpRequest");x.setRequestHeader("Accept",e.dataType&&e.accepts[e.dataType]?e.accepts[e.dataType]+", */*":e.accepts._default)}catch(ga){}if(e.beforeSend&&e.beforeSend.call(k,x,e)===false){e.global&&!--c.active&&c.event.trigger("ajaxStop");x.abort();return false}e.global&&f("ajaxSend",[x,e]);var g=x.onreadystatechange=function(q){if(!x||x.readyState===0||q==="abort"){E|| +d();E=true;if(x)x.onreadystatechange=c.noop}else if(!E&&x&&(x.readyState===4||q==="timeout")){E=true;x.onreadystatechange=c.noop;i=q==="timeout"?"timeout":!c.httpSuccess(x)?"error":e.ifModified&&c.httpNotModified(x,e.url)?"notmodified":"success";var p;if(i==="success")try{o=c.httpData(x,e.dataType,e)}catch(v){i="parsererror";p=v}if(i==="success"||i==="notmodified")j||b();else c.handleError(e,x,i,p);d();q==="timeout"&&x.abort();if(e.async)x=null}};try{var h=x.abort;x.abort=function(){x&&h.call(x); +g("abort")}}catch(l){}e.async&&e.timeout>0&&setTimeout(function(){x&&!E&&g("timeout")},e.timeout);try{x.send(n==="POST"||n==="PUT"||n==="DELETE"?e.data:null)}catch(m){c.handleError(e,x,null,m);d()}e.async||g();return x}},handleError:function(a,b,d,f){if(a.error)a.error.call(a.context||a,b,d,f);if(a.global)(a.context?c(a.context):c.event).trigger("ajaxError",[b,a,f])},active:0,httpSuccess:function(a){try{return!a.status&&location.protocol==="file:"||a.status>=200&&a.status<300||a.status===304||a.status=== +1223||a.status===0}catch(b){}return false},httpNotModified:function(a,b){var d=a.getResponseHeader("Last-Modified"),f=a.getResponseHeader("Etag");if(d)c.lastModified[b]=d;if(f)c.etag[b]=f;return a.status===304||a.status===0},httpData:function(a,b,d){var f=a.getResponseHeader("content-type")||"",e=b==="xml"||!b&&f.indexOf("xml")>=0;a=e?a.responseXML:a.responseText;e&&a.documentElement.nodeName==="parsererror"&&c.error("parsererror");if(d&&d.dataFilter)a=d.dataFilter(a,b);if(typeof a==="string")if(b=== +"json"||!b&&f.indexOf("json")>=0)a=c.parseJSON(a);else if(b==="script"||!b&&f.indexOf("javascript")>=0)c.globalEval(a);return a},param:function(a,b){function d(i,o){if(c.isArray(o))c.each(o,function(k,n){b||/\[\]$/.test(i)?f(i,n):d(i+"["+(typeof n==="object"||c.isArray(n)?k:"")+"]",n)});else!b&&o!=null&&typeof o==="object"?c.each(o,function(k,n){d(i+"["+k+"]",n)}):f(i,o)}function f(i,o){o=c.isFunction(o)?o():o;e[e.length]=encodeURIComponent(i)+"="+encodeURIComponent(o)}var e=[];if(b===w)b=c.ajaxSettings.traditional; +if(c.isArray(a)||a.jquery)c.each(a,function(){f(this.name,this.value)});else for(var j in a)d(j,a[j]);return e.join("&").replace(yb,"+")}});var la={},Ab=/toggle|show|hide/,Bb=/^([+-]=)?([\d+-.]+)(.*)$/,W,va=[["height","marginTop","marginBottom","paddingTop","paddingBottom"],["width","marginLeft","marginRight","paddingLeft","paddingRight"],["opacity"]];c.fn.extend({show:function(a,b){if(a||a===0)return this.animate(K("show",3),a,b);else{a=0;for(b=this.length;a").appendTo("body");f=e.css("display");if(f==="none")f="block";e.remove();la[d]=f}c.data(this[a],"olddisplay",f)}}a=0;for(b=this.length;a=0;f--)if(d[f].elem===this){b&&d[f](true);d.splice(f,1)}});b||this.dequeue();return this}});c.each({slideDown:K("show",1),slideUp:K("hide",1),slideToggle:K("toggle",1),fadeIn:{opacity:"show"},fadeOut:{opacity:"hide"}},function(a,b){c.fn[a]=function(d,f){return this.animate(b,d,f)}});c.extend({speed:function(a,b,d){var f=a&&typeof a==="object"?a:{complete:d||!d&&b||c.isFunction(a)&&a,duration:a,easing:d&&b||b&&!c.isFunction(b)&&b};f.duration=c.fx.off?0:typeof f.duration=== +"number"?f.duration:c.fx.speeds[f.duration]||c.fx.speeds._default;f.old=f.complete;f.complete=function(){f.queue!==false&&c(this).dequeue();c.isFunction(f.old)&&f.old.call(this)};return f},easing:{linear:function(a,b,d,f){return d+f*a},swing:function(a,b,d,f){return(-Math.cos(a*Math.PI)/2+0.5)*f+d}},timers:[],fx:function(a,b,d){this.options=b;this.elem=a;this.prop=d;if(!b.orig)b.orig={}}});c.fx.prototype={update:function(){this.options.step&&this.options.step.call(this.elem,this.now,this);(c.fx.step[this.prop]|| +c.fx.step._default)(this);if((this.prop==="height"||this.prop==="width")&&this.elem.style)this.elem.style.display="block"},cur:function(a){if(this.elem[this.prop]!=null&&(!this.elem.style||this.elem.style[this.prop]==null))return this.elem[this.prop];return(a=parseFloat(c.css(this.elem,this.prop,a)))&&a>-10000?a:parseFloat(c.curCSS(this.elem,this.prop))||0},custom:function(a,b,d){function f(j){return e.step(j)}this.startTime=J();this.start=a;this.end=b;this.unit=d||this.unit||"px";this.now=this.start; +this.pos=this.state=0;var e=this;f.elem=this.elem;if(f()&&c.timers.push(f)&&!W)W=setInterval(c.fx.tick,13)},show:function(){this.options.orig[this.prop]=c.style(this.elem,this.prop);this.options.show=true;this.custom(this.prop==="width"||this.prop==="height"?1:0,this.cur());c(this.elem).show()},hide:function(){this.options.orig[this.prop]=c.style(this.elem,this.prop);this.options.hide=true;this.custom(this.cur(),0)},step:function(a){var b=J(),d=true;if(a||b>=this.options.duration+this.startTime){this.now= +this.end;this.pos=this.state=1;this.update();this.options.curAnim[this.prop]=true;for(var f in this.options.curAnim)if(this.options.curAnim[f]!==true)d=false;if(d){if(this.options.display!=null){this.elem.style.overflow=this.options.overflow;a=c.data(this.elem,"olddisplay");this.elem.style.display=a?a:this.options.display;if(c.css(this.elem,"display")==="none")this.elem.style.display="block"}this.options.hide&&c(this.elem).hide();if(this.options.hide||this.options.show)for(var e in this.options.curAnim)c.style(this.elem, +e,this.options.orig[e]);this.options.complete.call(this.elem)}return false}else{e=b-this.startTime;this.state=e/this.options.duration;a=this.options.easing||(c.easing.swing?"swing":"linear");this.pos=c.easing[this.options.specialEasing&&this.options.specialEasing[this.prop]||a](this.state,e,0,1,this.options.duration);this.now=this.start+(this.end-this.start)*this.pos;this.update()}return true}};c.extend(c.fx,{tick:function(){for(var a=c.timers,b=0;b
"; +a.insertBefore(b,a.firstChild);d=b.firstChild;f=d.firstChild;e=d.nextSibling.firstChild.firstChild;this.doesNotAddBorder=f.offsetTop!==5;this.doesAddBorderForTableAndCells=e.offsetTop===5;f.style.position="fixed";f.style.top="20px";this.supportsFixedPosition=f.offsetTop===20||f.offsetTop===15;f.style.position=f.style.top="";d.style.overflow="hidden";d.style.position="relative";this.subtractsBorderForOverflowNotVisible=f.offsetTop===-5;this.doesNotIncludeMarginInBodyOffset=a.offsetTop!==j;a.removeChild(b); +c.offset.initialize=c.noop},bodyOffset:function(a){var b=a.offsetTop,d=a.offsetLeft;c.offset.initialize();if(c.offset.doesNotIncludeMarginInBodyOffset){b+=parseFloat(c.curCSS(a,"marginTop",true))||0;d+=parseFloat(c.curCSS(a,"marginLeft",true))||0}return{top:b,left:d}},setOffset:function(a,b,d){if(/static/.test(c.curCSS(a,"position")))a.style.position="relative";var f=c(a),e=f.offset(),j=parseInt(c.curCSS(a,"top",true),10)||0,i=parseInt(c.curCSS(a,"left",true),10)||0;if(c.isFunction(b))b=b.call(a, +d,e);d={top:b.top-e.top+j,left:b.left-e.left+i};"using"in b?b.using.call(a,d):f.css(d)}};c.fn.extend({position:function(){if(!this[0])return null;var a=this[0],b=this.offsetParent(),d=this.offset(),f=/^body|html$/i.test(b[0].nodeName)?{top:0,left:0}:b.offset();d.top-=parseFloat(c.curCSS(a,"marginTop",true))||0;d.left-=parseFloat(c.curCSS(a,"marginLeft",true))||0;f.top+=parseFloat(c.curCSS(b[0],"borderTopWidth",true))||0;f.left+=parseFloat(c.curCSS(b[0],"borderLeftWidth",true))||0;return{top:d.top- +f.top,left:d.left-f.left}},offsetParent:function(){return this.map(function(){for(var a=this.offsetParent||s.body;a&&!/^body|html$/i.test(a.nodeName)&&c.css(a,"position")==="static";)a=a.offsetParent;return a})}});c.each(["Left","Top"],function(a,b){var d="scroll"+b;c.fn[d]=function(f){var e=this[0],j;if(!e)return null;if(f!==w)return this.each(function(){if(j=wa(this))j.scrollTo(!a?f:c(j).scrollLeft(),a?f:c(j).scrollTop());else this[d]=f});else return(j=wa(e))?"pageXOffset"in j?j[a?"pageYOffset": +"pageXOffset"]:c.support.boxModel&&j.document.documentElement[d]||j.document.body[d]:e[d]}});c.each(["Height","Width"],function(a,b){var d=b.toLowerCase();c.fn["inner"+b]=function(){return this[0]?c.css(this[0],d,false,"padding"):null};c.fn["outer"+b]=function(f){return this[0]?c.css(this[0],d,false,f?"margin":"border"):null};c.fn[d]=function(f){var e=this[0];if(!e)return f==null?null:this;if(c.isFunction(f))return this.each(function(j){var i=c(this);i[d](f.call(this,j,i[d]()))});return"scrollTo"in +e&&e.document?e.document.compatMode==="CSS1Compat"&&e.document.documentElement["client"+b]||e.document.body["client"+b]:e.nodeType===9?Math.max(e.documentElement["client"+b],e.body["scroll"+b],e.documentElement["scroll"+b],e.body["offset"+b],e.documentElement["offset"+b]):f===w?c.css(e,d):this.css(d,typeof f==="string"?f:f+"px")}});A.jQuery=A.$=c})(window); diff --git a/src/civetweb/examples/_obsolete/docroot/login.html b/src/civetweb/examples/_obsolete/docroot/login.html new file mode 100644 index 000000000..2b09f01c5 --- /dev/null +++ b/src/civetweb/examples/_obsolete/docroot/login.html @@ -0,0 +1,43 @@ + + + + + Civetweb chat: login + + + + + + + +
+

Civetweb chat server login

+
+ Username and password can be any non-empty strings. +
+
+
+
+
+ +
+
+ + diff --git a/src/civetweb/examples/_obsolete/docroot/logo.png b/src/civetweb/examples/_obsolete/docroot/logo.png new file mode 100644 index 000000000..8a47c0ab8 Binary files /dev/null and b/src/civetweb/examples/_obsolete/docroot/logo.png differ diff --git a/src/civetweb/examples/_obsolete/docroot/main.js b/src/civetweb/examples/_obsolete/docroot/main.js new file mode 100644 index 000000000..d4af86ddb --- /dev/null +++ b/src/civetweb/examples/_obsolete/docroot/main.js @@ -0,0 +1,107 @@ +// This file is part of Civetweb project, +// http://sourceforge.net/projects/civetweb/ + +var chat = { + // Backend URL, string. + // 'http://backend.address.com' or '' if backend is the same as frontend + backendUrl: '', + maxVisibleMessages: 10, + errorMessageFadeOutTimeoutMs: 2000, + errorMessageFadeOutTimer: null, + lastMessageId: 0, + getMessagesIntervalMs: 1000, +}; + +chat.normalizeText = function(text) { + return text.replace('<', '<').replace('>', '>'); +}; + +chat.refresh = function(data) { + + if (data === undefined) { + return; + } + + $.each(data, function(index, entry) { + var row = $('
').addClass('message-row').appendTo('#mml'); + var timestamp = (new Date(entry.timestamp * 1000)).toLocaleTimeString(); + $('') + .addClass('message-timestamp') + .html('[' + timestamp + ']') + .prependTo(row); + $('') + .addClass('message-user') + .addClass(entry.user ? '' : 'message-user-server') + .html(chat.normalizeText((entry.user || '[server]') + ':')) + .appendTo(row); + $('') + .addClass('message-text') + .addClass(entry.user ? '' : 'message-text-server') + .html(chat.normalizeText(entry.text)) + .appendTo(row); + chat.lastMessageId = Math.max(chat.lastMessageId, entry.id) + 1; + }); + + // Keep only chat.maxVisibleMessages, delete older ones. + while ($('#mml').children().length > chat.maxVisibleMessages) { + $('#mml div:first-child').remove(); + } +}; + +chat.getMessages = function(enter_loop) { + $.ajax({ + dataType: 'jsonp', + url: chat.backendUrl + '/ajax/get_messages', + data: {last_id: chat.lastMessageId}, + success: chat.refresh, + error: function() { + }, + }); + if (enter_loop) { + window.setTimeout('chat.getMessages(true)', chat.getMessagesIntervalMs); + } +}; + +chat.handleMenuItemClick = function(ev) { + $('.menu-item').removeClass('menu-item-selected'); // Deselect menu buttons + $(this).addClass('menu-item-selected'); // Select clicked button + $('.main').addClass('hidden'); // Hide all main windows + $('#' + $(this).attr('name')).removeClass('hidden'); // Show main window +}; + +chat.showError = function(message) { + $('#error').html(message).fadeIn('fast'); + window.clearTimeout(chat.errorMessageFadeOutTimer); + chat.errorMessageFadeOutTimer = window.setTimeout(function() { + $('#error').fadeOut('slow'); + }, chat.errorMessageFadeOutTimeoutMs); +}; + +chat.handleMessageInput = function(ev) { + var input = ev.target; + if (ev.keyCode != 13 || !input.value) + return; + //input.disabled = true; + $.ajax({ + dataType: 'jsonp', + url: chat.backendUrl + '/ajax/send_message', + data: {text: input.value}, + success: function(ev) { + input.value = ''; + input.disabled = false; + chat.getMessages(false); + }, + error: function(ev) { + chat.showError('Error sending message'); + input.disabled = false; + }, + }); +}; + +$(document).ready(function() { + $('.menu-item').click(chat.handleMenuItemClick); + $('.message-input').keypress(chat.handleMessageInput); + chat.getMessages(true); +}); + +// vim:ts=2:sw=2:et diff --git a/src/civetweb/examples/_obsolete/docroot/prime_numbers.lp b/src/civetweb/examples/_obsolete/docroot/prime_numbers.lp new file mode 100644 index 000000000..0c71bb824 --- /dev/null +++ b/src/civetweb/examples/_obsolete/docroot/prime_numbers.lp @@ -0,0 +1,46 @@ +HTTP/1.0 200 OK +Content-Type: text/html + + +

Prime numbers from 0 to 100, calculated by Lua:

+ ' .. i .. '
 ') end + end + + ?> + +

Reading POST data from Lua (click submit):

+
+ +
+   POST data: []
+   request method: []
+   IP/port: []
+   URI: []
+   HTTP version []
+   HEADERS:
+   
+
+ diff --git a/src/civetweb/examples/_obsolete/docroot/style.css b/src/civetweb/examples/_obsolete/docroot/style.css new file mode 100644 index 000000000..716351d2d --- /dev/null +++ b/src/civetweb/examples/_obsolete/docroot/style.css @@ -0,0 +1,154 @@ +/* + * vim:ts=2:sw=2:et:ai + */ + +body { + font: 13px Arial; margin: 0.5em 1em; +} + +#logo { + background: url('logo.png') no-repeat ; + width: 160px; + height: 40px; + float: left; +} + +td { + text-align: left; +} + +#motd { + margin-left: 170px; +} + +.infobox { + background: #eed; + padding: 1px 1em; +} + +.help-message { + color: #aaa; +} + +#middle { + margin: 0.5em 0; +} + +#error { + background: #c44; + color: white; + font-weight: bold; +} + +#content, .menu-item-selected, .chat-title, .chat-content { + background: #c3d9ff; +} + +#content { + overflow: hidden; + min-height: 7em; + padding: 1em; +} + +.chat-title { + padding: 1px 1ex; +} + +.chat-content { + padding: 1ex; +} + +.chat-window { +} + +.message-row { + margin: 2px; + border-bottom: 1px solid #bbb; +} + +.message-timestamp { + color: #484; +} + +.message-user { + margin-left: 0.5em; + font-weight: bold; +} + +.message-text { + margin-left: 0.5em; +} + +.message-user-server { + color: purple; +} + +.message-text-server { + font-style: italic; +} + +.main { + padding: 0.5em; + background: #f0fcff; +} + +#menu { + margin-top: 1em; + min-width: 7em; + float: left; +} + +#footer { + position: fixed; + bottom: 0; + right: 0; + color: #ccc; + padding: 0.5em; +} + +.section { + clear: both; +} + +.hidden { + display: none; +} + +.menu-item { + cursor: pointer; + padding: 0.1em 0.5em; +} + +.menu-item-selected { + font-weight: bold; +} + +.message-list { + min-height: 1em; + background: white; + margin: 0.5em 0; +} + +.rounded { + border-radius: 6px; + -moz-border-radius: 6px; + -webkit-border-radius: 6px; +} + +.left-rounded { + border-radius: 6px 0 0 6px; + -moz-border-radius: 6px 0 0 6px; + -webkit-border-radius: 6px 0 0 6px; +} + +.bottom-rounded { + border-radius: 0 0 6px 6px; + -moz-border-radius: 0 0 6px 6px; + -webkit-border-radius: 0 0 6px 6px; +} + +.top-rounded { + border-radius: 6px 6px 0 0; + -moz-border-radius: 6px 6px 0 0; + -webkit-border-radius: 6px 6px 0 0; +} diff --git a/src/civetweb/examples/_obsolete/hello/Makefile b/src/civetweb/examples/_obsolete/hello/Makefile new file mode 100644 index 000000000..4424e3b58 --- /dev/null +++ b/src/civetweb/examples/_obsolete/hello/Makefile @@ -0,0 +1,36 @@ +# +# Copyright (c) 2013 No Face Press, LLC +# License http://opensource.org/licenses/mit-license.php MIT License +# + +#This makefile is used to test the other Makefiles + + +PROG = hello +SRC = hello.c + +TOP = ../.. +CIVETWEB_LIB = libcivetweb.a + +CFLAGS = -I$(TOP)/include $(COPT) +LIBS = -lpthread + +include $(TOP)/resources/Makefile.in-os + +ifeq ($(TARGET_OS),LINUX) + LIBS += -ldl +endif + +all: $(PROG) + +$(PROG): $(CIVETWEB_LIB) $(SRC) + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS) + +$(CIVETWEB_LIB): + $(MAKE) -C $(TOP) clean lib + cp $(TOP)/$(CIVETWEB_LIB) . + +clean: + rm -f $(CIVETWEB_LIB) $(PROG) + +.PHONY: all clean diff --git a/src/civetweb/examples/_obsolete/hello/hello.c b/src/civetweb/examples/_obsolete/hello/hello.c new file mode 100644 index 000000000..39253c1ad --- /dev/null +++ b/src/civetweb/examples/_obsolete/hello/hello.c @@ -0,0 +1,53 @@ +#include +#include +#include "civetweb.h" + +// This function will be called by civetweb on every new request. +static int begin_request_handler(struct mg_connection *conn) +{ + const struct mg_request_info *request_info = mg_get_request_info(conn); + char content[100]; + + // Prepare the message we're going to send + int content_length = snprintf(content, sizeof(content), + "Hello from civetweb! Remote port: %d", + request_info->remote_port); + + // Send HTTP reply to the client + mg_printf(conn, + "HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: %d\r\n" // Always set Content-Length + "\r\n" + "%s", + content_length, content); + + // Returning non-zero tells civetweb that our function has replied to + // the client, and civetweb should not send client any more data. + return 1; +} + +int main(void) +{ + struct mg_context *ctx; + struct mg_callbacks callbacks; + + // List of options. Last element must be NULL. + const char *options[] = {"listening_ports", "8080", NULL}; + + // Prepare callbacks structure. We have only one callback, the rest are NULL. + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.begin_request = begin_request_handler; + + // Start the web server. + ctx = mg_start(&callbacks, NULL, options); + + // Wait until user hits "enter". Server is running in separate thread. + // Navigating to http://localhost:8080 will invoke begin_request_handler(). + getchar(); + + // Stop the server. + mg_stop(ctx); + + return 0; +} diff --git a/src/civetweb/examples/_obsolete/lua/lua_dll.c b/src/civetweb/examples/_obsolete/lua/lua_dll.c new file mode 100644 index 000000000..79e6b97c0 --- /dev/null +++ b/src/civetweb/examples/_obsolete/lua/lua_dll.c @@ -0,0 +1,21 @@ +#include + +#include "lua.h" +#include "lauxlib.h" + +static int smile(lua_State *L) +{ + (void) L; // Unused + printf("%s\n", ":-)"); + return 0; +} + +int LUA_API luaopen_lua_dll(lua_State *L) +{ + static const struct luaL_Reg api[] = { + {"smile", smile}, + {NULL, NULL}, + }; + luaL_openlib(L, "lua_dll", api, 0); + return 1; +} diff --git a/src/civetweb/examples/_obsolete/post/Makefile b/src/civetweb/examples/_obsolete/post/Makefile new file mode 100644 index 000000000..6e504f437 --- /dev/null +++ b/src/civetweb/examples/_obsolete/post/Makefile @@ -0,0 +1,36 @@ +# +# Copyright (c) 2013 No Face Press, LLC +# License http://opensource.org/licenses/mit-license.php MIT License +# + +#This makefile is used to test the other Makefiles + + +PROG = post +SRC = post.c + +TOP = ../.. +CIVETWEB_LIB = libcivetweb.a + +CFLAGS = -I$(TOP)/include $(COPT) +LIBS = -lpthread + +include $(TOP)/resources/Makefile.in-os + +ifeq ($(TARGET_OS),LINUX) + LIBS += -ldl +endif + +all: $(PROG) + +$(PROG): $(CIVETWEB_LIB) $(SRC) + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS) + +$(CIVETWEB_LIB): + $(MAKE) -C $(TOP) clean lib + cp $(TOP)/$(CIVETWEB_LIB) . + +clean: + rm -f $(CIVETWEB_LIB) $(PROG) + +.PHONY: all clean diff --git a/src/civetweb/examples/_obsolete/post/post.c b/src/civetweb/examples/_obsolete/post/post.c new file mode 100644 index 000000000..3c5c68c8d --- /dev/null +++ b/src/civetweb/examples/_obsolete/post/post.c @@ -0,0 +1,58 @@ +#include +#include +#include "civetweb.h" + +static const char *html_form = + "POST example." + "
" + "Input 1:
" + "Input 2:
" + "" + "
"; + +static int begin_request_handler(struct mg_connection *conn) +{ + const struct mg_request_info *ri = mg_get_request_info(conn); + char post_data[1024], input1[sizeof(post_data)], input2[sizeof(post_data)]; + int post_data_len; + + if (!strcmp(ri->uri, "/handle_post_request")) { + // User has submitted a form, show submitted data and a variable value + post_data_len = mg_read(conn, post_data, sizeof(post_data)); + + // Parse form data. input1 and input2 are guaranteed to be NUL-terminated + mg_get_var(post_data, post_data_len, "input_1", input1, sizeof(input1)); + mg_get_var(post_data, post_data_len, "input_2", input2, sizeof(input2)); + + // Send reply to the client, showing submitted form values. + mg_printf(conn, "HTTP/1.0 200 OK\r\n" + "Content-Type: text/plain\r\n\r\n" + "Submitted data: [%.*s]\n" + "Submitted data length: %d bytes\n" + "input_1: [%s]\n" + "input_2: [%s]\n", + post_data_len, post_data, post_data_len, input1, input2); + } else { + // Show HTML form. + mg_printf(conn, "HTTP/1.0 200 OK\r\n" + "Content-Length: %d\r\n" + "Content-Type: text/html\r\n\r\n%s", + (int) strlen(html_form), html_form); + } + return 1; // Mark request as processed +} + +int main(void) +{ + struct mg_context *ctx; + const char *options[] = {"listening_ports", "8080", NULL}; + struct mg_callbacks callbacks; + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.begin_request = begin_request_handler; + ctx = mg_start(&callbacks, NULL, options); + getchar(); // Wait until user hits "enter" + mg_stop(ctx); + + return 0; +} diff --git a/src/civetweb/examples/_obsolete/upload/Makefile b/src/civetweb/examples/_obsolete/upload/Makefile new file mode 100644 index 000000000..9c6270fb2 --- /dev/null +++ b/src/civetweb/examples/_obsolete/upload/Makefile @@ -0,0 +1,36 @@ +# +# Copyright (c) 2013 No Face Press, LLC +# License http://opensource.org/licenses/mit-license.php MIT License +# + +#This makefile is used to test the other Makefiles + + +PROG = upload +SRC = upload.c + +TOP = ../.. +CIVETWEB_LIB = libcivetweb.a + +CFLAGS = -I$(TOP)/include $(COPT) +LIBS = -lpthread + +include $(TOP)/resources/Makefile.in-os + +ifeq ($(TARGET_OS),LINUX) + LIBS += -ldl +endif + +all: $(PROG) + +$(PROG): $(CIVETWEB_LIB) $(SRC) + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS) + +$(CIVETWEB_LIB): + $(MAKE) -C $(TOP) clean lib + cp $(TOP)/$(CIVETWEB_LIB) . + +clean: + rm -f $(CIVETWEB_LIB) $(PROG) + +.PHONY: all clean diff --git a/src/civetweb/examples/_obsolete/upload/upload.c b/src/civetweb/examples/_obsolete/upload/upload.c new file mode 100644 index 000000000..56c4be4f3 --- /dev/null +++ b/src/civetweb/examples/_obsolete/upload/upload.c @@ -0,0 +1,110 @@ +/* Copyright (c) 2014 the Civetweb developers + * Copyright (c) 2004-2012 Sergey Lyubka + * This file is a part of civetweb project, http://github.com/bel2125/civetweb + */ + +/* This example is deprecated and no longer maintained. + * All relevant parts have been merged into the embedded_c example. */ + + +#ifdef _WIN32 +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS +#endif +#include +#include +#define strtoll strtol +typedef __int64 int64_t; +#else +#include +#include +#endif /* !_WIN32 */ + +#include +#include +#include +#include + +#include "civetweb.h" + + +/* callback: used to generate all content */ +static int begin_request_handler(struct mg_connection *conn) +{ + const char * tempPath = "."; +#ifdef _WIN32 + const char * env = getenv("TEMP"); + if (!env) env = getenv("TMP"); + if (env) tempPath = env; +#else + tempPath = "/tmp"; +#endif + + if (!strcmp(mg_get_request_info(conn)->uri, "/handle_post_request")) { + + mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n"); + mg_upload(conn, tempPath); + } else { + /* Show HTML form. */ + /* See http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1 */ + static const char *html_form = + "Upload example." + "" + /* enctype="multipart/form-data" */ + "
" + "
" + "
" + "" + "
" + "" + ""; + + mg_printf(conn, "HTTP/1.0 200 OK\r\n" + "Content-Length: %d\r\n" + "Content-Type: text/html\r\n\r\n%s", + (int) strlen(html_form), html_form); + } + + /* Mark request as processed */ + return 1; +} + + +/* callback: called after uploading a file is completed */ +static void upload_handler(struct mg_connection *conn, const char *path) +{ + mg_printf(conn, "Saved [%s]", path); +} + + +/* Main program: Set callbacks and start the server. */ +int main(void) +{ + /* Test server will use this port */ + const char * PORT = "8080"; + + /* Startup options for the server */ + struct mg_context *ctx; + const char *options[] = { + "listening_ports", PORT, + NULL}; + struct mg_callbacks callbacks; + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.begin_request = begin_request_handler; + callbacks.upload = upload_handler; + + /* Display a welcome message */ + printf("File upload demo.\n"); + printf("Open http://localhost:%s/ in your browser.\n\n", PORT); + + /* Start the server */ + ctx = mg_start(&callbacks, NULL, options); + + /* Wait until thr user hits "enter", then stop the server */ + getchar(); + mg_stop(ctx); + + return 0; +} diff --git a/src/civetweb/examples/_obsolete/websocket/Makefile b/src/civetweb/examples/_obsolete/websocket/Makefile new file mode 100644 index 000000000..3d6549297 --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket/Makefile @@ -0,0 +1,36 @@ +# +# Copyright (c) 2013 No Face Press, LLC +# License http://opensource.org/licenses/mit-license.php MIT License +# + +#This makefile is used to test the other Makefiles + + +PROG = websocket +SRC = WebSockCallbacks.c websocket.c + +TOP = ../.. +CIVETWEB_LIB = libcivetweb.a + +CFLAGS = -I$(TOP)/include $(COPT) +LIBS = -lpthread + +include $(TOP)/resources/Makefile.in-os + +ifeq ($(TARGET_OS),LINUX) + LIBS += -ldl +endif + +all: $(PROG) + +$(PROG): $(CIVETWEB_LIB) $(SRC) + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS) + +$(CIVETWEB_LIB): + $(MAKE) -C $(TOP) clean lib WITH_WEBSOCKET=1 + cp $(TOP)/$(CIVETWEB_LIB) . + +clean: + rm -f $(CIVETWEB_LIB) $(PROG) + +.PHONY: all clean diff --git a/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.c b/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.c new file mode 100644 index 000000000..bb81fb0cc --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.c @@ -0,0 +1,225 @@ +/* This example uses deprecated interfaces: global websocket callbacks. + They have been superseeded by URI specific callbacks. + See examples/embedded_c for an up to date example. + */ + +#include +#include +#include +#include +#include "WebSockCallbacks.h" + +#ifdef _WIN32 +#include +#define mg_sleep(x) Sleep(x) +#else +#include +#include +#define mg_sleep(x) usleep((x)*1000) +#endif + + +static void +send_to_all_websockets(struct mg_context *ctx, const char *data, int data_len) +{ + + int i; + tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + + mg_lock_context(ctx); + for (i = 0; i < MAX_NUM_OF_WEBSOCKS; i++) { + if (ws_ctx->socketList[i] + && (ws_ctx->socketList[i]->webSockState == 2)) { + mg_websocket_write(ws_ctx->socketList[i]->conn, + WEBSOCKET_OPCODE_TEXT, + data, + data_len); + } + } + mg_unlock_context(ctx); +} + + +void +websocket_ready_handler(struct mg_connection *conn, void *_ignored) +{ + + int i; + const struct mg_request_info *rq = mg_get_request_info(conn); + struct mg_context *ctx = mg_get_context(conn); + tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + tWebSockInfo *wsock = malloc(sizeof(tWebSockInfo)); + assert(wsock); + wsock->webSockState = 0; + mg_set_user_connection_data(conn, wsock); + + mg_lock_context(ctx); + for (i = 0; i < MAX_NUM_OF_WEBSOCKS; i++) { + if (0 == ws_ctx->socketList[i]) { + ws_ctx->socketList[i] = wsock; + wsock->conn = conn; + wsock->webSockState = 1; + break; + } + } + printf("\nNew websocket attached: %s:%u\n", + rq->remote_addr, + rq->remote_port); + mg_unlock_context(ctx); +} + + +static void +websocket_done(tWebSockContext *ws_ctx, tWebSockInfo *wsock) +{ + + int i; + + if (wsock) { + wsock->webSockState = 99; + for (i = 0; i < MAX_NUM_OF_WEBSOCKS; i++) { + if (wsock == ws_ctx->socketList[i]) { + ws_ctx->socketList[i] = 0; + break; + } + } + printf("\nClose websocket attached: %s:%u\n", + mg_get_request_info(wsock->conn)->remote_addr, + mg_get_request_info(wsock->conn)->remote_port); + free(wsock); + } +} + + +int +websocket_data_handler(struct mg_connection *conn, + int flags, + char *data, + size_t data_len, + void *_ignored) +{ + + const struct mg_request_info *rq = mg_get_request_info(conn); + tWebSockInfo *wsock = (tWebSockInfo *)rq->conn_data; + struct mg_context *ctx = mg_get_context(conn); + tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + char msg[128]; + + mg_lock_context(ctx); + if (flags == 136) { + // close websock + websocket_done(ws_ctx, wsock); + mg_set_user_connection_data(conn, NULL); + mg_unlock_context(ctx); + return 1; + } + if (((data_len >= 5) && (data_len < 100) && (flags == 129)) + || (flags == 130)) { + + // init command + if ((wsock->webSockState == 1) && (!memcmp(data, "init ", 5))) { + char *chk; + unsigned long gid; + memcpy(msg, data + 5, data_len - 5); + msg[data_len - 5] = 0; + gid = strtoul(msg, &chk, 10); + wsock->initId = gid; + if (gid > 0 && chk != NULL && *chk == 0) { + wsock->webSockState = 2; + } + mg_unlock_context(ctx); + return 1; + } + + // chat message + if ((wsock->webSockState == 2) && (!memcmp(data, "msg ", 4))) { + send_to_all_websockets(ctx, data, data_len); + mg_unlock_context(ctx); + return 1; + } + } + + // keep alive + if ((data_len == 4) && !memcmp(data, "ping", 4)) { + mg_unlock_context(ctx); + return 1; + } + + mg_unlock_context(ctx); + return 0; +} + + +void +connection_close_handler(const struct mg_connection *conn, void *_ignored) +{ + + const struct mg_request_info *rq = mg_get_request_info(conn); + tWebSockInfo *wsock = (tWebSockInfo *)rq->conn_data; + struct mg_context *ctx = mg_get_context(conn); + tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + + mg_lock_context(ctx); + websocket_done(ws_ctx, wsock); + mg_set_user_connection_data(conn, NULL); + mg_unlock_context(ctx); +} + + +static void * +eventMain(void *arg) +{ + + char msg[256]; + struct mg_context *ctx = (struct mg_context *)arg; + tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + + ws_ctx->runLoop = 1; + while (ws_ctx->runLoop) { + time_t t = time(0); + struct tm *timestr = localtime(&t); + strftime(msg, sizeof(msg), "title %c", timestr); + send_to_all_websockets(ctx, msg, strlen(msg)); + + mg_sleep(1000); + } + + return NULL; +} + + +void +websock_send_broadcast(struct mg_context *ctx, const char *data, int data_len) +{ + + char buffer[260]; + + if (data_len <= 256) { + strcpy(buffer, "msg "); + memcpy(buffer + 4, data, data_len); + + send_to_all_websockets(ctx, buffer, data_len + 4); + } +} + + +void +websock_init_lib(const struct mg_context *ctx) +{ + + tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + memset(ws_ctx, 0, sizeof(*ws_ctx)); + /* todo: use mg_start_thread_id instead of mg_start_thread */ + mg_start_thread(eventMain, (void *)ctx); +} + + +void +websock_exit_lib(const struct mg_context *ctx) +{ + + tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + ws_ctx->runLoop = 0; + /* todo: wait for the thread instead of a timeout */ + mg_sleep(2000); +} diff --git a/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.h b/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.h new file mode 100644 index 000000000..f44821daf --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.h @@ -0,0 +1,44 @@ + +#ifndef WEBSOCKCALLBACKS_H_INCLUDED +#define WEBSOCKCALLBACKS_H_INCLUDED + +#include "civetweb.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct tWebSockInfo { + int webSockState; + unsigned long initId; + struct mg_connection *conn; +} tWebSockInfo; + +#define MAX_NUM_OF_WEBSOCKS (256) +typedef struct tWebSockContext { + int runLoop; + void *thread_id; + tWebSockInfo *socketList[MAX_NUM_OF_WEBSOCKS]; +} tWebSockContext; + + +void websock_init_lib(const struct mg_context *ctx); +void websock_exit_lib(const struct mg_context *ctx); + +void +websock_send_broadcast(struct mg_context *ctx, const char *data, int data_len); + +void websocket_ready_handler(struct mg_connection *conn, void *_ignored); +int websocket_data_handler(struct mg_connection *conn, + int flags, + char *data, + size_t data_len, + void *_ignored); +void connection_close_handler(const struct mg_connection *conn, void *_ignored); + + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/civetweb/examples/_obsolete/websocket/websock.htm b/src/civetweb/examples/_obsolete/websocket/websock.htm new file mode 100644 index 000000000..4ff3a5fae --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket/websock.htm @@ -0,0 +1,55 @@ + + + + Test + + + + + + + + + + + \ No newline at end of file diff --git a/src/civetweb/examples/_obsolete/websocket/websocket.c b/src/civetweb/examples/_obsolete/websocket/websocket.c new file mode 100644 index 000000000..3aadf98b8 --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket/websocket.c @@ -0,0 +1,65 @@ +/* This example uses deprecated interfaces: global websocket callbacks. + They have been superseeded by URI specific callbacks. + See examples/embedded_c for an up to date example. + */ + +#include +#include +#include + +#include "civetweb.h" +#include "WebSockCallbacks.h" + +int +main(void) +{ + struct mg_context *ctx = 0; + struct mg_callbacks callback_funcs = {0}; + tWebSockContext ws_ctx; + char inbuf[4]; + + const char *server_options[] = { + /* document_root: The path to the test function websock.htm */ + "document_root", + "../../examples/websocket", + + /* port: use http standard to match websocket url in websock.htm: + ws://127.0.0.1/MyWebSock */ + /* if the port is changed here, it needs to be changed in + websock.htm as well */ + "listening_ports", + "80", + + NULL}; + + callback_funcs.init_context = websock_init_lib; + callback_funcs.exit_context = websock_exit_lib; + + ctx = mg_start(&callback_funcs, &ws_ctx, server_options); + + mg_set_websocket_handler(ctx, + "/MyWebSock", + NULL, + websocket_ready_handler, + websocket_data_handler, + connection_close_handler, + NULL); + + printf("Connect to localhost:%s/websock.htm\n", + mg_get_option(ctx, "listening_ports")); + + puts("Enter an (ASCII) character or * to exit:"); + for (;;) { + fgets(inbuf, sizeof(inbuf), stdin); + + if (inbuf[0] == '*') { + break; + } + inbuf[0] = toupper(inbuf[0]); + websock_send_broadcast(ctx, inbuf, 1); + } + + mg_stop(ctx); + + return 0; +} diff --git a/src/civetweb/examples/_obsolete/websocket_client/Makefile b/src/civetweb/examples/_obsolete/websocket_client/Makefile new file mode 100644 index 000000000..e3ef13468 --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket_client/Makefile @@ -0,0 +1,37 @@ +# +# Copyright (c) 2014 Jordan Shelley +# https://github.com/jshelley +# License http://opensource.org/licenses/mit-license.php MIT License +# + +#This makefile is used to test the other Makefiles + + +PROG = websocket_client +SRC = websocket_client.c + +TOP = ../.. +CIVETWEB_LIB = libcivetweb.a + +CFLAGS = -I$(TOP)/include $(COPT) +LIBS = -lpthread + +include $(TOP)/resources/Makefile.in-os + +ifeq ($(TARGET_OS),LINUX) + LIBS += -ldl +endif + +all: $(PROG) + +$(PROG): $(CIVETWEB_LIB) $(SRC) + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS) + +$(CIVETWEB_LIB): + $(MAKE) -C $(TOP) clean lib WITH_WEBSOCKET=1 + cp $(TOP)/$(CIVETWEB_LIB) . + +clean: + rm -f $(CIVETWEB_LIB) $(PROG) + +.PHONY: all clean diff --git a/src/civetweb/examples/_obsolete/websocket_client/ssl/server.crt b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.crt new file mode 100644 index 000000000..a26359abf --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.crt @@ -0,0 +1,13 @@ +-----BEGIN CERTIFICATE----- +MIICATCCAWoCCQC2BCIqIvgSUTANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB +VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0 +cyBQdHkgTHRkMB4XDTE0MDgyMTEyMzAwMVoXDTI0MDgxODEyMzAwMVowRTELMAkG +A1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0 +IFdpZGdpdHMgUHR5IEx0ZDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA9k9s +gH22BCo9neTeB/YnilK7n0sMe0+pjS9KSWhU59Q4w8hqPrW0tuYikIDd0wVggkJF +BZNg4WPoulTdwXsgNBeG88q2wnNtUosXTS+KQTQBSiQof9Ay9GHQtgxnogI1zIXb +HOppyyG5zre8a/X6fzDOnFc4iJMBwxTAnjCqObkCAwEAATANBgkqhkiG9w0BAQUF +AAOBgQBX9V46VUVsB9P9fb8sFuMx2ezFE42ynEeJPrKRrof+dFYbjvR1OUZFSLCy +aZKwVH7iCnVBJiU12JxO7PR3L6ob3FYPyNHQWYq1/IFUvqBRagehldj5H8iFeEDz +Wtz2+p1rUyVxcSUqTjobaji0aC8lzPZio0nd1KKM6A92/adHyQ== +-----END CERTIFICATE----- diff --git a/src/civetweb/examples/_obsolete/websocket_client/ssl/server.csr b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.csr new file mode 100644 index 000000000..4d4723b24 --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.csr @@ -0,0 +1,11 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIIBhDCB7gIBADBFMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEh +MB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEB +AQUAA4GNADCBiQKBgQD2T2yAfbYEKj2d5N4H9ieKUrufSwx7T6mNL0pJaFTn1DjD +yGo+tbS25iKQgN3TBWCCQkUFk2DhY+i6VN3BeyA0F4bzyrbCc21SixdNL4pBNAFK +JCh/0DL0YdC2DGeiAjXMhdsc6mnLIbnOt7xr9fp/MM6cVziIkwHDFMCeMKo5uQID +AQABoAAwDQYJKoZIhvcNAQEFBQADgYEA1EOFwyFJ2NAnRNktZCy5yVcLx9C78HoC +oHPPCOElu0VDIqe6ZecYdaqWbYlhGE0+isbOQn2CwHOeBGN8mIDsNUYzVEpsEfgg +9OK873LpE5pf4mdjSiRBXkk/h8BxuqkcKi+Qx+qEE7+dH2nK5aKeIHVvbLyfGOch +9I85q+msBNE= +-----END CERTIFICATE REQUEST----- diff --git a/src/civetweb/examples/_obsolete/websocket_client/ssl/server.key b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.key new file mode 100644 index 000000000..f09cc38cb --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.key @@ -0,0 +1,15 @@ +-----BEGIN RSA PRIVATE KEY----- +MIICXQIBAAKBgQD2T2yAfbYEKj2d5N4H9ieKUrufSwx7T6mNL0pJaFTn1DjDyGo+ +tbS25iKQgN3TBWCCQkUFk2DhY+i6VN3BeyA0F4bzyrbCc21SixdNL4pBNAFKJCh/ +0DL0YdC2DGeiAjXMhdsc6mnLIbnOt7xr9fp/MM6cVziIkwHDFMCeMKo5uQIDAQAB +AoGAYwospr3lomcZv5N3c9wWqhf6OWMD8dFma87IIBxDh7Rd3tuHXQ/TSnffDhvD +FkbjN31OI5/PJNH3knTtdg78MywPloE4jYsbt4+fEaW7Fzww2nU61N1p+mYk5d/b +SCTAHhGzF9g9ZMw25CCUFGjDU2z+Ty6my22Euxhk2Qq8tMECQQD9ZYIxWkPhywDW +pX3v70dqIv7411hEYpuL/ZJl26UCmQsj4HPtXQCraQksVPs74WY5aTtd6MAV9V3M +UnErHO5/AkEA+NdG2MmfBOBPusDB/WwQaUPiCWGITS9llGTR2JXbvDqmKgL1+UTG +o27sLNIFCrF1wejpyRGqwjcObFYR0yKrxwJBAOB2uPuK4DL1psp9Uq/mIDbOxVod +OF1rlCpP9w0vol5Iv+uJ+mc7SUqOAsg4h0yl/+2/YA1yDiXlcq96IDF2sXUCQGAv +Nh9Nr72+xpK1N0axopZNuu1NWdYb3/PAFKzXIBxdvyS2CEXVo8JAeeHJPFGpzo6p +bNRfk9WGWnjdu/4UhLkCQQCekR9zpIpzdJiPYCd6XMya+TPCDYlOQL1jlnJIRa2V +BEOz0rSpzXAGh0PyCB/kMneyVk87LWn8joE6179RoUfv +-----END RSA PRIVATE KEY----- diff --git a/src/civetweb/examples/_obsolete/websocket_client/ssl/server.key.orig b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.key.orig new file mode 100644 index 000000000..58e5653ca --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.key.orig @@ -0,0 +1,18 @@ +-----BEGIN RSA PRIVATE KEY----- +Proc-Type: 4,ENCRYPTED +DEK-Info: DES-EDE3-CBC,89778A6427F05D4A + +4aXqO/8oCHVfMLB+a1DfjbXyEddjbd7nB+YVFLPKy68Tam9PRTvC1zRHBet59ll0 +1w7R8tXR6/xH7HRhBeqDHCcuvBhtw3xGxtXWv54WBFhzuq7TvKOAaCFl++cw/JHq +PCS0rAaYnqF2MAgMi7QBjZKmHFHL43Gy60VfOrB0mmOdxqqXA0NBFC2uEd7Z/MAx +S2A85bNJJKQaWEeDThP1u0OOlNCq99lkLJ31jiOH7ntdL0/vqcbZ+PUtdPLwAG4L +1GUHuiC2v5FvDlPiejMk2dvrxCNpcu2e3tQKHpg2KcsTVrpB7EVzRSazln4HywUZ +EJfBvxqqrS7plImZgj4LXSnln0JPuBb+aHnhKIFvisjYSwqDGJnnp/OaD7YdRhYh +UCcL011Ge+yUbRipeAmHdtJlSUSdB14KWq+WdIX/KgCRGx06QZm9s1PBLH+fww+I +EL3A/LFX0a5KUHkCp29akYYv9bUYaQ79Nt7BlaEON+/SW3pJMbGr+nx8aqogr0Yo +SJ/Zz5TSDBhecRjbCDGkT6DizVZ8cbm2xl8QLBd0H+ZA6uYMgfpAOJGrJx3Nm4Lv +prEApgFtjSrsQDGYHAcmDMW1UWOVHuNp7BSvwUze9Ftnzr/jlpdzES2rhgMyGhg1 +0Szbsfs3vgw4iM83LFJXza07GQJzF8gRF79dY5JiQX/sOKUprA6Lofk631jE0G8r +3z59cxblaq9y7EgFsE944Gk7/HIEimBRiqIZzGVJVukD0itynQ+XmYTdbyH1lpvi +c0ZheZPUoGwUW9RYy+nle5gEDFyZWXcCAuJasQvDBXt//r/bso3ZpA== +-----END RSA PRIVATE KEY----- diff --git a/src/civetweb/examples/_obsolete/websocket_client/ssl/server.pem b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.pem new file mode 100644 index 000000000..300834e39 --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket_client/ssl/server.pem @@ -0,0 +1,28 @@ +-----BEGIN CERTIFICATE----- +MIICATCCAWoCCQC2BCIqIvgSUTANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB +VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0 +cyBQdHkgTHRkMB4XDTE0MDgyMTEyMzAwMVoXDTI0MDgxODEyMzAwMVowRTELMAkG +A1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0 +IFdpZGdpdHMgUHR5IEx0ZDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA9k9s +gH22BCo9neTeB/YnilK7n0sMe0+pjS9KSWhU59Q4w8hqPrW0tuYikIDd0wVggkJF +BZNg4WPoulTdwXsgNBeG88q2wnNtUosXTS+KQTQBSiQof9Ay9GHQtgxnogI1zIXb +HOppyyG5zre8a/X6fzDOnFc4iJMBwxTAnjCqObkCAwEAATANBgkqhkiG9w0BAQUF +AAOBgQBX9V46VUVsB9P9fb8sFuMx2ezFE42ynEeJPrKRrof+dFYbjvR1OUZFSLCy +aZKwVH7iCnVBJiU12JxO7PR3L6ob3FYPyNHQWYq1/IFUvqBRagehldj5H8iFeEDz +Wtz2+p1rUyVxcSUqTjobaji0aC8lzPZio0nd1KKM6A92/adHyQ== +-----END CERTIFICATE----- +-----BEGIN RSA PRIVATE KEY----- +MIICXQIBAAKBgQD2T2yAfbYEKj2d5N4H9ieKUrufSwx7T6mNL0pJaFTn1DjDyGo+ +tbS25iKQgN3TBWCCQkUFk2DhY+i6VN3BeyA0F4bzyrbCc21SixdNL4pBNAFKJCh/ +0DL0YdC2DGeiAjXMhdsc6mnLIbnOt7xr9fp/MM6cVziIkwHDFMCeMKo5uQIDAQAB +AoGAYwospr3lomcZv5N3c9wWqhf6OWMD8dFma87IIBxDh7Rd3tuHXQ/TSnffDhvD +FkbjN31OI5/PJNH3knTtdg78MywPloE4jYsbt4+fEaW7Fzww2nU61N1p+mYk5d/b +SCTAHhGzF9g9ZMw25CCUFGjDU2z+Ty6my22Euxhk2Qq8tMECQQD9ZYIxWkPhywDW +pX3v70dqIv7411hEYpuL/ZJl26UCmQsj4HPtXQCraQksVPs74WY5aTtd6MAV9V3M +UnErHO5/AkEA+NdG2MmfBOBPusDB/WwQaUPiCWGITS9llGTR2JXbvDqmKgL1+UTG +o27sLNIFCrF1wejpyRGqwjcObFYR0yKrxwJBAOB2uPuK4DL1psp9Uq/mIDbOxVod +OF1rlCpP9w0vol5Iv+uJ+mc7SUqOAsg4h0yl/+2/YA1yDiXlcq96IDF2sXUCQGAv +Nh9Nr72+xpK1N0axopZNuu1NWdYb3/PAFKzXIBxdvyS2CEXVo8JAeeHJPFGpzo6p +bNRfk9WGWnjdu/4UhLkCQQCekR9zpIpzdJiPYCd6XMya+TPCDYlOQL1jlnJIRa2V +BEOz0rSpzXAGh0PyCB/kMneyVk87LWn8joE6179RoUfv +-----END RSA PRIVATE KEY----- diff --git a/src/civetweb/examples/_obsolete/websocket_client/websocket_client.c b/src/civetweb/examples/_obsolete/websocket_client/websocket_client.c new file mode 100644 index 000000000..158ad7d85 --- /dev/null +++ b/src/civetweb/examples/_obsolete/websocket_client/websocket_client.c @@ -0,0 +1,421 @@ +/* +* Copyright (c) 2014-2016 the Civetweb developers +* Copyright (c) 2014 Jordan Shelley +* https://github.com/jshelley +* License http://opensource.org/licenses/mit-license.php MIT License +*/ + +/* This example is superseeded by other examples, and no longer + * actively maintained. + * See examples/embedded_c for an up to date example. + */ + +// Simple example program on how to use websocket client embedded C interface. +#ifdef _WIN32 +#include +#define sleep(x) Sleep(1000 * (x)) +#else +#include +#endif + +#include +#include +#include "civetweb.h" + +#define DOCUMENT_ROOT "." +#define PORT "8888" +#define SSL_CERT "./ssl/server.pem" + +const char *websocket_welcome_msg = "websocket welcome\n"; +const size_t websocket_welcome_msg_len = 18 /* strlen(websocket_welcome_msg) */; +const char *websocket_acknowledge_msg = "websocket msg ok\n"; +const size_t websocket_acknowledge_msg_len = + 17 /* strlen(websocket_acknowledge_msg) */; +const char *websocket_goodbye_msg = "websocket bye\n"; +const size_t websocket_goodbye_msg_len = 14 /* strlen(websocket_goodbye_msg) */; + + +/*************************************************************************************/ +/* WEBSOCKET SERVER */ +/*************************************************************************************/ +#if defined(MG_LEGACY_INTERFACE) +int +websock_server_connect(const struct mg_connection *conn) +#else +int +websocket_server_connect(const struct mg_connection *conn, void *_ignored) +#endif +{ + printf("Server: Websocket connected\n"); + return 0; /* return 0 to accept every connection */ +} + + +#if defined(MG_LEGACY_INTERFACE) +void +websocket_server_ready(struct mg_connection *conn) +#else +void +websocket_server_ready(struct mg_connection *conn, void *_ignored) +#endif +{ + printf("Server: Websocket ready\n"); + + /* Send websocket welcome message */ + mg_lock_connection(conn); + mg_websocket_write(conn, + WEBSOCKET_OPCODE_TEXT, + websocket_welcome_msg, + websocket_welcome_msg_len); + mg_unlock_connection(conn); +} + + +#if defined(MG_LEGACY_INTERFACE) +int +websocket_server_data(struct mg_connection *conn, + int bits, + char *data, + size_t data_len) +#else +int +websocket_server_data(struct mg_connection *conn, + int bits, + char *data, + size_t data_len, + void *_ignored) +#endif +{ + printf("Server: Got %lu bytes from the client\n", (unsigned long)data_len); + printf("Server received data from client: "); + fwrite(data, 1, data_len, stdout); + printf("\n"); + + if (data_len < 3 || 0 != memcmp(data, "bye", 3)) { + /* Send websocket acknowledge message */ + mg_lock_connection(conn); + mg_websocket_write(conn, + WEBSOCKET_OPCODE_TEXT, + websocket_acknowledge_msg, + websocket_acknowledge_msg_len); + mg_unlock_connection(conn); + } else { + /* Send websocket acknowledge message */ + mg_lock_connection(conn); + mg_websocket_write(conn, + WEBSOCKET_OPCODE_TEXT, + websocket_goodbye_msg, + websocket_goodbye_msg_len); + mg_unlock_connection(conn); + } + + return 1; /* return 1 to keep the connetion open */ +} + + +#if defined(MG_LEGACY_INTERFACE) +void +websocket_server_connection_close(const struct mg_connection *conn) +#else +void +websocket_server_connection_close(const struct mg_connection *conn, + void *_ignored) +#endif +{ + printf("Server: Close connection\n"); + + /* Can not send a websocket goodbye message here - the connection is already + * closed */ +} + + +struct mg_context * +start_websocket_server() +{ + const char *options[] = {"document_root", + DOCUMENT_ROOT, + "ssl_certificate", + SSL_CERT, + "listening_ports", + PORT, + "request_timeout_ms", + "5000", + 0}; + struct mg_callbacks callbacks; + struct mg_context *ctx; + + memset(&callbacks, 0, sizeof(callbacks)); + +#if defined(MG_LEGACY_INTERFACE) + /* Obsolete: */ + callbacks.websocket_connect = websock_server_connect; + callbacks.websocket_ready = websocket_server_ready; + callbacks.websocket_data = websocket_server_data; + callbacks.connection_close = websocket_server_connection_close; + + ctx = mg_start(&callbacks, 0, options); +#else + /* New interface: */ + ctx = mg_start(&callbacks, 0, options); + + mg_set_websocket_handler(ctx, + "/websocket", + websocket_server_connect, + websocket_server_ready, + websocket_server_data, + websocket_server_connection_close, + NULL); +#endif + + return ctx; +} + + +/*************************************************************************************/ +/* WEBSOCKET CLIENT */ +/*************************************************************************************/ +struct tclient_data { + void *data; + size_t len; + int closed; +}; + +static int +websocket_client_data_handler(struct mg_connection *conn, + int flags, + char *data, + size_t data_len, + void *user_data) +{ + struct mg_context *ctx = mg_get_context(conn); + struct tclient_data *pclient_data = + (struct tclient_data *)mg_get_user_data(ctx); + + printf("Client received data from server: "); + fwrite(data, 1, data_len, stdout); + printf("\n"); + + pclient_data->data = malloc(data_len); + assert(pclient_data->data != NULL); + memcpy(pclient_data->data, data, data_len); + pclient_data->len = data_len; + + return 1; +} + +static void +websocket_client_close_handler(const struct mg_connection *conn, + void *user_data) +{ + struct mg_context *ctx = mg_get_context(conn); + struct tclient_data *pclient_data = + (struct tclient_data *)mg_get_user_data(ctx); + + printf("Client: Close handler\n"); + pclient_data->closed++; +} + + +int +main(int argc, char *argv[]) +{ + struct mg_context *ctx = NULL; + struct tclient_data client1_data = {NULL, 0, 0}; + struct tclient_data client2_data = {NULL, 0, 0}; + struct tclient_data client3_data = {NULL, 0, 0}; + struct mg_connection *newconn1 = NULL; + struct mg_connection *newconn2 = NULL; + struct mg_connection *newconn3 = NULL; + char ebuf[100] = {0}; + + assert(websocket_welcome_msg_len == strlen(websocket_welcome_msg)); + + /* First set up a websocket server */ + ctx = start_websocket_server(); + assert(ctx != NULL); + printf("Server init\n\n"); + + /* Then connect a first client */ + newconn1 = mg_connect_websocket_client("localhost", + atoi(PORT), + 0, + ebuf, + sizeof(ebuf), + "/websocket", + NULL, + websocket_client_data_handler, + websocket_client_close_handler, + &client1_data); + + if (newconn1 == NULL) { + printf("Error: %s", ebuf); + return 1; + } + + sleep(1); /* Should get the websocket welcome message */ + assert(client1_data.closed == 0); + assert(client2_data.closed == 0); + assert(client2_data.data == NULL); + assert(client2_data.len == 0); + assert(client1_data.data != NULL); + assert(client1_data.len == websocket_welcome_msg_len); + assert(!memcmp(client1_data.data, + websocket_welcome_msg, + websocket_welcome_msg_len)); + free(client1_data.data); + client1_data.data = NULL; + client1_data.len = 0; + + mg_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "data1", 5); + + sleep(1); /* Should get the acknowledge message */ + assert(client1_data.closed == 0); + assert(client2_data.closed == 0); + assert(client2_data.data == NULL); + assert(client2_data.len == 0); + assert(client1_data.data != NULL); + assert(client1_data.len == websocket_acknowledge_msg_len); + assert(!memcmp(client1_data.data, + websocket_acknowledge_msg, + websocket_acknowledge_msg_len)); + free(client1_data.data); + client1_data.data = NULL; + client1_data.len = 0; + + /* Now connect a second client */ + newconn2 = mg_connect_websocket_client("localhost", + atoi(PORT), + 0, + ebuf, + sizeof(ebuf), + "/websocket", + NULL, + websocket_client_data_handler, + websocket_client_close_handler, + &client2_data); + + if (newconn2 == NULL) { + printf("Error: %s", ebuf); + return 1; + } + + sleep(1); /* Client 2 should get the websocket welcome message */ + assert(client1_data.closed == 0); + assert(client2_data.closed == 0); + assert(client1_data.data == NULL); + assert(client1_data.len == 0); + assert(client2_data.data != NULL); + assert(client2_data.len == websocket_welcome_msg_len); + assert(!memcmp(client2_data.data, + websocket_welcome_msg, + websocket_welcome_msg_len)); + free(client2_data.data); + client2_data.data = NULL; + client2_data.len = 0; + + mg_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "data2", 5); + + sleep(1); /* Should get the acknowledge message */ + assert(client1_data.closed == 0); + assert(client2_data.closed == 0); + assert(client2_data.data == NULL); + assert(client2_data.len == 0); + assert(client1_data.data != NULL); + assert(client1_data.len == websocket_acknowledge_msg_len); + assert(!memcmp(client1_data.data, + websocket_acknowledge_msg, + websocket_acknowledge_msg_len)); + free(client1_data.data); + client1_data.data = NULL; + client1_data.len = 0; + + mg_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "bye", 3); + + sleep(1); /* Should get the goodbye message */ + assert(client1_data.closed == 0); + assert(client2_data.closed == 0); + assert(client2_data.data == NULL); + assert(client2_data.len == 0); + assert(client1_data.data != NULL); + assert(client1_data.len == websocket_goodbye_msg_len); + assert(!memcmp(client1_data.data, + websocket_goodbye_msg, + websocket_goodbye_msg_len)); + free(client1_data.data); + client1_data.data = NULL; + client1_data.len = 0; + + mg_close_connection(newconn1); + + sleep(1); /* Won't get any message */ + assert(client1_data.closed == 1); + assert(client2_data.closed == 0); + assert(client1_data.data == NULL); + assert(client1_data.len == 0); + assert(client2_data.data == NULL); + assert(client2_data.len == 0); + + mg_websocket_client_write(newconn2, WEBSOCKET_OPCODE_TEXT, "bye", 3); + + sleep(1); /* Should get the goodbye message */ + assert(client1_data.closed == 1); + assert(client2_data.closed == 0); + assert(client1_data.data == NULL); + assert(client1_data.len == 0); + assert(client2_data.data != NULL); + assert(client2_data.len == websocket_goodbye_msg_len); + assert(!memcmp(client2_data.data, + websocket_goodbye_msg, + websocket_goodbye_msg_len)); + free(client2_data.data); + client2_data.data = NULL; + client2_data.len = 0; + + mg_close_connection(newconn2); + + sleep(1); /* Won't get any message */ + assert(client1_data.closed == 1); + assert(client2_data.closed == 1); + assert(client1_data.data == NULL); + assert(client1_data.len == 0); + assert(client2_data.data == NULL); + assert(client2_data.len == 0); + + /* Connect client 3 */ + newconn3 = mg_connect_websocket_client("localhost", + atoi(PORT), + 0, + ebuf, + sizeof(ebuf), + "/websocket", + NULL, + websocket_client_data_handler, + websocket_client_close_handler, + &client3_data); + + sleep(1); /* Client 3 should get the websocket welcome message */ + assert(client1_data.closed == 1); + assert(client2_data.closed == 1); + assert(client3_data.closed == 0); + assert(client1_data.data == NULL); + assert(client1_data.len == 0); + assert(client2_data.data == NULL); + assert(client2_data.len == 0); + assert(client3_data.data != NULL); + assert(client3_data.len == websocket_welcome_msg_len); + assert(!memcmp(client3_data.data, + websocket_welcome_msg, + websocket_welcome_msg_len)); + free(client3_data.data); + client3_data.data = NULL; + client3_data.len = 0; + + mg_stop(ctx); + printf("Server shutdown\n"); + + sleep(10); + + assert(client3_data.closed == 1); + + return 0; +} diff --git a/src/civetweb/examples/_obsolete/ws_server/Makefile b/src/civetweb/examples/_obsolete/ws_server/Makefile new file mode 100644 index 000000000..60d9dbedb --- /dev/null +++ b/src/civetweb/examples/_obsolete/ws_server/Makefile @@ -0,0 +1,36 @@ +# +# Copyright (c) 2013 No Face Press, LLC +# License http://opensource.org/licenses/mit-license.php MIT License +# + +#This makefile is used to test the other Makefiles + + +PROG = ws_server +SRC = ws_server.c + +TOP = ../.. +CIVETWEB_LIB = libcivetweb.a + +CFLAGS = -I$(TOP)/include $(COPT) +LIBS = -lpthread + +include $(TOP)/resources/Makefile.in-os + +ifeq ($(TARGET_OS),LINUX) + LIBS += -ldl -DMG_LEGACY_INTERFACE=1 +endif + +all: $(PROG) + +$(PROG): $(CIVETWEB_LIB) $(SRC) + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS) + +$(CIVETWEB_LIB): + $(MAKE) -C $(TOP) clean lib WITH_WEBSOCKET=1 + cp $(TOP)/$(CIVETWEB_LIB) . + +clean: + rm -f $(CIVETWEB_LIB) $(PROG) + +.PHONY: all clean diff --git a/src/civetweb/examples/_obsolete/ws_server/docroot/index.html b/src/civetweb/examples/_obsolete/ws_server/docroot/index.html new file mode 100644 index 000000000..f1d1af3eb --- /dev/null +++ b/src/civetweb/examples/_obsolete/ws_server/docroot/index.html @@ -0,0 +1,316 @@ + + + + + + + Websocket Meters + + + + + +
+ +

Meter Updates via WebSocket

+ +

+ +

+ +
+ +

+ +

+ +
+ +

+ +

+ +
+ +

+ +

+
+
+ + +
+
+

+

+ + + + + + diff --git a/src/civetweb/examples/_obsolete/ws_server/ws_server.c b/src/civetweb/examples/_obsolete/ws_server/ws_server.c new file mode 100644 index 000000000..575a26a69 --- /dev/null +++ b/src/civetweb/examples/_obsolete/ws_server/ws_server.c @@ -0,0 +1,271 @@ +// Copyright (c) 2004-2012 Sergey Lyubka +// This file is a part of civetweb project, http://github.com/bel2125/civetweb +// +// v 0.1 Contributed by William Greathouse 9-Sep-2013 + +#include +#include +#include +#include + +#include "civetweb.h" + +// simple structure for keeping track of websocket connection +struct ws_connection { + struct mg_connection *conn; + int update; + int closing; +}; + +// time base and structure periodic updates to client for demo +#define BASETIME 100000 /* 0.1 seconds */ +struct progress { + int limit; + int increment; + int period; + int value; +}; + +// up to 16 independent client connections +#define CONNECTIONS 16 +static struct ws_connection ws_conn[CONNECTIONS]; + + +// ws_server_thread() +// Simple demo server thread. Sends periodic updates to connected clients +static void *ws_server_thread(void *parm) +{ + int wsd = (long)parm; + struct mg_connection *conn = ws_conn[wsd].conn; + int timer = 0; + char tstr[32]; + int i; + struct progress meter[] = { + /* first meter 0 to 1000, by 5 every 0.1 second */ + { 1000, 5, 1, 0 }, + /* second meter 0 to 500, by 10 every 0.5 second */ + { 500, 10, 5, 0 }, + /* third meter 0 to 100, by 10 every 1.0 second */ + { 100, 10, 10, 0}, + /* end of list */ + { 0, 0, 0, 0} + }; + + fprintf(stderr, "ws_server_thread %d\n", wsd); + + /* Send initial meter updates */ + for (i=0; meter[i].period != 0; i++) { + if (meter[i].value >= meter[i].limit) + meter[i].value = 0; + if (meter[i].value >= meter[i].limit) + meter[i].value = meter[i].limit; + sprintf(tstr, "meter%d:%d,%d", i+1, + meter[i].value, meter[i].limit); + mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, tstr, strlen(tstr)); + } + + /* While the connection is open, send periodic updates */ + while(!ws_conn[wsd].closing) { + usleep(100000); /* 0.1 second */ + timer++; + + /* Send meter updates */ + if (ws_conn[wsd].update) { + for (i=0; meter[i].period != 0; i++) { + if (timer%meter[i].period == 0) { + if (meter[i].value >= meter[i].limit) + meter[i].value = 0; + else + meter[i].value += meter[i].increment; + if (meter[i].value >= meter[i].limit) + meter[i].value = meter[i].limit; + // if we are closing, server should not send new data + if (!ws_conn[wsd].closing) { + sprintf(tstr, "meter%d:%d,%d", i+1, + meter[i].value, meter[i].limit); + mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, tstr, strlen(tstr)); + } + } + } + } + + /* Send periodic PING to assure websocket remains connected, except if we are closing */ + if (timer%100 == 0 && !ws_conn[wsd].closing) + mg_websocket_write(conn, WEBSOCKET_OPCODE_PING, NULL, 0); + } + + fprintf(stderr, "ws_server_thread %d exiting\n", wsd); + + // reset connection information to allow reuse by new client + ws_conn[wsd].conn = NULL; + ws_conn[wsd].update = 0; + ws_conn[wsd].closing = 2; + + return NULL; +} + +// websocket_connect_handler() +// On new client connection, find next available server connection and store +// new connection information. If no more server connections are available +// tell civetweb to not accept the client request. +static int websocket_connect_handler(const struct mg_connection *conn) +{ + int i; + + fprintf(stderr, "connect handler\n"); + + for(i=0; i < CONNECTIONS; ++i) { + if (ws_conn[i].conn == NULL) { + fprintf(stderr, "...prep for server %d\n", i); + ws_conn[i].conn = (struct mg_connection *)conn; + ws_conn[i].closing = 0; + ws_conn[i].update = 0; + break; + } + } + if (i >= CONNECTIONS) { + fprintf(stderr, "Refused connection: Max connections exceeded\n"); + return 1; + } + + return 0; +} + +// websocket_ready_handler() +// Once websocket negotiation is complete, start a server for the connection +static void websocket_ready_handler(struct mg_connection *conn) +{ + int i; + + fprintf(stderr, "ready handler\n"); + + for(i=0; i < CONNECTIONS; ++i) { + if (ws_conn[i].conn == conn) { + fprintf(stderr, "...start server %d\n", i); + mg_start_thread(ws_server_thread, (void *)(long)i); + break; + } + } +} + +// websocket_close_handler() +// When websocket is closed, tell the associated server to shut down +static void websocket_close_handler(struct mg_connection *conn) +{ + int i; + + //fprintf(stderr, "close handler\n"); /* called for every close, not just websockets */ + + for(i=0; i < CONNECTIONS; ++i) { + if (ws_conn[i].conn == conn) { + fprintf(stderr, "...close server %d\n", i); + ws_conn[i].closing = 1; + } + } +} + +// Arguments: +// flags: first byte of websocket frame, see websocket RFC, +// http://tools.ietf.org/html/rfc6455, section 5.2 +// data, data_len: payload data. Mask, if any, is already applied. +static int websocket_data_handler(struct mg_connection *conn, int flags, + char *data, size_t data_len) +{ + int i; + int wsd; + + for(i=0; i < CONNECTIONS; ++i) { + if (ws_conn[i].conn == conn) { + wsd = i; + break; + } + } + if (i >= CONNECTIONS) { + fprintf(stderr, "Received websocket data from unknown connection\n"); + return 1; + } + + if (flags & 0x80) { + flags &= 0x7f; + switch (flags) { + case WEBSOCKET_OPCODE_CONTINUATION: + fprintf(stderr, "CONTINUATION...\n"); + break; + case WEBSOCKET_OPCODE_TEXT: + fprintf(stderr, "TEXT: %-.*s\n", (int)data_len, data); + /*** interpret data as commands here ***/ + if (strncmp("update on", data, data_len)== 0) { + /* turn on updates */ + ws_conn[wsd].update = 1; + /* echo back */ + mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len); + } else if (strncmp("update off", data, data_len)== 0) { + /* turn off updates */ + ws_conn[wsd].update = 0; + /* echo back */ + mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len); + } + break; + case WEBSOCKET_OPCODE_BINARY: + fprintf(stderr, "BINARY...\n"); + break; + case WEBSOCKET_OPCODE_CONNECTION_CLOSE: + fprintf(stderr, "CLOSE...\n"); + /* If client initiated close, respond with close message in acknowlegement */ + if (!ws_conn[wsd].closing) { + mg_websocket_write(conn, WEBSOCKET_OPCODE_CONNECTION_CLOSE, data, data_len); + ws_conn[wsd].closing = 1; /* we should not send addional messages when close requested/acknowledged */ + } + return 0; /* time to close the connection */ + break; + case WEBSOCKET_OPCODE_PING: + /* client sent PING, respond with PONG */ + mg_websocket_write(conn, WEBSOCKET_OPCODE_PONG, data, data_len); + break; + case WEBSOCKET_OPCODE_PONG: + /* received PONG to our PING, no action */ + break; + default: + fprintf(stderr, "Unknown flags: %02x\n", flags); + break; + } + } + + return 1; /* keep connection open */ +} + + +int main(void) +{ + char server_name[40]; + struct mg_context *ctx; + struct mg_callbacks callbacks; + const char *options[] = { + "listening_ports", "8080", + "document_root", "docroot", + NULL + }; + + /* get simple greeting for the web server */ + snprintf(server_name, sizeof(server_name), + "Civetweb websocket server v. %s", + mg_version()); + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.websocket_connect = websocket_connect_handler; + callbacks.websocket_ready = websocket_ready_handler; + callbacks.websocket_data = websocket_data_handler; + callbacks.connection_close = websocket_close_handler; + + ctx = mg_start(&callbacks, NULL, options); + + /* show the greeting and some basic information */ + printf("%s started on port(s) %s with web root [%s]\n", + server_name, mg_get_option(ctx, "listening_ports"), + mg_get_option(ctx, "document_root")); + + getchar(); // Wait until user hits "enter" + mg_stop(ctx); + + return 0; +} diff --git a/src/civetweb/examples/embedded_c/Makefile b/src/civetweb/examples/embedded_c/Makefile new file mode 100644 index 000000000..93d6379db --- /dev/null +++ b/src/civetweb/examples/embedded_c/Makefile @@ -0,0 +1,37 @@ +# +# Copyright (c) 2013 No Face Press, LLC +# License http://opensource.org/licenses/mit-license.php MIT License +# + +#This makefile is used to test the other Makefiles + + +PROG = embedded_c +SRC = embedded_c.c + +TOP = ../.. +CIVETWEB_LIB = libcivetweb.a + +CFLAGS = -I$(TOP)/include $(COPT) -DUSE_WEBSOCKET -DUSE_IPV6 +LIBS = -lpthread + +include $(TOP)/resources/Makefile.in-os + +ifeq ($(TARGET_OS),LINUX) + LIBS += -ldl +endif + +all: $(PROG) + +$(PROG): $(CIVETWEB_LIB) $(SRC) + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS) -lcrypto -lssl -DUSE_SSL_DH=1 + +$(CIVETWEB_LIB): + $(MAKE) -C $(TOP) WITH_IPV6=1 WITH_WEBSOCKET=1 COPT='-DNO_SSL_DL=1' clean lib + cp $(TOP)/$(CIVETWEB_LIB) . + +clean: + rm -f $(CIVETWEB_LIB) $(PROG) + +.PHONY: all clean + diff --git a/src/civetweb/examples/embedded_c/embedded_c.c b/src/civetweb/examples/embedded_c/embedded_c.c new file mode 100644 index 000000000..34a361e85 --- /dev/null +++ b/src/civetweb/examples/embedded_c/embedded_c.c @@ -0,0 +1,1089 @@ +/* +* Copyright (c) 2013-2017 the CivetWeb developers +* Copyright (c) 2013 No Face Press, LLC +* License http://opensource.org/licenses/mit-license.php MIT License +*/ + +/* Simple example program on how to use CivetWeb embedded into a C program. */ +#ifdef _WIN32 +#include +#else +#include +#endif + +#include +#include +#include + +#include "civetweb.h" + + +#define DOCUMENT_ROOT "." +#ifdef NO_SSL +#ifdef USE_IPV6 +#define PORT "[::]:8888,8884" +#else +#define PORT "8888,8884" +#endif +#else +#ifdef USE_IPV6 +#define PORT "[::]:8888r,[::]:8843s,8884" +#else +#define PORT "8888r,8843s,8884" +#endif +#endif +#define EXAMPLE_URI "/example" +#define EXIT_URI "/exit" +int exitNow = 0; + + +int +ExampleHandler(struct mg_connection *conn, void *cbdata) +{ + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " + "close\r\n\r\n"); + mg_printf(conn, ""); + mg_printf(conn, "

This is an example text from a C handler

"); + mg_printf( + conn, + "

To see a page from the A handler click A

"); + mg_printf(conn, + "

To see a page from the A handler click " + "A/A

"); + mg_printf(conn, + "

To see a page from the A/B handler click A/B

"); + mg_printf(conn, + "

To see a page from the B handler (0) click B

"); + mg_printf(conn, + "

To see a page from the B handler (1) click B/A

"); + mg_printf(conn, + "

To see a page from the B handler (2) click B/B

"); + mg_printf(conn, + "

To see a page from the *.foo handler click xy.foo

"); + mg_printf(conn, + "

To see a page from the close handler click close

"); + mg_printf(conn, + "

To see a page from the FileHandler handler click form (the starting point of the " + "form test)

"); + mg_printf(conn, + "

To see a page from the CookieHandler handler click cookie

"); + mg_printf(conn, + "

To see a page from the PostResponser handler click post response

"); + mg_printf(conn, + "

To see an example for parsing files on the fly click form (form for " + "uploading files)

"); + +#ifdef USE_WEBSOCKET + mg_printf(conn, + "

To test the websocket handler click " + "websocket

"); +#endif + + mg_printf(conn, + "

To test the authentication handler click " + "auth

"); + + mg_printf(conn, "

To exit click exit

", EXIT_URI); + mg_printf(conn, "\n"); + return 1; +} + + +int +ExitHandler(struct mg_connection *conn, void *cbdata) +{ + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: " + "text/plain\r\nConnection: close\r\n\r\n"); + mg_printf(conn, "Server will shut down.\n"); + mg_printf(conn, "Bye!\n"); + exitNow = 1; + return 1; +} + + +int +AHandler(struct mg_connection *conn, void *cbdata) +{ + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " + "close\r\n\r\n"); + mg_printf(conn, ""); + mg_printf(conn, "

This is the A handler!!!

"); + mg_printf(conn, "\n"); + return 1; +} + + +int +ABHandler(struct mg_connection *conn, void *cbdata) +{ + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " + "close\r\n\r\n"); + mg_printf(conn, ""); + mg_printf(conn, "

This is the AB handler!!!

"); + mg_printf(conn, "\n"); + return 1; +} + + +int +BXHandler(struct mg_connection *conn, void *cbdata) +{ + /* Handler may access the request info using mg_get_request_info */ + const struct mg_request_info *req_info = mg_get_request_info(conn); + + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " + "close\r\n\r\n"); + mg_printf(conn, ""); + mg_printf(conn, "

This is the BX handler %p!!!

", cbdata); + mg_printf(conn, "

The actual uri is %s

", req_info->local_uri); + mg_printf(conn, "\n"); + return 1; +} + + +int +FooHandler(struct mg_connection *conn, void *cbdata) +{ + /* Handler may access the request info using mg_get_request_info */ + const struct mg_request_info *req_info = mg_get_request_info(conn); + + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " + "close\r\n\r\n"); + mg_printf(conn, ""); + mg_printf(conn, "

This is the Foo handler!!!

"); + mg_printf(conn, + "

The request was:

%s %s HTTP/%s

", + req_info->request_method, + req_info->local_uri, + req_info->http_version); + mg_printf(conn, "\n"); + return 1; +} + + +int +CloseHandler(struct mg_connection *conn, void *cbdata) +{ + /* Handler may access the request info using mg_get_request_info */ + const struct mg_request_info *req_info = mg_get_request_info(conn); + + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " + "close\r\n\r\n"); + mg_printf(conn, ""); + mg_printf(conn, + "

This handler will close the connection in a second

"); +#ifdef _WIN32 + Sleep(1000); +#else + sleep(1); +#endif + mg_printf(conn, "bye"); + printf("CloseHandler: close connection\n"); + mg_close_connection(conn); + printf("CloseHandler: wait 10 sec\n"); +#ifdef _WIN32 + Sleep(10000); +#else + sleep(10); +#endif + printf("CloseHandler: return from function\n"); + return 1; +} + + +int +FileHandler(struct mg_connection *conn, void *cbdata) +{ + /* In this handler, we ignore the req_info and send the file "fileName". */ + const char *fileName = (const char *)cbdata; + + mg_send_file(conn, fileName); + return 1; +} + + +int +field_found(const char *key, + const char *filename, + char *path, + size_t pathlen, + void *user_data) +{ + struct mg_connection *conn = (struct mg_connection *)user_data; + + mg_printf(conn, "\r\n\r\n%s:\r\n", key); + + if (filename && *filename) { +#ifdef _WIN32 + _snprintf(path, pathlen, "D:\\tmp\\%s", filename); +#else + snprintf(path, pathlen, "/tmp/%s", filename); +#endif + return FORM_FIELD_STORAGE_STORE; + } + return FORM_FIELD_STORAGE_GET; +} + + +int +field_get(const char *key, const char *value, size_t valuelen, void *user_data) +{ + struct mg_connection *conn = (struct mg_connection *)user_data; + + if (key[0]) { + mg_printf(conn, "%s = ", key); + } + mg_write(conn, value, valuelen); + + return 0; +} + + +int +field_stored(const char *path, long long file_size, void *user_data) +{ + struct mg_connection *conn = (struct mg_connection *)user_data; + + mg_printf(conn, + "stored as %s (%lu bytes)\r\n\r\n", + path, + (unsigned long)file_size); + + return 0; +} + + +int +FormHandler(struct mg_connection *conn, void *cbdata) +{ + /* Handler may access the request info using mg_get_request_info */ + const struct mg_request_info *req_info = mg_get_request_info(conn); + int ret; + struct mg_form_data_handler fdh = {field_found, field_get, field_stored, 0}; + + /* It would be possible to check the request info here before calling + * mg_handle_form_request. */ + (void)req_info; + + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: " + "text/plain\r\nConnection: close\r\n\r\n"); + fdh.user_data = (void *)conn; + + /* Call the form handler */ + mg_printf(conn, "Form data:"); + ret = mg_handle_form_request(conn, &fdh); + mg_printf(conn, "\r\n%i fields found", ret); + + return 1; +} + + +int +FileUploadForm(struct mg_connection *conn, void *cbdata) +{ + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " + "close\r\n\r\n"); + + mg_printf(conn, "\n"); + mg_printf(conn, "\n\n"); + mg_printf(conn, "\n"); + mg_printf(conn, "File upload\n"); + mg_printf(conn, "\n\n"); + mg_printf(conn, + "
\n", + (const char *)cbdata); + mg_printf(conn, "\n"); + mg_printf(conn, "\n"); + mg_printf(conn, "
\n\n\n"); + return 1; +} + +#define MD5_STATIC static +#include "../src/md5.inl" + +struct tfile_checksum { + char name[128]; + unsigned long long length; + md5_state_t chksum; +}; + +#define MAX_FILES (10) + +struct tfiles_checksums { + int index; + struct tfile_checksum file[MAX_FILES]; +}; + + +int +field_disp_read_on_the_fly(const char *key, + const char *filename, + char *path, + size_t pathlen, + void *user_data) +{ + struct tfiles_checksums *context = (struct tfiles_checksums *)user_data; + + (void)key; + (void)path; + (void)pathlen; + + if (context->index < MAX_FILES) { + context->index++; + strncpy(context->file[context->index - 1].name, filename, 128); + context->file[context->index - 1].name[127] = 0; + context->file[context->index - 1].length = 0; + md5_init(&(context->file[context->index - 1].chksum)); + return FORM_FIELD_STORAGE_GET; + } + return FORM_FIELD_STORAGE_ABORT; +} + + +int +field_get_checksum(const char *key, + const char *value, + size_t valuelen, + void *user_data) +{ + struct tfiles_checksums *context = (struct tfiles_checksums *)user_data; + (void)key; + + context->file[context->index - 1].length += valuelen; + md5_append(&(context->file[context->index - 1].chksum), + (const md5_byte_t *)value, + valuelen); + + return 0; +} + + +int +CheckSumHandler(struct mg_connection *conn, void *cbdata) +{ + /* Handler may access the request info using mg_get_request_info */ + const struct mg_request_info *req_info = mg_get_request_info(conn); + int i, j, ret; + struct tfiles_checksums chksums; + md5_byte_t digest[16]; + struct mg_form_data_handler fdh = {field_disp_read_on_the_fly, + field_get_checksum, + 0, + (void *)&chksums}; + + /* It would be possible to check the request info here before calling + * mg_handle_form_request. */ + (void)req_info; + + memset(&chksums, 0, sizeof(chksums)); + + mg_printf(conn, + "HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Connection: close\r\n\r\n"); + + /* Call the form handler */ + mg_printf(conn, "File checksums:"); + ret = mg_handle_form_request(conn, &fdh); + for (i = 0; i < chksums.index; i++) { + md5_finish(&(chksums.file[i].chksum), digest); + /* Visual Studio 2010+ support llu */ + mg_printf(conn, + "\r\n%s %llu ", + chksums.file[i].name, + chksums.file[i].length); + for (j = 0; j < 16; j++) { + mg_printf(conn, "%02x", (unsigned int)digest[j]); + } + } + mg_printf(conn, "\r\n%i files\r\n", ret); + + return 1; +} + + +int +CookieHandler(struct mg_connection *conn, void *cbdata) +{ + /* Handler may access the request info using mg_get_request_info */ + const struct mg_request_info *req_info = mg_get_request_info(conn); + const char *cookie = mg_get_header(conn, "Cookie"); + char first_str[64], count_str[64]; + int count; + + (void)mg_get_cookie(cookie, "first", first_str, sizeof(first_str)); + (void)mg_get_cookie(cookie, "count", count_str, sizeof(count_str)); + + mg_printf(conn, "HTTP/1.1 200 OK\r\nConnection: close\r\n"); + if (first_str[0] == 0) { + time_t t = time(0); + struct tm *ptm = localtime(&t); + mg_printf(conn, + "Set-Cookie: first=%04i-%02i-%02iT%02i:%02i:%02i\r\n", + ptm->tm_year + 1900, + ptm->tm_mon + 1, + ptm->tm_mday, + ptm->tm_hour, + ptm->tm_min, + ptm->tm_sec); + } + count = (count_str[0] == 0) ? 0 : atoi(count_str); + mg_printf(conn, "Set-Cookie: count=%i\r\n", count + 1); + mg_printf(conn, "Content-Type: text/html\r\n\r\n"); + + mg_printf(conn, ""); + mg_printf(conn, "

This is the CookieHandler.

"); + mg_printf(conn, "

The actual uri is %s

", req_info->local_uri); + + if (first_str[0] == 0) { + mg_printf(conn, "

This is the first time, you opened this page

"); + } else { + mg_printf(conn, "

You opened this page %i times before.

", count); + mg_printf(conn, "

You first opened this page on %s.

", first_str); + } + + mg_printf(conn, "\n"); + return 1; +} + + +int +PostResponser(struct mg_connection *conn, void *cbdata) +{ + long long r_total = 0; + int r, s; + + char buf[2048]; + + const struct mg_request_info *ri = mg_get_request_info(conn); + + if (strcmp(ri->request_method, "POST")) { + char buf[1024]; + int ret = mg_get_request_link(conn, buf, sizeof(buf)); + + mg_printf(conn, + "HTTP/1.1 405 Method Not Allowed\r\nConnection: close\r\n"); + mg_printf(conn, "Content-Type: text/plain\r\n\r\n"); + mg_printf(conn, + "%s method not allowed in the POST handler\n", + ri->request_method); + if (ret >= 0) { + mg_printf(conn, + "use a web tool to send a POST request to %s\n", + buf); + } + return 1; + } + + if (ri->content_length >= 0) { + /* We know the content length in advance */ + } else { + /* We must read until we find the end (chunked encoding + * or connection close), indicated my mg_read returning 0 */ + } + + mg_printf(conn, + "HTTP/1.1 200 OK\r\nConnection: " + "close\r\nTransfer-Encoding: chunked\r\n"); + mg_printf(conn, "Content-Type: text/plain\r\n\r\n"); + + r = mg_read(conn, buf, sizeof(buf)); + while (r > 0) { + r_total += r; + s = mg_send_chunk(conn, buf, r); + if (r != s) { + /* Send error */ + break; + } + r = mg_read(conn, buf, sizeof(buf)); + } + mg_printf(conn, "0\r\n"); + + return 1; +} + + +int +AuthStartHandler(struct mg_connection *conn, void *cbdata) +{ + static unsigned long long firstload = 0; + const char *passfile = "password_example_file.txt"; + const char *realm = "password_example"; + const char *user = "user"; + char passwd[64]; + + if (firstload == 0) { + + /* Set a random password (4 digit number - bad idea from a security + * point of view, but this is an API demo, not a security tutorial), + * and store it in some directory within the document root (extremely + * bad idea, but this is still not a security tutorial). + * The reason we create a new password every time the server starts + * is just for demonstration - we don't want the browser to store the + * password, so when we repeat the test we start with a new password. + */ + firstload = (unsigned long long)time(NULL); + sprintf(passwd, "%04u", (unsigned int)(firstload % 10000)); + mg_modify_passwords_file(passfile, realm, user, passwd); + + /* Just tell the user the new password generated for this test. */ + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " + "close\r\n\r\n"); + + mg_printf(conn, "\n"); + mg_printf(conn, "\n\n"); + mg_printf(conn, "\n"); + mg_printf(conn, "Auth handlerexample\n"); + mg_printf(conn, "\n"); + + mg_printf(conn, "\n"); + mg_printf(conn, + "

The first time you visit this page, it's free!

\n"); + mg_printf(conn, + "

Next time, use username \"%s\" and password \"%s\"

\n", + user, + passwd); + mg_printf(conn, "\n\n"); + + return 1; + } + + if (mg_check_digest_access_authentication(conn, realm, passfile) <= 0) { + /* No valid authorization */ + mg_send_digest_access_authentication_request(conn, realm); + return 1; + } + + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " + "close\r\n\r\n"); + + mg_printf(conn, "\n"); + mg_printf(conn, "\n\n"); + mg_printf(conn, "\n"); + mg_printf(conn, "Auth handlerexample\n"); + mg_printf(conn, "\n"); + + mg_printf(conn, "\n"); + mg_printf(conn, "

This is the password protected contents

\n"); + mg_printf(conn, "\n\n"); + + return 1; +} + + +int +WebSocketStartHandler(struct mg_connection *conn, void *cbdata) +{ + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " + "close\r\n\r\n"); + + mg_printf(conn, "\n"); + mg_printf(conn, "\n\n"); + mg_printf(conn, "\n"); + mg_printf(conn, "Embedded websocket example\n"); + +#ifdef USE_WEBSOCKET + /* mg_printf(conn, "\n"); ... xhtml style */ + mg_printf(conn, "\n"); + mg_printf(conn, "\n\n"); + mg_printf( + conn, + "
No websocket connection yet
\n"); +#else + mg_printf(conn, "\n\n"); + mg_printf(conn, "Example not compiled with USE_WEBSOCKET\n"); +#endif + mg_printf(conn, "\n\n"); + + return 1; +} + + +#ifdef USE_WEBSOCKET + +/* MAX_WS_CLIENTS defines how many clients can connect to a websocket at the + * same time. The value 5 is very small and used here only for demonstration; + * it can be easily tested to connect more than MAX_WS_CLIENTS clients. + * A real server should use a much higher number, or better use a dynamic list + * of currently connected websocket clients. */ +#define MAX_WS_CLIENTS (5) + +struct t_ws_client { + struct mg_connection *conn; + int state; +} static ws_clients[MAX_WS_CLIENTS]; + + +#define ASSERT(x) \ + { \ + if (!(x)) { \ + fprintf(stderr, \ + "Assertion failed in line %u\n", \ + (unsigned)__LINE__); \ + } \ + } + + +int +WebSocketConnectHandler(const struct mg_connection *conn, void *cbdata) +{ + struct mg_context *ctx = mg_get_context(conn); + int reject = 1; + int i; + + mg_lock_context(ctx); + for (i = 0; i < MAX_WS_CLIENTS; i++) { + if (ws_clients[i].conn == NULL) { + ws_clients[i].conn = (struct mg_connection *)conn; + ws_clients[i].state = 1; + mg_set_user_connection_data(ws_clients[i].conn, + (void *)(ws_clients + i)); + reject = 0; + break; + } + } + mg_unlock_context(ctx); + + fprintf(stdout, + "Websocket client %s\r\n\r\n", + (reject ? "rejected" : "accepted")); + return reject; +} + + +void +WebSocketReadyHandler(struct mg_connection *conn, void *cbdata) +{ + const char *text = "Hello from the websocket ready handler"; + struct t_ws_client *client = mg_get_user_connection_data(conn); + + mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, text, strlen(text)); + fprintf(stdout, "Greeting message sent to websocket client\r\n\r\n"); + ASSERT(client->conn == conn); + ASSERT(client->state == 1); + + client->state = 2; +} + + +int +WebsocketDataHandler(struct mg_connection *conn, + int bits, + char *data, + size_t len, + void *cbdata) +{ + struct t_ws_client *client = mg_get_user_connection_data(conn); + ASSERT(client->conn == conn); + ASSERT(client->state >= 1); + + fprintf(stdout, "Websocket got %lu bytes of ", (unsigned long)len); + switch (((unsigned char)bits) & 0x0F) { + case WEBSOCKET_OPCODE_CONTINUATION: + fprintf(stdout, "continuation"); + break; + case WEBSOCKET_OPCODE_TEXT: + fprintf(stdout, "text"); + break; + case WEBSOCKET_OPCODE_BINARY: + fprintf(stdout, "binary"); + break; + case WEBSOCKET_OPCODE_CONNECTION_CLOSE: + fprintf(stdout, "close"); + break; + case WEBSOCKET_OPCODE_PING: + fprintf(stdout, "ping"); + break; + case WEBSOCKET_OPCODE_PONG: + fprintf(stdout, "pong"); + break; + default: + fprintf(stdout, "unknown(%1xh)", ((unsigned char)bits) & 0x0F); + break; + } + fprintf(stdout, " data:\r\n"); + fwrite(data, len, 1, stdout); + fprintf(stdout, "\r\n\r\n"); + + return 1; +} + + +void +WebSocketCloseHandler(const struct mg_connection *conn, void *cbdata) +{ + struct mg_context *ctx = mg_get_context(conn); + struct t_ws_client *client = mg_get_user_connection_data(conn); + ASSERT(client->conn == conn); + ASSERT(client->state >= 1); + + mg_lock_context(ctx); + client->state = 0; + client->conn = NULL; + mg_unlock_context(ctx); + + fprintf(stdout, + "Client droped from the set of webserver connections\r\n\r\n"); +} + + +void +InformWebsockets(struct mg_context *ctx) +{ + static unsigned long cnt = 0; + char text[32]; + int i; + + sprintf(text, "%lu", ++cnt); + + mg_lock_context(ctx); + for (i = 0; i < MAX_WS_CLIENTS; i++) { + if (ws_clients[i].state == 2) { + mg_websocket_write(ws_clients[i].conn, + WEBSOCKET_OPCODE_TEXT, + text, + strlen(text)); + } + } + mg_unlock_context(ctx); +} +#endif + + +#ifdef USE_SSL_DH +#include "openssl/ssl.h" +#include "openssl/dh.h" +#include "openssl/ec.h" +#include "openssl/evp.h" +#include "openssl/ecdsa.h" + +DH * +get_dh2236() +{ + static unsigned char dh2236_p[] = { + 0x0E, 0x97, 0x6E, 0x6A, 0x88, 0x84, 0xD2, 0xD7, 0x55, 0x6A, 0x17, 0xB7, + 0x81, 0x9A, 0x98, 0xBC, 0x7E, 0xD1, 0x6A, 0x44, 0xB1, 0x18, 0xE6, 0x25, + 0x3A, 0x62, 0x35, 0xF0, 0x41, 0x91, 0xE2, 0x16, 0x43, 0x9D, 0x8F, 0x7D, + 0x5D, 0xDA, 0x85, 0x47, 0x25, 0xC4, 0xBA, 0x68, 0x0A, 0x87, 0xDC, 0x2C, + 0x33, 0xF9, 0x75, 0x65, 0x17, 0xCB, 0x8B, 0x80, 0xFE, 0xE0, 0xA8, 0xAF, + 0xC7, 0x9E, 0x82, 0xBE, 0x6F, 0x1F, 0x00, 0x04, 0xBD, 0x69, 0x50, 0x8D, + 0x9C, 0x3C, 0x41, 0x69, 0x21, 0x4E, 0x86, 0xC8, 0x2B, 0xCC, 0x07, 0x4D, + 0xCF, 0xE4, 0xA2, 0x90, 0x8F, 0x66, 0xA9, 0xEF, 0xF7, 0xFC, 0x6F, 0x5F, + 0x06, 0x22, 0x00, 0xCB, 0xCB, 0xC3, 0x98, 0x3F, 0x06, 0xB9, 0xEC, 0x48, + 0x3B, 0x70, 0x6E, 0x94, 0xE9, 0x16, 0xE1, 0xB7, 0x63, 0x2E, 0xAB, 0xB2, + 0xF3, 0x84, 0xB5, 0x3D, 0xD7, 0x74, 0xF1, 0x6A, 0xD1, 0xEF, 0xE8, 0x04, + 0x18, 0x76, 0xD2, 0xD6, 0xB0, 0xB7, 0x71, 0xB6, 0x12, 0x8F, 0xD1, 0x33, + 0xAB, 0x49, 0xAB, 0x09, 0x97, 0x35, 0x9D, 0x4B, 0xBB, 0x54, 0x22, 0x6E, + 0x1A, 0x33, 0x18, 0x02, 0x8A, 0xF4, 0x7C, 0x0A, 0xCE, 0x89, 0x75, 0x2D, + 0x10, 0x68, 0x25, 0xA9, 0x6E, 0xCD, 0x97, 0x49, 0xED, 0xAE, 0xE6, 0xA7, + 0xB0, 0x07, 0x26, 0x25, 0x60, 0x15, 0x2B, 0x65, 0x88, 0x17, 0xF2, 0x5D, + 0x2C, 0xF6, 0x2A, 0x7A, 0x8C, 0xAD, 0xB6, 0x0A, 0xA2, 0x57, 0xB0, 0xC1, + 0x0E, 0x5C, 0xA8, 0xA1, 0x96, 0x58, 0x9A, 0x2B, 0xD4, 0xC0, 0x8A, 0xCF, + 0x91, 0x25, 0x94, 0xB4, 0x14, 0xA7, 0xE4, 0xE2, 0x1B, 0x64, 0x5F, 0xD2, + 0xCA, 0x70, 0x46, 0xD0, 0x2C, 0x95, 0x6B, 0x9A, 0xFB, 0x83, 0xF9, 0x76, + 0xE6, 0xD4, 0xA4, 0xA1, 0x2B, 0x2F, 0xF5, 0x1D, 0xE4, 0x06, 0xAF, 0x7D, + 0x22, 0xF3, 0x04, 0x30, 0x2E, 0x4C, 0x64, 0x12, 0x5B, 0xB0, 0x55, 0x3E, + 0xC0, 0x5E, 0x56, 0xCB, 0x99, 0xBC, 0xA8, 0xD9, 0x23, 0xF5, 0x57, 0x40, + 0xF0, 0x52, 0x85, 0x9B, + }; + static unsigned char dh2236_g[] = { + 0x02, + }; + DH *dh; + + if ((dh = DH_new()) == NULL) + return (NULL); + dh->p = BN_bin2bn(dh2236_p, sizeof(dh2236_p), NULL); + dh->g = BN_bin2bn(dh2236_g, sizeof(dh2236_g), NULL); + if ((dh->p == NULL) || (dh->g == NULL)) { + DH_free(dh); + return (NULL); + } + return (dh); +} +#endif + + +#ifndef NO_SSL +int +init_ssl(void *ssl_context, void *user_data) +{ + /* Add application specific SSL initialization */ + struct ssl_ctx_st *ctx = (struct ssl_ctx_st *)ssl_context; + +#ifdef USE_SSL_DH + /* example from https://github.com/civetweb/civetweb/issues/347 */ + DH *dh = get_dh2236(); + if (!dh) + return -1; + if (1 != SSL_CTX_set_tmp_dh(ctx, dh)) + return -1; + DH_free(dh); + + EC_KEY *ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); + if (!ecdh) + return -1; + if (1 != SSL_CTX_set_tmp_ecdh(ctx, ecdh)) + return -1; + EC_KEY_free(ecdh); + + printf("ECDH ciphers initialized\n"); +#endif + return 0; +} +#endif + + +int +log_message(const struct mg_connection *conn, const char *message) +{ + puts(message); + return 1; +} + + +int +main(int argc, char *argv[]) +{ + const char *options[] = { + "document_root", + DOCUMENT_ROOT, + "listening_ports", + PORT, + "request_timeout_ms", + "10000", + "error_log_file", + "error.log", +#ifdef USE_WEBSOCKET + "websocket_timeout_ms", + "3600000", +#endif +#ifndef NO_SSL + "ssl_certificate", + "../../resources/cert/server.pem", + "ssl_protocol_version", + "3", + "ssl_cipher_list", +#ifdef USE_SSL_DH + "ECDHE-RSA-AES256-GCM-SHA384:DES-CBC3-SHA:AES128-SHA:AES128-GCM-SHA256", +#else + "DES-CBC3-SHA:AES128-SHA:AES128-GCM-SHA256", +#endif +#endif + "enable_auth_domain_check", + "no", + 0}; + struct mg_callbacks callbacks; + struct mg_context *ctx; + struct mg_server_ports ports[32]; + int port_cnt, n; + int err = 0; + +/* Check if libcivetweb has been built with all required features. */ +#ifdef USE_IPV6 + if (!mg_check_feature(8)) { + fprintf(stderr, + "Error: Embedded example built with IPv6 support, " + "but civetweb library build without.\n"); + err = 1; + } +#endif +#ifdef USE_WEBSOCKET + if (!mg_check_feature(16)) { + fprintf(stderr, + "Error: Embedded example built with websocket support, " + "but civetweb library build without.\n"); + err = 1; + } +#endif +#ifndef NO_SSL + if (!mg_check_feature(2)) { + fprintf(stderr, + "Error: Embedded example built with SSL support, " + "but civetweb library build without.\n"); + err = 1; + } +#endif + if (err) { + fprintf(stderr, "Cannot start CivetWeb - inconsistent build.\n"); + return EXIT_FAILURE; + } + + /* Start CivetWeb web server */ + memset(&callbacks, 0, sizeof(callbacks)); +#ifndef NO_SSL + callbacks.init_ssl = init_ssl; +#endif + callbacks.log_message = log_message; + ctx = mg_start(&callbacks, 0, options); + + /* Check return value: */ + if (ctx == NULL) { + fprintf(stderr, "Cannot start CivetWeb - mg_start failed.\n"); + return EXIT_FAILURE; + } + + /* Add handler EXAMPLE_URI, to explain the example */ + mg_set_request_handler(ctx, EXAMPLE_URI, ExampleHandler, 0); + mg_set_request_handler(ctx, EXIT_URI, ExitHandler, 0); + + /* Add handler for /A* and special handler for /A/B */ + mg_set_request_handler(ctx, "/A", AHandler, 0); + mg_set_request_handler(ctx, "/A/B", ABHandler, 0); + + /* Add handler for /B, /B/A, /B/B but not for /B* */ + mg_set_request_handler(ctx, "/B$", BXHandler, (void *)0); + mg_set_request_handler(ctx, "/B/A$", BXHandler, (void *)1); + mg_set_request_handler(ctx, "/B/B$", BXHandler, (void *)2); + + /* Add handler for all files with .foo extention */ + mg_set_request_handler(ctx, "**.foo$", FooHandler, 0); + + /* Add handler for /close extention */ + mg_set_request_handler(ctx, "/close", CloseHandler, 0); + + /* Add handler for /form (serve a file outside the document root) */ + mg_set_request_handler(ctx, + "/form", + FileHandler, + (void *)"../../test/form.html"); + + /* Add handler for form data */ + mg_set_request_handler(ctx, + "/handle_form.embedded_c.example.callback", + FormHandler, + (void *)0); + + /* Add a file upload handler for parsing files on the fly */ + mg_set_request_handler(ctx, + "/on_the_fly_form", + FileUploadForm, + (void *)"/on_the_fly_form.md5.callback"); + mg_set_request_handler(ctx, + "/on_the_fly_form.md5.callback", + CheckSumHandler, + (void *)0); + + /* Add handler for /cookie example */ + mg_set_request_handler(ctx, "/cookie", CookieHandler, 0); + + /* Add handler for /postresponse example */ + mg_set_request_handler(ctx, "/postresponse", PostResponser, 0); + + /* Add HTTP site to open a websocket connection */ + mg_set_request_handler(ctx, "/websocket", WebSocketStartHandler, 0); + + /* Add HTTP site with auth */ + mg_set_request_handler(ctx, "/auth", AuthStartHandler, 0); + + +#ifdef USE_WEBSOCKET + /* WS site for the websocket connection */ + mg_set_websocket_handler(ctx, + "/websocket", + WebSocketConnectHandler, + WebSocketReadyHandler, + WebsocketDataHandler, + WebSocketCloseHandler, + 0); +#endif + + /* List all listening ports */ + memset(ports, 0, sizeof(ports)); + port_cnt = mg_get_server_ports(ctx, 32, ports); + printf("\n%i listening ports:\n\n", port_cnt); + + for (n = 0; n < port_cnt && n < 32; n++) { + const char *proto = ports[n].is_ssl ? "https" : "http"; + const char *host; + + if ((ports[n].protocol & 1) == 1) { + /* IPv4 */ + host = "127.0.0.1"; + printf("Browse files at %s://%s:%i/\n", proto, host, ports[n].port); + printf("Run example at %s://%s:%i%s\n", + proto, + host, + ports[n].port, + EXAMPLE_URI); + printf( + "Exit at %s://%s:%i%s\n", proto, host, ports[n].port, EXIT_URI); + printf("\n"); + } + + if ((ports[n].protocol & 2) == 2) { + /* IPv6 */ + host = "[::1]"; + printf("Browse files at %s://%s:%i/\n", proto, host, ports[n].port); + printf("Run example at %s://%s:%i%s\n", + proto, + host, + ports[n].port, + EXAMPLE_URI); + printf( + "Exit at %s://%s:%i%s\n", proto, host, ports[n].port, EXIT_URI); + printf("\n"); + } + } + + /* Wait until the server should be closed */ + while (!exitNow) { +#ifdef _WIN32 + Sleep(1000); +#else + sleep(1); +#endif +#ifdef USE_WEBSOCKET + InformWebsockets(ctx); +#endif + } + + /* Stop the server */ + mg_stop(ctx); + printf("Server stopped.\n"); + printf("Bye!\n"); + + return EXIT_SUCCESS; +} diff --git a/src/civetweb/examples/embedded_cpp/Makefile b/src/civetweb/examples/embedded_cpp/Makefile new file mode 100644 index 000000000..65adf98bb --- /dev/null +++ b/src/civetweb/examples/embedded_cpp/Makefile @@ -0,0 +1,36 @@ +# +# Copyright (c) 2013 No Face Press, LLC +# License http://opensource.org/licenses/mit-license.php MIT License +# + +#This makefile is used to test the other Makefiles + + +PROG = embedded_cpp +SRC = embedded_cpp.cpp + +TOP = ../.. +CIVETWEB_LIB = libcivetweb.a + +CFLAGS = -I$(TOP)/include $(COPT) +LIBS = -lpthread + +include $(TOP)/resources/Makefile.in-os + +ifeq ($(TARGET_OS),LINUX) + LIBS += -ldl +endif + +all: $(PROG) + +$(PROG): $(CIVETWEB_LIB) $(SRC) + $(CXX) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS) + +$(CIVETWEB_LIB): + $(MAKE) -C $(TOP) clean lib WITH_CPP=1 + cp $(TOP)/$(CIVETWEB_LIB) . + +clean: + rm -f $(CIVETWEB_LIB) $(PROG) + +.PHONY: all clean diff --git a/src/civetweb/examples/embedded_cpp/embedded_cpp.cpp b/src/civetweb/examples/embedded_cpp/embedded_cpp.cpp new file mode 100644 index 000000000..d45a573cf --- /dev/null +++ b/src/civetweb/examples/embedded_cpp/embedded_cpp.cpp @@ -0,0 +1,432 @@ +/* Copyright (c) 2013-2017 the Civetweb developers + * Copyright (c) 2013 No Face Press, LLC + * License http://opensource.org/licenses/mit-license.php MIT License + */ + +// Simple example program on how to use Embedded C++ interface. + +#include "CivetServer.h" +#include + +#ifdef _WIN32 +#include +#else +#include +#endif + +#define DOCUMENT_ROOT "." +#define PORT "8081" +#define EXAMPLE_URI "/example" +#define EXIT_URI "/exit" +bool exitNow = false; + +class ExampleHandler : public CivetHandler +{ + public: + bool + handleGet(CivetServer *server, struct mg_connection *conn) + { + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: " + "text/html\r\nConnection: close\r\n\r\n"); + mg_printf(conn, "\r\n"); + mg_printf(conn, + "

This is an example text from a C++ handler

\r\n"); + mg_printf(conn, + "

To see a page from the A handler click here

\r\n"); + mg_printf(conn, + "

To see a page from the A handler with a parameter " + "click here

\r\n"); + mg_printf(conn, + "

To see a page from the A/B handler click here

\r\n"); + mg_printf(conn, + "

To see a page from the *.foo handler click here

\r\n"); + mg_printf(conn, + "

To see a page from the WebSocket handler click here

\r\n"); + mg_printf(conn, + "

To exit click here

\r\n", + EXIT_URI); + mg_printf(conn, "\r\n"); + return true; + } +}; + +class ExitHandler : public CivetHandler +{ + public: + bool + handleGet(CivetServer *server, struct mg_connection *conn) + { + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: " + "text/plain\r\nConnection: close\r\n\r\n"); + mg_printf(conn, "Bye!\n"); + exitNow = true; + return true; + } +}; + +class AHandler : public CivetHandler +{ + private: + bool + handleAll(const char *method, + CivetServer *server, + struct mg_connection *conn) + { + std::string s = ""; + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: " + "text/html\r\nConnection: close\r\n\r\n"); + mg_printf(conn, ""); + mg_printf(conn, "

This is the A handler for \"%s\" !

", method); + if (CivetServer::getParam(conn, "param", s)) { + mg_printf(conn, "

param set to %s

", s.c_str()); + } else { + mg_printf(conn, "

param not set

"); + } + mg_printf(conn, "\n"); + return true; + } + + public: + bool + handleGet(CivetServer *server, struct mg_connection *conn) + { + return handleAll("GET", server, conn); + } + bool + handlePost(CivetServer *server, struct mg_connection *conn) + { + return handleAll("POST", server, conn); + } +}; + +class ABHandler : public CivetHandler +{ + public: + bool + handleGet(CivetServer *server, struct mg_connection *conn) + { + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: " + "text/html\r\nConnection: close\r\n\r\n"); + mg_printf(conn, ""); + mg_printf(conn, "

This is the AB handler!!!

"); + mg_printf(conn, "\n"); + return true; + } +}; + +class FooHandler : public CivetHandler +{ + public: + bool + handleGet(CivetServer *server, struct mg_connection *conn) + { + /* Handler may access the request info using mg_get_request_info */ + const struct mg_request_info *req_info = mg_get_request_info(conn); + + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: " + "text/html\r\nConnection: close\r\n\r\n"); + + mg_printf(conn, "\n"); + mg_printf(conn, "

This is the Foo GET handler!!!

\n"); + mg_printf(conn, + "

The request was:

%s %s HTTP/%s

\n", + req_info->request_method, + req_info->request_uri, + req_info->http_version); + mg_printf(conn, "\n"); + + return true; + } + bool + handlePost(CivetServer *server, struct mg_connection *conn) + { + /* Handler may access the request info using mg_get_request_info */ + const struct mg_request_info *req_info = mg_get_request_info(conn); + long long rlen, wlen; + long long nlen = 0; + long long tlen = req_info->content_length; + char buf[1024]; + + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: " + "text/html\r\nConnection: close\r\n\r\n"); + + mg_printf(conn, "\n"); + mg_printf(conn, "

This is the Foo POST handler!!!

\n"); + mg_printf(conn, + "

The request was:

%s %s HTTP/%s

\n", + req_info->request_method, + req_info->request_uri, + req_info->http_version); + mg_printf(conn, "

Content Length: %li

\n", (long)tlen); + mg_printf(conn, "
\n");
+
+		while (nlen < tlen) {
+			rlen = tlen - nlen;
+			if (rlen > sizeof(buf)) {
+				rlen = sizeof(buf);
+			}
+			rlen = mg_read(conn, buf, (size_t)rlen);
+			if (rlen <= 0) {
+				break;
+			}
+			wlen = mg_write(conn, buf, (size_t)rlen);
+			if (wlen != rlen) {
+				break;
+			}
+			nlen += wlen;
+		}
+
+		mg_printf(conn, "\n
\n"); + mg_printf(conn, "\n"); + + return true; + } + + #define fopen_recursive fopen + + bool + handlePut(CivetServer *server, struct mg_connection *conn) + { + /* Handler may access the request info using mg_get_request_info */ + const struct mg_request_info *req_info = mg_get_request_info(conn); + long long rlen, wlen; + long long nlen = 0; + long long tlen = req_info->content_length; + FILE * f; + char buf[1024]; + int fail = 0; + +#ifdef _WIN32 + _snprintf(buf, sizeof(buf), "D:\\somewhere\\%s\\%s", req_info->remote_user, req_info->local_uri); + buf[sizeof(buf)-1] = 0; + if (strlen(buf)>255) { + /* Windows will not work with path > 260 (MAX_PATH), unless we use + * the unicode API. However, this is just an example code: A real + * code will probably never store anything to D:\\somewhere and + * must be adapted to the specific needs anyhow. */ + fail = 1; + f = NULL; + } else { + f = fopen_recursive(buf, "wb"); + } +#else + snprintf(buf, sizeof(buf), "~/somewhere/%s/%s", req_info->remote_user, req_info->local_uri); + buf[sizeof(buf)-1] = 0; + if (strlen(buf)>1020) { + /* The string is too long and probably truncated. Make sure an + * UTF-8 string is never truncated between the UTF-8 code bytes. + * This example code must be adapted to the specific needs. */ + fail = 1; + f = NULL; + } else { + f = fopen_recursive(buf, "w"); + } +#endif + + if (!f) { + fail = 1; + } else { + while (nlen < tlen) { + rlen = tlen - nlen; + if (rlen > sizeof(buf)) { + rlen = sizeof(buf); + } + rlen = mg_read(conn, buf, (size_t)rlen); + if (rlen <= 0) { + fail = 1; + break; + } + wlen = fwrite(buf, 1, (size_t)rlen, f); + if (wlen != rlen) { + fail = 1; + break; + } + nlen += wlen; + } + fclose(f); + } + + if (fail) { + mg_printf(conn, + "HTTP/1.1 409 Conflict\r\n" + "Content-Type: text/plain\r\n" + "Connection: close\r\n\r\n"); + } else { + mg_printf(conn, + "HTTP/1.1 201 Created\r\n" + "Content-Type: text/plain\r\n" + "Connection: close\r\n\r\n"); + } + + return true; + } +}; + +class WsStartHandler : public CivetHandler +{ + public: + bool + handleGet(CivetServer *server, struct mg_connection *conn) + { + + mg_printf(conn, + "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " + "close\r\n\r\n"); + + mg_printf(conn, "\n"); + mg_printf(conn, "\n\n"); + mg_printf(conn, "\n"); + mg_printf(conn, "Embedded websocket example\n"); + +#ifdef USE_WEBSOCKET + /* mg_printf(conn, "\n"); ... xhtml style */ + mg_printf(conn, "\n"); + mg_printf(conn, "\n\n"); + mg_printf( + conn, + "
No websocket connection yet
\n"); +#else + mg_printf(conn, "\n\n"); + mg_printf(conn, "Example not compiled with USE_WEBSOCKET\n"); +#endif + mg_printf(conn, "\n\n"); + + return 1; +} +}; + + +#ifdef USE_WEBSOCKET +class WebSocketHandler : public CivetWebSocketHandler { + + virtual bool handleConnection(CivetServer *server, + const struct mg_connection *conn) { + printf("WS connected\n"); + return true; + } + + virtual void handleReadyState(CivetServer *server, + struct mg_connection *conn) { + printf("WS ready\n"); + + const char *text = "Hello from the websocket ready handler"; + mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, text, strlen(text)); + } + + virtual bool handleData(CivetServer *server, + struct mg_connection *conn, + int bits, + char *data, + size_t data_len) { + printf("WS got %lu bytes: ", (long unsigned)data_len); + fwrite(data, 1, data_len, stdout); + printf("\n"); + + mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len); + return (data_len<4); + } + + virtual void handleClose(CivetServer *server, + const struct mg_connection *conn) { + printf("WS closed\n"); + } +}; +#endif + + +int +main(int argc, char *argv[]) +{ + const char *options[] = { + "document_root", DOCUMENT_ROOT, "listening_ports", PORT, 0}; + + std::vector cpp_options; + for (int i=0; i<(sizeof(options)/sizeof(options[0])-1); i++) { + cpp_options.push_back(options[i]); + } + + // CivetServer server(options); // <-- C style start + CivetServer server(cpp_options); // <-- C++ style start + + ExampleHandler h_ex; + server.addHandler(EXAMPLE_URI, h_ex); + + ExitHandler h_exit; + server.addHandler(EXIT_URI, h_exit); + + AHandler h_a; + server.addHandler("/a", h_a); + + ABHandler h_ab; + server.addHandler("/a/b", h_ab); + + WsStartHandler h_ws; + server.addHandler("/ws", h_ws); + +#ifdef NO_FILES + /* This handler will handle "everything else", including + * requests to files. If this handler is installed, + * NO_FILES should be set. */ + FooHandler h_foo; + server.addHandler("", h_foo); + + printf("See a page from the \"all\" handler at http://localhost:%s/\n", PORT); +#else + FooHandler h_foo; + server.addHandler("**.foo", h_foo); + printf("Browse files at http://localhost:%s/\n", PORT); +#endif + +#ifdef USE_WEBSOCKET + WebSocketHandler h_websocket; + server.addWebSocketHandler("/websocket", h_websocket); + printf("Run websocket example at http://localhost:%s/ws\n", PORT); +#endif + + printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI); + printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI); + + while (!exitNow) { +#ifdef _WIN32 + Sleep(1000); +#else + sleep(1); +#endif + } + + printf("Bye!\n"); + + return 0; +} diff --git a/src/civetweb/examples/https/README.md b/src/civetweb/examples/https/README.md new file mode 100644 index 000000000..7b18b4724 --- /dev/null +++ b/src/civetweb/examples/https/README.md @@ -0,0 +1,15 @@ +HTTPS Server configuration example +==== + +This directory contains an example [`civetweb.conf`](civetweb.conf) configuration file for a secure HTTPS server. You can run a HTTPS server without most of the options there - only `ssl_certificate` and one port (e.g., `443s`) in `listening_ports` is required. The default settings will work, but not comply with up to date security standards. It is somewhat debatable what "up to date security" means - you can use the following web sites to run tests: + +- https://securityheaders.io +- https://www.htbridge.com/ssl +- https://www.htbridge.com/websec +- https://www.ssllabs.com/ssltest/analyze.html / https://www.qualys.com/forms/freescan/ +- probably there are some more ... let me know! + +Instructions to run the test and to adapt the configuration can be found [`civetweb.conf`](civetweb.conf). You can test this configuration directly with the standalone server, or you can take the settings and add it into your embedding code. + +Note: I do not take any warranty or liability for this configuration, or for the content of any linked web site. + diff --git a/src/civetweb/examples/https/civetweb.conf b/src/civetweb/examples/https/civetweb.conf new file mode 100644 index 000000000..cd10eddab --- /dev/null +++ b/src/civetweb/examples/https/civetweb.conf @@ -0,0 +1,86 @@ +# Instructions to run (on Linux) to reproduce test results: +# +# 1) copy civetweb executable here (examples/https directory) +# 2) sudo ./civetweb +# +# Instructions to adapt to your own server: +# +# 1) generate your own server cert +# 2) generate at least one backup server cert +# in case you want a self signed cert, you can use the script +# in resources/cert for both steps +# 3) copy the content of the *.pin files into the Public-Key-Pins +# header config (the base64 encoded certificate hash) +# 4) set the document root, and all other required http server settings +# 5) Run the tests from the three websites below. They will tell you +# also what clients are compatible with your settings. The settings +# here are very strict and lock out most older clients/browsers. +# You will find some hints for fine tuning there as well. +# 6) If you know all your clients, and give them client certificates in +# advance, you can significantly improve security by setting +# "ssl_verify_peer" to "yes" and specifying a client cert (directory) +# using "ssl_ca_file/path". This will lock out all clients without a +# proper certificate. Don't use it for your public home page, but +# consider it for your private remote access server. +# 7) run civetweb, like above - or better create your own start script +# You are welcome to share your thoughts and experience on GitHub +# (or Google groups) - see README.md in CivetWeb main directory + +# Don't run as super user, switch back to a regular user +run_as_user user + +# The standard HTTP port 80 should redirect to the standard HTTPS port 443 +listening_ports 80r,443s + +# Don't forget to set the document root and domain +#document_root tdb +#authentication_domain mydomain.com + +# Set the a certificate +ssl_certificate ../../resources/cert/server.pem + +# Require a client cert for your private server (see above) +#ssl_verify_peer yes +#ssl_ca_file ../../resources/cert/client.pem + +# Enforce TLS1.2 and some strong cipher(s) +ssl_protocol_version 4 +ssl_cipher_list ECDH+AESGCM+AES256:!aNULL:!MD5:!DSS + +# Tell all browsers to access this site only as HTTPS for the next 180 days +strict_transport_security_max_age 15552000 + +# Set some HTTP security header, see https://securityheaders.io +additional_header Content-Security-Policy: script-src 'self' +additional_header X-Frame-Options: SAMEORIGIN +additional_header X-Xss-Protection: 1; mode=block +additional_header X-Content-Type-Options: nosniff +additional_header Referrer-Policy: same-origin +additional_header Public-Key-Pins: pin-sha256="uz1UTAPen+xb+UoQqkVlEx4H653LbMjfRJcZx5OrjbI="; pin-sha256="pf3px1MBPmlTGAPoiHWqaSJ9L9Z+DKfwgsU7LfLnmsk="; max-age=7776000 +#additional_header Expect-CT: max-age=86400,report-uri="https://mydomain.com/report" + + +# Ratings from 2017-09-03 (tests performed later may require more +# strict security settings) +# +# Headers rated A+ from https://securityheaders.io/ +# +# SSL rated B from https://www.htbridge.com/ssl when using a self signed +# certificate, but no other weaknesses for modern browsers. +# Site remarks some older TLS versions and some weaker ciphers are not +# supported (but that's accessibility, not security). +# +# HTTPS rated A+ from https://www.htbridge.com/websec/ when using a self +# signed certificate, generated with make_certs.sh in resources/cert/ +# and adding the server.pin and server_bkup.pin content into the +# Public-Key-Pins header above. +# +# A rating of "T / If trust issues are ignored: A" (ignoring self-signed cert) +# from https://www.ssllabs.com/ssltest/, https://www.qualys.com/forms/freescan/ +# (Note: this test is runs with reverse DNS name, while all others use the +# IP address). +# +# Note: This settings are very strict and prevent some older but still common +# versions of major browsers to access this site. The test web sites will give +# you an overview. Test, before you use this settings. + diff --git a/src/civetweb/format.bat b/src/civetweb/format.bat new file mode 100755 index 000000000..e1ce64094 --- /dev/null +++ b/src/civetweb/format.bat @@ -0,0 +1,33 @@ +#!/bin/sh +clang-format -i src/civetweb.c +clang-format -i src/main.c +clang-format -i src/CivetServer.cpp +clang-format -i src/civetweb_private_lua.h +clang-format -i src/md5.inl +clang-format -i src/sha1.inl +clang-format -i src/mod_lua.inl +clang-format -i src/mod_duktape.inl +clang-format -i src/timer.inl +clang-format -i src/handle_form.inl + +clang-format -i src/third_party/civetweb_lua.h + +clang-format -i include/civetweb.h +clang-format -i include/CivetServer.h + +clang-format -i test/public_func.h +clang-format -i test/public_func.c +clang-format -i test/public_server.h +clang-format -i test/public_server.c +clang-format -i test/private.h +clang-format -i test/private.c +clang-format -i test/private_exe.h +clang-format -i test/private_exe.c +clang-format -i test/shared.h +clang-format -i test/shared.c +clang-format -i test/timertest.h +clang-format -i test/timertest.c +clang-format -i test/civetweb_check.h +clang-format -i test/main.c + +clang-format -i examples/embedded_c/embedded_c.c diff --git a/src/civetweb/include/CivetServer.h b/src/civetweb/include/CivetServer.h new file mode 100644 index 000000000..2da1096d8 --- /dev/null +++ b/src/civetweb/include/CivetServer.h @@ -0,0 +1,611 @@ +/* Copyright (c) 2013-2017 the Civetweb developers + * Copyright (c) 2013 No Face Press, LLC + * + * License http://opensource.org/licenses/mit-license.php MIT License + */ + +#ifndef _CIVETWEB_SERVER_H_ +#define _CIVETWEB_SERVER_H_ +#ifdef __cplusplus + +#include "civetweb.h" +#include +#include +#include +#include + +// forward declaration +class CivetServer; + +/** + * Exception class for thrown exceptions within the CivetHandler object. + */ +class CIVETWEB_API CivetException : public std::runtime_error +{ + public: + CivetException(const std::string &msg) : std::runtime_error(msg) + { + } +}; + +/** + * Basic interface for a URI request handler. Handlers implementations + * must be reentrant. + */ +class CIVETWEB_API CivetHandler +{ + public: + /** + * Destructor + */ + virtual ~CivetHandler() + { + } + + /** + * Callback method for GET request. + * + * @param server - the calling server + * @param conn - the connection information + * @returns true if implemented, false otherwise + */ + virtual bool handleGet(CivetServer *server, struct mg_connection *conn); + + /** + * Callback method for POST request. + * + * @param server - the calling server + * @param conn - the connection information + * @returns true if implemented, false otherwise + */ + virtual bool handlePost(CivetServer *server, struct mg_connection *conn); + + /** + * Callback method for HEAD request. + * + * @param server - the calling server + * @param conn - the connection information + * @returns true if implemented, false otherwise + */ + virtual bool handleHead(CivetServer *server, struct mg_connection *conn); + + /** + * Callback method for PUT request. + * + * @param server - the calling server + * @param conn - the connection information + * @returns true if implemented, false otherwise + */ + virtual bool handlePut(CivetServer *server, struct mg_connection *conn); + + /** + * Callback method for DELETE request. + * + * @param server - the calling server + * @param conn - the connection information + * @returns true if implemented, false otherwise + */ + virtual bool handleDelete(CivetServer *server, struct mg_connection *conn); + + /** + * Callback method for OPTIONS request. + * + * @param server - the calling server + * @param conn - the connection information + * @returns true if implemented, false otherwise + */ + virtual bool handleOptions(CivetServer *server, struct mg_connection *conn); + + /** + * Callback method for PATCH request. + * + * @param server - the calling server + * @param conn - the connection information + * @returns true if implemented, false otherwise + */ + virtual bool handlePatch(CivetServer *server, struct mg_connection *conn); +}; + +/** + * Basic interface for a URI authorization handler. Handler implementations + * must be reentrant. + */ +class CIVETWEB_API CivetAuthHandler +{ + public: + /** + * Destructor + */ + virtual ~CivetAuthHandler() + { + } + + /** + * Callback method for authorization requests. It is up the this handler + * to generate 401 responses if authorization fails. + * + * @param server - the calling server + * @param conn - the connection information + * @returns true if authorization succeeded, false otherwise + */ + virtual bool authorize(CivetServer *server, struct mg_connection *conn) = 0; +}; + +/** + * Basic interface for a websocket handler. Handlers implementations + * must be reentrant. + */ +class CIVETWEB_API CivetWebSocketHandler +{ + public: + /** + * Destructor + */ + virtual ~CivetWebSocketHandler() + { + } + + /** + * Callback method for when the client intends to establish a websocket + *connection, before websocket handshake. + * + * @param server - the calling server + * @param conn - the connection information + * @returns true to keep socket open, false to close it + */ + virtual bool handleConnection(CivetServer *server, + const struct mg_connection *conn); + + /** + * Callback method for when websocket handshake is successfully completed, + *and connection is ready for data exchange. + * + * @param server - the calling server + * @param conn - the connection information + */ + virtual void handleReadyState(CivetServer *server, + struct mg_connection *conn); + + /** + * Callback method for when a data frame has been received from the client. + * + * @param server - the calling server + * @param conn - the connection information + * @bits: first byte of the websocket frame, see websocket RFC at + *http://tools.ietf.org/html/rfc6455, section 5.2 + * @data, data_len: payload, with mask (if any) already applied. + * @returns true to keep socket open, false to close it + */ + virtual bool handleData(CivetServer *server, + struct mg_connection *conn, + int bits, + char *data, + size_t data_len); + + /** + * Callback method for when the connection is closed. + * + * @param server - the calling server + * @param conn - the connection information + */ + virtual void handleClose(CivetServer *server, + const struct mg_connection *conn); +}; + +/** + * CivetCallbacks + * + * wrapper for mg_callbacks + */ +struct CIVETWEB_API CivetCallbacks : public mg_callbacks { + CivetCallbacks(); +}; + +/** + * CivetServer + * + * Basic class for embedded web server. This has an URL mapping built-in. + */ +class CIVETWEB_API CivetServer +{ + public: + /** + * Constructor + * + * This automatically starts the sever. + * It is good practice to call getContext() after this in case there + * were errors starting the server. + * + * Note: CivetServer should not be used as a static instance in a Windows + * DLL, since the constructor creates threads and the destructor joins + * them again (creating/joining threads should not be done in static + * constructors). + * + * @param options - the web server options. + * @param callbacks - optional web server callback methods. + * + * @throws CivetException + */ + CivetServer(const char **options, + const struct CivetCallbacks *callbacks = 0, + const void *UserContext = 0); + CivetServer(std::vector options, + const struct CivetCallbacks *callbacks = 0, + const void *UserContext = 0); + + /** + * Destructor + */ + virtual ~CivetServer(); + + /** + * close() + * + * Stops server and frees resources. + */ + void close(); + + /** + * getContext() + * + * @return the context or 0 if not running. + */ + const struct mg_context * + getContext() const + { + return context; + } + + /** + * addHandler(const std::string &, CivetHandler *) + * + * Adds a URI handler. If there is existing URI handler, it will + * be replaced with this one. + * + * URI's are ordered and prefix (REST) URI's are supported. + * + * @param uri - URI to match. + * @param handler - handler instance to use. + */ + void addHandler(const std::string &uri, CivetHandler *handler); + + void + addHandler(const std::string &uri, CivetHandler &handler) + { + addHandler(uri, &handler); + } + + /** + * addWebSocketHandler + * + * Adds a WebSocket handler for a specific URI. If there is existing URI + *handler, it will + * be replaced with this one. + * + * URI's are ordered and prefix (REST) URI's are supported. + * + * @param uri - URI to match. + * @param handler - handler instance to use. + */ + void addWebSocketHandler(const std::string &uri, + CivetWebSocketHandler *handler); + + void + addWebSocketHandler(const std::string &uri, CivetWebSocketHandler &handler) + { + addWebSocketHandler(uri, &handler); + } + + /** + * removeHandler(const std::string &) + * + * Removes a handler. + * + * @param uri - the exact URL used in addHandler(). + */ + void removeHandler(const std::string &uri); + + /** + * removeWebSocketHandler(const std::string &) + * + * Removes a web socket handler. + * + * @param uri - the exact URL used in addWebSocketHandler(). + */ + void removeWebSocketHandler(const std::string &uri); + + /** + * addAuthHandler(const std::string &, CivetAuthHandler *) + * + * Adds a URI authorization handler. If there is existing URI authorization + * handler, it will be replaced with this one. + * + * URI's are ordered and prefix (REST) URI's are supported. + * + * @param uri - URI to match. + * @param handler - authorization handler instance to use. + */ + void addAuthHandler(const std::string &uri, CivetAuthHandler *handler); + + void + addAuthHandler(const std::string &uri, CivetAuthHandler &handler) + { + addAuthHandler(uri, &handler); + } + + /** + * removeAuthHandler(const std::string &) + * + * Removes an authorization handler. + * + * @param uri - the exact URL used in addAuthHandler(). + */ + void removeAuthHandler(const std::string &uri); + + /** + * getListeningPorts() + * + * Returns a list of ports that are listening + * + * @return A vector of ports + */ + + std::vector getListeningPorts(); + + /** + * getCookie(struct mg_connection *conn, const std::string &cookieName, + *std::string &cookieValue) + * + * Puts the cookie value string that matches the cookie name in the + *cookieValue destinaton string. + * + * @param conn - the connection information + * @param cookieName - cookie name to get the value from + * @param cookieValue - cookie value is returned using thiis reference + * @returns the size of the cookie value string read. + */ + static int getCookie(struct mg_connection *conn, + const std::string &cookieName, + std::string &cookieValue); + + /** + * getHeader(struct mg_connection *conn, const std::string &headerName) + * @param conn - the connection information + * @param headerName - header name to get the value from + * @returns a char array whcih contains the header value as string + */ + static const char *getHeader(struct mg_connection *conn, + const std::string &headerName); + + /** + * getParam(struct mg_connection *conn, const char *, std::string &, size_t) + * + * Returns a query paramter contained in the supplied buffer. The + * occurance value is a zero-based index of a particular key name. This + * should not be confused with the index over all of the keys. Note that + *this + * function assumes that parameters are sent as text in http query string + * format, which is the default for web forms. This function will work for + * html forms with method="GET" and method="POST" attributes. In other + *cases, + * you may use a getParam version that directly takes the data instead of + *the + * connection as a first argument. + * + * @param conn - parameters are read from the data sent through this + *connection + * @param name - the key to search for + * @param dst - the destination string + * @param occurrence - the occurrence of the selected name in the query (0 + *based). + * @return true if key was found + */ + static bool getParam(struct mg_connection *conn, + const char *name, + std::string &dst, + size_t occurrence = 0); + + /** + * getParam(const std::string &, const char *, std::string &, size_t) + * + * Returns a query paramter contained in the supplied buffer. The + * occurance value is a zero-based index of a particular key name. This + * should not be confused with the index over all of the keys. + * + * @param data - the query string (text) + * @param name - the key to search for + * @param dst - the destination string + * @param occurrence - the occurrence of the selected name in the query (0 + *based). + * @return true if key was found + */ + static bool + getParam(const std::string &data, + const char *name, + std::string &dst, + size_t occurrence = 0) + { + return getParam(data.c_str(), data.length(), name, dst, occurrence); + } + + /** + * getParam(const char *, size_t, const char *, std::string &, size_t) + * + * Returns a query paramter contained in the supplied buffer. The + * occurance value is a zero-based index of a particular key name. This + * should not be confused with the index over all of the keys. + * + * @param data the - query string (text) + * @param data_len - length of the query string + * @param name - the key to search for + * @param dst - the destination string + * @param occurrence - the occurrence of the selected name in the query (0 + *based). + * @return true if key was found + */ + static bool getParam(const char *data, + size_t data_len, + const char *name, + std::string &dst, + size_t occurrence = 0); + + /** + * urlDecode(const std::string &, std::string &, bool) + * + * @param src - string to be decoded + * @param dst - destination string + * @param is_form_url_encoded - true if form url encoded + * form-url-encoded data differs from URI encoding in a way that it + * uses '+' as character for space, see RFC 1866 section 8.2.1 + * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt + */ + static void + urlDecode(const std::string &src, + std::string &dst, + bool is_form_url_encoded = true) + { + urlDecode(src.c_str(), src.length(), dst, is_form_url_encoded); + } + + /** + * urlDecode(const char *, size_t, std::string &, bool) + * + * @param src - buffer to be decoded + * @param src_len - length of buffer to be decoded + * @param dst - destination string + * @param is_form_url_encoded - true if form url encoded + * form-url-encoded data differs from URI encoding in a way that it + * uses '+' as character for space, see RFC 1866 section 8.2.1 + * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt + */ + static void urlDecode(const char *src, + size_t src_len, + std::string &dst, + bool is_form_url_encoded = true); + + /** + * urlDecode(const char *, std::string &, bool) + * + * @param src - buffer to be decoded (0 terminated) + * @param dst - destination string + * @param is_form_url_encoded true - if form url encoded + * form-url-encoded data differs from URI encoding in a way that it + * uses '+' as character for space, see RFC 1866 section 8.2.1 + * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt + */ + static void urlDecode(const char *src, + std::string &dst, + bool is_form_url_encoded = true); + + /** + * urlEncode(const std::string &, std::string &, bool) + * + * @param src - buffer to be encoded + * @param dst - destination string + * @param append - true if string should not be cleared before encoding. + */ + static void + urlEncode(const std::string &src, std::string &dst, bool append = false) + { + urlEncode(src.c_str(), src.length(), dst, append); + } + + /** + * urlEncode(const char *, size_t, std::string &, bool) + * + * @param src - buffer to be encoded (0 terminated) + * @param dst - destination string + * @param append - true if string should not be cleared before encoding. + */ + static void + urlEncode(const char *src, std::string &dst, bool append = false); + + /** + * urlEncode(const char *, size_t, std::string &, bool) + * + * @param src - buffer to be encoded + * @param src_len - length of buffer to be decoded + * @param dst - destination string + * @param append - true if string should not be cleared before encoding. + */ + static void urlEncode(const char *src, + size_t src_len, + std::string &dst, + bool append = false); + + // generic user context which can be set/read, + // the server does nothing with this apart from keep it. + const void * + getUserContext() const + { + return UserContext; + } + + protected: + class CivetConnection + { + public: + char *postData; + unsigned long postDataLen; + + CivetConnection(); + ~CivetConnection(); + }; + + struct mg_context *context; + std::map connections; + + // generic user context which can be set/read, + // the server does nothing with this apart from keep it. + const void *UserContext; + + private: + /** + * requestHandler(struct mg_connection *, void *cbdata) + * + * Handles the incomming request. + * + * @param conn - the connection information + * @param cbdata - pointer to the CivetHandler instance. + * @returns 0 if implemented, false otherwise + */ + static int requestHandler(struct mg_connection *conn, void *cbdata); + + static int webSocketConnectionHandler(const struct mg_connection *conn, + void *cbdata); + static void webSocketReadyHandler(struct mg_connection *conn, void *cbdata); + static int webSocketDataHandler(struct mg_connection *conn, + int bits, + char *data, + size_t data_len, + void *cbdata); + static void webSocketCloseHandler(const struct mg_connection *conn, + void *cbdata); + /** + * authHandler(struct mg_connection *, void *cbdata) + * + * Handles the authorization requests. + * + * @param conn - the connection information + * @param cbdata - pointer to the CivetAuthHandler instance. + * @returns 1 if authorized, 0 otherwise + */ + static int authHandler(struct mg_connection *conn, void *cbdata); + + /** + * closeHandler(struct mg_connection *) + * + * Handles closing a request (internal handler) + * + * @param conn - the connection information + */ + static void closeHandler(const struct mg_connection *conn); + + /** + * Stores the user provided close handler + */ + void (*userCloseHandler)(const struct mg_connection *conn); +}; + +#endif /* __cplusplus */ +#endif /* _CIVETWEB_SERVER_H_ */ diff --git a/src/civetweb/include/civetweb.h b/src/civetweb/include/civetweb.h new file mode 100644 index 000000000..6ddde4f2f --- /dev/null +++ b/src/civetweb/include/civetweb.h @@ -0,0 +1,1372 @@ +/* Copyright (c) 2013-2017 the Civetweb developers + * Copyright (c) 2004-2013 Sergey Lyubka + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef CIVETWEB_HEADER_INCLUDED +#define CIVETWEB_HEADER_INCLUDED + +#define CIVETWEB_VERSION "1.10" +#define CIVETWEB_VERSION_MAJOR (1) +#define CIVETWEB_VERSION_MINOR (10) +#define CIVETWEB_VERSION_PATCH (0) +#define CIVETWEB_VERSION_RELEASED + +#ifndef CIVETWEB_API +#if defined(_WIN32) +#if defined(CIVETWEB_DLL_EXPORTS) +#define CIVETWEB_API __declspec(dllexport) +#elif defined(CIVETWEB_DLL_IMPORTS) +#define CIVETWEB_API __declspec(dllimport) +#else +#define CIVETWEB_API +#endif +#elif __GNUC__ >= 4 +#define CIVETWEB_API __attribute__((visibility("default"))) +#else +#define CIVETWEB_API +#endif +#endif + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + + +/* Initialize this library. This should be called once before any other + * function from this library. This function is not guaranteed to be + * thread safe. + * Parameters: + * features: bit mask for features to be initialized. + * Return value: + * initialized features + * 0: error + */ +CIVETWEB_API unsigned mg_init_library(unsigned features); + + +/* Un-initialize this library. + * Return value: + * 0: error + */ +CIVETWEB_API unsigned mg_exit_library(void); + + +struct mg_context; /* Handle for the HTTP service itself */ +struct mg_connection; /* Handle for the individual connection */ + + +/* Maximum number of headers */ +#define MG_MAX_HEADERS (64) + +struct mg_header { + const char *name; /* HTTP header name */ + const char *value; /* HTTP header value */ +}; + + +/* This structure contains information about the HTTP request. */ +struct mg_request_info { + const char *request_method; /* "GET", "POST", etc */ + const char *request_uri; /* URL-decoded URI (absolute or relative, + * as in the request) */ + const char *local_uri; /* URL-decoded URI (relative). Can be NULL + * if the request_uri does not address a + * resource at the server host. */ +#if defined(MG_LEGACY_INTERFACE) + const char *uri; /* Deprecated: use local_uri instead */ +#endif + const char *http_version; /* E.g. "1.0", "1.1" */ + const char *query_string; /* URL part after '?', not including '?', or + NULL */ + const char *remote_user; /* Authenticated user, or NULL if no auth + used */ + char remote_addr[48]; /* Client's IP address as a string. */ + +#if defined(MG_LEGACY_INTERFACE) + long remote_ip; /* Client's IP address. Deprecated: use remote_addr instead + */ +#endif + + long long content_length; /* Length (in bytes) of the request body, + can be -1 if no length was given. */ + int remote_port; /* Client's port */ + int is_ssl; /* 1 if SSL-ed, 0 if not */ + void *user_data; /* User data pointer passed to mg_start() */ + void *conn_data; /* Connection-specific user data */ + + int num_headers; /* Number of HTTP headers */ + struct mg_header + http_headers[MG_MAX_HEADERS]; /* Allocate maximum headers */ + + struct mg_client_cert *client_cert; /* Client certificate information */ + + const char *acceptedWebSocketSubprotocol; /* websocket subprotocol, + * accepted during handshake */ +}; + + +/* This structure contains information about the HTTP request. */ +/* This structure may be extended in future versions. */ +struct mg_response_info { + int status_code; /* E.g. 200 */ + const char *status_text; /* E.g. "OK" */ + const char *http_version; /* E.g. "1.0", "1.1" */ + + long long content_length; /* Length (in bytes) of the request body, + can be -1 if no length was given. */ + + int num_headers; /* Number of HTTP headers */ + struct mg_header + http_headers[MG_MAX_HEADERS]; /* Allocate maximum headers */ +}; + + +/* Client certificate information (part of mg_request_info) */ +/* New nomenclature. */ +struct mg_client_cert { + const char *subject; + const char *issuer; + const char *serial; + const char *finger; +}; + +/* Old nomenclature. */ +struct client_cert { + const char *subject; + const char *issuer; + const char *serial; + const char *finger; +}; + + +/* This structure needs to be passed to mg_start(), to let civetweb know + which callbacks to invoke. For a detailed description, see + https://github.com/civetweb/civetweb/blob/master/docs/UserManual.md */ +struct mg_callbacks { + /* Called when civetweb has received new HTTP request. + If the callback returns one, it must process the request + by sending valid HTTP headers and a body. Civetweb will not do + any further processing. Otherwise it must return zero. + Note that since V1.7 the "begin_request" function is called + before an authorization check. If an authorization check is + required, use a request_handler instead. + Return value: + 0: civetweb will process the request itself. In this case, + the callback must not send any data to the client. + 1-999: callback already processed the request. Civetweb will + not send any data after the callback returned. The + return code is stored as a HTTP status code for the + access log. */ + int (*begin_request)(struct mg_connection *); + + /* Called when civetweb has finished processing request. */ + void (*end_request)(const struct mg_connection *, int reply_status_code); + + /* Called when civetweb is about to log a message. If callback returns + non-zero, civetweb does not log anything. */ + int (*log_message)(const struct mg_connection *, const char *message); + + /* Called when civetweb is about to log access. If callback returns + non-zero, civetweb does not log anything. */ + int (*log_access)(const struct mg_connection *, const char *message); + + /* Called when civetweb initializes SSL library. + Parameters: + user_data: parameter user_data passed when starting the server. + Return value: + 0: civetweb will set up the SSL certificate. + 1: civetweb assumes the callback already set up the certificate. + -1: initializing ssl fails. */ + int (*init_ssl)(void *ssl_context, void *user_data); + +#if defined(MG_LEGACY_INTERFACE) + /* Called when websocket request is received, before websocket handshake. + Return value: + 0: civetweb proceeds with websocket handshake. + 1: connection is closed immediately. + This callback is deprecated: Use mg_set_websocket_handler instead. */ + int (*websocket_connect)(const struct mg_connection *); + + /* Called when websocket handshake is successfully completed, and + connection is ready for data exchange. + This callback is deprecated: Use mg_set_websocket_handler instead. */ + void (*websocket_ready)(struct mg_connection *); + + /* Called when data frame has been received from the client. + Parameters: + bits: first byte of the websocket frame, see websocket RFC at + http://tools.ietf.org/html/rfc6455, section 5.2 + data, data_len: payload, with mask (if any) already applied. + Return value: + 1: keep this websocket connection open. + 0: close this websocket connection. + This callback is deprecated: Use mg_set_websocket_handler instead. */ + int (*websocket_data)(struct mg_connection *, + int bits, + char *data, + size_t data_len); +#endif /* MG_LEGACY_INTERFACE */ + + /* Called when civetweb is closing a connection. The per-context mutex is + locked when this is invoked. + + Websockets: + Before mg_set_websocket_handler has been added, it was primarily useful + for noting when a websocket is closing, and used to remove it from any + application-maintained list of clients. + Using this callback for websocket connections is deprecated: Use + mg_set_websocket_handler instead. + + Connection specific data: + If memory has been allocated for the connection specific user data + (mg_request_info->conn_data, mg_get_user_connection_data), + this is the last chance to free it. + */ + void (*connection_close)(const struct mg_connection *); + +#if defined(MG_USE_OPEN_FILE) + /* Note: The "file in memory" feature is a deletion candidate, since + * it complicates the code, and does not add any value compared to + * "mg_add_request_handler". + * See this discussion thread: + * https://groups.google.com/forum/#!topic/civetweb/h9HT4CmeYqI + * If you disagree, if there is any situation this is indeed useful + * and cannot trivially be replaced by another existing feature, + * please contribute to this discussion during the next 3 month + * (till end of April 2017), otherwise this feature might be dropped + * in future releases. */ + + /* Called when civetweb tries to open a file. Used to intercept file open + calls, and serve file data from memory instead. + Parameters: + path: Full path to the file to open. + data_len: Placeholder for the file size, if file is served from + memory. + Return value: + NULL: do not serve file from memory, proceed with normal file open. + non-NULL: pointer to the file contents in memory. data_len must be + initialized with the size of the memory block. */ + const char *(*open_file)(const struct mg_connection *, + const char *path, + size_t *data_len); +#endif + + /* Called when civetweb is about to serve Lua server page, if + Lua support is enabled. + Parameters: + lua_context: "lua_State *" pointer. */ + void (*init_lua)(const struct mg_connection *, void *lua_context); + +#if defined(MG_LEGACY_INTERFACE) + /* Called when civetweb has uploaded a file to a temporary directory as a + result of mg_upload() call. + Note that mg_upload is deprecated. Use mg_handle_form_request instead. + Parameters: + file_name: full path name to the uploaded file. */ + void (*upload)(struct mg_connection *, const char *file_name); +#endif + + /* Called when civetweb is about to send HTTP error to the client. + Implementing this callback allows to create custom error pages. + Parameters: + status: HTTP error status code. + Return value: + 1: run civetweb error handler. + 0: callback already handled the error. */ + int (*http_error)(struct mg_connection *, int status); + + /* Called after civetweb context has been created, before requests + are processed. + Parameters: + ctx: context handle */ + void (*init_context)(const struct mg_context *ctx); + + /* Called when a new worker thread is initialized. + Parameters: + ctx: context handle + thread_type: + 0 indicates the master thread + 1 indicates a worker thread handling client connections + 2 indicates an internal helper thread (timer thread) + */ + void (*init_thread)(const struct mg_context *ctx, int thread_type); + + /* Called when civetweb context is deleted. + Parameters: + ctx: context handle */ + void (*exit_context)(const struct mg_context *ctx); + + /* Called when initializing a new connection object. + * Can be used to initialize the connection specific user data + * (mg_request_info->conn_data, mg_get_user_connection_data). + * When the callback is called, it is not yet known if a + * valid HTTP(S) request will be made. + * Parameters: + * conn: not yet fully initialized connection object + * conn_data: output parameter, set to initialize the + * connection specific user data + * Return value: + * must be 0 + * Otherwise, the result is undefined + */ + int (*init_connection)(const struct mg_connection *conn, void **conn_data); +}; + + +/* Start web server. + + Parameters: + callbacks: mg_callbacks structure with user-defined callbacks. + options: NULL terminated list of option_name, option_value pairs that + specify Civetweb configuration parameters. + + Side-effects: on UNIX, ignores SIGCHLD and SIGPIPE signals. If custom + processing is required for these, signal handlers must be set up + after calling mg_start(). + + + Example: + const char *options[] = { + "document_root", "/var/www", + "listening_ports", "80,443s", + NULL + }; + struct mg_context *ctx = mg_start(&my_func, NULL, options); + + Refer to https://github.com/civetweb/civetweb/blob/master/docs/UserManual.md + for the list of valid option and their possible values. + + Return: + web server context, or NULL on error. */ +CIVETWEB_API struct mg_context *mg_start(const struct mg_callbacks *callbacks, + void *user_data, + const char **configuration_options); + + +/* Stop the web server. + + Must be called last, when an application wants to stop the web server and + release all associated resources. This function blocks until all Civetweb + threads are stopped. Context pointer becomes invalid. */ +CIVETWEB_API void mg_stop(struct mg_context *); + + +/* mg_request_handler + + Called when a new request comes in. This callback is URI based + and configured with mg_set_request_handler(). + + Parameters: + conn: current connection information. + cbdata: the callback data configured with mg_set_request_handler(). + Returns: + 0: the handler could not handle the request, so fall through. + 1 - 999: the handler processed the request. The return code is + stored as a HTTP status code for the access log. */ +typedef int (*mg_request_handler)(struct mg_connection *conn, void *cbdata); + + +/* mg_set_request_handler + + Sets or removes a URI mapping for a request handler. + This function uses mg_lock_context internally. + + URI's are ordered and prefixed URI's are supported. For example, + consider two URIs: /a/b and /a + /a matches /a + /a/b matches /a/b + /a/c matches /a + + Parameters: + ctx: server context + uri: the URI (exact or pattern) for the handler + handler: the callback handler to use when the URI is requested. + If NULL, an already registered handler for this URI will + be removed. + The URI used to remove a handler must match exactly the + one used to register it (not only a pattern match). + cbdata: the callback data to give to the handler when it is called. */ +CIVETWEB_API void mg_set_request_handler(struct mg_context *ctx, + const char *uri, + mg_request_handler handler, + void *cbdata); + + +/* Callback types for websocket handlers in C/C++. + + mg_websocket_connect_handler + Is called when the client intends to establish a websocket connection, + before websocket handshake. + Return value: + 0: civetweb proceeds with websocket handshake. + 1: connection is closed immediately. + + mg_websocket_ready_handler + Is called when websocket handshake is successfully completed, and + connection is ready for data exchange. + + mg_websocket_data_handler + Is called when a data frame has been received from the client. + Parameters: + bits: first byte of the websocket frame, see websocket RFC at + http://tools.ietf.org/html/rfc6455, section 5.2 + data, data_len: payload, with mask (if any) already applied. + Return value: + 1: keep this websocket connection open. + 0: close this websocket connection. + + mg_connection_close_handler + Is called, when the connection is closed.*/ +typedef int (*mg_websocket_connect_handler)(const struct mg_connection *, + void *); +typedef void (*mg_websocket_ready_handler)(struct mg_connection *, void *); +typedef int (*mg_websocket_data_handler)(struct mg_connection *, + int, + char *, + size_t, + void *); +typedef void (*mg_websocket_close_handler)(const struct mg_connection *, + void *); + +/* struct mg_websocket_subprotocols + * + * List of accepted subprotocols + */ +struct mg_websocket_subprotocols { + int nb_subprotocols; + char **subprotocols; +}; + +/* mg_set_websocket_handler + + Set or remove handler functions for websocket connections. + This function works similar to mg_set_request_handler - see there. */ +CIVETWEB_API void +mg_set_websocket_handler(struct mg_context *ctx, + const char *uri, + mg_websocket_connect_handler connect_handler, + mg_websocket_ready_handler ready_handler, + mg_websocket_data_handler data_handler, + mg_websocket_close_handler close_handler, + void *cbdata); + +/* mg_set_websocket_handler + + Set or remove handler functions for websocket connections. + This function works similar to mg_set_request_handler - see there. */ +CIVETWEB_API void mg_set_websocket_handler_with_subprotocols( + struct mg_context *ctx, + const char *uri, + struct mg_websocket_subprotocols *subprotocols, + mg_websocket_connect_handler connect_handler, + mg_websocket_ready_handler ready_handler, + mg_websocket_data_handler data_handler, + mg_websocket_close_handler close_handler, + void *cbdata); + + +/* mg_authorization_handler + + Callback function definition for mg_set_auth_handler + + Parameters: + conn: current connection information. + cbdata: the callback data configured with mg_set_request_handler(). + Returns: + 0: access denied + 1: access granted + */ +typedef int (*mg_authorization_handler)(struct mg_connection *conn, + void *cbdata); + + +/* mg_set_auth_handler + + Sets or removes a URI mapping for an authorization handler. + This function works similar to mg_set_request_handler - see there. */ +CIVETWEB_API void mg_set_auth_handler(struct mg_context *ctx, + const char *uri, + mg_authorization_handler handler, + void *cbdata); + + +/* Get the value of particular configuration parameter. + The value returned is read-only. Civetweb does not allow changing + configuration at run time. + If given parameter name is not valid, NULL is returned. For valid + names, return value is guaranteed to be non-NULL. If parameter is not + set, zero-length string is returned. */ +CIVETWEB_API const char *mg_get_option(const struct mg_context *ctx, + const char *name); + + +/* Get context from connection. */ +CIVETWEB_API struct mg_context * +mg_get_context(const struct mg_connection *conn); + + +/* Get user data passed to mg_start from context. */ +CIVETWEB_API void *mg_get_user_data(const struct mg_context *ctx); + + +/* Set user data for the current connection. */ +CIVETWEB_API void mg_set_user_connection_data(struct mg_connection *conn, + void *data); + + +/* Get user data set for the current connection. */ +CIVETWEB_API void * +mg_get_user_connection_data(const struct mg_connection *conn); + + +/* Get a formatted link corresponding to the current request + + Parameters: + conn: current connection information. + buf: string buffer (out) + buflen: length of the string buffer + Returns: + <0: error + >=0: ok */ +CIVETWEB_API int +mg_get_request_link(const struct mg_connection *conn, char *buf, size_t buflen); + + +#if defined(MG_LEGACY_INTERFACE) +/* Return array of strings that represent valid configuration options. + For each option, option name and default value is returned, i.e. the + number of entries in the array equals to number_of_options x 2. + Array is NULL terminated. */ +/* Deprecated: Use mg_get_valid_options instead. */ +CIVETWEB_API const char **mg_get_valid_option_names(void); +#endif + + +struct mg_option { + const char *name; + int type; + const char *default_value; +}; + +/* Old nomenclature */ +enum { + CONFIG_TYPE_UNKNOWN = 0x0, + CONFIG_TYPE_NUMBER = 0x1, + CONFIG_TYPE_STRING = 0x2, + CONFIG_TYPE_FILE = 0x3, + CONFIG_TYPE_DIRECTORY = 0x4, + CONFIG_TYPE_BOOLEAN = 0x5, + CONFIG_TYPE_EXT_PATTERN = 0x6, + CONFIG_TYPE_STRING_LIST = 0x7, + CONFIG_TYPE_STRING_MULTILINE = 0x8 +}; + +/* New nomenclature */ +enum { + MG_CONFIG_TYPE_UNKNOWN = 0x0, + MG_CONFIG_TYPE_NUMBER = 0x1, + MG_CONFIG_TYPE_STRING = 0x2, + MG_CONFIG_TYPE_FILE = 0x3, + MG_CONFIG_TYPE_DIRECTORY = 0x4, + MG_CONFIG_TYPE_BOOLEAN = 0x5, + MG_CONFIG_TYPE_EXT_PATTERN = 0x6, + MG_CONFIG_TYPE_STRING_LIST = 0x7, + MG_CONFIG_TYPE_STRING_MULTILINE = 0x8 +}; + +/* Return array of struct mg_option, representing all valid configuration + options of civetweb.c. + The array is terminated by a NULL name option. */ +CIVETWEB_API const struct mg_option *mg_get_valid_options(void); + + +struct mg_server_ports { + int protocol; /* 1 = IPv4, 2 = IPv6, 3 = both */ + int port; /* port number */ + int is_ssl; /* https port: 0 = no, 1 = yes */ + int is_redirect; /* redirect all requests: 0 = no, 1 = yes */ + int _reserved1; + int _reserved2; + int _reserved3; + int _reserved4; +}; + + +/* Get the list of ports that civetweb is listening on. + The parameter size is the size of the ports array in elements. + The caller is responsibility to allocate the required memory. + This function returns the number of struct mg_server_ports elements + filled in, or <0 in case of an error. */ +CIVETWEB_API int mg_get_server_ports(const struct mg_context *ctx, + int size, + struct mg_server_ports *ports); + + +#if defined(MG_LEGACY_INTERFACE) +/* Deprecated: Use mg_get_server_ports instead. */ +CIVETWEB_API size_t +mg_get_ports(const struct mg_context *ctx, size_t size, int *ports, int *ssl); +#endif + + +/* Add, edit or delete the entry in the passwords file. + * + * This function allows an application to manipulate .htpasswd files on the + * fly by adding, deleting and changing user records. This is one of the + * several ways of implementing authentication on the server side. For another, + * cookie-based way please refer to the examples/chat in the source tree. + * + * Parameter: + * passwords_file_name: Path and name of a file storing multiple passwords + * realm: HTTP authentication realm (authentication domain) name + * user: User name + * password: + * If password is not NULL, entry modified or added. + * If password is NULL, entry is deleted. + * + * Return: + * 1 on success, 0 on error. + */ +CIVETWEB_API int mg_modify_passwords_file(const char *passwords_file_name, + const char *realm, + const char *user, + const char *password); + + +/* Return information associated with the request. + * Use this function to implement a server and get data about a request + * from a HTTP/HTTPS client. + * Note: Before CivetWeb 1.10, this function could be used to read + * a response from a server, when implementing a client, although the + * values were never returned in appropriate mg_request_info elements. + * It is strongly advised to use mg_get_response_info for clients. + */ +CIVETWEB_API const struct mg_request_info * +mg_get_request_info(const struct mg_connection *); + +/* Return the local address (server side) of the socket for a connection */ +CIVETWEB_API struct sockaddr * +mg_get_local_addr(struct mg_connection *); + +/* Return information associated with a HTTP/HTTPS response. + * Use this function in a client, to check the response from + * the server. */ +CIVETWEB_API const struct mg_response_info * +mg_get_response_info(const struct mg_connection *); + + +/* Send data to the client. + Return: + 0 when the connection has been closed + -1 on error + >0 number of bytes written on success */ +CIVETWEB_API int mg_write(struct mg_connection *, const void *buf, size_t len); + + +/* Send data to a websocket client wrapped in a websocket frame. Uses + mg_lock_connection to ensure that the transmission is not interrupted, + i.e., when the application is proactively communicating and responding to + a request simultaneously. + + Send data to a websocket client wrapped in a websocket frame. + This function is available when civetweb is compiled with -DUSE_WEBSOCKET + + Return: + 0 when the connection has been closed + -1 on error + >0 number of bytes written on success */ +CIVETWEB_API int mg_websocket_write(struct mg_connection *conn, + int opcode, + const char *data, + size_t data_len); + + +/* Send data to a websocket server wrapped in a masked websocket frame. Uses + mg_lock_connection to ensure that the transmission is not interrupted, + i.e., when the application is proactively communicating and responding to + a request simultaneously. + + Send data to a websocket server wrapped in a masked websocket frame. + This function is available when civetweb is compiled with -DUSE_WEBSOCKET + + Return: + 0 when the connection has been closed + -1 on error + >0 number of bytes written on success */ +CIVETWEB_API int mg_websocket_client_write(struct mg_connection *conn, + int opcode, + const char *data, + size_t data_len); + + +/* Blocks until unique access is obtained to this connection. Intended for use + with websockets only. + Invoke this before mg_write or mg_printf when communicating with a + websocket if your code has server-initiated communication as well as + communication in direct response to a message. */ +CIVETWEB_API void mg_lock_connection(struct mg_connection *conn); +CIVETWEB_API void mg_unlock_connection(struct mg_connection *conn); + + +#if defined(MG_LEGACY_INTERFACE) +#define mg_lock mg_lock_connection +#define mg_unlock mg_unlock_connection +#endif + + +/* Lock server context. This lock may be used to protect resources + that are shared between different connection/worker threads. */ +CIVETWEB_API void mg_lock_context(struct mg_context *ctx); +CIVETWEB_API void mg_unlock_context(struct mg_context *ctx); + + +/* Opcodes, from http://tools.ietf.org/html/rfc6455 */ +/* Old nomenclature */ +enum { + WEBSOCKET_OPCODE_CONTINUATION = 0x0, + WEBSOCKET_OPCODE_TEXT = 0x1, + WEBSOCKET_OPCODE_BINARY = 0x2, + WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8, + WEBSOCKET_OPCODE_PING = 0x9, + WEBSOCKET_OPCODE_PONG = 0xa +}; + +/* New nomenclature */ +enum { + MG_WEBSOCKET_OPCODE_CONTINUATION = 0x0, + MG_WEBSOCKET_OPCODE_TEXT = 0x1, + MG_WEBSOCKET_OPCODE_BINARY = 0x2, + MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8, + MG_WEBSOCKET_OPCODE_PING = 0x9, + MG_WEBSOCKET_OPCODE_PONG = 0xa +}; + +/* Macros for enabling compiler-specific checks for printf-like arguments. */ +#undef PRINTF_FORMAT_STRING +#if defined(_MSC_VER) && _MSC_VER >= 1400 +#include +#if defined(_MSC_VER) && _MSC_VER > 1400 +#define PRINTF_FORMAT_STRING(s) _Printf_format_string_ s +#else +#define PRINTF_FORMAT_STRING(s) __format_string s +#endif +#else +#define PRINTF_FORMAT_STRING(s) s +#endif + +#ifdef __GNUC__ +#define PRINTF_ARGS(x, y) __attribute__((format(printf, x, y))) +#else +#define PRINTF_ARGS(x, y) +#endif + + +/* Send data to the client using printf() semantics. + Works exactly like mg_write(), but allows to do message formatting. */ +CIVETWEB_API int mg_printf(struct mg_connection *, + PRINTF_FORMAT_STRING(const char *fmt), + ...) PRINTF_ARGS(2, 3); + + +/* Send a part of the message body, if chunked transfer encoding is set. + * Only use this function after sending a complete HTTP request or response + * header with "Transfer-Encoding: chunked" set. */ +CIVETWEB_API int mg_send_chunk(struct mg_connection *conn, + const char *chunk, + unsigned int chunk_len); + + +/* Send contents of the entire file together with HTTP headers. */ +CIVETWEB_API void mg_send_file(struct mg_connection *conn, const char *path); + + +/* Send HTTP error reply. */ +CIVETWEB_API void mg_send_http_error(struct mg_connection *conn, + int status_code, + PRINTF_FORMAT_STRING(const char *fmt), + ...) PRINTF_ARGS(3, 4); + + +/* Send HTTP digest access authentication request. + * Browsers will send a user name and password in their next request, showing + * an authentication dialog if the password is not stored. + * Parameters: + * conn: Current connection handle. + * realm: Authentication realm. If NULL is supplied, the sever domain + * set in the authentication_domain configuration is used. + * Return: + * < 0 Error + */ +CIVETWEB_API int +mg_send_digest_access_authentication_request(struct mg_connection *conn, + const char *realm); + + +/* Check if the current request has a valid authentication token set. + * A file is used to provide a list of valid user names, realms and + * password hashes. The file can be created and modified using the + * mg_modify_passwords_file API function. + * Parameters: + * conn: Current connection handle. + * realm: Authentication realm. If NULL is supplied, the sever domain + * set in the authentication_domain configuration is used. + * filename: Path and name of a file storing multiple password hashes. + * Return: + * > 0 Valid authentication + * 0 Invalid authentication + * < 0 Error (all values < 0 should be considered as invalid + * authentication, future error codes will have negative + * numbers) + * -1 Parameter error + * -2 File not found + */ +CIVETWEB_API int +mg_check_digest_access_authentication(struct mg_connection *conn, + const char *realm, + const char *filename); + + +/* Send contents of the entire file together with HTTP headers. + * Parameters: + * conn: Current connection handle. + * path: Full path to the file to send. + * mime_type: Content-Type for file. NULL will cause the type to be + * looked up by the file extension. + */ +CIVETWEB_API void mg_send_mime_file(struct mg_connection *conn, + const char *path, + const char *mime_type); + + +/* Send contents of the entire file together with HTTP headers. + Parameters: + conn: Current connection information. + path: Full path to the file to send. + mime_type: Content-Type for file. NULL will cause the type to be + looked up by the file extension. + additional_headers: Additional custom header fields appended to the header. + Each header should start with an X-, to ensure it is + not included twice. + NULL does not append anything. +*/ +CIVETWEB_API void mg_send_mime_file2(struct mg_connection *conn, + const char *path, + const char *mime_type, + const char *additional_headers); + + +/* Store body data into a file. */ +CIVETWEB_API long long mg_store_body(struct mg_connection *conn, + const char *path); +/* Read entire request body and store it in a file "path". + Return: + < 0 Error + >= 0 Number of bytes stored in file "path". +*/ + + +/* Read data from the remote end, return number of bytes read. + Return: + 0 connection has been closed by peer. No more data could be read. + < 0 read error. No more data could be read from the connection. + > 0 number of bytes read into the buffer. */ +CIVETWEB_API int mg_read(struct mg_connection *, void *buf, size_t len); + + +/* Get the value of particular HTTP header. + + This is a helper function. It traverses request_info->http_headers array, + and if the header is present in the array, returns its value. If it is + not present, NULL is returned. */ +CIVETWEB_API const char *mg_get_header(const struct mg_connection *, + const char *name); + + +/* Get a value of particular form variable. + + Parameters: + data: pointer to form-uri-encoded buffer. This could be either POST data, + or request_info.query_string. + data_len: length of the encoded data. + var_name: variable name to decode from the buffer + dst: destination buffer for the decoded variable + dst_len: length of the destination buffer + + Return: + On success, length of the decoded variable. + On error: + -1 (variable not found). + -2 (destination buffer is NULL, zero length or too small to hold the + decoded variable). + + Destination buffer is guaranteed to be '\0' - terminated if it is not + NULL or zero length. */ +CIVETWEB_API int mg_get_var(const char *data, + size_t data_len, + const char *var_name, + char *dst, + size_t dst_len); + + +/* Get a value of particular form variable. + + Parameters: + data: pointer to form-uri-encoded buffer. This could be either POST data, + or request_info.query_string. + data_len: length of the encoded data. + var_name: variable name to decode from the buffer + dst: destination buffer for the decoded variable + dst_len: length of the destination buffer + occurrence: which occurrence of the variable, 0 is the first, 1 the + second... + this makes it possible to parse a query like + b=x&a=y&a=z which will have occurrence values b:0, a:0 and a:1 + + Return: + On success, length of the decoded variable. + On error: + -1 (variable not found). + -2 (destination buffer is NULL, zero length or too small to hold the + decoded variable). + + Destination buffer is guaranteed to be '\0' - terminated if it is not + NULL or zero length. */ +CIVETWEB_API int mg_get_var2(const char *data, + size_t data_len, + const char *var_name, + char *dst, + size_t dst_len, + size_t occurrence); + + +/* Fetch value of certain cookie variable into the destination buffer. + + Destination buffer is guaranteed to be '\0' - terminated. In case of + failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same + parameter. This function returns only first occurrence. + + Return: + On success, value length. + On error: + -1 (either "Cookie:" header is not present at all or the requested + parameter is not found). + -2 (destination buffer is NULL, zero length or too small to hold the + value). */ +CIVETWEB_API int mg_get_cookie(const char *cookie, + const char *var_name, + char *buf, + size_t buf_len); + + +/* Download data from the remote web server. + host: host name to connect to, e.g. "foo.com", or "10.12.40.1". + port: port number, e.g. 80. + use_ssl: wether to use SSL connection. + error_buffer, error_buffer_size: error message placeholder. + request_fmt,...: HTTP request. + Return: + On success, valid pointer to the new connection, suitable for mg_read(). + On error, NULL. error_buffer contains error message. + Example: + char ebuf[100]; + struct mg_connection *conn; + conn = mg_download("google.com", 80, 0, ebuf, sizeof(ebuf), + "%s", "GET / HTTP/1.0\r\nHost: google.com\r\n\r\n"); + */ +CIVETWEB_API struct mg_connection * +mg_download(const char *host, + int port, + int use_ssl, + char *error_buffer, + size_t error_buffer_size, + PRINTF_FORMAT_STRING(const char *request_fmt), + ...) PRINTF_ARGS(6, 7); + + +/* Close the connection opened by mg_download(). */ +CIVETWEB_API void mg_close_connection(struct mg_connection *conn); + + +#if defined(MG_LEGACY_INTERFACE) +/* File upload functionality. Each uploaded file gets saved into a temporary + file and MG_UPLOAD event is sent. + Return number of uploaded files. + Deprecated: Use mg_handle_form_request instead. */ +CIVETWEB_API int mg_upload(struct mg_connection *conn, + const char *destination_dir); +#endif + + +/* This structure contains callback functions for handling form fields. + It is used as an argument to mg_handle_form_request. */ +struct mg_form_data_handler { + /* This callback function is called, if a new field has been found. + * The return value of this callback is used to define how the field + * should be processed. + * + * Parameters: + * key: Name of the field ("name" property of the HTML input field). + * filename: Name of a file to upload, at the client computer. + * Only set for input fields of type "file", otherwise NULL. + * path: Output parameter: File name (incl. path) to store the file + * at the server computer. Only used if FORM_FIELD_STORAGE_STORE + * is returned by this callback. Existing files will be + * overwritten. + * pathlen: Length of the buffer for path. + * user_data: Value of the member user_data of mg_form_data_handler + * + * Return value: + * The callback must return the intended storage for this field + * (See FORM_FIELD_STORAGE_*). + */ + int (*field_found)(const char *key, + const char *filename, + char *path, + size_t pathlen, + void *user_data); + + /* If the "field_found" callback returned FORM_FIELD_STORAGE_GET, + * this callback will receive the field data. + * + * Parameters: + * key: Name of the field ("name" property of the HTML input field). + * value: Value of the input field. + * user_data: Value of the member user_data of mg_form_data_handler + * + * Return value: + * TODO: Needs to be defined. + */ + int (*field_get)(const char *key, + const char *value, + size_t valuelen, + void *user_data); + + /* If the "field_found" callback returned FORM_FIELD_STORAGE_STORE, + * the data will be stored into a file. If the file has been written + * successfully, this callback will be called. This callback will + * not be called for only partially uploaded files. The + * mg_handle_form_request function will either store the file completely + * and call this callback, or it will remove any partial content and + * not call this callback function. + * + * Parameters: + * path: Path of the file stored at the server. + * file_size: Size of the stored file in bytes. + * user_data: Value of the member user_data of mg_form_data_handler + * + * Return value: + * TODO: Needs to be defined. + */ + int (*field_store)(const char *path, long long file_size, void *user_data); + + /* User supplied argument, passed to all callback functions. */ + void *user_data; +}; + + +/* Return values definition for the "field_found" callback in + * mg_form_data_handler. */ +/* Old nomenclature */ +enum { + /* Skip this field (neither get nor store it). Continue with the + * next field. */ + FORM_FIELD_STORAGE_SKIP = 0x0, + /* Get the field value. */ + FORM_FIELD_STORAGE_GET = 0x1, + /* Store the field value into a file. */ + FORM_FIELD_STORAGE_STORE = 0x2, + /* Stop parsing this request. Skip the remaining fields. */ + FORM_FIELD_STORAGE_ABORT = 0x10 +}; + +/* New nomenclature */ +enum { + /* Skip this field (neither get nor store it). Continue with the + * next field. */ + MG_FORM_FIELD_STORAGE_SKIP = 0x0, + /* Get the field value. */ + MG_FORM_FIELD_STORAGE_GET = 0x1, + /* Store the field value into a file. */ + MG_FORM_FIELD_STORAGE_STORE = 0x2, + /* Stop parsing this request. Skip the remaining fields. */ + MG_FORM_FIELD_STORAGE_ABORT = 0x10 +}; + +/* Process form data. + * Returns the number of fields handled, or < 0 in case of an error. + * Note: It is possible that several fields are already handled successfully + * (e.g., stored into files), before the request handling is stopped with an + * error. In this case a number < 0 is returned as well. + * In any case, it is the duty of the caller to remove files once they are + * no longer required. */ +CIVETWEB_API int mg_handle_form_request(struct mg_connection *conn, + struct mg_form_data_handler *fdh); + + +/* Convenience function -- create detached thread. + Return: 0 on success, non-0 on error. */ +typedef void *(*mg_thread_func_t)(void *); +CIVETWEB_API int mg_start_thread(mg_thread_func_t f, void *p); + + +/* Return builtin mime type for the given file name. + For unrecognized extensions, "text/plain" is returned. */ +CIVETWEB_API const char *mg_get_builtin_mime_type(const char *file_name); + + +/* Get text representation of HTTP status code. */ +CIVETWEB_API const char * +mg_get_response_code_text(const struct mg_connection *conn, int response_code); + + +/* Return CivetWeb version. */ +CIVETWEB_API const char *mg_version(void); + + +/* URL-decode input buffer into destination buffer. + 0-terminate the destination buffer. + form-url-encoded data differs from URI encoding in a way that it + uses '+' as character for space, see RFC 1866 section 8.2.1 + http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt + Return: length of the decoded data, or -1 if dst buffer is too small. */ +CIVETWEB_API int mg_url_decode(const char *src, + int src_len, + char *dst, + int dst_len, + int is_form_url_encoded); + + +/* URL-encode input buffer into destination buffer. + returns the length of the resulting buffer or -1 + is the buffer is too small. */ +CIVETWEB_API int mg_url_encode(const char *src, char *dst, size_t dst_len); + + +/* MD5 hash given strings. + Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of + ASCIIz strings. When function returns, buf will contain human-readable + MD5 hash. Example: + char buf[33]; + mg_md5(buf, "aa", "bb", NULL); */ +CIVETWEB_API char *mg_md5(char buf[33], ...); + + +/* Print error message to the opened error log stream. + This utilizes the provided logging configuration. + conn: connection (not used for sending data, but to get perameters) + fmt: format string without the line return + ...: variable argument list + Example: + mg_cry(conn,"i like %s", "logging"); */ +CIVETWEB_API void mg_cry(const struct mg_connection *conn, + PRINTF_FORMAT_STRING(const char *fmt), + ...) PRINTF_ARGS(2, 3); + + +/* utility methods to compare two buffers, case insensitive. */ +CIVETWEB_API int mg_strcasecmp(const char *s1, const char *s2); +CIVETWEB_API int mg_strncasecmp(const char *s1, const char *s2, size_t len); + + /* set connection's http status */ +CIVETWEB_API void mg_set_http_status(struct mg_connection *conn, int status); + +/* Connect to a websocket as a client + Parameters: + host: host to connect to, i.e. "echo.websocket.org" or "192.168.1.1" or + "localhost" + port: server port + use_ssl: make a secure connection to server + error_buffer, error_buffer_size: buffer for an error message + path: server path you are trying to connect to, i.e. if connection to + localhost/app, path should be "/app" + origin: value of the Origin HTTP header + data_func: callback that should be used when data is received from the + server + user_data: user supplied argument + + Return: + On success, valid mg_connection object. + On error, NULL. Se error_buffer for details. +*/ +CIVETWEB_API struct mg_connection * +mg_connect_websocket_client(const char *host, + int port, + int use_ssl, + char *error_buffer, + size_t error_buffer_size, + const char *path, + const char *origin, + mg_websocket_data_handler data_func, + mg_websocket_close_handler close_func, + void *user_data); + + +/* Connect to a TCP server as a client (can be used to connect to a HTTP server) + Parameters: + host: host to connect to, i.e. "www.wikipedia.org" or "192.168.1.1" or + "localhost" + port: server port + use_ssl: make a secure connection to server + error_buffer, error_buffer_size: buffer for an error message + + Return: + On success, valid mg_connection object. + On error, NULL. Se error_buffer for details. +*/ +CIVETWEB_API struct mg_connection *mg_connect_client(const char *host, + int port, + int use_ssl, + char *error_buffer, + size_t error_buffer_size); + + +struct mg_client_options { + const char *host; + int port; + const char *client_cert; + const char *server_cert; + /* TODO: add more data */ +}; + + +CIVETWEB_API struct mg_connection * +mg_connect_client_secure(const struct mg_client_options *client_options, + char *error_buffer, + size_t error_buffer_size); + + +enum { TIMEOUT_INFINITE = -1 }; +enum { MG_TIMEOUT_INFINITE = -1 }; + +/* Wait for a response from the server + Parameters: + conn: connection + ebuf, ebuf_len: error message placeholder. + timeout: time to wait for a response in milliseconds (if < 0 then wait + forever) + + Return: + On success, >= 0 + On error/timeout, < 0 +*/ +CIVETWEB_API int mg_get_response(struct mg_connection *conn, + char *ebuf, + size_t ebuf_len, + int timeout); + + +/* Check which features where set when the civetweb library has been compiled. + The function explicitly addresses compile time defines used when building + the library - it does not mean, the feature has been initialized using a + mg_init_library call. + mg_check_feature can be called anytime, even before mg_init_library has + been called. + + Parameters: + feature: specifies which feature should be checked + The value is a bit mask. The individual bits are defined as: + 1 serve files (NO_FILES not set) + 2 support HTTPS (NO_SSL not set) + 4 support CGI (NO_CGI not set) + 8 support IPv6 (USE_IPV6 set) + 16 support WebSocket (USE_WEBSOCKET set) + 32 support Lua scripts and Lua server pages (USE_LUA is set) + 64 support server side JavaScript (USE_DUKTAPE is set) + 128 support caching (NO_CACHING not set) + 256 support server statistics (USE_SERVER_STATS is set) + The result is undefined, if bits are set that do not represent a + defined feature (currently: feature >= 512). + The result is undefined, if no bit is set (feature == 0). + + Return: + If feature is available, the corresponding bit is set + If feature is not available, the bit is 0 +*/ +CIVETWEB_API unsigned mg_check_feature(unsigned feature); + + +/* Get information on the system. Useful for support requests. + Parameters: + buffer: Store system information as string here. + buflen: Length of buffer (including a byte required for a terminating 0). + Return: + Available size of system information, exluding a terminating 0. + The information is complete, if the return value is smaller than buflen. + The result is a JSON formatted string, the exact content may vary. + Note: + It is possible to determine the required buflen, by first calling this + function with buffer = NULL and buflen = NULL. The required buflen is + one byte more than the returned value. +*/ +CIVETWEB_API int mg_get_system_info(char *buffer, int buflen); + + +/* Get context information. Useful for server diagnosis. + Parameters: + ctx: Context handle + buffer: Store context information here. + buflen: Length of buffer (including a byte required for a terminating 0). + Return: + Available size of system information, exluding a terminating 0. + The information is complete, if the return value is smaller than buflen. + The result is a JSON formatted string, the exact content may vary. + Note: + It is possible to determine the required buflen, by first calling this + function with buffer = NULL and buflen = NULL. The required buflen is + one byte more than the returned value. However, since the available + context information changes, you should allocate a few bytes more. +*/ +CIVETWEB_API int +mg_get_context_info(const struct mg_context *ctx, char *buffer, int buflen); + + +#ifdef MG_EXPERIMENTAL_INTERFACES +/* Get connection information. Useful for server diagnosis. + Parameters: + ctx: Context handle + idx: Connection index + buffer: Store context information here. + buflen: Length of buffer (including a byte required for a terminating 0). + Return: + Available size of system information, exluding a terminating 0. + The information is complete, if the return value is smaller than buflen. + The result is a JSON formatted string, the exact content may vary. + Note: + It is possible to determine the required buflen, by first calling this + function with buffer = NULL and buflen = NULL. The required buflen is + one byte more than the returned value. However, since the available + context information changes, you should allocate a few bytes more. +*/ +CIVETWEB_API int mg_get_connection_info(const struct mg_context *ctx, + int idx, + char *buffer, + int buflen); +#endif + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CIVETWEB_HEADER_INCLUDED */ diff --git a/src/civetweb/mingw.cmd b/src/civetweb/mingw.cmd new file mode 100644 index 000000000..4b26215f1 --- /dev/null +++ b/src/civetweb/mingw.cmd @@ -0,0 +1,884 @@ +:: Make sure the extensions are enabled +@verify other 2>nul +@setlocal EnableExtensions EnableDelayedExpansion +@if errorlevel 1 ( + @call :print_usage "Failed to enable extensions" + @exit /b 1 +) + +::Change the code page to unicode +@chcp 65001 1>nul 2>nul +@if errorlevel 1 ( + @call :print_usage "Failed to change the code page to unicode" + @exit /b 1 +) + +:: Set up some global variables +@set "script_name=%~nx0" +@set "script_folder=%~dp0" +@set "script_folder=%script_folder:~0,-1%" +@set "dependency_path=%TEMP%\mingw-build-dependencies" + +:: Check the command line parameters +@set logging_level=1 +:options_loop +@if [%1] == [] goto :options_parsed +@set "arg=%~1" +@set one=%arg:~0,1% +@set two=%arg:~0,2% +@set three=%arg:~0,3% +@if /i [%arg%] == [/?] ( + @call :print_usage "Downloads a specific version of MinGW" + @exit /b 0 +) +@if /i [%arg%] == [/q] set quiet=true +@if /i [%two%] == [/v] @if /i not [%three%] == [/ve] @call :verbosity "!arg!" +@if /i [%arg%] == [/version] set "version=%~2" & shift +@if /i [%arg%] == [/arch] set "arch=%~2" & shift +@if /i [%arg%] == [/exceptions] set "exceptions=%~2" & shift +@if /i [%arg%] == [/threading] set "threading=%~2" & shift +@if /i [%arg%] == [/revision] set "revision=%~2" & shift +@if /i not [!one!] == [/] ( + if not defined output_path ( + set output_path=!arg! + ) else ( + @call :print_usage "Too many output locations: !output_path! !arg!" ^ + "There should only be one output location" + @exit /b 1 + ) +) +@shift +@goto :options_loop +:options_parsed +@if defined quiet set logging_level=0 +@if not defined output_path set "output_path=%script_folder%\mingw-builds" +@set "output_path=%output_path:/=\%" + +:: Set up the logging +@set "log_folder=%output_path%\logs" +@call :iso8601 timestamp +@set "log_path=%log_folder%\%timestamp%.log" +@set log_keep=10 + +:: Get default architecture +@if not defined arch @call :architecture arch + +:: Only keep a certain amount of logs +@set /a "log_keep=log_keep-1" +@if not exist %log_folder% @mkdir %log_folder% +@for /f "skip=%log_keep%" %%f in ('dir /b /o-D /tc %log_folder%') do @( + @call :log 4 "Removing old log file %log_folder%\%%f" + del %log_folder%\%%f +) + +:: Set up some more global variables +@call :windows_version win_ver win_ver_major win_ver_minor win_ver_rev +@call :script_source script_source +@if [%script_source%] == [explorer] ( + set /a "logging_level=logging_level+1" +) + +:: Execute the main function +@call :main "%arch%" "%version%" "%threading%" "%exceptions%" "%revision%" +@if errorlevel 1 ( + @call :log 0 "Failed to download MinGW" + @call :log 0 "View the log at %log_path%" + @exit /b 1 +) + +:: Stop the script if the user double clicked +@if [%script_source%] == [explorer] ( + pause +) + +@endlocal +@goto :eof + +:: -------------------------- Functions start here ---------------------------- + +:main - Main function that performs the download +:: %1 - Target architecture +:: %2 - Version of MinGW to get [optional] +:: %3 - Threading model [optional] +:: %4 - Exception model [optional] +:: %5 - Package revision [optional] +@setlocal +@call :log 6 +@call :log 2 "Welcome to the MinGW download script" +@call :log 6 "------------------------------------" +@call :log 6 +@call :log 2 "This script downloads a specific version of MinGW" +@set "arch=%~1" +@if "%arch%" == "" @exit /b 1 +@set "version=%~2" +@set "threading=%~3" +@set "exceptions=%~4" +@set "revision=%~5" +@call :log 3 "arch = %arch%" +@call :log 3 "version = %version%" +@call :log 3 "exceptions = %exceptions%" +@call :log 3 "threading = %threading%" +@call :log 3 "revision = %revision%" +@call :repository repo +@if errorlevel 1 ( + @call :log 0 "Failed to get the MinGW-builds repository information" + @exit /b 1 +) +@call :resolve slug url "%repo%" "%arch%" "%version%" "%threading%" "%exceptions%" "%revision%" +@if errorlevel 1 ( + @call :log 0 "Failed to resolve the correct URL of MinGW" + @exit /b 1 +) +@call :unpack compiler_path "%url%" "%output_path%\mingw\%slug%" +@if errorlevel 1 ( + @call :log 0 "Failed to unpack the MinGW archive" + @exit /b 1 +) +@rmdir /s /q "%dependency_path%" +@echo.%compiler_path% +@endlocal +@goto :eof + +:repository - Gets the MinGW-builds repository +:: %1 - The return variable for the repository file path +@verify other 2>nul +@setlocal EnableDelayedExpansion +@if errorlevel 1 ( + @call :log 0 "Failed to enable extensions" + @exit /b 1 +) +@set "var=%~1" +@if "%var%" == "" @exit /b 1 +@call :log 7 +@call :log 2 "Getting MinGW repository information" +@set "url=http://downloads.sourceforge.net/project/mingw-w64/Toolchains targetting Win32/Personal Builds/mingw-builds/installer/repository.txt" +@call :log 6 +@call :log 1 "Downloading MinGW repository" +@set "file_path=%dependency_path%\mingw-repository.txt" +@call :download "%url%" "%file_path%" +@if errorlevel 1 ( + @call :log 0 "Failed to download the MinGW repository information" + @exit /b 1 +) +@set "repository_path=%dependency_path%\repository.txt" +@del "%repository_path%" 2>nul +@for /f "delims=| tokens=1-6,*" %%a in (%file_path%) do @( + @set "version=%%~a" + @set "version=!version: =!" + @set "arch=%%~b" + @set "arch=!arch: =!" + @set "threading=%%~c" + @set "threading=!threading: =!" + @set "exceptions=%%~d" + @set "exceptions=!exceptions: =!" + @set "revision=%%~e" + @set "revision=!revision: =!" + @set "revision=!revision:rev=!" + @set "url=%%~f" + @set "url=!url:%%20= !" + @for /l %%a in (1,1,32) do @if "!url:~-1!" == " " set url=!url:~0,-1! + @echo !arch!^|!version!^|!threading!^|!exceptions!^|!revision!^|!url!>> "%repository_path%" +) +@del "%file_path%" 2>nul +@endlocal & set "%var%=%repository_path%" +@goto :eof + +:resolve - Gets the MinGW-builds repository +:: %1 - The return variable for the MinGW slug +:: %2 - The return variable for the MinGW URL +:: %3 - The repository information to use +:: %4 - Target architecture +:: %5 - Version of MinGW to get [optional] +:: %6 - Threading model [optional] +:: %7 - Exception model [optional] +:: %8 - Package revision [optional] +@setlocal +@set "slug_var=%~1" +@if "%slug_var%" == "" @exit /b 1 +@set "url_var=%~2" +@if "%url_var%" == "" @exit /b 1 +@set "repository=%~3" +@if "%repository%" == "" @exit /b 1 +@set "arch=%~4" +@if "%arch%" == "" @exit /b 1 +@call :resolve_version version "%repository%" "%arch%" "%~5" +@if errorlevel 1 @exit /b 1 +@call :resolve_threading threading "%repository%" "%arch%" "%version%" "%~6" +@if errorlevel 1 @exit /b 1 +@call :resolve_exceptions exceptions "%repository%" "%arch%" "%version%" "%threading%" "%~7" +@if errorlevel 1 @exit /b 1 +@call :resolve_revision revision "%repository%" "%arch%" "%version%" "%threading%" "%exceptions%" "%~8" +@if errorlevel 1 @exit /b 1 +@call :log 3 "Finding URL" +@for /f "delims=| tokens=1-6" %%a in (%repository%) do @( + @if "%arch%" == "%%a" ( + @if "%version%" == "%%b" ( + @if "%threading%" == "%%c" ( + @if "%exceptions%" == "%%d" ( + @if "%revision%" == "%%e" ( + @set "url=%%f" +) ) ) ) ) ) +@if "%url%" == "" ( + @call :log 0 "Failed to resolve URL" + @exit /b 1 +) +@set slug=gcc-%version%-%arch%-%threading%-%exceptions%-rev%revision% +@call :log 2 "Resolved slug: %slug%" +@call :log 2 "Resolved url: %url%" +@endlocal & set "%slug_var%=%slug%" & set "%url_var%=%url%" +@goto :eof + +:unpack - Unpacks the MinGW archive +:: %1 - The return variable name for the compiler path +:: %2 - The filepath or URL of the archive +:: %3 - The folder to unpack to +@verify other 2>nul +@setlocal EnableDelayedExpansion +@if errorlevel 1 ( + @call :log 0 "Failed to enable extensions" + @exit /b 1 +) +@set "var=%~1" +@if "%var%" == "" @exit /b 1 +@set "archive_path=%~2" +@if "%archive_path%" == "" @exit /b 1 +@set "folder_path=%~3" +@if "%folder_path%" == "" @exit /b 1 +@set "compiler_path=%folder_path%\bin" +@if exist "%compiler_path%" goto :unpack_done +@call :log 7 +@call :log 2 "Unpacking MinGW archive" +@set "http=%archive_path:~0,4%" +@if "%http%" == "http" ( + @set "url=%archive_path%" + @for /f %%a in ("!url: =-!") do @set "file_name=%%~na" + @for /f %%a in ("!url: =-!") do @set "file_ext=%%~xa" + @set "archive_path=%dependency_path%\!file_name!!file_ext!" + @if not exist "!archive_path!" ( + @call :log 6 + @call :log 1 "Downloading MinGW archive" + @call :download "!url!" "!archive_path!" + @if errorlevel 1 ( + @del "!archive_path!" 2>nul + @call :log 0 "Failed to download: !file_name!!file_ext!" + @exit /b 1 + ) + ) +) +@if not exist "%archive_path%" ( + @call :log 0 "The archive did not exist to unpack: %archive_path%" + @exit /b 1 +) +@for /f %%a in ("%archive_path: =-%") do @set "file_name=%%~na" +@for /f %%a in ("%archive_path: =-%") do @set "file_ext=%%~xa" +@call :log 6 +@call :log 1 "Unpacking MinGW %file_name%%file_ext%" +@call :find_sevenzip sevenzip_executable +@if errorlevel 1 ( + @call :log 0 "Need 7zip to unpack the MinGW archive" + @exit /b 1 +) +@call :iso8601 iso8601 +@for /f %%a in ("%folder_path%") do @set "tmp_path=%%~dpatmp-%iso8601%" +@"%sevenzip_executable%" x -y "-o%tmp_path%" "%archive_path%" > nul +@if errorlevel 1 ( + @rmdir /s /q "%folder_path%" + @call :log 0 "Failed to unpack the MinGW archive" + @exit /b 1 +) +@set "expected_path=%tmp_path%\mingw64" +@if not exist "%expected_path%" ( + @set "expected_path=%tmp_path%\mingw32" +) +@move /y "%expected_path%" "%folder_path%" > nul +@if errorlevel 1 ( + @rmdir /s /q "%tmp_path%" 2>nul + @call :log 0 "Failed to move MinGW folder" + @call :log 0 "%expected_path%" + @call :log 0 "%folder_path%" + @exit /b 1 +) +@rmdir /s /q %tmp_path% +@set "compiler_path=%folder_path%\bin" +:unpack_done +@if not exist "%compiler_path%\gcc.exe" ( + @call :log 0 "Failed to find gcc: %compiler_path%" + @exit /b 1 +) +@endlocal & set "%var%=%compiler_path%" +@goto :eof + +:find_sevenzip - Finds (or downloads) the 7zip executable +:: %1 - The return variable for the 7zip executable path +@setlocal +@set "var=%~1" +@if "%var%" == "" @exit /b 1 +@call :log 2 "Finding 7zip" +@call :find_in_path sevenzip_executable 7z.exe +@if not errorlevel 1 goto :find_sevenzip_done +@call :find_in_path sevenzip_executable 7za.exe +@if not errorlevel 1 goto :find_sevenzip_done +@set checksum=2FAC454A90AE96021F4FFC607D4C00F8 +@set "url=http://7-zip.org/a/7za920.zip" +@for /f %%a in ("%url: =-%") do @set "file_name=%%~na" +@for /f %%a in ("%url: =-%") do @set "file_ext=%%~xa" +@set "archive_path=%dependency_path%\%file_name%%file_ext%" +@if not exist "%archive_path%" ( + @call :log 6 + @call :log 1 "Downloading 7zip archive" + @call :download "%url%" "%archive_path%" %checksum% + @if errorlevel 1 ( + @del "%archive_path%" 2>nul + @call :log 0 "Failed to download: %file_name%%file_ext%" + @exit /b 1 + ) +) +@set "sevenzip_path=%dependency_path%\sevenzip" +@if not exist "%sevenzip_path%" ( + @call :unzip "%archive_path%" "%sevenzip_path%" + @if errorlevel 1 ( + @call :log 0 "Failed to unzip the7zip archive" + @exit /b 1 + ) +) +@set "sevenzip_executable=%sevenzip_path%\7za.exe" +@if not exist "%sevenzip_executable%" ( + @call :log 0 "Failed to find unpacked 7zip: %sevenzip_executable%" + @exit /b 1 +) +:find_sevenzip_done +@call :log 2 "Found 7zip: %sevenzip_executable%" +@endlocal & set "%var%=%sevenzip_executable%" +@goto :eof + +:unzip - Unzips a .zip archive +:: %1 - The archive to unzip +:: %2 - The location to unzip to +@setlocal +@set "archive_path=%~1" +@if "%archive_path%" == "" @exit /b 1 +@set "folder_path=%~2" +@if "%folder_path%" == "" @exit /b 1 +@for /f %%a in ("%archive_path: =-%") do @set "file_name=%%~na" +@for /f %%a in ("%archive_path: =-%") do @set "file_ext=%%~xa" +@call :log 2 "Unzipping: %file_name%%file_ext%" +@call :iso8601 iso8601 +@set "log_path=%temp%\unzip-%iso8601%-%file_name%.log" +@powershell ^ + Add-Type -assembly "system.io.compression.filesystem"; ^ + [io.compression.zipfile]::ExtractToDirectory(^ + '%archive_path%', '%folder_path%') 2>"%log_path%" +@if errorlevel 1 ( + @call :log 0 "Failed to unzip: %file_name%%file_ext%" + @call :log_append "%log_path%" + @exit /b 1 +) +@endlocal +@goto :eof + +:resolve_version - Gets the version of the MinGW compiler +:: %1 - The return variable for the version +:: %2 - The repository information to use +:: %3 - The architecture of the compiler +:: %4 - Version of MinGW to get [optional] +@verify other 2>nul +@setlocal EnableDelayedExpansion +@if errorlevel 1 ( + @call :log 0 "Failed to enable extensions" + @exit /b 1 +) +@set "var=%~1" +@if "%var%" == "" @exit /b 1 +@set "repository=%~2" +@if "%repository%" == "" @exit /b 1 +@set "arch=%~3" +@if "%arch%" == "" @exit /b 1 +@set "version=%~4" +@if not "%version%" == "" goto :resolve_version_done +:: Find the latest version +@call :log 3 "Finding latest version" +@set version=0.0.0 +@for /f "delims=| tokens=1-6" %%a in (%repository%) do @( + @if "%arch%" == "%%a" ( + @call :version_compare result "%version%" "%%b" + @if errorlevel 1 ( + @call :log 0 "Failed to compare versions: %version% %%a" + @exit /b 1 + ) + @if !result! lss 0 set version=%%b + ) +) +:resolve_version_done +@if "%version%" == "" ( + @call :log 0 "Failed to resolve latest version number" + @exit /b 1 +) +@call :log 2 "Resolved version: %version%" +@endlocal & set "%var%=%version%" +@goto :eof + +:resolve_threading - Gets the threading model of the MinGW compiler +:: %1 - The return variable for the threading model +:: %2 - The repository information to use +:: %3 - The architecture of the compiler +:: %4 - The version of the compiler +:: %5 - threading model of MinGW to use [optional] +@verify other 2>nul +@setlocal EnableDelayedExpansion +@if errorlevel 1 ( + @call :log 0 "Failed to enable extensions" + @exit /b 1 +) +@set "var=%~1" +@if "%var%" == "" @exit /b 1 +@set "repository=%~2" +@if "%repository%" == "" @exit /b 1 +@set "arch=%~3" +@if "%arch%" == "" @exit /b 1 +@set "version=%~4" +@if "%version%" == "" @exit /b 1 +@set "threading=%~5" +@if not "%threading%" == "" goto :resolve_threading_done +@call :log 3 "Finding best threading model" +@for /f "delims=| tokens=1-6" %%a in (%repository%) do @( + @if "%arch%" == "%%a" ( + @if "%version%" == "%%b" ( + @if not defined threading ( + @set "threading=%%c" + ) + @if "%%c" == "posix" ( + @set "threading=%%c" +) ) ) ) +:resolve_threading_done +@if "%threading%" == "" ( + @call :log 0 "Failed to resolve the best threading model" + @exit /b 1 +) +@call :log 2 "Resolved threading model: %threading%" +@endlocal & set "%var%=%threading%" +@goto :eof + +:resolve_exceptions - Gets the exception model of the MinGW compiler +:: %1 - The return variable for the exception model +:: %2 - The repository information to use +:: %3 - The architecture of the compiler +:: %4 - The version of the compiler +:: %4 - The threading model of the compiler +:: %5 - exception model of MinGW to use [optional] +@verify other 2>nul +@setlocal EnableDelayedExpansion +@if errorlevel 1 ( + @call :log 0 "Failed to enable extensions" + @exit /b 1 +) +@set "var=%~1" +@if "%var%" == "" @exit /b 1 +@set "repository=%~2" +@if "%repository%" == "" @exit /b 1 +@set "arch=%~3" +@if "%arch%" == "" @exit /b 1 +@set "version=%~4" +@if "%version%" == "" @exit /b 1 +@set "threading=%~5" +@if "%threading%" == "" @exit /b 1 +@set "exceptions=%~6" +@if not "%exceptions%" == "" goto :resolve_exceptions_done +@call :log 3 "Finding best exception model" +@for /f "delims=| tokens=1-6" %%a in (%repository%) do @( + @if "%arch%" == "%%a" ( + @if "%version%" == "%%b" ( + @if "%threading%" == "%%c" ( + @if not defined exceptions ( + @set "exceptions=%%d" + ) + @if "%%d" == "dwarf" ( + @set "exceptions=%%d" + ) + @if "%%d" == "seh" ( + @set "exceptions=%%d" +) ) ) ) ) +:resolve_exceptions_done +@if "%exceptions%" == "" ( + @call :log 0 "Failed to resolve the best exception model" + @exit /b 1 +) +@call :log 2 "Resolved exception model: %exceptions%" +@endlocal & set "%var%=%exceptions%" +@goto :eof + +:resolve_revision - Gets the revision of the MinGW compiler +:: %1 - The return variable for the revision +:: %2 - The repository information to use +:: %3 - The architecture of the compiler +:: %4 - The version of the compiler +:: %4 - The threading model of the compiler +:: %4 - The exception model of the compiler +:: %5 - revision of the MinGW package to use [optional] +@verify other 2>nul +@setlocal EnableDelayedExpansion +@if errorlevel 1 ( + @call :log 0 "Failed to enable extensions" + @exit /b 1 +) +@set "var=%~1" +@if "%var%" == "" @exit /b 1 +@set "repository=%~2" +@if "%repository%" == "" @exit /b 1 +@set "arch=%~3" +@if "%arch%" == "" @exit /b 1 +@set "version=%~4" +@if "%version%" == "" @exit /b 1 +@set "threading=%~5" +@if "%threading%" == "" @exit /b 1 +@set "exceptions=%~6" +@if "%exceptions%" == "" @exit /b 1 +@set "revision=%~7" +@if not "%revision%" == "" goto :resolve_revision_done +@call :log 3 "Finding latest revision" +@for /f "delims=| tokens=1-6" %%a in (%repository%) do @( + @if "%arch%" == "%%a" ( + @if "%version%" == "%%b" ( + @if "%threading%" == "%%c" ( + @if "%exceptions%" == "%%d" ( + @if "%%e" gtr "%revision%" ( + @set "revision=%%e" +) ) ) ) ) ) +:resolve_revision_done +@if "%revision%" == "" ( + @call :log 0 "Failed to resolve latest revision" + @exit /b 1 +) +@call :log 2 "Resolved revision: %revision%" +@endlocal & set "%var%=%revision%" +@goto :eof + +:version_compare - Compares two semantic version numbers +:: %1 - The return variable: +:: - < 0 : if %2 < %3 +:: - 0 : if %2 == %3 +:: - > 0 : if %2 > %3 +:: %2 - The first version to compare +:: %3 - The second version to compare +@setlocal +@set "var=%~1" +@if "%var%" == "" @exit /b 1 +@set "lhs=%~2" +@if "%lhs%" == "" @exit /b 1 +@set "rhs=%~3" +@if "%lhs%" == "" @exit /b 1 +@set result=0 +@for /f "delims=. tokens=1-6" %%a in ("%lhs%.%rhs%") do @( + @if %%a lss %%d ( + set result=-1 + goto :version_compare_done + ) else ( + @if %%a gtr %%d ( + set result=1 + goto :version_compare_done + ) else ( + @if %%b lss %%e ( + set result=-1 + goto :version_compare_done + ) else ( + @if %%b gtr %%e ( + set result=1 + goto :version_compare_done + ) else ( + @if %%c lss %%f ( + set result=-1 + goto :version_compare_done + ) else ( + @if %%c gtr %%f ( + set result=1 + goto :version_compare_done + ) + ) + ) + ) + ) + ) +) +:version_compare_done +@endlocal & set "%var%=%result%" +@goto :eof + +:print_usage - Prints the usage of the script +:: %* - message to print, each argument on it's own line +@setlocal +@for %%a in (%*) do @echo.%%~a +@echo. +@echo.build [/?][/v[v...]^|/q][/version][/arch a][/threading t] +@echo. [/exceptions e][/revision r] location +@echo. +@echo. /version v The version of MinGW to download +@echo. /arch a The target architecture [i686^|x86_64] +@echo. /threading t +@echo. Threading model to use [posix^|win32] +@echo. /exceptions e +@echo. Exception model to use [sjlj^|seh^|dwarf] +@echo. /revision e Revision of the release to use +@echo. /v Sets the output to be more verbose +@echo. /v[v...] Extra verbosity, /vv, /vvv, etc +@echo. /q Quiets the output +@echo. /? Shows this usage message +@echo. +@endlocal +@goto :eof + +:script_source - Determines if the script was ran from the cli or explorer +:: %1 - The return variable [cli|explorer] +@verify other 2>nul +@setlocal EnableDelayedExpansion +@if errorlevel 1 ( + @call :log 0 "Failed to enable extensions" + @exit /b 1 +) +@call :log 3 "Attempting to detect the script source" +@echo "The invocation command was: '%cmdcmdline%'" >> %log_path% +@for /f "tokens=1-3,*" %%a in ("%cmdcmdline%") do @( + set cmd=%%~a + set arg1=%%~b + set arg2=%%~c + set rest=%%~d +) +@set quote=" +@if "!arg2:~0,1!" equ "!quote!" ( + if "!arg2:~-1!" neq "!quote!" ( + set "arg2=!arg2:~1!" + ) +) +@call :log 4 "cmd = %cmd%" +@call :log 4 "arg1 = %arg1%" +@call :log 4 "arg2 = %arg2%" +@call :log 4 "rest = %rest%" +@call :log 4 "src = %~f0" +@if /i "%arg2%" == "call" ( + set script_source=cli +) else ( + @if /i "%arg1%" == "/c" ( + set script_source=explorer + ) else ( + set script_source=cli + ) +) +@call :log 3 "The script was invoked from %script_source%" +@endlocal & set "%~1=%script_source%" +@goto :eof + +:architecture - Finds the system architecture +:: %1 - The return variable [i686|x86_64] +@setlocal +@call :log 3 "Determining the processor architecture" +@set "key=HKLM\System\CurrentControlSet\Control\Session Manager\Environment" +@set "var=PROCESSOR_ARCHITECTURE" +@for /f "skip=2 tokens=2,*" %%a in ('reg query "%key%" /v "%var%"') do @set "arch=%%b" +@if "%arch%" == "AMD64" set arch=x86_64 +@if "%arch%" == "x64" set arch=i686 +@call :log 4 "arch = %arch%" +@endlocal & set "%~1=%arch%" +@goto :eof + +:md5 - Gets the MD5 checksum for a file +:: %1 - The hash +:: %2 - The file path +@setlocal +@set "var=%~1" +@set "file_path=%~2" +@if "%var%" == "" @exit /b 1 +@if "%file_path%" == "" @exit /b 1 +@if not exist "%file_path%" @exit /b 1 +@for /f "skip=3 tokens=1,*" %%a in ('powershell Get-FileHash -Algorithm MD5 "'%file_path%'"') do @set hash=%%b +@if not defined hash ( + @call :log 6 + @call :log 0 "Failed to get MD5 hash for %file_path%" + @exit /b 1 +) +@endlocal & set "%var%=%hash: =%" +@goto :eof + +:windows_version - Checks the windows version +:: %1 - The windows version +:: %2 - The major version number return variable +:: %3 - The minor version number return variable +:: %4 - The revision version number return variable +@setlocal +@call :log 3 "Retrieving the Windows version" +@for /f "tokens=2 delims=[]" %%x in ('ver') do @set win_ver=%%x +@set win_ver=%win_ver:Version =% +@set win_ver_major=%win_ver:~0,1% +@set win_ver_minor=%win_ver:~2,1% +@set win_ver_rev=%win_ver:~4% +@call :log 4 "win_ver = %win_ver%" +@endlocal & set "%~1=%win_ver%" ^ + & set "%~2=%win_ver_major%" ^ + & set "%~3=%win_ver_minor%" ^ + & set "%~4=%win_ver_rev%" +@goto :eof + +:find_in_path - Finds a program of file in the PATH +:: %1 - return variable of the file path +@setlocal +@set "var=%~1" +@if "%var%" == "" @exit /b 1 +@set "file=%~2" +@if "%file%" == "" @exit /b 1 +@call :log 3 "Searching PATH for %file%" +@for %%x in ("%file%") do @set "file_path=%%~f$PATH:x" +@if not defined file_path @exit /b 1 +@endlocal & set "%var%=%file_path%" +@goto :eof + +:log_append - Appends another file into the current logging file +:: %1 - the file_path to the file to concatenate +@setlocal +@set "file_path=%~1" +@if "%file_path%" == "" @exit /b 1 +@call :log 3 "Appending to log: %file_path%" +@call :iso8601 iso8601 +@set temp_log=%temp%\append-%iso8601%.log +@call :log 4 "Using temp file %temp_log%" +@type "%log_path%" "%file_path%" > "%temp_log%" 2>nul +@move /y "%temp_log%" "%log_path%" 1>nul +@del "%file_path% 2>nul +@del "%temp_log% 2>nul +@endlocal +@goto :eof + +:iso8601 - Returns the current time in ISO8601 format +:: %1 - the return variable +:: %2 - format [extended|basic*] +:: iso8601 - contains the resulting timestamp +@setlocal +@wmic Alias /? >NUL 2>&1 || @exit /b 1 +@set "var=%~1" +@if "%var%" == "" @exit /b 1 +@set "format=%~2" +@if "%format%" == "" set format=basic +@for /F "skip=1 tokens=1-6" %%g IN ('wmic Path Win32_UTCTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') do @( + @if "%%~l"=="" goto :iso8601_done + @set "yyyy=%%l" + @set "mm=00%%j" + @set "dd=00%%g" + @set "hour=00%%h" + @set "minute=00%%i" + @set "seconds=00%%k" +) +:iso8601_done +@set mm=%mm:~-2% +@set dd=%dd:~-2% +@set hour=%hour:~-2% +@set minute=%minute:~-2% +@set seconds=%seconds:~-2% +@if /i [%format%] == [extended] ( + set iso8601=%yyyy%-%mm%-%dd%T%hour%:%minute%:%seconds%Z +) else ( + if /i [%format%] == [basic] ( + set iso8601=%yyyy%%mm%%dd%T%hour%%minute%%seconds%Z + ) else ( + @exit /b 1 + ) +) +@set iso8601=%iso8601: =0% +@endlocal & set %var%=%iso8601% +@goto :eof + +:verbosity - Processes the verbosity parameter '/v[v...] +:: %1 - verbosity given on the command line +:: logging_level - set to the number of v's +@setlocal +@set logging_level=0 +@set verbosity=%~1 +:verbosity_loop +@set verbosity=%verbosity:~1% +@if not [%verbosity%] == [] @( + set /a "logging_level=logging_level+1" + goto verbosity_loop +) +@endlocal & set logging_level=%logging_level% +@goto :eof + +:log - Logs a message, depending on verbosity +:: %1 - level +:: [0-4] for CLI logging +:: [5-9] for GUI logging +:: %2 - message to print +@setlocal +@set "level=%~1" +@set "msg=%~2" +@if "%log_folder%" == "" ( + echo Logging was used to early in the script, log_folder isn't set yet + goto :eof +) +@if "%log_path%" == "" ( + echo Logging was used to early in the script, log_path isn't set yet + goto :eof +) +@if not exist "%log_folder%" mkdir "%log_folder%" +@if not exist "%log_path%" echo. 1>nul 2>"%log_path%" +@echo.%msg% >> "%log_path%" +@if %level% geq 5 ( + @if [%script_source%] == [explorer] ( + set /a "level=level-5" + ) else ( + @goto :eof + ) +) +@if "%logging_level%" == "" ( + echo Logging was used to early in the script, logging_level isn't set yet + goto :eof +) +@if %logging_level% geq %level% echo.%msg% 1>&2 +@endlocal +@goto :eof + +:download - Downloads a file from the internet +:: %1 - the url of the file to download +:: %2 - the file to download to +:: %3 - the MD5 checksum of the file (optional) +@setlocal EnableDelayedExpansion +@if errorlevel 1 ( + @call :print_usage "Failed to enable extensions" + @exit /b 1 +) +@set "url=%~1" +@set "file_path=%~2" +@set "checksum=%~3" +@for %%a in (%file_path%) do @set dir_path=%%~dpa +@for %%a in (%file_path%) do @set file_name=%%~nxa +@if "%url%" == "" @exit /b 1 +@if "%file_path%" == "" @exit /b 1 +@if "%dir_path%" == "" @exit /b 1 +@if "%file_name%" == "" @exit /b 1 +@if not exist "%dir_path%" mkdir "%dir_path%" +@call :log 2 "Downloading %url%" +@call :iso8601 iso8601 +@set "temp_path=%temp%\download-%iso8601%-%file_name%" +@set "log_path=%temp%\download-%iso8601%-log-%file_name%" +@call :log 4 "Using temp file %temp_path%" +@powershell Invoke-WebRequest "'%url%'" ^ + -OutFile "'%temp_path%'" ^ + -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::IE ^ + 1>nul 2>"%log_path%" +@if errorlevel 1 ( + @call :log 0 "Failed to download %url%" + @call :log_append "%log_path%" + @exit /b 1 +) +@if [%checksum%] neq [] ( + @call :log 4 "Checking %checksum% against %temp_path%" + @call :md5 hash "%temp_path%" + if "!hash!" neq "%checksum%" ( + @call :log 0 "Failed to match checksum: %temp_path%" + @call :log 0 "Hash : !hash!" + @call :log 0 "Checksum: %checksum%" + @exit /b 1 + ) else ( + @call :log 3 "Checksum matched: %temp_path%" + @call :log 3 "Hash : !hash!" + @call :log 3 "Checksum: %checksum%" + ) +) +@call :log 4 "Renaming %temp_path% to %file_path%" +@move /y "%temp_path%" "%file_path%" 1>nul +@endlocal +@goto :eof diff --git a/src/civetweb/resources/Info.plist b/src/civetweb/resources/Info.plist new file mode 100644 index 000000000..f02e19a61 --- /dev/null +++ b/src/civetweb/resources/Info.plist @@ -0,0 +1,21 @@ + + + + + CFBundleExecutable Civetweb + CFBundlePackageType APPL + CFBundleTypeRole None + CFBundleIconFile civetweb + CFBundleIconFiles + civetweb_16x16.png + civetweb_22x22.png + civetweb_32x32.png + civetweb_64x64.png + + LSUIElement + RunAtLoad + Label com.nofacepress.civetweb + ProgramArguments + KeepAlive + + diff --git a/src/civetweb/resources/Makefile.in-duktape b/src/civetweb/resources/Makefile.in-duktape new file mode 100644 index 000000000..3a53f2e3d --- /dev/null +++ b/src/civetweb/resources/Makefile.in-duktape @@ -0,0 +1,77 @@ +# +# Copyright (c) 2015-2017 the Civetweb developers +# +# License http://opensource.org/licenses/mit-license.php MIT License +# + +ifndef WITH_DUKTAPE + $(error WITH_DUKTAPE is not defined) +endif + +# Duktape default version is 1.5.2 (105) +WITH_DUKTAPE_VERSION ?= 105 +DUKTAPE_VERSION_KNOWN = 0 + +# Select src and header according to the Duktape version +ifeq ($(WITH_DUKTAPE_VERSION), 105) + $(info Duktape: Using version 1.5.2) + DUKTAPE_DIR = src/third_party/duktape-1.5.2/src + DUKTAPE_SHARED_LIB_FLAG = -lduktape1.5 + DUKTAPE_CFLAGS = -DDUKTAPE_VERSION_MAKEFILE=105 + DUKTAPE_VERSION_KNOWN = 1 +endif + +ifeq ($(WITH_DUKTAPE_VERSION), 108) + $(info Duktape: Using version 1.8.0) + DUKTAPE_DIR = src/third_party/duktape-1.8.0/src + DUKTAPE_SHARED_LIB_FLAG = -lduktape1.8 + DUKTAPE_CFLAGS = -DDUKTAPE_VERSION_MAKEFILE=108 + DUKTAPE_VERSION_KNOWN = 1 +endif + +ifeq ($(WITH_DUKTAPE_VERSION), 201) + $(info Duktape: Using version 2.1.1) + DUKTAPE_DIR = src/third_party/duktape-2.1.1/src + DUKTAPE_SHARED_LIB_FLAG = -lduktape2.1 + DUKTAPE_CFLAGS = -DDUKTAPE_VERSION_MAKEFILE=201 + DUKTAPE_VERSION_KNOWN = 1 +endif + +ifneq ($(DUKTAPE_VERSION_KNOWN), 1) + $(error Duktape: Unknwon version - $(WITH_DUKTAPE_VERSION)) +endif + + +# Add flags for all Duktape versions +DUKTAPE_CFLAGS += -I$(DUKTAPE_DIR) -DUSE_DUKTAPE + +ifneq ($(TARGET_OS),WIN32) +# DUKTAPE_CFLAGS += +endif + +ifdef WITH_DUKTAPE_SHARED + + DUKTAPE_SOURCE_FILES = + + $(info Duktape: using dynamic linking) + +else + + DUKTAPE_SOURCE_FILES = duktape.c + +ifeq ($(WITH_DUKTAPE_VERSION), 104) +# DUKTAPE_SOURCE_FILES += ... TODO ... +endif + + $(info Duktape: using static library) + +endif + +DUKTAPE_SOURCES = $(addprefix $(DUKTAPE_DIR)/, $(DUKTAPE_SOURCE_FILES)) +DUKTAPE_OBJECTS = $(DUKTAPE_SOURCES:.c=.o) + +OBJECTS += $(DUKTAPE_OBJECTS) +CFLAGS += $(DUKTAPE_CFLAGS) +SOURCE_DIRS = $(DUKTAPE_DIR) +BUILD_DIRS += $(BUILD_DIR)/$(DUKTAPE_DIR) + diff --git a/src/civetweb/resources/Makefile.in-lua b/src/civetweb/resources/Makefile.in-lua new file mode 100644 index 000000000..e91d019c9 --- /dev/null +++ b/src/civetweb/resources/Makefile.in-lua @@ -0,0 +1,149 @@ +# +# Copyright (c) 2013 No Face Press, LLC +# Copyright (c) 2014-2017 the Civetweb developers +# +# License http://opensource.org/licenses/mit-license.php MIT License +# + +ifndef WITH_LUA + $(error WITH_LUA is not defined) +endif + +# Lua Default version is 502 +WITH_LUA_VERSION ?= 502 +LUA_VERSION_KNOWN = 0 + +# Select src and header according to the Lua version +ifeq ($(WITH_LUA_VERSION), 501) + $(info Lua: Using version 5.1.5) + LUA_DIR = src/third_party/lua-5.1.5/src + LUA_SHARED_LIB_FLAG = -llua5.1 + LUA_CFLAGS = -DLUA_VERSION_MAKEFILE=501 + LUA_VERSION_KNOWN = 1 +endif +ifeq ($(WITH_LUA_VERSION), 502) + $(info Lua: Using version 5.2.4) + LUA_DIR = src/third_party/lua-5.2.4/src + LUA_SHARED_LIB_FLAG = -llua5.2 + LUA_CFLAGS = -DLUA_VERSION_MAKEFILE=502 + LUA_VERSION_KNOWN = 1 +endif +ifeq ($(WITH_LUA_VERSION), 503) + $(info Lua: Using version 5.3.3) + LUA_DIR = src/third_party/lua-5.3.3/src + LUA_SHARED_LIB_FLAG = -llua5.3 + LUA_CFLAGS = -DLUA_COMPAT_5_2 -DLUA_VERSION_MAKEFILE=503 + LUA_VERSION_KNOWN = 1 +endif + +ifneq ($(LUA_VERSION_KNOWN), 1) + $(error Lua: Unknwon version - $(WITH_LUA_VERSION)) +endif + + +# Add flags for all Lua versions +LUA_CFLAGS += -I$(LUA_DIR) -DLUA_COMPAT_ALL -DUSE_LUA + +ifneq ($(TARGET_OS),WIN32) + LUA_CFLAGS += -DLUA_USE_POSIX -DLUA_USE_DLOPEN +endif + +ifdef WITH_LUA_SHARED + + LUA_SOURCE_FILES = + + $(info Lua: using dynamic linking) + +else + + LUA_SOURCE_FILES = lapi.c \ + lauxlib.c \ + lbaselib.c \ + lcode.c \ + ldblib.c \ + ldebug.c \ + ldo.c \ + ldump.c \ + lfunc.c \ + lgc.c \ + linit.c \ + liolib.c \ + llex.c \ + lmathlib.c \ + lmem.c \ + loadlib.c \ + lobject.c \ + lopcodes.c \ + loslib.c \ + lparser.c \ + lstate.c \ + lstring.c \ + lstrlib.c \ + ltable.c \ + ltablib.c \ + ltm.c \ + lundump.c \ + lvm.c \ + lzio.c + +ifeq ($(WITH_LUA_VERSION), 502) + LUA_SOURCE_FILES += lbitlib.c \ + lcorolib.c \ + lctype.c +endif +ifeq ($(WITH_LUA_VERSION), 503) + LUA_SOURCE_FILES += lbitlib.c \ + lcorolib.c \ + lctype.c \ + lutf8lib.c +endif + + $(info Lua: using static library) + +endif + +LUA_SOURCES = $(addprefix $(LUA_DIR)/, $(LUA_SOURCE_FILES)) +LUA_OBJECTS = $(LUA_SOURCES:.c=.o) + +OBJECTS += $(LUA_OBJECTS) +CFLAGS += $(LUA_CFLAGS) +SOURCE_DIRS = $(LUA_DIR) +BUILD_DIRS += $(BUILD_DIR)/$(LUA_DIR) + + +ifneq ($(WITH_LUA_VERSION), 501) + SQLITE_DIR = src/third_party + SQLITE_SOURCE_FILES = sqlite3.c lsqlite3.c + SQLITE_SOURCES = $(addprefix $(SQLITE_DIR)/, $(SQLITE_SOURCE_FILES)) + SQLITE_OBJECTS = $(SQLITE_SOURCES:.c=.o) + SQLITE_CFLAGS = -I$(SQLITE_DIR) -DTHREADSAFE=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS + OBJECTS += $(SQLITE_OBJECTS) + CFLAGS += $(SQLITE_CFLAGS) + CFLAGS += -DUSE_LUA_SQLITE3 + #SOURCE_DIRS = $(SQLITE_DIR) +endif + + +LFS_DIR = src/third_party +LFS_SOURCE_FILES = lfs.c +LFS_SOURCES = $(addprefix $(LFS_DIR)/, $(LFS_SOURCE_FILES)) +LFS_OBJECTS = $(LFS_SOURCES:.c=.o) +LFS_CFLAGS = -I$(LFS_DIR) +OBJECTS += $(LFS_OBJECTS) +CFLAGS += $(LFS_CFLAGS) +CFLAGS += -DUSE_LUA_FILE_SYSTEM +#SOURCE_DIRS = $(LFS_DIR) + + +ifneq ($(WITH_LUA_VERSION), 501) + LXML_DIR = src/third_party + LXML_SOURCE_FILES = LuaXML_lib.c + LXML_SOURCES = $(addprefix $(LXML_DIR)/, $(LXML_SOURCE_FILES)) + LXML_OBJECTS = $(LXML_SOURCES:.c=.o) + LXML_CFLAGS = -I$(LXML_DIR) + OBJECTS += $(LXML_OBJECTS) + CFLAGS += $(LXML_CFLAGS) + CFLAGS += -DUSE_LUA_LUAXML + #SOURCE_DIRS = $(LXML_DIR) +endif + diff --git a/src/civetweb/resources/Makefile.in-os b/src/civetweb/resources/Makefile.in-os new file mode 100644 index 000000000..a75913486 --- /dev/null +++ b/src/civetweb/resources/Makefile.in-os @@ -0,0 +1,23 @@ +# +# Copyright (c) 2013 No Face Press, LLC +# License http://opensource.org/licenses/mit-license.php MIT License +# + +# Override this using TARGET_OS=LINUX on the command line +ifeq ($(TARGET_OS),) + ifeq ($(OS),Windows_NT) + TARGET_OS = WIN32 + else + UNAME_S := $(shell uname -s) + ifeq ($(UNAME_S),Linux) + TARGET_OS = LINUX + else + ifeq ($(UNAME_S),Darwin) + TARGET_OS = OSX + else + TARGET_OS = BSD + endif + endif + endif +endif + diff --git a/src/civetweb/resources/cert/client.crt b/src/civetweb/resources/cert/client.crt new file mode 100644 index 000000000..f6bbdd868 --- /dev/null +++ b/src/civetweb/resources/cert/client.crt @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE----- +MIIDBjCCAe4CCQCFpskbTEyGpTANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJB +VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0 +cyBQdHkgTHRkMB4XDTE3MDkwMzE5MjIwNVoXDTI3MDkwMTE5MjIwNVowRTELMAkG +A1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0 +IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB +ANyFUuYxv/uexSr/K9aSmcnEcylNH4S3NdlvMwFvW3XFqAV05tV6HnPnSELEk6t3 +8aMDUGKDBrrjwsVK6+S7OyrkioXeB9dWldHbqD7o3MkIM3sUxUtaR6x0RMZ+sIX4 +XpE0xULcip1bG0etP4Z2frEP2IOOValQcm4SCnKYZJyTr/oR31NmlIPU/47s74U6 +rqwwUE92bzvf1jGeUHEn7IAgSJNIUBNsOIdRQAMBuTJIAmG2qawXaetjLi/NBwNS +d0OX2v3o9SrA+ZhQYpPG5xp3B3ncHgVvmhmp7hUdlYbiemcUHn18hZjxPVZLbtY8 +gQldrWyMZkVabSZjuIH3IKcCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAUZsxxYVK +l0tH8E0FCnRJTvG6gjOeiqJRIk7Mmg+hfFZK/ewqBixxg1OBM/xmPXfnI/ULRz74 +UMXnyDIsGakzrFDqWqPt3xots35yHHo2ZkVao6gV4qx0Reu86qeN5iRvG0EjoGMD +7XRaw56E0XhvMBJW1CiUg944HSw4ptJli0dJCYa+P9s1Fop3lA0d9+dwKMKUyCDr +yBz4XjyO9jXSQC/t0fkxC4gHhdH/ZaAq0Lem6Xxc40ZwoVc1+dHWFxn8d6L/RYvb +16gOuw6s2Xt9h2K8OFKzehOgNZAkI2oUELRFUx9Wc8/Bcl6uEkBmPHRqeX5l35jo +ztBrpAEsCy0cGg== +-----END CERTIFICATE----- diff --git a/src/civetweb/resources/cert/client.csr b/src/civetweb/resources/cert/client.csr new file mode 100644 index 000000000..beb846694 --- /dev/null +++ b/src/civetweb/resources/cert/client.csr @@ -0,0 +1,16 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIICijCCAXICAQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUx +ITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcN +AQEBBQADggEPADCCAQoCggEBANyFUuYxv/uexSr/K9aSmcnEcylNH4S3NdlvMwFv +W3XFqAV05tV6HnPnSELEk6t38aMDUGKDBrrjwsVK6+S7OyrkioXeB9dWldHbqD7o +3MkIM3sUxUtaR6x0RMZ+sIX4XpE0xULcip1bG0etP4Z2frEP2IOOValQcm4SCnKY +ZJyTr/oR31NmlIPU/47s74U6rqwwUE92bzvf1jGeUHEn7IAgSJNIUBNsOIdRQAMB +uTJIAmG2qawXaetjLi/NBwNSd0OX2v3o9SrA+ZhQYpPG5xp3B3ncHgVvmhmp7hUd +lYbiemcUHn18hZjxPVZLbtY8gQldrWyMZkVabSZjuIH3IKcCAwEAAaAAMA0GCSqG +SIb3DQEBCwUAA4IBAQB/bapQm4nxXA01msL6nkjiVaeh/mj8Cr8sPFtQXVu+hxl9 +mjbisxDXwPhiFOiTlokQkINf+RMxQsVNr2y/sGZrSMimabwODDXnPpyir4b2WOWp +VQQWbgnMVnvgKsjBpLLDr8VnLBiQ3mED+2QV0bxxJSgvvEuiZx/BlCgiu77D/8kj +XUY/CXIBi00fIYigpRRdv2WtMQjtQe2fCSZZKOWu2ZWu2o24kEk28x5LO/WaJ4Ft +lUHFOIp/wkKz/US4mbdQaD0bsg7MirAyGrCmZIHqQDhdDWq+o/brI7N/8yOk3qwc +qPGkr9PYIPnuzZwStLJlPxKGXjCA40HpdmWA0kyc +-----END CERTIFICATE REQUEST----- diff --git a/src/civetweb/resources/cert/client.key b/src/civetweb/resources/cert/client.key new file mode 100644 index 000000000..f041acc0c --- /dev/null +++ b/src/civetweb/resources/cert/client.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEA3IVS5jG/+57FKv8r1pKZycRzKU0fhLc12W8zAW9bdcWoBXTm +1Xoec+dIQsSTq3fxowNQYoMGuuPCxUrr5Ls7KuSKhd4H11aV0duoPujcyQgzexTF +S1pHrHRExn6whfhekTTFQtyKnVsbR60/hnZ+sQ/Yg45VqVBybhIKcphknJOv+hHf +U2aUg9T/juzvhTqurDBQT3ZvO9/WMZ5QcSfsgCBIk0hQE2w4h1FAAwG5MkgCYbap +rBdp62MuL80HA1J3Q5fa/ej1KsD5mFBik8bnGncHedweBW+aGanuFR2VhuJ6ZxQe +fXyFmPE9Vktu1jyBCV2tbIxmRVptJmO4gfcgpwIDAQABAoIBAEpiBlZzTYi4Q1V/ +gO/9vzYZt6akxw7jJZzUL2Y6g6U0KLq+deZoLMF3sB4lZJIgATe1NHYmMCz2Coq1 +/N/Ib+rF8Bu7ivWN1TdWWmft8Bs3UvYfSXVjXG3FQjWaIjzuTCe6nxcwgOkXBBqn +S5g1fAKJj8TATBCyfAa4uyFwWe+eGRs0W9pOMP8eU0EtvTer34rSU4L/LG3d7UcI +upm/0T5QeLqv6Htv8UbHNQto701vJQVdWLavALMXGfGO112yTSz7OpitKpBEYDrV +3+781zYm8AKkFIsRMXVK2HiBEF43zIrnNuoozsKpps/tZdlv9VqCSJ4hIaHm9mxJ +3zMN3OECgYEA8dr5w68jTLrthDZ2qOG/6tZw9fMfXoF7hSUXplgxMN5Sohfr23Xm +/IHVm7oiqhDNNZzplGyux7jB00x2/1ltOzay5mx4PMMLlsDBgiURgUwqS8C8dPVh +0sN2RytdKGDmFP6lnKS7c15CEw1ChvdL4RwtqzjTKE0ZOK3zUY5/MykCgYEA6Wru +Dusip4p4PA1K6eiCoC6SaqCuQCB7ZR5WPR5szAFkgoW63rNtC8S4Bl1qXXUb/v/V +ptaVsGrqBc8/CxvCac1KCREbcyjuVWUAfw2VwdwgDbfrEieWrZNvsDs86EgB+Bo4 +Jm/cUjrFqSTJAbtvp4SYl1reax86XmCsHhNNf08CgYEApAhxd9/0IBlz+ET8K8SY +5sy0ZouTjgRh40bqCF8uVcej4d45kGoh1Ma2Ot1+nzuwApm+7nTcAgd0JjxpRPzB +EfUiVxfgYM2ksYVgeUVs3vXqheBdsTGwPENnmBN4Jme6BSlE573uiOu4ArXulh1p +sG7tJoDu7hmEbqXELl9oNCkCgYEA51zWGnN3JhpakyuZ1cBhueRvvMEH9wg7Rz+K +u4oszQmUVsu3Locqzz9uKODvTTOHTHrJi1WnifZvgNKr6pbZXYXenJ4YV01676nt +lAIjLsTCANcMajJTaDl7u3L8LEEzsnhKr86w09Dtm3qawtzHD4Seu2eWjxelA2dP +M4BukIECgYAn5n+HhCi5JD3I1VCX70uE5nj8alYyQ85qE57Lopmau1RyVfP4oeCt +gMsy0o7vIF+xW1Z2yDxm+mJghOY/myDsbTGX9G8rY7PC7tWE8okjsQT5UoayFzKp +mmvrTV8TQBVcTQqn0Jyj7T5MBnuwfioXYN9pKPQlvc4pPmHbqPi7CA== +-----END RSA PRIVATE KEY----- diff --git a/src/civetweb/resources/cert/client.key.orig b/src/civetweb/resources/cert/client.key.orig new file mode 100644 index 000000000..6b30836ae --- /dev/null +++ b/src/civetweb/resources/cert/client.key.orig @@ -0,0 +1,30 @@ +-----BEGIN RSA PRIVATE KEY----- +Proc-Type: 4,ENCRYPTED +DEK-Info: DES-EDE3-CBC,161D2DE9FC3C5EE4 + +U+3WYIvh4n4cJouZXJkuPMUucnymOWME9ZPBs3CzbWao/HSYS7tEh0gUMb6VtkQo +zq/jQUO2aecWC3+LPZlkUGGWwCU3OCgyFW9xiMRduzKhbPUig+4k9aeKki5RypmR +XFnn/W5ddJSHTVgQI0hF7ZxvC86ldBkCHzSwkKyL8dwBNouXyMgl4PfaZ90N3/pZ +jBnuqbwF1EN0V6OYo3QSxsifpu2M62a229yW9U0/vV7bkGgyqTbtV6V6ny5+IWn+ +r99bb+hS1SSt5VVuHfNKh06ww0Mc7bSfeyVcdgoT5ltftsRWEA1Au5DfS+nhztVf +0l/yiMTm60x61WuXsbQz+4hwH7nCAMIFbBmCYbP/eqY9BIiKy9Ue7vOouTHdi7Op +wQL/r3Wc/+IcssoExITnOBX2San6NvtC7ej+8Wf/wKOh5mTp4uT5cbdif3ifj7uo +GmYbWNJSo0VVtl1nru9uSIbJSKVzMcDO2aVbofCGB4KUpkm+3Sw2ZnbJ6o/AVZb3 +ip7NoudU11d6KBzpyvfyFzSNfGVmtasB24BYt22vSQWJ5Ob5MQgg+zFEpGUeoq6p +LqRaZCDqXHsofM49JEbU/32SV3JhSjfzJ9gVG+lymGZIdmYD4y+SH/6K0QtCN9cY +7++96vAPYgKmJ6vK1N8o6owDkxQ3+FilKWanp2rwETc1RyWSg5hi62+8YllmsQXc +3zvFXgCS+yfAKXqtHTugtb6c8o2RuPdpNtVmtncEnf3OeuP/gDbqLumzHRDQk+o1 +rgGiLsvY4MmbmS0D/c4NZ8fyBqN7d2TRHnHS6mpqgzpKHOuRPmLQ+azmf3ISY9m4 +H8ROjKqTMHUfxQRKizHPTRjtvYjk/uXBrpnDa2buPaW+m1brsiHdhruhB6QPaJGQ +8uFFHyur5ZRMQhST/fWLmNxPX9CAsXBuKm1cPJWw+QzR8uo7bFwups1ODrLj0L0U +ECAq343KS6aoCMoLGw4EZRHeSdrequtsaGFHMtweSxs7PJR+UVLf/JSw4f0L4Pa8 +GvY9nBGrI/uoN4H88YR1NjxLCzU5mZ3yLV4ZL9zGq1gX5ZfAr1b9i2pJI4daBFvW +gJBSzoiN9dq4I4ZZgZ7KSTG2n2mnPPlOhAIgabwpDb2buf0GrZYhgby78Vwy3bdb +Yk8ToEvLnKzDH8P4JjClIVC7R+bfH1uUO+uDRYZBlpkzc0+HgagW0vt9xZpWrdOm +DJeZ7/ciCbD+90b1mc8xkPgegKjvrMfEMk81GEGhv28FpIbEAX9nuA4UaQHI58s/ +tgQ3kThVrlJCC/77teNMubcIWycV8ufm4NgDeGtmIP47Z9Vdrv+VFv0hib3hch9M +7lIJX0aSVRHhEFi6QquQmTlvEB/Z8T640k9ZGiTgkGEFrEh6nnGeiLP+WJodLd/u +CKkx3KLEi8T90dsPhyo852o+oKOFMA/nEGEOBE7VcgOGkZ2NTBOerVZrBp6T0PGi +0x2A32YDMjeI19CdsvxgIq7wj9wXuWDXAFV2Ond75XwM4CnGSKefh+Z82ZlfnmNf +GyKsUSn0Nno58y5LLzXw06E4Wp4WtQcw4KiiaEYA6s92RAsoslY6XqdYSeSvtSz2 +-----END RSA PRIVATE KEY----- diff --git a/src/civetweb/resources/cert/client.pem b/src/civetweb/resources/cert/client.pem new file mode 100644 index 000000000..6974c4c81 --- /dev/null +++ b/src/civetweb/resources/cert/client.pem @@ -0,0 +1,46 @@ +-----BEGIN CERTIFICATE----- +MIIDBjCCAe4CCQCFpskbTEyGpTANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJB +VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0 +cyBQdHkgTHRkMB4XDTE3MDkwMzE5MjIwNVoXDTI3MDkwMTE5MjIwNVowRTELMAkG +A1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0 +IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB +ANyFUuYxv/uexSr/K9aSmcnEcylNH4S3NdlvMwFvW3XFqAV05tV6HnPnSELEk6t3 +8aMDUGKDBrrjwsVK6+S7OyrkioXeB9dWldHbqD7o3MkIM3sUxUtaR6x0RMZ+sIX4 +XpE0xULcip1bG0etP4Z2frEP2IOOValQcm4SCnKYZJyTr/oR31NmlIPU/47s74U6 +rqwwUE92bzvf1jGeUHEn7IAgSJNIUBNsOIdRQAMBuTJIAmG2qawXaetjLi/NBwNS +d0OX2v3o9SrA+ZhQYpPG5xp3B3ncHgVvmhmp7hUdlYbiemcUHn18hZjxPVZLbtY8 +gQldrWyMZkVabSZjuIH3IKcCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAUZsxxYVK +l0tH8E0FCnRJTvG6gjOeiqJRIk7Mmg+hfFZK/ewqBixxg1OBM/xmPXfnI/ULRz74 +UMXnyDIsGakzrFDqWqPt3xots35yHHo2ZkVao6gV4qx0Reu86qeN5iRvG0EjoGMD +7XRaw56E0XhvMBJW1CiUg944HSw4ptJli0dJCYa+P9s1Fop3lA0d9+dwKMKUyCDr +yBz4XjyO9jXSQC/t0fkxC4gHhdH/ZaAq0Lem6Xxc40ZwoVc1+dHWFxn8d6L/RYvb +16gOuw6s2Xt9h2K8OFKzehOgNZAkI2oUELRFUx9Wc8/Bcl6uEkBmPHRqeX5l35jo +ztBrpAEsCy0cGg== +-----END CERTIFICATE----- +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEA3IVS5jG/+57FKv8r1pKZycRzKU0fhLc12W8zAW9bdcWoBXTm +1Xoec+dIQsSTq3fxowNQYoMGuuPCxUrr5Ls7KuSKhd4H11aV0duoPujcyQgzexTF +S1pHrHRExn6whfhekTTFQtyKnVsbR60/hnZ+sQ/Yg45VqVBybhIKcphknJOv+hHf +U2aUg9T/juzvhTqurDBQT3ZvO9/WMZ5QcSfsgCBIk0hQE2w4h1FAAwG5MkgCYbap +rBdp62MuL80HA1J3Q5fa/ej1KsD5mFBik8bnGncHedweBW+aGanuFR2VhuJ6ZxQe +fXyFmPE9Vktu1jyBCV2tbIxmRVptJmO4gfcgpwIDAQABAoIBAEpiBlZzTYi4Q1V/ +gO/9vzYZt6akxw7jJZzUL2Y6g6U0KLq+deZoLMF3sB4lZJIgATe1NHYmMCz2Coq1 +/N/Ib+rF8Bu7ivWN1TdWWmft8Bs3UvYfSXVjXG3FQjWaIjzuTCe6nxcwgOkXBBqn +S5g1fAKJj8TATBCyfAa4uyFwWe+eGRs0W9pOMP8eU0EtvTer34rSU4L/LG3d7UcI +upm/0T5QeLqv6Htv8UbHNQto701vJQVdWLavALMXGfGO112yTSz7OpitKpBEYDrV +3+781zYm8AKkFIsRMXVK2HiBEF43zIrnNuoozsKpps/tZdlv9VqCSJ4hIaHm9mxJ +3zMN3OECgYEA8dr5w68jTLrthDZ2qOG/6tZw9fMfXoF7hSUXplgxMN5Sohfr23Xm +/IHVm7oiqhDNNZzplGyux7jB00x2/1ltOzay5mx4PMMLlsDBgiURgUwqS8C8dPVh +0sN2RytdKGDmFP6lnKS7c15CEw1ChvdL4RwtqzjTKE0ZOK3zUY5/MykCgYEA6Wru +Dusip4p4PA1K6eiCoC6SaqCuQCB7ZR5WPR5szAFkgoW63rNtC8S4Bl1qXXUb/v/V +ptaVsGrqBc8/CxvCac1KCREbcyjuVWUAfw2VwdwgDbfrEieWrZNvsDs86EgB+Bo4 +Jm/cUjrFqSTJAbtvp4SYl1reax86XmCsHhNNf08CgYEApAhxd9/0IBlz+ET8K8SY +5sy0ZouTjgRh40bqCF8uVcej4d45kGoh1Ma2Ot1+nzuwApm+7nTcAgd0JjxpRPzB +EfUiVxfgYM2ksYVgeUVs3vXqheBdsTGwPENnmBN4Jme6BSlE573uiOu4ArXulh1p +sG7tJoDu7hmEbqXELl9oNCkCgYEA51zWGnN3JhpakyuZ1cBhueRvvMEH9wg7Rz+K +u4oszQmUVsu3Locqzz9uKODvTTOHTHrJi1WnifZvgNKr6pbZXYXenJ4YV01676nt +lAIjLsTCANcMajJTaDl7u3L8LEEzsnhKr86w09Dtm3qawtzHD4Seu2eWjxelA2dP +M4BukIECgYAn5n+HhCi5JD3I1VCX70uE5nj8alYyQ85qE57Lopmau1RyVfP4oeCt +gMsy0o7vIF+xW1Z2yDxm+mJghOY/myDsbTGX9G8rY7PC7tWE8okjsQT5UoayFzKp +mmvrTV8TQBVcTQqn0Jyj7T5MBnuwfioXYN9pKPQlvc4pPmHbqPi7CA== +-----END RSA PRIVATE KEY----- diff --git a/src/civetweb/resources/cert/client.pfx b/src/civetweb/resources/cert/client.pfx new file mode 100644 index 000000000..601b3d51a Binary files /dev/null and b/src/civetweb/resources/cert/client.pfx differ diff --git a/src/civetweb/resources/cert/make_certs.bat b/src/civetweb/resources/cert/make_certs.bat new file mode 100644 index 000000000..66a091d37 --- /dev/null +++ b/src/civetweb/resources/cert/make_certs.bat @@ -0,0 +1,55 @@ +@echo off +REM We need admin rights, otherwise the random state cannot be written +REM Thanks to http://stackoverflow.com/a/10052222/1531708 + +:: BatchGotAdmin +:------------------------------------- +REM --> Check for permissions + IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" ( +>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system" +) ELSE ( +>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" +) + +REM --> If error flag set, we do not have admin. +if '%errorlevel%' NEQ '0' ( + echo Requesting administrative privileges... + goto UACPrompt +) else ( goto gotAdmin ) + +:UACPrompt + echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" + set params = %*:"="" + echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" + + "%temp%\getadmin.vbs" + del "%temp%\getadmin.vbs" + exit /B + +:gotAdmin + pushd "%CD%" + CD /D "%~dp0" +:-------------------------------------- + +del server.* + +c:\OpenSSL-Win32\bin\openssl.exe genrsa -des3 -out server.key 4096 + +c:\OpenSSL-Win32\bin\openssl.exe req -sha256 -new -key server.key -out server.csr -utf8 + +copy server.key server.key.orig + +c:\OpenSSL-Win32\bin\openssl.exe rsa -in server.key.orig -out server.key + +echo [ v3_ca ] > server.ext.txt +echo [ req ] >> server.ext.txt +echo req_extensions = my_extensions >> server.ext.txt +echo [ my_extensions ] >> server.ext.txt +echo extendedKeyUsage=serverAuth >> server.ext.txt +echo crlDistributionPoints=URI:http://localhost/crl.pem >> server.ext.txt + +c:\OpenSSL-Win32\bin\openssl.exe x509 -req -days 365 -extensions v3_ca -extfile server.ext.txt -in server.csr -signkey server.key -out server.crt + +copy server.crt server.pem + +type server.key >> server.pem diff --git a/src/civetweb/resources/cert/make_certs.sh b/src/civetweb/resources/cert/make_certs.sh new file mode 100644 index 000000000..b4b6714cc --- /dev/null +++ b/src/civetweb/resources/cert/make_certs.sh @@ -0,0 +1,64 @@ +#!/bin/sh +#using "pass" for every password + +echo "Generating client certificate ..." + +openssl genrsa -des3 -out client.key 2048 +openssl req -new -key client.key -out client.csr + +cp client.key client.key.orig + +openssl rsa -in client.key.orig -out client.key + +openssl x509 -req -days 3650 -in client.csr -signkey client.key -out client.crt + +cp client.crt client.pem +cat client.key >> client.pem + +openssl pkcs12 -export -inkey client.key -in client.pem -name ClientName -out client.pfx + + +echo "Generating first server certificate ..." + +openssl genrsa -des3 -out server.key 2048 +openssl req -new -key server.key -out server.csr + +cp server.key server.key.orig + +openssl rsa -in server.key.orig -out server.key + +openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt + +cp server.crt server.pem +cat server.key >> server.pem + +openssl pkcs12 -export -inkey server.key -in server.pem -name ServerName -out server.pfx + +echo "First server certificate hash for Public-Key-Pins header:" + +openssl x509 -pubkey < server.crt | openssl pkey -pubin -outform der | openssl dgst -sha256 -binary | base64 > server.pin + +cat server.pin + +echo "Generating backup server certificate ..." + +openssl genrsa -des3 -out server_bkup.key 2048 +openssl req -new -key server_bkup.key -out server_bkup.csr + +cp server_bkup.key server_bkup.key.orig + +openssl rsa -in server_bkup.key.orig -out server_bkup.key + +openssl x509 -req -days 3650 -in server_bkup.csr -signkey server_bkup.key -out server_bkup.crt + +cp server_bkup.crt server_bkup.pem +cat server_bkup.key >> server_bkup.pem + +openssl pkcs12 -export -inkey server_bkup.key -in server_bkup.pem -name ServerName -out server_bkup.pfx + +echo "Backup server certificate hash for Public-Key-Pins header:" + +openssl x509 -pubkey < server_bkup.crt | openssl pkey -pubin -outform der | openssl dgst -sha256 -binary | base64 > server_bkup.pin + +cat server_bkup.pin + diff --git a/src/civetweb/resources/cert/server.crt b/src/civetweb/resources/cert/server.crt new file mode 100644 index 000000000..a13fa015a --- /dev/null +++ b/src/civetweb/resources/cert/server.crt @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE----- +MIIDBjCCAe4CCQDDIH/hK1C0BjANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJB +VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0 +cyBQdHkgTHRkMB4XDTE3MDkwMzE5MjIyOVoXDTI3MDkwMTE5MjIyOVowRTELMAkG +A1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0 +IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB +ALUmHEoJcebkUOyqEAhH2OdEuTTk8AxjjVvq9B1dXjlf/dvxGnZX2InScGCJA9Uy +kO1XI8nLXKAGl6OL9jDt/0K3/oFLedDLtZf1qE+kEBuaqAgL+VVAPqwtQZcyCoI9 +zx777I1tPUOl1Q1ass3T7lYsTN8QADmW5zjJn4MJPMQ55qoQUL7HVQR4VJ/ELAXu +xGkQlJFBY5q0Qq6buN102D2upNKXKpDYYPc0OgyJ73fR2+rzQapc52QD4Oh6cbD8 +Fh5Vh/qGNMckh1cQsVm6fRtlkoUqxANZk58rqkEwOuk04p7vlnVvZTidOng7G2nW +1n7YQXCycI+JhofCqOqT9x8CAwEAATANBgkqhkiG9w0BAQsFAAOCAQEATx5GZCxU +KKQCDsafzAwoodbjRlpsJhvdCBGpgMrFTPyQo7BNF/E2XyVCDXbCmbxTRlhFafJG +Loj/73toGkU8+1qUIy/Fffsmeh9YCyMlA2bE+85ccMCVKgCIEx0+fa6Au6/Ref7/ +n7vN/9deJzxWUaNbP26LNq3prbuIbKN6WFNT5mR8HLTmP3O45sqy1jwOZgSwvbgH +bhugE4tSsKghMV5rUgiMhGIrEakFH+1LCZjQh+ojcWWEWyVk3QTQMmSd6tAZf4pb +/Y1GuN6DAiLfzbabUQZCeQ1iZcgrwIOGHWJUPAf+BTPcFLlR3k/kYA9lrqvra7ln +dFIuUv3YzfenfA== +-----END CERTIFICATE----- diff --git a/src/civetweb/resources/cert/server.csr b/src/civetweb/resources/cert/server.csr new file mode 100644 index 000000000..fe5f3fc80 --- /dev/null +++ b/src/civetweb/resources/cert/server.csr @@ -0,0 +1,16 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIICijCCAXICAQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUx +ITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcN +AQEBBQADggEPADCCAQoCggEBALUmHEoJcebkUOyqEAhH2OdEuTTk8AxjjVvq9B1d +Xjlf/dvxGnZX2InScGCJA9UykO1XI8nLXKAGl6OL9jDt/0K3/oFLedDLtZf1qE+k +EBuaqAgL+VVAPqwtQZcyCoI9zx777I1tPUOl1Q1ass3T7lYsTN8QADmW5zjJn4MJ +PMQ55qoQUL7HVQR4VJ/ELAXuxGkQlJFBY5q0Qq6buN102D2upNKXKpDYYPc0OgyJ +73fR2+rzQapc52QD4Oh6cbD8Fh5Vh/qGNMckh1cQsVm6fRtlkoUqxANZk58rqkEw +Ouk04p7vlnVvZTidOng7G2nW1n7YQXCycI+JhofCqOqT9x8CAwEAAaAAMA0GCSqG +SIb3DQEBCwUAA4IBAQCbJgy8LBoI+XCliwPGVM+ZuxEVuR15iaUSX7epuKb4jvyC +y2+YQnNyxLkK8Bu2z9uxXUBbmhqXNiXZd7/SnbTR9MGMq3vyYg6Ggypo24DWez04 +tFaUiLJZsKVoVM6DP3zwpaKKSSJILU2GbNQKW87PHIPSdmAEh+gFD2Uy5sFrvuFJ +LtHfIMMAhMSoEMjmjaLI7N4GVgFhGEr5q5HGpLuAU8cKGyKPkIkSyYN5Ott4u22d +rpASF3TXfCJJ0YiM84U86rhZ0BrMqrVtw8r3uj+4G7hrE92eBU+DDn1D8jWzbyVc +6dlTZaknMeJqsQe2/vq+T5P2yl+/39TnlvDO+cS2 +-----END CERTIFICATE REQUEST----- diff --git a/src/civetweb/resources/cert/server.key b/src/civetweb/resources/cert/server.key new file mode 100644 index 000000000..bd010effb --- /dev/null +++ b/src/civetweb/resources/cert/server.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAtSYcSglx5uRQ7KoQCEfY50S5NOTwDGONW+r0HV1eOV/92/Ea +dlfYidJwYIkD1TKQ7VcjyctcoAaXo4v2MO3/Qrf+gUt50Mu1l/WoT6QQG5qoCAv5 +VUA+rC1BlzIKgj3PHvvsjW09Q6XVDVqyzdPuVixM3xAAOZbnOMmfgwk8xDnmqhBQ +vsdVBHhUn8QsBe7EaRCUkUFjmrRCrpu43XTYPa6k0pcqkNhg9zQ6DInvd9Hb6vNB +qlznZAPg6HpxsPwWHlWH+oY0xySHVxCxWbp9G2WShSrEA1mTnyuqQTA66TTinu+W +dW9lOJ06eDsbadbWfthBcLJwj4mGh8Ko6pP3HwIDAQABAoIBAGgaacGGogW+Cl+n +8CTCHX3y+bjTJL0J7S/426eQg9jXOI3QhpOiMlgqLtjbhO9d6vnqzS9oBmgUwcqE +YcyGyd5u3P0zAeOjXk3hKIP0Vil2/L/7GaQLkrjiHUKlyHJG0SQORUiVkdKxl7nf ++Mfe1qaBOQAsMuTluyXggSIOCfT+FdHoi6nr/+Nugyx7e/UrZ3GWHVbh8KXOlvHh +kETfcI6KUkWKtE+YJx9w89Bjh8TBvU0nkOntR11T2SMNllyIS9nND8pqa7QPz3N0 +Ag/iN4Wh8S5f4Nn4GccAOtIORuYuw9Pmt1E9dFWEna1fGztBHlClFQPOLUhZ+/zR +MfQV5bkCgYEA3pQTLZ5ldX1Kvg5sYw63wwewr147R0pav6AoJ8HTnWGqi5y485CX +uKE/IcJseidG9FmkO7rfexQaBtW9eW0GCVru416VSP9g2r1iUu0ViaqctYt7ZacE +UEI+g4FmaXHyn1CKTjJXgUAdoDbtlyHwLmLmNt+B3zKGa1lPIb5MwdMCgYEA0Fl7 +VCTnmdyFH8m/bK76uW7SgkYmKYd5AvDr2QFCSqY3tdZh2VIukoUPmheCFq0kpDc0 ++eT680rF/m6CCu+00nM6v/3TNARRANeQ2G73kTPpyiphE+ttKCBQ/tke3TcHQA85 +7cI6bfkMonyKi0JRdLs4QEWf86Avr6p6JKdQWgUCgYEA3oAT8+SF9D89umRcwWFz +HcnQPF7sz0VrFmiZ+7RtQMTjYhFXalQ+91hp7euX2TzuV1JNNVCIG1dq9S4x7PKp +uCxo5m4kugZg4gm0AsXyY95kLa+zuViOnVS7fWab5Aj+y3gN6kG07AYWF5URSaWp +nhVLocso3uB5M1LiIg9EV/UCgYBNrN6Wyz9xFE6pQDzWlxGwakme+eomV3RdDVbQ +S3DchcWFTEykicgFJghgCV2deKWNd2uPsreAVqMkLSzcSOuf/gesJkREQ0uzxaoh +lpVDlBgYH96bX40NhabMrEOec3KHhmWxZ1UDRPNZ7JZ2Pp5Bp77b71knqdO9aRAq +dBo3xQKBgQCnxheQbozuzPO/6nixAng1GP1GuuB2bVb4e5Z0+0dt2RfI8+MSqnSL +q9Yr2+p/fJFkVthrOUYwJkMf7ujhK2uNCJ7aKmwHPSIRztNV3UDGFd9wgpj3Pebx +36ahCvDzidTEG+EEra6zPJ1An3KEbPsfXwcy1NVEZ/kFQyzczL0AOw== +-----END RSA PRIVATE KEY----- diff --git a/src/civetweb/resources/cert/server.key.orig b/src/civetweb/resources/cert/server.key.orig new file mode 100644 index 000000000..e99f08e8f --- /dev/null +++ b/src/civetweb/resources/cert/server.key.orig @@ -0,0 +1,30 @@ +-----BEGIN RSA PRIVATE KEY----- +Proc-Type: 4,ENCRYPTED +DEK-Info: DES-EDE3-CBC,9D77FEDB1409D1BC + +uC330C4tHeQ7HAGqwIlcTtZia9xbCwMAkUn/PJxdsRqk+vZ3NcheeWdSbq9BMioM +06Vl0DtlhfAr4kF/IDhRCkUsLafPFaiDoAiSjZx9xOoCpSdJTe8W19vq6bf9nE1t +WKLR7Ot73E5Qinzpon0ewqia3bDgAgZsE4o99HmXKHghlSPV2we6ROMWn3QfoEfE +tTCIhDrgsrCobrMtDdlzTe6FHACc5nrVloq9LCynuoig2/W1eGS0WVPWMMuo09Qn +UQbjANNSfZcSWZTg1ynCqk74W1X/0hPo2/FpEPIV3K4ZykKbqgB4HIerZ/sJO2LF +g5KtRBtyqj/OsTs+v8i2eEtMYEbz17oJfgL8e2R+AD8QKWjEWrgS5R71LrCgX+v3 ++lOANQi+xIaz45h6StnH52Yl+4iQ4KyaKquJ7ZKRJDfKw41SOSAYCUIkm9AGIqN2 +/WHxfDCaJEN4da890AwmEtzIlVf5TRcJYRpCtxP4kNihrjjQddV+LYdg09nMgcoZ +zVk5eXLh6cg0KzKTDbM3AUmedmrj6R+diyE8zkJKKfStJcejhjTB8KxAIsg+JFFj +tnWUv6dHfPq/K3qbMGqJyuST9pjW9xBiIbscc4HOLmfpJHcClYEbDgPbAqnDp8p9 +nAkI1LFqwpix7rKvFsqWgt8B9TCrxdUqKnDYUNXt/9C1o8ZR8muaH1Jpp8xCjve3 +XpTAMj9HVE/pRkySn8YHvehnMK9+UdCy1sO4RoF5qn881sgetDIoZ/825u4erDVf +Az20D0TZ9wBz+BG0MW8DxdOJmKjpW2ew0RlkG9mcMvKvkcl0XbKFqpF1ZqNQxMTt +Gw1Ef5Hu9j+ijpBKb/4Pk7xeC6OaYdu9NxzYQtpC5aCKXwca8VlykBlPFGrN3eBV +wrsQ+aEv+Cf0bziGHPhili5eNRW8F53LqKF9WdmAAHdOPNZR5PHL8FudNRV77pcY +0bXiY3cHD/YnUpXWZYqU3qDabwtExugImYzLrVg+BSZROwvUYwgKmcrTpx6lJjcv +ntoC26QVKAHhnaclBjGZKl0Q+pYZwR0TxEMPiKLe47TBcYTwj8GNrLk1Lf/VJcaz +lntXsqVjCkwT7cFYfeb8XOvBlrTYQA0LlEHbTn9VTwRiM5O2JB0nzoC2q8LXlzki +wC5AECcUhzh2zpVuLB/FttxPT37a3ibJBkX0UHf7CsPoR/FnEYKL6Bytp6bovUYy +Ed3L/DWdGCB0+tMX0+GBcaxAz/Fqe++Hp9FvZx+ob6k86FC50x2lRPRk3jZmmcCp +W1mub0DK1Pbdh2cutmutbqXF5R2bWpK9lRb5jiSJuQPODTLS5TQjnwVYmqv9qszv +lnJ54HtfrMfKIEqE9mzBfJ4agsHXG2HLrsmRzF6jflleeJddLKqJJ7U8sGjwlJAS +0NIKtWmw5kpRKStbLDX+uGuWLQfM4ITX33fIRlujvbeJcFwtmM4i4sJC1yYYOvLQ +aHV46KNkTVYouKMpJVvGWfXfcV90gbD3pYtkN8YAjswtWoQAnOLFwFJrM0HORkpU +173uMGgOA/EoBhS0XQd6k1+y00JPGky8GSWUEPKDoqX/uZl8POO7VcbAtV41Wkq8 +-----END RSA PRIVATE KEY----- diff --git a/src/civetweb/resources/cert/server.pem b/src/civetweb/resources/cert/server.pem new file mode 100644 index 000000000..9a0027c17 --- /dev/null +++ b/src/civetweb/resources/cert/server.pem @@ -0,0 +1,46 @@ +-----BEGIN CERTIFICATE----- +MIIDBjCCAe4CCQDDIH/hK1C0BjANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJB +VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0 +cyBQdHkgTHRkMB4XDTE3MDkwMzE5MjIyOVoXDTI3MDkwMTE5MjIyOVowRTELMAkG +A1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0 +IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB +ALUmHEoJcebkUOyqEAhH2OdEuTTk8AxjjVvq9B1dXjlf/dvxGnZX2InScGCJA9Uy +kO1XI8nLXKAGl6OL9jDt/0K3/oFLedDLtZf1qE+kEBuaqAgL+VVAPqwtQZcyCoI9 +zx777I1tPUOl1Q1ass3T7lYsTN8QADmW5zjJn4MJPMQ55qoQUL7HVQR4VJ/ELAXu +xGkQlJFBY5q0Qq6buN102D2upNKXKpDYYPc0OgyJ73fR2+rzQapc52QD4Oh6cbD8 +Fh5Vh/qGNMckh1cQsVm6fRtlkoUqxANZk58rqkEwOuk04p7vlnVvZTidOng7G2nW +1n7YQXCycI+JhofCqOqT9x8CAwEAATANBgkqhkiG9w0BAQsFAAOCAQEATx5GZCxU +KKQCDsafzAwoodbjRlpsJhvdCBGpgMrFTPyQo7BNF/E2XyVCDXbCmbxTRlhFafJG +Loj/73toGkU8+1qUIy/Fffsmeh9YCyMlA2bE+85ccMCVKgCIEx0+fa6Au6/Ref7/ +n7vN/9deJzxWUaNbP26LNq3prbuIbKN6WFNT5mR8HLTmP3O45sqy1jwOZgSwvbgH +bhugE4tSsKghMV5rUgiMhGIrEakFH+1LCZjQh+ojcWWEWyVk3QTQMmSd6tAZf4pb +/Y1GuN6DAiLfzbabUQZCeQ1iZcgrwIOGHWJUPAf+BTPcFLlR3k/kYA9lrqvra7ln +dFIuUv3YzfenfA== +-----END CERTIFICATE----- +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAtSYcSglx5uRQ7KoQCEfY50S5NOTwDGONW+r0HV1eOV/92/Ea +dlfYidJwYIkD1TKQ7VcjyctcoAaXo4v2MO3/Qrf+gUt50Mu1l/WoT6QQG5qoCAv5 +VUA+rC1BlzIKgj3PHvvsjW09Q6XVDVqyzdPuVixM3xAAOZbnOMmfgwk8xDnmqhBQ +vsdVBHhUn8QsBe7EaRCUkUFjmrRCrpu43XTYPa6k0pcqkNhg9zQ6DInvd9Hb6vNB +qlznZAPg6HpxsPwWHlWH+oY0xySHVxCxWbp9G2WShSrEA1mTnyuqQTA66TTinu+W +dW9lOJ06eDsbadbWfthBcLJwj4mGh8Ko6pP3HwIDAQABAoIBAGgaacGGogW+Cl+n +8CTCHX3y+bjTJL0J7S/426eQg9jXOI3QhpOiMlgqLtjbhO9d6vnqzS9oBmgUwcqE +YcyGyd5u3P0zAeOjXk3hKIP0Vil2/L/7GaQLkrjiHUKlyHJG0SQORUiVkdKxl7nf ++Mfe1qaBOQAsMuTluyXggSIOCfT+FdHoi6nr/+Nugyx7e/UrZ3GWHVbh8KXOlvHh +kETfcI6KUkWKtE+YJx9w89Bjh8TBvU0nkOntR11T2SMNllyIS9nND8pqa7QPz3N0 +Ag/iN4Wh8S5f4Nn4GccAOtIORuYuw9Pmt1E9dFWEna1fGztBHlClFQPOLUhZ+/zR +MfQV5bkCgYEA3pQTLZ5ldX1Kvg5sYw63wwewr147R0pav6AoJ8HTnWGqi5y485CX +uKE/IcJseidG9FmkO7rfexQaBtW9eW0GCVru416VSP9g2r1iUu0ViaqctYt7ZacE +UEI+g4FmaXHyn1CKTjJXgUAdoDbtlyHwLmLmNt+B3zKGa1lPIb5MwdMCgYEA0Fl7 +VCTnmdyFH8m/bK76uW7SgkYmKYd5AvDr2QFCSqY3tdZh2VIukoUPmheCFq0kpDc0 ++eT680rF/m6CCu+00nM6v/3TNARRANeQ2G73kTPpyiphE+ttKCBQ/tke3TcHQA85 +7cI6bfkMonyKi0JRdLs4QEWf86Avr6p6JKdQWgUCgYEA3oAT8+SF9D89umRcwWFz +HcnQPF7sz0VrFmiZ+7RtQMTjYhFXalQ+91hp7euX2TzuV1JNNVCIG1dq9S4x7PKp +uCxo5m4kugZg4gm0AsXyY95kLa+zuViOnVS7fWab5Aj+y3gN6kG07AYWF5URSaWp +nhVLocso3uB5M1LiIg9EV/UCgYBNrN6Wyz9xFE6pQDzWlxGwakme+eomV3RdDVbQ +S3DchcWFTEykicgFJghgCV2deKWNd2uPsreAVqMkLSzcSOuf/gesJkREQ0uzxaoh +lpVDlBgYH96bX40NhabMrEOec3KHhmWxZ1UDRPNZ7JZ2Pp5Bp77b71knqdO9aRAq +dBo3xQKBgQCnxheQbozuzPO/6nixAng1GP1GuuB2bVb4e5Z0+0dt2RfI8+MSqnSL +q9Yr2+p/fJFkVthrOUYwJkMf7ujhK2uNCJ7aKmwHPSIRztNV3UDGFd9wgpj3Pebx +36ahCvDzidTEG+EEra6zPJ1An3KEbPsfXwcy1NVEZ/kFQyzczL0AOw== +-----END RSA PRIVATE KEY----- diff --git a/src/civetweb/resources/cert/server.pin b/src/civetweb/resources/cert/server.pin new file mode 100644 index 000000000..015792a47 --- /dev/null +++ b/src/civetweb/resources/cert/server.pin @@ -0,0 +1 @@ +uz1UTAPen+xb+UoQqkVlEx4H653LbMjfRJcZx5OrjbI= diff --git a/src/civetweb/resources/cert/server_bkup.crt b/src/civetweb/resources/cert/server_bkup.crt new file mode 100644 index 000000000..afe0d5dd0 --- /dev/null +++ b/src/civetweb/resources/cert/server_bkup.crt @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE----- +MIIDBjCCAe4CCQCFKfFGF1i10TANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJB +VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0 +cyBQdHkgTHRkMB4XDTE3MDkwMzE5MjI1MVoXDTI3MDkwMTE5MjI1MVowRTELMAkG +A1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0 +IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB +AKTJr3PzWOR1Hrjfk9bBA7TptI1hNYVn/Xvi2GSferhJaWg69b2Li4t5/JxElESR +8fy0lBMzQ/yaFiQb51y7Q1c+Z6xWLxk322rfy3WhU3DYiFL2sJndrDvAhmso122Z +xVADA0cQwo520MgFYpHNBF8BcFV2IRukzVX+/nVkki05XcwfbI2y6gqCRpOSXdE9 +gCDVan3tSRbtrwKu7IHy88mL6057o82Uezpl0KesoCwb4f5oqs2vThUmXKuxu8GO +WpZNK4JFWnTgDOJrubZvKxzzL9E85DS9aXLk6dNKBJVKPCETnYw+2ArMgXzs+JuA +C4AhV0e6unLX9DcavZ6j7JcCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAJJWqVuQs +guFZG/LZPeeh1WtZr9S6R5BT4+b+PH2teVyGtClXV6KpwcLNEVWzY3qPtrFFPQI1 +uEg6cY8w1JOiCmj/IWKsiHd+IdsqsFVKL+Bmvthm3HSgA6p6ZiVCG4E67p8xwiJP +p5EwtMM/7BdS/tHLUOe1OpNZ8XtHRVUNbzy/+JV0So7WLP9ksGb6COL/9MF0/qG4 +4XrrvpZ9FAgRC9/22QyYiQqoaegGEy4E+KHOBxRmipInsU2H8aQA2sZzQ49Zew9E +QI2jSJTC7EeuZ0OcZawKkJY1ZtIGmOo/Q956keOLdG8cxyq6pXW3gmq1X5QBxy1M +pZYi5eIENGE63g== +-----END CERTIFICATE----- diff --git a/src/civetweb/resources/cert/server_bkup.csr b/src/civetweb/resources/cert/server_bkup.csr new file mode 100644 index 000000000..c866b57df --- /dev/null +++ b/src/civetweb/resources/cert/server_bkup.csr @@ -0,0 +1,16 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIICijCCAXICAQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUx +ITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcN +AQEBBQADggEPADCCAQoCggEBAKTJr3PzWOR1Hrjfk9bBA7TptI1hNYVn/Xvi2GSf +erhJaWg69b2Li4t5/JxElESR8fy0lBMzQ/yaFiQb51y7Q1c+Z6xWLxk322rfy3Wh +U3DYiFL2sJndrDvAhmso122ZxVADA0cQwo520MgFYpHNBF8BcFV2IRukzVX+/nVk +ki05XcwfbI2y6gqCRpOSXdE9gCDVan3tSRbtrwKu7IHy88mL6057o82Uezpl0Kes +oCwb4f5oqs2vThUmXKuxu8GOWpZNK4JFWnTgDOJrubZvKxzzL9E85DS9aXLk6dNK +BJVKPCETnYw+2ArMgXzs+JuAC4AhV0e6unLX9DcavZ6j7JcCAwEAAaAAMA0GCSqG +SIb3DQEBCwUAA4IBAQBvbql7sAA8XOwsszRUzOCLkFxfDsWJ0l5re2mGgHTEd5hc +eDfM+Vdy8SVZX9OySdioVD6ACTse3rc1ULYn8jj1wvOd3/z/J9aUBcBACJG5D1Dl ++j+xvfhvgAGCEQn7ZMaWLFWrLs++aQ+EKbl0SypEI2rTJkyZlYSDVpa+LhqX4UOa ++RNlq1CX+85HCjBn0sWBNzhjrf3gwERRn5NfTab4FqwqGp2+s4GvbOJHrm8saMWu +BlhcTzGGLBRKCQUHo5i9393b3oBOqtcpWPcZGhyAF1NUbYL7USnsiH6lkGReeaFi +xy7vYmUn9j//vT64SmASG0oF+ecUF0q2W42sSqnU +-----END CERTIFICATE REQUEST----- diff --git a/src/civetweb/resources/cert/server_bkup.key b/src/civetweb/resources/cert/server_bkup.key new file mode 100644 index 000000000..235c83a79 --- /dev/null +++ b/src/civetweb/resources/cert/server_bkup.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEApMmvc/NY5HUeuN+T1sEDtOm0jWE1hWf9e+LYZJ96uElpaDr1 +vYuLi3n8nESURJHx/LSUEzND/JoWJBvnXLtDVz5nrFYvGTfbat/LdaFTcNiIUvaw +md2sO8CGayjXbZnFUAMDRxDCjnbQyAVikc0EXwFwVXYhG6TNVf7+dWSSLTldzB9s +jbLqCoJGk5Jd0T2AINVqfe1JFu2vAq7sgfLzyYvrTnujzZR7OmXQp6ygLBvh/miq +za9OFSZcq7G7wY5alk0rgkVadOAM4mu5tm8rHPMv0TzkNL1pcuTp00oElUo8IROd +jD7YCsyBfOz4m4ALgCFXR7q6ctf0Nxq9nqPslwIDAQABAoIBAE/B7lHIrnWk2kHQ +tNV0hj7B/smPC0COnHmhyeqp5dPcdFAmeVpMeDYBzOo1py2pFd6h6CmC3p0cVysS +9mBDosxPQA6BiDpEdsa7mtZMRv6PTywYilFuoTYqcOTc16gMjRu02ZlD22boyxSE +xria6kqxf5Vdn5ipo1jEGpTnIHkSS+Y8CetCaYgcezLaXlXN3RyjF6tCHMeS3iLl +/zY2O4avG2BM+vvDGDW2FWtZg+hN+5Yk90Qt8dFTwvWRCfYaSWfi7id91p5X0rnL +x1G07qw18LziKJj4HZiueqbDcDOYhfcA6sd0OHcvtXfGIoeqkXxi54cIOReRhN2/ +7ib3iUECgYEA2DNH5aiwc5uqTAL9RHTnuuFwQe46onJwnBkho+xEvvdsp2Q2f7VR +c5M17fL+Rb5gq0O4vzeegKiYpo8gKjFp3Duv9Gdc/TB9sLEEt4NQMD4shV7ihBwC +Rjsflww45dt0mccFZp1ncDYKWHDFzdhO+WB828FPFh/5dl6S+v4Q9bUCgYEAwx+G +XhheTMSqoKGVJ283+4bNZWUSE99wcAhx9J3FkJera030mh0OHoCn0myBjRjxOSY/ +eBH8/0YoLkGYvTdEU/tYiLIWJ/ehC0eweXiwDehb5meco6u9WCeYvyPMLErXbe3K +BQVyfcFzva4eC3dZ7lzxmyVyKXVTYgY0Hf7biJsCgYBKHdJg/eJ3z36jDkdK55Tl +cRFt2MCLHhZSvR7WNlIe8W1zORyhzUP+DhJn32yh9jDnpZC5JNUWoDWsq9ZIAKac +1G1uqNytA6mjIBxQ2RhtYXMbybp3ta5l6zDaNFtxGTmw2hSU6BMk2bHUPdzhw2zX +eudy4qM9H3sCxEs49k5UHQKBgDz7I0FRGFehtznQhg73AWYIsTSZK9cuI7O/z+2F +SXNxE0/L40AvCHSb/NcUtkBkpS8ZNwjNhmY5hOE/+v5XwXEFwpumHKqNB7XAx/SO +tWcDUYVmqFu2lsxwQ5qpE2xcT4u5n0OGeku3I/cJ7bXjrSWDwracM1uloVOnYK5n +MjE1AoGAeM6Wrb0VXRq025+OEfoFis3P9S0REkkkabM9+VLRSWi63uxg2cyipxUg +qJUThbUm1aSS1F+XWjG7vrDjWT/GQTYj9/CH3mZRflZgLUltVHEZesLwWqn15Gl/ +BDwaV6RN7F3BCSzgEfCutrfGJqxA0tx5TTcupOgwpZVakN+hm3c= +-----END RSA PRIVATE KEY----- diff --git a/src/civetweb/resources/cert/server_bkup.key.orig b/src/civetweb/resources/cert/server_bkup.key.orig new file mode 100644 index 000000000..3e227f271 --- /dev/null +++ b/src/civetweb/resources/cert/server_bkup.key.orig @@ -0,0 +1,30 @@ +-----BEGIN RSA PRIVATE KEY----- +Proc-Type: 4,ENCRYPTED +DEK-Info: DES-EDE3-CBC,E700A7E4CCCF78FC + +1/jqqjzwHWxdTvayJ+y//FTMuxZbIOT8hl4h+4zzh+/o45RHUa8n6nTI3mLSIK/i +6yZqSaPaCiBINcLWmijbeDN73OhVL3rrum1VotcTUTA2LU0M+kLWpmcJrY5lsku2 +2voulXmLoCniup+nhnGXDWOk3jaLsMBOn3kYyVBgphwzGzKinFQnqzo/w6/2pJ/C +iKb/GWNEuXwHkoTz7A4o1ZVVOQyx8h0klWbwjBEPB+cPE3h/bRTkJWTTy2yfCeUC +nyRQ1BkWC6I5LLesC7gG0jsw3rcGkJnu+zNkxG43N9CkU74S/jNwktjqzKiSbO14 +SYu/NiaVLCtQTeaTeh39X8OGW4RzFbO32PVBl0jL5d8h3DpvTufbtC9BS9dADM0o +pwCUVWiKKgApmqhFDyUpQB8ptfLewmp5ksB93NzdlPLV8vojJ7nBOrppaueXKQ6Z +f380J3xCrVN7pTt/yT3mlvCnmb0gNhL6jAE53C112Upjmu40OBhwjGDjDXsOZBmZ +NH0KEsLecIPgbx0W27h0j4JDdXGtI4mjFHHEnFj75We73BZ8BIlyRt7nxS+iFYNH +hpbwuNc4dX953jwPy7CIxyms5cy4BigTv/rsgFWCxWxFcTqYkp6L3z8IC6apJDSp +htKLzhFKjlXqP3vcwZYrAF26eOl6Pxn74853dRskAZNY5F40/7IjVb8l71tedWof +WE5wpuXrZJd+GBeIM+plbPCnBiNSHuRzJPhLU3tsT3X70WfDxgIgM38P5uOOBLma +hU/0wcL8+Q3HESPx5ju+5lYZBmozs5ANwMggXv0a1vBptI/kA0HeMQx52G4m1F+9 +aYRrNc+Exl901Yjn120Y6NllIk5EolsqNifDsems9sTiC91Fvh7yiYI5Pd7qCwoR +2ssLbWda/+bgqtDXjmv4x1btWhWmbInBpCrp1YEwILlyNN6FQ/sQ8pvHtyC9+FcB +xQ7wI0JnsCpj8E+P5C08EEgpTiUfelwYyTZLarAFMRe5HWZjgQW7+JvbiQ4Q3ZXM +/DaYh6zGXvR4OomTqKN63i6iAckS7Uh3/XzaWbjOAxvMIgKhU7OBqDAlPvgXJh1y +sDig2zTR603AURkWZir2OOr7XKZcqNj8lxt+BG+E6bwxdeIeEQn7f8Ef5oUmFNXQ +o5zQwI12RDQXaBRDG/wTONrUhvc8TEM04jNGKMRPU6FlZSEF2zlTSU8/QtO56FwR +MwhPlNUSgWgvqzFWoA4W7XWjAfYqvoVRcLjcZF2WBUJwYq4ZODNqBH+j3+Ftge9l +wKc/9KjcurpAWbg7gq9XtwgiT8bYHcM3NSOwgnrOJ4wtukuwbLq2JjMLgFQeDAev +NFJfbYoZ0iTyIq1WRS0MlhAwDKujDOoQLqkF41LC5Ac6RpMeR689xN18YE6b6iXp +f93tHI+6Ru1I+2ZoGbvCPmasv3jk69C8A/StmUEcFfxmOr5qhQEVR2pIXfXKeqbE +KCUoR4Wax42q/bDhgIOOmq+E9YsTuy4Iq5J25WDIbgWN35s0qXDm91s+aY0KtDUu +fxGdOIHQHx10ARHUKZ6AhXSF+q5QQFy4ejlgiTMorvT0XUHmIQ82hw== +-----END RSA PRIVATE KEY----- diff --git a/src/civetweb/resources/cert/server_bkup.pem b/src/civetweb/resources/cert/server_bkup.pem new file mode 100644 index 000000000..a409c1435 --- /dev/null +++ b/src/civetweb/resources/cert/server_bkup.pem @@ -0,0 +1,46 @@ +-----BEGIN CERTIFICATE----- +MIIDBjCCAe4CCQCFKfFGF1i10TANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJB +VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0 +cyBQdHkgTHRkMB4XDTE3MDkwMzE5MjI1MVoXDTI3MDkwMTE5MjI1MVowRTELMAkG +A1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0 +IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB +AKTJr3PzWOR1Hrjfk9bBA7TptI1hNYVn/Xvi2GSferhJaWg69b2Li4t5/JxElESR +8fy0lBMzQ/yaFiQb51y7Q1c+Z6xWLxk322rfy3WhU3DYiFL2sJndrDvAhmso122Z +xVADA0cQwo520MgFYpHNBF8BcFV2IRukzVX+/nVkki05XcwfbI2y6gqCRpOSXdE9 +gCDVan3tSRbtrwKu7IHy88mL6057o82Uezpl0KesoCwb4f5oqs2vThUmXKuxu8GO +WpZNK4JFWnTgDOJrubZvKxzzL9E85DS9aXLk6dNKBJVKPCETnYw+2ArMgXzs+JuA +C4AhV0e6unLX9DcavZ6j7JcCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAJJWqVuQs +guFZG/LZPeeh1WtZr9S6R5BT4+b+PH2teVyGtClXV6KpwcLNEVWzY3qPtrFFPQI1 +uEg6cY8w1JOiCmj/IWKsiHd+IdsqsFVKL+Bmvthm3HSgA6p6ZiVCG4E67p8xwiJP +p5EwtMM/7BdS/tHLUOe1OpNZ8XtHRVUNbzy/+JV0So7WLP9ksGb6COL/9MF0/qG4 +4XrrvpZ9FAgRC9/22QyYiQqoaegGEy4E+KHOBxRmipInsU2H8aQA2sZzQ49Zew9E +QI2jSJTC7EeuZ0OcZawKkJY1ZtIGmOo/Q956keOLdG8cxyq6pXW3gmq1X5QBxy1M +pZYi5eIENGE63g== +-----END CERTIFICATE----- +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEApMmvc/NY5HUeuN+T1sEDtOm0jWE1hWf9e+LYZJ96uElpaDr1 +vYuLi3n8nESURJHx/LSUEzND/JoWJBvnXLtDVz5nrFYvGTfbat/LdaFTcNiIUvaw +md2sO8CGayjXbZnFUAMDRxDCjnbQyAVikc0EXwFwVXYhG6TNVf7+dWSSLTldzB9s +jbLqCoJGk5Jd0T2AINVqfe1JFu2vAq7sgfLzyYvrTnujzZR7OmXQp6ygLBvh/miq +za9OFSZcq7G7wY5alk0rgkVadOAM4mu5tm8rHPMv0TzkNL1pcuTp00oElUo8IROd +jD7YCsyBfOz4m4ALgCFXR7q6ctf0Nxq9nqPslwIDAQABAoIBAE/B7lHIrnWk2kHQ +tNV0hj7B/smPC0COnHmhyeqp5dPcdFAmeVpMeDYBzOo1py2pFd6h6CmC3p0cVysS +9mBDosxPQA6BiDpEdsa7mtZMRv6PTywYilFuoTYqcOTc16gMjRu02ZlD22boyxSE +xria6kqxf5Vdn5ipo1jEGpTnIHkSS+Y8CetCaYgcezLaXlXN3RyjF6tCHMeS3iLl +/zY2O4avG2BM+vvDGDW2FWtZg+hN+5Yk90Qt8dFTwvWRCfYaSWfi7id91p5X0rnL +x1G07qw18LziKJj4HZiueqbDcDOYhfcA6sd0OHcvtXfGIoeqkXxi54cIOReRhN2/ +7ib3iUECgYEA2DNH5aiwc5uqTAL9RHTnuuFwQe46onJwnBkho+xEvvdsp2Q2f7VR +c5M17fL+Rb5gq0O4vzeegKiYpo8gKjFp3Duv9Gdc/TB9sLEEt4NQMD4shV7ihBwC +Rjsflww45dt0mccFZp1ncDYKWHDFzdhO+WB828FPFh/5dl6S+v4Q9bUCgYEAwx+G +XhheTMSqoKGVJ283+4bNZWUSE99wcAhx9J3FkJera030mh0OHoCn0myBjRjxOSY/ +eBH8/0YoLkGYvTdEU/tYiLIWJ/ehC0eweXiwDehb5meco6u9WCeYvyPMLErXbe3K +BQVyfcFzva4eC3dZ7lzxmyVyKXVTYgY0Hf7biJsCgYBKHdJg/eJ3z36jDkdK55Tl +cRFt2MCLHhZSvR7WNlIe8W1zORyhzUP+DhJn32yh9jDnpZC5JNUWoDWsq9ZIAKac +1G1uqNytA6mjIBxQ2RhtYXMbybp3ta5l6zDaNFtxGTmw2hSU6BMk2bHUPdzhw2zX +eudy4qM9H3sCxEs49k5UHQKBgDz7I0FRGFehtznQhg73AWYIsTSZK9cuI7O/z+2F +SXNxE0/L40AvCHSb/NcUtkBkpS8ZNwjNhmY5hOE/+v5XwXEFwpumHKqNB7XAx/SO +tWcDUYVmqFu2lsxwQ5qpE2xcT4u5n0OGeku3I/cJ7bXjrSWDwracM1uloVOnYK5n +MjE1AoGAeM6Wrb0VXRq025+OEfoFis3P9S0REkkkabM9+VLRSWi63uxg2cyipxUg +qJUThbUm1aSS1F+XWjG7vrDjWT/GQTYj9/CH3mZRflZgLUltVHEZesLwWqn15Gl/ +BDwaV6RN7F3BCSzgEfCutrfGJqxA0tx5TTcupOgwpZVakN+hm3c= +-----END RSA PRIVATE KEY----- diff --git a/src/civetweb/resources/cert/server_bkup.pin b/src/civetweb/resources/cert/server_bkup.pin new file mode 100644 index 000000000..0e0561290 --- /dev/null +++ b/src/civetweb/resources/cert/server_bkup.pin @@ -0,0 +1 @@ +pf3px1MBPmlTGAPoiHWqaSJ9L9Z+DKfwgsU7LfLnmsk= diff --git a/src/civetweb/resources/civetweb.conf b/src/civetweb/resources/civetweb.conf new file mode 100644 index 000000000..00aacdee8 --- /dev/null +++ b/src/civetweb/resources/civetweb.conf @@ -0,0 +1,32 @@ +# Civetweb web server configuration file. +# For detailed description of every option, visit +# https://github.com/civetweb/civetweb/blob/master/docs/UserManual.md +# Lines starting with '#' and empty lines are ignored. +# To make a change, remove leading '#', modify option's value, +# save this file and then restart Civetweb. + +document_root . +listening_ports 8080 + +# cgi_pattern **.cgi$|**.pl$|**.php$ +# cgi_environment +# put_delete_auth_file +# cgi_interpreter +# protect_uri +# authentication_domain mydomain.com +# ssi_pattern **.shtml$|**.shtm$ +# throttle +# access_log_file +# enable_directory_listing yes +# error_log_file +# global_auth_file +# index_files index.html,index.htm,index.cgi,index.shtml,index.php,index.lp +# enable_keep_alive no +# access_control_list +# extra_mime_types +# ssl_certificate +# num_threads 50 +# run_as_user +# url_rewrite_patterns +# hide_files_patterns +# request_timeout_ms 30000 diff --git a/src/civetweb/resources/civetweb.icns b/src/civetweb/resources/civetweb.icns new file mode 100644 index 000000000..af1f12112 Binary files /dev/null and b/src/civetweb/resources/civetweb.icns differ diff --git a/src/civetweb/resources/civetweb.psd b/src/civetweb/resources/civetweb.psd new file mode 100644 index 000000000..accbe3112 Binary files /dev/null and b/src/civetweb/resources/civetweb.psd differ diff --git a/src/civetweb/resources/civetweb_16x16.png b/src/civetweb/resources/civetweb_16x16.png new file mode 100644 index 000000000..10ea1d2f6 Binary files /dev/null and b/src/civetweb/resources/civetweb_16x16.png differ diff --git a/src/civetweb/resources/civetweb_16x16@2.png b/src/civetweb/resources/civetweb_16x16@2.png new file mode 100644 index 000000000..7621fb10b Binary files /dev/null and b/src/civetweb/resources/civetweb_16x16@2.png differ diff --git a/src/civetweb/resources/civetweb_22x22.png b/src/civetweb/resources/civetweb_22x22.png new file mode 100644 index 000000000..f0b9094eb Binary files /dev/null and b/src/civetweb/resources/civetweb_22x22.png differ diff --git a/src/civetweb/resources/civetweb_22x22@2.png b/src/civetweb/resources/civetweb_22x22@2.png new file mode 100644 index 000000000..bdb3614c8 Binary files /dev/null and b/src/civetweb/resources/civetweb_22x22@2.png differ diff --git a/src/civetweb/resources/civetweb_32x32.png b/src/civetweb/resources/civetweb_32x32.png new file mode 100644 index 000000000..62471e3c2 Binary files /dev/null and b/src/civetweb/resources/civetweb_32x32.png differ diff --git a/src/civetweb/resources/civetweb_32x32@2.png b/src/civetweb/resources/civetweb_32x32@2.png new file mode 100644 index 000000000..e192a45aa Binary files /dev/null and b/src/civetweb/resources/civetweb_32x32@2.png differ diff --git a/src/civetweb/resources/civetweb_64x64.png b/src/civetweb/resources/civetweb_64x64.png new file mode 100644 index 000000000..bc8b995b8 Binary files /dev/null and b/src/civetweb/resources/civetweb_64x64.png differ diff --git a/src/civetweb/resources/civetweb_64x64@2.png b/src/civetweb/resources/civetweb_64x64@2.png new file mode 100644 index 000000000..d6ad7b429 Binary files /dev/null and b/src/civetweb/resources/civetweb_64x64@2.png differ diff --git a/src/civetweb/resources/coverity_check.sh b/src/civetweb/resources/coverity_check.sh new file mode 100755 index 000000000..063d7c8c2 --- /dev/null +++ b/src/civetweb/resources/coverity_check.sh @@ -0,0 +1,60 @@ +#! /bin/sh + +# check if we use the correct directory +ls src/civetweb.c +if [ "$?" = "0" ]; then + echo "Building files for coverity check ..." +else + echo "Run this script from the root directory of project!" 1>&2 + echo "username@hostname:/somewhere/civetweb$ ./resources/coverity_check.sh" 1>&2 + exit 1 +fi + +# remove last build +rm -rf cov_build/ + +# copy files to build folder +mkdir cov_build +mkdir cov_build/src +mkdir cov_build/include +mkdir cov_build/resources + +cp Makefile cov_build/ +cp src/*.c cov_build/src/ +cp src/*.inl cov_build/src/ +cp include/civetweb.h cov_build/include/ +cp resources/Makefile.in-os cov_build/resources/ + +cd cov_build + +# new scan build +../../cov-analysis-linux64-8.7.0/bin/cov-build --dir cov-int make WITH_IPV6=1 WITH_WEBSOCKET=1 WITH_SERVER_STATS=1 + + +# pack build results for upload +tar czvf civetweb_coverity_check.tgz cov-int + +cd .. + +# check if the build was successful +echo +ls -la cov_build/civetweb_coverity_check.tgz + +if [ "$?" = "0" ]; then + echo "... done" + echo + echo "submit to https://scan.coverity.com/projects/bel2125-civetweb" + echo + echo "last commit was" + git log -n 1 + echo + echo +else + echo "No civetweb_coverity_check.tgz file" 1>&2 + echo + exit 1 +fi + +# return "ok" +exit 0 + diff --git a/src/civetweb/resources/duktape-logo.png b/src/civetweb/resources/duktape-logo.png new file mode 100644 index 000000000..afdc0f8a0 Binary files /dev/null and b/src/civetweb/resources/duktape-logo.png differ diff --git a/src/civetweb/resources/itworks.html b/src/civetweb/resources/itworks.html new file mode 100644 index 000000000..cba3bad02 --- /dev/null +++ b/src/civetweb/resources/itworks.html @@ -0,0 +1,23 @@ + + +Civetweb: It Works! + + +
+
+
+
+logo +

+Civetweb
+Your web server +

+

+
+ + diff --git a/src/civetweb/resources/jni/Android.mk b/src/civetweb/resources/jni/Android.mk new file mode 100644 index 000000000..f24981a60 --- /dev/null +++ b/src/civetweb/resources/jni/Android.mk @@ -0,0 +1,6 @@ +LOCAL_PATH := $(call my-dir)/../.. +include $(CLEAR_VARS) +LOCAL_CFLAGS := -std=c99 -O2 -W -Wall -pthread -pipe $(COPT) +LOCAL_MODULE := civetweb +LOCAL_SRC_FILES := src\main.c src\civetweb.c +include $(BUILD_EXECUTABLE) diff --git a/src/civetweb/resources/lua-logo.jpg b/src/civetweb/resources/lua-logo.jpg new file mode 100644 index 000000000..67de66686 Binary files /dev/null and b/src/civetweb/resources/lua-logo.jpg differ diff --git a/src/civetweb/resources/luafilesystem-logo.jpg b/src/civetweb/resources/luafilesystem-logo.jpg new file mode 100644 index 000000000..4a2e855b5 Binary files /dev/null and b/src/civetweb/resources/luafilesystem-logo.jpg differ diff --git a/src/civetweb/resources/luasqlite-logo.jpg b/src/civetweb/resources/luasqlite-logo.jpg new file mode 100644 index 000000000..9388126f4 Binary files /dev/null and b/src/civetweb/resources/luasqlite-logo.jpg differ diff --git a/src/civetweb/resources/luaxml-logo.jpg b/src/civetweb/resources/luaxml-logo.jpg new file mode 100644 index 000000000..9916f6ed8 Binary files /dev/null and b/src/civetweb/resources/luaxml-logo.jpg differ diff --git a/src/civetweb/resources/mingw.bat b/src/civetweb/resources/mingw.bat new file mode 100644 index 000000000..87032662a --- /dev/null +++ b/src/civetweb/resources/mingw.bat @@ -0,0 +1,15 @@ +@rem MinGW build test - used to test MinGW builds locally +@rem Adapt path/versions before use + +@rem This batch file must be used from the repository root +@if exist mingw.bat cd .. + + +@set PATH=%ProgramFiles%\mingw-w64\i686-4.9.2-win32-dwarf-rt_v3-rev1\mingw32\bin;%PATH% +@set PATH=%ProgramFiles%\GnuWin32\bin;%PATH% + +@rem Alternative ways to use mingw +@rem make CC=gcc CFLAGS=-w CFLAGS+=-Iinclude/ CFLAGS+=-lws2_32 CFLAGS+=-liphlpapi +@rem gcc src\civetweb.c src\main.c -Iinclude\ -lws2_32 -lpthread -lcomdlg32 -w + +make build CC=gcc WITH_LUA=1 WITH_WEBSOCKET=1 diff --git a/src/civetweb/resources/res.rc b/src/civetweb/resources/res.rc new file mode 100644 index 000000000..38a80bb95 --- /dev/null +++ b/src/civetweb/resources/res.rc @@ -0,0 +1 @@ +100 ICON DISCARDABLE "systray.ico" diff --git a/src/civetweb/resources/sqlite3-logo.jpg b/src/civetweb/resources/sqlite3-logo.jpg new file mode 100644 index 000000000..14b01df06 Binary files /dev/null and b/src/civetweb/resources/sqlite3-logo.jpg differ diff --git a/src/civetweb/resources/ssl_cert.pem b/src/civetweb/resources/ssl_cert.pem new file mode 100644 index 000000000..f7e15a0e7 --- /dev/null +++ b/src/civetweb/resources/ssl_cert.pem @@ -0,0 +1,50 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEAwONaLOP7EdegqjRuQKSDXzvHmFMZfBufjhELhNjo5KsL4ieH +hMSGCcSV6y32hzhqR5lvTViaQez+xhc58NZRu+OUgEhodRBW/vAOjpz/xdMz5HaC +EhP3E9W1pkitVseS8B5rrgJo1BfCGai1fPav1nutPq2Kj7vMy24+g460Lonf6ln1 +di4aTIRtAqXtUU6RFpPJP35PkCXbTK65O8HJSxxt/XtfoezHCU5+UIwmZGYx46UB +Wzg3IfK6bGPSiHU3pdiTol0uMPt/GUK+x4NyZJ4/ImsNAicRwMBdja4ywHKXJehH +gXBthsVIHbL21x+4ibsg9eVM/XioTV6tW3IrdwIDAQABAoIBACFfdLutmkQFBcRN +HAJNNHmmsyr0vcUOVnXTFyYeDXV67qxrYHQlOHe6LqIpKq1Mon7O2kYMnWvooFAP +trOnsS6L+qaTYJdYg2TKjgo4ubw1hZXytyB/mdExuaMSkgMgtpia+tB5lD+V+LxN +x1DesZ+veFMO3Zluyckswt4qM5yVa04YFrt31H0E1rJfIen61lidXIKYmHHWuRxK +SadjFfbcqJ6P9ZF22BOkleg5Fm5NaxJmyQynOWaAkSZa5w1XySFfRjRfsbDr64G6 ++LSG8YtRuvfxnvUNhynVPHcpE40eiPo6v8Ho6yZKXpV5klCKciodXAORsswSoGJa +N3nnu/ECgYEA6Yb2rM3QUEPIALdL8f/OzZ1GBSdiQB2WSAxzl9pR/dLF2H+0pitS +to0830mk92ppVmRVD3JGxYDRZQ56tlFXyGaCzJBMRIcsotAhBoNbjV0i9n5bLJYf +BmjU9yvWcgsTt0tr3B0FrtYyp2tCvwHqlxvFpFdUCj2oRw2uGpkhmNkCgYEA03M6 +WxFhsix3y6eVCVvShfbLBSOqp8l0qiTEty+dgVQcWN4CO/5eyaZXKxlCG9KMmKxy +Yx+YgxZrDhfaZ0cxhHGPRKEAxM3IKwT2C8/wCaSiLWXZZpTifnSD99vtOt4wEfrG ++AghNd5kamFiM9tU0AyvhJc2vdJFuXrfeC7ntM8CgYBGDA+t4cZcbRhu7ow/OKYF +kulP3nJgHP/Y+LMrl3cEldZ2jEfZmCElVNQvfd2XwTl7injhOzvzPiKRF3jDez7D +g8w0JAxceddvttJRK9GoY4l7OoeKpjUELSnEQkf+yUfOsTbXPXVY7jMfeNL6jE6b +qN7t3qv8rmXtejMBE3G6cQKBgGR5W2BMiRSlxqKx1cKlrApV87BUe1HRCyuR3xuA +d6Item7Lx1oEi7vb242yKdSYnpApWQ06xTh83Y/Ly87JaIEbiM0+h+P8OEIg0F1a +iB+86AcUX1I8KseVy+Np0HbpfwP8GrFfA5DaRPK7pXMopEtby8cAJ1XZZaI1/ZvZ +BebHAoGAcQU9WvCkT+nIp9FpXfBybYUsvgkaizMIqp66/l3GYgYAq8p1VLGvN4v5 +ec0dW58SJrCpqsM3NP78DtEzQf9OOsk+FsjBFzDU2RkeUreyt2/nQBj/2mN/+hEy +hYN0Zii2yTb63jGxKY6gH1R/r9dL8kXaJmcZrfSa3AgywnteJWg= +-----END RSA PRIVATE KEY----- +-----BEGIN CERTIFICATE----- +MIIDBjCCAe4CCQCX05m0b053QzANBgkqhkiG9w0BAQQFADBFMQswCQYDVQQGEwJB +VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0 +cyBQdHkgTHRkMB4XDTA4MTIwNzEwMjUyMloXDTE4MTIwNTEwMjUyMlowRTELMAkG +A1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0 +IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB +AMDjWizj+xHXoKo0bkCkg187x5hTGXwbn44RC4TY6OSrC+Inh4TEhgnElest9oc4 +akeZb01YmkHs/sYXOfDWUbvjlIBIaHUQVv7wDo6c/8XTM+R2ghIT9xPVtaZIrVbH +kvAea64CaNQXwhmotXz2r9Z7rT6tio+7zMtuPoOOtC6J3+pZ9XYuGkyEbQKl7VFO +kRaTyT9+T5Al20yuuTvByUscbf17X6HsxwlOflCMJmRmMeOlAVs4NyHyumxj0oh1 +N6XYk6JdLjD7fxlCvseDcmSePyJrDQInEcDAXY2uMsBylyXoR4FwbYbFSB2y9tcf +uIm7IPXlTP14qE1erVtyK3cCAwEAATANBgkqhkiG9w0BAQQFAAOCAQEAW4yZdqpB +oIdiuXRosr86Sg9FiMg/cn+2OwQ0QIaA8ZBwKsc+wIIHEgXCS8J6316BGQeUvMD+ +plNe0r4GWzzmlDMdobeQ5arPRB89qd9skE6pAMdLg3FyyfEjz3A0VpskolW5VBMr +P5R7uJ1FLgH12RyAjZCWYcCRqEMOffqvyMCH6oAjyDmQOA5IssRKX/HsHntSH/HW +W7slTcP45ty1b44Nq22/ubYk0CJRQgqKOIQ3cLgPomN1jNFQbAbfVTaK1DpEysrQ +5V8a8gNW+3sVZmV6d1Mj3pN2Le62wUKuV2g6BNU7iiwcoY8HI68aRxz2hVMS+t5f +SEGI4JSxV56lYg== +-----END CERTIFICATE----- +-----BEGIN DH PARAMETERS----- +MEYCQQD+ef8hZ4XbdoyIpJyCTF2UrUEfX6mYDvxuS5O1UNYcslUqlj6JkA11e/yS +6DK8Z86W6mSj5CEk4IjbyEOECXH7AgEC +-----END DH PARAMETERS----- diff --git a/src/civetweb/resources/systray.ico b/src/civetweb/resources/systray.ico new file mode 100644 index 000000000..380c0ee08 Binary files /dev/null and b/src/civetweb/resources/systray.ico differ diff --git a/src/civetweb/src/CMakeLists.txt b/src/civetweb/src/CMakeLists.txt new file mode 100644 index 000000000..a0528b287 --- /dev/null +++ b/src/civetweb/src/CMakeLists.txt @@ -0,0 +1,297 @@ +# The C API library +add_library(c-library civetweb.c) +set_target_properties(c-library PROPERTIES + OUTPUT_NAME "civetweb" + VERSION ${CIVETWEB_VERSION} + SOVERSION ${CIVETWEB_VERSION} +) +if (BUILD_SHARED_LIBS) + target_compile_definitions(c-library PRIVATE CIVETWEB_DLL_EXPORTS) +endif() +target_include_directories( + c-library PUBLIC + ${PROJECT_SOURCE_DIR}/include) +install( + TARGETS c-library + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin + COMPONENT c-library) +install(FILES + ${PROJECT_SOURCE_DIR}/include/civetweb.h + DESTINATION include + COMPONENT c-library) + +# Need Windows sockets if available +find_package(WinSock) +if (WINSOCK_FOUND) + target_link_libraries(c-library WINSOCK::WINSOCK) +endif() + +# We need threading +find_package(Threads) +target_link_libraries(c-library ${CMAKE_THREAD_LIBS_INIT}) + +# Need the realtime library if we're using timers +find_package(LibRt) +if (CIVETWEB_ENABLE_WEBSOCKETS AND CIVETWEB_ENABLE_LUA AND LIBRT_FOUND) + target_link_libraries(c-library LIBRT::LIBRT) +endif() + +# We need to link OpenSSL if not dynamically loading +if (CIVETWEB_ENABLE_SSL) + if (CIVETWEB_ENABLE_SSL_DYNAMIC_LOADING) + find_package(LibDl) + if (LIBDL_FOUND) + target_link_libraries(c-library -ldl) + endif() + else() + find_package(OpenSSL) + include_directories(${OPENSSL_INCLUDE_DIR}) + message(STATUS "OpenSSL include directory: ${OPENSSL_INCLUDE_DIR}") + target_link_libraries(c-library ${OPENSSL_LIBRARIES}) + endif() +endif() + +# If Lua support is needed we build some extra Lua libraries +if (CIVETWEB_ENABLE_LUA) + include(ExternalProject) + + # Determine if we should print to the output + if (CIVETWEB_ENABLE_THIRD_PARTY_OUTPUT) + set(THIRD_PARTY_LOGGING 0) + else() + set(THIRD_PARTY_LOGGING 1) + endif() + + # If Lua is static we must build it from source + if (NOT CIVETWEB_ENABLE_LUA_SHARED) + if (LINUX) + set(LUA_MAKE_TARGET linux) + elseif(DARWIN) + set(LUA_MAKE_TARGET macosx) + elseif(FREEBSD) + set(LUA_MAKE_TARGET freebsd) + elseif(WINDOWS) + set(LUA_MAKE_TARGET mingw) + elseif(UNIX) + set(LUA_MAKE_TARGET posix) + else() + set(LUA_MAKE_TARGET generic) + endif() + set(LUA_BUILD_COMMAND "${CMAKE_MAKE_PROGRAM};${LUA_MAKE_TARGET}") + if (BUILD_SHARED_LIBS) + set(LUA_BUILD_COMMAND "${LUA_BUILD_COMMAND};MYCFLAGS=-fPIC") + endif() + ExternalProject_Add(lua + URL "http://www.lua.org/ftp/lua-${CIVETWEB_LUA_VERSION}.tar.gz" + URL_MD5 ${CIVETWEB_LUA_MD5_HASH} + PREFIX "${CIVETWEB_THIRD_PARTY_DIR}" + CONFIGURE_COMMAND "" + BUILD_COMMAND ${LUA_BUILD_COMMAND} + BUILD_IN_SOURCE 1 + INSTALL_COMMAND make install "INSTALL_TOP=" + LOG_DOWNLOAD ${THIRD_PARTY_LOGGING} + LOG_UPDATE ${THIRD_PARTY_LOGGING} + LOG_CONFIGURE ${THIRD_PARTY_LOGGING} + LOG_BUILD ${THIRD_PARTY_LOGGING} + LOG_TEST ${THIRD_PARTY_LOGGING} + LOG_INSTALL ${THIRD_PARTY_LOGGING}) + ExternalProject_Get_Property(lua INSTALL_DIR) + set(LUA_INSTALL_DIR ${INSTALL_DIR}) + unset(INSTALL_DIR) + link_directories("${LUA_INSTALL_DIR}/lib") + include_directories("${LUA_INSTALL_DIR}/include") + set(LUA_LIBRARIES "${LUA_INSTALL_DIR}/lib/liblua.a") + add_dependencies(c-library lua) + else() + find_package(Lua) + endif() + + # Lua Filesystem Support + string(REPLACE "." "_" LUA_FILESYSTEM_VERSION_UNDERSCORE ${CIVETWEB_LUA_FILESYSTEM_VERSION}) + ExternalProject_Add(luafilesystem + URL "https://github.com/keplerproject/luafilesystem/archive/v_${LUA_FILESYSTEM_VERSION_UNDERSCORE}.tar.gz" + URL_MD5 ${CIVETWEB_LUA_FILESYSTEM_MD5_HASH} + PREFIX "${CIVETWEB_THIRD_PARTY_DIR}" + PATCH_COMMAND ${CMAKE_COMMAND} -E copy + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/luafilesystem/CMakeLists.txt" /CMakeLists.txt + CMAKE_ARGS + "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}" + "-DCMAKE_INSTALL_PREFIX=" + LOG_DOWNLOAD ${THIRD_PARTY_LOGGING} + LOG_UPDATE ${THIRD_PARTY_LOGGING} + LOG_CONFIGURE ${THIRD_PARTY_LOGGING} + LOG_BUILD ${THIRD_PARTY_LOGGING} + LOG_TEST ${THIRD_PARTY_LOGGING} + LOG_INSTALL ${THIRD_PARTY_LOGGING}) + ExternalProject_Get_Property(luafilesystem INSTALL_DIR) + set(LUA_FILESYSTEM_INSTALL_DIR ${INSTALL_DIR}) + unset(INSTALL_DIR) + link_directories("${LUA_FILESYSTEM_INSTALL_DIR}/lib") + include_directories("${LUA_FILESYSTEM_INSTALL_DIR}/include") + set(LUA_LIBRARIES "${LUA_LIBRARIES};${LUA_FILESYSTEM_INSTALL_DIR}/lib/libluafilesystem.a") + add_dependencies(c-library luafilesystem) + + # Lua SQLite Support + if (${CIVETWEB_LUA_SQLITE_VERSION} VERSION_EQUAL "0.9.3") + set(LUA_SQLITE_FILENAME lsqlite3_fsl09w.zip) + elseif (${CIVETWEB_LUA_SQLITE_VERSION} VERSION_EQUAL "0.9.2") + set(LUA_SQLITE_FILENAME lsqlite3_fsl09v.zip) + elseif (${CIVETWEB_LUA_SQLITE_VERSION} VERSION_EQUAL "0.9.1") + set(LUA_SQLITE_FILENAME lsqlite3_fsl09t.zip) + else() + message(FATAL_ERROR "The Lua SQLite archive filename is unknown for version ${CIVETWEB_LUA_SQLITE_VERSION}") + endif() + ExternalProject_Add(luasqlite + URL "http://lua.sqlite.org/index.cgi/zip/${LUA_SQLITE_FILENAME}" + URL_MD5 ${CIVETWEB_LUA_SQLITE_MD5_HASH} + PREFIX "${CIVETWEB_THIRD_PARTY_DIR}" + PATCH_COMMAND ${CMAKE_COMMAND} -E copy + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/luasqlite/CMakeLists.txt" /CMakeLists.txt + CMAKE_ARGS + "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}" + "-DCMAKE_INSTALL_PREFIX=" + LOG_DOWNLOAD ${THIRD_PARTY_LOGGING} + LOG_UPDATE ${THIRD_PARTY_LOGGING} + LOG_CONFIGURE ${THIRD_PARTY_LOGGING} + LOG_BUILD ${THIRD_PARTY_LOGGING} + LOG_TEST ${THIRD_PARTY_LOGGING} + LOG_INSTALL ${THIRD_PARTY_LOGGING}) + ExternalProject_Get_Property(luasqlite INSTALL_DIR) + set(LUA_SQLITE_INSTALL_DIR ${INSTALL_DIR}) + unset(INSTALL_DIR) + link_directories("${LUA_SQLITE_INSTALL_DIR}/lib") + set(LUA_LIBRARIES "${LUA_LIBRARIES};${LUA_SQLITE_INSTALL_DIR}/lib/libluasqlite.a") + add_dependencies(c-library luasqlite) + + # Lua XML Support + if (${CIVETWEB_LUA_XML_VERSION} VERSION_EQUAL "1.8.0") + set(LUA_XML_FILENAME LuaXML_130610.zip) + elseif (${CIVETWEB_LUA_XML_VERSION} VERSION_EQUAL "1.7.4") + set(LUA_XML_FILENAME LuaXML_101012.zip) + else() + message(FATAL_ERROR "The Lua XML archive filename is unknown for version ${CIVETWEB_LUA_XML_VERSION}") + endif() + ExternalProject_Add(luaxml + URL "http://viremo.eludi.net/LuaXML/${LUA_XML_FILENAME}" + URL_MD5 ${CIVETWEB_LUA_XML_MD5_HASH} + PREFIX "${CIVETWEB_THIRD_PARTY_DIR}" + PATCH_COMMAND ${CMAKE_COMMAND} -E copy + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/luaxml/CMakeLists.txt" /CMakeLists.txt + CMAKE_ARGS + "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}" + "-DCMAKE_INSTALL_PREFIX=" + LOG_DOWNLOAD ${THIRD_PARTY_LOGGING} + LOG_UPDATE ${THIRD_PARTY_LOGGING} + LOG_CONFIGURE ${THIRD_PARTY_LOGGING} + LOG_BUILD ${THIRD_PARTY_LOGGING} + LOG_TEST ${THIRD_PARTY_LOGGING} + LOG_INSTALL ${THIRD_PARTY_LOGGING}) + ExternalProject_Get_Property(luaxml INSTALL_DIR) + set(LUA_XML_INSTALL_DIR ${INSTALL_DIR}) + unset(INSTALL_DIR) + link_directories("${LUA_XML_INSTALL_DIR}/lib") + set(LUA_LIBRARIES "${LUA_LIBRARIES};${LUA_XML_INSTALL_DIR}/lib/libluaxml.a") + add_dependencies(c-library luaxml) + + # SQLite Support + string (REGEX MATCHALL "[0-9]+" SQLITE_VERSION_MATCHES ${CIVETWEB_SQLITE_VERSION}) + list(GET SQLITE_VERSION_MATCHES 0 SQLITE_VERSION_MAJOR) + list(GET SQLITE_VERSION_MATCHES 1 SQLITE_VERSION_MINOR) + list(GET SQLITE_VERSION_MATCHES 2 SQLITE_VERSION_PATCH) + set(SQLITE_FILE_VERSION ${SQLITE_VERSION_MAJOR}0${SQLITE_VERSION_MINOR}0${SQLITE_VERSION_PATCH}00) + ExternalProject_Add(sqlite + URL "http://www.sqlite.org/2015/sqlite-amalgamation-${SQLITE_FILE_VERSION}.zip" + URL_MD5 ${CIVETWEB_SQLITE_MD5_HASH} + PREFIX "${CIVETWEB_THIRD_PARTY_DIR}" + PATCH_COMMAND ${CMAKE_COMMAND} -E copy + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/sqlite/CMakeLists.txt" /CMakeLists.txt + CMAKE_ARGS + "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}" + "-DCMAKE_INSTALL_PREFIX=" + LOG_DOWNLOAD ${THIRD_PARTY_LOGGING} + LOG_UPDATE ${THIRD_PARTY_LOGGING} + LOG_CONFIGURE ${THIRD_PARTY_LOGGING} + LOG_BUILD ${THIRD_PARTY_LOGGING} + LOG_TEST ${THIRD_PARTY_LOGGING} + LOG_INSTALL ${THIRD_PARTY_LOGGING}) + ExternalProject_Get_Property(sqlite INSTALL_DIR) + set(SQLITE_INSTALL_DIR ${INSTALL_DIR}) + unset(INSTALL_DIR) + link_directories("${SQLITE_INSTALL_DIR}/lib") + include_directories("${SQLITE_INSTALL_DIR}/include") + set(LUA_LIBRARIES "${LUA_LIBRARIES};${SQLITE_INSTALL_DIR}/lib/libsqlite.a") + add_dependencies(c-library sqlite) + + # Link all the Lua libraries + target_link_libraries(c-library ${LUA_LIBRARIES}) +endif() + +# The web server executable +add_executable(c-executable main.c) +set_target_properties(c-executable PROPERTIES + OUTPUT_NAME "civetweb" +) +if (CIVETWEB_INSTALL_EXECUTABLE) + install( + TARGETS c-executable + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin + COMPONENT server) +endif() +if (BUILD_SHARED_LIBS) + target_compile_definitions(c-executable PRIVATE CIVETWEB_DLL_IMPORTS) +endif() +target_include_directories( + c-executable PUBLIC + ${PROJECT_SOURCE_DIR}/include) +target_link_libraries(c-executable c-library) +if (LIBRT_FOUND) + target_link_libraries(c-executable LIBRT::LIBRT) +endif() + +if (CIVETWEB_ENABLE_LUA) + add_library(lua-library third_party/lfs.c third_party/lsqlite3.c third_party/LuaXML_lib.c third_party/sqlite3.c) + set_target_properties(lua-library PROPERTIES + OUTPUT_NAME "lua-library" + VERSION ${CIVETWEB_VERSION} + SOVERSION ${CIVETWEB_VERSION} + ) + target_include_directories( + lua-library PUBLIC + ${PROJECT_SOURCE_DIR}/src/third_party/lua-5.2.4) + install( + TARGETS lua-library + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin + COMPONENT lua-library) +endif() + +# The C++ API library +if (CIVETWEB_ENABLE_CXX) + add_library(civetweb-cpp CivetServer.cpp) + set_target_properties(civetweb-cpp PROPERTIES + OUTPUT_NAME "civetweb-cpp" + VERSION ${CIVETWEB_VERSION} + SOVERSION ${CIVETWEB_VERSION} + ) + if (BUILD_SHARED_LIBS) + target_compile_definitions(civetweb-cpp PRIVATE CIVETWEB_DLL_EXPORTS) + endif() + target_include_directories( + civetweb-cpp PUBLIC + ${PROJECT_SOURCE_DIR}/include) + install( + TARGETS civetweb-cpp + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin + COMPONENT civetweb-cpp) + install(FILES + ${PROJECT_SOURCE_DIR}/include/CivetServer.h + DESTINATION include + COMPONENT civetweb-cpp) +endif() diff --git a/src/civetweb/src/CivetServer.cpp b/src/civetweb/src/CivetServer.cpp new file mode 100644 index 000000000..186f18b15 --- /dev/null +++ b/src/civetweb/src/CivetServer.cpp @@ -0,0 +1,609 @@ +/* Copyright (c) 2013-2017 the Civetweb developers + * Copyright (c) 2013 No Face Press, LLC + * + * License http://opensource.org/licenses/mit-license.php MIT License + */ + +#include "CivetServer.h" + +#include +#include +#include +#include + +#ifndef UNUSED_PARAMETER +#define UNUSED_PARAMETER(x) (void)(x) +#endif + +bool +CivetHandler::handleGet(CivetServer *server, struct mg_connection *conn) +{ + UNUSED_PARAMETER(server); + UNUSED_PARAMETER(conn); + return false; +} + +bool +CivetHandler::handlePost(CivetServer *server, struct mg_connection *conn) +{ + UNUSED_PARAMETER(server); + UNUSED_PARAMETER(conn); + return false; +} + +bool +CivetHandler::handleHead(CivetServer *server, struct mg_connection *conn) +{ + UNUSED_PARAMETER(server); + UNUSED_PARAMETER(conn); + return false; +} + +bool +CivetHandler::handlePut(CivetServer *server, struct mg_connection *conn) +{ + UNUSED_PARAMETER(server); + UNUSED_PARAMETER(conn); + return false; +} + +bool +CivetHandler::handlePatch(CivetServer *server, struct mg_connection *conn) +{ + UNUSED_PARAMETER(server); + UNUSED_PARAMETER(conn); + return false; +} + +bool +CivetHandler::handleDelete(CivetServer *server, struct mg_connection *conn) +{ + UNUSED_PARAMETER(server); + UNUSED_PARAMETER(conn); + return false; +} + +bool +CivetHandler::handleOptions(CivetServer *server, struct mg_connection *conn) +{ + UNUSED_PARAMETER(server); + UNUSED_PARAMETER(conn); + return false; +} + +bool +CivetWebSocketHandler::handleConnection(CivetServer *server, + const struct mg_connection *conn) +{ + UNUSED_PARAMETER(server); + UNUSED_PARAMETER(conn); + return true; +} + +void +CivetWebSocketHandler::handleReadyState(CivetServer *server, + struct mg_connection *conn) +{ + UNUSED_PARAMETER(server); + UNUSED_PARAMETER(conn); + return; +} + +bool +CivetWebSocketHandler::handleData(CivetServer *server, + struct mg_connection *conn, + int bits, + char *data, + size_t data_len) +{ + UNUSED_PARAMETER(server); + UNUSED_PARAMETER(conn); + UNUSED_PARAMETER(bits); + UNUSED_PARAMETER(data); + UNUSED_PARAMETER(data_len); + return true; +} + +void +CivetWebSocketHandler::handleClose(CivetServer *server, + const struct mg_connection *conn) +{ + UNUSED_PARAMETER(server); + UNUSED_PARAMETER(conn); + return; +} + +int +CivetServer::requestHandler(struct mg_connection *conn, void *cbdata) +{ + const struct mg_request_info *request_info = mg_get_request_info(conn); + assert(request_info != NULL); + CivetServer *me = (CivetServer *)(request_info->user_data); + assert(me != NULL); + + // Happens when a request hits the server before the context is saved + if (me->context == NULL) + return 0; + + mg_lock_context(me->context); + me->connections[conn] = CivetConnection(); + mg_unlock_context(me->context); + + CivetHandler *handler = (CivetHandler *)cbdata; + + if (handler) { + if (strcmp(request_info->request_method, "GET") == 0) { + return handler->handleGet(me, conn) ? 1 : 0; + } else if (strcmp(request_info->request_method, "POST") == 0) { + return handler->handlePost(me, conn) ? 1 : 0; + } else if (strcmp(request_info->request_method, "HEAD") == 0) { + return handler->handleHead(me, conn) ? 1 : 0; + } else if (strcmp(request_info->request_method, "PUT") == 0) { + return handler->handlePut(me, conn) ? 1 : 0; + } else if (strcmp(request_info->request_method, "DELETE") == 0) { + return handler->handleDelete(me, conn) ? 1 : 0; + } else if (strcmp(request_info->request_method, "OPTIONS") == 0) { + return handler->handleOptions(me, conn) ? 1 : 0; + } else if (strcmp(request_info->request_method, "PATCH") == 0) { + return handler->handlePatch(me, conn) ? 1 : 0; + } + } + + return 0; // No handler found +} + +int +CivetServer::authHandler(struct mg_connection *conn, void *cbdata) +{ + const struct mg_request_info *request_info = mg_get_request_info(conn); + assert(request_info != NULL); + CivetServer *me = (CivetServer *)(request_info->user_data); + assert(me != NULL); + + // Happens when a request hits the server before the context is saved + if (me->context == NULL) + return 0; + + mg_lock_context(me->context); + me->connections[conn] = CivetConnection(); + mg_unlock_context(me->context); + + CivetAuthHandler *handler = (CivetAuthHandler *)cbdata; + + if (handler) { + return handler->authorize(me, conn) ? 1 : 0; + } + + return 0; // No handler found +} + +int +CivetServer::webSocketConnectionHandler(const struct mg_connection *conn, + void *cbdata) +{ + const struct mg_request_info *request_info = mg_get_request_info(conn); + assert(request_info != NULL); + CivetServer *me = (CivetServer *)(request_info->user_data); + assert(me != NULL); + + // Happens when a request hits the server before the context is saved + if (me->context == NULL) + return 0; + + CivetWebSocketHandler *handler = (CivetWebSocketHandler *)cbdata; + + if (handler) { + return handler->handleConnection(me, conn) ? 0 : 1; + } + + return 1; // No handler found, close connection +} + +void +CivetServer::webSocketReadyHandler(struct mg_connection *conn, void *cbdata) +{ + const struct mg_request_info *request_info = mg_get_request_info(conn); + assert(request_info != NULL); + CivetServer *me = (CivetServer *)(request_info->user_data); + assert(me != NULL); + + // Happens when a request hits the server before the context is saved + if (me->context == NULL) + return; + + CivetWebSocketHandler *handler = (CivetWebSocketHandler *)cbdata; + + if (handler) { + handler->handleReadyState(me, conn); + } +} + +int +CivetServer::webSocketDataHandler(struct mg_connection *conn, + int bits, + char *data, + size_t data_len, + void *cbdata) +{ + const struct mg_request_info *request_info = mg_get_request_info(conn); + assert(request_info != NULL); + CivetServer *me = (CivetServer *)(request_info->user_data); + assert(me != NULL); + + // Happens when a request hits the server before the context is saved + if (me->context == NULL) + return 0; + + CivetWebSocketHandler *handler = (CivetWebSocketHandler *)cbdata; + + if (handler) { + return handler->handleData(me, conn, bits, data, data_len) ? 1 : 0; + } + + return 1; // No handler found +} + +void +CivetServer::webSocketCloseHandler(const struct mg_connection *conn, + void *cbdata) +{ + const struct mg_request_info *request_info = mg_get_request_info(conn); + assert(request_info != NULL); + CivetServer *me = (CivetServer *)(request_info->user_data); + assert(me != NULL); + + // Happens when a request hits the server before the context is saved + if (me->context == NULL) + return; + + CivetWebSocketHandler *handler = (CivetWebSocketHandler *)cbdata; + + if (handler) { + handler->handleClose(me, conn); + } +} + +CivetCallbacks::CivetCallbacks() +{ + memset(this, 0, sizeof(*this)); +} + +CivetServer::CivetServer(const char **options, + const struct CivetCallbacks *_callbacks, + const void *UserContextIn) + : context(0) +{ + struct CivetCallbacks callbacks; + + UserContext = UserContextIn; + + if (_callbacks) { + callbacks = *_callbacks; + userCloseHandler = _callbacks->connection_close; + } else { + userCloseHandler = NULL; + } + callbacks.connection_close = closeHandler; + context = mg_start(&callbacks, this, options); + if (context == NULL) + throw CivetException("null context when constructing CivetServer. " + "Possible problem binding to port."); +} + +CivetServer::CivetServer(std::vector options, + const struct CivetCallbacks *_callbacks, + const void *UserContextIn) + : context(0) +{ + struct CivetCallbacks callbacks; + + UserContext = UserContextIn; + + if (_callbacks) { + callbacks = *_callbacks; + userCloseHandler = _callbacks->connection_close; + } else { + userCloseHandler = NULL; + } + callbacks.connection_close = closeHandler; + + std::vector pointers(options.size()); + for (size_t i = 0; i < options.size(); i++) { + pointers[i] = (options[i].c_str()); + } + pointers.push_back(0); + + context = mg_start(&callbacks, this, &pointers[0]); + if (context == NULL) + throw CivetException("null context when constructing CivetServer. " + "Possible problem binding to port."); +} + +CivetServer::~CivetServer() +{ + close(); +} + +void +CivetServer::closeHandler(const struct mg_connection *conn) +{ + CivetServer *me = (CivetServer *)mg_get_user_data(mg_get_context(conn)); + assert(me != NULL); + + // Happens when a request hits the server before the context is saved + if (me->context == NULL) + return; + + if (me->userCloseHandler) { + me->userCloseHandler(conn); + } + mg_lock_context(me->context); + me->connections.erase(const_cast(conn)); + mg_unlock_context(me->context); +} + +void +CivetServer::addHandler(const std::string &uri, CivetHandler *handler) +{ + mg_set_request_handler(context, uri.c_str(), requestHandler, handler); +} + +void +CivetServer::addWebSocketHandler(const std::string &uri, + CivetWebSocketHandler *handler) +{ + mg_set_websocket_handler(context, + uri.c_str(), + webSocketConnectionHandler, + webSocketReadyHandler, + webSocketDataHandler, + webSocketCloseHandler, + handler); +} + +void +CivetServer::addAuthHandler(const std::string &uri, CivetAuthHandler *handler) +{ + mg_set_auth_handler(context, uri.c_str(), authHandler, handler); +} + +void +CivetServer::removeHandler(const std::string &uri) +{ + mg_set_request_handler(context, uri.c_str(), NULL, NULL); +} + +void +CivetServer::removeWebSocketHandler(const std::string &uri) +{ + mg_set_websocket_handler( + context, uri.c_str(), NULL, NULL, NULL, NULL, NULL); +} + +void +CivetServer::removeAuthHandler(const std::string &uri) +{ + mg_set_auth_handler(context, uri.c_str(), NULL, NULL); +} + +void +CivetServer::close() +{ + if (context) { + mg_stop(context); + context = 0; + } +} + +int +CivetServer::getCookie(struct mg_connection *conn, + const std::string &cookieName, + std::string &cookieValue) +{ + // Maximum cookie length as per microsoft is 4096. + // http://msdn.microsoft.com/en-us/library/ms178194.aspx + char _cookieValue[4096]; + const char *cookie = mg_get_header(conn, "Cookie"); + int lRead = mg_get_cookie(cookie, + cookieName.c_str(), + _cookieValue, + sizeof(_cookieValue)); + cookieValue.clear(); + cookieValue.append(_cookieValue); + return lRead; +} + +const char * +CivetServer::getHeader(struct mg_connection *conn, + const std::string &headerName) +{ + return mg_get_header(conn, headerName.c_str()); +} + +void +CivetServer::urlDecode(const char *src, + std::string &dst, + bool is_form_url_encoded) +{ + urlDecode(src, strlen(src), dst, is_form_url_encoded); +} + +void +CivetServer::urlDecode(const char *src, + size_t src_len, + std::string &dst, + bool is_form_url_encoded) +{ + int i, j, a, b; +#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W') + + dst.clear(); + for (i = j = 0; i < (int)src_len; i++, j++) { + if (i < (int)src_len - 2 && src[i] == '%' + && isxdigit(*(const unsigned char *)(src + i + 1)) + && isxdigit(*(const unsigned char *)(src + i + 2))) { + a = tolower(*(const unsigned char *)(src + i + 1)); + b = tolower(*(const unsigned char *)(src + i + 2)); + dst.push_back((char)((HEXTOI(a) << 4) | HEXTOI(b))); + i += 2; + } else if (is_form_url_encoded && src[i] == '+') { + dst.push_back(' '); + } else { + dst.push_back(src[i]); + } + } +} + +bool +CivetServer::getParam(struct mg_connection *conn, + const char *name, + std::string &dst, + size_t occurrence) +{ + const char *formParams = NULL; + const struct mg_request_info *ri = mg_get_request_info(conn); + assert(ri != NULL); + CivetServer *me = (CivetServer *)(ri->user_data); + assert(me != NULL); + mg_lock_context(me->context); + CivetConnection &conobj = me->connections[conn]; + mg_lock_connection(conn); + mg_unlock_context(me->context); + + if (conobj.postData != NULL) { + formParams = conobj.postData; + } else { + const char *con_len_str = mg_get_header(conn, "Content-Length"); + if (con_len_str) { + unsigned long con_len = atoi(con_len_str); + if (con_len > 0) { + // Add one extra character: in case the post-data is a text, it + // is required as 0-termination. + // Do not increment con_len, since the 0 terminating is not part + // of the content (text or binary). + conobj.postData = (char *)malloc(con_len + 1); + if (conobj.postData != NULL) { + // malloc may fail for huge requests + mg_read(conn, conobj.postData, con_len); + conobj.postData[con_len] = 0; + formParams = conobj.postData; + conobj.postDataLen = con_len; + } + } + } + } + if (formParams == NULL) { + // get requests do store html
field values in the http + // query_string + formParams = ri->query_string; + } + mg_unlock_connection(conn); + + if (formParams != NULL) { + return getParam(formParams, strlen(formParams), name, dst, occurrence); + } + + return false; +} + +bool +CivetServer::getParam(const char *data, + size_t data_len, + const char *name, + std::string &dst, + size_t occurrence) +{ + const char *p, *e, *s; + size_t name_len; + + dst.clear(); + if (data == NULL || name == NULL || data_len == 0) { + return false; + } + name_len = strlen(name); + e = data + data_len; + + // data is "var1=val1&var2=val2...". Find variable first + for (p = data; p + name_len < e; p++) { + if ((p == data || p[-1] == '&') && p[name_len] == '=' + && !mg_strncasecmp(name, p, name_len) && 0 == occurrence--) { + + // Point p to variable value + p += name_len + 1; + + // Point s to the end of the value + s = (const char *)memchr(p, '&', (size_t)(e - p)); + if (s == NULL) { + s = e; + } + assert(s >= p); + + // Decode variable into destination buffer + urlDecode(p, (int)(s - p), dst, true); + return true; + } + } + return false; +} + +void +CivetServer::urlEncode(const char *src, std::string &dst, bool append) +{ + urlEncode(src, strlen(src), dst, append); +} + +void +CivetServer::urlEncode(const char *src, + size_t src_len, + std::string &dst, + bool append) +{ + static const char *dont_escape = "._-$,;~()"; + static const char *hex = "0123456789abcdef"; + + if (!append) + dst.clear(); + + for (; src_len > 0; src++, src_len--) { + if (isalnum(*(const unsigned char *)src) + || strchr(dont_escape, *(const unsigned char *)src) != NULL) { + dst.push_back(*src); + } else { + dst.push_back('%'); + dst.push_back(hex[(*(const unsigned char *)src) >> 4]); + dst.push_back(hex[(*(const unsigned char *)src) & 0xf]); + } + } +} + +std::vector +CivetServer::getListeningPorts() +{ + std::vector ports(50); + std::vector server_ports(50); + int size = mg_get_server_ports(context, + (int)server_ports.size(), + &server_ports[0]); + if (size <= 0) { + ports.resize(0); + return ports; + } + ports.resize(size); + server_ports.resize(size); + for (int i = 0; i < size; i++) { + ports[i] = server_ports[i].port; + } + + return ports; +} + +CivetServer::CivetConnection::CivetConnection() +{ + postData = NULL; + postDataLen = 0; +} + +CivetServer::CivetConnection::~CivetConnection() +{ + free(postData); +} diff --git a/src/civetweb/src/civetweb.c b/src/civetweb/src/civetweb.c new file mode 100644 index 000000000..76f097ac1 --- /dev/null +++ b/src/civetweb/src/civetweb.c @@ -0,0 +1,18073 @@ +/* Copyright (c) 2013-2017 the Civetweb developers + * Copyright (c) 2004-2013 Sergey Lyubka + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#if defined(_WIN32) +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS /* Disable deprecation warning in VS2005 */ +#endif +#ifndef _WIN32_WINNT /* defined for tdm-gcc so we can use getnameinfo */ +#define _WIN32_WINNT 0x0501 +#endif +#else +#if defined(__GNUC__) && !defined(_GNU_SOURCE) +#define _GNU_SOURCE /* for setgroups() */ +#endif +#if defined(__linux__) && !defined(_XOPEN_SOURCE) +#define _XOPEN_SOURCE 600 /* For flockfile() on Linux */ +#endif +#ifndef _LARGEFILE_SOURCE +#define _LARGEFILE_SOURCE /* For fseeko(), ftello() */ +#endif +#ifndef _FILE_OFFSET_BITS +#define _FILE_OFFSET_BITS 64 /* Use 64-bit file offsets by default */ +#endif +#ifndef __STDC_FORMAT_MACROS +#define __STDC_FORMAT_MACROS /* wants this for C++ */ +#endif +#ifndef __STDC_LIMIT_MACROS +#define __STDC_LIMIT_MACROS /* C++ wants that for INT64_MAX */ +#endif +#ifdef __sun +#define __EXTENSIONS__ /* to expose flockfile and friends in stdio.h */ +#define __inline inline /* not recognized on older compiler versions */ +#endif +#endif + +#if defined(USE_LUA) +#define USE_TIMERS +#endif + +#if defined(_MSC_VER) +/* 'type cast' : conversion from 'int' to 'HANDLE' of greater size */ +#pragma warning(disable : 4306) +/* conditional expression is constant: introduced by FD_SET(..) */ +#pragma warning(disable : 4127) +/* non-constant aggregate initializer: issued due to missing C99 support */ +#pragma warning(disable : 4204) +/* padding added after data member */ +#pragma warning(disable : 4820) +/* not defined as a preprocessor macro, replacing with '0' for '#if/#elif' */ +#pragma warning(disable : 4668) +/* no function prototype given: converting '()' to '(void)' */ +#pragma warning(disable : 4255) +/* function has been selected for automatic inline expansion */ +#pragma warning(disable : 4711) +#endif + + +/* This code uses static_assert to check some conditions. + * Unfortunately some compilers still do not support it, so we have a + * replacement function here. */ +#if defined(_MSC_VER) && (_MSC_VER >= 1600) +#define mg_static_assert static_assert +#elif defined(__cplusplus) && (__cplusplus >= 201103L) +#define mg_static_assert static_assert +#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) +#define mg_static_assert _Static_assert +#else +char static_assert_replacement[1]; +#define mg_static_assert(cond, txt) \ + extern char static_assert_replacement[(cond) ? 1 : -1] +#endif + +mg_static_assert(sizeof(int) == 4 || sizeof(int) == 8, + "int data type size check"); +mg_static_assert(sizeof(void *) == 4 || sizeof(void *) == 8, + "pointer data type size check"); +mg_static_assert(sizeof(void *) >= sizeof(int), "data type size check"); + + +/* Alternative queue is well tested and should be the new default */ +#ifdef NO_ALTERNATIVE_QUEUE +#ifdef ALTERNATIVE_QUEUE +#error "Define ALTERNATIVE_QUEUE or NO_ALTERNATIVE_QUEUE or none, but not both" +#endif +#else +#define ALTERNATIVE_QUEUE +#endif + + +/* DTL -- including winsock2.h works better if lean and mean */ +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif + +#if defined(__SYMBIAN32__) +/* According to https://en.wikipedia.org/wiki/Symbian#History, + * Symbian is no longer maintained since 2014-01-01. + * Recent versions of CivetWeb are no longer tested for Symbian. + * It makes no sense, to support an abandoned operating system. + * All remaining "#ifdef __SYMBIAN__" cases will be droped from + * the code sooner or later. + */ +#pragma message \ + "Symbian is no longer maintained. CivetWeb will drop Symbian support." +#define NO_SSL /* SSL is not supported */ +#define NO_CGI /* CGI is not supported */ +#define PATH_MAX FILENAME_MAX +#endif /* __SYMBIAN32__ */ + + +#ifndef CIVETWEB_HEADER_INCLUDED +/* Include the header file here, so the CivetWeb interface is defined for the + * entire implementation, including the following forward definitions. */ +#include "civetweb.h" +#endif + + +#ifndef IGNORE_UNUSED_RESULT +#define IGNORE_UNUSED_RESULT(a) ((void)((a) && 1)) +#endif + + +#if defined(__GNUC__) || defined(__MINGW32__) + +/* GCC unused function attribute seems fundamentally broken. + * Several attempts to tell the compiler "THIS FUNCTION MAY BE USED + * OR UNUSED" for individual functions failed. + * Either the compiler creates an "unused-function" warning if a + * function is not marked with __attribute__((unused)). + * On the other hand, if the function is marked with this attribute, + * but is used, the compiler raises a completely idiotic + * "used-but-marked-unused" warning - and + * #pragma GCC diagnostic ignored "-Wused-but-marked-unused" + * raises error: unknown option after ‘#pragma GCC diagnostic’. + * Disable this warning completely, until the GCC guys sober up + * again. + */ + +#pragma GCC diagnostic ignored "-Wunused-function" + +#define FUNCTION_MAY_BE_UNUSED /* __attribute__((unused)) */ + +#else +#define FUNCTION_MAY_BE_UNUSED +#endif + + +#ifndef _WIN32_WCE /* Some ANSI #includes are not available on Windows CE */ +#include +#include +#include +#include +#include +#endif /* !_WIN32_WCE */ + + +#ifdef __clang__ +/* When using -Weverything, clang does not accept it's own headers + * in a release build configuration. Disable what is too much in + * -Weverything. */ +#pragma clang diagnostic ignored "-Wdisabled-macro-expansion" +#endif + +#if defined(__GNUC__) || defined(__MINGW32__) +/* Who on earth came to the conclusion, using __DATE__ should rise + * an "expansion of date or time macro is not reproducible" + * warning. That's exactly what was intended by using this macro. + * Just disable this nonsense warning. */ + +/* And disabling them does not work either: + * #pragma clang diagnostic ignored "-Wno-error=date-time" + * #pragma clang diagnostic ignored "-Wdate-time" + * So we just have to disable ALL warnings for some lines + * of code. + */ +#endif + + +#ifdef __MACH__ /* Apple OSX section */ + +#ifdef __clang__ +#if (__clang_major__ == 3) && ((__clang_minor__ == 7) || (__clang_minor__ == 8)) +/* Avoid warnings for Xcode 7. It seems it does no longer exist in Xcode 8 */ +#pragma clang diagnostic ignored "-Wno-reserved-id-macro" +#pragma clang diagnostic ignored "-Wno-keyword-macro" +#endif +#endif + +#define CLOCK_MONOTONIC (1) +#define CLOCK_REALTIME (2) + +#include +#include +#include +#include +#include +#include + +/* clock_gettime is not implemented on OSX prior to 10.12 */ +static int +_civet_clock_gettime(int clk_id, struct timespec *t) +{ + memset(t, 0, sizeof(*t)); + if (clk_id == CLOCK_REALTIME) { + struct timeval now; + int rv = gettimeofday(&now, NULL); + if (rv) { + return rv; + } + t->tv_sec = now.tv_sec; + t->tv_nsec = now.tv_usec * 1000; + return 0; + + } else if (clk_id == CLOCK_MONOTONIC) { + static uint64_t clock_start_time = 0; + static mach_timebase_info_data_t timebase_ifo = {0, 0}; + + uint64_t now = mach_absolute_time(); + + if (clock_start_time == 0) { + kern_return_t mach_status = mach_timebase_info(&timebase_ifo); +#if defined(DEBUG) + assert(mach_status == KERN_SUCCESS); +#else + /* appease "unused variable" warning for release builds */ + (void)mach_status; +#endif + clock_start_time = now; + } + + now = (uint64_t)((double)(now - clock_start_time) + * (double)timebase_ifo.numer + / (double)timebase_ifo.denom); + + t->tv_sec = now / 1000000000; + t->tv_nsec = now % 1000000000; + return 0; + } + return -1; /* EINVAL - Clock ID is unknown */ +} + +/* if clock_gettime is declared, then __CLOCK_AVAILABILITY will be defined */ +#ifdef __CLOCK_AVAILABILITY +/* If we compiled with Mac OSX 10.12 or later, then clock_gettime will be + * declared but it may be NULL at runtime. So we need to check before using + * it. */ +static int +_civet_safe_clock_gettime(int clk_id, struct timespec *t) +{ + if (clock_gettime) { + return clock_gettime(clk_id, t); + } + return _civet_clock_gettime(clk_id, t); +} +#define clock_gettime _civet_safe_clock_gettime +#else +#define clock_gettime _civet_clock_gettime +#endif + +#endif + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef INT64_MAX +#define INT64_MAX (9223372036854775807) +#endif + + +#ifndef MAX_WORKER_THREADS +#define MAX_WORKER_THREADS (1024 * 64) +#endif + +#ifndef SOCKET_TIMEOUT_QUANTUM /* in ms */ +#define SOCKET_TIMEOUT_QUANTUM (2000) +#endif + +#define SHUTDOWN_RD (0) +#define SHUTDOWN_WR (1) +#define SHUTDOWN_BOTH (2) + +mg_static_assert(MAX_WORKER_THREADS >= 1, + "worker threads must be a positive number"); + +mg_static_assert(sizeof(size_t) == 4 || sizeof(size_t) == 8, + "size_t data type size check"); + +#if defined(_WIN32) \ + && !defined(__SYMBIAN32__) /* WINDOWS / UNIX include block */ +#include +#include /* DTL add for SO_EXCLUSIVE */ +#include + +typedef const char *SOCK_OPT_TYPE; + +#if !defined(PATH_MAX) +#define PATH_MAX (MAX_PATH) +#endif + +#if !defined(PATH_MAX) +#define PATH_MAX (4096) +#endif + +mg_static_assert(PATH_MAX >= 1, "path length must be a positive number"); + +#ifndef _IN_PORT_T +#ifndef in_port_t +#define in_port_t u_short +#endif +#endif + +#ifndef _WIN32_WCE +#include +#include +#include +#else /* _WIN32_WCE */ +#define NO_CGI /* WinCE has no pipes */ +#define NO_POPEN /* WinCE has no popen */ + +typedef long off_t; + +#define errno ((int)(GetLastError())) +#define strerror(x) (_ultoa(x, (char *)_alloca(sizeof(x) * 3), 10)) +#endif /* _WIN32_WCE */ + +#define MAKEUQUAD(lo, hi) \ + ((uint64_t)(((uint32_t)(lo)) | ((uint64_t)((uint32_t)(hi))) << 32)) +#define RATE_DIFF (10000000) /* 100 nsecs */ +#define EPOCH_DIFF (MAKEUQUAD(0xd53e8000, 0x019db1de)) +#define SYS2UNIX_TIME(lo, hi) \ + ((time_t)((MAKEUQUAD((lo), (hi)) - EPOCH_DIFF) / RATE_DIFF)) + +/* Visual Studio 6 does not know __func__ or __FUNCTION__ + * The rest of MS compilers use __FUNCTION__, not C99 __func__ + * Also use _strtoui64 on modern M$ compilers */ +#if defined(_MSC_VER) +#if (_MSC_VER < 1300) +#define STRX(x) #x +#define STR(x) STRX(x) +#define __func__ __FILE__ ":" STR(__LINE__) +#define strtoull(x, y, z) ((unsigned __int64)_atoi64(x)) +#define strtoll(x, y, z) (_atoi64(x)) +#else +#define __func__ __FUNCTION__ +#define strtoull(x, y, z) (_strtoui64(x, y, z)) +#define strtoll(x, y, z) (_strtoi64(x, y, z)) +#endif +#endif /* _MSC_VER */ + +#define ERRNO ((int)(GetLastError())) +#define NO_SOCKLEN_T + +#if defined(_WIN64) || defined(__MINGW64__) +#define SSL_LIB "ssleay64.dll" +#define CRYPTO_LIB "libeay64.dll" +#else +#define SSL_LIB "ssleay32.dll" +#define CRYPTO_LIB "libeay32.dll" +#endif + +#define O_NONBLOCK (0) +#ifndef W_OK +#define W_OK (2) /* http://msdn.microsoft.com/en-us/library/1w06ktdy.aspx */ +#endif +#if !defined(EWOULDBLOCK) +#define EWOULDBLOCK WSAEWOULDBLOCK +#endif /* !EWOULDBLOCK */ +#define _POSIX_ +#define INT64_FMT "I64d" +#define UINT64_FMT "I64u" + +#define WINCDECL __cdecl +#define vsnprintf_impl _vsnprintf +#define access _access +#define mg_sleep(x) (Sleep(x)) + +#define pipe(x) _pipe(x, MG_BUF_LEN, _O_BINARY) +#ifndef popen +#define popen(x, y) (_popen(x, y)) +#endif +#ifndef pclose +#define pclose(x) (_pclose(x)) +#endif +#define close(x) (_close(x)) +#define dlsym(x, y) (GetProcAddress((HINSTANCE)(x), (y))) +#define RTLD_LAZY (0) +#define fseeko(x, y, z) ((_lseeki64(_fileno(x), (y), (z)) == -1) ? -1 : 0) +#define fdopen(x, y) (_fdopen((x), (y))) +#define write(x, y, z) (_write((x), (y), (unsigned)z)) +#define read(x, y, z) (_read((x), (y), (unsigned)z)) +#define flockfile(x) (EnterCriticalSection(&global_log_file_lock)) +#define funlockfile(x) (LeaveCriticalSection(&global_log_file_lock)) +#define sleep(x) (Sleep((x)*1000)) +#define rmdir(x) (_rmdir(x)) +#define timegm(x) (_mkgmtime(x)) + +#if !defined(fileno) +#define fileno(x) (_fileno(x)) +#endif /* !fileno MINGW #defines fileno */ + +typedef HANDLE pthread_mutex_t; +typedef DWORD pthread_key_t; +typedef HANDLE pthread_t; +typedef struct { + CRITICAL_SECTION threadIdSec; + struct mg_workerTLS *waiting_thread; /* The chain of threads */ +} pthread_cond_t; + +#ifndef __clockid_t_defined +typedef DWORD clockid_t; +#endif +#ifndef CLOCK_MONOTONIC +#define CLOCK_MONOTONIC (1) +#endif +#ifndef CLOCK_REALTIME +#define CLOCK_REALTIME (2) +#endif +#ifndef CLOCK_THREAD +#define CLOCK_THREAD (3) +#endif +#ifndef CLOCK_PROCESS +#define CLOCK_PROCESS (4) +#endif + + +#if defined(_MSC_VER) && (_MSC_VER >= 1900) +#define _TIMESPEC_DEFINED +#endif +#ifndef _TIMESPEC_DEFINED +struct timespec { + time_t tv_sec; /* seconds */ + long tv_nsec; /* nanoseconds */ +}; +#endif + +#if !defined(WIN_PTHREADS_TIME_H) +#define MUST_IMPLEMENT_CLOCK_GETTIME +#endif + +#ifdef MUST_IMPLEMENT_CLOCK_GETTIME +#define clock_gettime mg_clock_gettime +static int +clock_gettime(clockid_t clk_id, struct timespec *tp) +{ + FILETIME ft; + ULARGE_INTEGER li, li2; + BOOL ok = FALSE; + double d; + static double perfcnt_per_sec = 0.0; + + if (tp) { + memset(tp, 0, sizeof(*tp)); + + if (clk_id == CLOCK_REALTIME) { + + /* BEGIN: CLOCK_REALTIME = wall clock (date and time) */ + GetSystemTimeAsFileTime(&ft); + li.LowPart = ft.dwLowDateTime; + li.HighPart = ft.dwHighDateTime; + li.QuadPart -= 116444736000000000; /* 1.1.1970 in filedate */ + tp->tv_sec = (time_t)(li.QuadPart / 10000000); + tp->tv_nsec = (long)(li.QuadPart % 10000000) * 100; + ok = TRUE; + /* END: CLOCK_REALTIME */ + + } else if (clk_id == CLOCK_MONOTONIC) { + + /* BEGIN: CLOCK_MONOTONIC = stopwatch (time differences) */ + if (perfcnt_per_sec == 0.0) { + QueryPerformanceFrequency((LARGE_INTEGER *)&li); + perfcnt_per_sec = 1.0 / li.QuadPart; + } + if (perfcnt_per_sec != 0.0) { + QueryPerformanceCounter((LARGE_INTEGER *)&li); + d = li.QuadPart * perfcnt_per_sec; + tp->tv_sec = (time_t)d; + d -= tp->tv_sec; + tp->tv_nsec = (long)(d * 1.0E9); + ok = TRUE; + } + /* END: CLOCK_MONOTONIC */ + + } else if (clk_id == CLOCK_THREAD) { + + /* BEGIN: CLOCK_THREAD = CPU usage of thread */ + FILETIME t_create, t_exit, t_kernel, t_user; + if (GetThreadTimes(GetCurrentThread(), + &t_create, + &t_exit, + &t_kernel, + &t_user)) { + li.LowPart = t_user.dwLowDateTime; + li.HighPart = t_user.dwHighDateTime; + li2.LowPart = t_kernel.dwLowDateTime; + li2.HighPart = t_kernel.dwHighDateTime; + li.QuadPart += li2.QuadPart; + tp->tv_sec = (time_t)(li.QuadPart / 10000000); + tp->tv_nsec = (long)(li.QuadPart % 10000000) * 100; + ok = TRUE; + } + /* END: CLOCK_THREAD */ + + } else if (clk_id == CLOCK_PROCESS) { + + /* BEGIN: CLOCK_PROCESS = CPU usage of process */ + FILETIME t_create, t_exit, t_kernel, t_user; + if (GetProcessTimes(GetCurrentProcess(), + &t_create, + &t_exit, + &t_kernel, + &t_user)) { + li.LowPart = t_user.dwLowDateTime; + li.HighPart = t_user.dwHighDateTime; + li2.LowPart = t_kernel.dwLowDateTime; + li2.HighPart = t_kernel.dwHighDateTime; + li.QuadPart += li2.QuadPart; + tp->tv_sec = (time_t)(li.QuadPart / 10000000); + tp->tv_nsec = (long)(li.QuadPart % 10000000) * 100; + ok = TRUE; + } + /* END: CLOCK_PROCESS */ + + } else { + + /* BEGIN: unknown clock */ + /* ok = FALSE; already set by init */ + /* END: unknown clock */ + } + } + + return ok ? 0 : -1; +} +#endif + + +#define pid_t HANDLE /* MINGW typedefs pid_t to int. Using #define here. */ + +static int pthread_mutex_lock(pthread_mutex_t *); +static int pthread_mutex_unlock(pthread_mutex_t *); +static void path_to_unicode(const struct mg_connection *conn, + const char *path, + wchar_t *wbuf, + size_t wbuf_len); + +/* All file operations need to be rewritten to solve #246. */ + +#include "file_ops.inl" + +struct mg_file; + +static const char * +mg_fgets(char *buf, size_t size, struct mg_file *filep, char **p); + + +/* POSIX dirent interface */ +struct dirent { + char d_name[PATH_MAX]; +}; + +typedef struct DIR { + HANDLE handle; + WIN32_FIND_DATAW info; + struct dirent result; +} DIR; + +#if defined(_WIN32) && !defined(POLLIN) +#ifndef HAVE_POLL +struct pollfd { + SOCKET fd; + short events; + short revents; +}; +#define POLLIN (0x0300) +#endif +#endif + +/* Mark required libraries */ +#if defined(_MSC_VER) +#pragma comment(lib, "Ws2_32.lib") +#endif + +#else /* defined(_WIN32) && !defined(__SYMBIAN32__) - \ + WINDOWS / UNIX include block */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +typedef const void *SOCK_OPT_TYPE; + +#if defined(ANDROID) +typedef unsigned short int in_port_t; +#endif + +#include +#include +#include +#include +#define vsnprintf_impl vsnprintf + +#if !defined(NO_SSL_DL) && !defined(NO_SSL) +#include +#endif +#include +#if defined(__MACH__) +#define SSL_LIB "libssl.dylib" +#define CRYPTO_LIB "libcrypto.dylib" +#else +#if !defined(SSL_LIB) +#define SSL_LIB "libssl.so" +#endif +#if !defined(CRYPTO_LIB) +#define CRYPTO_LIB "libcrypto.so" +#endif +#endif +#ifndef O_BINARY +#define O_BINARY (0) +#endif /* O_BINARY */ +#define closesocket(a) (close(a)) +#define mg_mkdir(conn, path, mode) (mkdir(path, mode)) +#define mg_remove(conn, x) (remove(x)) +#define mg_sleep(x) (usleep((x)*1000)) +#define mg_opendir(conn, x) (opendir(x)) +#define mg_closedir(x) (closedir(x)) +#define mg_readdir(x) (readdir(x)) +#define ERRNO (errno) +#define INVALID_SOCKET (-1) +#define INT64_FMT PRId64 +#define UINT64_FMT PRIu64 +typedef int SOCKET; +#define WINCDECL + +#if defined(__hpux) +/* HPUX 11 does not have monotonic, fall back to realtime */ +#ifndef CLOCK_MONOTONIC +#define CLOCK_MONOTONIC CLOCK_REALTIME +#endif + +/* HPUX defines socklen_t incorrectly as size_t which is 64bit on + * Itanium. Without defining _XOPEN_SOURCE or _XOPEN_SOURCE_EXTENDED + * the prototypes use int* rather than socklen_t* which matches the + * actual library expectation. When called with the wrong size arg + * accept() returns a zero client inet addr and check_acl() always + * fails. Since socklen_t is widely used below, just force replace + * their typedef with int. - DTL + */ +#define socklen_t int +#endif /* hpux */ + +#endif /* defined(_WIN32) && !defined(__SYMBIAN32__) - \ + WINDOWS / UNIX include block */ + +/* va_copy should always be a macro, C99 and C++11 - DTL */ +#ifndef va_copy +#define va_copy(x, y) ((x) = (y)) +#endif + +#ifdef _WIN32 +/* Create substitutes for POSIX functions in Win32. */ + +#if defined(__MINGW32__) +/* Show no warning in case system functions are not used. */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +#endif + + +static CRITICAL_SECTION global_log_file_lock; + +FUNCTION_MAY_BE_UNUSED +static DWORD +pthread_self(void) +{ + return GetCurrentThreadId(); +} + + +FUNCTION_MAY_BE_UNUSED +static int +pthread_key_create( + pthread_key_t *key, + void (*_ignored)(void *) /* destructor not supported for Windows */ + ) +{ + (void)_ignored; + + if ((key != 0)) { + *key = TlsAlloc(); + return (*key != TLS_OUT_OF_INDEXES) ? 0 : -1; + } + return -2; +} + + +FUNCTION_MAY_BE_UNUSED +static int +pthread_key_delete(pthread_key_t key) +{ + return TlsFree(key) ? 0 : 1; +} + + +FUNCTION_MAY_BE_UNUSED +static int +pthread_setspecific(pthread_key_t key, void *value) +{ + return TlsSetValue(key, value) ? 0 : 1; +} + + +FUNCTION_MAY_BE_UNUSED +static void * +pthread_getspecific(pthread_key_t key) +{ + return TlsGetValue(key); +} + +#if defined(__MINGW32__) +/* Enable unused function warning again */ +#pragma GCC diagnostic pop +#endif + +static struct pthread_mutex_undefined_struct *pthread_mutex_attr = NULL; +#else +static pthread_mutexattr_t pthread_mutex_attr; +#endif /* _WIN32 */ + + +#define PASSWORDS_FILE_NAME ".htpasswd" +#define CGI_ENVIRONMENT_SIZE (4096) +#define MAX_CGI_ENVIR_VARS (256) +#define MG_BUF_LEN (8192) + +#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) + + +#if defined(_WIN32_WCE) +/* Create substitutes for POSIX functions in Win32. */ + +#if defined(__MINGW32__) +/* Show no warning in case system functions are not used. */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +#endif + + +FUNCTION_MAY_BE_UNUSED +static time_t +time(time_t *ptime) +{ + time_t t; + SYSTEMTIME st; + FILETIME ft; + + GetSystemTime(&st); + SystemTimeToFileTime(&st, &ft); + t = SYS2UNIX_TIME(ft.dwLowDateTime, ft.dwHighDateTime); + + if (ptime != NULL) { + *ptime = t; + } + + return t; +} + + +FUNCTION_MAY_BE_UNUSED +static struct tm * +localtime_s(const time_t *ptime, struct tm *ptm) +{ + int64_t t = ((int64_t)*ptime) * RATE_DIFF + EPOCH_DIFF; + FILETIME ft, lft; + SYSTEMTIME st; + TIME_ZONE_INFORMATION tzinfo; + + if (ptm == NULL) { + return NULL; + } + + *(int64_t *)&ft = t; + FileTimeToLocalFileTime(&ft, &lft); + FileTimeToSystemTime(&lft, &st); + ptm->tm_year = st.wYear - 1900; + ptm->tm_mon = st.wMonth - 1; + ptm->tm_wday = st.wDayOfWeek; + ptm->tm_mday = st.wDay; + ptm->tm_hour = st.wHour; + ptm->tm_min = st.wMinute; + ptm->tm_sec = st.wSecond; + ptm->tm_yday = 0; /* hope nobody uses this */ + ptm->tm_isdst = + (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_DAYLIGHT) ? 1 : 0; + + return ptm; +} + + +FUNCTION_MAY_BE_UNUSED +static struct tm * +gmtime_s(const time_t *ptime, struct tm *ptm) +{ + /* FIXME(lsm): fix this. */ + return localtime_s(ptime, ptm); +} + + +static int mg_atomic_inc(volatile int *addr); +static struct tm tm_array[MAX_WORKER_THREADS]; +static int tm_index = 0; + + +FUNCTION_MAY_BE_UNUSED +static struct tm * +localtime(const time_t *ptime) +{ + int i = mg_atomic_inc(&tm_index) % (sizeof(tm_array) / sizeof(tm_array[0])); + return localtime_s(ptime, tm_array + i); +} + + +FUNCTION_MAY_BE_UNUSED +static struct tm * +gmtime(const time_t *ptime) +{ + int i = mg_atomic_inc(&tm_index) % ARRAY_SIZE(tm_array); + return gmtime_s(ptime, tm_array + i); +} + + +FUNCTION_MAY_BE_UNUSED +static size_t +strftime(char *dst, size_t dst_size, const char *fmt, const struct tm *tm) +{ + /* TODO: (void)mg_snprintf(NULL, dst, dst_size, "implement strftime() + * for WinCE"); */ + return 0; +} + +#define _beginthreadex(psec, stack, func, prm, flags, ptid) \ + (uintptr_t) CreateThread(psec, stack, func, prm, flags, ptid) + +#define remove(f) mg_remove(NULL, f) + + +FUNCTION_MAY_BE_UNUSED +static int +rename(const char *a, const char *b) +{ + wchar_t wa[PATH_MAX]; + wchar_t wb[PATH_MAX]; + path_to_unicode(NULL, a, wa, ARRAY_SIZE(wa)); + path_to_unicode(NULL, b, wb, ARRAY_SIZE(wb)); + + return MoveFileW(wa, wb) ? 0 : -1; +} + + +struct stat { + int64_t st_size; + time_t st_mtime; +}; + + +FUNCTION_MAY_BE_UNUSED +static int +stat(const char *name, struct stat *st) +{ + wchar_t wbuf[PATH_MAX]; + WIN32_FILE_ATTRIBUTE_DATA attr; + time_t creation_time, write_time; + + path_to_unicode(NULL, name, wbuf, ARRAY_SIZE(wbuf)); + memset(&attr, 0, sizeof(attr)); + + GetFileAttributesExW(wbuf, GetFileExInfoStandard, &attr); + st->st_size = + (((int64_t)attr.nFileSizeHigh) << 32) + (int64_t)attr.nFileSizeLow; + + write_time = SYS2UNIX_TIME(attr.ftLastWriteTime.dwLowDateTime, + attr.ftLastWriteTime.dwHighDateTime); + creation_time = SYS2UNIX_TIME(attr.ftCreationTime.dwLowDateTime, + attr.ftCreationTime.dwHighDateTime); + + if (creation_time > write_time) { + st->st_mtime = creation_time; + } else { + st->st_mtime = write_time; + } + return 0; +} + +#define access(x, a) 1 /* not required anyway */ + +/* WinCE-TODO: define stat, remove, rename, _rmdir, _lseeki64 */ +/* Values from errno.h in Windows SDK (Visual Studio). */ +#define EEXIST 17 +#define EACCES 13 +#define ENOENT 2 + +#if defined(__MINGW32__) +/* Enable unused function warning again */ +#pragma GCC diagnostic pop +#endif + +#endif /* defined(_WIN32_WCE) */ + + +#if defined(__GNUC__) || defined(__MINGW32__) +/* Show no warning in case system functions are not used. */ +#define GCC_VERSION \ + (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) +#if GCC_VERSION >= 40500 +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +#endif /* GCC_VERSION >= 40500 */ +#endif /* defined(__GNUC__) || defined(__MINGW32__) */ +#if defined(__clang__) +/* Show no warning in case system functions are not used. */ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-function" +#endif + +static pthread_mutex_t global_lock_mutex; + + +#if defined(_WIN32) && !defined(__SYMBIAN32__) +/* Forward declaration for Windows */ +FUNCTION_MAY_BE_UNUSED +static int pthread_mutex_lock(pthread_mutex_t *mutex); + +FUNCTION_MAY_BE_UNUSED +static int pthread_mutex_unlock(pthread_mutex_t *mutex); +#endif + + +FUNCTION_MAY_BE_UNUSED +static void +mg_global_lock(void) +{ + (void)pthread_mutex_lock(&global_lock_mutex); +} + + +FUNCTION_MAY_BE_UNUSED +static void +mg_global_unlock(void) +{ + (void)pthread_mutex_unlock(&global_lock_mutex); +} + + +FUNCTION_MAY_BE_UNUSED +static int +mg_atomic_inc(volatile int *addr) +{ + int ret; +#if defined(_WIN32) && !defined(__SYMBIAN32__) && !defined(NO_ATOMICS) + /* Depending on the SDK, this function uses either + * (volatile unsigned int *) or (volatile LONG *), + * so whatever you use, the other SDK is likely to raise a warning. */ + ret = InterlockedIncrement((volatile long *)addr); +#elif defined(__GNUC__) \ + && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 0))) \ + && !defined(NO_ATOMICS) + ret = __sync_add_and_fetch(addr, 1); +#else + mg_global_lock(); + ret = (++(*addr)); + mg_global_unlock(); +#endif + return ret; +} + + +FUNCTION_MAY_BE_UNUSED +static int +mg_atomic_dec(volatile int *addr) +{ + int ret; +#if defined(_WIN32) && !defined(__SYMBIAN32__) && !defined(NO_ATOMICS) + /* Depending on the SDK, this function uses either + * (volatile unsigned int *) or (volatile LONG *), + * so whatever you use, the other SDK is likely to raise a warning. */ + ret = InterlockedDecrement((volatile long *)addr); +#elif defined(__GNUC__) \ + && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 0))) \ + && !defined(NO_ATOMICS) + ret = __sync_sub_and_fetch(addr, 1); +#else + mg_global_lock(); + ret = (--(*addr)); + mg_global_unlock(); +#endif + return ret; +} + + +#if defined(USE_SERVER_STATS) +static int64_t +mg_atomic_add(volatile int64_t *addr, int64_t value) +{ + int64_t ret; +#if defined(_WIN64) && !defined(__SYMBIAN32__) && !defined(NO_ATOMICS) + ret = InterlockedAdd64(addr, value); +#elif defined(__GNUC__) \ + && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 0))) \ + && !defined(NO_ATOMICS) + ret = __sync_add_and_fetch(addr, value); +#else + mg_global_lock(); + *addr += value; + ret = (*addr); + mg_global_unlock(); +#endif + return ret; +} +#endif + + +#if defined(__GNUC__) +/* Show no warning in case system functions are not used. */ +#if GCC_VERSION >= 40500 +#pragma GCC diagnostic pop +#endif /* GCC_VERSION >= 40500 */ +#endif /* defined(__GNUC__) */ +#if defined(__clang__) +/* Show no warning in case system functions are not used. */ +#pragma clang diagnostic pop +#endif + + +#if defined(USE_SERVER_STATS) + +struct mg_memory_stat { + volatile int64_t totalMemUsed; + volatile int64_t maxMemUsed; + volatile int blockCount; +}; + + +static struct mg_memory_stat *get_memory_stat(struct mg_context *ctx); + + +static void * +mg_malloc_ex(size_t size, + struct mg_context *ctx, + const char *file, + unsigned line) +{ + void *data = malloc(size + 2 * sizeof(uintptr_t)); + void *memory = 0; + struct mg_memory_stat *mstat = get_memory_stat(ctx); + +#if defined(MEMORY_DEBUGGING) + char mallocStr[256]; +#else + (void)file; + (void)line; +#endif + + if (data) { + int64_t mmem = mg_atomic_add(&mstat->totalMemUsed, (int64_t)size); + if (mmem > mstat->maxMemUsed) { + /* could use atomic compare exchange, but this + * seems overkill for statistics data */ + mstat->maxMemUsed = mmem; + } + + mg_atomic_inc(&mstat->blockCount); + ((uintptr_t *)data)[0] = size; + ((uintptr_t *)data)[1] = (uintptr_t)mstat; + memory = (void *)(((char *)data) + 2 * sizeof(uintptr_t)); + } + +#if defined(MEMORY_DEBUGGING) + sprintf(mallocStr, + "MEM: %p %5lu alloc %7lu %4lu --- %s:%u\n", + memory, + (unsigned long)size, + (unsigned long)mstat->totalMemUsed, + (unsigned long)mstat->blockCount, + file, + line); +#if defined(_WIN32) + OutputDebugStringA(mallocStr); +#else + DEBUG_TRACE("%s", mallocStr); +#endif +#endif + + return memory; +} + + +static void * +mg_calloc_ex(size_t count, + size_t size, + struct mg_context *ctx, + const char *file, + unsigned line) +{ + void *data = mg_malloc_ex(size * count, ctx, file, line); + + if (data) { + memset(data, 0, size * count); + } + return data; +} + + +static void +mg_free_ex(void *memory, const char *file, unsigned line) +{ + void *data = (void *)(((char *)memory) - 2 * sizeof(uintptr_t)); + + +#if defined(MEMORY_DEBUGGING) + char mallocStr[256]; +#else + (void)file; + (void)line; +#endif + + if (memory) { + uintptr_t size = ((uintptr_t *)data)[0]; + struct mg_memory_stat *mstat = + (struct mg_memory_stat *)(((uintptr_t *)data)[1]); + mg_atomic_add(&mstat->totalMemUsed, -(int64_t)size); + mg_atomic_dec(&mstat->blockCount); +#if defined(MEMORY_DEBUGGING) + sprintf(mallocStr, + "MEM: %p %5lu free %7lu %4lu --- %s:%u\n", + memory, + (unsigned long)size, + (unsigned long)mstat->totalMemUsed, + (unsigned long)mstat->blockCount, + file, + line); +#if defined(_WIN32) + OutputDebugStringA(mallocStr); +#else + DEBUG_TRACE("%s", mallocStr); +#endif +#endif + free(data); + } +} + + +static void * +mg_realloc_ex(void *memory, + size_t newsize, + struct mg_context *ctx, + const char *file, + unsigned line) +{ + void *data; + void *_realloc; + uintptr_t oldsize; + +#if defined(MEMORY_DEBUGGING) + char mallocStr[256]; +#else + (void)file; + (void)line; +#endif + + if (newsize) { + if (memory) { + /* Reallocate existing block */ + struct mg_memory_stat *mstat; + data = (void *)(((char *)memory) - 2 * sizeof(uintptr_t)); + oldsize = ((uintptr_t *)data)[0]; + mstat = (struct mg_memory_stat *)((uintptr_t *)data)[1]; + _realloc = realloc(data, newsize + 2 * sizeof(uintptr_t)); + if (_realloc) { + data = _realloc; + mg_atomic_add(&mstat->totalMemUsed, -(int64_t)oldsize); +#if defined(MEMORY_DEBUGGING) + sprintf(mallocStr, + "MEM: %p %5lu r-free %7lu %4lu --- %s:%u\n", + memory, + (unsigned long)oldsize, + (unsigned long)mstat->totalMemUsed, + (unsigned long)mstat->blockCount, + file, + line); +#if defined(_WIN32) + OutputDebugStringA(mallocStr); +#else + DEBUG_TRACE("%s", mallocStr); +#endif +#endif + mg_atomic_add(&mstat->totalMemUsed, (int64_t)newsize); +#if defined(MEMORY_DEBUGGING) + sprintf(mallocStr, + "MEM: %p %5lu r-alloc %7lu %4lu --- %s:%u\n", + memory, + (unsigned long)newsize, + (unsigned long)mstat->totalMemUsed, + (unsigned long)mstat->blockCount, + file, + line); +#if defined(_WIN32) + OutputDebugStringA(mallocStr); +#else + DEBUG_TRACE("%s", mallocStr); +#endif +#endif + *(uintptr_t *)data = newsize; + data = (void *)(((char *)data) + 2 * sizeof(uintptr_t)); + } else { +#if defined(MEMORY_DEBUGGING) +#if defined(_WIN32) + OutputDebugStringA("MEM: realloc failed\n"); +#else + DEBUG_TRACE("%s", "MEM: realloc failed\n"); +#endif +#endif + return _realloc; + } + } else { + /* Allocate new block */ + data = mg_malloc_ex(newsize, ctx, file, line); + } + } else { + /* Free existing block */ + data = 0; + mg_free_ex(memory, file, line); + } + + return data; +} + +#define mg_malloc(a) mg_malloc_ex(a, NULL, __FILE__, __LINE__) +#define mg_calloc(a, b) mg_calloc_ex(a, b, NULL, __FILE__, __LINE__) +#define mg_realloc(a, b) mg_realloc_ex(a, b, NULL, __FILE__, __LINE__) +#define mg_free(a) mg_free_ex(a, __FILE__, __LINE__) + +#define mg_malloc_ctx(a, c) mg_malloc_ex(a, c, __FILE__, __LINE__) +#define mg_calloc_ctx(a, b, c) mg_calloc_ex(a, b, c, __FILE__, __LINE__) +#define mg_realloc_ctx(a, b, c) mg_realloc_ex(a, b, c, __FILE__, __LINE__) + +#else /* USE_SERVER_STATS */ + +static __inline void * +mg_malloc(size_t a) +{ + return malloc(a); +} + +static __inline void * +mg_calloc(size_t a, size_t b) +{ + return calloc(a, b); +} + +static __inline void * +mg_realloc(void *a, size_t b) +{ + return realloc(a, b); +} + +static __inline void +mg_free(void *a) +{ + free(a); +} + +#define mg_malloc_ctx(a, c) mg_malloc(a) +#define mg_calloc_ctx(a, b, c) mg_calloc(a, b) +#define mg_realloc_ctx(a, b, c) mg_realloc(a, b) +#define mg_free_ctx(a, c) mg_free(a) + +#endif /* USE_SERVER_STATS */ + + +static void mg_vsnprintf(const struct mg_connection *conn, + int *truncated, + char *buf, + size_t buflen, + const char *fmt, + va_list ap); + +static void mg_snprintf(const struct mg_connection *conn, + int *truncated, + char *buf, + size_t buflen, + PRINTF_FORMAT_STRING(const char *fmt), + ...) PRINTF_ARGS(5, 6); + +/* This following lines are just meant as a reminder to use the mg-functions + * for memory management */ +#ifdef malloc +#undef malloc +#endif +#ifdef calloc +#undef calloc +#endif +#ifdef realloc +#undef realloc +#endif +#ifdef free +#undef free +#endif +#ifdef snprintf +#undef snprintf +#endif +#ifdef vsnprintf +#undef vsnprintf +#endif +#define malloc DO_NOT_USE_THIS_FUNCTION__USE_mg_malloc +#define calloc DO_NOT_USE_THIS_FUNCTION__USE_mg_calloc +#define realloc DO_NOT_USE_THIS_FUNCTION__USE_mg_realloc +#define free DO_NOT_USE_THIS_FUNCTION__USE_mg_free +#define snprintf DO_NOT_USE_THIS_FUNCTION__USE_mg_snprintf +#ifdef _WIN32 /* vsnprintf must not be used in any system, * \ \ \ \ + * but this define only works well for Windows. */ +#define vsnprintf DO_NOT_USE_THIS_FUNCTION__USE_mg_vsnprintf +#endif + + +/* mg_init_library counter */ +static int mg_init_library_called = 0; + +#if !defined(NO_SSL) +static int mg_ssl_initialized = 0; +#endif + +static pthread_key_t sTlsKey; /* Thread local storage index */ +static int thread_idx_max = 0; + + +struct mg_workerTLS { + int is_master; + unsigned long thread_idx; +#if defined(_WIN32) && !defined(__SYMBIAN32__) + HANDLE pthread_cond_helper_mutex; + struct mg_workerTLS *next_waiting_thread; +#endif +}; + + +#if defined(__GNUC__) || defined(__MINGW32__) +/* Show no warning in case system functions are not used. */ +#if GCC_VERSION >= 40500 +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +#endif /* GCC_VERSION >= 40500 */ +#endif /* defined(__GNUC__) || defined(__MINGW32__) */ +#if defined(__clang__) +/* Show no warning in case system functions are not used. */ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-function" +#endif + + +/* Get a unique thread ID as unsigned long, independent from the data type + * of thread IDs defined by the operating system API. + * If two calls to mg_current_thread_id return the same value, they calls + * are done from the same thread. If they return different values, they are + * done from different threads. (Provided this function is used in the same + * process context and threads are not repeatedly created and deleted, but + * CivetWeb does not do that). + * This function must match the signature required for SSL id callbacks: + * CRYPTO_set_id_callback + */ +FUNCTION_MAY_BE_UNUSED +static unsigned long +mg_current_thread_id(void) +{ +#ifdef _WIN32 + return GetCurrentThreadId(); +#else + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunreachable-code" +/* For every compiler, either "sizeof(pthread_t) > sizeof(unsigned long)" + * or not, so one of the two conditions will be unreachable by construction. + * Unfortunately the C standard does not define a way to check this at + * compile time, since the #if preprocessor conditions can not use the sizeof + * operator as an argument. */ +#endif + + if (sizeof(pthread_t) > sizeof(unsigned long)) { + /* This is the problematic case for CRYPTO_set_id_callback: + * The OS pthread_t can not be cast to unsigned long. */ + struct mg_workerTLS *tls = + (struct mg_workerTLS *)pthread_getspecific(sTlsKey); + if (tls == NULL) { + /* SSL called from an unknown thread: Create some thread index. + */ + tls = (struct mg_workerTLS *)mg_malloc(sizeof(struct mg_workerTLS)); + tls->is_master = -2; /* -2 means "3rd party thread" */ + tls->thread_idx = (unsigned)mg_atomic_inc(&thread_idx_max); + pthread_setspecific(sTlsKey, tls); + } + return tls->thread_idx; + } else { + /* pthread_t may be any data type, so a simple cast to unsigned long + * can rise a warning/error, depending on the platform. + * Here memcpy is used as an anything-to-anything cast. */ + unsigned long ret = 0; + pthread_t t = pthread_self(); + memcpy(&ret, &t, sizeof(pthread_t)); + return ret; + } + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#endif +} + + +FUNCTION_MAY_BE_UNUSED +static uint64_t +mg_get_current_time_ns(void) +{ + struct timespec tsnow; + clock_gettime(CLOCK_REALTIME, &tsnow); + return (((uint64_t)tsnow.tv_sec) * 1000000000) + (uint64_t)tsnow.tv_nsec; +} + + +#if defined(__GNUC__) +/* Show no warning in case system functions are not used. */ +#if GCC_VERSION >= 40500 +#pragma GCC diagnostic pop +#endif /* GCC_VERSION >= 40500 */ +#endif /* defined(__GNUC__) */ +#if defined(__clang__) +/* Show no warning in case system functions are not used. */ +#pragma clang diagnostic pop +#endif + + +#if !defined(DEBUG_TRACE) +#if defined(DEBUG) +static void DEBUG_TRACE_FUNC(const char *func, + unsigned line, + PRINTF_FORMAT_STRING(const char *fmt), + ...) PRINTF_ARGS(3, 4); + +static void +DEBUG_TRACE_FUNC(const char *func, unsigned line, const char *fmt, ...) +{ + va_list args; + uint64_t nsnow; + static uint64_t nslast; + struct timespec tsnow; + + /* Get some operating system independent thread id */ + unsigned long thread_id = mg_current_thread_id(); + + clock_gettime(CLOCK_REALTIME, &tsnow); + nsnow = ((uint64_t)tsnow.tv_sec) * ((uint64_t)1000000000) + + ((uint64_t)tsnow.tv_nsec); + + if (!nslast) { + nslast = nsnow; + } + + flockfile(stdout); + printf("*** %lu.%09lu %12" INT64_FMT " %lu %s:%u: ", + (unsigned long)tsnow.tv_sec, + (unsigned long)tsnow.tv_nsec, + nsnow - nslast, + thread_id, + func, + line); + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); + putchar('\n'); + fflush(stdout); + funlockfile(stdout); + nslast = nsnow; +} + +#define DEBUG_TRACE(fmt, ...) \ + DEBUG_TRACE_FUNC(__func__, __LINE__, fmt, __VA_ARGS__) + +#else +#define DEBUG_TRACE(fmt, ...) \ + do { \ + } while (0) +#endif /* DEBUG */ +#endif /* DEBUG_TRACE */ + + +#define MD5_STATIC static +#include "md5.inl" + +/* Darwin prior to 7.0 and Win32 do not have socklen_t */ +#ifdef NO_SOCKLEN_T +typedef int socklen_t; +#endif /* NO_SOCKLEN_T */ +#define _DARWIN_UNLIMITED_SELECT + +#define IP_ADDR_STR_LEN (50) /* IPv6 hex string is 46 chars */ + +#if !defined(MSG_NOSIGNAL) +#define MSG_NOSIGNAL (0) +#endif + +#if !defined(SOMAXCONN) +#define SOMAXCONN (100) +#endif + +/* Size of the accepted socket queue */ +#if !defined(MGSQLEN) +#define MGSQLEN (20) +#endif + + +#if defined(NO_SSL) +typedef struct SSL SSL; /* dummy for SSL argument to push/pull */ +typedef struct SSL_CTX SSL_CTX; +#else +#if defined(NO_SSL_DL) +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#else + +/* SSL loaded dynamically from DLL. + * I put the prototypes here to be independent from OpenSSL source + * installation. */ + +typedef struct ssl_st SSL; +typedef struct ssl_method_st SSL_METHOD; +typedef struct ssl_ctx_st SSL_CTX; +typedef struct x509_store_ctx_st X509_STORE_CTX; +typedef struct x509_name X509_NAME; +typedef struct asn1_integer ASN1_INTEGER; +typedef struct bignum BIGNUM; +typedef struct ossl_init_settings_st OPENSSL_INIT_SETTINGS; +typedef struct evp_md EVP_MD; +typedef struct x509 X509; + + +#define SSL_CTRL_OPTIONS (32) +#define SSL_CTRL_CLEAR_OPTIONS (77) +#define SSL_CTRL_SET_ECDH_AUTO (94) + +#define OPENSSL_INIT_NO_LOAD_SSL_STRINGS 0x00100000L +#define OPENSSL_INIT_LOAD_SSL_STRINGS 0x00200000L +#define OPENSSL_INIT_LOAD_CRYPTO_STRINGS 0x00000002L + +#define SSL_VERIFY_NONE (0) +#define SSL_VERIFY_PEER (1) +#define SSL_VERIFY_FAIL_IF_NO_PEER_CERT (2) +#define SSL_VERIFY_CLIENT_ONCE (4) +#define SSL_OP_ALL ((long)(0x80000BFFUL)) +#define SSL_OP_NO_SSLv2 (0x01000000L) +#define SSL_OP_NO_SSLv3 (0x02000000L) +#define SSL_OP_NO_TLSv1 (0x04000000L) +#define SSL_OP_NO_TLSv1_2 (0x08000000L) +#define SSL_OP_NO_TLSv1_1 (0x10000000L) +#define SSL_OP_SINGLE_DH_USE (0x00100000L) +#define SSL_OP_CIPHER_SERVER_PREFERENCE (0x00400000L) +#define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (0x00010000L) +#define SSL_OP_NO_COMPRESSION (0x00020000L) + +#define SSL_CB_HANDSHAKE_START (0x10) +#define SSL_CB_HANDSHAKE_DONE (0x20) + +#define SSL_ERROR_NONE (0) +#define SSL_ERROR_SSL (1) +#define SSL_ERROR_WANT_READ (2) +#define SSL_ERROR_WANT_WRITE (3) +#define SSL_ERROR_WANT_X509_LOOKUP (4) +#define SSL_ERROR_SYSCALL (5) /* see errno */ +#define SSL_ERROR_ZERO_RETURN (6) +#define SSL_ERROR_WANT_CONNECT (7) +#define SSL_ERROR_WANT_ACCEPT (8) + + +struct ssl_func { + const char *name; /* SSL function name */ + void (*ptr)(void); /* Function pointer */ +}; + + +#ifdef OPENSSL_API_1_1 + +#define SSL_free (*(void (*)(SSL *))ssl_sw[0].ptr) +#define SSL_accept (*(int (*)(SSL *))ssl_sw[1].ptr) +#define SSL_connect (*(int (*)(SSL *))ssl_sw[2].ptr) +#define SSL_read (*(int (*)(SSL *, void *, int))ssl_sw[3].ptr) +#define SSL_write (*(int (*)(SSL *, const void *, int))ssl_sw[4].ptr) +#define SSL_get_error (*(int (*)(SSL *, int))ssl_sw[5].ptr) +#define SSL_set_fd (*(int (*)(SSL *, SOCKET))ssl_sw[6].ptr) +#define SSL_new (*(SSL * (*)(SSL_CTX *))ssl_sw[7].ptr) +#define SSL_CTX_new (*(SSL_CTX * (*)(SSL_METHOD *))ssl_sw[8].ptr) +#define TLS_server_method (*(SSL_METHOD * (*)(void))ssl_sw[9].ptr) +#define OPENSSL_init_ssl \ + (*(int (*)(uint64_t opts, \ + const OPENSSL_INIT_SETTINGS *settings))ssl_sw[10].ptr) +#define SSL_CTX_use_PrivateKey_file \ + (*(int (*)(SSL_CTX *, const char *, int))ssl_sw[11].ptr) +#define SSL_CTX_use_certificate_file \ + (*(int (*)(SSL_CTX *, const char *, int))ssl_sw[12].ptr) +#define SSL_CTX_set_default_passwd_cb \ + (*(void (*)(SSL_CTX *, mg_callback_t))ssl_sw[13].ptr) +#define SSL_CTX_free (*(void (*)(SSL_CTX *))ssl_sw[14].ptr) +#define SSL_CTX_use_certificate_chain_file \ + (*(int (*)(SSL_CTX *, const char *))ssl_sw[15].ptr) +#define TLS_client_method (*(SSL_METHOD * (*)(void))ssl_sw[16].ptr) +#define SSL_pending (*(int (*)(SSL *))ssl_sw[17].ptr) +#define SSL_CTX_set_verify \ + (*(void (*)(SSL_CTX *, \ + int, \ + int (*verify_callback)(int, X509_STORE_CTX *)))ssl_sw[18].ptr) +#define SSL_shutdown (*(int (*)(SSL *))ssl_sw[19].ptr) +#define SSL_CTX_load_verify_locations \ + (*(int (*)(SSL_CTX *, const char *, const char *))ssl_sw[20].ptr) +#define SSL_CTX_set_default_verify_paths (*(int (*)(SSL_CTX *))ssl_sw[21].ptr) +#define SSL_CTX_set_verify_depth (*(void (*)(SSL_CTX *, int))ssl_sw[22].ptr) +#define SSL_get_peer_certificate (*(X509 * (*)(SSL *))ssl_sw[23].ptr) +#define SSL_get_version (*(const char *(*)(SSL *))ssl_sw[24].ptr) +#define SSL_get_current_cipher (*(SSL_CIPHER * (*)(SSL *))ssl_sw[25].ptr) +#define SSL_CIPHER_get_name \ + (*(const char *(*)(const SSL_CIPHER *))ssl_sw[26].ptr) +#define SSL_CTX_check_private_key (*(int (*)(SSL_CTX *))ssl_sw[27].ptr) +#define SSL_CTX_set_session_id_context \ + (*(int (*)(SSL_CTX *, const unsigned char *, unsigned int))ssl_sw[28].ptr) +#define SSL_CTX_ctrl (*(long (*)(SSL_CTX *, int, long, void *))ssl_sw[29].ptr) +#define SSL_CTX_set_cipher_list \ + (*(int (*)(SSL_CTX *, const char *))ssl_sw[30].ptr) +#define SSL_CTX_set_options \ + (*(unsigned long (*)(SSL_CTX *, unsigned long))ssl_sw[31].ptr) +#define SSL_CTX_set_info_callback \ + (*(void (*)(SSL_CTX * ctx, \ + void (*callback)(SSL * s, int, int)))ssl_sw[32].ptr) +#define SSL_get_ex_data (*(char *(*)(SSL *, int))ssl_sw[33].ptr) +#define SSL_set_ex_data (*(void (*)(SSL *, int, char *))ssl_sw[34].ptr) + +#define SSL_CTX_clear_options(ctx, op) \ + SSL_CTX_ctrl((ctx), SSL_CTRL_CLEAR_OPTIONS, (op), NULL) +#define SSL_CTX_set_ecdh_auto(ctx, onoff) \ + SSL_CTX_ctrl(ctx, SSL_CTRL_SET_ECDH_AUTO, onoff, NULL) + +#define X509_get_notBefore(x) ((x)->cert_info->validity->notBefore) +#define X509_get_notAfter(x) ((x)->cert_info->validity->notAfter) + +#define SSL_set_app_data(s, arg) (SSL_set_ex_data(s, 0, (char *)arg)) +#define SSL_get_app_data(s) (SSL_get_ex_data(s, 0)) + +#define ERR_get_error (*(unsigned long (*)(void))crypto_sw[0].ptr) +#define ERR_error_string (*(char *(*)(unsigned long, char *))crypto_sw[1].ptr) +#define ERR_remove_state (*(void (*)(unsigned long))crypto_sw[2].ptr) +#define CONF_modules_unload (*(void (*)(int))crypto_sw[3].ptr) +#define X509_free (*(void (*)(X509 *))crypto_sw[4].ptr) +#define X509_get_subject_name (*(X509_NAME * (*)(X509 *))crypto_sw[5].ptr) +#define X509_get_issuer_name (*(X509_NAME * (*)(X509 *))crypto_sw[6].ptr) +#define X509_NAME_oneline \ + (*(char *(*)(X509_NAME *, char *, int))crypto_sw[7].ptr) +#define X509_get_serialNumber (*(ASN1_INTEGER * (*)(X509 *))crypto_sw[8].ptr) +#define EVP_get_digestbyname \ + (*(const EVP_MD *(*)(const char *))crypto_sw[9].ptr) +#define EVP_Digest \ + (*(int (*)( \ + const void *, size_t, void *, unsigned int *, const EVP_MD *, void *)) \ + crypto_sw[10].ptr) +#define i2d_X509 (*(int (*)(X509 *, unsigned char **))crypto_sw[11].ptr) +#define BN_bn2hex (*(char *(*)(const BIGNUM *a))crypto_sw[12].ptr) +#define ASN1_INTEGER_to_BN \ + (*(BIGNUM * (*)(const ASN1_INTEGER *ai, BIGNUM *bn))crypto_sw[13].ptr) +#define BN_free (*(void (*)(const BIGNUM *a))crypto_sw[14].ptr) +#define CRYPTO_free (*(void (*)(void *addr))crypto_sw[15].ptr) + +#define OPENSSL_free(a) CRYPTO_free(a) + + +/* set_ssl_option() function updates this array. + * It loads SSL library dynamically and changes NULLs to the actual addresses + * of respective functions. The macros above (like SSL_connect()) are really + * just calling these functions indirectly via the pointer. */ +static struct ssl_func ssl_sw[] = {{"SSL_free", NULL}, + {"SSL_accept", NULL}, + {"SSL_connect", NULL}, + {"SSL_read", NULL}, + {"SSL_write", NULL}, + {"SSL_get_error", NULL}, + {"SSL_set_fd", NULL}, + {"SSL_new", NULL}, + {"SSL_CTX_new", NULL}, + {"TLS_server_method", NULL}, + {"OPENSSL_init_ssl", NULL}, + {"SSL_CTX_use_PrivateKey_file", NULL}, + {"SSL_CTX_use_certificate_file", NULL}, + {"SSL_CTX_set_default_passwd_cb", NULL}, + {"SSL_CTX_free", NULL}, + {"SSL_CTX_use_certificate_chain_file", NULL}, + {"TLS_client_method", NULL}, + {"SSL_pending", NULL}, + {"SSL_CTX_set_verify", NULL}, + {"SSL_shutdown", NULL}, + {"SSL_CTX_load_verify_locations", NULL}, + {"SSL_CTX_set_default_verify_paths", NULL}, + {"SSL_CTX_set_verify_depth", NULL}, + {"SSL_get_peer_certificate", NULL}, + {"SSL_get_version", NULL}, + {"SSL_get_current_cipher", NULL}, + {"SSL_CIPHER_get_name", NULL}, + {"SSL_CTX_check_private_key", NULL}, + {"SSL_CTX_set_session_id_context", NULL}, + {"SSL_CTX_ctrl", NULL}, + {"SSL_CTX_set_cipher_list", NULL}, + {"SSL_CTX_set_options", NULL}, + {"SSL_CTX_set_info_callback", NULL}, + {"SSL_get_ex_data", NULL}, + {"SSL_set_ex_data", NULL}, + {NULL, NULL}}; + + +/* Similar array as ssl_sw. These functions could be located in different + * lib. */ +static struct ssl_func crypto_sw[] = {{"ERR_get_error", NULL}, + {"ERR_error_string", NULL}, + {"ERR_remove_state", NULL}, + {"CONF_modules_unload", NULL}, + {"X509_free", NULL}, + {"X509_get_subject_name", NULL}, + {"X509_get_issuer_name", NULL}, + {"X509_NAME_oneline", NULL}, + {"X509_get_serialNumber", NULL}, + {"EVP_get_digestbyname", NULL}, + {"EVP_Digest", NULL}, + {"i2d_X509", NULL}, + {"BN_bn2hex", NULL}, + {"ASN1_INTEGER_to_BN", NULL}, + {"BN_free", NULL}, + {"CRYPTO_free", NULL}, + {NULL, NULL}}; +#else + +#define SSL_free (*(void (*)(SSL *))ssl_sw[0].ptr) +#define SSL_accept (*(int (*)(SSL *))ssl_sw[1].ptr) +#define SSL_connect (*(int (*)(SSL *))ssl_sw[2].ptr) +#define SSL_read (*(int (*)(SSL *, void *, int))ssl_sw[3].ptr) +#define SSL_write (*(int (*)(SSL *, const void *, int))ssl_sw[4].ptr) +#define SSL_get_error (*(int (*)(SSL *, int))ssl_sw[5].ptr) +#define SSL_set_fd (*(int (*)(SSL *, SOCKET))ssl_sw[6].ptr) +#define SSL_new (*(SSL * (*)(SSL_CTX *))ssl_sw[7].ptr) +#define SSL_CTX_new (*(SSL_CTX * (*)(SSL_METHOD *))ssl_sw[8].ptr) +#define SSLv23_server_method (*(SSL_METHOD * (*)(void))ssl_sw[9].ptr) +#define SSL_library_init (*(int (*)(void))ssl_sw[10].ptr) +#define SSL_CTX_use_PrivateKey_file \ + (*(int (*)(SSL_CTX *, const char *, int))ssl_sw[11].ptr) +#define SSL_CTX_use_certificate_file \ + (*(int (*)(SSL_CTX *, const char *, int))ssl_sw[12].ptr) +#define SSL_CTX_set_default_passwd_cb \ + (*(void (*)(SSL_CTX *, mg_callback_t))ssl_sw[13].ptr) +#define SSL_CTX_free (*(void (*)(SSL_CTX *))ssl_sw[14].ptr) +#define SSL_load_error_strings (*(void (*)(void))ssl_sw[15].ptr) +#define SSL_CTX_use_certificate_chain_file \ + (*(int (*)(SSL_CTX *, const char *))ssl_sw[16].ptr) +#define SSLv23_client_method (*(SSL_METHOD * (*)(void))ssl_sw[17].ptr) +#define SSL_pending (*(int (*)(SSL *))ssl_sw[18].ptr) +#define SSL_CTX_set_verify \ + (*(void (*)(SSL_CTX *, \ + int, \ + int (*verify_callback)(int, X509_STORE_CTX *)))ssl_sw[19].ptr) +#define SSL_shutdown (*(int (*)(SSL *))ssl_sw[20].ptr) +#define SSL_CTX_load_verify_locations \ + (*(int (*)(SSL_CTX *, const char *, const char *))ssl_sw[21].ptr) +#define SSL_CTX_set_default_verify_paths (*(int (*)(SSL_CTX *))ssl_sw[22].ptr) +#define SSL_CTX_set_verify_depth (*(void (*)(SSL_CTX *, int))ssl_sw[23].ptr) +#define SSL_get_peer_certificate (*(X509 * (*)(SSL *))ssl_sw[24].ptr) +#define SSL_get_version (*(const char *(*)(SSL *))ssl_sw[25].ptr) +#define SSL_get_current_cipher (*(SSL_CIPHER * (*)(SSL *))ssl_sw[26].ptr) +#define SSL_CIPHER_get_name \ + (*(const char *(*)(const SSL_CIPHER *))ssl_sw[27].ptr) +#define SSL_CTX_check_private_key (*(int (*)(SSL_CTX *))ssl_sw[28].ptr) +#define SSL_CTX_set_session_id_context \ + (*(int (*)(SSL_CTX *, const unsigned char *, unsigned int))ssl_sw[29].ptr) +#define SSL_CTX_ctrl (*(long (*)(SSL_CTX *, int, long, void *))ssl_sw[30].ptr) +#define SSL_CTX_set_cipher_list \ + (*(int (*)(SSL_CTX *, const char *))ssl_sw[31].ptr) +#define SSL_CTX_set_info_callback \ + (*(void (*)(SSL_CTX * ctx, \ + void (*callback)(SSL * s, int, int)))ssl_sw[32].ptr) +#define SSL_get_ex_data (*(char *(*)(SSL *, int))ssl_sw[33].ptr) +#define SSL_set_ex_data (*(void (*)(SSL *, int, char *))ssl_sw[34].ptr) + +#define SSL_CTX_set_options(ctx, op) \ + SSL_CTX_ctrl((ctx), SSL_CTRL_OPTIONS, (op), NULL) +#define SSL_CTX_clear_options(ctx, op) \ + SSL_CTX_ctrl((ctx), SSL_CTRL_CLEAR_OPTIONS, (op), NULL) +#define SSL_CTX_set_ecdh_auto(ctx, onoff) \ + SSL_CTX_ctrl(ctx, SSL_CTRL_SET_ECDH_AUTO, onoff, NULL) + + +#define X509_get_notBefore(x) ((x)->cert_info->validity->notBefore) +#define X509_get_notAfter(x) ((x)->cert_info->validity->notAfter) + +#define SSL_set_app_data(s, arg) (SSL_set_ex_data(s, 0, (char *)arg)) +#define SSL_get_app_data(s) (SSL_get_ex_data(s, 0)) + +#define CRYPTO_num_locks (*(int (*)(void))crypto_sw[0].ptr) +#define CRYPTO_set_locking_callback \ + (*(void (*)(void (*)(int, int, const char *, int)))crypto_sw[1].ptr) +#define CRYPTO_set_id_callback \ + (*(void (*)(unsigned long (*)(void)))crypto_sw[2].ptr) +#define ERR_get_error (*(unsigned long (*)(void))crypto_sw[3].ptr) +#define ERR_error_string (*(char *(*)(unsigned long, char *))crypto_sw[4].ptr) +#define ERR_remove_state (*(void (*)(unsigned long))crypto_sw[5].ptr) +#define ERR_free_strings (*(void (*)(void))crypto_sw[6].ptr) +#define ENGINE_cleanup (*(void (*)(void))crypto_sw[7].ptr) +#define CONF_modules_unload (*(void (*)(int))crypto_sw[8].ptr) +#define CRYPTO_cleanup_all_ex_data (*(void (*)(void))crypto_sw[9].ptr) +#define EVP_cleanup (*(void (*)(void))crypto_sw[10].ptr) +#define X509_free (*(void (*)(X509 *))crypto_sw[11].ptr) +#define X509_get_subject_name (*(X509_NAME * (*)(X509 *))crypto_sw[12].ptr) +#define X509_get_issuer_name (*(X509_NAME * (*)(X509 *))crypto_sw[13].ptr) +#define X509_NAME_oneline \ + (*(char *(*)(X509_NAME *, char *, int))crypto_sw[14].ptr) +#define X509_get_serialNumber (*(ASN1_INTEGER * (*)(X509 *))crypto_sw[15].ptr) +#define i2c_ASN1_INTEGER \ + (*(int (*)(ASN1_INTEGER *, unsigned char **))crypto_sw[16].ptr) +#define EVP_get_digestbyname \ + (*(const EVP_MD *(*)(const char *))crypto_sw[17].ptr) +#define EVP_Digest \ + (*(int (*)( \ + const void *, size_t, void *, unsigned int *, const EVP_MD *, void *)) \ + crypto_sw[18].ptr) +#define i2d_X509 (*(int (*)(X509 *, unsigned char **))crypto_sw[19].ptr) +#define BN_bn2hex (*(char *(*)(const BIGNUM *a))crypto_sw[20].ptr) +#define ASN1_INTEGER_to_BN \ + (*(BIGNUM * (*)(const ASN1_INTEGER *ai, BIGNUM *bn))crypto_sw[21].ptr) +#define BN_free (*(void (*)(const BIGNUM *a))crypto_sw[22].ptr) +#define CRYPTO_free (*(void (*)(void *addr))crypto_sw[23].ptr) + +#define OPENSSL_free(a) CRYPTO_free(a) + +/* set_ssl_option() function updates this array. + * It loads SSL library dynamically and changes NULLs to the actual addresses + * of respective functions. The macros above (like SSL_connect()) are really + * just calling these functions indirectly via the pointer. */ +static struct ssl_func ssl_sw[] = {{"SSL_free", NULL}, + {"SSL_accept", NULL}, + {"SSL_connect", NULL}, + {"SSL_read", NULL}, + {"SSL_write", NULL}, + {"SSL_get_error", NULL}, + {"SSL_set_fd", NULL}, + {"SSL_new", NULL}, + {"SSL_CTX_new", NULL}, + {"SSLv23_server_method", NULL}, + {"SSL_library_init", NULL}, + {"SSL_CTX_use_PrivateKey_file", NULL}, + {"SSL_CTX_use_certificate_file", NULL}, + {"SSL_CTX_set_default_passwd_cb", NULL}, + {"SSL_CTX_free", NULL}, + {"SSL_load_error_strings", NULL}, + {"SSL_CTX_use_certificate_chain_file", NULL}, + {"SSLv23_client_method", NULL}, + {"SSL_pending", NULL}, + {"SSL_CTX_set_verify", NULL}, + {"SSL_shutdown", NULL}, + {"SSL_CTX_load_verify_locations", NULL}, + {"SSL_CTX_set_default_verify_paths", NULL}, + {"SSL_CTX_set_verify_depth", NULL}, + {"SSL_get_peer_certificate", NULL}, + {"SSL_get_version", NULL}, + {"SSL_get_current_cipher", NULL}, + {"SSL_CIPHER_get_name", NULL}, + {"SSL_CTX_check_private_key", NULL}, + {"SSL_CTX_set_session_id_context", NULL}, + {"SSL_CTX_ctrl", NULL}, + {"SSL_CTX_set_cipher_list", NULL}, + {"SSL_CTX_set_info_callback", NULL}, + {"SSL_get_ex_data", NULL}, + {"SSL_set_ex_data", NULL}, + {NULL, NULL}}; + + +/* Similar array as ssl_sw. These functions could be located in different + * lib. */ +static struct ssl_func crypto_sw[] = {{"CRYPTO_num_locks", NULL}, + {"CRYPTO_set_locking_callback", NULL}, + {"CRYPTO_set_id_callback", NULL}, + {"ERR_get_error", NULL}, + {"ERR_error_string", NULL}, + {"ERR_remove_state", NULL}, + {"ERR_free_strings", NULL}, + {"ENGINE_cleanup", NULL}, + {"CONF_modules_unload", NULL}, + {"CRYPTO_cleanup_all_ex_data", NULL}, + {"EVP_cleanup", NULL}, + {"X509_free", NULL}, + {"X509_get_subject_name", NULL}, + {"X509_get_issuer_name", NULL}, + {"X509_NAME_oneline", NULL}, + {"X509_get_serialNumber", NULL}, + {"i2c_ASN1_INTEGER", NULL}, + {"EVP_get_digestbyname", NULL}, + {"EVP_Digest", NULL}, + {"i2d_X509", NULL}, + {"BN_bn2hex", NULL}, + {"ASN1_INTEGER_to_BN", NULL}, + {"BN_free", NULL}, + {"CRYPTO_free", NULL}, + {NULL, NULL}}; +#endif /* OPENSSL_API_1_1 */ +#endif /* NO_SSL_DL */ +#endif /* NO_SSL */ + + +#if !defined(NO_CACHING) +static const char *month_names[] = {"Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec"}; +#endif /* !NO_CACHING */ + +/* Unified socket address. For IPv6 support, add IPv6 address structure in + * the + * union u. */ +union usa { + struct sockaddr sa; + struct sockaddr_in sin; +#if defined(USE_IPV6) + struct sockaddr_in6 sin6; +#endif +}; + +/* Describes a string (chunk of memory). */ +struct vec { + const char *ptr; + size_t len; +}; + +struct mg_file_stat { + /* File properties filled by mg_stat: */ + uint64_t size; + time_t last_modified; + int is_directory; /* Set to 1 if mg_stat is called for a directory */ + int is_gzipped; /* Set to 1 if the content is gzipped, in which + * case we need a "Content-Eencoding: gzip" header */ + int location; /* 0 = nowhere, 1 = on disk, 2 = in memory */ +}; + +struct mg_file_in_memory { + char *p; + uint32_t pos; + char mode; +}; + +struct mg_file_access { + /* File properties filled by mg_fopen: */ + FILE *fp; + /* TODO (low): Replace "membuf" implementation by a "file in memory" + * support library. Use some struct mg_file_in_memory *mf; instead of + * membuf char pointer. */ + const char *membuf; +}; + +struct mg_file { + struct mg_file_stat stat; + struct mg_file_access access; +}; + +#define STRUCT_FILE_INITIALIZER \ + { \ + { \ + (uint64_t)0, (time_t)0, 0, 0, 0 \ + } \ + , \ + { \ + (FILE *) NULL, (const char *)NULL \ + } \ + } + +/* Describes listening socket, or socket which was accept()-ed by the master + * thread and queued for future handling by the worker thread. */ +struct socket { + SOCKET sock; /* Listening socket */ + union usa lsa; /* Local socket address */ + union usa rsa; /* Remote socket address */ + unsigned char is_ssl; /* Is port SSL-ed */ + unsigned char ssl_redir; /* Is port supposed to redirect everything to SSL + * port */ + unsigned char in_use; /* Is valid */ +}; + +/* NOTE(lsm): this enum shoulds be in sync with the config_options below. */ +enum { + CGI_EXTENSIONS, + CGI_ENVIRONMENT, + PUT_DELETE_PASSWORDS_FILE, + CGI_INTERPRETER, + PROTECT_URI, + AUTHENTICATION_DOMAIN, + ENABLE_AUTH_DOMAIN_CHECK, + SSI_EXTENSIONS, + THROTTLE, + ACCESS_LOG_FILE, + ENABLE_DIRECTORY_LISTING, + ERROR_LOG_FILE, + GLOBAL_PASSWORDS_FILE, + INDEX_FILES, + ENABLE_KEEP_ALIVE, + ACCESS_CONTROL_LIST, + EXTRA_MIME_TYPES, + LISTENING_PORTS, + DOCUMENT_ROOT, + SSL_CERTIFICATE, + SSL_CERTIFICATE_CHAIN, + NUM_THREADS, + RUN_AS_USER, + URL_REWRITE_PATTERN, + HIDE_FILES, + REQUEST_TIMEOUT, + KEEP_ALIVE_TIMEOUT, + LINGER_TIMEOUT, + SSL_DO_VERIFY_PEER, + SSL_CA_PATH, + SSL_CA_FILE, + SSL_VERIFY_DEPTH, + SSL_DEFAULT_VERIFY_PATHS, + SSL_CIPHER_LIST, + SSL_PROTOCOL_VERSION, + SSL_SHORT_TRUST, + +#if defined(USE_WEBSOCKET) + WEBSOCKET_TIMEOUT, +#endif + + DECODE_URL, + +#if defined(USE_LUA) + LUA_PRELOAD_FILE, + LUA_SCRIPT_EXTENSIONS, + LUA_SERVER_PAGE_EXTENSIONS, +#endif +#if defined(USE_DUKTAPE) + DUKTAPE_SCRIPT_EXTENSIONS, +#endif + +#if defined(USE_WEBSOCKET) + WEBSOCKET_ROOT, +#endif +#if defined(USE_LUA) && defined(USE_WEBSOCKET) + LUA_WEBSOCKET_EXTENSIONS, +#endif + + ACCESS_CONTROL_ALLOW_ORIGIN, + ACCESS_CONTROL_ALLOW_METHODS, + ACCESS_CONTROL_ALLOW_HEADERS, + ERROR_PAGES, + CONFIG_TCP_NODELAY, /* Prepended CONFIG_ to avoid conflict with the + * socket option typedef TCP_NODELAY. */ +#if !defined(NO_CACHING) + STATIC_FILE_MAX_AGE, +#endif +#if !defined(NO_SSL) + STRICT_HTTPS_MAX_AGE, +#endif +#if defined(__linux__) + ALLOW_SENDFILE_CALL, +#endif +#if defined(_WIN32) + CASE_SENSITIVE_FILES, +#endif +#if defined(USE_LUA) + LUA_BACKGROUND_SCRIPT, + LUA_BACKGROUND_SCRIPT_PARAMS, +#endif + ADDITIONAL_HEADER, + MAX_REQUEST_SIZE, + ALLOW_INDEX_SCRIPT_SUB_RES, + + VALIDATE_HTTP_METHOD, + CANONICALIZE_URL_PATH, + ALLOW_UNICODE_IN_URLS, + NUM_OPTIONS +}; + + +/* Config option name, config types, default value */ +static struct mg_option config_options[] = { + {"cgi_pattern", CONFIG_TYPE_EXT_PATTERN, "**.cgi$|**.pl$|**.php$"}, + {"cgi_environment", CONFIG_TYPE_STRING_LIST, NULL}, + {"put_delete_auth_file", CONFIG_TYPE_FILE, NULL}, + {"cgi_interpreter", CONFIG_TYPE_FILE, NULL}, + {"protect_uri", CONFIG_TYPE_STRING_LIST, NULL}, + {"authentication_domain", CONFIG_TYPE_STRING, "mydomain.com"}, + {"enable_auth_domain_check", CONFIG_TYPE_BOOLEAN, "yes"}, + {"ssi_pattern", CONFIG_TYPE_EXT_PATTERN, "**.shtml$|**.shtm$"}, + {"throttle", CONFIG_TYPE_STRING_LIST, NULL}, + {"access_log_file", CONFIG_TYPE_FILE, NULL}, + {"enable_directory_listing", CONFIG_TYPE_BOOLEAN, "yes"}, + {"error_log_file", CONFIG_TYPE_FILE, NULL}, + {"global_auth_file", CONFIG_TYPE_FILE, NULL}, + {"index_files", + CONFIG_TYPE_STRING_LIST, +#ifdef USE_LUA + "index.xhtml,index.html,index.htm," + "index.lp,index.lsp,index.lua,index.cgi," + "index.shtml,index.php"}, +#else + "index.xhtml,index.html,index.htm,index.cgi,index.shtml,index.php"}, +#endif + {"enable_keep_alive", CONFIG_TYPE_BOOLEAN, "no"}, + {"access_control_list", CONFIG_TYPE_STRING_LIST, NULL}, + {"extra_mime_types", CONFIG_TYPE_STRING_LIST, NULL}, + {"listening_ports", CONFIG_TYPE_STRING_LIST, "8080"}, + {"document_root", CONFIG_TYPE_DIRECTORY, NULL}, + {"ssl_certificate", CONFIG_TYPE_FILE, NULL}, + {"ssl_certificate_chain", CONFIG_TYPE_FILE, NULL}, + {"num_threads", CONFIG_TYPE_NUMBER, "50"}, + {"run_as_user", CONFIG_TYPE_STRING, NULL}, + {"url_rewrite_patterns", CONFIG_TYPE_STRING_LIST, NULL}, + {"hide_files_patterns", CONFIG_TYPE_EXT_PATTERN, NULL}, + {"request_timeout_ms", CONFIG_TYPE_NUMBER, "30000"}, + {"keep_alive_timeout_ms", CONFIG_TYPE_NUMBER, "500"}, + {"linger_timeout_ms", CONFIG_TYPE_NUMBER, NULL}, + + /* TODO(Feature): this is no longer a boolean, but yes/no/optional */ + {"ssl_verify_peer", CONFIG_TYPE_BOOLEAN, "no"}, + + {"ssl_ca_path", CONFIG_TYPE_DIRECTORY, NULL}, + {"ssl_ca_file", CONFIG_TYPE_FILE, NULL}, + {"ssl_verify_depth", CONFIG_TYPE_NUMBER, "9"}, + {"ssl_default_verify_paths", CONFIG_TYPE_BOOLEAN, "yes"}, + {"ssl_cipher_list", CONFIG_TYPE_STRING, NULL}, + {"ssl_protocol_version", CONFIG_TYPE_NUMBER, "0"}, + {"ssl_short_trust", CONFIG_TYPE_BOOLEAN, "no"}, +#if defined(USE_WEBSOCKET) + {"websocket_timeout_ms", CONFIG_TYPE_NUMBER, "30000"}, +#endif + {"decode_url", CONFIG_TYPE_BOOLEAN, "yes"}, + +#if defined(USE_LUA) + {"lua_preload_file", CONFIG_TYPE_FILE, NULL}, + {"lua_script_pattern", CONFIG_TYPE_EXT_PATTERN, "**.lua$"}, + {"lua_server_page_pattern", CONFIG_TYPE_EXT_PATTERN, "**.lp$|**.lsp$"}, +#endif +#if defined(USE_DUKTAPE) + /* The support for duktape is still in alpha version state. + * The name of this config option might change. */ + {"duktape_script_pattern", CONFIG_TYPE_EXT_PATTERN, "**.ssjs$"}, +#endif + +#if defined(USE_WEBSOCKET) + {"websocket_root", CONFIG_TYPE_DIRECTORY, NULL}, +#endif +#if defined(USE_LUA) && defined(USE_WEBSOCKET) + {"lua_websocket_pattern", CONFIG_TYPE_EXT_PATTERN, "**.lua$"}, +#endif + {"access_control_allow_origin", CONFIG_TYPE_STRING, "*"}, + {"access_control_allow_methods", CONFIG_TYPE_STRING, "*"}, + {"access_control_allow_headers", CONFIG_TYPE_STRING, "*"}, + {"error_pages", CONFIG_TYPE_DIRECTORY, NULL}, + {"tcp_nodelay", CONFIG_TYPE_NUMBER, "0"}, +#if !defined(NO_CACHING) + {"static_file_max_age", CONFIG_TYPE_NUMBER, "3600"}, +#endif +#if !defined(NO_SSL) + {"strict_transport_security_max_age", CONFIG_TYPE_NUMBER, NULL}, +#endif +#if defined(__linux__) + {"allow_sendfile_call", CONFIG_TYPE_BOOLEAN, "yes"}, +#endif +#if defined(_WIN32) + {"case_sensitive", CONFIG_TYPE_BOOLEAN, "no"}, +#endif +#if defined(USE_LUA) + {"lua_background_script", CONFIG_TYPE_FILE, NULL}, + {"lua_background_script_params", CONFIG_TYPE_STRING_LIST, NULL}, +#endif + {"additional_header", CONFIG_TYPE_STRING_MULTILINE, NULL}, + {"max_request_size", CONFIG_TYPE_NUMBER, "16384"}, + {"allow_index_script_resource", CONFIG_TYPE_BOOLEAN, "no"}, + {"validate_http_method", CONFIG_TYPE_BOOLEAN, "yes"}, + {"canonicalize_url_path", CONFIG_TYPE_BOOLEAN, "yes"}, + {"allow_unicode_in_urls", CONFIG_TYPE_BOOLEAN, "no"}, + {NULL, CONFIG_TYPE_UNKNOWN, NULL}}; + + +/* Check if the config_options and the corresponding enum have compatible + * sizes. */ +mg_static_assert((sizeof(config_options) / sizeof(config_options[0])) + == (NUM_OPTIONS + 1), + "config_options and enum not sync"); + + +enum { REQUEST_HANDLER, WEBSOCKET_HANDLER, AUTH_HANDLER }; + + +struct mg_handler_info { + /* Name/Pattern of the URI. */ + char *uri; + size_t uri_len; + + /* handler type */ + int handler_type; + + /* Handler for http/https or authorization requests. */ + mg_request_handler handler; + + /* Handler for ws/wss (websocket) requests. */ + mg_websocket_connect_handler connect_handler; + mg_websocket_ready_handler ready_handler; + mg_websocket_data_handler data_handler; + mg_websocket_close_handler close_handler; + + /* accepted subprotocols for ws/wss requests. */ + struct mg_websocket_subprotocols *subprotocols; + + /* Handler for authorization requests */ + mg_authorization_handler auth_handler; + + /* User supplied argument for the handler function. */ + void *cbdata; + + /* next handler in a linked list */ + struct mg_handler_info *next; +}; + + +enum { + CONTEXT_INVALID, + CONTEXT_SERVER, + CONTEXT_HTTP_CLIENT, + CONTEXT_WS_CLIENT +}; + + +struct mg_context { + volatile int stop_flag; /* Should we stop event loop */ + SSL_CTX *ssl_ctx; /* SSL context */ + char *config[NUM_OPTIONS]; /* Civetweb configuration parameters */ + struct mg_callbacks callbacks; /* User-defined callback function */ + void *user_data; /* User-defined data */ + int context_type; /* See CONTEXT_* above */ + + struct socket *listening_sockets; + struct pollfd *listening_socket_fds; + unsigned int num_listening_sockets; + + pthread_mutex_t thread_mutex; /* Protects (max|num)_threads */ + +#ifdef ALTERNATIVE_QUEUE + struct socket *client_socks; + void **client_wait_events; +#else + struct socket queue[MGSQLEN]; /* Accepted sockets */ + volatile int sq_head; /* Head of the socket queue */ + volatile int sq_tail; /* Tail of the socket queue */ + pthread_cond_t sq_full; /* Signaled when socket is produced */ + pthread_cond_t sq_empty; /* Signaled when socket is consumed */ +#endif + + unsigned int max_request_size; /* The max request size */ + + pthread_t masterthreadid; /* The master thread ID */ + unsigned int + cfg_worker_threads; /* The number of configured worker threads. */ + pthread_t *worker_threadids; /* The worker thread IDs */ + struct mg_connection *worker_connections; /* The connection struct, pre- + * allocated for each worker */ + + time_t start_time; /* Server start time, used for authentication + * and for diagnstics. */ + + uint64_t auth_nonce_mask; /* Mask for all nonce values */ + pthread_mutex_t nonce_mutex; /* Protects nonce_count */ + unsigned long nonce_count; /* Used nonces, used for authentication */ + + char *systemName; /* What operating system is running */ + + /* linked list of uri handlers */ + struct mg_handler_info *handlers; + +#if defined(USE_LUA) && defined(USE_WEBSOCKET) + /* linked list of shared lua websockets */ + struct mg_shared_lua_websocket_list *shared_lua_websockets; +#endif + +#if defined(USE_TIMERS) + struct ttimers *timers; +#endif + +#if defined(USE_LUA) + void *lua_background_state; +#endif + +#if defined(USE_SERVER_STATS) + int active_connections; + int max_connections; + int64_t total_connections; + int64_t total_requests; + struct mg_memory_stat ctx_memory; + int64_t total_data_read; + int64_t total_data_written; +#endif +}; + + +#if defined(USE_SERVER_STATS) +static struct mg_memory_stat mg_common_memory = {0, 0, 0}; + +static struct mg_memory_stat * +get_memory_stat(struct mg_context *ctx) +{ + if (ctx) { + return &(ctx->ctx_memory); + } + return &mg_common_memory; +} +#endif + +enum { + CONNECTION_TYPE_INVALID, + CONNECTION_TYPE_REQUEST, + CONNECTION_TYPE_RESPONSE +}; + +struct mg_connection { + int connection_type; /* see CONNECTION_TYPE_* above */ + + struct mg_request_info request_info; + struct mg_response_info response_info; + + struct mg_context *ctx; + +#if defined(USE_SERVER_STATS) + int conn_state; /* 0 = undef, numerical value may change in different + * versions. For the current definition, see + * mg_get_connection_info_impl */ +#endif + + SSL *ssl; /* SSL descriptor */ + SSL_CTX *client_ssl_ctx; /* SSL context for client connections */ + struct socket client; /* Connected client */ + time_t conn_birth_time; /* Time (wall clock) when connection was + * established */ + struct timespec req_time; /* Time (since system start) when the request + * was received */ + int64_t num_bytes_sent; /* Total bytes sent to client */ + int64_t content_len; /* Content-Length header value */ + int64_t consumed_content; /* How many bytes of content have been read */ + int is_chunked; /* Transfer-Encoding is chunked: + * 0 = not chunked, + * 1 = chunked, do data read yet, + * 2 = chunked, some data read, + * 3 = chunked, all data read + */ + size_t chunk_remainder; /* Unread data from the last chunk */ + char *buf; /* Buffer for received data */ + char *path_info; /* PATH_INFO part of the URL */ + + int must_close; /* 1 if connection must be closed */ + int accept_gzip; /* 1 if gzip encoding is accepted */ + int in_error_handler; /* 1 if in handler for user defined error + * pages */ +#if defined(USE_WEBSOCKET) + int in_websocket_handling; /* 1 if in read_websocket */ +#endif + int handled_requests; /* Number of requests handled by this connection + */ + int buf_size; /* Buffer size */ + int request_len; /* Size of the request + headers in a buffer */ + int data_len; /* Total size of data in a buffer */ + int status_code; /* HTTP reply status code, e.g. 200 */ + int throttle; /* Throttling, bytes/sec. <= 0 means no + * throttle */ + + time_t last_throttle_time; /* Last time throttled data was sent */ + int64_t last_throttle_bytes; /* Bytes sent this second */ + pthread_mutex_t mutex; /* Used by mg_(un)lock_connection to ensure + * atomic transmissions for websockets */ +#if defined(USE_LUA) && defined(USE_WEBSOCKET) + void *lua_websocket_state; /* Lua_State for a websocket connection */ +#endif + + int thread_index; /* Thread index within ctx */ +}; + + +/* Directory entry */ +struct de { + struct mg_connection *conn; + char *file_name; + struct mg_file_stat file; +}; + + +#if defined(USE_WEBSOCKET) +static int is_websocket_protocol(const struct mg_connection *conn); +#else +#define is_websocket_protocol(conn) (0) +#endif + + +#if !defined(NO_THREAD_NAME) +#if defined(_WIN32) && defined(_MSC_VER) +/* Set the thread name for debugging purposes in Visual Studio + * http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx + */ +#pragma pack(push, 8) +typedef struct tagTHREADNAME_INFO { + DWORD dwType; /* Must be 0x1000. */ + LPCSTR szName; /* Pointer to name (in user addr space). */ + DWORD dwThreadID; /* Thread ID (-1=caller thread). */ + DWORD dwFlags; /* Reserved for future use, must be zero. */ +} THREADNAME_INFO; +#pragma pack(pop) + +#elif defined(__linux__) + +#include +#include +#ifdef ALTERNATIVE_QUEUE +#include +#endif /* ALTERNATIVE_QUEUE */ + + +#if defined(ALTERNATIVE_QUEUE) + + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunreachable-code" +/* For every system, "(sizeof(int) == sizeof(void *))" is either always + * true or always false. One of the two branches is unreachable in any case. + * Unfortunately the C standard does not define a way to check this at + * compile time, since the #if preprocessor conditions can not use the sizeof + * operator as an argument. */ +#endif + +#if defined(__GNUC__) || defined(__MINGW32__) +/* GCC does not realize one branch is unreachable, so it raises some + * pointer cast warning within the unreachable branch. + */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wint-to-pointer-cast" +#pragma GCC diagnostic ignored "-Wpointer-to-int-cast" +#endif + + +static void * +event_create(void) +{ + int evhdl = eventfd(0, EFD_CLOEXEC); + int *ret; + + if (evhdl == -1) { + /* Linux uses -1 on error, Windows NULL. */ + /* However, Linux does not return 0 on success either. */ + return 0; + } + if (sizeof(int) == sizeof(void *)) { + ret = (void *)evhdl; + } else { + ret = (int *)mg_malloc(sizeof(int)); + if (ret) { + *ret = evhdl; + } else { + (void)close(evhdl); + } + } + + return (void *)ret; +} + + +static int +event_wait(void *eventhdl) +{ + uint64_t u; + int evhdl, s; + + if (sizeof(int) == sizeof(void *)) { + evhdl = (int)eventhdl; + } else { + if (!eventhdl) { + /* error */ + return 0; + } + evhdl = *(int *)eventhdl; + } + + s = (int)read(evhdl, &u, sizeof(u)); + if (s != sizeof(uint64_t)) { + /* error */ + return 0; + } + (void)u; /* the value is not required */ + return 1; +} + + +static int +event_signal(void *eventhdl) +{ + uint64_t u = 1; + int evhdl, s; + + if (sizeof(int) == sizeof(void *)) { + evhdl = (int)eventhdl; + } else { + if (!eventhdl) { + /* error */ + return 0; + } + evhdl = *(int *)eventhdl; + } + + s = (int)write(evhdl, &u, sizeof(u)); + if (s != sizeof(uint64_t)) { + /* error */ + return 0; + } + return 1; +} + + +static void +event_destroy(void *eventhdl) +{ + int evhdl; + + if (sizeof(int) == sizeof(void *)) { + evhdl = (int)eventhdl; + close(evhdl); + } else { + if (!eventhdl) { + /* error */ + return; + } + evhdl = *(int *)eventhdl; + close(evhdl); + mg_free(eventhdl); + } +} + + +#if defined(__GNUC__) || defined(__MINGW32__) +#pragma GCC diagnostic pop +#endif + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#endif + +#endif + + +#if !defined(__linux__) && !defined(_WIN32) && defined(ALTERNATIVE_QUEUE) + +struct posix_event { + pthread_mutex_t mutex; + pthread_cond_t cond; +}; + + +static void * +event_create(void) +{ + struct posix_event *ret = mg_malloc(sizeof(struct posix_event)); + if (ret == 0) { + /* out of memory */ + return 0; + } + if (0 != pthread_mutex_init(&(ret->mutex), NULL)) { + /* pthread mutex not available */ + mg_free(ret); + return 0; + } + if (0 != pthread_cond_init(&(ret->cond), NULL)) { + /* pthread cond not available */ + pthread_mutex_destroy(&(ret->mutex)); + mg_free(ret); + return 0; + } + return (void *)ret; +} + + +static int +event_wait(void *eventhdl) +{ + struct posix_event *ev = (struct posix_event *)eventhdl; + pthread_mutex_lock(&(ev->mutex)); + pthread_cond_wait(&(ev->cond), &(ev->mutex)); + pthread_mutex_unlock(&(ev->mutex)); + return 1; +} + + +static int +event_signal(void *eventhdl) +{ + struct posix_event *ev = (struct posix_event *)eventhdl; + pthread_mutex_lock(&(ev->mutex)); + pthread_cond_signal(&(ev->cond)); + pthread_mutex_unlock(&(ev->mutex)); + return 1; +} + + +static void +event_destroy(void *eventhdl) +{ + struct posix_event *ev = (struct posix_event *)eventhdl; + pthread_cond_destroy(&(ev->cond)); + pthread_mutex_destroy(&(ev->mutex)); + mg_free(ev); +} +#endif + + +static void +mg_set_thread_name(const char *name) +{ + char threadName[16 + 1]; /* 16 = Max. thread length in Linux/OSX/.. */ + + mg_snprintf( + NULL, NULL, threadName, sizeof(threadName), "civetweb-%s", name); + +#if defined(_WIN32) +#if defined(_MSC_VER) + /* Windows and Visual Studio Compiler */ + __try + { + THREADNAME_INFO info; + info.dwType = 0x1000; + info.szName = threadName; + info.dwThreadID = ~0U; + info.dwFlags = 0; + + RaiseException(0x406D1388, + 0, + sizeof(info) / sizeof(ULONG_PTR), + (ULONG_PTR *)&info); + } + __except(EXCEPTION_EXECUTE_HANDLER) + { + } +#elif defined(__MINGW32__) +/* No option known to set thread name for MinGW */ +#endif +#elif defined(__GLIBC__) \ + && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 12))) + /* pthread_setname_np first appeared in glibc in version 2.12*/ + (void)pthread_setname_np(pthread_self(), threadName); +#elif defined(__linux__) + /* on linux we can use the old prctl function */ + (void)prctl(PR_SET_NAME, threadName, 0, 0, 0); +#endif +} +#else /* !defined(NO_THREAD_NAME) */ +void +mg_set_thread_name(const char *threadName) +{ +} +#endif + + +#if defined(MG_LEGACY_INTERFACE) +const char ** +mg_get_valid_option_names(void) +{ + /* This function is deprecated. Use mg_get_valid_options instead. */ + static const char * + data[2 * sizeof(config_options) / sizeof(config_options[0])] = {0}; + int i; + + for (i = 0; config_options[i].name != NULL; i++) { + data[i * 2] = config_options[i].name; + data[i * 2 + 1] = config_options[i].default_value; + } + + return data; +} +#endif + + +const struct mg_option * +mg_get_valid_options(void) +{ + return config_options; +} + + +/* Do not open file (used in is_file_in_memory) */ +#define MG_FOPEN_MODE_NONE (0) + +/* Open file for read only access */ +#define MG_FOPEN_MODE_READ (1) + +/* Open file for writing, create and overwrite */ +#define MG_FOPEN_MODE_WRITE (2) + +/* Open file for writing, create and append */ +#define MG_FOPEN_MODE_APPEND (4) + + +/* If a file is in memory, set all "stat" members and the membuf pointer of + * output filep and return 1, otherwise return 0 and don't modify anything. + */ +static int +open_file_in_memory(const struct mg_connection *conn, + const char *path, + struct mg_file *filep, + int mode) +{ +#if defined(MG_USE_OPEN_FILE) + + size_t size = 0; + const char *buf = NULL; + if (!conn) { + return 0; + } + + if ((mode != MG_FOPEN_MODE_NONE) && (mode != MG_FOPEN_MODE_READ)) { + return 0; + } + + if (conn->ctx->callbacks.open_file) { + buf = conn->ctx->callbacks.open_file(conn, path, &size); + if (buf != NULL) { + if (filep == NULL) { + /* This is a file in memory, but we cannot store the + * properties + * now. + * Called from "is_file_in_memory" function. */ + return 1; + } + + /* NOTE: override filep->size only on success. Otherwise, it + * might + * break constructs like if (!mg_stat() || !mg_fopen()) ... */ + filep->access.membuf = buf; + filep->access.fp = NULL; + + /* Size was set by the callback */ + filep->stat.size = size; + + /* Assume the data may change during runtime by setting + * last_modified = now */ + filep->stat.last_modified = time(NULL); + + filep->stat.is_directory = 0; + filep->stat.is_gzipped = 0; + } + } + + return (buf != NULL); + +#else + (void)conn; + (void)path; + (void)filep; + (void)mode; + + return 0; + +#endif +} + + +static int +is_file_in_memory(const struct mg_connection *conn, const char *path) +{ + return open_file_in_memory(conn, path, NULL, MG_FOPEN_MODE_NONE); +} + + +static int +is_file_opened(const struct mg_file_access *fileacc) +{ + if (!fileacc) { + return 0; + } + return (fileacc->membuf != NULL) || (fileacc->fp != NULL); +} + + +static int mg_stat(const struct mg_connection *conn, + const char *path, + struct mg_file_stat *filep); + + +/* mg_fopen will open a file either in memory or on the disk. + * The input parameter path is a string in UTF-8 encoding. + * The input parameter mode is MG_FOPEN_MODE_* + * On success, either fp or membuf will be set in the output + * struct file. All status members will also be set. + * The function returns 1 on success, 0 on error. */ +static int +mg_fopen(const struct mg_connection *conn, + const char *path, + int mode, + struct mg_file *filep) +{ + int found; + + if (!filep) { + return 0; + } + filep->access.fp = NULL; + filep->access.membuf = NULL; + + if (!is_file_in_memory(conn, path)) { + + /* filep is initialized in mg_stat: all fields with memset to, + * some fields like size and modification date with values */ + found = mg_stat(conn, path, &(filep->stat)); + + if ((mode == MG_FOPEN_MODE_READ) && (!found)) { + /* file does not exist and will not be created */ + return 0; + } + +#ifdef _WIN32 + { + wchar_t wbuf[PATH_MAX]; + path_to_unicode(conn, path, wbuf, ARRAY_SIZE(wbuf)); + switch (mode) { + case MG_FOPEN_MODE_READ: + filep->access.fp = _wfopen(wbuf, L"rb"); + break; + case MG_FOPEN_MODE_WRITE: + filep->access.fp = _wfopen(wbuf, L"wb"); + break; + case MG_FOPEN_MODE_APPEND: + filep->access.fp = _wfopen(wbuf, L"ab"); + break; + } + } +#else + /* Linux et al already use unicode. No need to convert. */ + switch (mode) { + case MG_FOPEN_MODE_READ: + filep->access.fp = fopen(path, "r"); + break; + case MG_FOPEN_MODE_WRITE: + filep->access.fp = fopen(path, "w"); + break; + case MG_FOPEN_MODE_APPEND: + filep->access.fp = fopen(path, "a"); + break; + } + +#endif + if (!found) { + /* File did not exist before fopen was called. + * Maybe it has been created now. Get stat info + * like creation time now. */ + found = mg_stat(conn, path, &(filep->stat)); + (void)found; + } + + /* file is on disk */ + return (filep->access.fp != NULL); + + } else { + /* is_file_in_memory returned true */ + if (open_file_in_memory(conn, path, filep, mode)) { + /* file is in memory */ + return (filep->access.membuf != NULL); + } + } + + /* Open failed */ + return 0; +} + + +/* return 0 on success, just like fclose */ +static int +mg_fclose(struct mg_file_access *fileacc) +{ + int ret = -1; + if (fileacc != NULL) { + if (fileacc->fp != NULL) { + ret = fclose(fileacc->fp); + } else if (fileacc->membuf != NULL) { + ret = 0; + } + /* reset all members of fileacc */ + memset(fileacc, 0, sizeof(*fileacc)); + } + return ret; +} + + +static void +mg_strlcpy(register char *dst, register const char *src, size_t n) +{ + for (; *src != '\0' && n > 1; n--) { + *dst++ = *src++; + } + *dst = '\0'; +} + + +static int +lowercase(const char *s) +{ + return tolower(*(const unsigned char *)s); +} + + +int +mg_strncasecmp(const char *s1, const char *s2, size_t len) +{ + int diff = 0; + + if (len > 0) { + do { + diff = lowercase(s1++) - lowercase(s2++); + } while (diff == 0 && s1[-1] != '\0' && --len > 0); + } + + return diff; +} + + +int +mg_strcasecmp(const char *s1, const char *s2) +{ + int diff; + + do { + diff = lowercase(s1++) - lowercase(s2++); + } while (diff == 0 && s1[-1] != '\0'); + + return diff; +} + + +static char * +mg_strndup(const char *ptr, size_t len) +{ + char *p; + + if ((p = (char *)mg_malloc(len + 1)) != NULL) { + mg_strlcpy(p, ptr, len + 1); + } + + return p; +} + + +static char * +mg_strdup(const char *str) +{ + return mg_strndup(str, strlen(str)); +} + + +static const char * +mg_strcasestr(const char *big_str, const char *small_str) +{ + size_t i, big_len = strlen(big_str), small_len = strlen(small_str); + + if (big_len >= small_len) { + for (i = 0; i <= (big_len - small_len); i++) { + if (mg_strncasecmp(big_str + i, small_str, small_len) == 0) { + return big_str + i; + } + } + } + + return NULL; +} + + +/* Return null terminated string of given maximum length. + * Report errors if length is exceeded. */ +static void +mg_vsnprintf(const struct mg_connection *conn, + int *truncated, + char *buf, + size_t buflen, + const char *fmt, + va_list ap) +{ + int n, ok; + + if (buflen == 0) { + if (truncated) { + *truncated = 1; + } + return; + } + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wformat-nonliteral" +/* Using fmt as a non-literal is intended here, since it is mostly called + * indirectly by mg_snprintf */ +#endif + + n = (int)vsnprintf_impl(buf, buflen, fmt, ap); + ok = (n >= 0) && ((size_t)n < buflen); + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + + if (ok) { + if (truncated) { + *truncated = 0; + } + } else { + if (truncated) { + *truncated = 1; + } + mg_cry(conn, + "truncating vsnprintf buffer: [%.*s]", + (int)((buflen > 200) ? 200 : (buflen - 1)), + buf); + n = (int)buflen - 1; + } + buf[n] = '\0'; +} + + +static void +mg_snprintf(const struct mg_connection *conn, + int *truncated, + char *buf, + size_t buflen, + const char *fmt, + ...) +{ + va_list ap; + + va_start(ap, fmt); + mg_vsnprintf(conn, truncated, buf, buflen, fmt, ap); + va_end(ap); +} + + +static int +get_option_index(const char *name) +{ + int i; + + for (i = 0; config_options[i].name != NULL; i++) { + if (strcmp(config_options[i].name, name) == 0) { + return i; + } + } + return -1; +} + + +const char * +mg_get_option(const struct mg_context *ctx, const char *name) +{ + int i; + if ((i = get_option_index(name)) == -1) { + return NULL; + } else if (!ctx || ctx->config[i] == NULL) { + return ""; + } else { + return ctx->config[i]; + } +} + + +struct mg_context * +mg_get_context(const struct mg_connection *conn) +{ + return (conn == NULL) ? (struct mg_context *)NULL : (conn->ctx); +} + + +void * +mg_get_user_data(const struct mg_context *ctx) +{ + return (ctx == NULL) ? NULL : ctx->user_data; +} + + +void +mg_set_user_connection_data(struct mg_connection *conn, void *data) +{ + if (conn != NULL) { + conn->request_info.conn_data = data; + } +} + + +void * +mg_get_user_connection_data(const struct mg_connection *conn) +{ + if (conn != NULL) { + return conn->request_info.conn_data; + } + return NULL; +} + + +#if defined(MG_LEGACY_INTERFACE) +/* Deprecated: Use mg_get_server_ports instead. */ +size_t +mg_get_ports(const struct mg_context *ctx, size_t size, int *ports, int *ssl) +{ + size_t i; + if (!ctx) { + return 0; + } + for (i = 0; i < size && i < ctx->num_listening_sockets; i++) { + ssl[i] = ctx->listening_sockets[i].is_ssl; + ports[i] = +#if defined(USE_IPV6) + (ctx->listening_sockets[i].lsa.sa.sa_family == AF_INET6) + ? ntohs(ctx->listening_sockets[i].lsa.sin6.sin6_port) + : +#endif + ntohs(ctx->listening_sockets[i].lsa.sin.sin_port); + } + return i; +} +#endif + + +int +mg_get_server_ports(const struct mg_context *ctx, + int size, + struct mg_server_ports *ports) +{ + int i, cnt = 0; + + if (size <= 0) { + return -1; + } + memset(ports, 0, sizeof(*ports) * (size_t)size); + if (!ctx) { + return -1; + } + if (!ctx->listening_sockets) { + return -1; + } + + for (i = 0; (i < size) && (i < (int)ctx->num_listening_sockets); i++) { + + ports[cnt].port = +#if defined(USE_IPV6) + (ctx->listening_sockets[i].lsa.sa.sa_family == AF_INET6) + ? ntohs(ctx->listening_sockets[i].lsa.sin6.sin6_port) + : +#endif + ntohs(ctx->listening_sockets[i].lsa.sin.sin_port); + ports[cnt].is_ssl = ctx->listening_sockets[i].is_ssl; + ports[cnt].is_redirect = ctx->listening_sockets[i].ssl_redir; + + if (ctx->listening_sockets[i].lsa.sa.sa_family == AF_INET) { + /* IPv4 */ + ports[cnt].protocol = 1; + cnt++; + } else if (ctx->listening_sockets[i].lsa.sa.sa_family == AF_INET6) { + /* IPv6 */ + ports[cnt].protocol = 3; + cnt++; + } + } + + return cnt; +} + + +static void +sockaddr_to_string(char *buf, size_t len, const union usa *usa) +{ + buf[0] = '\0'; + + if (!usa) { + return; + } + + if (usa->sa.sa_family == AF_INET) { + getnameinfo(&usa->sa, + sizeof(usa->sin), + buf, + (unsigned)len, + NULL, + 0, + NI_NUMERICHOST); + } +#if defined(USE_IPV6) + else if (usa->sa.sa_family == AF_INET6) { + getnameinfo(&usa->sa, + sizeof(usa->sin6), + buf, + (unsigned)len, + NULL, + 0, + NI_NUMERICHOST); + } +#endif +} + + +/* Convert time_t to a string. According to RFC2616, Sec 14.18, this must be + * included in all responses other than 100, 101, 5xx. */ +static void +gmt_time_string(char *buf, size_t buf_len, time_t *t) +{ + struct tm *tm; + + tm = ((t != NULL) ? gmtime(t) : NULL); + if (tm != NULL) { + strftime(buf, buf_len, "%a, %d %b %Y %H:%M:%S GMT", tm); + } else { + mg_strlcpy(buf, "Thu, 01 Jan 1970 00:00:00 GMT", buf_len); + buf[buf_len - 1] = '\0'; + } +} + + +/* difftime for struct timespec. Return value is in seconds. */ +static double +mg_difftimespec(const struct timespec *ts_now, const struct timespec *ts_before) +{ + return (double)(ts_now->tv_nsec - ts_before->tv_nsec) * 1.0E-9 + + (double)(ts_now->tv_sec - ts_before->tv_sec); +} + + +/* Print error message to the opened error log stream. */ +void +mg_cry(const struct mg_connection *conn, const char *fmt, ...) +{ + char buf[MG_BUF_LEN], src_addr[IP_ADDR_STR_LEN]; + va_list ap; + struct mg_file fi; + time_t timestamp; + + va_start(ap, fmt); + IGNORE_UNUSED_RESULT(vsnprintf_impl(buf, sizeof(buf), fmt, ap)); + va_end(ap); + buf[sizeof(buf) - 1] = 0; + + DEBUG_TRACE("mg_cry: %s", buf); + + if (!conn) { + puts(buf); + return; + } + + /* Do not lock when getting the callback value, here and below. + * I suppose this is fine, since function cannot disappear in the + * same way string option can. */ + if ((conn->ctx->callbacks.log_message == NULL) + || (conn->ctx->callbacks.log_message(conn, buf) == 0)) { + + if (conn->ctx->config[ERROR_LOG_FILE] != NULL) { + if (mg_fopen(conn, + conn->ctx->config[ERROR_LOG_FILE], + MG_FOPEN_MODE_APPEND, + &fi) == 0) { + fi.access.fp = NULL; + } + } else { + fi.access.fp = NULL; + } + + if (fi.access.fp != NULL) { + flockfile(fi.access.fp); + timestamp = time(NULL); + + sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa); + fprintf(fi.access.fp, + "[%010lu] [error] [client %s] ", + (unsigned long)timestamp, + src_addr); + + if (conn->request_info.request_method != NULL) { + fprintf(fi.access.fp, + "%s %s: ", + conn->request_info.request_method, + conn->request_info.request_uri + ? conn->request_info.request_uri + : ""); + } + + fprintf(fi.access.fp, "%s", buf); + fputc('\n', fi.access.fp); + fflush(fi.access.fp); + funlockfile(fi.access.fp); + (void)mg_fclose(&fi.access); /* Ignore errors. We can't call + * mg_cry here anyway ;-) */ + } + } +} + +void +mg_set_http_status(struct mg_connection *conn, int status) +{ + conn->status_code = status; +} + +/* Return fake connection structure. Used for logging, if connection + * is not applicable at the moment of logging. */ +static struct mg_connection * +fc(struct mg_context *ctx) +{ + static struct mg_connection fake_connection; + fake_connection.ctx = ctx; + return &fake_connection; +} + + +const char * +mg_version(void) +{ + return CIVETWEB_VERSION; +} + + +const struct mg_request_info * +mg_get_request_info(const struct mg_connection *conn) +{ + if (!conn) { + return NULL; + } +#if 1 /* TODO: deal with legacy */ + if (conn->connection_type == CONNECTION_TYPE_RESPONSE) { + static char txt[16]; + sprintf(txt, "%03i", conn->response_info.status_code); + + ((struct mg_connection *)conn)->request_info.local_uri = + ((struct mg_connection *)conn)->request_info.request_uri = + txt; /* TODO: not thread safe */ + + ((struct mg_connection *)conn)->request_info.num_headers = + conn->response_info.num_headers; + memcpy(((struct mg_connection *)conn)->request_info.http_headers, + conn->response_info.http_headers, + sizeof(conn->response_info.http_headers)); + } else +#endif + if (conn->connection_type != CONNECTION_TYPE_REQUEST) { + return NULL; + } + return &conn->request_info; +} + + +const struct mg_response_info * +mg_get_response_info(const struct mg_connection *conn) +{ + if (!conn) { + return NULL; + } + if (conn->connection_type != CONNECTION_TYPE_RESPONSE) { + return NULL; + } + return &conn->response_info; +} + + +static const char * +get_proto_name(const struct mg_connection *conn) +{ +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunreachable-code" +/* Depending on USE_WEBSOCKET and NO_SSL, some oft the protocols might be + * not supported. Clang raises an "unreachable code" warning for parts of ?: + * unreachable, but splitting into four different #ifdef clauses here is more + * complicated. + */ +#endif + + const struct mg_request_info *ri = &conn->request_info; + + const char *proto = + (is_websocket_protocol(conn) ? (ri->is_ssl ? "wss" : "ws") + : (ri->is_ssl ? "https" : "http")); + + return proto; + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif +} + + +int +mg_get_request_link(const struct mg_connection *conn, char *buf, size_t buflen) +{ + if ((buflen < 1) || (buf == 0) || (conn == 0)) { + return -1; + } else { + + int truncated = 0; + const struct mg_request_info *ri = &conn->request_info; + + const char *proto = get_proto_name(conn); + + if (ri->local_uri == NULL) { + return -1; + } + + if ((ri->request_uri != NULL) + && strcmp(ri->local_uri, ri->request_uri)) { + mg_snprintf(conn, + &truncated, + buf, + buflen, + "%s://%s", + proto, + ri->request_uri); + if (truncated) { + return -1; + } + return 0; + } else { + +#if defined(USE_IPV6) + int is_ipv6 = (conn->client.lsa.sa.sa_family == AF_INET6); + int port = is_ipv6 ? htons(conn->client.lsa.sin6.sin6_port) + : htons(conn->client.lsa.sin.sin_port); +#else + int port = htons(conn->client.lsa.sin.sin_port); +#endif + int def_port = ri->is_ssl ? 443 : 80; + int auth_domain_check_enabled = + conn->ctx->config[ENABLE_AUTH_DOMAIN_CHECK] + && (!mg_strcasecmp(conn->ctx->config[ENABLE_AUTH_DOMAIN_CHECK], + "yes")); + const char *server_domain = + conn->ctx->config[AUTHENTICATION_DOMAIN]; + + char portstr[16]; + char server_ip[48]; + + if (port != def_port) { + sprintf(portstr, ":%u", (unsigned)port); + } else { + portstr[0] = 0; + } + + if (!auth_domain_check_enabled || !server_domain) { + + sockaddr_to_string(server_ip, + sizeof(server_ip), + &conn->client.lsa); + + server_domain = server_ip; + } + + mg_snprintf(conn, + &truncated, + buf, + buflen, + "%s://%s%s%s", + proto, + server_domain, + portstr, + ri->local_uri); + if (truncated) { + return -1; + } + return 0; + } + } +} + +struct sockaddr * +mg_get_local_addr(struct mg_connection *conn) +{ + return &conn->client.lsa.sa; +} + + +/* Skip the characters until one of the delimiters characters found. + * 0-terminate resulting word. Skip the delimiter and following whitespaces. + * Advance pointer to buffer to the next word. Return found 0-terminated + * word. + * Delimiters can be quoted with quotechar. */ +static char * +skip_quoted(char **buf, + const char *delimiters, + const char *whitespace, + char quotechar) +{ + char *p, *begin_word, *end_word, *end_whitespace; + + begin_word = *buf; + end_word = begin_word + strcspn(begin_word, delimiters); + + /* Check for quotechar */ + if (end_word > begin_word) { + p = end_word - 1; + while (*p == quotechar) { + /* While the delimiter is quoted, look for the next delimiter. + */ + /* This happens, e.g., in calls from parse_auth_header, + * if the user name contains a " character. */ + + /* If there is anything beyond end_word, copy it. */ + if (*end_word != '\0') { + size_t end_off = strcspn(end_word + 1, delimiters); + memmove(p, end_word, end_off + 1); + p += end_off; /* p must correspond to end_word - 1 */ + end_word += end_off + 1; + } else { + *p = '\0'; + break; + } + } + for (p++; p < end_word; p++) { + *p = '\0'; + } + } + + if (*end_word == '\0') { + *buf = end_word; + } else { + +#if defined(__GNUC__) || defined(__MINGW32__) +/* Disable spurious conversion warning for GCC */ +#if GCC_VERSION >= 40500 +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wsign-conversion" +#endif /* GCC_VERSION >= 40500 */ +#endif /* defined(__GNUC__) || defined(__MINGW32__) */ + + end_whitespace = end_word + strspn(&end_word[1], whitespace) + 1; + +#if defined(__GNUC__) || defined(__MINGW32__) +#if GCC_VERSION >= 40500 +#pragma GCC diagnostic pop +#endif /* GCC_VERSION >= 40500 */ +#endif /* defined(__GNUC__) || defined(__MINGW32__) */ + + for (p = end_word; p < end_whitespace; p++) { + *p = '\0'; + } + + *buf = end_whitespace; + } + + return begin_word; +} + + +/* Return HTTP header value, or NULL if not found. */ +static const char * +get_header(const struct mg_header *hdr, int num_hdr, const char *name) +{ + int i; + for (i = 0; i < num_hdr; i++) { + if (!mg_strcasecmp(name, hdr[i].name)) { + return hdr[i].value; + } + } + + return NULL; +} + + +#if defined(USE_WEBSOCKET) +/* Retrieve requested HTTP header multiple values, and return the number of + * found occurences */ +static int +get_req_headers(const struct mg_request_info *ri, + const char *name, + const char **output, + int output_max_size) +{ + int i; + int cnt = 0; + if (ri) { + for (i = 0; i < ri->num_headers && cnt < output_max_size; i++) { + if (!mg_strcasecmp(name, ri->http_headers[i].name)) { + output[cnt++] = ri->http_headers[i].value; + } + } + } + return cnt; +} +#endif + + +const char * +mg_get_header(const struct mg_connection *conn, const char *name) +{ + if (!conn) { + return NULL; + } + + if (conn->connection_type == CONNECTION_TYPE_REQUEST) { + return get_header(conn->request_info.http_headers, + conn->request_info.num_headers, + name); + } + if (conn->connection_type == CONNECTION_TYPE_RESPONSE) { + return get_header(conn->response_info.http_headers, + conn->request_info.num_headers, + name); + } + return NULL; +} + + +static const char * +get_http_version(const struct mg_connection *conn) +{ + if (!conn) { + return NULL; + } + + if (conn->connection_type == CONNECTION_TYPE_REQUEST) { + return conn->request_info.http_version; + } + if (conn->connection_type == CONNECTION_TYPE_RESPONSE) { + return conn->response_info.http_version; + } + return NULL; +} + + +/* A helper function for traversing a comma separated list of values. + * It returns a list pointer shifted to the next value, or NULL if the end + * of the list found. + * Value is stored in val vector. If value has form "x=y", then eq_val + * vector is initialized to point to the "y" part, and val vector length + * is adjusted to point only to "x". */ +static const char * +next_option(const char *list, struct vec *val, struct vec *eq_val) +{ + int end; + +reparse: + if (val == NULL || list == NULL || *list == '\0') { + /* End of the list */ + return NULL; + } + + /* Skip over leading LWS */ + while (*list == ' ' || *list == '\t') + list++; + + val->ptr = list; + if ((list = strchr(val->ptr, ',')) != NULL) { + /* Comma found. Store length and shift the list ptr */ + val->len = ((size_t)(list - val->ptr)); + list++; + } else { + /* This value is the last one */ + list = val->ptr + strlen(val->ptr); + val->len = ((size_t)(list - val->ptr)); + } + + /* Adjust length for trailing LWS */ + end = (int)val->len - 1; + while (end >= 0 && ((val->ptr[end] == ' ') || (val->ptr[end] == '\t'))) + end--; + val->len = (size_t)(end + 1); + + if (val->len == 0) { + /* Ignore any empty entries. */ + goto reparse; + } + + if (eq_val != NULL) { + /* Value has form "x=y", adjust pointers and lengths + * so that val points to "x", and eq_val points to "y". */ + eq_val->len = 0; + eq_val->ptr = (const char *)memchr(val->ptr, '=', val->len); + if (eq_val->ptr != NULL) { + eq_val->ptr++; /* Skip over '=' character */ + eq_val->len = ((size_t)(val->ptr - eq_val->ptr)) + val->len; + val->len = ((size_t)(eq_val->ptr - val->ptr)) - 1; + } + } + + return list; +} + + +/* A helper function for checking if a comma separated list of values + * contains + * the given option (case insensitvely). + * 'header' can be NULL, in which case false is returned. */ +static int +header_has_option(const char *header, const char *option) +{ + struct vec opt_vec; + struct vec eq_vec; + + /* + assert(option != NULL); + assert(option[0] != '\0'); + */ + + while ((header = next_option(header, &opt_vec, &eq_vec)) != NULL) { + if (mg_strncasecmp(option, opt_vec.ptr, opt_vec.len) == 0) + return 1; + } + + return 0; +} + + +/* Perform case-insensitive match of string against pattern */ +static int +match_prefix(const char *pattern, size_t pattern_len, const char *str) +{ + const char *or_str; + size_t i; + int j, len, res; + + if ((or_str = (const char *)memchr(pattern, '|', pattern_len)) != NULL) { + res = match_prefix(pattern, (size_t)(or_str - pattern), str); + return (res > 0) ? res : match_prefix(or_str + 1, + (size_t)((pattern + pattern_len) + - (or_str + 1)), + str); + } + + for (i = 0, j = 0; (i < pattern_len); i++, j++) { + if ((pattern[i] == '?') && (str[j] != '\0')) { + continue; + } else if (pattern[i] == '$') { + return (str[j] == '\0') ? j : -1; + } else if (pattern[i] == '*') { + i++; + if (pattern[i] == '*') { + i++; + len = (int)strlen(str + j); + } else { + len = (int)strcspn(str + j, "/"); + } + if (i == pattern_len) { + return j + len; + } + do { + res = match_prefix(pattern + i, pattern_len - i, str + j + len); + } while (res == -1 && len-- > 0); + return (res == -1) ? -1 : j + res + len; + } else if (lowercase(&pattern[i]) != lowercase(&str[j])) { + return -1; + } + } + return j; +} + + +/* HTTP 1.1 assumes keep alive if "Connection:" header is not set + * This function must tolerate situations when connection info is not + * set up, for example if request parsing failed. */ +static int +should_keep_alive(const struct mg_connection *conn) +{ + const char *http_version; + const char *header; + + /* First satisfy needs of the server */ + if ((conn == NULL) || conn->must_close) { + /* Close, if civetweb framework needs to close */ + return 0; + } + + if (mg_strcasecmp(conn->ctx->config[ENABLE_KEEP_ALIVE], "yes") != 0) { + /* Close, if keep alive is not enabled */ + return 0; + } + + /* Check explicit wish of the client */ + header = mg_get_header(conn, "Connection"); + if (header) { + /* If there is a connection header from the client, obey */ + if (header_has_option(header, "keep-alive")) { + return 1; + } + return 0; + } + + /* Use default of the standard */ + http_version = get_http_version(conn); + if (http_version && (0 == strcmp(http_version, "1.1"))) { + /* HTTP 1.1 default is keep alive */ + return 1; + } + + /* HTTP 1.0 (and earlier) default is to close the connection */ + return 0; +} + + +static int +should_decode_url(const struct mg_connection *conn) +{ + if (!conn || !conn->ctx) { + return 0; + } + + return (mg_strcasecmp(conn->ctx->config[DECODE_URL], "yes") == 0); +} + + +static const char * +suggest_connection_header(const struct mg_connection *conn) +{ + return should_keep_alive(conn) ? "keep-alive" : "close"; +} + + +static int +send_no_cache_header(struct mg_connection *conn) +{ + /* Send all current and obsolete cache opt-out directives. */ + return mg_printf(conn, + "Cache-Control: no-cache, no-store, " + "must-revalidate, private, max-age=0\r\n" + "Pragma: no-cache\r\n" + "Expires: 0\r\n"); +} + + +static int +send_static_cache_header(struct mg_connection *conn) +{ +#if !defined(NO_CACHING) + /* Read the server config to check how long a file may be cached. + * The configuration is in seconds. */ + int max_age = atoi(conn->ctx->config[STATIC_FILE_MAX_AGE]); + if (max_age <= 0) { + /* 0 means "do not cache". All values <0 are reserved + * and may be used differently in the future. */ + /* If a file should not be cached, do not only send + * max-age=0, but also pragmas and Expires headers. */ + return send_no_cache_header(conn); + } + + /* Use "Cache-Control: max-age" instead of "Expires" header. + * Reason: see https://www.mnot.net/blog/2007/05/15/expires_max-age */ + /* See also https://www.mnot.net/cache_docs/ */ + /* According to RFC 2616, Section 14.21, caching times should not exceed + * one year. A year with 365 days corresponds to 31536000 seconds, a + * leap + * year to 31622400 seconds. For the moment, we just send whatever has + * been configured, still the behavior for >1 year should be considered + * as undefined. */ + return mg_printf(conn, "Cache-Control: max-age=%u\r\n", (unsigned)max_age); +#else /* NO_CACHING */ + return send_no_cache_header(conn); +#endif /* !NO_CACHING */ +} + + +static int +send_additional_header(struct mg_connection *conn) +{ + int i = 0; + const char *header = conn->ctx->config[ADDITIONAL_HEADER]; + +#if !defined(NO_SSL) + if (conn->ctx->config[STRICT_HTTPS_MAX_AGE]) { + int max_age = atoi(conn->ctx->config[STRICT_HTTPS_MAX_AGE]); + if (max_age >= 0) { + i += mg_printf(conn, + "Strict-Transport-Security: max-age=%u\r\n", + (unsigned)max_age); + } + } +#endif + + if (header && header[0]) { + i += mg_printf(conn, "%s\r\n", header); + } + + return i; +} + + +static void handle_file_based_request(struct mg_connection *conn, + const char *path, + struct mg_file *filep); + + +const char * +mg_get_response_code_text(const struct mg_connection *conn, int response_code) +{ + /* See IANA HTTP status code assignment: + * http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml + */ + + switch (response_code) { + /* RFC2616 Section 10.1 - Informational 1xx */ + case 100: + return "Continue"; /* RFC2616 Section 10.1.1 */ + case 101: + return "Switching Protocols"; /* RFC2616 Section 10.1.2 */ + case 102: + return "Processing"; /* RFC2518 Section 10.1 */ + + /* RFC2616 Section 10.2 - Successful 2xx */ + case 200: + return "OK"; /* RFC2616 Section 10.2.1 */ + case 201: + return "Created"; /* RFC2616 Section 10.2.2 */ + case 202: + return "Accepted"; /* RFC2616 Section 10.2.3 */ + case 203: + return "Non-Authoritative Information"; /* RFC2616 Section 10.2.4 */ + case 204: + return "No Content"; /* RFC2616 Section 10.2.5 */ + case 205: + return "Reset Content"; /* RFC2616 Section 10.2.6 */ + case 206: + return "Partial Content"; /* RFC2616 Section 10.2.7 */ + case 207: + return "Multi-Status"; /* RFC2518 Section 10.2, RFC4918 Section 11.1 + */ + case 208: + return "Already Reported"; /* RFC5842 Section 7.1 */ + + case 226: + return "IM used"; /* RFC3229 Section 10.4.1 */ + + /* RFC2616 Section 10.3 - Redirection 3xx */ + case 300: + return "Multiple Choices"; /* RFC2616 Section 10.3.1 */ + case 301: + return "Moved Permanently"; /* RFC2616 Section 10.3.2 */ + case 302: + return "Found"; /* RFC2616 Section 10.3.3 */ + case 303: + return "See Other"; /* RFC2616 Section 10.3.4 */ + case 304: + return "Not Modified"; /* RFC2616 Section 10.3.5 */ + case 305: + return "Use Proxy"; /* RFC2616 Section 10.3.6 */ + case 307: + return "Temporary Redirect"; /* RFC2616 Section 10.3.8 */ + case 308: + return "Permanent Redirect"; /* RFC7238 Section 3 */ + + /* RFC2616 Section 10.4 - Client Error 4xx */ + case 400: + return "Bad Request"; /* RFC2616 Section 10.4.1 */ + case 401: + return "Unauthorized"; /* RFC2616 Section 10.4.2 */ + case 402: + return "Payment Required"; /* RFC2616 Section 10.4.3 */ + case 403: + return "Forbidden"; /* RFC2616 Section 10.4.4 */ + case 404: + return "Not Found"; /* RFC2616 Section 10.4.5 */ + case 405: + return "Method Not Allowed"; /* RFC2616 Section 10.4.6 */ + case 406: + return "Not Acceptable"; /* RFC2616 Section 10.4.7 */ + case 407: + return "Proxy Authentication Required"; /* RFC2616 Section 10.4.8 */ + case 408: + return "Request Time-out"; /* RFC2616 Section 10.4.9 */ + case 409: + return "Conflict"; /* RFC2616 Section 10.4.10 */ + case 410: + return "Gone"; /* RFC2616 Section 10.4.11 */ + case 411: + return "Length Required"; /* RFC2616 Section 10.4.12 */ + case 412: + return "Precondition Failed"; /* RFC2616 Section 10.4.13 */ + case 413: + return "Request Entity Too Large"; /* RFC2616 Section 10.4.14 */ + case 414: + return "Request-URI Too Large"; /* RFC2616 Section 10.4.15 */ + case 415: + return "Unsupported Media Type"; /* RFC2616 Section 10.4.16 */ + case 416: + return "Requested range not satisfiable"; /* RFC2616 Section 10.4.17 + */ + case 417: + return "Expectation Failed"; /* RFC2616 Section 10.4.18 */ + + case 421: + return "Misdirected Request"; /* RFC7540 Section 9.1.2 */ + case 422: + return "Unproccessable entity"; /* RFC2518 Section 10.3, RFC4918 + * Section 11.2 */ + case 423: + return "Locked"; /* RFC2518 Section 10.4, RFC4918 Section 11.3 */ + case 424: + return "Failed Dependency"; /* RFC2518 Section 10.5, RFC4918 + * Section 11.4 */ + + case 426: + return "Upgrade Required"; /* RFC 2817 Section 4 */ + + case 428: + return "Precondition Required"; /* RFC 6585, Section 3 */ + case 429: + return "Too Many Requests"; /* RFC 6585, Section 4 */ + + case 431: + return "Request Header Fields Too Large"; /* RFC 6585, Section 5 */ + + case 451: + return "Unavailable For Legal Reasons"; /* draft-tbray-http-legally-restricted-status-05, + * Section 3 */ + + /* RFC2616 Section 10.5 - Server Error 5xx */ + case 500: + return "Internal Server Error"; /* RFC2616 Section 10.5.1 */ + case 501: + return "Not Implemented"; /* RFC2616 Section 10.5.2 */ + case 502: + return "Bad Gateway"; /* RFC2616 Section 10.5.3 */ + case 503: + return "Service Unavailable"; /* RFC2616 Section 10.5.4 */ + case 504: + return "Gateway Time-out"; /* RFC2616 Section 10.5.5 */ + case 505: + return "HTTP Version not supported"; /* RFC2616 Section 10.5.6 */ + case 506: + return "Variant Also Negotiates"; /* RFC 2295, Section 8.1 */ + case 507: + return "Insufficient Storage"; /* RFC2518 Section 10.6, RFC4918 + * Section 11.5 */ + case 508: + return "Loop Detected"; /* RFC5842 Section 7.1 */ + + case 510: + return "Not Extended"; /* RFC 2774, Section 7 */ + case 511: + return "Network Authentication Required"; /* RFC 6585, Section 6 */ + + /* Other status codes, not shown in the IANA HTTP status code + * assignment. + * E.g., "de facto" standards due to common use, ... */ + case 418: + return "I am a teapot"; /* RFC2324 Section 2.3.2 */ + case 419: + return "Authentication Timeout"; /* common use */ + case 420: + return "Enhance Your Calm"; /* common use */ + case 440: + return "Login Timeout"; /* common use */ + case 509: + return "Bandwidth Limit Exceeded"; /* common use */ + + default: + /* This error code is unknown. This should not happen. */ + if (conn) { + mg_cry(conn, "Unknown HTTP response code: %u", response_code); + } + + /* Return at least a category according to RFC 2616 Section 10. */ + if (response_code >= 100 && response_code < 200) { + /* Unknown informational status code */ + return "Information"; + } + if (response_code >= 200 && response_code < 300) { + /* Unknown success code */ + return "Success"; + } + if (response_code >= 300 && response_code < 400) { + /* Unknown redirection code */ + return "Redirection"; + } + if (response_code >= 400 && response_code < 500) { + /* Unknown request error code */ + return "Client Error"; + } + if (response_code >= 500 && response_code < 600) { + /* Unknown server error code */ + return "Server Error"; + } + + /* Response code not even within reasonable range */ + return ""; + } +} + + +void +mg_send_http_error(struct mg_connection *conn, int status, const char *fmt, ...) +{ + char buf[MG_BUF_LEN]; + va_list ap; + int len, i, page_handler_found, scope, truncated, has_body; + char date[64]; + time_t curtime = time(NULL); + const char *error_handler = NULL; + struct mg_file error_page_file = STRUCT_FILE_INITIALIZER; + const char *error_page_file_ext, *tstr; + + const char *status_text = mg_get_response_code_text(conn, status); + + if (conn == NULL) { + return; + } + + conn->status_code = status; + if (conn->in_error_handler || (conn->ctx->callbacks.http_error == NULL) + || conn->ctx->callbacks.http_error(conn, status)) { + + /* Check for recursion */ + if (conn->in_error_handler) { + DEBUG_TRACE( + "Recursion when handling error %u - fall back to default", + status); + } else { + /* Send user defined error pages, if defined */ + error_handler = conn->ctx->config[ERROR_PAGES]; + error_page_file_ext = conn->ctx->config[INDEX_FILES]; + page_handler_found = 0; + + if (error_handler != NULL) { + for (scope = 1; (scope <= 3) && !page_handler_found; scope++) { + switch (scope) { + case 1: /* Handler for specific error, e.g. 404 error */ + mg_snprintf(conn, + &truncated, + buf, + sizeof(buf) - 32, + "%serror%03u.", + error_handler, + status); + break; + case 2: /* Handler for error group, e.g., 5xx error + * handler + * for all server errors (500-599) */ + mg_snprintf(conn, + &truncated, + buf, + sizeof(buf) - 32, + "%serror%01uxx.", + error_handler, + status / 100); + break; + default: /* Handler for all errors */ + mg_snprintf(conn, + &truncated, + buf, + sizeof(buf) - 32, + "%serror.", + error_handler); + break; + } + + /* String truncation in buf may only occur if + * error_handler is too long. This string is + * from the config, not from a client. */ + (void)truncated; + + len = (int)strlen(buf); + + tstr = strchr(error_page_file_ext, '.'); + + while (tstr) { + for (i = 1; + (i < 32) && (tstr[i] != 0) && (tstr[i] != ','); + i++) + buf[len + i - 1] = tstr[i]; + buf[len + i - 1] = 0; + + if (mg_stat(conn, buf, &error_page_file.stat)) { + DEBUG_TRACE("Check error page %s - found", buf); + page_handler_found = 1; + break; + } + DEBUG_TRACE("Check error page %s - not found", buf); + + tstr = strchr(tstr + i, '.'); + } + } + } + + if (page_handler_found) { + conn->in_error_handler = 1; + handle_file_based_request(conn, buf, &error_page_file); + conn->in_error_handler = 0; + return; + } + } + + /* No custom error page. Send default error page. */ + gmt_time_string(date, sizeof(date), &curtime); + + /* Errors 1xx, 204 and 304 MUST NOT send a body */ + has_body = ((status > 199) && (status != 204) && (status != 304)); + + conn->must_close = 1; + mg_printf(conn, "HTTP/1.1 %d %s\r\n", status, status_text); + send_no_cache_header(conn); + send_additional_header(conn); + if (has_body) { + mg_printf(conn, + "%s", + "Content-Type: text/plain; charset=utf-8\r\n"); + } + mg_printf(conn, + "Date: %s\r\n" + "Connection: close\r\n\r\n", + date); + + /* Errors 1xx, 204 and 304 MUST NOT send a body */ + if (has_body) { + mg_printf(conn, "Error %d: %s\n", status, status_text); + + if (fmt != NULL) { + va_start(ap, fmt); + mg_vsnprintf(conn, NULL, buf, sizeof(buf), fmt, ap); + va_end(ap); + mg_write(conn, buf, strlen(buf)); + DEBUG_TRACE("Error %i - [%s]", status, buf); + } + + } else { + /* No body allowed. Close the connection. */ + DEBUG_TRACE("Error %i", status); + } + } +} + +#if defined(_WIN32) && !defined(__SYMBIAN32__) +/* Create substitutes for POSIX functions in Win32. */ + +#if defined(__MINGW32__) +/* Show no warning in case system functions are not used. */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +#endif + + +FUNCTION_MAY_BE_UNUSED +static int +pthread_mutex_init(pthread_mutex_t *mutex, void *unused) +{ + (void)unused; + *mutex = CreateMutex(NULL, FALSE, NULL); + return (*mutex == NULL) ? -1 : 0; +} + +FUNCTION_MAY_BE_UNUSED +static int +pthread_mutex_destroy(pthread_mutex_t *mutex) +{ + return (CloseHandle(*mutex) == 0) ? -1 : 0; +} + + +FUNCTION_MAY_BE_UNUSED +static int +pthread_mutex_lock(pthread_mutex_t *mutex) +{ + return (WaitForSingleObject(*mutex, (DWORD)INFINITE) == WAIT_OBJECT_0) ? 0 + : -1; +} + + +#ifdef ENABLE_UNUSED_PTHREAD_FUNCTIONS +FUNCTION_MAY_BE_UNUSED +static int +pthread_mutex_trylock(pthread_mutex_t *mutex) +{ + switch (WaitForSingleObject(*mutex, 0)) { + case WAIT_OBJECT_0: + return 0; + case WAIT_TIMEOUT: + return -2; /* EBUSY */ + } + return -1; +} +#endif + + +FUNCTION_MAY_BE_UNUSED +static int +pthread_mutex_unlock(pthread_mutex_t *mutex) +{ + return (ReleaseMutex(*mutex) == 0) ? -1 : 0; +} + + +FUNCTION_MAY_BE_UNUSED +static int +pthread_cond_init(pthread_cond_t *cv, const void *unused) +{ + (void)unused; + InitializeCriticalSection(&cv->threadIdSec); + cv->waiting_thread = NULL; + return 0; +} + + +FUNCTION_MAY_BE_UNUSED +static int +pthread_cond_timedwait(pthread_cond_t *cv, + pthread_mutex_t *mutex, + const struct timespec *abstime) +{ + struct mg_workerTLS **ptls, + *tls = (struct mg_workerTLS *)pthread_getspecific(sTlsKey); + int ok; + int64_t nsnow, nswaitabs, nswaitrel; + DWORD mswaitrel; + + EnterCriticalSection(&cv->threadIdSec); + /* Add this thread to cv's waiting list */ + ptls = &cv->waiting_thread; + for (; *ptls != NULL; ptls = &(*ptls)->next_waiting_thread) + ; + tls->next_waiting_thread = NULL; + *ptls = tls; + LeaveCriticalSection(&cv->threadIdSec); + + if (abstime) { + nsnow = mg_get_current_time_ns(); + nswaitabs = + (((int64_t)abstime->tv_sec) * 1000000000) + abstime->tv_nsec; + nswaitrel = nswaitabs - nsnow; + if (nswaitrel < 0) { + nswaitrel = 0; + } + mswaitrel = (DWORD)(nswaitrel / 1000000); + } else { + mswaitrel = (DWORD)INFINITE; + } + + pthread_mutex_unlock(mutex); + ok = (WAIT_OBJECT_0 + == WaitForSingleObject(tls->pthread_cond_helper_mutex, mswaitrel)); + if (!ok) { + ok = 1; + EnterCriticalSection(&cv->threadIdSec); + ptls = &cv->waiting_thread; + for (; *ptls != NULL; ptls = &(*ptls)->next_waiting_thread) { + if (*ptls == tls) { + *ptls = tls->next_waiting_thread; + ok = 0; + break; + } + } + LeaveCriticalSection(&cv->threadIdSec); + if (ok) { + WaitForSingleObject(tls->pthread_cond_helper_mutex, + (DWORD)INFINITE); + } + } + /* This thread has been removed from cv's waiting list */ + pthread_mutex_lock(mutex); + + return ok ? 0 : -1; +} + + +FUNCTION_MAY_BE_UNUSED +static int +pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mutex) +{ + return pthread_cond_timedwait(cv, mutex, NULL); +} + + +FUNCTION_MAY_BE_UNUSED +static int +pthread_cond_signal(pthread_cond_t *cv) +{ + HANDLE wkup = NULL; + BOOL ok = FALSE; + + EnterCriticalSection(&cv->threadIdSec); + if (cv->waiting_thread) { + wkup = cv->waiting_thread->pthread_cond_helper_mutex; + cv->waiting_thread = cv->waiting_thread->next_waiting_thread; + + ok = SetEvent(wkup); + assert(ok); + } + LeaveCriticalSection(&cv->threadIdSec); + + return ok ? 0 : 1; +} + + +FUNCTION_MAY_BE_UNUSED +static int +pthread_cond_broadcast(pthread_cond_t *cv) +{ + EnterCriticalSection(&cv->threadIdSec); + while (cv->waiting_thread) { + pthread_cond_signal(cv); + } + LeaveCriticalSection(&cv->threadIdSec); + + return 0; +} + + +FUNCTION_MAY_BE_UNUSED +static int +pthread_cond_destroy(pthread_cond_t *cv) +{ + EnterCriticalSection(&cv->threadIdSec); + assert(cv->waiting_thread == NULL); + LeaveCriticalSection(&cv->threadIdSec); + DeleteCriticalSection(&cv->threadIdSec); + + return 0; +} + + +#ifdef ALTERNATIVE_QUEUE +FUNCTION_MAY_BE_UNUSED +static void * +event_create(void) +{ + return (void *)CreateEvent(NULL, FALSE, FALSE, NULL); +} + + +FUNCTION_MAY_BE_UNUSED +static int +event_wait(void *eventhdl) +{ + int res = WaitForSingleObject((HANDLE)eventhdl, (DWORD)INFINITE); + return (res == WAIT_OBJECT_0); +} + + +FUNCTION_MAY_BE_UNUSED +static int +event_signal(void *eventhdl) +{ + return (int)SetEvent((HANDLE)eventhdl); +} + + +FUNCTION_MAY_BE_UNUSED +static void +event_destroy(void *eventhdl) +{ + CloseHandle((HANDLE)eventhdl); +} +#endif + + +#if defined(__MINGW32__) +/* Enable unused function warning again */ +#pragma GCC diagnostic pop +#endif + + +/* For Windows, change all slashes to backslashes in path names. */ +static void +change_slashes_to_backslashes(char *path) +{ + int i; + + for (i = 0; path[i] != '\0'; i++) { + if (path[i] == '/') { + path[i] = '\\'; + } + + /* remove double backslash (check i > 0 to preserve UNC paths, + * like \\server\file.txt) */ + if ((path[i] == '\\') && (i > 0)) { + while ((path[i + 1] == '\\') || (path[i + 1] == '/')) { + (void)memmove(path + i + 1, path + i + 2, strlen(path + i + 1)); + } + } + } +} + + +static int +mg_wcscasecmp(const wchar_t *s1, const wchar_t *s2) +{ + int diff; + + do { + diff = tolower(*s1) - tolower(*s2); + s1++; + s2++; + } while ((diff == 0) && (s1[-1] != '\0')); + + return diff; +} + + +/* Encode 'path' which is assumed UTF-8 string, into UNICODE string. + * wbuf and wbuf_len is a target buffer and its length. */ +static void +path_to_unicode(const struct mg_connection *conn, + const char *path, + wchar_t *wbuf, + size_t wbuf_len) +{ + char buf[PATH_MAX], buf2[PATH_MAX]; + wchar_t wbuf2[MAX_PATH + 1]; + DWORD long_len, err; + int (*fcompare)(const wchar_t *, const wchar_t *) = mg_wcscasecmp; + + mg_strlcpy(buf, path, sizeof(buf)); + change_slashes_to_backslashes(buf); + + /* Convert to Unicode and back. If doubly-converted string does not + * match the original, something is fishy, reject. */ + memset(wbuf, 0, wbuf_len * sizeof(wchar_t)); + MultiByteToWideChar(CP_UTF8, 0, buf, -1, wbuf, (int)wbuf_len); + WideCharToMultiByte( + CP_UTF8, 0, wbuf, (int)wbuf_len, buf2, sizeof(buf2), NULL, NULL); + if (strcmp(buf, buf2) != 0) { + wbuf[0] = L'\0'; + } + + /* Windows file systems are not case sensitive, but you can still use + * uppercase and lowercase letters (on all modern file systems). + * The server can check if the URI uses the same upper/lowercase + * letters an the file system, effectively making Windows servers + * case sensitive (like Linux servers are). It is still not possible + * to use two files with the same name in different cases on Windows + * (like /a and /A) - this would be possible in Linux. + * As a default, Windows is not case sensitive, but the case sensitive + * file name check can be activated by an additional configuration. */ + if (conn) { + if (conn->ctx->config[CASE_SENSITIVE_FILES] + && !mg_strcasecmp(conn->ctx->config[CASE_SENSITIVE_FILES], "yes")) { + /* Use case sensitive compare function */ + fcompare = wcscmp; + } + } + (void)conn; /* conn is currently unused */ + +#if !defined(_WIN32_WCE) + /* Only accept a full file path, not a Windows short (8.3) path. */ + memset(wbuf2, 0, ARRAY_SIZE(wbuf2) * sizeof(wchar_t)); + long_len = GetLongPathNameW(wbuf, wbuf2, ARRAY_SIZE(wbuf2) - 1); + if (long_len == 0) { + err = GetLastError(); + if (err == ERROR_FILE_NOT_FOUND) { + /* File does not exist. This is not always a problem here. */ + return; + } + } + if ((long_len >= ARRAY_SIZE(wbuf2)) || (fcompare(wbuf, wbuf2) != 0)) { + /* Short name is used. */ + wbuf[0] = L'\0'; + } +#else + (void)long_len; + (void)wbuf2; + (void)err; + + if (strchr(path, '~')) { + wbuf[0] = L'\0'; + } +#endif +} + + +/* Windows happily opens files with some garbage at the end of file name. + * For example, fopen("a.cgi ", "r") on Windows successfully opens + * "a.cgi", despite one would expect an error back. + * This function returns non-0 if path ends with some garbage. */ +static int +path_cannot_disclose_cgi(const char *path) +{ + static const char *allowed_last_characters = "_-"; + int last = path[strlen(path) - 1]; + return isalnum(last) || strchr(allowed_last_characters, last) != NULL; +} + + +static int +mg_stat(const struct mg_connection *conn, + const char *path, + struct mg_file_stat *filep) +{ + wchar_t wbuf[PATH_MAX]; + WIN32_FILE_ATTRIBUTE_DATA info; + time_t creation_time; + + if (!filep) { + return 0; + } + memset(filep, 0, sizeof(*filep)); + + if (conn && is_file_in_memory(conn, path)) { + /* filep->is_directory = 0; filep->gzipped = 0; .. already done by + * memset */ + + /* Quick fix (for 1.9.x): */ + /* mg_stat must fill all fields, also for files in memory */ + struct mg_file tmp_file = STRUCT_FILE_INITIALIZER; + open_file_in_memory(conn, path, &tmp_file, MG_FOPEN_MODE_NONE); + filep->size = tmp_file.stat.size; + filep->location = 2; + /* TODO: for 1.10: restructure how files in memory are handled */ + + /* The "file in memory" feature is a candidate for deletion. + * Please join the discussion at + * https://groups.google.com/forum/#!topic/civetweb/h9HT4CmeYqI + */ + + filep->last_modified = time(NULL); /* TODO */ + /* last_modified = now ... assumes the file may change during + * runtime, + * so every mg_fopen call may return different data */ + /* last_modified = conn->ctx.start_time; + * May be used it the data does not change during runtime. This + * allows + * browser caching. Since we do not know, we have to assume the file + * in memory may change. */ + return 1; + } + + path_to_unicode(conn, path, wbuf, ARRAY_SIZE(wbuf)); + if (GetFileAttributesExW(wbuf, GetFileExInfoStandard, &info) != 0) { + filep->size = MAKEUQUAD(info.nFileSizeLow, info.nFileSizeHigh); + filep->last_modified = + SYS2UNIX_TIME(info.ftLastWriteTime.dwLowDateTime, + info.ftLastWriteTime.dwHighDateTime); + + /* On Windows, the file creation time can be higher than the + * modification time, e.g. when a file is copied. + * Since the Last-Modified timestamp is used for caching + * it should be based on the most recent timestamp. */ + creation_time = SYS2UNIX_TIME(info.ftCreationTime.dwLowDateTime, + info.ftCreationTime.dwHighDateTime); + if (creation_time > filep->last_modified) { + filep->last_modified = creation_time; + } + + filep->is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; + /* If file name is fishy, reset the file structure and return + * error. + * Note it is important to reset, not just return the error, cause + * functions like is_file_opened() check the struct. */ + if (!filep->is_directory && !path_cannot_disclose_cgi(path)) { + memset(filep, 0, sizeof(*filep)); + return 0; + } + + return 1; + } + + return 0; +} + + +static int +mg_remove(const struct mg_connection *conn, const char *path) +{ + wchar_t wbuf[PATH_MAX]; + path_to_unicode(conn, path, wbuf, ARRAY_SIZE(wbuf)); + return DeleteFileW(wbuf) ? 0 : -1; +} + + +static int +mg_mkdir(const struct mg_connection *conn, const char *path, int mode) +{ + wchar_t wbuf[PATH_MAX]; + (void)mode; + path_to_unicode(conn, path, wbuf, ARRAY_SIZE(wbuf)); + return CreateDirectoryW(wbuf, NULL) ? 0 : -1; +} + + +/* Create substitutes for POSIX functions in Win32. */ + +#if defined(__MINGW32__) +/* Show no warning in case system functions are not used. */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +#endif + + +/* Implementation of POSIX opendir/closedir/readdir for Windows. */ +FUNCTION_MAY_BE_UNUSED +static DIR * +mg_opendir(const struct mg_connection *conn, const char *name) +{ + DIR *dir = NULL; + wchar_t wpath[PATH_MAX]; + DWORD attrs; + + if (name == NULL) { + SetLastError(ERROR_BAD_ARGUMENTS); + } else if ((dir = (DIR *)mg_malloc(sizeof(*dir))) == NULL) { + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + } else { + path_to_unicode(conn, name, wpath, ARRAY_SIZE(wpath)); + attrs = GetFileAttributesW(wpath); + if (attrs != 0xFFFFFFFF && ((attrs & FILE_ATTRIBUTE_DIRECTORY) + == FILE_ATTRIBUTE_DIRECTORY)) { + (void)wcscat(wpath, L"\\*"); + dir->handle = FindFirstFileW(wpath, &dir->info); + dir->result.d_name[0] = '\0'; + } else { + mg_free(dir); + dir = NULL; + } + } + + return dir; +} + + +FUNCTION_MAY_BE_UNUSED +static int +mg_closedir(DIR *dir) +{ + int result = 0; + + if (dir != NULL) { + if (dir->handle != INVALID_HANDLE_VALUE) + result = FindClose(dir->handle) ? 0 : -1; + + mg_free(dir); + } else { + result = -1; + SetLastError(ERROR_BAD_ARGUMENTS); + } + + return result; +} + + +FUNCTION_MAY_BE_UNUSED +static struct dirent * +mg_readdir(DIR *dir) +{ + struct dirent *result = 0; + + if (dir) { + if (dir->handle != INVALID_HANDLE_VALUE) { + result = &dir->result; + (void)WideCharToMultiByte(CP_UTF8, + 0, + dir->info.cFileName, + -1, + result->d_name, + sizeof(result->d_name), + NULL, + NULL); + + if (!FindNextFileW(dir->handle, &dir->info)) { + (void)FindClose(dir->handle); + dir->handle = INVALID_HANDLE_VALUE; + } + + } else { + SetLastError(ERROR_FILE_NOT_FOUND); + } + } else { + SetLastError(ERROR_BAD_ARGUMENTS); + } + + return result; +} + + +#ifndef HAVE_POLL +FUNCTION_MAY_BE_UNUSED +static int +poll(struct pollfd *pfd, unsigned int n, int milliseconds) +{ + struct timeval tv; + fd_set set; + unsigned int i; + int result; + SOCKET maxfd = 0; + + memset(&tv, 0, sizeof(tv)); + tv.tv_sec = milliseconds / 1000; + tv.tv_usec = (milliseconds % 1000) * 1000; + FD_ZERO(&set); + + for (i = 0; i < n; i++) { + FD_SET((SOCKET)pfd[i].fd, &set); + pfd[i].revents = 0; + + if (pfd[i].fd > maxfd) { + maxfd = pfd[i].fd; + } + } + + if ((result = select((int)maxfd + 1, &set, NULL, NULL, &tv)) > 0) { + for (i = 0; i < n; i++) { + if (FD_ISSET(pfd[i].fd, &set)) { + pfd[i].revents = POLLIN; + } + } + } + + /* We should subtract the time used in select from remaining + * "milliseconds", in particular if called from mg_poll with a + * timeout quantum. + * Unfortunately, the remaining time is not stored in "tv" in all + * implementations, so the result in "tv" must be considered undefined. + * See http://man7.org/linux/man-pages/man2/select.2.html */ + + return result; +} +#endif /* HAVE_POLL */ + + +#if defined(__MINGW32__) +/* Enable unused function warning again */ +#pragma GCC diagnostic pop +#endif + + +static void +set_close_on_exec(SOCKET sock, struct mg_connection *conn /* may be null */) +{ + (void)conn; /* Unused. */ +#if defined(_WIN32_WCE) + (void)sock; +#else + (void)SetHandleInformation((HANDLE)(intptr_t)sock, HANDLE_FLAG_INHERIT, 0); +#endif +} + + +int +mg_start_thread(mg_thread_func_t f, void *p) +{ +#if defined(USE_STACK_SIZE) && (USE_STACK_SIZE > 1) + /* Compile-time option to control stack size, e.g. + * -DUSE_STACK_SIZE=16384 + */ + return ((_beginthread((void(__cdecl *)(void *))f, USE_STACK_SIZE, p) + == ((uintptr_t)(-1L))) + ? -1 + : 0); +#else + return ( + (_beginthread((void(__cdecl *)(void *))f, 0, p) == ((uintptr_t)(-1L))) + ? -1 + : 0); +#endif /* defined(USE_STACK_SIZE) && (USE_STACK_SIZE > 1) */ +} + + +/* Start a thread storing the thread context. */ +static int +mg_start_thread_with_id(unsigned(__stdcall *f)(void *), + void *p, + pthread_t *threadidptr) +{ + uintptr_t uip; + HANDLE threadhandle; + int result = -1; + + uip = _beginthreadex(NULL, 0, (unsigned(__stdcall *)(void *))f, p, 0, NULL); + threadhandle = (HANDLE)uip; + if ((uip != (uintptr_t)(-1L)) && (threadidptr != NULL)) { + *threadidptr = threadhandle; + result = 0; + } + + return result; +} + + +/* Wait for a thread to finish. */ +static int +mg_join_thread(pthread_t threadid) +{ + int result; + DWORD dwevent; + + result = -1; + dwevent = WaitForSingleObject(threadid, (DWORD)INFINITE); + if (dwevent == WAIT_FAILED) { + DEBUG_TRACE("WaitForSingleObject() failed, error %d", ERRNO); + } else { + if (dwevent == WAIT_OBJECT_0) { + CloseHandle(threadid); + result = 0; + } + } + + return result; +} + +#if !defined(NO_SSL_DL) && !defined(NO_SSL) +/* If SSL is loaded dynamically, dlopen/dlclose is required. */ +/* Create substitutes for POSIX functions in Win32. */ + +#if defined(__MINGW32__) +/* Show no warning in case system functions are not used. */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +#endif + + +FUNCTION_MAY_BE_UNUSED +static HANDLE +dlopen(const char *dll_name, int flags) +{ + wchar_t wbuf[PATH_MAX]; + (void)flags; + path_to_unicode(NULL, dll_name, wbuf, ARRAY_SIZE(wbuf)); + return LoadLibraryW(wbuf); +} + + +FUNCTION_MAY_BE_UNUSED +static int +dlclose(void *handle) +{ + int result; + + if (FreeLibrary((HMODULE)handle) != 0) { + result = 0; + } else { + result = -1; + } + + return result; +} + + +#if defined(__MINGW32__) +/* Enable unused function warning again */ +#pragma GCC diagnostic pop +#endif + +#endif + + +#if !defined(NO_CGI) +#define SIGKILL (0) + +static int +kill(pid_t pid, int sig_num) +{ + (void)TerminateProcess((HANDLE)pid, (UINT)sig_num); + (void)CloseHandle((HANDLE)pid); + return 0; +} + + +static void +trim_trailing_whitespaces(char *s) +{ + char *e = s + strlen(s) - 1; + while ((e > s) && isspace(*(unsigned char *)e)) { + *e-- = '\0'; + } +} + + +static pid_t +spawn_process(struct mg_connection *conn, + const char *prog, + char *envblk, + char *envp[], + int fdin[2], + int fdout[2], + int fderr[2], + const char *dir) +{ + HANDLE me; + char *p, *interp, full_interp[PATH_MAX], full_dir[PATH_MAX], + cmdline[PATH_MAX], buf[PATH_MAX]; + int truncated; + struct mg_file file = STRUCT_FILE_INITIALIZER; + STARTUPINFOA si; + PROCESS_INFORMATION pi = {0}; + + (void)envp; + + memset(&si, 0, sizeof(si)); + si.cb = sizeof(si); + + si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; + si.wShowWindow = SW_HIDE; + + me = GetCurrentProcess(); + DuplicateHandle(me, + (HANDLE)_get_osfhandle(fdin[0]), + me, + &si.hStdInput, + 0, + TRUE, + DUPLICATE_SAME_ACCESS); + DuplicateHandle(me, + (HANDLE)_get_osfhandle(fdout[1]), + me, + &si.hStdOutput, + 0, + TRUE, + DUPLICATE_SAME_ACCESS); + DuplicateHandle(me, + (HANDLE)_get_osfhandle(fderr[1]), + me, + &si.hStdError, + 0, + TRUE, + DUPLICATE_SAME_ACCESS); + + /* Mark handles that should not be inherited. See + * https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499%28v=vs.85%29.aspx + */ + SetHandleInformation((HANDLE)_get_osfhandle(fdin[1]), + HANDLE_FLAG_INHERIT, + 0); + SetHandleInformation((HANDLE)_get_osfhandle(fdout[0]), + HANDLE_FLAG_INHERIT, + 0); + SetHandleInformation((HANDLE)_get_osfhandle(fderr[0]), + HANDLE_FLAG_INHERIT, + 0); + + /* If CGI file is a script, try to read the interpreter line */ + interp = conn->ctx->config[CGI_INTERPRETER]; + if (interp == NULL) { + buf[0] = buf[1] = '\0'; + + /* Read the first line of the script into the buffer */ + mg_snprintf( + conn, &truncated, cmdline, sizeof(cmdline), "%s/%s", dir, prog); + + if (truncated) { + pi.hProcess = (pid_t)-1; + goto spawn_cleanup; + } + + if (mg_fopen(conn, cmdline, MG_FOPEN_MODE_READ, &file)) { + p = (char *)file.access.membuf; + mg_fgets(buf, sizeof(buf), &file, &p); + (void)mg_fclose(&file.access); /* ignore error on read only file */ + buf[sizeof(buf) - 1] = '\0'; + } + + if ((buf[0] == '#') && (buf[1] == '!')) { + trim_trailing_whitespaces(buf + 2); + } else { + buf[2] = '\0'; + } + interp = buf + 2; + } + + if (interp[0] != '\0') { + GetFullPathNameA(interp, sizeof(full_interp), full_interp, NULL); + interp = full_interp; + } + GetFullPathNameA(dir, sizeof(full_dir), full_dir, NULL); + + if (interp[0] != '\0') { + mg_snprintf(conn, + &truncated, + cmdline, + sizeof(cmdline), + "\"%s\" \"%s\\%s\"", + interp, + full_dir, + prog); + } else { + mg_snprintf(conn, + &truncated, + cmdline, + sizeof(cmdline), + "\"%s\\%s\"", + full_dir, + prog); + } + + if (truncated) { + pi.hProcess = (pid_t)-1; + goto spawn_cleanup; + } + + DEBUG_TRACE("Running [%s]", cmdline); + if (CreateProcessA(NULL, + cmdline, + NULL, + NULL, + TRUE, + CREATE_NEW_PROCESS_GROUP, + envblk, + NULL, + &si, + &pi) == 0) { + mg_cry( + conn, "%s: CreateProcess(%s): %ld", __func__, cmdline, (long)ERRNO); + pi.hProcess = (pid_t)-1; + /* goto spawn_cleanup; */ + } + +spawn_cleanup: + (void)CloseHandle(si.hStdOutput); + (void)CloseHandle(si.hStdError); + (void)CloseHandle(si.hStdInput); + if (pi.hThread != NULL) { + (void)CloseHandle(pi.hThread); + } + + return (pid_t)pi.hProcess; +} +#endif /* !NO_CGI */ + + +static int +set_blocking_mode(SOCKET sock) +{ + unsigned long non_blocking = 0; + return ioctlsocket(sock, (long)FIONBIO, &non_blocking); +} + +static int +set_non_blocking_mode(SOCKET sock) +{ + unsigned long non_blocking = 1; + return ioctlsocket(sock, (long)FIONBIO, &non_blocking); +} +#else + +static int +mg_stat(const struct mg_connection *conn, + const char *path, + struct mg_file_stat *filep) +{ + struct stat st; + if (!filep) { + return 0; + } + memset(filep, 0, sizeof(*filep)); + + if (conn && is_file_in_memory(conn, path)) { + + /* Quick fix (for 1.9.x): */ + /* mg_stat must fill all fields, also for files in memory */ + struct mg_file tmp_file = STRUCT_FILE_INITIALIZER; + open_file_in_memory(conn, path, &tmp_file, MG_FOPEN_MODE_NONE); + filep->size = tmp_file.stat.size; + filep->last_modified = time(NULL); + filep->location = 2; + /* TODO: for 1.10: restructure how files in memory are handled */ + + return 1; + } + + if (0 == stat(path, &st)) { + filep->size = (uint64_t)(st.st_size); + filep->last_modified = st.st_mtime; + filep->is_directory = S_ISDIR(st.st_mode); + return 1; + } + + return 0; +} + + +static void +set_close_on_exec(SOCKET fd, struct mg_connection *conn /* may be null */) +{ + if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) { + if (conn) { + mg_cry(conn, + "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s", + __func__, + strerror(ERRNO)); + } + } +} + + +int +mg_start_thread(mg_thread_func_t func, void *param) +{ + pthread_t thread_id; + pthread_attr_t attr; + int result; + + (void)pthread_attr_init(&attr); + (void)pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + +#if defined(USE_STACK_SIZE) && (USE_STACK_SIZE > 1) + /* Compile-time option to control stack size, + * e.g. -DUSE_STACK_SIZE=16384 */ + (void)pthread_attr_setstacksize(&attr, USE_STACK_SIZE); +#endif /* defined(USE_STACK_SIZE) && (USE_STACK_SIZE > 1) */ + + result = pthread_create(&thread_id, &attr, func, param); + pthread_attr_destroy(&attr); + + return result; +} + + +/* Start a thread storing the thread context. */ +static int +mg_start_thread_with_id(mg_thread_func_t func, + void *param, + pthread_t *threadidptr) +{ + pthread_t thread_id; + pthread_attr_t attr; + int result; + + (void)pthread_attr_init(&attr); + +#if defined(USE_STACK_SIZE) && (USE_STACK_SIZE > 1) + /* Compile-time option to control stack size, + * e.g. -DUSE_STACK_SIZE=16384 */ + (void)pthread_attr_setstacksize(&attr, USE_STACK_SIZE); +#endif /* defined(USE_STACK_SIZE) && USE_STACK_SIZE > 1 */ + + result = pthread_create(&thread_id, &attr, func, param); + pthread_attr_destroy(&attr); + if ((result == 0) && (threadidptr != NULL)) { + *threadidptr = thread_id; + } + return result; +} + + +/* Wait for a thread to finish. */ +static int +mg_join_thread(pthread_t threadid) +{ + int result; + + result = pthread_join(threadid, NULL); + return result; +} + + +#ifndef NO_CGI +static pid_t +spawn_process(struct mg_connection *conn, + const char *prog, + char *envblk, + char *envp[], + int fdin[2], + int fdout[2], + int fderr[2], + const char *dir) +{ + pid_t pid; + const char *interp; + + (void)envblk; + + if (conn == NULL) { + return 0; + } + + if ((pid = fork()) == -1) { + /* Parent */ + mg_send_http_error(conn, + 500, + "Error: Creating CGI process\nfork(): %s", + strerror(ERRNO)); + } else if (pid == 0) { + /* Child */ + if (chdir(dir) != 0) { + mg_cry(conn, "%s: chdir(%s): %s", __func__, dir, strerror(ERRNO)); + } else if (dup2(fdin[0], 0) == -1) { + mg_cry(conn, + "%s: dup2(%d, 0): %s", + __func__, + fdin[0], + strerror(ERRNO)); + } else if (dup2(fdout[1], 1) == -1) { + mg_cry(conn, + "%s: dup2(%d, 1): %s", + __func__, + fdout[1], + strerror(ERRNO)); + } else if (dup2(fderr[1], 2) == -1) { + mg_cry(conn, + "%s: dup2(%d, 2): %s", + __func__, + fderr[1], + strerror(ERRNO)); + } else { + /* Keep stderr and stdout in two different pipes. + * Stdout will be sent back to the client, + * stderr should go into a server error log. */ + (void)close(fdin[0]); + (void)close(fdout[1]); + (void)close(fderr[1]); + + /* Close write end fdin and read end fdout and fderr */ + (void)close(fdin[1]); + (void)close(fdout[0]); + (void)close(fderr[0]); + + /* After exec, all signal handlers are restored to their default + * values, with one exception of SIGCHLD. According to + * POSIX.1-2001 and Linux's implementation, SIGCHLD's handler will + * leave unchanged after exec if it was set to be ignored. Restore + * it to default action. */ + signal(SIGCHLD, SIG_DFL); + + interp = conn->ctx->config[CGI_INTERPRETER]; + if (interp == NULL) { + (void)execle(prog, prog, NULL, envp); + mg_cry(conn, + "%s: execle(%s): %s", + __func__, + prog, + strerror(ERRNO)); + } else { + (void)execle(interp, interp, prog, NULL, envp); + mg_cry(conn, + "%s: execle(%s %s): %s", + __func__, + interp, + prog, + strerror(ERRNO)); + } + } + exit(EXIT_FAILURE); + } + + return pid; +} +#endif /* !NO_CGI */ + + +static int +set_non_blocking_mode(SOCKET sock) +{ + int flags = fcntl(sock, F_GETFL, 0); + if (flags < 0) { + return -1; + } + + if (fcntl(sock, F_SETFL, (flags | O_NONBLOCK)) < 0) { + return -1; + } + return 0; +} + +static int +set_blocking_mode(SOCKET sock) +{ + int flags = fcntl(sock, F_GETFL, 0); + if (flags < 0) { + return -1; + } + + if (fcntl(sock, F_SETFL, flags & (~(int)(O_NONBLOCK))) < 0) { + return -1; + } + return 0; +} +#endif /* _WIN32 / else */ + +/* End of initial operating system specific define block. */ + + +/* Get a random number (independent of C rand function) */ +static uint64_t +get_random(void) +{ + static uint64_t lfsr = 0; /* Linear feedback shift register */ + static uint64_t lcg = 0; /* Linear congruential generator */ + uint64_t now = mg_get_current_time_ns(); + + if (lfsr == 0) { + /* lfsr will be only 0 if has not been initialized, + * so this code is called only once. */ + lfsr = mg_get_current_time_ns(); + lcg = mg_get_current_time_ns(); + } else { + /* Get the next step of both random number generators. */ + lfsr = (lfsr >> 1) + | ((((lfsr >> 0) ^ (lfsr >> 1) ^ (lfsr >> 3) ^ (lfsr >> 4)) & 1) + << 63); + lcg = lcg * 6364136223846793005LL + 1442695040888963407LL; + } + + /* Combining two pseudo-random number generators and a high resolution + * part + * of the current server time will make it hard (impossible?) to guess + * the + * next number. */ + return (lfsr ^ lcg ^ now); +} + + +static int +mg_poll(struct pollfd *pfd, + unsigned int n, + int milliseconds, + volatile int *stop_server) +{ + /* Call poll, but only for a maximum time of a few seconds. + * This will allow to stop the server after some seconds, instead + * of having to wait for a long socket timeout. */ + int ms_now = SOCKET_TIMEOUT_QUANTUM; /* Sleep quantum in ms */ + + do { + int result; + + if (*stop_server) { + /* Shut down signal */ + return -2; + } + + if ((milliseconds >= 0) && (milliseconds < ms_now)) { + ms_now = milliseconds; + } + + result = poll(pfd, n, ms_now); + if (result != 0) { + /* Poll returned either success (1) or error (-1). + * Forward both to the caller. */ + return result; + } + + /* Poll returned timeout (0). */ + if (milliseconds > 0) { + milliseconds -= ms_now; + } + + } while (milliseconds != 0); + + /* timeout: return 0 */ + return 0; +} + + +/* Write data to the IO channel - opened file descriptor, socket or SSL + * descriptor. + * Return value: + * >=0 .. number of bytes successfully written + * -1 .. timeout + * -2 .. error + */ +static int +push_inner(struct mg_context *ctx, + FILE *fp, + SOCKET sock, + SSL *ssl, + const char *buf, + int len, + double timeout) +{ + uint64_t start = 0, now = 0, timeout_ns = 0; + int n, err; + unsigned ms_wait = SOCKET_TIMEOUT_QUANTUM; /* Sleep quantum in ms */ + +#ifdef _WIN32 + typedef int len_t; +#else + typedef size_t len_t; +#endif + + if (timeout > 0) { + now = mg_get_current_time_ns(); + start = now; + timeout_ns = (uint64_t)(timeout * 1.0E9); + } + + if (ctx == NULL) { + return -2; + } + +#ifdef NO_SSL + if (ssl) { + return -2; + } +#endif + + /* Try to read until it succeeds, fails, times out, or the server + * shuts down. */ + for (;;) { + +#ifndef NO_SSL + if (ssl != NULL) { + n = SSL_write(ssl, buf, len); + if (n <= 0) { + err = SSL_get_error(ssl, n); + if ((err == SSL_ERROR_SYSCALL) && (n == -1)) { + err = ERRNO; + } else if ((err == SSL_ERROR_WANT_READ) + || (err == SSL_ERROR_WANT_WRITE)) { + n = 0; + } else { + DEBUG_TRACE("SSL_write() failed, error %d", err); + return -2; + } + } else { + err = 0; + } + } else +#endif + if (fp != NULL) { + n = (int)fwrite(buf, 1, (size_t)len, fp); + if (ferror(fp)) { + n = -1; + err = ERRNO; + } else { + err = 0; + } + } else { + n = (int)send(sock, buf, (len_t)len, MSG_NOSIGNAL); + err = (n < 0) ? ERRNO : 0; +#ifdef _WIN32 + if (err == WSAEWOULDBLOCK) { + err = 0; + n = 0; + } +#else + if (err == EWOULDBLOCK) { + err = 0; + n = 0; + } +#endif + if (n < 0) { + /* shutdown of the socket at client side */ + return -2; + } + } + + if (ctx->stop_flag) { + return -2; + } + + if ((n > 0) || ((n == 0) && (len == 0))) { + /* some data has been read, or no data was requested */ + return n; + } + if (n < 0) { + /* socket error - check errno */ + DEBUG_TRACE("send() failed, error %d", err); + + /* TODO (mid): error handling depending on the error code. + * These codes are different between Windows and Linux. + * Currently there is no problem with failing send calls, + * if there is a reproducible situation, it should be + * investigated in detail. + */ + return -2; + } + + /* Only in case n=0 (timeout), repeat calling the write function */ + + /* If send failed, wait before retry */ + if (fp != NULL) { + /* For files, just wait a fixed time, + * maybe an average disk seek time. */ + mg_sleep(ms_wait > 10 ? 10 : ms_wait); + } else { + /* For sockets, wait for the socket using poll */ + struct pollfd pfd[1]; + int pollres; + + pfd[0].fd = sock; + pfd[0].events = POLLOUT; + pollres = mg_poll(pfd, 1, (int)(ms_wait), &(ctx->stop_flag)); + if (ctx->stop_flag) { + return -2; + } + if (pollres > 0) { + continue; + } + } + + if (timeout > 0) { + now = mg_get_current_time_ns(); + if ((now - start) > timeout_ns) { + /* Timeout */ + break; + } + } + } + + (void)err; /* Avoid unused warning if NO_SSL is set and DEBUG_TRACE is not + used */ + + return -1; +} + + +static int64_t +push_all(struct mg_context *ctx, + FILE *fp, + SOCKET sock, + SSL *ssl, + const char *buf, + int64_t len) +{ + double timeout = -1.0; + int64_t n, nwritten = 0; + + if (ctx == NULL) { + return -1; + } + + if (ctx->config[REQUEST_TIMEOUT]) { + timeout = atoi(ctx->config[REQUEST_TIMEOUT]) / 1000.0; + } + + while ((len > 0) && (ctx->stop_flag == 0)) { + n = push_inner(ctx, fp, sock, ssl, buf + nwritten, (int)len, timeout); + if (n < 0) { + if (nwritten == 0) { + nwritten = n; /* Propagate the error */ + } + break; + } else if (n == 0) { + break; /* No more data to write */ + } else { + nwritten += n; + len -= n; + } + } + + return nwritten; +} + + +/* Read from IO channel - opened file descriptor, socket, or SSL descriptor. + * Return value: + * >=0 .. number of bytes successfully read + * -1 .. timeout + * -2 .. error + */ +static int +pull_inner(FILE *fp, + struct mg_connection *conn, + char *buf, + int len, + double timeout) +{ + int nread, err = 0; + +#ifdef _WIN32 + typedef int len_t; +#else + typedef size_t len_t; +#endif +#ifndef NO_SSL + int ssl_pending; +#endif + + /* We need an additional wait loop around this, because in some cases + * with TLSwe may get data from the socket but not from SSL_read. + * In this case we need to repeat at least once. + */ + + if (fp != NULL) { +#if !defined(_WIN32_WCE) + /* Use read() instead of fread(), because if we're reading from the + * CGI pipe, fread() may block until IO buffer is filled up. We + * cannot afford to block and must pass all read bytes immediately + * to the client. */ + nread = (int)read(fileno(fp), buf, (size_t)len); +#else + /* WinCE does not support CGI pipes */ + nread = (int)fread(buf, 1, (size_t)len, fp); +#endif + err = (nread < 0) ? ERRNO : 0; + if ((nread == 0) && (len > 0)) { + /* Should get data, but got EOL */ + return -2; + } + +#ifndef NO_SSL + } else if ((conn->ssl != NULL) + && ((ssl_pending = SSL_pending(conn->ssl)) > 0)) { + /* We already know there is no more data buffered in conn->buf + * but there is more available in the SSL layer. So don't poll + * conn->client.sock yet. */ + if (ssl_pending > len) { + ssl_pending = len; + } + nread = SSL_read(conn->ssl, buf, ssl_pending); + if (nread <= 0) { + err = SSL_get_error(conn->ssl, nread); + if ((err == SSL_ERROR_SYSCALL) && (nread == -1)) { + err = ERRNO; + } else if ((err == SSL_ERROR_WANT_READ) + || (err == SSL_ERROR_WANT_WRITE)) { + nread = 0; + } else { + DEBUG_TRACE("SSL_read() failed, error %d", err); + return -1; + } + } else { + err = 0; + } + + } else if (conn->ssl != NULL) { + + struct pollfd pfd[1]; + int pollres; + + pfd[0].fd = conn->client.sock; + pfd[0].events = POLLIN; + pollres = + mg_poll(pfd, 1, (int)(timeout * 1000.0), &(conn->ctx->stop_flag)); + if (conn->ctx->stop_flag) { + return -2; + } + if (pollres > 0) { + nread = SSL_read(conn->ssl, buf, len); + if (nread <= 0) { + err = SSL_get_error(conn->ssl, nread); + if ((err == SSL_ERROR_SYSCALL) && (nread == -1)) { + err = ERRNO; + } else if ((err == SSL_ERROR_WANT_READ) + || (err == SSL_ERROR_WANT_WRITE)) { + nread = 0; + } else { + DEBUG_TRACE("SSL_read() failed, error %d", err); + return -2; + } + } else { + err = 0; + } + + } else if (pollres < 0) { + /* Error */ + return -2; + } else { + /* pollres = 0 means timeout */ + nread = 0; + } +#endif + + } else { + struct pollfd pfd[1]; + int pollres; + + pfd[0].fd = conn->client.sock; + pfd[0].events = POLLIN; + pollres = + mg_poll(pfd, 1, (int)(timeout * 1000.0), &(conn->ctx->stop_flag)); + if (conn->ctx->stop_flag) { + return -2; + } + if (pollres > 0) { + nread = (int)recv(conn->client.sock, buf, (len_t)len, 0); + err = (nread < 0) ? ERRNO : 0; + if (nread <= 0) { + /* shutdown of the socket at client side */ + return -2; + } + } else if (pollres < 0) { + /* error callint poll */ + return -2; + } else { + /* pollres = 0 means timeout */ + nread = 0; + } + } + + if (conn->ctx->stop_flag) { + return -2; + } + + if ((nread > 0) || ((nread == 0) && (len == 0))) { + /* some data has been read, or no data was requested */ + return nread; + } + + if (nread < 0) { +/* socket error - check errno */ +#ifdef _WIN32 + if (err == WSAEWOULDBLOCK) { + /* TODO (low): check if this is still required */ + /* standard case if called from close_socket_gracefully */ + return -2; + } else if (err == WSAETIMEDOUT) { + /* TODO (low): check if this is still required */ + /* timeout is handled by the while loop */ + return 0; + } else if (err == WSAECONNABORTED) { + /* See https://www.chilkatsoft.com/p/p_299.asp */ + return -2; + } else { + DEBUG_TRACE("recv() failed, error %d", err); + return -2; + } +#else + /* TODO: POSIX returns either EAGAIN or EWOULDBLOCK in both cases, + * if the timeout is reached and if the socket was set to non- + * blocking in close_socket_gracefully, so we can not distinguish + * here. We have to wait for the timeout in both cases for now. + */ + if ((err == EAGAIN) || (err == EWOULDBLOCK) || (err == EINTR)) { + /* TODO (low): check if this is still required */ + /* EAGAIN/EWOULDBLOCK: + * standard case if called from close_socket_gracefully + * => should return -1 */ + /* or timeout occured + * => the code must stay in the while loop */ + + /* EINTR can be generated on a socket with a timeout set even + * when SA_RESTART is effective for all relevant signals + * (see signal(7)). + * => stay in the while loop */ + } else { + DEBUG_TRACE("recv() failed, error %d", err); + return -2; + } +#endif + } + + /* Timeout occured, but no data available. */ + return -1; +} + + +static int +pull_all(FILE *fp, struct mg_connection *conn, char *buf, int len) +{ + int n, nread = 0; + double timeout = -1.0; + uint64_t start_time = 0, now = 0, timeout_ns = 0; + + if (conn->ctx->config[REQUEST_TIMEOUT]) { + timeout = atoi(conn->ctx->config[REQUEST_TIMEOUT]) / 1000.0; + } + if (timeout >= 0.0) { + start_time = mg_get_current_time_ns(); + timeout_ns = (uint64_t)(timeout * 1.0E9); + } + + while ((len > 0) && (conn->ctx->stop_flag == 0)) { + n = pull_inner(fp, conn, buf + nread, len, timeout); + if (n == -2) { + if (nread == 0) { + nread = -1; /* Propagate the error */ + } + break; + } else if (n == -1) { + /* timeout */ + if (timeout >= 0.0) { + now = mg_get_current_time_ns(); + if ((now - start_time) <= timeout_ns) { + continue; + } + } + break; + } else if (n == 0) { + break; /* No more data to read */ + } else { + conn->consumed_content += n; + nread += n; + len -= n; + } + } + + return nread; +} + + +static void +discard_unread_request_data(struct mg_connection *conn) +{ + char buf[MG_BUF_LEN]; + size_t to_read; + int nread; + + if (conn == NULL) { + return; + } + + to_read = sizeof(buf); + + if (conn->is_chunked) { + /* Chunked encoding: 3=chunk read completely + * completely */ + while (conn->is_chunked != 3) { + nread = mg_read(conn, buf, to_read); + if (nread <= 0) { + break; + } + } + + } else { + /* Not chunked: content length is known */ + while (conn->consumed_content < conn->content_len) { + if (to_read + > (size_t)(conn->content_len - conn->consumed_content)) { + to_read = (size_t)(conn->content_len - conn->consumed_content); + } + + nread = mg_read(conn, buf, to_read); + if (nread <= 0) { + break; + } + } + } +} + + +static int +mg_read_inner(struct mg_connection *conn, void *buf, size_t len) +{ + int64_t n, buffered_len, nread; + int64_t len64 = + (int64_t)((len > INT_MAX) ? INT_MAX : len); /* since the return value is + * int, we may not read more + * bytes */ + const char *body; + + if (conn == NULL) { + return 0; + } + + /* If Content-Length is not set for a request with body data + * (e.g., a PUT or POST request), we do not know in advance + * how much data should be read. */ + if (conn->consumed_content == 0) { + if (conn->is_chunked == 1) { + conn->content_len = len64; + conn->is_chunked = 2; + } else if (conn->content_len == -1) { + /* The body data is completed when the connection + * is closed. */ + conn->content_len = INT64_MAX; + conn->must_close = 1; + } + } + + nread = 0; + if (conn->consumed_content < conn->content_len) { + /* Adjust number of bytes to read. */ + int64_t left_to_read = conn->content_len - conn->consumed_content; + if (left_to_read < len64) { + /* Do not read more than the total content length of the + * request. + */ + len64 = left_to_read; + } + + /* Return buffered data */ + buffered_len = (int64_t)(conn->data_len) - (int64_t)conn->request_len + - conn->consumed_content; + if (buffered_len > 0) { + if (len64 < buffered_len) { + buffered_len = len64; + } + body = conn->buf + conn->request_len + conn->consumed_content; + memcpy(buf, body, (size_t)buffered_len); + len64 -= buffered_len; + conn->consumed_content += buffered_len; + nread += buffered_len; + buf = (char *)buf + buffered_len; + } + + /* We have returned all buffered data. Read new data from the remote + * socket. + */ + if ((n = pull_all(NULL, conn, (char *)buf, (int)len64)) >= 0) { + nread += n; + } else { + nread = ((nread > 0) ? nread : n); + } + } + return (int)nread; +} + + +static char +mg_getc(struct mg_connection *conn) +{ + char c; + if (conn == NULL) { + return 0; + } + if (mg_read_inner(conn, &c, 1) <= 0) { + return (char)0; + } + return c; +} + + +int +mg_read(struct mg_connection *conn, void *buf, size_t len) +{ + if (len > INT_MAX) { + len = INT_MAX; + } + + if (conn == NULL) { + return 0; + } + + if (conn->is_chunked) { + size_t all_read = 0; + + while (len > 0) { + if (conn->is_chunked == 3) { + /* No more data left to read */ + return 0; + } + + if (conn->chunk_remainder) { + /* copy from the remainder of the last received chunk */ + long read_ret; + size_t read_now = + ((conn->chunk_remainder > len) ? (len) + : (conn->chunk_remainder)); + + conn->content_len += (int)read_now; + read_ret = + mg_read_inner(conn, (char *)buf + all_read, read_now); + + if (read_ret < 1) { + /* read error */ + return -1; + } + + all_read += (size_t)read_ret; + conn->chunk_remainder -= (size_t)read_ret; + len -= (size_t)read_ret; + + if (conn->chunk_remainder == 0) { + /* Add data bytes in the current chunk have been read, + * so we are expecting \r\n now. */ + char x1, x2; + conn->content_len += 2; + x1 = mg_getc(conn); + x2 = mg_getc(conn); + if ((x1 != '\r') || (x2 != '\n')) { + /* Protocol violation */ + return -1; + } + } + + } else { + /* fetch a new chunk */ + int i = 0; + char lenbuf[64]; + char *end = 0; + unsigned long chunkSize = 0; + + for (i = 0; i < ((int)sizeof(lenbuf) - 1); i++) { + conn->content_len++; + lenbuf[i] = mg_getc(conn); + if ((i > 0) && (lenbuf[i] == '\r') + && (lenbuf[i - 1] != '\r')) { + continue; + } + if ((i > 1) && (lenbuf[i] == '\n') + && (lenbuf[i - 1] == '\r')) { + lenbuf[i + 1] = 0; + chunkSize = strtoul(lenbuf, &end, 16); + if (chunkSize == 0) { + /* regular end of content */ + conn->is_chunked = 3; + } + break; + } + if (!isxdigit(lenbuf[i])) { + /* illegal character for chunk length */ + return -1; + } + } + if ((end == NULL) || (*end != '\r')) { + /* chunksize not set correctly */ + return -1; + } + if (chunkSize == 0) { + break; + } + + conn->chunk_remainder = chunkSize; + } + } + + return (int)all_read; + } + return mg_read_inner(conn, buf, len); +} + + +int +mg_write(struct mg_connection *conn, const void *buf, size_t len) +{ + time_t now; + int64_t n, total, allowed; + + if (conn == NULL) { + return 0; + } + + if (conn->throttle > 0) { + if ((now = time(NULL)) != conn->last_throttle_time) { + conn->last_throttle_time = now; + conn->last_throttle_bytes = 0; + } + allowed = conn->throttle - conn->last_throttle_bytes; + if (allowed > (int64_t)len) { + allowed = (int64_t)len; + } + if ((total = push_all(conn->ctx, + NULL, + conn->client.sock, + conn->ssl, + (const char *)buf, + (int64_t)allowed)) == allowed) { + buf = (const char *)buf + total; + conn->last_throttle_bytes += total; + while ((total < (int64_t)len) && (conn->ctx->stop_flag == 0)) { + allowed = (conn->throttle > ((int64_t)len - total)) + ? (int64_t)len - total + : conn->throttle; + if ((n = push_all(conn->ctx, + NULL, + conn->client.sock, + conn->ssl, + (const char *)buf, + (int64_t)allowed)) != allowed) { + break; + } + sleep(1); + conn->last_throttle_bytes = allowed; + conn->last_throttle_time = time(NULL); + buf = (const char *)buf + n; + total += n; + } + } + } else { + total = push_all(conn->ctx, + NULL, + conn->client.sock, + conn->ssl, + (const char *)buf, + (int64_t)len); + } + if (total > 0) { + conn->num_bytes_sent += total; + } + return (int)total; +} + + +/* Send a chunk, if "Transfer-Encoding: chunked" is used */ +int +mg_send_chunk(struct mg_connection *conn, + const char *chunk, + unsigned int chunk_len) +{ + char lenbuf[16]; + size_t lenbuf_len; + int ret; + int t; + + /* First store the length information in a text buffer. */ + sprintf(lenbuf, "%x\r\n", chunk_len); + lenbuf_len = strlen(lenbuf); + + /* Then send length information, chunk and terminating \r\n. */ + ret = mg_write(conn, lenbuf, lenbuf_len); + if (ret != (int)lenbuf_len) { + return -1; + } + t = ret; + + ret = mg_write(conn, chunk, chunk_len); + if (ret != (int)chunk_len) { + return -1; + } + t += ret; + + ret = mg_write(conn, "\r\n", 2); + if (ret != 2) { + return -1; + } + t += ret; + + return t; +} + + +/* Alternative alloc_vprintf() for non-compliant C runtimes */ +static int +alloc_vprintf2(char **buf, const char *fmt, va_list ap) +{ + va_list ap_copy; + size_t size = MG_BUF_LEN / 4; + int len = -1; + + *buf = NULL; + while (len < 0) { + if (*buf) { + mg_free(*buf); + } + + size *= 4; + *buf = (char *)mg_malloc(size); + if (!*buf) { + break; + } + + va_copy(ap_copy, ap); + len = vsnprintf_impl(*buf, size - 1, fmt, ap_copy); + va_end(ap_copy); + (*buf)[size - 1] = 0; + } + + return len; +} + + +/* Print message to buffer. If buffer is large enough to hold the message, + * return buffer. If buffer is to small, allocate large enough buffer on + * heap, + * and return allocated buffer. */ +static int +alloc_vprintf(char **out_buf, + char *prealloc_buf, + size_t prealloc_size, + const char *fmt, + va_list ap) +{ + va_list ap_copy; + int len; + + /* Windows is not standard-compliant, and vsnprintf() returns -1 if + * buffer is too small. Also, older versions of msvcrt.dll do not have + * _vscprintf(). However, if size is 0, vsnprintf() behaves correctly. + * Therefore, we make two passes: on first pass, get required message + * length. + * On second pass, actually print the message. */ + va_copy(ap_copy, ap); + len = vsnprintf_impl(NULL, 0, fmt, ap_copy); + va_end(ap_copy); + + if (len < 0) { + /* C runtime is not standard compliant, vsnprintf() returned -1. + * Switch to alternative code path that uses incremental + * allocations. + */ + va_copy(ap_copy, ap); + len = alloc_vprintf2(out_buf, fmt, ap_copy); + va_end(ap_copy); + + } else if ((size_t)(len) >= prealloc_size) { + /* The pre-allocated buffer not large enough. */ + /* Allocate a new buffer. */ + *out_buf = (char *)mg_malloc((size_t)(len) + 1); + if (!*out_buf) { + /* Allocation failed. Return -1 as "out of memory" error. */ + return -1; + } + /* Buffer allocation successful. Store the string there. */ + va_copy(ap_copy, ap); + IGNORE_UNUSED_RESULT( + vsnprintf_impl(*out_buf, (size_t)(len) + 1, fmt, ap_copy)); + va_end(ap_copy); + + } else { + /* The pre-allocated buffer is large enough. + * Use it to store the string and return the address. */ + va_copy(ap_copy, ap); + IGNORE_UNUSED_RESULT( + vsnprintf_impl(prealloc_buf, prealloc_size, fmt, ap_copy)); + va_end(ap_copy); + *out_buf = prealloc_buf; + } + + return len; +} + + +static int +mg_vprintf(struct mg_connection *conn, const char *fmt, va_list ap) +{ + char mem[MG_BUF_LEN]; + char *buf = NULL; + int len; + + if ((len = alloc_vprintf(&buf, mem, sizeof(mem), fmt, ap)) > 0) { + len = mg_write(conn, buf, (size_t)len); + } + if ((buf != mem) && (buf != NULL)) { + mg_free(buf); + } + + return len; +} + + +int +mg_printf(struct mg_connection *conn, const char *fmt, ...) +{ + va_list ap; + int result; + + va_start(ap, fmt); + result = mg_vprintf(conn, fmt, ap); + va_end(ap); + + return result; +} + + +int +mg_url_decode(const char *src, + int src_len, + char *dst, + int dst_len, + int is_form_url_encoded) +{ + int i, j, a, b; +#define HEXTOI(x) (isdigit(x) ? (x - '0') : (x - 'W')) + + for (i = j = 0; (i < src_len) && (j < (dst_len - 1)); i++, j++) { + if ((i < src_len - 2) && (src[i] == '%') + && isxdigit(*(const unsigned char *)(src + i + 1)) + && isxdigit(*(const unsigned char *)(src + i + 2))) { + a = tolower(*(const unsigned char *)(src + i + 1)); + b = tolower(*(const unsigned char *)(src + i + 2)); + dst[j] = (char)((HEXTOI(a) << 4) | HEXTOI(b)); + i += 2; + } else if (is_form_url_encoded && (src[i] == '+')) { + dst[j] = ' '; + } else { + dst[j] = src[i]; + } + } + + dst[j] = '\0'; /* Null-terminate the destination */ + + return (i >= src_len) ? j : -1; +} + + +int +mg_get_var(const char *data, + size_t data_len, + const char *name, + char *dst, + size_t dst_len) +{ + return mg_get_var2(data, data_len, name, dst, dst_len, 0); +} + + +int +mg_get_var2(const char *data, + size_t data_len, + const char *name, + char *dst, + size_t dst_len, + size_t occurrence) +{ + const char *p, *e, *s; + size_t name_len; + int len; + + if ((dst == NULL) || (dst_len == 0)) { + len = -2; + } else if ((data == NULL) || (name == NULL) || (data_len == 0)) { + len = -1; + dst[0] = '\0'; + } else { + name_len = strlen(name); + e = data + data_len; + len = -1; + dst[0] = '\0'; + + /* data is "var1=val1&var2=val2...". Find variable first */ + for (p = data; p + name_len < e; p++) { + if (((p == data) || (p[-1] == '&')) && (p[name_len] == '=') + && !mg_strncasecmp(name, p, name_len) && 0 == occurrence--) { + /* Point p to variable value */ + p += name_len + 1; + + /* Point s to the end of the value */ + s = (const char *)memchr(p, '&', (size_t)(e - p)); + if (s == NULL) { + s = e; + } + /* assert(s >= p); */ + if (s < p) { + return -3; + } + + /* Decode variable into destination buffer */ + len = mg_url_decode(p, (int)(s - p), dst, (int)dst_len, 1); + + /* Redirect error code from -1 to -2 (destination buffer too + * small). */ + if (len == -1) { + len = -2; + } + break; + } + } + } + + return len; +} + + +/* HCP24: some changes to compare hole var_name */ +int +mg_get_cookie(const char *cookie_header, + const char *var_name, + char *dst, + size_t dst_size) +{ + const char *s, *p, *end; + int name_len, len = -1; + + if ((dst == NULL) || (dst_size == 0)) { + return -2; + } + + dst[0] = '\0'; + if ((var_name == NULL) || ((s = cookie_header) == NULL)) { + return -1; + } + + name_len = (int)strlen(var_name); + end = s + strlen(s); + for (; (s = mg_strcasestr(s, var_name)) != NULL; s += name_len) { + if (s[name_len] == '=') { + /* HCP24: now check is it a substring or a full cookie name */ + if ((s == cookie_header) || (s[-1] == ' ')) { + s += name_len + 1; + if ((p = strchr(s, ' ')) == NULL) { + p = end; + } + if (p[-1] == ';') { + p--; + } + if ((*s == '"') && (p[-1] == '"') && (p > s + 1)) { + s++; + p--; + } + if ((size_t)(p - s) < dst_size) { + len = (int)(p - s); + mg_strlcpy(dst, s, (size_t)len + 1); + } else { + len = -3; + } + break; + } + } + } + return len; +} + + +#if defined(USE_WEBSOCKET) || defined(USE_LUA) +static void +base64_encode(const unsigned char *src, int src_len, char *dst) +{ + static const char *b64 = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + int i, j, a, b, c; + + for (i = j = 0; i < src_len; i += 3) { + a = src[i]; + b = ((i + 1) >= src_len) ? 0 : src[i + 1]; + c = ((i + 2) >= src_len) ? 0 : src[i + 2]; + + dst[j++] = b64[a >> 2]; + dst[j++] = b64[((a & 3) << 4) | (b >> 4)]; + if (i + 1 < src_len) { + dst[j++] = b64[(b & 15) << 2 | (c >> 6)]; + } + if (i + 2 < src_len) { + dst[j++] = b64[c & 63]; + } + } + while (j % 4 != 0) { + dst[j++] = '='; + } + dst[j++] = '\0'; +} +#endif + + +#if defined(USE_LUA) +static unsigned char +b64reverse(char letter) +{ + if ((letter >= 'A') && (letter <= 'Z')) { + return letter - 'A'; + } + if ((letter >= 'a') && (letter <= 'z')) { + return letter - 'a' + 26; + } + if ((letter >= '0') && (letter <= '9')) { + return letter - '0' + 52; + } + if (letter == '+') { + return 62; + } + if (letter == '/') { + return 63; + } + if (letter == '=') { + return 255; /* normal end */ + } + return 254; /* error */ +} + + +static int +base64_decode(const unsigned char *src, int src_len, char *dst, size_t *dst_len) +{ + int i; + unsigned char a, b, c, d; + + *dst_len = 0; + + for (i = 0; i < src_len; i += 4) { + a = b64reverse(src[i]); + if (a >= 254) { + return i; + } + + b = b64reverse(((i + 1) >= src_len) ? 0 : src[i + 1]); + if (b >= 254) { + return i + 1; + } + + c = b64reverse(((i + 2) >= src_len) ? 0 : src[i + 2]); + if (c == 254) { + return i + 2; + } + + d = b64reverse(((i + 3) >= src_len) ? 0 : src[i + 3]); + if (d == 254) { + return i + 3; + } + + dst[(*dst_len)++] = (a << 2) + (b >> 4); + if (c != 255) { + dst[(*dst_len)++] = (b << 4) + (c >> 2); + if (d != 255) { + dst[(*dst_len)++] = (c << 6) + d; + } + } + } + return -1; +} +#endif + + +static int +is_put_or_delete_method(const struct mg_connection *conn) +{ + if (conn) { + const char *s = conn->request_info.request_method; + return (s != NULL) && (!strcmp(s, "PUT") || !strcmp(s, "DELETE") + || !strcmp(s, "MKCOL") || !strcmp(s, "PATCH")); + } + return 0; +} + + +#if !defined(NO_FILES) +static int +extention_matches_script( + struct mg_connection *conn, /* in: request (must be valid) */ + const char *filename /* in: filename (must be valid) */ + ) +{ +#if !defined(NO_CGI) + if (match_prefix(conn->ctx->config[CGI_EXTENSIONS], + strlen(conn->ctx->config[CGI_EXTENSIONS]), + filename) > 0) { + return 1; + } +#endif +#if defined(USE_LUA) + if (match_prefix(conn->ctx->config[LUA_SCRIPT_EXTENSIONS], + strlen(conn->ctx->config[LUA_SCRIPT_EXTENSIONS]), + filename) > 0) { + return 1; + } +#endif +#if defined(USE_DUKTAPE) + if (match_prefix(conn->ctx->config[DUKTAPE_SCRIPT_EXTENSIONS], + strlen(conn->ctx->config[DUKTAPE_SCRIPT_EXTENSIONS]), + filename) > 0) { + return 1; + } +#endif + /* filename and conn could be unused, if all preocessor conditions + * are false (no script language supported). */ + (void)filename; + (void)conn; + + return 0; +} + + +/* For given directory path, substitute it to valid index file. + * Return 1 if index file has been found, 0 if not found. + * If the file is found, it's stats is returned in stp. */ +static int +substitute_index_file(struct mg_connection *conn, + char *path, + size_t path_len, + struct mg_file_stat *filestat) +{ + const char *list = conn->ctx->config[INDEX_FILES]; + struct vec filename_vec; + size_t n = strlen(path); + int found = 0; + + /* The 'path' given to us points to the directory. Remove all trailing + * directory separator characters from the end of the path, and + * then append single directory separator character. */ + while ((n > 0) && (path[n - 1] == '/')) { + n--; + } + path[n] = '/'; + + /* Traverse index files list. For each entry, append it to the given + * path and see if the file exists. If it exists, break the loop */ + while ((list = next_option(list, &filename_vec, NULL)) != NULL) { + /* Ignore too long entries that may overflow path buffer */ + if (filename_vec.len > (path_len - (n + 2))) { + continue; + } + + /* Prepare full path to the index file */ + mg_strlcpy(path + n + 1, filename_vec.ptr, filename_vec.len + 1); + + /* Does it exist? */ + if (mg_stat(conn, path, filestat)) { + /* Yes it does, break the loop */ + found = 1; + break; + } + } + + /* If no index file exists, restore directory path */ + if (!found) { + path[n] = '\0'; + } + + return found; +} +#endif + + +static void +interpret_uri(struct mg_connection *conn, /* in/out: request (must be valid) */ + char *filename, /* out: filename */ + size_t filename_buf_len, /* in: size of filename buffer */ + struct mg_file_stat *filestat, /* out: file status structure */ + int *is_found, /* out: file found (directly) */ + int *is_script_resource, /* out: handled by a script? */ + int *is_websocket_request, /* out: websocket connetion? */ + int *is_put_or_delete_request /* out: put/delete a file? */ + ) +{ + char const *accept_encoding; + +#if !defined(NO_FILES) + const char *uri = conn->request_info.local_uri; + const char *root = conn->ctx->config[DOCUMENT_ROOT]; + const char *rewrite; + struct vec a, b; + int match_len; + char gz_path[PATH_MAX]; + int truncated; +#if !defined(NO_CGI) || defined(USE_LUA) || defined(USE_DUKTAPE) + char *tmp_str; + size_t tmp_str_len, sep_pos; + int allow_substitute_script_subresources; +#endif +#else + (void)filename_buf_len; /* unused if NO_FILES is defined */ +#endif + + /* Step 1: Set all initially unknown outputs to zero */ + memset(filestat, 0, sizeof(*filestat)); + *filename = 0; + *is_found = 0; + *is_script_resource = 0; + + /* Step 2: Check if the request attempts to modify the file system */ + *is_put_or_delete_request = is_put_or_delete_method(conn); + +/* Step 3: Check if it is a websocket request, and modify the document + * root if required */ +#if defined(USE_WEBSOCKET) + *is_websocket_request = is_websocket_protocol(conn); +#if !defined(NO_FILES) + if (*is_websocket_request && conn->ctx->config[WEBSOCKET_ROOT]) { + root = conn->ctx->config[WEBSOCKET_ROOT]; + } +#endif /* !NO_FILES */ +#else /* USE_WEBSOCKET */ + *is_websocket_request = 0; +#endif /* USE_WEBSOCKET */ + + /* Step 4: Check if gzip encoded response is allowed */ + conn->accept_gzip = 0; + if ((accept_encoding = mg_get_header(conn, "Accept-Encoding")) != NULL) { + if (strstr(accept_encoding, "gzip") != NULL) { + conn->accept_gzip = 1; + } + } + +#if !defined(NO_FILES) + /* Step 5: If there is no root directory, don't look for files. */ + /* Note that root == NULL is a regular use case here. This occurs, + * if all requests are handled by callbacks, so the WEBSOCKET_ROOT + * config is not required. */ + if (root == NULL) { + /* all file related outputs have already been set to 0, just return + */ + return; + } + + /* Step 6: Determine the local file path from the root path and the + * request uri. */ + /* Using filename_buf_len - 1 because memmove() for PATH_INFO may shift + * part of the path one byte on the right. */ + mg_snprintf( + conn, &truncated, filename, filename_buf_len - 1, "%s%s", root, uri); + + if (truncated) { + goto interpret_cleanup; + } + + /* Step 7: URI rewriting */ + rewrite = conn->ctx->config[URL_REWRITE_PATTERN]; + while ((rewrite = next_option(rewrite, &a, &b)) != NULL) { + if ((match_len = match_prefix(a.ptr, a.len, uri)) > 0) { + mg_snprintf(conn, + &truncated, + filename, + filename_buf_len - 1, + "%.*s%s", + (int)b.len, + b.ptr, + uri + match_len); + break; + } + } + + if (truncated) { + goto interpret_cleanup; + } + + /* Step 8: Check if the file exists at the server */ + /* Local file path and name, corresponding to requested URI + * is now stored in "filename" variable. */ + if (mg_stat(conn, filename, filestat)) { + /* 8.1: File exists. */ + *is_found = 1; + + /* 8.2: Check if it is a script type. */ + if (extention_matches_script(conn, filename)) { + /* The request addresses a CGI resource, Lua script or + * server-side javascript. + * The URI corresponds to the script itself (like + * /path/script.cgi), and there is no additional resource + * path (like /path/script.cgi/something). + * Requests that modify (replace or delete) a resource, like + * PUT and DELETE requests, should replace/delete the script + * file. + * Requests that read or write from/to a resource, like GET and + * POST requests, should call the script and return the + * generated response. */ + *is_script_resource = (!*is_put_or_delete_request); + } + + /* 8.3: If the request target is a directory, there could be + * a substitute file (index.html, index.cgi, ...). */ + if (filestat->is_directory) { + /* Use a local copy here, since substitute_index_file will + * change the content of the file status */ + struct mg_file_stat tmp_filestat; + memset(&tmp_filestat, 0, sizeof(tmp_filestat)); + + if (substitute_index_file( + conn, filename, filename_buf_len, &tmp_filestat)) { + + /* Substitute file found. Copy stat to the output, then + * check if the file is a script file */ + *filestat = tmp_filestat; + + if (extention_matches_script(conn, filename)) { + /* Substitute file is a script file */ + *is_script_resource = 1; + } else { + /* Substitute file is a regular file */ + *is_script_resource = 0; + *is_found = (mg_stat(conn, filename, filestat) ? 1 : 0); + } + } + /* If there is no substitute file, the server could return + * a directory listing in a later step */ + } + return; + } + + /* Step 9: Check for zipped files: */ + /* If we can't find the actual file, look for the file + * with the same name but a .gz extension. If we find it, + * use that and set the gzipped flag in the file struct + * to indicate that the response need to have the content- + * encoding: gzip header. + * We can only do this if the browser declares support. */ + if (conn->accept_gzip) { + mg_snprintf( + conn, &truncated, gz_path, sizeof(gz_path), "%s.gz", filename); + + if (truncated) { + goto interpret_cleanup; + } + + if (mg_stat(conn, gz_path, filestat)) { + if (filestat) { + filestat->is_gzipped = 1; + *is_found = 1; + } + /* Currently gz files can not be scripts. */ + return; + } + } + +#if !defined(NO_CGI) || defined(USE_LUA) || defined(USE_DUKTAPE) + /* Step 10: Script resources may handle sub-resources */ + /* Support PATH_INFO for CGI scripts. */ + tmp_str_len = strlen(filename); + tmp_str = (char *)mg_malloc_ctx(tmp_str_len + PATH_MAX + 1, conn->ctx); + if (!tmp_str) { + /* Out of memory */ + goto interpret_cleanup; + } + memcpy(tmp_str, filename, tmp_str_len + 1); + + /* Check config, if index scripts may have sub-resources */ + allow_substitute_script_subresources = + !mg_strcasecmp(conn->ctx->config[ALLOW_INDEX_SCRIPT_SUB_RES], "yes"); + + sep_pos = tmp_str_len; + while (sep_pos > 0) { + sep_pos--; + if (tmp_str[sep_pos] == '/') { + int is_script = 0, does_exist = 0; + + tmp_str[sep_pos] = 0; + if (tmp_str[0]) { + is_script = extention_matches_script(conn, tmp_str); + does_exist = mg_stat(conn, tmp_str, filestat); + } + + if (does_exist && is_script) { + filename[sep_pos] = 0; + memmove(filename + sep_pos + 2, + filename + sep_pos + 1, + strlen(filename + sep_pos + 1) + 1); + conn->path_info = filename + sep_pos + 1; + filename[sep_pos + 1] = '/'; + *is_script_resource = 1; + *is_found = 1; + break; + } + + if (allow_substitute_script_subresources) { + if (substitute_index_file( + conn, tmp_str, tmp_str_len + PATH_MAX, filestat)) { + + /* some intermediate directory has an index file */ + if (extention_matches_script(conn, tmp_str)) { + + char *tmp_str2; + + DEBUG_TRACE("Substitute script %s serving path %s", + tmp_str, + filename); + + /* this index file is a script */ + tmp_str2 = mg_strdup(filename + sep_pos + 1); + mg_snprintf(conn, + &truncated, + filename, + filename_buf_len, + "%s//%s", + tmp_str, + tmp_str2); + mg_free(tmp_str2); + + if (truncated) { + mg_free(tmp_str); + goto interpret_cleanup; + } + sep_pos = strlen(tmp_str); + filename[sep_pos] = 0; + conn->path_info = filename + sep_pos + 1; + *is_script_resource = 1; + *is_found = 1; + break; + + } else { + + DEBUG_TRACE("Substitute file %s serving path %s", + tmp_str, + filename); + + /* non-script files will not have sub-resources */ + filename[sep_pos] = 0; + conn->path_info = 0; + *is_script_resource = 0; + *is_found = 0; + break; + } + } + } + + tmp_str[sep_pos] = '/'; + } + } + + mg_free(tmp_str); + +#endif /* !defined(NO_CGI) || defined(USE_LUA) || defined(USE_DUKTAPE) */ +#endif /* !defined(NO_FILES) */ + return; + +#if !defined(NO_FILES) +/* Reset all outputs */ +interpret_cleanup: + memset(filestat, 0, sizeof(*filestat)); + *filename = 0; + *is_found = 0; + *is_script_resource = 0; + *is_websocket_request = 0; + *is_put_or_delete_request = 0; +#endif /* !defined(NO_FILES) */ +} + + +/* Check whether full request is buffered. Return: + * -1 if request or response is malformed + * 0 if request or response is not yet fully buffered + * >0 actual request length, including last \r\n\r\n */ +static int +get_http_header_len(const char *buf, int buflen) +{ + int i; + for (i = 0; i < buflen; i++) { + /* Do an unsigned comparison in some conditions below */ + const unsigned char c = ((const unsigned char *)buf)[i]; + + if ((c < 128) && ((char)c != '\r') && ((char)c != '\n') + && !isprint(c)) { + /* abort scan as soon as one malformed character is found */ + return -1; + } + + if (i < buflen - 1) { + if ((buf[i] == '\n') && (buf[i + 1] == '\n')) { + /* Two newline, no carriage return - not standard compliant, + * but + * it + * should be accepted */ + return i + 2; + } + } + + if (i < buflen - 3) { + if ((buf[i] == '\r') && (buf[i + 1] == '\n') && (buf[i + 2] == '\r') + && (buf[i + 3] == '\n')) { + /* Two \r\n - standard compliant */ + return i + 4; + } + } + } + + return 0; +} + + +#if !defined(NO_CACHING) +/* Convert month to the month number. Return -1 on error, or month number */ +static int +get_month_index(const char *s) +{ + size_t i; + + for (i = 0; i < ARRAY_SIZE(month_names); i++) { + if (!strcmp(s, month_names[i])) { + return (int)i; + } + } + + return -1; +} + + +/* Parse UTC date-time string, and return the corresponding time_t value. */ +static time_t +parse_date_string(const char *datetime) +{ + char month_str[32] = {0}; + int second, minute, hour, day, month, year; + time_t result = (time_t)0; + struct tm tm; + + if ((sscanf(datetime, + "%d/%3s/%d %d:%d:%d", + &day, + month_str, + &year, + &hour, + &minute, + &second) == 6) || (sscanf(datetime, + "%d %3s %d %d:%d:%d", + &day, + month_str, + &year, + &hour, + &minute, + &second) == 6) + || (sscanf(datetime, + "%*3s, %d %3s %d %d:%d:%d", + &day, + month_str, + &year, + &hour, + &minute, + &second) == 6) || (sscanf(datetime, + "%d-%3s-%d %d:%d:%d", + &day, + month_str, + &year, + &hour, + &minute, + &second) == 6)) { + month = get_month_index(month_str); + if ((month >= 0) && (year >= 1970)) { + memset(&tm, 0, sizeof(tm)); + tm.tm_year = year - 1900; + tm.tm_mon = month; + tm.tm_mday = day; + tm.tm_hour = hour; + tm.tm_min = minute; + tm.tm_sec = second; + result = timegm(&tm); + } + } + + return result; +} +#endif /* !NO_CACHING */ + + +/* Protect against directory disclosure attack by removing '..', + * excessive '/' and '\' characters */ +static void +remove_double_dots_and_double_slashes(char *s) +{ + char *p = s; + + while ((s[0] == '.') && (s[1] == '.')) { + s++; + } + + while (*s != '\0') { + *p++ = *s++; + if ((s[-1] == '/') || (s[-1] == '\\')) { + /* Skip all following slashes, backslashes and double-dots */ + while (s[0] != '\0') { + if ((s[0] == '/') || (s[0] == '\\')) { + s++; + } else if ((s[0] == '.') && (s[1] == '.')) { + s += 2; + } else { + break; + } + } + } + } + *p = '\0'; +} + + +static const struct { + const char *extension; + size_t ext_len; + const char *mime_type; +} builtin_mime_types[] = { + /* IANA registered MIME types + * (http://www.iana.org/assignments/media-types) + * application types */ + {".doc", 4, "application/msword"}, + {".eps", 4, "application/postscript"}, + {".exe", 4, "application/octet-stream"}, + {".js", 3, "application/javascript"}, + {".json", 5, "application/json"}, + {".pdf", 4, "application/pdf"}, + {".ps", 3, "application/postscript"}, + {".rtf", 4, "application/rtf"}, + {".xhtml", 6, "application/xhtml+xml"}, + {".xsl", 4, "application/xml"}, + {".xslt", 5, "application/xml"}, + + /* fonts */ + {".ttf", 4, "application/font-sfnt"}, + {".cff", 4, "application/font-sfnt"}, + {".otf", 4, "application/font-sfnt"}, + {".aat", 4, "application/font-sfnt"}, + {".sil", 4, "application/font-sfnt"}, + {".pfr", 4, "application/font-tdpfr"}, + {".woff", 5, "application/font-woff"}, + + /* audio */ + {".mp3", 4, "audio/mpeg"}, + {".oga", 4, "audio/ogg"}, + {".ogg", 4, "audio/ogg"}, + + /* image */ + {".gif", 4, "image/gif"}, + {".ief", 4, "image/ief"}, + {".jpeg", 5, "image/jpeg"}, + {".jpg", 4, "image/jpeg"}, + {".jpm", 4, "image/jpm"}, + {".jpx", 4, "image/jpx"}, + {".png", 4, "image/png"}, + {".svg", 4, "image/svg+xml"}, + {".tif", 4, "image/tiff"}, + {".tiff", 5, "image/tiff"}, + + /* model */ + {".wrl", 4, "model/vrml"}, + + /* text */ + {".css", 4, "text/css"}, + {".csv", 4, "text/csv"}, + {".htm", 4, "text/html"}, + {".html", 5, "text/html"}, + {".sgm", 4, "text/sgml"}, + {".shtm", 5, "text/html"}, + {".shtml", 6, "text/html"}, + {".txt", 4, "text/plain"}, + {".xml", 4, "text/xml"}, + + /* video */ + {".mov", 4, "video/quicktime"}, + {".mp4", 4, "video/mp4"}, + {".mpeg", 5, "video/mpeg"}, + {".mpg", 4, "video/mpeg"}, + {".ogv", 4, "video/ogg"}, + {".qt", 3, "video/quicktime"}, + + /* not registered types + * (http://reference.sitepoint.com/html/mime-types-full, + * http://www.hansenb.pdx.edu/DMKB/dict/tutorials/mime_typ.php, ..) */ + {".arj", 4, "application/x-arj-compressed"}, + {".gz", 3, "application/x-gunzip"}, + {".rar", 4, "application/x-arj-compressed"}, + {".swf", 4, "application/x-shockwave-flash"}, + {".tar", 4, "application/x-tar"}, + {".tgz", 4, "application/x-tar-gz"}, + {".torrent", 8, "application/x-bittorrent"}, + {".ppt", 4, "application/x-mspowerpoint"}, + {".xls", 4, "application/x-msexcel"}, + {".zip", 4, "application/x-zip-compressed"}, + {".aac", + 4, + "audio/aac"}, /* http://en.wikipedia.org/wiki/Advanced_Audio_Coding */ + {".aif", 4, "audio/x-aif"}, + {".m3u", 4, "audio/x-mpegurl"}, + {".mid", 4, "audio/x-midi"}, + {".ra", 3, "audio/x-pn-realaudio"}, + {".ram", 4, "audio/x-pn-realaudio"}, + {".wav", 4, "audio/x-wav"}, + {".bmp", 4, "image/bmp"}, + {".ico", 4, "image/x-icon"}, + {".pct", 4, "image/x-pct"}, + {".pict", 5, "image/pict"}, + {".rgb", 4, "image/x-rgb"}, + {".webm", 5, "video/webm"}, /* http://en.wikipedia.org/wiki/WebM */ + {".asf", 4, "video/x-ms-asf"}, + {".avi", 4, "video/x-msvideo"}, + {".m4v", 4, "video/x-m4v"}, + {NULL, 0, NULL}}; + + +const char * +mg_get_builtin_mime_type(const char *path) +{ + const char *ext; + size_t i, path_len; + + path_len = strlen(path); + + for (i = 0; builtin_mime_types[i].extension != NULL; i++) { + ext = path + (path_len - builtin_mime_types[i].ext_len); + if ((path_len > builtin_mime_types[i].ext_len) + && (mg_strcasecmp(ext, builtin_mime_types[i].extension) == 0)) { + return builtin_mime_types[i].mime_type; + } + } + + return "text/plain"; +} + + +/* Look at the "path" extension and figure what mime type it has. + * Store mime type in the vector. */ +static void +get_mime_type(struct mg_context *ctx, const char *path, struct vec *vec) +{ + struct vec ext_vec, mime_vec; + const char *list, *ext; + size_t path_len; + + path_len = strlen(path); + + if ((ctx == NULL) || (vec == NULL)) { + if (vec != NULL) { + memset(vec, '\0', sizeof(struct vec)); + } + return; + } + + /* Scan user-defined mime types first, in case user wants to + * override default mime types. */ + list = ctx->config[EXTRA_MIME_TYPES]; + while ((list = next_option(list, &ext_vec, &mime_vec)) != NULL) { + /* ext now points to the path suffix */ + ext = path + path_len - ext_vec.len; + if (mg_strncasecmp(ext, ext_vec.ptr, ext_vec.len) == 0) { + *vec = mime_vec; + return; + } + } + + vec->ptr = mg_get_builtin_mime_type(path); + vec->len = strlen(vec->ptr); +} + + +/* Stringify binary data. Output buffer must be twice as big as input, + * because each byte takes 2 bytes in string representation */ +static void +bin2str(char *to, const unsigned char *p, size_t len) +{ + static const char *hex = "0123456789abcdef"; + + for (; len--; p++) { + *to++ = hex[p[0] >> 4]; + *to++ = hex[p[0] & 0x0f]; + } + *to = '\0'; +} + + +/* Return stringified MD5 hash for list of strings. Buffer must be 33 bytes. + */ +char * +mg_md5(char buf[33], ...) +{ + md5_byte_t hash[16]; + const char *p; + va_list ap; + md5_state_t ctx; + + md5_init(&ctx); + + va_start(ap, buf); + while ((p = va_arg(ap, const char *)) != NULL) { + md5_append(&ctx, (const md5_byte_t *)p, strlen(p)); + } + va_end(ap); + + md5_finish(&ctx, hash); + bin2str(buf, hash, sizeof(hash)); + return buf; +} + + +/* Check the user's password, return 1 if OK */ +static int +check_password(const char *method, + const char *ha1, + const char *uri, + const char *nonce, + const char *nc, + const char *cnonce, + const char *qop, + const char *response) +{ + char ha2[32 + 1], expected_response[32 + 1]; + + /* Some of the parameters may be NULL */ + if ((method == NULL) || (nonce == NULL) || (nc == NULL) || (cnonce == NULL) + || (qop == NULL) || (response == NULL)) { + return 0; + } + + /* NOTE(lsm): due to a bug in MSIE, we do not compare the URI */ + if (strlen(response) != 32) { + return 0; + } + + mg_md5(ha2, method, ":", uri, NULL); + mg_md5(expected_response, + ha1, + ":", + nonce, + ":", + nc, + ":", + cnonce, + ":", + qop, + ":", + ha2, + NULL); + + return mg_strcasecmp(response, expected_response) == 0; +} + + +/* Use the global passwords file, if specified by auth_gpass option, + * or search for .htpasswd in the requested directory. */ +static void +open_auth_file(struct mg_connection *conn, + const char *path, + struct mg_file *filep) +{ + if ((conn != NULL) && (conn->ctx != NULL)) { + char name[PATH_MAX]; + const char *p, *e, *gpass = conn->ctx->config[GLOBAL_PASSWORDS_FILE]; + int truncated; + + if (gpass != NULL) { + /* Use global passwords file */ + if (!mg_fopen(conn, gpass, MG_FOPEN_MODE_READ, filep)) { +#ifdef DEBUG + /* Use mg_cry here, since gpass has been configured. */ + mg_cry(conn, "fopen(%s): %s", gpass, strerror(ERRNO)); +#endif + } + /* Important: using local struct mg_file to test path for + * is_directory flag. If filep is used, mg_stat() makes it + * appear as if auth file was opened. + * TODO(mid): Check if this is still required after rewriting + * mg_stat */ + } else if (mg_stat(conn, path, &filep->stat) + && filep->stat.is_directory) { + mg_snprintf(conn, + &truncated, + name, + sizeof(name), + "%s/%s", + path, + PASSWORDS_FILE_NAME); + + if (truncated || !mg_fopen(conn, name, MG_FOPEN_MODE_READ, filep)) { +#ifdef DEBUG + /* Don't use mg_cry here, but only a trace, since this is + * a typical case. It will occur for every directory + * without a password file. */ + DEBUG_TRACE("fopen(%s): %s", name, strerror(ERRNO)); +#endif + } + } else { + /* Try to find .htpasswd in requested directory. */ + for (p = path, e = p + strlen(p) - 1; e > p; e--) { + if (e[0] == '/') { + break; + } + } + mg_snprintf(conn, + &truncated, + name, + sizeof(name), + "%.*s/%s", + (int)(e - p), + p, + PASSWORDS_FILE_NAME); + + if (truncated || !mg_fopen(conn, name, MG_FOPEN_MODE_READ, filep)) { +#ifdef DEBUG + /* Don't use mg_cry here, but only a trace, since this is + * a typical case. It will occur for every directory + * without a password file. */ + DEBUG_TRACE("fopen(%s): %s", name, strerror(ERRNO)); +#endif + } + } + } +} + + +/* Parsed Authorization header */ +struct ah { + char *user, *uri, *cnonce, *response, *qop, *nc, *nonce; +}; + + +/* Return 1 on success. Always initializes the ah structure. */ +static int +parse_auth_header(struct mg_connection *conn, + char *buf, + size_t buf_size, + struct ah *ah) +{ + char *name, *value, *s; + const char *auth_header; + uint64_t nonce; + + if (!ah || !conn) { + return 0; + } + + (void)memset(ah, 0, sizeof(*ah)); + if (((auth_header = mg_get_header(conn, "Authorization")) == NULL) + || mg_strncasecmp(auth_header, "Digest ", 7) != 0) { + return 0; + } + + /* Make modifiable copy of the auth header */ + (void)mg_strlcpy(buf, auth_header + 7, buf_size); + s = buf; + + /* Parse authorization header */ + for (;;) { + /* Gobble initial spaces */ + while (isspace(*(unsigned char *)s)) { + s++; + } + name = skip_quoted(&s, "=", " ", 0); + /* Value is either quote-delimited, or ends at first comma or space. + */ + if (s[0] == '\"') { + s++; + value = skip_quoted(&s, "\"", " ", '\\'); + if (s[0] == ',') { + s++; + } + } else { + value = skip_quoted(&s, ", ", " ", 0); /* IE uses commas, FF uses + * spaces */ + } + if (*name == '\0') { + break; + } + + if (!strcmp(name, "username")) { + ah->user = value; + } else if (!strcmp(name, "cnonce")) { + ah->cnonce = value; + } else if (!strcmp(name, "response")) { + ah->response = value; + } else if (!strcmp(name, "uri")) { + ah->uri = value; + } else if (!strcmp(name, "qop")) { + ah->qop = value; + } else if (!strcmp(name, "nc")) { + ah->nc = value; + } else if (!strcmp(name, "nonce")) { + ah->nonce = value; + } + } + +#ifndef NO_NONCE_CHECK + /* Read the nonce from the response. */ + if (ah->nonce == NULL) { + return 0; + } + s = NULL; + nonce = strtoull(ah->nonce, &s, 10); + if ((s == NULL) || (*s != 0)) { + return 0; + } + + /* Convert the nonce from the client to a number. */ + nonce ^= conn->ctx->auth_nonce_mask; + + /* The converted number corresponds to the time the nounce has been + * created. This should not be earlier than the server start. */ + /* Server side nonce check is valuable in all situations but one: + * if the server restarts frequently, but the client should not see + * that, so the server should accept nonces from previous starts. */ + /* However, the reasonable default is to not accept a nonce from a + * previous start, so if anyone changed the access rights between + * two restarts, a new login is required. */ + if (nonce < (uint64_t)conn->ctx->start_time) { + /* nonce is from a previous start of the server and no longer valid + * (replay attack?) */ + return 0; + } + /* Check if the nonce is too high, so it has not (yet) been used by the + * server. */ + if (nonce >= ((uint64_t)conn->ctx->start_time + conn->ctx->nonce_count)) { + return 0; + } +#else + (void)nonce; +#endif + + /* CGI needs it as REMOTE_USER */ + if (ah->user != NULL) { + conn->request_info.remote_user = mg_strdup(ah->user); + } else { + return 0; + } + + return 1; +} + + +static const char * +mg_fgets(char *buf, size_t size, struct mg_file *filep, char **p) +{ + const char *eof; + size_t len; + const char *memend; + + if (!filep) { + return NULL; + } + + if ((filep->access.membuf != NULL) && (*p != NULL)) { + memend = (const char *)&filep->access.membuf[filep->stat.size]; + /* Search for \n from p till the end of stream */ + eof = (char *)memchr(*p, '\n', (size_t)(memend - *p)); + if (eof != NULL) { + eof += 1; /* Include \n */ + } else { + eof = memend; /* Copy remaining data */ + } + len = + ((size_t)(eof - *p) > (size - 1)) ? (size - 1) : (size_t)(eof - *p); + memcpy(buf, *p, len); + buf[len] = '\0'; + *p += len; + return len ? eof : NULL; + } else if (filep->access.fp != NULL) { + return fgets(buf, (int)size, filep->access.fp); + } else { + return NULL; + } +} + +/* Define the initial recursion depth for procesesing htpasswd files that + * include other htpasswd + * (or even the same) files. It is not difficult to provide a file or files + * s.t. they force civetweb + * to infinitely recurse and then crash. + */ +#define INITIAL_DEPTH 9 +#if INITIAL_DEPTH <= 0 +#error Bad INITIAL_DEPTH for recursion, set to at least 1 +#endif + +struct read_auth_file_struct { + struct mg_connection *conn; + struct ah ah; + const char *domain; + char buf[256 + 256 + 40]; + const char *f_user; + const char *f_domain; + const char *f_ha1; +}; + + +static int +read_auth_file(struct mg_file *filep, + struct read_auth_file_struct *workdata, + int depth) +{ + char *p; + int is_authorized = 0; + struct mg_file fp; + size_t l; + + if (!filep || !workdata || (0 == depth)) { + return 0; + } + + /* Loop over passwords file */ + p = (char *)filep->access.membuf; + while (mg_fgets(workdata->buf, sizeof(workdata->buf), filep, &p) != NULL) { + l = strlen(workdata->buf); + while (l > 0) { + if (isspace(workdata->buf[l - 1]) + || iscntrl(workdata->buf[l - 1])) { + l--; + workdata->buf[l] = 0; + } else + break; + } + if (l < 1) { + continue; + } + + workdata->f_user = workdata->buf; + + if (workdata->f_user[0] == ':') { + /* user names may not contain a ':' and may not be empty, + * so lines starting with ':' may be used for a special purpose + */ + if (workdata->f_user[1] == '#') { + /* :# is a comment */ + continue; + } else if (!strncmp(workdata->f_user + 1, "include=", 8)) { + if (mg_fopen(workdata->conn, + workdata->f_user + 9, + MG_FOPEN_MODE_READ, + &fp)) { + is_authorized = read_auth_file(&fp, workdata, depth - 1); + (void)mg_fclose( + &fp.access); /* ignore error on read only file */ + + /* No need to continue processing files once we have a + * match, since nothing will reset it back + * to 0. + */ + if (is_authorized) { + return is_authorized; + } + } else { + mg_cry(workdata->conn, + "%s: cannot open authorization file: %s", + __func__, + workdata->buf); + } + continue; + } + /* everything is invalid for the moment (might change in the + * future) */ + mg_cry(workdata->conn, + "%s: syntax error in authorization file: %s", + __func__, + workdata->buf); + continue; + } + + workdata->f_domain = strchr(workdata->f_user, ':'); + if (workdata->f_domain == NULL) { + mg_cry(workdata->conn, + "%s: syntax error in authorization file: %s", + __func__, + workdata->buf); + continue; + } + *(char *)(workdata->f_domain) = 0; + (workdata->f_domain)++; + + workdata->f_ha1 = strchr(workdata->f_domain, ':'); + if (workdata->f_ha1 == NULL) { + mg_cry(workdata->conn, + "%s: syntax error in authorization file: %s", + __func__, + workdata->buf); + continue; + } + *(char *)(workdata->f_ha1) = 0; + (workdata->f_ha1)++; + + if (!strcmp(workdata->ah.user, workdata->f_user) + && !strcmp(workdata->domain, workdata->f_domain)) { + return check_password(workdata->conn->request_info.request_method, + workdata->f_ha1, + workdata->ah.uri, + workdata->ah.nonce, + workdata->ah.nc, + workdata->ah.cnonce, + workdata->ah.qop, + workdata->ah.response); + } + } + + return is_authorized; +} + + +/* Authorize against the opened passwords file. Return 1 if authorized. */ +static int +authorize(struct mg_connection *conn, struct mg_file *filep, const char *realm) +{ + struct read_auth_file_struct workdata; + char buf[MG_BUF_LEN]; + + if (!conn || !conn->ctx) { + return 0; + } + + memset(&workdata, 0, sizeof(workdata)); + workdata.conn = conn; + + if (!parse_auth_header(conn, buf, sizeof(buf), &workdata.ah)) { + return 0; + } + + if (realm) { + workdata.domain = realm; + } else { + workdata.domain = conn->ctx->config[AUTHENTICATION_DOMAIN]; + } + + return read_auth_file(filep, &workdata, INITIAL_DEPTH); +} + + +/* Public function to check http digest authentication header */ +int +mg_check_digest_access_authentication(struct mg_connection *conn, + const char *realm, + const char *filename) +{ + struct mg_file file = STRUCT_FILE_INITIALIZER; + int auth; + + if (!conn || !filename) { + return -1; + } + if (!mg_fopen(conn, filename, MG_FOPEN_MODE_READ, &file)) { + return -2; + } + + auth = authorize(conn, &file, realm); + + mg_fclose(&file.access); + + return auth; +} + + +/* Return 1 if request is authorised, 0 otherwise. */ +static int +check_authorization(struct mg_connection *conn, const char *path) +{ + char fname[PATH_MAX]; + struct vec uri_vec, filename_vec; + const char *list; + struct mg_file file = STRUCT_FILE_INITIALIZER; + int authorized = 1, truncated; + + if (!conn || !conn->ctx) { + return 0; + } + + list = conn->ctx->config[PROTECT_URI]; + while ((list = next_option(list, &uri_vec, &filename_vec)) != NULL) { + if (!memcmp(conn->request_info.local_uri, uri_vec.ptr, uri_vec.len)) { + mg_snprintf(conn, + &truncated, + fname, + sizeof(fname), + "%.*s", + (int)filename_vec.len, + filename_vec.ptr); + + if (truncated + || !mg_fopen(conn, fname, MG_FOPEN_MODE_READ, &file)) { + mg_cry(conn, + "%s: cannot open %s: %s", + __func__, + fname, + strerror(errno)); + } + break; + } + } + + if (!is_file_opened(&file.access)) { + open_auth_file(conn, path, &file); + } + + if (is_file_opened(&file.access)) { + authorized = authorize(conn, &file, NULL); + (void)mg_fclose(&file.access); /* ignore error on read only file */ + } + + return authorized; +} + + +/* Internal function. Assumes conn is valid */ +static void +send_authorization_request(struct mg_connection *conn, const char *realm) +{ + char date[64]; + time_t curtime = time(NULL); + uint64_t nonce = (uint64_t)(conn->ctx->start_time); + + if (!realm) { + realm = conn->ctx->config[AUTHENTICATION_DOMAIN]; + } + + (void)pthread_mutex_lock(&conn->ctx->nonce_mutex); + nonce += conn->ctx->nonce_count; + ++conn->ctx->nonce_count; + (void)pthread_mutex_unlock(&conn->ctx->nonce_mutex); + + nonce ^= conn->ctx->auth_nonce_mask; + conn->status_code = 401; + conn->must_close = 1; + + gmt_time_string(date, sizeof(date), &curtime); + + mg_printf(conn, "HTTP/1.1 401 Unauthorized\r\n"); + send_no_cache_header(conn); + send_additional_header(conn); + mg_printf(conn, + "Date: %s\r\n" + "Connection: %s\r\n" + "Content-Length: 0\r\n" + "WWW-Authenticate: Digest qop=\"auth\", realm=\"%s\", " + "nonce=\"%" UINT64_FMT "\"\r\n\r\n", + date, + suggest_connection_header(conn), + realm, + nonce); +} + + +/* Interface function. Parameters are provided by the user, so do + * at least some basic checks. + */ +int +mg_send_digest_access_authentication_request(struct mg_connection *conn, + const char *realm) +{ + if (conn && conn->ctx) { + send_authorization_request(conn, realm); + return 0; + } + return -1; +} + + +#if !defined(NO_FILES) +static int +is_authorized_for_put(struct mg_connection *conn) +{ + if (conn) { + struct mg_file file = STRUCT_FILE_INITIALIZER; + const char *passfile = conn->ctx->config[PUT_DELETE_PASSWORDS_FILE]; + int ret = 0; + + if (passfile != NULL + && mg_fopen(conn, passfile, MG_FOPEN_MODE_READ, &file)) { + ret = authorize(conn, &file, NULL); + (void)mg_fclose(&file.access); /* ignore error on read only file */ + } + + return ret; + } + return 0; +} +#endif + + +int +mg_modify_passwords_file(const char *fname, + const char *domain, + const char *user, + const char *pass) +{ + int found, i; + char line[512], u[512] = "", d[512] = "", ha1[33], tmp[PATH_MAX + 8]; + FILE *fp, *fp2; + + found = 0; + fp = fp2 = NULL; + + /* Regard empty password as no password - remove user record. */ + if ((pass != NULL) && (pass[0] == '\0')) { + pass = NULL; + } + + /* Other arguments must not be empty */ + if ((fname == NULL) || (domain == NULL) || (user == NULL)) { + return 0; + } + + /* Using the given file format, user name and domain must not contain + * ':' + */ + if (strchr(user, ':') != NULL) { + return 0; + } + if (strchr(domain, ':') != NULL) { + return 0; + } + + /* Do not allow control characters like newline in user name and domain. + * Do not allow excessively long names either. */ + for (i = 0; ((i < 255) && (user[i] != 0)); i++) { + if (iscntrl(user[i])) { + return 0; + } + } + if (user[i]) { + return 0; + } + for (i = 0; ((i < 255) && (domain[i] != 0)); i++) { + if (iscntrl(domain[i])) { + return 0; + } + } + if (domain[i]) { + return 0; + } + + /* The maximum length of the path to the password file is limited */ + if ((strlen(fname) + 4) >= PATH_MAX) { + return 0; + } + + /* Create a temporary file name. Length has been checked before. */ + strcpy(tmp, fname); + strcat(tmp, ".tmp"); + + /* Create the file if does not exist */ + /* Use of fopen here is OK, since fname is only ASCII */ + if ((fp = fopen(fname, "a+")) != NULL) { + (void)fclose(fp); + } + + /* Open the given file and temporary file */ + if ((fp = fopen(fname, "r")) == NULL) { + return 0; + } else if ((fp2 = fopen(tmp, "w+")) == NULL) { + fclose(fp); + return 0; + } + + /* Copy the stuff to temporary file */ + while (fgets(line, sizeof(line), fp) != NULL) { + if (sscanf(line, "%255[^:]:%255[^:]:%*s", u, d) != 2) { + continue; + } + u[255] = 0; + d[255] = 0; + + if (!strcmp(u, user) && !strcmp(d, domain)) { + found++; + if (pass != NULL) { + mg_md5(ha1, user, ":", domain, ":", pass, NULL); + fprintf(fp2, "%s:%s:%s\n", user, domain, ha1); + } + } else { + fprintf(fp2, "%s", line); + } + } + + /* If new user, just add it */ + if (!found && (pass != NULL)) { + mg_md5(ha1, user, ":", domain, ":", pass, NULL); + fprintf(fp2, "%s:%s:%s\n", user, domain, ha1); + } + + /* Close files */ + fclose(fp); + fclose(fp2); + + /* Put the temp file in place of real file */ + IGNORE_UNUSED_RESULT(remove(fname)); + IGNORE_UNUSED_RESULT(rename(tmp, fname)); + + return 1; +} + + +static int +is_valid_port(unsigned long port) +{ + return (port <= 0xffff); +} + + +static int +mg_inet_pton(int af, const char *src, void *dst, size_t dstlen) +{ + struct addrinfo hints, *res, *ressave; + int func_ret = 0; + int gai_ret; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = af; + + gai_ret = getaddrinfo(src, NULL, &hints, &res); + if (gai_ret != 0) { + /* gai_strerror could be used to convert gai_ret to a string */ + /* POSIX return values: see + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/freeaddrinfo.html + */ + /* Windows return values: see + * https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520%28v=vs.85%29.aspx + */ + return 0; + } + + ressave = res; + + while (res) { + if (dstlen >= (size_t)res->ai_addrlen) { + memcpy(dst, res->ai_addr, res->ai_addrlen); + func_ret = 1; + } + res = res->ai_next; + } + + freeaddrinfo(ressave); + return func_ret; +} + + +static int +connect_socket(struct mg_context *ctx /* may be NULL */, + const char *host, + int port, + int use_ssl, + char *ebuf, + size_t ebuf_len, + SOCKET *sock /* output: socket, must not be NULL */, + union usa *sa /* output: socket address, must not be NULL */ + ) +{ + int ip_ver = 0; + *sock = INVALID_SOCKET; + memset(sa, 0, sizeof(*sa)); + + if (ebuf_len > 0) { + *ebuf = 0; + } + + if (host == NULL) { + mg_snprintf(NULL, + NULL, /* No truncation check for ebuf */ + ebuf, + ebuf_len, + "%s", + "NULL host"); + return 0; + } + + if ((port <= 0) || !is_valid_port((unsigned)port)) { + mg_snprintf(NULL, + NULL, /* No truncation check for ebuf */ + ebuf, + ebuf_len, + "%s", + "invalid port"); + return 0; + } + +#if !defined(NO_SSL) +#if !defined(NO_SSL_DL) +#ifdef OPENSSL_API_1_1 + if (use_ssl && (TLS_client_method == NULL)) { + mg_snprintf(NULL, + NULL, /* No truncation check for ebuf */ + ebuf, + ebuf_len, + "%s", + "SSL is not initialized"); + return 0; + } +#else + if (use_ssl && (SSLv23_client_method == NULL)) { + mg_snprintf(NULL, + NULL, /* No truncation check for ebuf */ + ebuf, + ebuf_len, + "%s", + "SSL is not initialized"); + return 0; + } + +#endif /* OPENSSL_API_1_1 */ +#else + (void)use_ssl; +#endif /* NO_SSL_DL */ +#else + (void)use_ssl; +#endif /* !defined(NO_SSL) */ + + if (mg_inet_pton(AF_INET, host, &sa->sin, sizeof(sa->sin))) { + sa->sin.sin_family = AF_INET; + sa->sin.sin_port = htons((uint16_t)port); + ip_ver = 4; +#ifdef USE_IPV6 + } else if (mg_inet_pton(AF_INET6, host, &sa->sin6, sizeof(sa->sin6))) { + sa->sin6.sin6_family = AF_INET6; + sa->sin6.sin6_port = htons((uint16_t)port); + ip_ver = 6; + } else if (host[0] == '[') { + /* While getaddrinfo on Windows will work with [::1], + * getaddrinfo on Linux only works with ::1 (without []). */ + size_t l = strlen(host + 1); + char *h = (l > 1) ? mg_strdup(host + 1) : NULL; + if (h) { + h[l - 1] = 0; + if (mg_inet_pton(AF_INET6, h, &sa->sin6, sizeof(sa->sin6))) { + sa->sin6.sin6_family = AF_INET6; + sa->sin6.sin6_port = htons((uint16_t)port); + ip_ver = 6; + } + mg_free(h); + } +#endif + } + + if (ip_ver == 0) { + mg_snprintf(NULL, + NULL, /* No truncation check for ebuf */ + ebuf, + ebuf_len, + "%s", + "host not found"); + return 0; + } + + if (ip_ver == 4) { + *sock = socket(PF_INET, SOCK_STREAM, 0); + } +#ifdef USE_IPV6 + else if (ip_ver == 6) { + *sock = socket(PF_INET6, SOCK_STREAM, 0); + } +#endif + + if (*sock == INVALID_SOCKET) { + mg_snprintf(NULL, + NULL, /* No truncation check for ebuf */ + ebuf, + ebuf_len, + "socket(): %s", + strerror(ERRNO)); + return 0; + } + + set_close_on_exec(*sock, fc(ctx)); + + if ((ip_ver == 4) + && (connect(*sock, (struct sockaddr *)&sa->sin, sizeof(sa->sin)) + == 0)) { + /* connected with IPv4 */ + if (0 == set_non_blocking_mode(*sock)) { + /* Ok */ + return 1; + } + /* failed */ + /* TODO: specific error message */ + } + +#ifdef USE_IPV6 + if ((ip_ver == 6) + && (connect(*sock, (struct sockaddr *)&sa->sin6, sizeof(sa->sin6)) + == 0)) { + /* connected with IPv6 */ + if (0 == set_non_blocking_mode(*sock)) { + /* Ok */ + return 1; + } + /* failed */ + /* TODO: specific error message */ + } +#endif + + /* Not connected */ + mg_snprintf(NULL, + NULL, /* No truncation check for ebuf */ + ebuf, + ebuf_len, + "connect(%s:%d): %s", + host, + port, + strerror(ERRNO)); + closesocket(*sock); + *sock = INVALID_SOCKET; + + return 0; +} + + +int +mg_url_encode(const char *src, char *dst, size_t dst_len) +{ + static const char *dont_escape = "._-$,;~()"; + static const char *hex = "0123456789abcdef"; + char *pos = dst; + const char *end = dst + dst_len - 1; + + for (; ((*src != '\0') && (pos < end)); src++, pos++) { + if (isalnum(*(const unsigned char *)src) + || (strchr(dont_escape, *(const unsigned char *)src) != NULL)) { + *pos = *src; + } else if (pos + 2 < end) { + pos[0] = '%'; + pos[1] = hex[(*(const unsigned char *)src) >> 4]; + pos[2] = hex[(*(const unsigned char *)src) & 0xf]; + pos += 2; + } else { + break; + } + } + + *pos = '\0'; + return (*src == '\0') ? (int)(pos - dst) : -1; +} + +/* Return 0 on success, non-zero if an error occurs. */ + +static int +print_dir_entry(struct de *de) +{ + size_t hrefsize; + char *href; + char size[64], mod[64]; + struct tm *tm; + + hrefsize = PATH_MAX * 3; /* worst case */ + href = (char *)mg_malloc(hrefsize); + if (href == NULL) { + return -1; + } + if (de->file.is_directory) { + mg_snprintf(de->conn, + NULL, /* Buffer is big enough */ + size, + sizeof(size), + "%s", + "[DIRECTORY]"); + } else { + /* We use (signed) cast below because MSVC 6 compiler cannot + * convert unsigned __int64 to double. Sigh. */ + if (de->file.size < 1024) { + mg_snprintf(de->conn, + NULL, /* Buffer is big enough */ + size, + sizeof(size), + "%d", + (int)de->file.size); + } else if (de->file.size < 0x100000) { + mg_snprintf(de->conn, + NULL, /* Buffer is big enough */ + size, + sizeof(size), + "%.1fk", + (double)de->file.size / 1024.0); + } else if (de->file.size < 0x40000000) { + mg_snprintf(de->conn, + NULL, /* Buffer is big enough */ + size, + sizeof(size), + "%.1fM", + (double)de->file.size / 1048576); + } else { + mg_snprintf(de->conn, + NULL, /* Buffer is big enough */ + size, + sizeof(size), + "%.1fG", + (double)de->file.size / 1073741824); + } + } + + /* Note: mg_snprintf will not cause a buffer overflow above. + * So, string truncation checks are not required here. */ + + tm = localtime(&de->file.last_modified); + if (tm != NULL) { + strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", tm); + } else { + mg_strlcpy(mod, "01-Jan-1970 00:00", sizeof(mod)); + mod[sizeof(mod) - 1] = '\0'; + } + mg_url_encode(de->file_name, href, hrefsize); + mg_printf(de->conn, + "%s%s" + " %s  %s\n", + de->conn->request_info.local_uri, + href, + de->file.is_directory ? "/" : "", + de->file_name, + de->file.is_directory ? "/" : "", + mod, + size); + mg_free(href); + return 0; +} + + +/* This function is called from send_directory() and used for + * sorting directory entries by size, or name, or modification time. + * On windows, __cdecl specification is needed in case if project is built + * with __stdcall convention. qsort always requires __cdels callback. */ +static int WINCDECL +compare_dir_entries(const void *p1, const void *p2) +{ + if (p1 && p2) { + const struct de *a = (const struct de *)p1, *b = (const struct de *)p2; + const char *query_string = a->conn->request_info.query_string; + int cmp_result = 0; + + if (query_string == NULL) { + query_string = "na"; + } + + if (a->file.is_directory && !b->file.is_directory) { + return -1; /* Always put directories on top */ + } else if (!a->file.is_directory && b->file.is_directory) { + return 1; /* Always put directories on top */ + } else if (*query_string == 'n') { + cmp_result = strcmp(a->file_name, b->file_name); + } else if (*query_string == 's') { + cmp_result = (a->file.size == b->file.size) + ? 0 + : ((a->file.size > b->file.size) ? 1 : -1); + } else if (*query_string == 'd') { + cmp_result = + (a->file.last_modified == b->file.last_modified) + ? 0 + : ((a->file.last_modified > b->file.last_modified) ? 1 + : -1); + } + + return (query_string[1] == 'd') ? -cmp_result : cmp_result; + } + return 0; +} + + +static int +must_hide_file(struct mg_connection *conn, const char *path) +{ + if (conn && conn->ctx) { + const char *pw_pattern = "**" PASSWORDS_FILE_NAME "$"; + const char *pattern = conn->ctx->config[HIDE_FILES]; + return (match_prefix(pw_pattern, strlen(pw_pattern), path) > 0) + || ((pattern != NULL) + && (match_prefix(pattern, strlen(pattern), path) > 0)); + } + return 0; +} + + +static int +scan_directory(struct mg_connection *conn, + const char *dir, + void *data, + int (*cb)(struct de *, void *)) +{ + char path[PATH_MAX]; + struct dirent *dp; + DIR *dirp; + struct de de; + int truncated; + + if ((dirp = mg_opendir(conn, dir)) == NULL) { + return 0; + } else { + de.conn = conn; + + while ((dp = mg_readdir(dirp)) != NULL) { + /* Do not show current dir and hidden files */ + if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") + || must_hide_file(conn, dp->d_name)) { + continue; + } + + mg_snprintf( + conn, &truncated, path, sizeof(path), "%s/%s", dir, dp->d_name); + + /* If we don't memset stat structure to zero, mtime will have + * garbage and strftime() will segfault later on in + * print_dir_entry(). memset is required only if mg_stat() + * fails. For more details, see + * http://code.google.com/p/mongoose/issues/detail?id=79 */ + memset(&de.file, 0, sizeof(de.file)); + + if (truncated) { + /* If the path is not complete, skip processing. */ + continue; + } + + if (!mg_stat(conn, path, &de.file)) { + mg_cry(conn, + "%s: mg_stat(%s) failed: %s", + __func__, + path, + strerror(ERRNO)); + } + de.file_name = dp->d_name; + cb(&de, data); + } + (void)mg_closedir(dirp); + } + return 1; +} + + +#if !defined(NO_FILES) +static int +remove_directory(struct mg_connection *conn, const char *dir) +{ + char path[PATH_MAX]; + struct dirent *dp; + DIR *dirp; + struct de de; + int truncated; + int ok = 1; + + if ((dirp = mg_opendir(conn, dir)) == NULL) { + return 0; + } else { + de.conn = conn; + + while ((dp = mg_readdir(dirp)) != NULL) { + /* Do not show current dir (but show hidden files as they will + * also be removed) */ + if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) { + continue; + } + + mg_snprintf( + conn, &truncated, path, sizeof(path), "%s/%s", dir, dp->d_name); + + /* If we don't memset stat structure to zero, mtime will have + * garbage and strftime() will segfault later on in + * print_dir_entry(). memset is required only if mg_stat() + * fails. For more details, see + * http://code.google.com/p/mongoose/issues/detail?id=79 */ + memset(&de.file, 0, sizeof(de.file)); + + if (truncated) { + /* Do not delete anything shorter */ + ok = 0; + continue; + } + + if (!mg_stat(conn, path, &de.file)) { + mg_cry(conn, + "%s: mg_stat(%s) failed: %s", + __func__, + path, + strerror(ERRNO)); + ok = 0; + } + + if (de.file.is_directory) { + if (remove_directory(conn, path) == 0) { + ok = 0; + } + } else { + /* This will fail file is the file is in memory */ + if (mg_remove(conn, path) == 0) { + ok = 0; + } + } + } + (void)mg_closedir(dirp); + + IGNORE_UNUSED_RESULT(rmdir(dir)); + } + + return ok; +} +#endif + + +struct dir_scan_data { + struct de *entries; + unsigned int num_entries; + unsigned int arr_size; +}; + + +/* Behaves like realloc(), but frees original pointer on failure */ +static void * +realloc2(void *ptr, size_t size) +{ + void *new_ptr = mg_realloc(ptr, size); + if (new_ptr == NULL) { + mg_free(ptr); + } + return new_ptr; +} + + +static int +dir_scan_callback(struct de *de, void *data) +{ + struct dir_scan_data *dsd = (struct dir_scan_data *)data; + + if ((dsd->entries == NULL) || (dsd->num_entries >= dsd->arr_size)) { + dsd->arr_size *= 2; + dsd->entries = + (struct de *)realloc2(dsd->entries, + dsd->arr_size * sizeof(dsd->entries[0])); + } + if (dsd->entries == NULL) { + /* TODO(lsm, low): propagate an error to the caller */ + dsd->num_entries = 0; + } else { + dsd->entries[dsd->num_entries].file_name = mg_strdup(de->file_name); + dsd->entries[dsd->num_entries].file = de->file; + dsd->entries[dsd->num_entries].conn = de->conn; + dsd->num_entries++; + } + + return 0; +} + + +static void +handle_directory_request(struct mg_connection *conn, const char *dir) +{ + unsigned int i; + int sort_direction; + struct dir_scan_data data = {NULL, 0, 128}; + char date[64]; + time_t curtime = time(NULL); + + if (!scan_directory(conn, dir, &data, dir_scan_callback)) { + mg_send_http_error(conn, + 500, + "Error: Cannot open directory\nopendir(%s): %s", + dir, + strerror(ERRNO)); + return; + } + + gmt_time_string(date, sizeof(date), &curtime); + + if (!conn) { + return; + } + + sort_direction = ((conn->request_info.query_string != NULL) + && (conn->request_info.query_string[1] == 'd')) + ? 'a' + : 'd'; + + conn->must_close = 1; + mg_printf(conn, "HTTP/1.1 200 OK\r\n"); + send_static_cache_header(conn); + send_additional_header(conn); + mg_printf(conn, + "Date: %s\r\n" + "Connection: close\r\n" + "Content-Type: text/html; charset=utf-8\r\n\r\n", + date); + mg_printf(conn, + "Index of %s" + "" + "

Index of %s

"
+	          ""
+	          ""
+	          ""
+	          "",
+	          conn->request_info.local_uri,
+	          conn->request_info.local_uri,
+	          sort_direction,
+	          sort_direction,
+	          sort_direction);
+
+	/* Print first entry - link to a parent directory */
+	mg_printf(conn,
+	          ""
+	          "\n",
+	          conn->request_info.local_uri,
+	          "..",
+	          "Parent directory",
+	          "-",
+	          "-");
+
+	/* Sort and print directory entries */
+	if (data.entries != NULL) {
+		qsort(data.entries,
+		      (size_t)data.num_entries,
+		      sizeof(data.entries[0]),
+		      compare_dir_entries);
+		for (i = 0; i < data.num_entries; i++) {
+			print_dir_entry(&data.entries[i]);
+			mg_free(data.entries[i].file_name);
+		}
+		mg_free(data.entries);
+	}
+
+	mg_printf(conn, "%s", "
NameModifiedSize

%s %s  %s
"); + conn->status_code = 200; +} + + +/* Send len bytes from the opened file to the client. */ +static void +send_file_data(struct mg_connection *conn, + struct mg_file *filep, + int64_t offset, + int64_t len) +{ + char buf[MG_BUF_LEN]; + int to_read, num_read, num_written; + int64_t size; + + if (!filep || !conn) { + return; + } + + /* Sanity check the offset */ + size = (filep->stat.size > INT64_MAX) ? INT64_MAX + : (int64_t)(filep->stat.size); + offset = (offset < 0) ? 0 : ((offset > size) ? size : offset); + + if ((len > 0) && (filep->access.membuf != NULL) && (size > 0)) { + /* file stored in memory */ + if (len > size - offset) { + len = size - offset; + } + mg_write(conn, filep->access.membuf + offset, (size_t)len); + } else if (len > 0 && filep->access.fp != NULL) { +/* file stored on disk */ +#if defined(__linux__) + /* sendfile is only available for Linux */ + if ((conn->ssl == 0) && (conn->throttle == 0) + && (!mg_strcasecmp(conn->ctx->config[ALLOW_SENDFILE_CALL], + "yes"))) { + off_t sf_offs = (off_t)offset; + ssize_t sf_sent; + int sf_file = fileno(filep->access.fp); + int loop_cnt = 0; + + do { + /* 2147479552 (0x7FFFF000) is a limit found by experiment on + * 64 bit Linux (2^31 minus one memory page of 4k?). */ + size_t sf_tosend = + (size_t)((len < 0x7FFFF000) ? len : 0x7FFFF000); + sf_sent = + sendfile(conn->client.sock, sf_file, &sf_offs, sf_tosend); + if (sf_sent > 0) { + len -= sf_sent; + offset += sf_sent; + } else if (loop_cnt == 0) { + /* This file can not be sent using sendfile. + * This might be the case for pseudo-files in the + * /sys/ and /proc/ file system. + * Use the regular user mode copy code instead. */ + break; + } else if (sf_sent == 0) { + /* No error, but 0 bytes sent. May be EOF? */ + return; + } + loop_cnt++; + + } while ((len > 0) && (sf_sent >= 0)); + + if (sf_sent > 0) { + return; /* OK */ + } + + /* sf_sent<0 means error, thus fall back to the classic way */ + /* This is always the case, if sf_file is not a "normal" file, + * e.g., for sending data from the output of a CGI process. */ + offset = (int64_t)sf_offs; + } +#endif + if ((offset > 0) && (fseeko(filep->access.fp, offset, SEEK_SET) != 0)) { + mg_cry(conn, "%s: fseeko() failed: %s", __func__, strerror(ERRNO)); + mg_send_http_error( + conn, + 500, + "%s", + "Error: Unable to access file at requested position."); + } else { + while (len > 0) { + /* Calculate how much to read from the file in the buffer */ + to_read = sizeof(buf); + if ((int64_t)to_read > len) { + to_read = (int)len; + } + + /* Read from file, exit the loop on error */ + if ((num_read = + (int)fread(buf, 1, (size_t)to_read, filep->access.fp)) + <= 0) { + break; + } + + /* Send read bytes to the client, exit the loop on error */ + if ((num_written = mg_write(conn, buf, (size_t)num_read)) + != num_read) { + break; + } + + /* Both read and were successful, adjust counters */ + len -= num_written; + } + } + } +} + + +static int +parse_range_header(const char *header, int64_t *a, int64_t *b) +{ + return sscanf(header, "bytes=%" INT64_FMT "-%" INT64_FMT, a, b); +} + + +static void +construct_etag(char *buf, size_t buf_len, const struct mg_file_stat *filestat) +{ + if ((filestat != NULL) && (buf != NULL)) { + mg_snprintf(NULL, + NULL, /* All calls to construct_etag use 64 byte buffer */ + buf, + buf_len, + "\"%lx.%" INT64_FMT "\"", + (unsigned long)filestat->last_modified, + filestat->size); + } +} + + +static void +fclose_on_exec(struct mg_file_access *filep, struct mg_connection *conn) +{ + if (filep != NULL && filep->fp != NULL) { +#ifdef _WIN32 + (void)conn; /* Unused. */ +#else + if (fcntl(fileno(filep->fp), F_SETFD, FD_CLOEXEC) != 0) { + mg_cry(conn, + "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s", + __func__, + strerror(ERRNO)); + } +#endif + } +} + + +static void +handle_static_file_request(struct mg_connection *conn, + const char *path, + struct mg_file *filep, + const char *mime_type, + const char *additional_headers) +{ + char date[64], lm[64], etag[64]; + char range[128]; /* large enough, so there will be no overflow */ + const char *msg = "OK", *hdr; + time_t curtime = time(NULL); + int64_t cl, r1, r2; + struct vec mime_vec; + int n, truncated; + char gz_path[PATH_MAX]; + const char *encoding = ""; + const char *cors1, *cors2, *cors3; + int allow_on_the_fly_compression; + + if ((conn == NULL) || (conn->ctx == NULL) || (filep == NULL)) { + return; + } + + if (mime_type == NULL) { + get_mime_type(conn->ctx, path, &mime_vec); + } else { + mime_vec.ptr = mime_type; + mime_vec.len = strlen(mime_type); + } + if (filep->stat.size > INT64_MAX) { + mg_send_http_error(conn, + 500, + "Error: File size is too large to send\n%" INT64_FMT, + filep->stat.size); + return; + } + cl = (int64_t)filep->stat.size; + conn->status_code = 200; + range[0] = '\0'; + + /* if this file is in fact a pre-gzipped file, rewrite its filename + * it's important to rewrite the filename after resolving + * the mime type from it, to preserve the actual file's type */ + allow_on_the_fly_compression = conn->accept_gzip; + + if (filep->stat.is_gzipped) { + mg_snprintf(conn, &truncated, gz_path, sizeof(gz_path), "%s.gz", path); + + if (truncated) { + mg_send_http_error(conn, + 500, + "Error: Path of zipped file too long (%s)", + path); + return; + } + + path = gz_path; + encoding = "Content-Encoding: gzip\r\n"; + + /* File is already compressed. No "on the fly" compression. */ + allow_on_the_fly_compression = 0; + } + + if (!mg_fopen(conn, path, MG_FOPEN_MODE_READ, filep)) { + mg_send_http_error(conn, + 500, + "Error: Cannot open file\nfopen(%s): %s", + path, + strerror(ERRNO)); + return; + } + + fclose_on_exec(&filep->access, conn); + + /* If Range: header specified, act accordingly */ + r1 = r2 = 0; + hdr = mg_get_header(conn, "Range"); + if ((hdr != NULL) && ((n = parse_range_header(hdr, &r1, &r2)) > 0) + && (r1 >= 0) && (r2 >= 0)) { + /* actually, range requests don't play well with a pre-gzipped + * file (since the range is specified in the uncompressed space) */ + if (filep->stat.is_gzipped) { + mg_send_http_error( + conn, + 416, /* 416 = Range Not Satisfiable */ + "%s", + "Error: Range requests in gzipped files are not supported"); + (void)mg_fclose( + &filep->access); /* ignore error on read only file */ + return; + } + conn->status_code = 206; + cl = (n == 2) ? (((r2 > cl) ? cl : r2) - r1 + 1) : (cl - r1); + mg_snprintf(conn, + NULL, /* range buffer is big enough */ + range, + sizeof(range), + "Content-Range: bytes " + "%" INT64_FMT "-%" INT64_FMT "/%" INT64_FMT "\r\n", + r1, + r1 + cl - 1, + filep->stat.size); + msg = "Partial Content"; + + /* Do not compress ranges. */ + allow_on_the_fly_compression = 0; + } + + hdr = mg_get_header(conn, "Origin"); + if (hdr) { + /* Cross-origin resource sharing (CORS), see + * http://www.html5rocks.com/en/tutorials/cors/, + * http://www.html5rocks.com/static/images/cors_server_flowchart.png + * - + * preflight is not supported for files. */ + cors1 = "Access-Control-Allow-Origin: "; + cors2 = conn->ctx->config[ACCESS_CONTROL_ALLOW_ORIGIN]; + cors3 = "\r\n"; + } else { + cors1 = cors2 = cors3 = ""; + } + + /* Prepare Etag, Date, Last-Modified headers. Must be in UTC, + * according to + * http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3 */ + gmt_time_string(date, sizeof(date), &curtime); + gmt_time_string(lm, sizeof(lm), &filep->stat.last_modified); + construct_etag(etag, sizeof(etag), &filep->stat); + + /* On the fly compression allowed */ + if (allow_on_the_fly_compression) { + ; + /* TODO: add interface to compression module */ + /* e.g., def from https://zlib.net/zlib_how.html */ + /* Check license (zlib has a permissive license, but */ + /* is still not MIT) and use dynamic binding like */ + /* done with OpenSSL */ + /* See #199 (https://github.com/civetweb/civetweb/issues/199) */ + } + + /* Send header */ + (void)mg_printf(conn, + "HTTP/1.1 %d %s\r\n" + "%s%s%s" + "Date: %s\r\n", + conn->status_code, + msg, + cors1, + cors2, + cors3, + date); + send_static_cache_header(conn); + send_additional_header(conn); + + (void)mg_printf(conn, + "Last-Modified: %s\r\n" + "Etag: %s\r\n" + "Content-Type: %.*s\r\n" + "Content-Length: %" INT64_FMT "\r\n" + "Connection: %s\r\n" + "Accept-Ranges: bytes\r\n" + "%s%s", + lm, + etag, + (int)mime_vec.len, + mime_vec.ptr, + cl, + suggest_connection_header(conn), + range, + encoding); + + /* The previous code must not add any header starting with X- to make + * sure no one of the additional_headers is included twice */ + + if (additional_headers != NULL) { + (void)mg_printf(conn, + "%.*s\r\n\r\n", + (int)strlen(additional_headers), + additional_headers); + } else { + (void)mg_printf(conn, "\r\n"); + } + + if (strcmp(conn->request_info.request_method, "HEAD") != 0) { + send_file_data(conn, filep, r1, cl); + } + (void)mg_fclose(&filep->access); /* ignore error on read only file */ +} + + +#if !defined(NO_CACHING) +static void +handle_not_modified_static_file_request(struct mg_connection *conn, + struct mg_file *filep) +{ + char date[64], lm[64], etag[64]; + time_t curtime = time(NULL); + + if ((conn == NULL) || (filep == NULL)) { + return; + } + conn->status_code = 304; + gmt_time_string(date, sizeof(date), &curtime); + gmt_time_string(lm, sizeof(lm), &filep->stat.last_modified); + construct_etag(etag, sizeof(etag), &filep->stat); + + (void)mg_printf(conn, + "HTTP/1.1 %d %s\r\n" + "Date: %s\r\n", + conn->status_code, + mg_get_response_code_text(conn, conn->status_code), + date); + send_static_cache_header(conn); + send_additional_header(conn); + (void)mg_printf(conn, + "Last-Modified: %s\r\n" + "Etag: %s\r\n" + "Connection: %s\r\n" + "\r\n", + lm, + etag, + suggest_connection_header(conn)); +} +#endif + + +void +mg_send_file(struct mg_connection *conn, const char *path) +{ + mg_send_mime_file(conn, path, NULL); +} + + +void +mg_send_mime_file(struct mg_connection *conn, + const char *path, + const char *mime_type) +{ + mg_send_mime_file2(conn, path, mime_type, NULL); +} + + +void +mg_send_mime_file2(struct mg_connection *conn, + const char *path, + const char *mime_type, + const char *additional_headers) +{ + struct mg_file file = STRUCT_FILE_INITIALIZER; + + if (!conn) { + /* No conn */ + return; + } + + if (mg_stat(conn, path, &file.stat)) { + if (file.stat.is_directory) { + if (!mg_strcasecmp(conn->ctx->config[ENABLE_DIRECTORY_LISTING], + "yes")) { + handle_directory_request(conn, path); + } else { + mg_send_http_error(conn, + 403, + "%s", + "Error: Directory listing denied"); + } + } else { + handle_static_file_request( + conn, path, &file, mime_type, additional_headers); + } + } else { + mg_send_http_error(conn, 404, "%s", "Error: File not found"); + } +} + + +/* For a given PUT path, create all intermediate subdirectories. + * Return 0 if the path itself is a directory. + * Return 1 if the path leads to a file. + * Return -1 for if the path is too long. + * Return -2 if path can not be created. +*/ +static int +put_dir(struct mg_connection *conn, const char *path) +{ + char buf[PATH_MAX]; + const char *s, *p; + struct mg_file file = STRUCT_FILE_INITIALIZER; + size_t len; + int res = 1; + + for (s = p = path + 2; (p = strchr(s, '/')) != NULL; s = ++p) { + len = (size_t)(p - path); + if (len >= sizeof(buf)) { + /* path too long */ + res = -1; + break; + } + memcpy(buf, path, len); + buf[len] = '\0'; + + /* Try to create intermediate directory */ + DEBUG_TRACE("mkdir(%s)", buf); + if (!mg_stat(conn, buf, &file.stat) && mg_mkdir(conn, buf, 0755) != 0) { + /* path does not exixt and can not be created */ + res = -2; + break; + } + + /* Is path itself a directory? */ + if (p[1] == '\0') { + res = 0; + } + } + + return res; +} + + +static void +remove_bad_file(const struct mg_connection *conn, const char *path) +{ + int r = mg_remove(conn, path); + if (r != 0) { + mg_cry(conn, "%s: Cannot remove invalid file %s", __func__, path); + } +} + + +long long +mg_store_body(struct mg_connection *conn, const char *path) +{ + char buf[MG_BUF_LEN]; + long long len = 0; + int ret, n; + struct mg_file fi; + + if (conn->consumed_content != 0) { + mg_cry(conn, "%s: Contents already consumed", __func__); + return -11; + } + + ret = put_dir(conn, path); + if (ret < 0) { + /* -1 for path too long, + * -2 for path can not be created. */ + return ret; + } + if (ret != 1) { + /* Return 0 means, path itself is a directory. */ + return 0; + } + + if (mg_fopen(conn, path, MG_FOPEN_MODE_WRITE, &fi) == 0) { + return -12; + } + + ret = mg_read(conn, buf, sizeof(buf)); + while (ret > 0) { + n = (int)fwrite(buf, 1, (size_t)ret, fi.access.fp); + if (n != ret) { + (void)mg_fclose( + &fi.access); /* File is bad and will be removed anyway. */ + remove_bad_file(conn, path); + return -13; + } + len += ret; + ret = mg_read(conn, buf, sizeof(buf)); + } + + /* File is open for writing. If fclose fails, there was probably an + * error flushing the buffer to disk, so the file on disk might be + * broken. Delete it and return an error to the caller. */ + if (mg_fclose(&fi.access) != 0) { + remove_bad_file(conn, path); + return -14; + } + + return len; +} + + +/* Parse a buffer: + * Forward the string pointer till the end of a word, then + * terminate it and forward till the begin of the next word. + */ +static int +skip_to_end_of_word_and_terminate(char **ppw, int eol) +{ + /* Forward until a space is found - use isgraph here */ + /* See http://www.cplusplus.com/reference/cctype/ */ + while (isgraph(**ppw)) { + (*ppw)++; + } + + /* Check end of word */ + if (eol) { + /* must be a end of line */ + if ((**ppw != '\r') && (**ppw != '\n')) { + return -1; + } + } else { + /* must be a end of a word, but not a line */ + if (**ppw != ' ') { + return -1; + } + } + + /* Terminate and forward to the next word */ + do { + **ppw = 0; + (*ppw)++; + } while ((**ppw) && isspace(**ppw)); + + /* Check after term */ + if (!eol) { + /* if it's not the end of line, there must be a next word */ + if (!isgraph(**ppw)) { + return -1; + } + } + + /* ok */ + return 1; +} + + +/* Parse HTTP headers from the given buffer, advance buf pointer + * to the point where parsing stopped. + * All parameters must be valid pointers (not NULL). + * Return <0 on error. */ +static int +parse_http_headers(char **buf, struct mg_header hdr[MG_MAX_HEADERS]) +{ + int i; + int num_headers = 0; + + for (i = 0; i < (int)MG_MAX_HEADERS; i++) { + char *dp = *buf; + while ((*dp != ':') && (*dp >= 33) && (*dp <= 126)) { + dp++; + } + if (dp == *buf) { + /* End of headers reached. */ + break; + } + if (*dp != ':') { + /* This is not a valid field. */ + return -1; + } + + /* End of header key (*dp == ':') */ + /* Truncate here and set the key name */ + *dp = 0; + hdr[i].name = *buf; + do { + dp++; + } while (*dp == ' '); + + /* The rest of the line is the value */ + hdr[i].value = dp; + *buf = dp + strcspn(dp, "\r\n"); + if (((*buf)[0] != '\r') || ((*buf)[1] != '\n')) { + *buf = NULL; + } + + num_headers = i + 1; + if (*buf) { + (*buf)[0] = 0; + (*buf)[1] = 0; + *buf += 2; + } else { + *buf = dp; + break; + } + + if ((*buf)[0] == '\r') { + /* This is the end of the header */ + break; + } + } + return num_headers; +} + + +struct mg_http_method_info { + const char *name; + int request_has_body; + int response_has_body; + int is_safe; + int is_idempotent; + int is_cacheable; +}; + + +/* https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods */ +static struct mg_http_method_info http_methods[] = { + /* HTTP (RFC 2616) */ + {"GET", 0, 1, 1, 1, 1}, + {"POST", 1, 1, 0, 0, 0}, + {"PUT", 1, 0, 0, 1, 0}, + {"DELETE", 0, 0, 0, 1, 0}, + {"HEAD", 0, 0, 1, 1, 1}, + {"OPTIONS", 0, 0, 1, 1, 0}, + {"CONNECT", 1, 1, 0, 0, 0}, + /* TRACE method (RFC 2616) is not supported for security reasons */ + + /* PATCH method (RFC 5789) */ + {"PATCH", 1, 0, 0, 0, 0}, + /* PATCH method only allowed for CGI/Lua/LSP and callbacks. */ + + /* WEBDAV (RFC 2518) */ + {"PROPFIND", 0, 1, 1, 1, 0}, + /* http://www.webdav.org/specs/rfc4918.html, 9.1: + * Some PROPFIND results MAY be cached, with care, + * as there is no cache validation mechanism for + * most properties. This method is both safe and + * idempotent (see Section 9.1 of [RFC2616]). */ + {"MKCOL", 0, 0, 0, 1, 0}, + /* http://www.webdav.org/specs/rfc4918.html, 9.1: + * When MKCOL is invoked without a request body, + * the newly created collection SHOULD have no + * members. A MKCOL request message may contain + * a message body. The precise behavior of a MKCOL + * request when the body is present is undefined, + * ... ==> We do not support MKCOL with body data. + * This method is idempotent, but not safe (see + * Section 9.1 of [RFC2616]). Responses to this + * method MUST NOT be cached. */ + + /* Unsupported WEBDAV Methods: */ + /* PROPPATCH, COPY, MOVE, LOCK, UNLOCK (RFC 2518) */ + /* + 11 methods from RFC 3253 */ + /* ORDERPATCH (RFC 3648) */ + /* ACL (RFC 3744) */ + /* SEARCH (RFC 5323) */ + /* + MicroSoft extensions + * https://msdn.microsoft.com/en-us/library/aa142917.aspx */ + + /* REPORT method (RFC 3253) */ + {"REPORT", 1, 1, 1, 1, 1}, + /* REPORT method only allowed for CGI/Lua/LSP and callbacks. */ + /* It was defined for WEBDAV in RFC 3253, Sec. 3.6 + * (https://tools.ietf.org/html/rfc3253#section-3.6), but seems + * to be useful for REST in case a "GET request with body" is + * required. */ + + {NULL, 0, 0, 0, 0, 0} + /* end of list */ +}; + + +static const struct mg_http_method_info * +get_http_method_info(const char *method) +{ + /* Check if the method is known to the server. The list of all known + * HTTP methods can be found here at + * http://www.iana.org/assignments/http-methods/http-methods.xhtml + */ + const struct mg_http_method_info *m = http_methods; + + while (m->name) { + if (!strcmp(m->name, method)) { + return m; + } + m++; + } + return m; +} + + +static int +is_valid_http_method(const char *method) +{ + return (get_http_method_info(method) != NULL); +} + + +/* Parse HTTP request, fill in mg_request_info structure. + * This function modifies the buffer by NUL-terminating + * HTTP request components, header names and header values. + * Parameters: + * buf (in/out): pointer to the HTTP header to parse and split + * len (in): length of HTTP header buffer + * re (out): parsed header as mg_request_info + * buf and ri must be valid pointers (not NULL), len>0. + * Returns <0 on error. */ +static int +parse_http_request(int check_method, char *buf, int len, struct mg_request_info *ri) +{ + int request_length; + int init_skip = 0; + + /* Reset attributes. DO NOT TOUCH is_ssl, remote_ip, remote_addr, + * remote_port */ + ri->remote_user = ri->request_method = ri->request_uri = ri->http_version = + NULL; + ri->num_headers = 0; + + /* RFC says that all initial whitespaces should be ingored */ + /* This included all leading \r and \n (isspace) */ + /* See table: http://www.cplusplus.com/reference/cctype/ */ + while ((len > 0) && isspace(*(unsigned char *)buf)) { + buf++; + len--; + init_skip++; + } + + if (len == 0) { + /* Incomplete request */ + return 0; + } + + /* Control characters are not allowed, including zero */ + if (iscntrl(*(unsigned char *)buf)) { + return -1; + } + + /* Find end of HTTP header */ + request_length = get_http_header_len(buf, len); + if (request_length <= 0) { + return request_length; + } + buf[request_length - 1] = '\0'; + + if ((*buf == 0) || (*buf == '\r') || (*buf == '\n')) { + return -1; + } + + /* The first word has to be the HTTP method */ + ri->request_method = buf; + + if (skip_to_end_of_word_and_terminate(&buf, 0) <= 0) { + return -1; + } + + /* Check for a valid http method */ + if (check_method && !is_valid_http_method(ri->request_method)) { + return -1; + } + + /* The second word is the URI */ + ri->request_uri = skip_quoted(&buf, " ", " ", 0); + + /* Next would be the HTTP version */ + ri->http_version = buf; + + if (skip_to_end_of_word_and_terminate(&buf, 1) <= 0) { + return -1; + } + + /* Check for a valid HTTP version key */ + if (strncmp(ri->http_version, "HTTP/", 5) != 0) { + /* Invalid request */ + return -1; + } + ri->http_version += 5; + + + /* Parse all HTTP headers */ + ri->num_headers = parse_http_headers(&buf, ri->http_headers); + if (ri->num_headers < 0) { + /* Error while parsing headers */ + return -1; + } + + return request_length + init_skip; +} + + +static int +parse_http_response(char *buf, int len, struct mg_response_info *ri) +{ + int response_length; + int init_skip = 0; + char *tmp, *tmp2; + long l; + + /* Initialize elements. */ + ri->http_version = ri->status_text = NULL; + ri->num_headers = ri->status_code = 0; + + /* RFC says that all initial whitespaces should be ingored */ + /* This included all leading \r and \n (isspace) */ + /* See table: http://www.cplusplus.com/reference/cctype/ */ + while ((len > 0) && isspace(*(unsigned char *)buf)) { + buf++; + len--; + init_skip++; + } + + if (len == 0) { + /* Incomplete request */ + return 0; + } + + /* Control characters are not allowed, including zero */ + if (iscntrl(*(unsigned char *)buf)) { + return -1; + } + + /* Find end of HTTP header */ + response_length = get_http_header_len(buf, len); + if (response_length <= 0) { + return response_length; + } + buf[response_length - 1] = '\0'; + + + /* TODO: Define mg_response_info and implement parsing */ + (void)buf; + (void)len; + (void)ri; + + /* RFC says that all initial whitespaces should be ingored */ + while ((*buf != '\0') && isspace(*(unsigned char *)buf)) { + buf++; + } + if ((*buf == 0) || (*buf == '\r') || (*buf == '\n')) { + return -1; + } + + /* The first word is the HTTP version */ + /* Check for a valid HTTP version key */ + if (strncmp(buf, "HTTP/", 5) != 0) { + /* Invalid request */ + return -1; + } + buf += 5; + if (!isgraph(buf[0])) { + /* Invalid request */ + return -1; + } + ri->http_version = buf; + + if (skip_to_end_of_word_and_terminate(&buf, 0) <= 0) { + return -1; + } + + /* The second word is the status as a number */ + tmp = buf; + + if (skip_to_end_of_word_and_terminate(&buf, 0) <= 0) { + return -1; + } + + l = strtol(tmp, &tmp2, 10); + if ((l < 100) || (l >= 1000) || ((tmp2 - tmp) != 3) || (*tmp2 != 0)) { + /* Everything else but a 3 digit code is invalid */ + return -1; + } + ri->status_code = (int)l; + + /* The rest of the line is the status text */ + ri->status_text = buf; + + /* Find end of status text */ + /* isgraph or isspace = isprint */ + while (isprint(*buf)) { + buf++; + } + if ((*buf != '\r') && (*buf != '\n')) { + return -1; + } + /* Terminate string and forward buf to next line */ + do { + *buf = 0; + buf++; + } while ((*buf) && isspace(*buf)); + + + /* Parse all HTTP headers */ + ri->num_headers = parse_http_headers(&buf, ri->http_headers); + if (ri->num_headers < 0) { + /* Error while parsing headers */ + return -1; + } + + return response_length + init_skip; +} + + +/* Keep reading the input (either opened file descriptor fd, or socket sock, + * or SSL descriptor ssl) into buffer buf, until \r\n\r\n appears in the + * buffer (which marks the end of HTTP request). Buffer buf may already + * have some data. The length of the data is stored in nread. + * Upon every read operation, increase nread by the number of bytes read. */ +static int +read_message(FILE *fp, + struct mg_connection *conn, + char *buf, + int bufsiz, + int *nread) +{ + int request_len, n = 0; + struct timespec last_action_time; + double request_timeout; + + if (!conn) { + return 0; + } + + memset(&last_action_time, 0, sizeof(last_action_time)); + + if (conn->ctx->config[REQUEST_TIMEOUT]) { + /* value of request_timeout is in seconds, config in milliseconds */ + request_timeout = atof(conn->ctx->config[REQUEST_TIMEOUT]) / 1000.0; + } else { + request_timeout = -1.0; + } + if (conn->handled_requests > 0) { + if (conn->ctx->config[KEEP_ALIVE_TIMEOUT]) { + request_timeout = + atof(conn->ctx->config[KEEP_ALIVE_TIMEOUT]) / 1000.0; + } + } + + request_len = get_http_header_len(buf, *nread); + + /* first time reading from this connection */ + clock_gettime(CLOCK_MONOTONIC, &last_action_time); + + while (request_len == 0) { + /* Full request not yet received */ + if (conn->ctx->stop_flag != 0) { + /* Server is to be stopped. */ + return -1; + } + + if (*nread >= bufsiz) { + /* Request too long */ + return -2; + } + + n = pull_inner( + fp, conn, buf + *nread, bufsiz - *nread, request_timeout); + if (n == -2) { + /* Receive error */ + return -1; + } + if (n > 0) { + *nread += n; + request_len = get_http_header_len(buf, *nread); + } else { + request_len = 0; + } + + if ((request_len == 0) && (request_timeout >= 0)) { + if (mg_difftimespec(&last_action_time, &(conn->req_time)) + > request_timeout) { + /* Timeout */ + return -1; + } + clock_gettime(CLOCK_MONOTONIC, &last_action_time); + } + } + + return request_len; +} + + +#if !defined(NO_CACHING) +/* Return True if we should reply 304 Not Modified. */ +static int +is_not_modified(const struct mg_connection *conn, + const struct mg_file_stat *filestat) +{ + char etag[64]; + const char *ims = mg_get_header(conn, "If-Modified-Since"); + const char *inm = mg_get_header(conn, "If-None-Match"); + construct_etag(etag, sizeof(etag), filestat); + + return ((inm != NULL) && !mg_strcasecmp(etag, inm)) + || ((ims != NULL) + && (filestat->last_modified <= parse_date_string(ims))); +} +#endif /* !NO_CACHING */ + + +#if !defined(NO_CGI) || !defined(NO_FILES) +static int +forward_body_data(struct mg_connection *conn, FILE *fp, SOCKET sock, SSL *ssl) +{ + const char *expect, *body; + char buf[MG_BUF_LEN]; + int to_read, nread, success = 0; + int64_t buffered_len; + double timeout = -1.0; + + if (!conn) { + return 0; + } + if (conn->ctx->config[REQUEST_TIMEOUT]) { + timeout = atoi(conn->ctx->config[REQUEST_TIMEOUT]) / 1000.0; + } + + expect = mg_get_header(conn, "Expect"); + /* assert(fp != NULL); */ + if (!fp) { + mg_send_http_error(conn, 500, "%s", "Error: NULL File"); + return 0; + } + + if ((conn->content_len == -1) && (!conn->is_chunked)) { + /* Content length is not specified by the client. */ + mg_send_http_error(conn, + 411, + "%s", + "Error: Client did not specify content length"); + } else if ((expect != NULL) + && (mg_strcasecmp(expect, "100-continue") != 0)) { + /* Client sent an "Expect: xyz" header and xyz is not 100-continue. + */ + mg_send_http_error(conn, + 417, + "Error: Can not fulfill expectation %s", + expect); + } else { + if (expect != NULL) { + (void)mg_printf(conn, "%s", "HTTP/1.1 100 Continue\r\n\r\n"); + conn->status_code = 100; + } else { + conn->status_code = 200; + } + + buffered_len = (int64_t)(conn->data_len) - (int64_t)conn->request_len + - conn->consumed_content; + + /* assert(buffered_len >= 0); */ + /* assert(conn->consumed_content == 0); */ + + if ((buffered_len < 0) || (conn->consumed_content != 0)) { + mg_send_http_error(conn, 500, "%s", "Error: Size mismatch"); + return 0; + } + + if (buffered_len > 0) { + if ((int64_t)buffered_len > conn->content_len) { + buffered_len = (int)conn->content_len; + } + body = conn->buf + conn->request_len + conn->consumed_content; + push_all(conn->ctx, fp, sock, ssl, body, (int64_t)buffered_len); + conn->consumed_content += buffered_len; + } + + nread = 0; + while (conn->consumed_content < conn->content_len) { + to_read = sizeof(buf); + if ((int64_t)to_read > conn->content_len - conn->consumed_content) { + to_read = (int)(conn->content_len - conn->consumed_content); + } + nread = pull_inner(NULL, conn, buf, to_read, timeout); + if (nread == -2) { + /* error */ + break; + } + if (nread > 0) { + if (push_all(conn->ctx, fp, sock, ssl, buf, nread) != nread) { + break; + } + } + conn->consumed_content += nread; + } + + if (conn->consumed_content == conn->content_len) { + success = (nread >= 0); + } + + /* Each error code path in this function must send an error */ + if (!success) { + /* NOTE: Maybe some data has already been sent. */ + /* TODO (low): If some data has been sent, a correct error + * reply can no longer be sent, so just close the connection */ + mg_send_http_error(conn, 500, "%s", ""); + } + } + + return success; +} +#endif + +#if !defined(NO_CGI) +/* This structure helps to create an environment for the spawned CGI + * program. + * Environment is an array of "VARIABLE=VALUE\0" ASCIIZ strings, + * last element must be NULL. + * However, on Windows there is a requirement that all these + * VARIABLE=VALUE\0 + * strings must reside in a contiguous buffer. The end of the buffer is + * marked by two '\0' characters. + * We satisfy both worlds: we create an envp array (which is vars), all + * entries are actually pointers inside buf. */ +struct cgi_environment { + struct mg_connection *conn; + /* Data block */ + char *buf; /* Environment buffer */ + size_t buflen; /* Space available in buf */ + size_t bufused; /* Space taken in buf */ + /* Index block */ + char **var; /* char **envp */ + size_t varlen; /* Number of variables available in var */ + size_t varused; /* Number of variables stored in var */ +}; + + +static void addenv(struct cgi_environment *env, + PRINTF_FORMAT_STRING(const char *fmt), + ...) PRINTF_ARGS(2, 3); + +/* Append VARIABLE=VALUE\0 string to the buffer, and add a respective + * pointer into the vars array. Assumes env != NULL and fmt != NULL. */ +static void +addenv(struct cgi_environment *env, const char *fmt, ...) +{ + size_t n, space; + int truncated = 0; + char *added; + va_list ap; + + /* Calculate how much space is left in the buffer */ + space = (env->buflen - env->bufused); + + /* Calculate an estimate for the required space */ + n = strlen(fmt) + 2 + 128; + + do { + if (space <= n) { + /* Allocate new buffer */ + n = env->buflen + CGI_ENVIRONMENT_SIZE; + added = (char *)mg_realloc_ctx(env->buf, n, env->conn->ctx); + if (!added) { + /* Out of memory */ + mg_cry(env->conn, + "%s: Cannot allocate memory for CGI variable [%s]", + __func__, + fmt); + return; + } + env->buf = added; + env->buflen = n; + space = (env->buflen - env->bufused); + } + + /* Make a pointer to the free space int the buffer */ + added = env->buf + env->bufused; + + /* Copy VARIABLE=VALUE\0 string into the free space */ + va_start(ap, fmt); + mg_vsnprintf(env->conn, &truncated, added, (size_t)space, fmt, ap); + va_end(ap); + + /* Do not add truncated strings to the environment */ + if (truncated) { + /* Reallocate the buffer */ + space = 0; + n = 1; + } + } while (truncated); + + /* Calculate number of bytes added to the environment */ + n = strlen(added) + 1; + env->bufused += n; + + /* Now update the variable index */ + space = (env->varlen - env->varused); + if (space < 2) { + mg_cry(env->conn, + "%s: Cannot register CGI variable [%s]", + __func__, + fmt); + return; + } + + /* Append a pointer to the added string into the envp array */ + env->var[env->varused] = added; + env->varused++; +} + +/* Return 0 on success, non-zero if an error occurs. */ + +static int +prepare_cgi_environment(struct mg_connection *conn, + const char *prog, + struct cgi_environment *env) +{ + const char *s; + struct vec var_vec; + char *p, src_addr[IP_ADDR_STR_LEN], http_var_name[128]; + int i, truncated, uri_len; + + if ((conn == NULL) || (prog == NULL) || (env == NULL)) { + return -1; + } + + env->conn = conn; + env->buflen = CGI_ENVIRONMENT_SIZE; + env->bufused = 0; + env->buf = (char *)mg_malloc_ctx(env->buflen, conn->ctx); + if (env->buf == NULL) { + mg_cry(conn, + "%s: Not enough memory for environmental buffer", + __func__); + return -1; + } + env->varlen = MAX_CGI_ENVIR_VARS; + env->varused = 0; + env->var = (char **)mg_malloc_ctx(env->buflen * sizeof(char *), conn->ctx); + if (env->var == NULL) { + mg_cry(conn, + "%s: Not enough memory for environmental variables", + __func__); + mg_free(env->buf); + return -1; + } + + addenv(env, "SERVER_NAME=%s", conn->ctx->config[AUTHENTICATION_DOMAIN]); + addenv(env, "SERVER_ROOT=%s", conn->ctx->config[DOCUMENT_ROOT]); + addenv(env, "DOCUMENT_ROOT=%s", conn->ctx->config[DOCUMENT_ROOT]); + addenv(env, "SERVER_SOFTWARE=%s/%s", "Civetweb", mg_version()); + + /* Prepare the environment block */ + addenv(env, "%s", "GATEWAY_INTERFACE=CGI/1.1"); + addenv(env, "%s", "SERVER_PROTOCOL=HTTP/1.1"); + addenv(env, "%s", "REDIRECT_STATUS=200"); /* For PHP */ + +#if defined(USE_IPV6) + if (conn->client.lsa.sa.sa_family == AF_INET6) { + addenv(env, "SERVER_PORT=%d", ntohs(conn->client.lsa.sin6.sin6_port)); + } else +#endif + { + addenv(env, "SERVER_PORT=%d", ntohs(conn->client.lsa.sin.sin_port)); + } + + sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa); + addenv(env, "REMOTE_ADDR=%s", src_addr); + + addenv(env, "REQUEST_METHOD=%s", conn->request_info.request_method); + addenv(env, "REMOTE_PORT=%d", conn->request_info.remote_port); + + addenv(env, "REQUEST_URI=%s", conn->request_info.request_uri); + addenv(env, "LOCAL_URI=%s", conn->request_info.local_uri); + + /* SCRIPT_NAME */ + uri_len = (int)strlen(conn->request_info.local_uri); + if (conn->path_info == NULL) { + if (conn->request_info.local_uri[uri_len - 1] != '/') { + /* URI: /path_to_script/script.cgi */ + addenv(env, "SCRIPT_NAME=%s", conn->request_info.local_uri); + } else { + /* URI: /path_to_script/ ... using index.cgi */ + const char *index_file = strrchr(prog, '/'); + if (index_file) { + addenv(env, + "SCRIPT_NAME=%s%s", + conn->request_info.local_uri, + index_file + 1); + } + } + } else { + /* URI: /path_to_script/script.cgi/path_info */ + addenv(env, + "SCRIPT_NAME=%.*s", + uri_len - (int)strlen(conn->path_info), + conn->request_info.local_uri); + } + + addenv(env, "SCRIPT_FILENAME=%s", prog); + if (conn->path_info == NULL) { + addenv(env, "PATH_TRANSLATED=%s", conn->ctx->config[DOCUMENT_ROOT]); + } else { + addenv(env, + "PATH_TRANSLATED=%s%s", + conn->ctx->config[DOCUMENT_ROOT], + conn->path_info); + } + + addenv(env, "HTTPS=%s", (conn->ssl == NULL) ? "off" : "on"); + + if ((s = mg_get_header(conn, "Content-Type")) != NULL) { + addenv(env, "CONTENT_TYPE=%s", s); + } + if (conn->request_info.query_string != NULL) { + addenv(env, "QUERY_STRING=%s", conn->request_info.query_string); + } + if ((s = mg_get_header(conn, "Content-Length")) != NULL) { + addenv(env, "CONTENT_LENGTH=%s", s); + } + if ((s = getenv("PATH")) != NULL) { + addenv(env, "PATH=%s", s); + } + if (conn->path_info != NULL) { + addenv(env, "PATH_INFO=%s", conn->path_info); + } + + if (conn->status_code > 0) { + /* CGI error handler should show the status code */ + addenv(env, "STATUS=%d", conn->status_code); + } + +#if defined(_WIN32) + if ((s = getenv("COMSPEC")) != NULL) { + addenv(env, "COMSPEC=%s", s); + } + if ((s = getenv("SYSTEMROOT")) != NULL) { + addenv(env, "SYSTEMROOT=%s", s); + } + if ((s = getenv("SystemDrive")) != NULL) { + addenv(env, "SystemDrive=%s", s); + } + if ((s = getenv("ProgramFiles")) != NULL) { + addenv(env, "ProgramFiles=%s", s); + } + if ((s = getenv("ProgramFiles(x86)")) != NULL) { + addenv(env, "ProgramFiles(x86)=%s", s); + } +#else + if ((s = getenv("LD_LIBRARY_PATH")) != NULL) { + addenv(env, "LD_LIBRARY_PATH=%s", s); + } +#endif /* _WIN32 */ + + if ((s = getenv("PERLLIB")) != NULL) { + addenv(env, "PERLLIB=%s", s); + } + + if (conn->request_info.remote_user != NULL) { + addenv(env, "REMOTE_USER=%s", conn->request_info.remote_user); + addenv(env, "%s", "AUTH_TYPE=Digest"); + } + + /* Add all headers as HTTP_* variables */ + for (i = 0; i < conn->request_info.num_headers; i++) { + + (void)mg_snprintf(conn, + &truncated, + http_var_name, + sizeof(http_var_name), + "HTTP_%s", + conn->request_info.http_headers[i].name); + + if (truncated) { + mg_cry(conn, + "%s: HTTP header variable too long [%s]", + __func__, + conn->request_info.http_headers[i].name); + continue; + } + + /* Convert variable name into uppercase, and change - to _ */ + for (p = http_var_name; *p != '\0'; p++) { + if (*p == '-') { + *p = '_'; + } + *p = (char)toupper(*(unsigned char *)p); + } + + addenv(env, + "%s=%s", + http_var_name, + conn->request_info.http_headers[i].value); + } + + /* Add user-specified variables */ + s = conn->ctx->config[CGI_ENVIRONMENT]; + while ((s = next_option(s, &var_vec, NULL)) != NULL) { + addenv(env, "%.*s", (int)var_vec.len, var_vec.ptr); + } + + env->var[env->varused] = NULL; + env->buf[env->bufused] = '\0'; + + return 0; +} + + +static void +handle_cgi_request(struct mg_connection *conn, const char *prog) +{ + char *buf; + size_t buflen; + int headers_len, data_len, i, truncated; + int fdin[2] = {-1, -1}, fdout[2] = {-1, -1}, fderr[2] = {-1, -1}; + const char *status, *status_text, *connection_state; + char *pbuf, dir[PATH_MAX], *p; + struct mg_request_info ri; + struct cgi_environment blk; + FILE *in = NULL, *out = NULL, *err = NULL; + struct mg_file fout = STRUCT_FILE_INITIALIZER; + pid_t pid = (pid_t)-1; + + if (conn == NULL) { + return; + } + + buf = NULL; + buflen = 16384; + i = prepare_cgi_environment(conn, prog, &blk); + if (i != 0) { + blk.buf = NULL; + blk.var = NULL; + goto done; + } + + /* CGI must be executed in its own directory. 'dir' must point to the + * directory containing executable program, 'p' must point to the + * executable program name relative to 'dir'. */ + (void)mg_snprintf(conn, &truncated, dir, sizeof(dir), "%s", prog); + + if (truncated) { + mg_cry(conn, "Error: CGI program \"%s\": Path too long", prog); + mg_send_http_error(conn, 500, "Error: %s", "CGI path too long"); + goto done; + } + + if ((p = strrchr(dir, '/')) != NULL) { + *p++ = '\0'; + } else { + dir[0] = '.'; + dir[1] = '\0'; + p = (char *)prog; + } + + if ((pipe(fdin) != 0) || (pipe(fdout) != 0) || (pipe(fderr) != 0)) { + status = strerror(ERRNO); + mg_cry(conn, + "Error: CGI program \"%s\": Can not create CGI pipes: %s", + prog, + status); + mg_send_http_error(conn, + 500, + "Error: Cannot create CGI pipe: %s", + status); + goto done; + } + + DEBUG_TRACE("CGI: spawn %s %s\n", dir, p); + pid = spawn_process(conn, p, blk.buf, blk.var, fdin, fdout, fderr, dir); + + if (pid == (pid_t)-1) { + status = strerror(ERRNO); + mg_cry(conn, + "Error: CGI program \"%s\": Can not spawn CGI process: %s", + prog, + status); + mg_send_http_error(conn, + 500, + "Error: Cannot spawn CGI process [%s]: %s", + prog, + status); + goto done; + } + + /* Make sure child closes all pipe descriptors. It must dup them to 0,1 + */ + set_close_on_exec((SOCKET)fdin[0], conn); /* stdin read */ + set_close_on_exec((SOCKET)fdout[1], conn); /* stdout write */ + set_close_on_exec((SOCKET)fderr[1], conn); /* stderr write */ + set_close_on_exec((SOCKET)fdin[1], conn); /* stdin write */ + set_close_on_exec((SOCKET)fdout[0], conn); /* stdout read */ + set_close_on_exec((SOCKET)fderr[0], conn); /* stderr read */ + + /* Parent closes only one side of the pipes. + * If we don't mark them as closed, close() attempt before + * return from this function throws an exception on Windows. + * Windows does not like when closed descriptor is closed again. */ + (void)close(fdin[0]); + (void)close(fdout[1]); + (void)close(fderr[1]); + fdin[0] = fdout[1] = fderr[1] = -1; + + if ((in = fdopen(fdin[1], "wb")) == NULL) { + status = strerror(ERRNO); + mg_cry(conn, + "Error: CGI program \"%s\": Can not open stdin: %s", + prog, + status); + mg_send_http_error(conn, + 500, + "Error: CGI can not open fdin\nfopen: %s", + status); + goto done; + } + + if ((out = fdopen(fdout[0], "rb")) == NULL) { + status = strerror(ERRNO); + mg_cry(conn, + "Error: CGI program \"%s\": Can not open stdout: %s", + prog, + status); + mg_send_http_error(conn, + 500, + "Error: CGI can not open fdout\nfopen: %s", + status); + goto done; + } + + if ((err = fdopen(fderr[0], "rb")) == NULL) { + status = strerror(ERRNO); + mg_cry(conn, + "Error: CGI program \"%s\": Can not open stderr: %s", + prog, + status); + mg_send_http_error(conn, + 500, + "Error: CGI can not open fdout\nfopen: %s", + status); + goto done; + } + + setbuf(in, NULL); + setbuf(out, NULL); + setbuf(err, NULL); + fout.access.fp = out; + + if ((conn->request_info.content_length != 0) || (conn->is_chunked)) { + DEBUG_TRACE("CGI: send body data (%lli)\n", + (signed long long)conn->request_info.content_length); + + /* This is a POST/PUT request, or another request with body data. */ + if (!forward_body_data(conn, in, INVALID_SOCKET, NULL)) { + /* Error sending the body data */ + mg_cry(conn, + "Error: CGI program \"%s\": Forward body data failed", + prog); + goto done; + } + } + + /* Close so child gets an EOF. */ + fclose(in); + in = NULL; + fdin[1] = -1; + + /* Now read CGI reply into a buffer. We need to set correct + * status code, thus we need to see all HTTP headers first. + * Do not send anything back to client, until we buffer in all + * HTTP headers. */ + data_len = 0; + buf = (char *)mg_malloc_ctx(buflen, conn->ctx); + if (buf == NULL) { + mg_send_http_error(conn, + 500, + "Error: Not enough memory for CGI buffer (%u bytes)", + (unsigned int)buflen); + mg_cry(conn, + "Error: CGI program \"%s\": Not enough memory for buffer (%u " + "bytes)", + prog, + (unsigned int)buflen); + goto done; + } + + DEBUG_TRACE("CGI: %s", "wait for response"); + headers_len = read_message(out, conn, buf, (int)buflen, &data_len); + DEBUG_TRACE("CGI: response: %li", (signed long)headers_len); + + if (headers_len <= 0) { + + /* Could not parse the CGI response. Check if some error message on + * stderr. */ + i = pull_all(err, conn, buf, (int)buflen); + if (i > 0) { + mg_cry(conn, + "Error: CGI program \"%s\" sent error " + "message: [%.*s]", + prog, + i, + buf); + mg_send_http_error(conn, + 500, + "Error: CGI program \"%s\" sent error " + "message: [%.*s]", + prog, + i, + buf); + } else { + mg_cry(conn, + "Error: CGI program sent malformed or too big " + "(>%u bytes) HTTP headers: [%.*s]", + (unsigned)buflen, + data_len, + buf); + + mg_send_http_error(conn, + 500, + "Error: CGI program sent malformed or too big " + "(>%u bytes) HTTP headers: [%.*s]", + (unsigned)buflen, + data_len, + buf); + } + + goto done; + } + + pbuf = buf; + buf[headers_len - 1] = '\0'; + ri.num_headers = parse_http_headers(&pbuf, ri.http_headers); + + /* Make up and send the status line */ + status_text = "OK"; + if ((status = get_header(ri.http_headers, ri.num_headers, "Status")) + != NULL) { + conn->status_code = atoi(status); + status_text = status; + while (isdigit(*(const unsigned char *)status_text) + || *status_text == ' ') { + status_text++; + } + } else if (get_header(ri.http_headers, ri.num_headers, "Location") + != NULL) { + conn->status_code = 302; + } else { + conn->status_code = 200; + } + connection_state = + get_header(ri.http_headers, ri.num_headers, "Connection"); + if (!header_has_option(connection_state, "keep-alive")) { + conn->must_close = 1; + } + + DEBUG_TRACE("CGI: response %u %s", conn->status_code, status_text); + + (void)mg_printf(conn, "HTTP/1.1 %d %s\r\n", conn->status_code, status_text); + + /* Send headers */ + for (i = 0; i < ri.num_headers; i++) { + mg_printf(conn, + "%s: %s\r\n", + ri.http_headers[i].name, + ri.http_headers[i].value); + } + mg_write(conn, "\r\n", 2); + + /* Send chunk of data that may have been read after the headers */ + mg_write(conn, buf + headers_len, (size_t)(data_len - headers_len)); + + /* Read the rest of CGI output and send to the client */ + send_file_data(conn, &fout, 0, INT64_MAX); + + DEBUG_TRACE("CGI: %s", "all data sent"); + +done: + mg_free(blk.var); + mg_free(blk.buf); + + if (pid != (pid_t)-1) { + kill(pid, SIGKILL); +#if !defined(_WIN32) + { + int st; + while (waitpid(pid, &st, 0) != -1) + ; /* clean zombies */ + } +#endif + } + if (fdin[0] != -1) { + close(fdin[0]); + } + if (fdout[1] != -1) { + close(fdout[1]); + } + + if (in != NULL) { + fclose(in); + } else if (fdin[1] != -1) { + close(fdin[1]); + } + + if (out != NULL) { + fclose(out); + } else if (fdout[0] != -1) { + close(fdout[0]); + } + + if (err != NULL) { + fclose(err); + } else if (fderr[0] != -1) { + close(fderr[0]); + } + + if (buf != NULL) { + mg_free(buf); + } +} +#endif /* !NO_CGI */ + + +#if !defined(NO_FILES) +static void +mkcol(struct mg_connection *conn, const char *path) +{ + int rc, body_len; + struct de de; + char date[64]; + time_t curtime = time(NULL); + + if (conn == NULL) { + return; + } + + /* TODO (mid): Check the mg_send_http_error situations in this function + */ + + memset(&de.file, 0, sizeof(de.file)); + if (!mg_stat(conn, path, &de.file)) { + mg_cry(conn, + "%s: mg_stat(%s) failed: %s", + __func__, + path, + strerror(ERRNO)); + } + + if (de.file.last_modified) { + /* TODO (mid): This check does not seem to make any sense ! */ + /* TODO (mid): Add a webdav unit test first, before changing + * anything here. */ + mg_send_http_error( + conn, 405, "Error: mkcol(%s): %s", path, strerror(ERRNO)); + return; + } + + body_len = conn->data_len - conn->request_len; + if (body_len > 0) { + mg_send_http_error( + conn, 415, "Error: mkcol(%s): %s", path, strerror(ERRNO)); + return; + } + + rc = mg_mkdir(conn, path, 0755); + + if (rc == 0) { + conn->status_code = 201; + gmt_time_string(date, sizeof(date), &curtime); + mg_printf(conn, + "HTTP/1.1 %d Created\r\n" + "Date: %s\r\n", + conn->status_code, + date); + send_static_cache_header(conn); + send_additional_header(conn); + mg_printf(conn, + "Content-Length: 0\r\n" + "Connection: %s\r\n\r\n", + suggest_connection_header(conn)); + } else if (rc == -1) { + if (errno == EEXIST) { + mg_send_http_error( + conn, 405, "Error: mkcol(%s): %s", path, strerror(ERRNO)); + } else if (errno == EACCES) { + mg_send_http_error( + conn, 403, "Error: mkcol(%s): %s", path, strerror(ERRNO)); + } else if (errno == ENOENT) { + mg_send_http_error( + conn, 409, "Error: mkcol(%s): %s", path, strerror(ERRNO)); + } else { + mg_send_http_error( + conn, 500, "fopen(%s): %s", path, strerror(ERRNO)); + } + } +} + + +static void +put_file(struct mg_connection *conn, const char *path) +{ + struct mg_file file = STRUCT_FILE_INITIALIZER; + const char *range; + int64_t r1, r2; + int rc; + char date[64]; + time_t curtime = time(NULL); + + if (conn == NULL) { + return; + } + + if (mg_stat(conn, path, &file.stat)) { + /* File already exists */ + conn->status_code = 200; + + if (file.stat.is_directory) { + /* This is an already existing directory, + * so there is nothing to do for the server. */ + rc = 0; + + } else { + /* File exists and is not a directory. */ + /* Can it be replaced? */ + + if (file.access.membuf != NULL) { + /* This is an "in-memory" file, that can not be replaced */ + mg_send_http_error(conn, + 405, + "Error: Put not possible\nReplacing %s " + "is not supported", + path); + return; + } + + /* Check if the server may write this file */ + if (access(path, W_OK) == 0) { + /* Access granted */ + conn->status_code = 200; + rc = 1; + } else { + mg_send_http_error( + conn, + 403, + "Error: Put not possible\nReplacing %s is not allowed", + path); + return; + } + } + } else { + /* File should be created */ + conn->status_code = 201; + rc = put_dir(conn, path); + } + + if (rc == 0) { + /* put_dir returns 0 if path is a directory */ + gmt_time_string(date, sizeof(date), &curtime); + mg_printf(conn, + "HTTP/1.1 %d %s\r\n", + conn->status_code, + mg_get_response_code_text(NULL, conn->status_code)); + send_no_cache_header(conn); + send_additional_header(conn); + mg_printf(conn, + "Date: %s\r\n" + "Content-Length: 0\r\n" + "Connection: %s\r\n\r\n", + date, + suggest_connection_header(conn)); + + /* Request to create a directory has been fulfilled successfully. + * No need to put a file. */ + return; + } + + if (rc == -1) { + /* put_dir returns -1 if the path is too long */ + mg_send_http_error(conn, + 414, + "Error: Path too long\nput_dir(%s): %s", + path, + strerror(ERRNO)); + return; + } + + if (rc == -2) { + /* put_dir returns -2 if the directory can not be created */ + mg_send_http_error(conn, + 500, + "Error: Can not create directory\nput_dir(%s): %s", + path, + strerror(ERRNO)); + return; + } + + /* A file should be created or overwritten. */ + /* Currently CivetWeb does not nead read+write access. */ + if (!mg_fopen(conn, path, MG_FOPEN_MODE_WRITE, &file) + || file.access.fp == NULL) { + (void)mg_fclose(&file.access); + mg_send_http_error(conn, + 500, + "Error: Can not create file\nfopen(%s): %s", + path, + strerror(ERRNO)); + return; + } + + fclose_on_exec(&file.access, conn); + range = mg_get_header(conn, "Content-Range"); + r1 = r2 = 0; + if ((range != NULL) && parse_range_header(range, &r1, &r2) > 0) { + conn->status_code = 206; /* Partial content */ + fseeko(file.access.fp, r1, SEEK_SET); + } + + if (!forward_body_data(conn, file.access.fp, INVALID_SOCKET, NULL)) { + /* forward_body_data failed. + * The error code has already been sent to the client, + * and conn->status_code is already set. */ + (void)mg_fclose(&file.access); + return; + } + + if (mg_fclose(&file.access) != 0) { + /* fclose failed. This might have different reasons, but a likely + * one is "no space on disk", http 507. */ + conn->status_code = 507; + } + + gmt_time_string(date, sizeof(date), &curtime); + mg_printf(conn, + "HTTP/1.1 %d %s\r\n", + conn->status_code, + mg_get_response_code_text(NULL, conn->status_code)); + send_no_cache_header(conn); + send_additional_header(conn); + mg_printf(conn, + "Date: %s\r\n" + "Content-Length: 0\r\n" + "Connection: %s\r\n\r\n", + date, + suggest_connection_header(conn)); +} + + +static void +delete_file(struct mg_connection *conn, const char *path) +{ + struct de de; + memset(&de.file, 0, sizeof(de.file)); + if (!mg_stat(conn, path, &de.file)) { + /* mg_stat returns 0 if the file does not exist */ + mg_send_http_error(conn, + 404, + "Error: Cannot delete file\nFile %s not found", + path); + return; + } + +#if 0 /* Ignore if a file in memory is inside a folder */ + if (de.access.membuf != NULL) { + /* the file is cached in memory */ + mg_send_http_error( + conn, + 405, + "Error: Delete not possible\nDeleting %s is not supported", + path); + return; + } +#endif + + if (de.file.is_directory) { + if (remove_directory(conn, path)) { + /* Delete is successful: Return 204 without content. */ + mg_send_http_error(conn, 204, "%s", ""); + } else { + /* Delete is not successful: Return 500 (Server error). */ + mg_send_http_error(conn, 500, "Error: Could not delete %s", path); + } + return; + } + + /* This is an existing file (not a directory). + * Check if write permission is granted. */ + if (access(path, W_OK) != 0) { + /* File is read only */ + mg_send_http_error( + conn, + 403, + "Error: Delete not possible\nDeleting %s is not allowed", + path); + return; + } + + /* Try to delete it. */ + if (mg_remove(conn, path) == 0) { + /* Delete was successful: Return 204 without content. */ + mg_send_http_error(conn, 204, "%s", ""); + } else { + /* Delete not successful (file locked). */ + mg_send_http_error(conn, + 423, + "Error: Cannot delete file\nremove(%s): %s", + path, + strerror(ERRNO)); + } +} +#endif /* !NO_FILES */ + + +static void +send_ssi_file(struct mg_connection *, const char *, struct mg_file *, int); + + +static void +do_ssi_include(struct mg_connection *conn, + const char *ssi, + char *tag, + int include_level) +{ + char file_name[MG_BUF_LEN], path[512], *p; + struct mg_file file = STRUCT_FILE_INITIALIZER; + size_t len; + int truncated = 0; + + if (conn == NULL) { + return; + } + + /* sscanf() is safe here, since send_ssi_file() also uses buffer + * of size MG_BUF_LEN to get the tag. So strlen(tag) is + * always < MG_BUF_LEN. */ + if (sscanf(tag, " virtual=\"%511[^\"]\"", file_name) == 1) { + /* File name is relative to the webserver root */ + file_name[511] = 0; + (void)mg_snprintf(conn, + &truncated, + path, + sizeof(path), + "%s/%s", + conn->ctx->config[DOCUMENT_ROOT], + file_name); + + } else if (sscanf(tag, " abspath=\"%511[^\"]\"", file_name) == 1) { + /* File name is relative to the webserver working directory + * or it is absolute system path */ + file_name[511] = 0; + (void) + mg_snprintf(conn, &truncated, path, sizeof(path), "%s", file_name); + + } else if ((sscanf(tag, " file=\"%511[^\"]\"", file_name) == 1) + || (sscanf(tag, " \"%511[^\"]\"", file_name) == 1)) { + /* File name is relative to the currect document */ + file_name[511] = 0; + (void)mg_snprintf(conn, &truncated, path, sizeof(path), "%s", ssi); + + if (!truncated) { + if ((p = strrchr(path, '/')) != NULL) { + p[1] = '\0'; + } + len = strlen(path); + (void)mg_snprintf(conn, + &truncated, + path + len, + sizeof(path) - len, + "%s", + file_name); + } + + } else { + mg_cry(conn, "Bad SSI #include: [%s]", tag); + return; + } + + if (truncated) { + mg_cry(conn, "SSI #include path length overflow: [%s]", tag); + return; + } + + if (!mg_fopen(conn, path, MG_FOPEN_MODE_READ, &file)) { + mg_cry(conn, + "Cannot open SSI #include: [%s]: fopen(%s): %s", + tag, + path, + strerror(ERRNO)); + } else { + fclose_on_exec(&file.access, conn); + if (match_prefix(conn->ctx->config[SSI_EXTENSIONS], + strlen(conn->ctx->config[SSI_EXTENSIONS]), + path) > 0) { + send_ssi_file(conn, path, &file, include_level + 1); + } else { + send_file_data(conn, &file, 0, INT64_MAX); + } + (void)mg_fclose(&file.access); /* Ignore errors for readonly files */ + } +} + + +#if !defined(NO_POPEN) +static void +do_ssi_exec(struct mg_connection *conn, char *tag) +{ + char cmd[1024] = ""; + struct mg_file file = STRUCT_FILE_INITIALIZER; + + if (sscanf(tag, " \"%1023[^\"]\"", cmd) != 1) { + mg_cry(conn, "Bad SSI #exec: [%s]", tag); + } else { + cmd[1023] = 0; + if ((file.access.fp = popen(cmd, "r")) == NULL) { + mg_cry(conn, "Cannot SSI #exec: [%s]: %s", cmd, strerror(ERRNO)); + } else { + send_file_data(conn, &file, 0, INT64_MAX); + pclose(file.access.fp); + } + } +} +#endif /* !NO_POPEN */ + + +static int +mg_fgetc(struct mg_file *filep, int offset) +{ + if (filep == NULL) { + return EOF; + } + if ((filep->access.membuf != NULL) && (offset >= 0) + && (((unsigned int)(offset)) < filep->stat.size)) { + return ((const unsigned char *)filep->access.membuf)[offset]; + } else if (filep->access.fp != NULL) { + return fgetc(filep->access.fp); + } else { + return EOF; + } +} + + +static void +send_ssi_file(struct mg_connection *conn, + const char *path, + struct mg_file *filep, + int include_level) +{ + char buf[MG_BUF_LEN]; + int ch, offset, len, in_tag, in_ssi_tag; + + if (include_level > 10) { + mg_cry(conn, "SSI #include level is too deep (%s)", path); + return; + } + + in_tag = in_ssi_tag = len = offset = 0; + + /* Read file, byte by byte, and look for SSI include tags */ + while ((ch = mg_fgetc(filep, offset++)) != EOF) { + + if (in_tag) { + /* We are in a tag, either SSI tag or html tag */ + + if (ch == '>') { + /* Tag is closing */ + buf[len++] = '>'; + + if (in_ssi_tag) { + /* Handle SSI tag */ + buf[len] = 0; + + if (!memcmp(buf + 5, "include", 7)) { + do_ssi_include(conn, path, buf + 12, include_level + 1); +#if !defined(NO_POPEN) + } else if (!memcmp(buf + 5, "exec", 4)) { + do_ssi_exec(conn, buf + 9); +#endif /* !NO_POPEN */ + } else { + mg_cry(conn, + "%s: unknown SSI " + "command: \"%s\"", + path, + buf); + } + len = 0; + in_ssi_tag = in_tag = 0; + + } else { + /* Not an SSI tag */ + /* Flush buffer */ + (void)mg_write(conn, buf, (size_t)len); + len = 0; + in_tag = 0; + } + + } else { + /* Tag is still open */ + buf[len++] = (char)(ch & 0xff); + + if ((len == 5) && !memcmp(buf, "\n\n") + file:write(xml.str(var)) + base.io.close(file) +end + + +-- recursively parses a Lua table for a substatement fitting to the provided tag and attribute +function xml.find(var, tag, attributeKey,attributeValue) + -- check input: + if base.type(var)~="table" then return end + if base.type(tag)=="string" and #tag==0 then tag=nil end + if base.type(attributeKey)~="string" or #attributeKey==0 then attributeKey=nil end + if base.type(attributeValue)=="string" and #attributeValue==0 then attributeValue=nil end + -- compare this table: + if tag~=nil then + if var[0]==tag and ( attributeValue == nil or var[attributeKey]==attributeValue ) then + base.setmetatable(var,{__index=xml, __tostring=xml.str}) + return var + end + else + if attributeValue == nil or var[attributeKey]==attributeValue then + base.setmetatable(var,{__index=xml, __tostring=xml.str}) + return var + end + end + -- recursively parse subtags: + for k,v in base.ipairs(var) do + if base.type(v)=="table" then + local ret = xml.find(v, tag, attributeKey,attributeValue) + if ret ~= nil then return ret end + end + end +end + diff --git a/src/civetweb/src/third_party/LuaXML_lib.c b/src/civetweb/src/third_party/LuaXML_lib.c new file mode 100644 index 000000000..9ac7f9fdf --- /dev/null +++ b/src/civetweb/src/third_party/LuaXML_lib.c @@ -0,0 +1,476 @@ +/** +LuaXML License + +LuaXml is licensed under the terms of the MIT license reproduced below, +the same as Lua itself. This means that LuaXml is free software and can be +used for both academic and commercial purposes at absolutely no cost. + +Copyright (C) 2007-2013 Gerald Franz, eludi.net + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#if defined __WIN32__ || defined WIN32 +# include +# define _EXPORT __declspec(dllexport) +#else +# define _EXPORT +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#include "civetweb_lua.h" + +#ifdef __cplusplus +} // extern "C" +#endif + +#include +#include +#include +#include + +static const char ESC=27; +static const char OPN=28; +static const char CLS=29; + +//--- auxliary functions ------------------------------------------- + +static const char* char2code(unsigned char ch, char buf[8]) { + unsigned char i=0; + buf[i++]='&'; + buf[i++]='#'; + if(ch>99) buf[i++]=ch/100+48; + if(ch>9) buf[i++]=(ch%100)/10+48; + buf[i++]=ch%10+48; + buf[i++]=';'; + buf[i]=0; + return buf; +} + +static size_t find(const char* s, const char* pattern, size_t start) { + const char* found =strstr(s+start, pattern); + return found ? found-s : strlen(s); +} + +//--- internal tokenizer ------------------------------------------- + +typedef struct Tokenizer_s { + /// stores string to be tokenized + const char* s; + /// stores size of string to be tokenized + size_t s_size; + /// stores current read position + size_t i; + /// stores current read context + int tagMode; + /// stores next token, if already determined + const char* m_next; + /// size of next token + size_t m_next_size; + /// pointer to current token + char* m_token; + /// size of current token + size_t m_token_size; + /// capacity of current token + size_t m_token_capacity; +} Tokenizer; + +Tokenizer* Tokenizer_new(const char* str, size_t str_size) { + Tokenizer *tok = (Tokenizer*)malloc(sizeof(Tokenizer)); + memset(tok, 0, sizeof(Tokenizer)); + tok->s_size = str_size; + tok->s = str; + return tok; +} + +void Tokenizer_delete(Tokenizer* tok) { + free(tok->m_token); + free(tok); +} + +//void Tokenizer_print(Tokenizer* tok) { printf(" @%u %s\n", tok->i, !tok->m_token ? "(null)" : (tok->m_token[0]==ESC)?"(esc)" : (tok->m_token[0]==OPN)?"(open)": (tok->m_token[0]==CLS)?"(close)" : tok->m_token); fflush(stdout); } + +static const char* Tokenizer_set(Tokenizer* tok, const char* s, size_t size) { + if(!size||!s) return 0; + free(tok->m_token); + tok->m_token = (char*)malloc(size+1); + strncpy(tok->m_token,s, size); + tok->m_token[size] = 0; + tok->m_token_size = tok->m_token_capacity = size; + //Tokenizer_print(tok); + return tok->m_token; +} + +static void Tokenizer_append(Tokenizer* tok, char ch) { + if(tok->m_token_size+1>=tok->m_token_capacity) { + tok->m_token_capacity = (tok->m_token_capacity==0) ? 16 : tok->m_token_capacity*2; + tok->m_token = (char*)realloc(tok->m_token, tok->m_token_capacity); + } + tok->m_token[tok->m_token_size]=ch; + tok->m_token[++tok->m_token_size]=0; +} + +const char* Tokenizer_next(Tokenizer* tok) { + const char* ESC_str = "\033"; + const char* OPEN_str = "\034"; + const char* CLOSE_str = "\035"; + int quotMode=0; + int tokenComplete = 0; + + if(tok->m_token) { + free(tok->m_token); + tok->m_token = 0; + tok->m_token_size=tok->m_token_capacity = 0; + } + + while(tok->m_next_size || (tok->i < tok->s_size)) { + + if(tok->m_next_size) { + Tokenizer_set(tok, tok->m_next, tok->m_next_size); + tok->m_next=0; + tok->m_next_size=0; + return tok->m_token; + } + + switch(tok->s[tok->i]) { + case '"': + case '\'': + if(tok->tagMode) { + if(!quotMode) quotMode=tok->s[tok->i]; + else if(quotMode==tok->s[tok->i]) quotMode=0; + } + Tokenizer_append(tok, tok->s[tok->i]); + break; + case '<': + if(!quotMode&&(tok->i+4s_size)&&(strncmp(tok->s+tok->i,"", tok->i+4)+2; + else if(!quotMode&&(tok->i+9s_size)&&(strncmp(tok->s+tok->i,"i+9; + tok->i=find(tok->s, "]]>",b)+3; + if(!tok->m_token_size) return Tokenizer_set(tok, tok->s+b, tok->i-b-3); + tokenComplete = 1; + tok->m_next = tok->s+b; + tok->m_next_size = tok->i-b-3; + --tok->i; + } + else if(!quotMode&&(tok->i+1s_size)&&((tok->s[tok->i+1]=='?')||(tok->s[tok->i+1]=='!'))) // strip meta information + tok->i=find(tok->s, ">", tok->i+2); + else if(!quotMode&&!tok->tagMode) { + if((tok->i+1s_size)&&(tok->s[tok->i+1]=='/')) { + tok->m_next=ESC_str; + tok->m_next_size = 1; + tok->i=find(tok->s, ">", tok->i+2); + } + else { + tok->m_next = OPEN_str; + tok->m_next_size = 1; + tok->tagMode=1; + } + tokenComplete = 1; + } + else Tokenizer_append(tok, tok->s[tok->i]); + break; + case '/': + if(tok->tagMode&&!quotMode) { + tokenComplete = 1; + if((tok->i+1 < tok->s_size) && (tok->s[tok->i+1]=='>')) { + tok->tagMode=0; + tok->m_next=ESC_str; + tok->m_next_size = 1; + ++tok->i; + } + else Tokenizer_append(tok, tok->s[tok->i]); + } + else Tokenizer_append(tok, tok->s[tok->i]); + break; + case '>': + if(!quotMode&&tok->tagMode) { + tok->tagMode=0; + tokenComplete = 1; + tok->m_next = CLOSE_str; + tok->m_next_size = 1; + } + else Tokenizer_append(tok, tok->s[tok->i]); + break; + case ' ': + case '\r': + case '\n': + case '\t': + if(tok->tagMode&&!quotMode) { + if(tok->m_token_size) tokenComplete=1; + } + else if(tok->m_token_size) Tokenizer_append(tok, tok->s[tok->i]); + break; + default: Tokenizer_append(tok, tok->s[tok->i]); + } + ++tok->i; + if((tok->i>=tok->s_size)||(tokenComplete&&tok->m_token_size)) { + tokenComplete=0; + while(tok->m_token_size&&isspace(tok->m_token[tok->m_token_size-1])) // trim whitespace + tok->m_token[--tok->m_token_size]=0; + if(tok->m_token_size) break; + } + } + //Tokenizer_print(tok); + return tok->m_token; +} + +//--- local variables ---------------------------------------------- + +/// stores number of special character codes +static size_t sv_code_size=0; +/// stores currently allocated capacity for special character codes +static size_t sv_code_capacity=16; +/// stores code table for special characters +static char** sv_code=0; + +//--- public methods ----------------------------------------------- + +static void Xml_pushDecode(lua_State* L, const char* s, size_t s_size) { + + luaL_Buffer b; + const char* found = strstr(s, "&#"); + size_t start=0, pos, i; + + if(!s_size) + s_size=strlen(s); + + luaL_buffinit(L, &b); + found = strstr(s, "&#"); + pos = found ? found-s : s_size; + + while(found) { + char ch = 0; + size_t i=0; + for(found += 2; i<3; ++i, ++found) + if(isdigit(*found)) + ch = ch * 10 + (*found - 48); + else break; + if(*found == ';') { + if(pos>start) + luaL_addlstring(&b, s+start, pos-start); + luaL_addchar(&b, ch); + start = pos + 3 + i; + } + found = strstr(found+1, "&#"); + pos = found ? found-s : s_size; + } + if(pos>start) + luaL_addlstring(&b,s+start, pos-start); + luaL_pushresult(&b); + + for(i=sv_code_size-1; i1) lua_settop(L,-2); // this tag has no content, only attributes + else break; + } + } + else if(token[0]==ESC) { // previous tag is over + if(lua_gettop(L)>1) lua_settop(L,-2); // pop current table + else break; + } + else { // read elements + lua_pushnumber(L,(lua_Number)lua_rawlen(L,-1)+1); + Xml_pushDecode(L, token, 0); + lua_settable(L, -3); + } + Tokenizer_delete(tok); + free(str); + return lua_gettop(L); +} + +int Xml_load (lua_State *L) { + const char * filename = luaL_checkstring(L,1); + FILE * file=fopen(filename,"r"); + char* buffer; + size_t sz; + + if(!file) + return luaL_error(L,"LuaXml ERROR: \"%s\" file error or file not found!",filename); + + fseek (file , 0 , SEEK_END); + sz = ftell (file); + rewind (file); + buffer = (char*)malloc(sz+1); + sz = fread (buffer,1,sz,file); + fclose(file); + buffer[sz]=0; + lua_pushlightuserdata(L,buffer); + lua_replace(L,1); + return Xml_eval(L); +}; + +int Xml_registerCode(lua_State *L) { + const char * decoded = luaL_checkstring(L,1); + const char * encoded = luaL_checkstring(L,2); + + size_t i; + for(i=0; isv_code_capacity) { + sv_code_capacity*=2; + sv_code = (char**)realloc(sv_code, sv_code_capacity*sizeof(char*)); + } + sv_code[sv_code_size]=(char*)malloc(strlen(decoded)+1); + strcpy(sv_code[sv_code_size++], decoded); + sv_code[sv_code_size]=(char*)malloc(strlen(encoded)+1); + strcpy(sv_code[sv_code_size++],encoded); + return 0; +} + +int Xml_encode(lua_State *L) { + + char buf[8]; + size_t start, pos; + luaL_Buffer b; + const char* s; + size_t i; + + if(lua_gettop(L)!=1) + return 0; + luaL_checkstring(L,-1); + + for(i=0; istart) luaL_addlstring(&b,s+start, pos-start); + luaL_addstring(&b,char2code((unsigned char)(s[pos]),buf)); + start=pos+1; + } + if(pos>start) + luaL_addlstring(&b,s+start, pos-start); + luaL_pushresult(&b); + lua_remove(L,-2); + return 1; +} + +#ifdef __cplusplus +extern "C" { +#endif +int _EXPORT luaopen_LuaXML_lib (lua_State* L) { + static const struct luaL_Reg funcs[] = { + {"load", Xml_load}, + {"eval", Xml_eval}, + {"encode", Xml_encode}, + {"registerCode", Xml_registerCode}, + {NULL, NULL} + }; + + luaL_newlibtable(L, funcs); + luaL_setfuncs(L, funcs, 0); + lua_setglobal(L, "xml"); + + // register default codes: + if(!sv_code) { + sv_code=(char**)malloc(sv_code_capacity*sizeof(char*)); + sv_code[sv_code_size++]="&"; + sv_code[sv_code_size++]="&"; + sv_code[sv_code_size++]="<"; + sv_code[sv_code_size++]="<"; + sv_code[sv_code_size++]=">"; + sv_code[sv_code_size++]=">"; + sv_code[sv_code_size++]="\""; + sv_code[sv_code_size++]="""; + sv_code[sv_code_size++]="'"; + sv_code[sv_code_size++]="'"; + } + return 1; +} +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/src/civetweb/src/third_party/civetweb_lua.h b/src/civetweb/src/third_party/civetweb_lua.h new file mode 100644 index 000000000..5ffefbe9a --- /dev/null +++ b/src/civetweb/src/third_party/civetweb_lua.h @@ -0,0 +1,73 @@ +/* Copyright (c) 2015-2017 the Civetweb developers + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/* This header is intended to support Lua 5.1, Lua 5.2 and Lua 5.3 in the same + * C source code. + */ + +#ifndef CIVETWEB_LUA_H +#define CIVETWEB_LUA_H + +#define LUA_LIB +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" + +#ifndef LUA_VERSION_NUM +#error "Unknown Lua version" + +#elif LUA_VERSION_NUM == 501 +/* Lua 5.1 detected */ +#define LUA_OK 0 +#define LUA_ERRGCMM 999 /* not supported */ +#define mg_lua_load(a, b, c, d, e) lua_load(a, b, c, d) +#define lua_rawlen lua_objlen +#define lua_newstate(a, b) \ + luaL_newstate() /* Must use luaL_newstate() for 64 bit target */ +#define lua_pushinteger lua_pushnumber +#define luaL_newlib(L, t) \ + { \ + luaL_Reg const *r = t; \ + while (r->name) { \ + lua_register(L, r->name, r->func); \ + r++; \ + } \ + } +#define luaL_setfuncs(L, r, u) lua_register(L, r->name, r->func) + +#elif LUA_VERSION_NUM == 502 +/* Lua 5.2 detected */ +#define mg_lua_load lua_load + +#elif LUA_VERSION_NUM == 503 +/* Lua 5.3 detected */ +#define mg_lua_load lua_load + +#endif + +#ifdef LUA_VERSION_MAKEFILE +#if LUA_VERSION_MAKEFILE != LUA_VERSION_NUM +#error \ + "Mismatch between Lua version specified in Makefile and Lua version in lua.h" +#endif +#endif + +#endif /* #ifndef CIVETWEB_LUA_H */ diff --git a/src/civetweb/src/third_party/duktape-1.5.2/AUTHORS.rst b/src/civetweb/src/third_party/duktape-1.5.2/AUTHORS.rst new file mode 100644 index 000000000..2799f70f4 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/AUTHORS.rst @@ -0,0 +1,72 @@ +=============== +Duktape authors +=============== + +Copyright +========= + +Duktape copyrights are held by its authors. Each author has a copyright +to their contribution, and agrees to irrevocably license the contribution +under the Duktape ``LICENSE.txt``. + +Authors +======= + +Please include an e-mail address, a link to your GitHub profile, or something +similar to allow your contribution to be identified accurately. + +The following people have contributed code, website contents, or Wiki contents, +and agreed to irrevocably license their contributions under the Duktape +``LICENSE.txt`` (in order of appearance): + +* Sami Vaarala +* Niki Dobrev +* Andreas Öman +* László Langó +* Legimet +* Karl Skomski +* Bruce Pascoe +* René Hollander +* Julien Hamaide (https://github.com/crazyjul) +* Sebastian Götte (https://github.com/jaseg) + +Other contributions +=================== + +The following people have contributed something other than code (e.g. reported +bugs, provided ideas, etc; roughly in order of appearance): + +* Greg Burns +* Anthony Rabine +* Carlos Costa +* Aurélien Bouilland +* Preet Desai (Pris Matic) +* judofyr (http://www.reddit.com/user/judofyr) +* Jason Woofenden +* Michał Przybyś +* Anthony Howe +* Conrad Pankoff +* Jim Schimpf +* Rajaran Gaunker (https://github.com/zimbabao) +* Andreas Öman +* Doug Sanden +* Josh Engebretson (https://github.com/JoshEngebretson) +* Remo Eichenberger (https://github.com/remoe) +* Mamod Mehyar (https://github.com/mamod) +* David Demelier (https://github.com/markand) +* Tim Caswell (https://github.com/creationix) +* Mitchell Blank Jr (https://github.com/mitchblank) +* https://github.com/yushli +* Seo Sanghyeon (https://github.com/sanxiyn) +* Han ChoongWoo (https://github.com/tunz) +* Joshua Peek (https://github.com/josh) +* Bruce E. Pascoe (https://github.com/fatcerberus) +* https://github.com/Kelledin +* https://github.com/sstruchtrup +* Michael Drake (https://github.com/tlsa) +* https://github.com/chris-y +* Laurent Zubiaur (https://github.com/lzubiaur) +* Ole André Vadla Ravnås (https://github.com/oleavr) + +If you are accidentally missing from this list, send me an e-mail +(``sami.vaarala@iki.fi``) and I'll fix the omission. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/LICENSE.txt b/src/civetweb/src/third_party/duktape-1.5.2/LICENSE.txt new file mode 100644 index 000000000..1b1c382c3 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/LICENSE.txt @@ -0,0 +1,25 @@ +=============== +Duktape license +=============== + +(http://opensource.org/licenses/MIT) + +Copyright (c) 2013-2016 by Duktape authors (see AUTHORS.rst) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/Makefile.cmdline b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.cmdline new file mode 100644 index 000000000..68a25d4e0 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.cmdline @@ -0,0 +1,34 @@ +# +# Example Makefile for building a program with embedded Duktape. +# The example program here is the Duktape command line tool. +# + +DUKTAPE_SOURCES = src/duktape.c + +DUKTAPE_CMDLINE_SOURCES = \ + examples/cmdline/duk_cmdline.c + +CC = gcc +CCOPTS = -Os -pedantic -std=c99 -Wall -fstrict-aliasing -fomit-frame-pointer +CCOPTS += -I./src # duktape.h and duk_config.h must be in include path +CCLIBS = -lm + +# If you want linenoise, you can enable these. At the moment linenoise +# will cause some harmless compilation warnings. +#CCOPTS += -DDUK_CMDLINE_FANCY +#DUKTAPE_CMDLINE_SOURCES += linenoise/linenoise.c +#CCOPTS += -I./linenoise +#duk: linenoise + +# Optional feature defines, see: http://duktape.org/guide.html#compiling +CCOPTS += -DDUK_OPT_SELF_TESTS +#CCOPTS += -DDUK_OPT_DEBUG +#CCOPTS += -DDUK_OPT_DPRINT +# ... + +duk: $(DUKTAPE_SOURCES) $(DUKTAPE_CMDLINE_SOURCES) + $(CC) -o $@ $(DEFINES) $(CCOPTS) $(DUKTAPE_SOURCES) $(DUKTAPE_CMDLINE_SOURCES) $(CCLIBS) + +linenoise/linenoise.c: linenoise +linenoise: + git clone https://github.com/antirez/linenoise.git diff --git a/src/civetweb/src/third_party/duktape-1.5.2/Makefile.codepage b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.codepage new file mode 100644 index 000000000..cdce9ab57 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.codepage @@ -0,0 +1,4 @@ +codepage: + gcc -o $@ -std=c99 -O2 -Wall -Wextra -Isrc/ \ + src/duktape.c examples/codepage-conv/duk_codepage_conv.c \ + examples/codepage-conv/test.c -lm diff --git a/src/civetweb/src/third_party/duktape-1.5.2/Makefile.coffee b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.coffee new file mode 100644 index 000000000..b99eea230 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.coffee @@ -0,0 +1,4 @@ +dummy: + coffee -c examples/coffee/globals.coffee + coffee -c examples/coffee/hello.coffee + coffee -c examples/coffee/mandel.coffee diff --git a/src/civetweb/src/third_party/duktape-1.5.2/Makefile.dukdebug b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.dukdebug new file mode 100644 index 000000000..dbbe6c8a2 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.dukdebug @@ -0,0 +1,26 @@ +# +# Duktape command line tool with debugger support. +# + +DUKTAPE_SOURCES = src/duktape.c + +# Windows (MinGW): use examples/debug-trans-socket/duk_trans_socket_windows.c +# and link with -lws2_32. +DUKTAPE_CMDLINE_SOURCES = \ + examples/cmdline/duk_cmdline.c \ + examples/debug-trans-socket/duk_trans_socket_unix.c + +CC = gcc +CCOPTS = -Os -pedantic -std=c99 -Wall -fstrict-aliasing -fomit-frame-pointer +CCOPTS += -I./src -I./examples/debug-trans-socket +CCOPTS += -DDUK_CMDLINE_DEBUGGER_SUPPORT # enable --debugger in ./duk +CCOPTS += -DDUK_OPT_DEBUGGER_SUPPORT # enable debugger support in Duktape +CCOPTS += -DDUK_OPT_INTERRUPT_COUNTER # prerequisite for debugging +CCOPTS += -DDUK_OPT_DEBUGGER_FWD_PRINTALERT # optional debugger features +CCOPTS += -DDUK_OPT_DEBUGGER_FWD_LOGGING +CCOPTS += -DDUK_OPT_DEBUGGER_DUMPHEAP +CCOPTS += -DDUK_OPT_DEBUGGER_INSPECT +CCLIBS = -lm + +duk: $(DUKTAPE_SOURCES) $(DUKTAPE_CMDLINE_SOURCES) + $(CC) -o $@ $(DEFINES) $(CCOPTS) $(DUKTAPE_SOURCES) $(DUKTAPE_CMDLINE_SOURCES) $(CCLIBS) diff --git a/src/civetweb/src/third_party/duktape-1.5.2/Makefile.eval b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.eval new file mode 100644 index 000000000..73c522501 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.eval @@ -0,0 +1,7 @@ +# +# Example Makefile for building the eval example +# + +eval: + gcc -o $@ -std=c99 -O2 -Wall -Wextra -Isrc/ \ + src/duktape.c examples/eval/eval.c -lm diff --git a/src/civetweb/src/third_party/duktape-1.5.2/Makefile.eventloop b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.eventloop new file mode 100644 index 000000000..14806ac8d --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.eventloop @@ -0,0 +1,22 @@ +# +# Example Makefile for building the eventloop example +# + +evloop: + @echo "NOTE: The eventloop is example is intended to be used on Linux" + @echo " or other common UNIX variants. It is not fully portable." + @echo "" + + gcc -o $@ -std=c99 -Wall -Wextra -O2 -Isrc \ + examples/eventloop/main.c \ + examples/eventloop/c_eventloop.c \ + examples/eventloop/poll.c \ + examples/eventloop/socket.c \ + examples/eventloop/fileio.c \ + examples/eventloop/ncurses.c \ + src/duktape.c \ + -lm -lncurses + + @echo "" + @echo "NOTE: You must 'cd examples/eventloop' before you execute the" + @echo " eventloop binary: it relies on finding .js files in CWD" diff --git a/src/civetweb/src/third_party/duktape-1.5.2/Makefile.hello b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.hello new file mode 100644 index 000000000..82e9ab6b5 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.hello @@ -0,0 +1,35 @@ +# +# Example Makefile for building a program with embedded Duktape. +# +# There are two source sets in the distribution: (1) combined sources where +# you only need duktape.c, duktape.h, and duk_config.h, and (2) separate +# sources where you have a bunch of source and header files. Whichever +# you use, simply include the relevant sources into your C project. This +# Makefile uses the combined source file. +# + +DUKTAPE_SOURCES = src/duktape.c + +# Compiler options are quite flexible. GCC versions have a significant impact +# on the size of -Os code, e.g. gcc-4.6 is much worse than gcc-4.5. + +CC = gcc +CCOPTS = -Os -pedantic -std=c99 -Wall -fstrict-aliasing -fomit-frame-pointer +CCOPTS += -I./src # for combined sources +CCLIBS = -lm +DEFINES = + +# If you want a 32-bit build on a 64-bit host +#CCOPTS += -m32 + +# Optional feature defines, see: http://duktape.org/guide.html#compiling +DEFINES += -DDUK_OPT_SELF_TESTS +#DEFINES += -DDUK_OPT_DEBUG +#DEFINES += -DDUK_OPT_DPRINT +#DEFINES += -DDUK_OPT_NO_TRACEBACKS +# ... + +# For debugging, use -O0 -g -ggdb, and don't add -fomit-frame-pointer + +hello: $(DUKTAPE_SOURCES) examples/hello/hello.c + $(CC) -o $@ $(DEFINES) $(CCOPTS) $(DUKTAPE_SOURCES) examples/hello/hello.c $(CCLIBS) diff --git a/src/civetweb/src/third_party/duktape-1.5.2/Makefile.jxpretty b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.jxpretty new file mode 100644 index 000000000..199247ee5 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.jxpretty @@ -0,0 +1,8 @@ +# +# Example Makefile for building the jxpretty example +# + +jxpretty: + gcc -o $@ -std=c99 -Wall -Wextra -O2 -Isrc \ + src/duktape.c examples/jxpretty/jxpretty.c \ + -lm diff --git a/src/civetweb/src/third_party/duktape-1.5.2/Makefile.sandbox b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.sandbox new file mode 100644 index 000000000..acd922ae3 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.sandbox @@ -0,0 +1,7 @@ +# +# Example Makefile for building the sandbox example +# + +sandbox: + gcc -o $@ -std=c99 -O2 -Wall -Wextra -Isrc/ \ + src/duktape.c examples/sandbox/sandbox.c -lm diff --git a/src/civetweb/src/third_party/duktape-1.5.2/Makefile.sharedlibrary b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.sharedlibrary new file mode 100644 index 000000000..32138ceaf --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/Makefile.sharedlibrary @@ -0,0 +1,71 @@ +# +# Example of how to build and install locally as a shared library +# +# Usage: +# +# $ make -f Makefile.sharedlibrary +# $ sudo make -f Makefile.sharedlibrary install +# $ make -f Makefile.sharedlibrary duk # --> example 'duk' linked to shared libduktape +# +# $ ls -l duk +# -rwxrwxr-x 1 duktape duktape 19407 Nov 30 15:48 duk +# +# $ ldd ./duk +# linux-vdso.so.1 => (0x00007ffd5ed3c000) +# libduktape.so.104 => /usr/local/lib/libduktape.so.104 (0x00007fb2f9753000) +# libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb2f944d000) +# libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb2f9088000) +# /lib64/ld-linux-x86-64.so.2 (0x00007fb2f9991000) +# +# Based on: http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html + +# Soname version must be bumped whenever a binary compatibility change occurs +# (and should not be bumped when the library is compatible). A simple Duktape +# convention is to set soname version to (100*MAJOR + MINOR), e.g. 104 for +# Duktape 1.4.x, so that it gets automatically bumped for major and minor +# releases (potentially binary incompatible), but not for patch releases. +DUK_VERSION=10502 +SONAME_VERSION=105 +REAL_VERSION=$(SONAME_VERSION).$(DUK_VERSION) + +# Change to actual path for actual distribution packaging. +INSTALL_PREFIX=/usr/local + +# The 'noline' variant may be more appropriate for some distributions; it +# doesn't have #line directives in the combined source. +DUKTAPE_SRCDIR=./src +#DUKTAPE_SRCDIR=./src-noline + +.PHONY: all +all: libduktape.so.$(REAL_VERSION) libduktaped.so.$(REAL_VERSION) + +# If the default duk_config.h is not suitable for the distribution, modify it +# before compiling the shared library and copy the same, edited duk_config.h +# to $INSTALL_PREFIX/include on installation. + +libduktape.so.$(REAL_VERSION): + gcc -shared -fPIC -Wall -Wextra -Os -Wl,-soname,libduktape.so.$(SONAME_VERSION) \ + -o $@ $(DUKTAPE_SRCDIR)/duktape.c + +libduktaped.so.$(REAL_VERSION): + gcc -shared -fPIC -g -Wall -Wextra -Os -Wl,-soname,libduktaped.so.$(SONAME_VERSION) \ + -o $@ $(DUKTAPE_SRCDIR)/duktape.c + +# Symlinks depend on platform conventions. +.PHONY: install +install: libduktape.so.$(REAL_VERSION) libduktaped.so.$(REAL_VERSION) + cp $+ $(INSTALL_PREFIX)/lib/ + rm -f $(INSTALL_PREFIX)/lib/libduktape.so $(INSTALL_PREFIX)/lib/libduktape.so.$(SONAME_VERSION) + ln -s libduktape.so.$(REAL_VERSION) $(INSTALL_PREFIX)/lib/libduktape.so + ln -s libduktape.so.$(REAL_VERSION) $(INSTALL_PREFIX)/lib/libduktape.so.$(SONAME_VERSION) + rm -f $(INSTALL_PREFIX)/lib/libduktaped.so $(INSTALL_PREFIX)/lib/libduktaped.so.$(SONAME_VERSION) + ln -s libduktaped.so.$(REAL_VERSION) $(INSTALL_PREFIX)/lib/libduktaped.so + ln -s libduktaped.so.$(REAL_VERSION) $(INSTALL_PREFIX)/lib/libduktaped.so.$(SONAME_VERSION) + cp $(DUKTAPE_SRCDIR)/duktape.h $(DUKTAPE_SRCDIR)/duk_config.h $(INSTALL_PREFIX)/include/ + +# Note: assumes /usr/local/include/ and /usr/local/lib/ are in include/link +# path which may not be the case for all distributions. +#CCOPTS=-I/usr/local/include -L/usr/local/lib +CCOPTS= +duk: + gcc $(CCOPTS) -Wall -Wextra -Os -o $@ ./examples/cmdline/duk_cmdline.c -lduktape -lm diff --git a/src/civetweb/src/third_party/duktape-1.5.2/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/README.rst new file mode 100644 index 000000000..65311a7c7 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/README.rst @@ -0,0 +1,110 @@ +======= +Duktape +======= + +Duktape is a small and portable Ecmascript E5/E5.1 implementation. It is +intended to be easily embeddable into C programs, with a C API similar in +spirit to Lua's. + +Duktape supports the full E5/E5.1 feature set including errors, Unicode +strings, and regular expressions, a subset of E6 features (e.g. Proxy +objects), Khronos/ES6 ArrayBuffer/TypedView, and Node.js Buffer bindings. + +Duktape also provides a number of custom features such as error tracebacks, +additional data types for better C integration, combined reference counting +and mark-and sweep garbage collector, object finalizers, co-operative +threads a.k.a. coroutines, tail calls, built-in logging and module frameworks, +a built-in debugger protocol, function bytecode dump/load, and so on. + +You can browse Duktape programmer's API and other documentation at: + +* http://duktape.org/ + +In particular, you should read the getting started section: + +* http://duktape.org/guide.html#gettingstarted + +More examples and how-to articles are in the Duktape Wiki: + +* http://wiki.duktape.org/ + +Building and integrating Duktape into your project is very straightforward: + +* http://duktape.org/guide.html#compiling + +See Makefile.hello for a concrete example:: + + $ cd + $ make -f Makefile.hello + [...] + $ ./hello + Hello world! + 2+3=5 + +To build an example command line tool, use the following:: + + $ cd + $ make -f Makefile.cmdline + [...] + + $ ./duk + ((o) Duktape + duk> print('Hello world!'); + Hello world! + = undefined + + $ ./duk mandel.js + [...] + +This distributable contains: + +* ``src/``: main Duktape library in a "single source file" format (duktape.c, + duktape.h, and duk_config.h). + +* ``src-noline/``: contains a variant of ``src/duktape.c`` with no ``#line`` + directives which is preferable for some users. See discussion in + https://github.com/svaarala/duktape/pull/363. + +* ``src-separate/``: main Duktape library in multiple files format. + +* ``config/``: genconfig utility for creating duk_config.h configuration + files, see: http://wiki.duktape.org/Configuring.html. + +* ``examples/``: further examples for using Duktape. Although Duktape + itself is widely portable, some of the examples are Linux only. + For instance the ``eventloop`` example illustrates how ``setTimeout()`` + and other standard timer functions could be implemented on Unix/Linux. + +* ``extras/``: utilities and modules which don't comfortably fit into the + main Duktape library because of footprint or portability concerns. + Extras are maintained and bug fixed code, but don't have the same version + guarantees as the main Duktape library. + +* ``polyfills/``: a few replacement suggestions for non-standard Javascript + functions provided by other implementations. + +* ``debugger/``: a debugger with a web UI, see ``debugger/README.rst`` and + https://github.com/svaarala/duktape/blob/master/doc/debugger.rst for + details on Duktape debugger support. Also contains a JSON debug proxy + (one written in Node.js and another in DukLuv) to make talking to the + debug target easier. + +* ``licenses/``: licensing information. + +You can find release notes at: + +* https://github.com/svaarala/duktape/blob/master/RELEASES.rst + +This distributable contains Duktape version 1.5.2, created from git +commit cad34ae155acb0846545ca6bf2d29f9463b22bbb (v1.5.2). + +Duktape is copyrighted by its authors (see ``AUTHORS.rst``) and licensed +under the MIT license (see ``LICENSE.txt``). String hashing algorithms are +based on the algorithm from Lua (MIT license), djb2 hash, and Murmurhash2 +(MIT license). Duktape module loader is based on the CommonJS module +loading specification (without sharing any code), CommonJS is under the +MIT license. + +Have fun! + +Sami Vaarala (sami.vaarala@iki.fi) diff --git a/src/civetweb/src/third_party/duktape-1.5.2/config/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/config/README.rst new file mode 100644 index 000000000..1d1722617 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/config/README.rst @@ -0,0 +1,39 @@ +================= +Duktape genconfig +================= + +Overview +======== + +``genconfig`` is a helper script for coming up with a ``duk_config.h`` for +compiling Duktape for your platform. + +To support this: + +* It creates a Duktape 1.2.x compatible ``duk_config.h`` with automatic + platform detection and ``DUK_OPT_xxx`` feature options. + +* It helps to create a ``duk_config.h`` for your platform/compiler + combination. You can give a base configuration and then force certain + values manually based on a YAML configuration file. + +* It autogenerates documentation for config options (and Duktape 1.2.x + feature options) based on option metadata files written in YAML. + +Usage +===== + +To create an autodetect duk_config.h header (compatible with Duktape 1.2.x):: + + $ python config/genconfig.py --metadata config --output /tmp/duk_config.h \ + autodetect-header + +To create a barebones duk_config.h header for a specific platform (easier to +edit manually):: + + $ python config/genconfig.py --metadata config --output /tmp/duk_config.h \ + --platform linux --compiler gcc --architecture x64 \ + barebones-header + +There are further commands to e.g. autogenerate config option documentation; +see ``genconfig.py`` for details. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/config/duk_config.h-modular-dll b/src/civetweb/src/third_party/duktape-1.5.2/config/duk_config.h-modular-dll new file mode 100644 index 000000000..5d5d0137c --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/config/duk_config.h-modular-dll @@ -0,0 +1,3415 @@ +/* + * duk_config.h configuration header generated by genconfig.py. + * + * Git commit: cad34ae155acb0846545ca6bf2d29f9463b22bbb + * Git describe: v1.5.2 + * Git branch: HEAD + * + * Supported platforms: + * - Mac OSX, iPhone, Darwin + * - OpenBSD + * - Generic BSD + * - Atari ST TOS + * - AmigaOS + * - Windows + * - Flashplayer (Crossbridge) + * - QNX + * - TI-Nspire + * - Emscripten + * - Linux + * - Solaris + * - Generic POSIX + * - Cygwin + * - Generic UNIX + * - Generic fallback + * + * Supported architectures: + * - x86 + * - x64 + * - x32 + * - ARM 32-bit + * - ARM 64-bit + * - MIPS 32-bit + * - MIPS 64-bit + * - PowerPC 32-bit + * - PowerPC 64-bit + * - SPARC 32-bit + * - SPARC 64-bit + * - SuperH + * - Motorola 68k + * - Emscripten + * - Generic + * + * Supported compilers: + * - Clang + * - GCC + * - MSVC + * - Emscripten + * - TinyC + * - VBCC + * - Bruce's C compiler + * - Generic + * + */ + +#if !defined(DUK_CONFIG_H_INCLUDED) +#define DUK_CONFIG_H_INCLUDED + +/* + * Intermediate helper defines + */ + +/* DLL build detection */ +#if defined(DUK_OPT_DLL_BUILD) +#define DUK_F_DLL_BUILD +#elif defined(DUK_OPT_NO_DLL_BUILD) +#undef DUK_F_DLL_BUILD +#else +/* configured for DLL build */ +#define DUK_F_DLL_BUILD +#endif + +/* Apple OSX, iOS */ +#if defined(__APPLE__) +#define DUK_F_APPLE +#endif + +/* OpenBSD */ +#if defined(__OpenBSD__) || defined(__OpenBSD) +#define DUK_F_OPENBSD +#endif + +/* NetBSD */ +#if defined(__NetBSD__) || defined(__NetBSD) +#define DUK_F_NETBSD +#endif + +/* FreeBSD */ +#if defined(__FreeBSD__) || defined(__FreeBSD) +#define DUK_F_FREEBSD +#endif + +/* BSD variant */ +#if defined(DUK_F_FREEBSD) || defined(DUK_F_NETBSD) || defined(DUK_F_OPENBSD) || \ + defined(__bsdi__) || defined(__DragonFly__) +#define DUK_F_BSD +#endif + +/* Atari ST TOS. __TOS__ defined by PureC. No platform define in VBCC + * apparently, so to use with VBCC user must define __TOS__ manually. + */ +#if defined(__TOS__) +#define DUK_F_TOS +#endif + +/* Motorola 68K. Not defined by VBCC, so user must define one of these + * manually when using VBCC. + */ +#if defined(__m68k__) || defined(M68000) || defined(__MC68K__) +#define DUK_F_M68K +#endif + +/* AmigaOS. Neither AMIGA nor __amigaos__ is defined on VBCC, so user must + * define 'AMIGA' manually when using VBCC. + */ +#if defined(AMIGA) || defined(__amigaos__) +#define DUK_F_AMIGAOS +#endif + +/* PowerPC */ +#if defined(__powerpc) || defined(__powerpc__) || defined(__PPC__) +#define DUK_F_PPC +#if defined(__PPC64__) || defined(__LP64__) || defined(_LP64) +#define DUK_F_PPC64 +#else +#define DUK_F_PPC32 +#endif +#endif + +/* Windows, both 32-bit and 64-bit */ +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || \ + defined(__WIN32__) || defined(__TOS_WIN__) || defined(__WINDOWS__) +#define DUK_F_WINDOWS +#if defined(_WIN64) || defined(WIN64) +#define DUK_F_WIN64 +#else +#define DUK_F_WIN32 +#endif +#endif + +/* Flash player (e.g. Crossbridge) */ +#if defined(__FLASHPLAYER__) +#define DUK_F_FLASHPLAYER +#endif + +/* QNX */ +#if defined(__QNX__) +#define DUK_F_QNX +#endif + +/* TI-Nspire (using Ndless) */ +#if defined(_TINSPIRE) +#define DUK_F_TINSPIRE +#endif + +/* Emscripten (provided explicitly by user), improve if possible */ +#if defined(EMSCRIPTEN) +#define DUK_F_EMSCRIPTEN +#endif + +/* BCC (Bruce's C compiler): this is a "torture target" for compilation */ +#if defined(__BCC__) || defined(__BCC_VERSION__) +#define DUK_F_BCC +#endif + +/* Linux */ +#if defined(__linux) || defined(__linux__) || defined(linux) +#define DUK_F_LINUX +#endif + +/* illumos / Solaris */ +#if defined(__sun) && defined(__SVR4) +#define DUK_F_SUN +#endif + +/* POSIX */ +#if defined(__posix) +#define DUK_F_POSIX +#endif + +/* Cygwin */ +#if defined(__CYGWIN__) +#define DUK_F_CYGWIN +#endif + +/* Generic Unix (includes Cygwin) */ +#if defined(__unix) || defined(__unix__) || defined(unix) || \ + defined(DUK_F_LINUX) || defined(DUK_F_BSD) +#define DUK_F_UNIX +#endif + +/* stdint.h not available */ +#if defined(DUK_F_WINDOWS) && defined(_MSC_VER) +#if (_MSC_VER < 1700) +/* VS2012+ has stdint.h, < VS2012 does not (but it's available for download). */ +#define DUK_F_NO_STDINT_H +#endif +#endif +#if !defined(DUK_F_NO_STDINT_H) && (defined(DUK_F_TOS) || defined(DUK_F_BCC)) +#define DUK_F_NO_STDINT_H +#endif + +/* C++ */ +#undef DUK_F_CPP +#if defined(__cplusplus) +#define DUK_F_CPP +#endif + +/* Intel x86 (32-bit), x64 (64-bit) or x32 (64-bit but 32-bit pointers), + * define only one of DUK_F_X86, DUK_F_X64, DUK_F_X32. + * https://sites.google.com/site/x32abi/ + */ +#if defined(__amd64__) || defined(__amd64) || \ + defined(__x86_64__) || defined(__x86_64) || \ + defined(_M_X64) || defined(_M_AMD64) +#if defined(__ILP32__) || defined(_ILP32) +#define DUK_F_X32 +#else +#define DUK_F_X64 +#endif +#elif defined(i386) || defined(__i386) || defined(__i386__) || \ + defined(__i486__) || defined(__i586__) || defined(__i686__) || \ + defined(__IA32__) || defined(_M_IX86) || defined(__X86__) || \ + defined(_X86_) || defined(__THW_INTEL__) || defined(__I86__) +#if defined(__LP64__) || defined(_LP64) +/* This should not really happen, but would indicate x64. */ +#define DUK_F_X64 +#else +#define DUK_F_X86 +#endif +#endif + +/* ARM */ +#if defined(__arm__) || defined(__thumb__) || defined(_ARM) || defined(_M_ARM) +#define DUK_F_ARM +#if defined(__LP64__) || defined(_LP64) || defined(__arm64) || defined(__arm64__) +#define DUK_F_ARM64 +#else +#define DUK_F_ARM32 +#endif +#endif + +/* MIPS. Related defines: __MIPSEB__, __MIPSEL__, __mips_isa_rev, __LP64__ */ +#if defined(__mips__) || defined(mips) || defined(_MIPS_ISA) || \ + defined(_R3000) || defined(_R4000) || defined(_R5900) || \ + defined(_MIPS_ISA_MIPS1) || defined(_MIPS_ISA_MIPS2) || \ + defined(_MIPS_ISA_MIPS3) || defined(_MIPS_ISA_MIPS4) || \ + defined(__mips) || defined(__MIPS__) +#define DUK_F_MIPS +#if defined(__LP64__) || defined(_LP64) || defined(__mips64) || \ + defined(__mips64__) || defined(__mips_n64) +#define DUK_F_MIPS64 +#else +#define DUK_F_MIPS32 +#endif +#endif + +/* SPARC */ +#if defined(sparc) || defined(__sparc) || defined(__sparc__) +#define DUK_F_SPARC +#if defined(__LP64__) || defined(_LP64) +#define DUK_F_SPARC64 +#else +#define DUK_F_SPARC32 +#endif +#endif + +/* SuperH */ +#if defined(__sh__) || \ + defined(__sh1__) || defined(__SH1__) || \ + defined(__sh2__) || defined(__SH2__) || \ + defined(__sh3__) || defined(__SH3__) || \ + defined(__sh4__) || defined(__SH4__) || \ + defined(__sh5__) || defined(__SH5__) +#define DUK_F_SUPERH +#endif + +/* Clang */ +#if defined(__clang__) +#define DUK_F_CLANG +#endif + +/* C99 or above */ +#undef DUK_F_C99 +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) +#define DUK_F_C99 +#endif + +/* C++11 or above */ +#undef DUK_F_CPP11 +#if defined(__cplusplus) && (__cplusplus >= 201103L) +#define DUK_F_CPP11 +#endif + +/* GCC. Clang also defines __GNUC__ so don't detect GCC if using Clang. */ +#if defined(__GNUC__) && !defined(__clang__) && !defined(DUK_F_CLANG) +#define DUK_F_GCC +#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) +/* Convenience, e.g. gcc 4.5.1 == 40501; http://stackoverflow.com/questions/6031819/emulating-gccs-builtin-unreachable */ +#define DUK_F_GCC_VERSION (__GNUC__ * 10000L + __GNUC_MINOR__ * 100L + __GNUC_PATCHLEVEL__) +#else +#error cannot figure out gcc version +#endif +#endif + +/* MinGW. Also GCC flags (DUK_F_GCC) are enabled now. */ +#if defined(__MINGW32__) || defined(__MINGW64__) +#define DUK_F_MINGW +#endif + +/* MSVC */ +#if defined(_MSC_VER) +/* MSVC preprocessor defines: http://msdn.microsoft.com/en-us/library/b0084kay.aspx + * _MSC_FULL_VER includes the build number, but it has at least two formats, see e.g. + * BOOST_MSVC_FULL_VER in http://www.boost.org/doc/libs/1_52_0/boost/config/compiler/visualc.hpp + */ +#define DUK_F_MSVC +#if defined(_MSC_FULL_VER) +#if (_MSC_FULL_VER > 100000000) +#define DUK_F_MSVC_FULL_VER _MSC_FULL_VER +#else +#define DUK_F_MSCV_FULL_VER (_MSC_FULL_VER * 10) +#endif +#endif +#endif /* _MSC_VER */ + +/* TinyC */ +#if defined(__TINYC__) +/* http://bellard.org/tcc/tcc-doc.html#SEC9 */ +#define DUK_F_TINYC +#endif + +/* VBCC */ +#if defined(__VBCC__) +#define DUK_F_VBCC +#endif + +/* Atari Mint */ +#if defined(__MINT__) +#define DUK_F_MINT +#endif + +/* + * Platform autodetection + */ + +/* Workaround for older C++ compilers before including , + * see e.g.: https://sourceware.org/bugzilla/show_bug.cgi?id=15366 + */ +#if defined(__cplusplus) && !defined(__STDC_LIMIT_MACROS) +#define __STDC_LIMIT_MACROS +#endif +#if defined(__cplusplus) && !defined(__STDC_CONSTANT_MACROS) +#define __STDC_CONSTANT_MACROS +#endif + +#if defined(DUK_F_APPLE) +/* --- Mac OSX, iPhone, Darwin --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +/* http://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor */ +#if TARGET_IPHONE_SIMULATOR +#define DUK_USE_OS_STRING "iphone-sim" +#elif TARGET_OS_IPHONE +#define DUK_USE_OS_STRING "iphone" +#elif TARGET_OS_MAC +#define DUK_USE_OS_STRING "osx" +#else +#define DUK_USE_OS_STRING "osx-unknown" +#endif + +/* Use _setjmp() on Apple by default, see GH-55. */ +#define DUK_JMPBUF_TYPE jmp_buf +#define DUK_SETJMP(jb) _setjmp((jb)) +#define DUK_LONGJMP(jb) _longjmp((jb), 1) +#elif defined(DUK_F_OPENBSD) +/* --- OpenBSD --- */ +/* http://www.monkey.org/openbsd/archive/ports/0401/msg00089.html */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "openbsd" +#elif defined(DUK_F_BSD) +/* --- Generic BSD --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "bsd" +#elif defined(DUK_F_TOS) +/* --- Atari ST TOS --- */ +#define DUK_USE_DATE_NOW_TIME +#define DUK_USE_DATE_TZO_GMTIME +/* no parsing (not an error) */ +#define DUK_USE_DATE_FMT_STRFTIME +#include + +#define DUK_USE_OS_STRING "tos" + +/* TOS on M68K is always big endian. */ +#if !defined(DUK_USE_BYTEORDER) && defined(DUK_F_M68K) +#define DUK_USE_BYTEORDER 3 +#endif +#elif defined(DUK_F_AMIGAOS) +/* --- AmigaOS --- */ +#if defined(DUK_F_M68K) +/* AmigaOS on M68k */ +#define DUK_USE_DATE_NOW_TIME +#define DUK_USE_DATE_TZO_GMTIME +/* no parsing (not an error) */ +#define DUK_USE_DATE_FMT_STRFTIME +#include +#elif defined(DUK_F_PPC) +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#ifndef UINTPTR_MAX +#define UINTPTR_MAX UINT_MAX +#endif +#else +#error AmigaOS but not M68K/PPC, not supported now +#endif + +#define DUK_USE_OS_STRING "amigaos" + +/* AmigaOS on M68K or PPC is always big endian. */ +#if !defined(DUK_USE_BYTEORDER) && (defined(DUK_F_M68K) || defined(DUK_F_PPC)) +#define DUK_USE_BYTEORDER 3 +#endif +#elif defined(DUK_F_WINDOWS) +/* --- Windows --- */ +/* Initial fix: disable secure CRT related warnings when compiling Duktape + * itself (must be defined before including Windows headers). Don't define + * for user code including duktape.h. + */ +#if defined(DUK_COMPILING_DUKTAPE) && !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +/* Windows 32-bit and 64-bit are currently the same. */ +/* MSVC does not have sys/param.h */ +#define DUK_USE_DATE_NOW_WINDOWS +#define DUK_USE_DATE_TZO_WINDOWS +/* Note: PRS and FMT are intentionally left undefined for now. This means + * there is no platform specific date parsing/formatting but there is still + * the ISO 8601 standard format. + */ +#if defined(DUK_COMPILING_DUKTAPE) +/* Only include when compiling Duktape to avoid polluting application build + * with a lot of unnecessary defines. + */ +#include +#endif + +#define DUK_USE_OS_STRING "windows" + +/* On Windows, assume we're little endian. Even Itanium which has a + * configurable endianness runs little endian in Windows. + */ +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +#elif defined(DUK_F_FLASHPLAYER) +/* --- Flashplayer (Crossbridge) --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "flashplayer" + +#if !defined(DUK_USE_BYTEORDER) && defined(DUK_F_FLASHPLAYER) +#define DUK_USE_BYTEORDER 1 +#endif +#elif defined(DUK_F_QNX) +/* --- QNX --- */ +#if defined(DUK_F_QNX) && defined(DUK_COMPILING_DUKTAPE) +/* See: /opt/qnx650/target/qnx6/usr/include/sys/platform.h */ +#define _XOPEN_SOURCE 600 +#define _POSIX_C_SOURCE 200112L +#endif + +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "qnx" +#elif defined(DUK_F_TINSPIRE) +/* --- TI-Nspire --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "tinspire" +#elif defined(DUK_F_EMSCRIPTEN) +/* --- Emscripten --- */ +#if defined(DUK_COMPILING_DUKTAPE) +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#ifndef _GNU_SOURCE +#define _GNU_SOURCE /* e.g. getdate_r */ +#endif +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE /* e.g. strptime */ +#endif +#endif /* DUK_COMPILING_DUKTAPE */ + +#include +#if defined(DUK_F_BCC) +/* no endian.h */ +#else +#include +#endif /* DUK_F_BCC */ +#include +#include +#include +#include + +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME + +#define DUK_USE_OS_STRING "emscripten" +#elif defined(DUK_F_LINUX) +/* --- Linux --- */ +#if defined(DUK_COMPILING_DUKTAPE) +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#ifndef _GNU_SOURCE +#define _GNU_SOURCE /* e.g. getdate_r */ +#endif +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE /* e.g. strptime */ +#endif +#endif /* DUK_COMPILING_DUKTAPE */ + +#include +#if defined(DUK_F_BCC) +/* no endian.h or stdint.h */ +#else +#include +#include +#endif /* DUK_F_BCC */ +#include +#include +#include + +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME + +#define DUK_USE_OS_STRING "linux" +#elif defined(DUK_F_SUN) +/* --- Solaris --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME + +#include +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "solaris" +#elif defined(DUK_F_POSIX) +/* --- Generic POSIX --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "posix" +#elif defined(DUK_F_CYGWIN) +/* --- Cygwin --- */ +/* don't use strptime() for now */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +#define DUK_JMPBUF_TYPE jmp_buf +#define DUK_SETJMP(jb) _setjmp((jb)) +#define DUK_LONGJMP(jb) _longjmp((jb), 1) + +#define DUK_USE_OS_STRING "windows" +#elif defined(DUK_F_UNIX) +/* --- Generic UNIX --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#define DUK_USE_OS_STRING "unknown" +#else +/* --- Generic fallback --- */ +/* The most portable current time provider is time(), but it only has a + * one second resolution. + */ +#define DUK_USE_DATE_NOW_TIME + +/* The most portable way to figure out local time offset is gmtime(), + * but it's not thread safe so use with caution. + */ +#define DUK_USE_DATE_TZO_GMTIME + +/* Avoid custom date parsing and formatting for portability. */ +#undef DUK_USE_DATE_PRS_STRPTIME +#undef DUK_USE_DATE_FMT_STRFTIME + +/* Rely on C89 headers only; time.h must be here. */ +#include + +#define DUK_USE_OS_STRING "unknown" +#endif /* autodetect platform */ + +/* Shared includes: C89 */ +#include +#include +#include +#include /* varargs */ +#include +#include /* e.g. ptrdiff_t */ +#include +#include + +/* date.h is omitted, and included per platform */ + +/* Shared includes: stdint.h is C99 */ +#if defined(DUK_F_NO_STDINT_H) +/* stdint.h not available */ +#else +/* Technically C99 (C++11) but found in many systems. On some systems + * __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS must be defined before + * including stdint.h (see above). + */ +#include +#endif + +#if defined(DUK_F_CPP) +#include /* std::exception */ +#endif + +/* + * Architecture autodetection + */ + +#if defined(DUK_F_X86) +/* --- x86 --- */ +#define DUK_USE_ARCH_STRING "x86" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +/* XXX: This is technically not guaranteed because it's possible to configure + * an x86 to require aligned accesses with Alignment Check (AC) flag. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 1 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_X64) +/* --- x64 --- */ +#define DUK_USE_ARCH_STRING "x64" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +/* XXX: This is technically not guaranteed because it's possible to configure + * an x86 to require aligned accesses with Alignment Check (AC) flag. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 1 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_X32) +/* --- x32 --- */ +#define DUK_USE_ARCH_STRING "x32" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +/* XXX: This is technically not guaranteed because it's possible to configure + * an x86 to require aligned accesses with Alignment Check (AC) flag. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 1 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_ARM32) +/* --- ARM 32-bit --- */ +#define DUK_USE_ARCH_STRING "arm32" +/* Byte order varies, so rely on autodetect. */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 4 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_ARM64) +/* --- ARM 64-bit --- */ +#define DUK_USE_ARCH_STRING "arm64" +/* Byte order varies, so rely on autodetect. */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_MIPS32) +/* --- MIPS 32-bit --- */ +#define DUK_USE_ARCH_STRING "mips32" +/* MIPS byte order varies so rely on autodetection. */ +/* Based on 'make checkalign' there are no alignment requirements on + * Linux MIPS except for doubles, which need align by 4. Alignment + * requirements vary based on target though. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 4 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_MIPS64) +/* --- MIPS 64-bit --- */ +#define DUK_USE_ARCH_STRING "mips64" +/* MIPS byte order varies so rely on autodetection. */ +/* Good default is a bit arbitrary because alignment requirements + * depend on target. See https://github.com/svaarala/duktape/issues/102. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_PPC32) +/* --- PowerPC 32-bit --- */ +#define DUK_USE_ARCH_STRING "ppc32" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 3 +#endif +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_PPC64) +/* --- PowerPC 64-bit --- */ +#define DUK_USE_ARCH_STRING "ppc64" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 3 +#endif +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_SPARC32) +/* --- SPARC 32-bit --- */ +#define DUK_USE_ARCH_STRING "sparc32" +/* SPARC byte order varies so rely on autodetection. */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_SPARC64) +/* --- SPARC 64-bit --- */ +#define DUK_USE_ARCH_STRING "sparc64" +/* SPARC byte order varies so rely on autodetection. */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_SUPERH) +/* --- SuperH --- */ +#define DUK_USE_ARCH_STRING "sh" +/* Byte order varies, rely on autodetection. */ +/* Based on 'make checkalign' there are no alignment requirements on + * Linux SH4, but align by 4 is probably a good basic default. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 4 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_M68K) +/* --- Motorola 68k --- */ +#define DUK_USE_ARCH_STRING "m68k" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 3 +#endif +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_EMSCRIPTEN) +/* --- Emscripten --- */ +#define DUK_USE_ARCH_STRING "emscripten" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#else +/* --- Generic --- */ +/* These are necessary wild guesses. */ +#define DUK_USE_ARCH_STRING "generic" +/* Rely on autodetection for byte order, alignment, and packed tval. */ +#endif /* autodetect architecture */ + +/* + * Compiler autodetection + */ + +#if defined(DUK_F_CLANG) +/* --- Clang --- */ +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +/* C99 / C++11 and above: rely on va_copy() which is required. */ +#define DUK_VA_COPY(dest,src) va_copy(dest,src) +#else +/* Clang: assume we have __va_copy() in non-C99 mode. */ +#define DUK_VA_COPY(dest,src) __va_copy(dest,src) +#endif + +#define DUK_NORETURN(decl) decl __attribute__((noreturn)) + +#if defined(__clang__) && defined(__has_builtin) +#if __has_builtin(__builtin_unreachable) +#define DUK_UNREACHABLE() do { __builtin_unreachable(); } while (0) +#endif +#endif + +#define DUK_USE_BRANCH_HINTS +#define DUK_LIKELY(x) __builtin_expect((x), 1) +#define DUK_UNLIKELY(x) __builtin_expect((x), 0) + +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_NOINLINE __attribute__((noinline)) +#define DUK_INLINE inline +#define DUK_ALWAYS_INLINE inline __attribute__((always_inline)) +#endif + +#if defined(DUK_F_DLL_BUILD) && defined(DUK_F_WINDOWS) +/* MSVC dllexport/dllimport: appropriate __declspec depends on whether we're + * compiling Duktape or the application. + */ +#if defined(DUK_COMPILING_DUKTAPE) +#define DUK_EXTERNAL_DECL extern __declspec(dllexport) +#define DUK_EXTERNAL __declspec(dllexport) +#else +#define DUK_EXTERNAL_DECL extern __declspec(dllimport) +#define DUK_EXTERNAL should_not_happen +#endif +#if defined(DUK_SINGLE_FILE) +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#else +#define DUK_INTERNAL_DECL extern +#define DUK_INTERNAL /*empty*/ +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static +#else +#define DUK_EXTERNAL_DECL __attribute__ ((visibility("default"))) extern +#define DUK_EXTERNAL __attribute__ ((visibility("default"))) +#if defined(DUK_SINGLE_FILE) +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +/* Minimize warnings for unused internal functions with GCC >= 3.1.1 and + * Clang. Based on documentation it should suffice to have the attribute + * in the declaration only, but in practice some warnings are generated unless + * the attribute is also applied to the definition. + */ +#define DUK_INTERNAL_DECL static __attribute__ ((unused)) +#define DUK_INTERNAL static __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#endif +#else +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) +#endif +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static +#endif + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "clang" +#else +#define DUK_USE_COMPILER_STRING "clang" +#endif + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_USE_VARIADIC_MACROS +#endif + +#define DUK_USE_UNION_INITIALIZERS + +#undef DUK_USE_FLEX_C99 +#undef DUK_USE_FLEX_ZEROSIZE +#undef DUK_USE_FLEX_ONESIZE +#if defined(DUK_F_C99) +#define DUK_USE_FLEX_C99 +#else +#define DUK_USE_FLEX_ZEROSIZE +#endif + +#undef DUK_USE_GCC_PRAGMAS +#define DUK_USE_PACK_CLANG_ATTR +#elif defined(DUK_F_GCC) +/* --- GCC --- */ +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +/* C99 / C++11 and above: rely on va_copy() which is required. */ +#define DUK_VA_COPY(dest,src) va_copy(dest,src) +#else +/* GCC: assume we have __va_copy() in non-C99 mode. */ +#define DUK_VA_COPY(dest,src) __va_copy(dest,src) +#endif + +#if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 20500L) +/* since gcc-2.5 */ +#define DUK_NORETURN(decl) decl __attribute__((noreturn)) +#endif + +#if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 40500L) +/* since gcc-4.5 */ +#define DUK_UNREACHABLE() do { __builtin_unreachable(); } while (0) +#endif + +#define DUK_USE_BRANCH_HINTS +#if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 40500L) +/* GCC: test not very accurate; enable only in relatively recent builds + * because of bugs in gcc-4.4 (http://lists.debian.org/debian-gcc/2010/04/msg00000.html) + */ +#define DUK_LIKELY(x) __builtin_expect((x), 1) +#define DUK_UNLIKELY(x) __builtin_expect((x), 0) +#endif + +#if (defined(DUK_F_C99) || defined(DUK_F_CPP11)) && \ + defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 30101) +#define DUK_NOINLINE __attribute__((noinline)) +#define DUK_INLINE inline +#define DUK_ALWAYS_INLINE inline __attribute__((always_inline)) +#endif + +#if defined(DUK_F_DLL_BUILD) && defined(DUK_F_WINDOWS) +/* MSVC dllexport/dllimport: appropriate __declspec depends on whether we're + * compiling Duktape or the application. + */ +#if defined(DUK_COMPILING_DUKTAPE) +#define DUK_EXTERNAL_DECL extern __declspec(dllexport) +#define DUK_EXTERNAL __declspec(dllexport) +#else +#define DUK_EXTERNAL_DECL extern __declspec(dllimport) +#define DUK_EXTERNAL should_not_happen +#endif +#if defined(DUK_SINGLE_FILE) +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#else +#define DUK_INTERNAL_DECL extern +#define DUK_INTERNAL /*empty*/ +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static +#elif defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 40000) +#define DUK_EXTERNAL_DECL __attribute__ ((visibility("default"))) extern +#define DUK_EXTERNAL __attribute__ ((visibility("default"))) +#if defined(DUK_SINGLE_FILE) +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +/* Minimize warnings for unused internal functions with GCC >= 3.1.1 and + * Clang. Based on documentation it should suffice to have the attribute + * in the declaration only, but in practice some warnings are generated unless + * the attribute is also applied to the definition. + */ +#define DUK_INTERNAL_DECL static __attribute__ ((unused)) +#define DUK_INTERNAL static __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#endif +#else +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) +#endif +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static +#endif + +#if defined(DUK_F_MINGW) +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "mingw++" +#else +#define DUK_USE_COMPILER_STRING "mingw" +#endif +#else +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "g++" +#else +#define DUK_USE_COMPILER_STRING "gcc" +#endif +#endif + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) || (defined(DUK_F_CPP11) && defined(__GNUC__)) +#define DUK_USE_VARIADIC_MACROS +#endif + +#define DUK_USE_UNION_INITIALIZERS + +#undef DUK_USE_FLEX_C99 +#undef DUK_USE_FLEX_ZEROSIZE +#undef DUK_USE_FLEX_ONESIZE +#if defined(DUK_F_C99) +#define DUK_USE_FLEX_C99 +#else +#define DUK_USE_FLEX_ZEROSIZE +#endif + +#if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 40600) +#define DUK_USE_GCC_PRAGMAS +#else +#undef DUK_USE_GCC_PRAGMAS +#endif + +#define DUK_USE_PACK_GCC_ATTR +#elif defined(DUK_F_MSVC) +/* --- MSVC --- */ +/* http://msdn.microsoft.com/en-us/library/aa235362(VS.60).aspx */ +#define DUK_NORETURN(decl) __declspec(noreturn) decl + +/* XXX: DUK_UNREACHABLE for msvc? */ + +#undef DUK_USE_BRANCH_HINTS + +/* XXX: DUK_LIKELY, DUK_UNLIKELY for msvc? */ +/* XXX: DUK_NOINLINE, DUK_INLINE, DUK_ALWAYS_INLINE for msvc? */ + +#if defined(DUK_F_DLL_BUILD) && defined(DUK_F_WINDOWS) +/* MSVC dllexport/dllimport: appropriate __declspec depends on whether we're + * compiling Duktape or the application. + */ +#if defined(DUK_COMPILING_DUKTAPE) +#define DUK_EXTERNAL_DECL extern __declspec(dllexport) +#define DUK_EXTERNAL __declspec(dllexport) +#else +#define DUK_EXTERNAL_DECL extern __declspec(dllimport) +#define DUK_EXTERNAL should_not_happen +#endif +#if defined(DUK_SINGLE_FILE) +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#else +#define DUK_INTERNAL_DECL extern +#define DUK_INTERNAL /*empty*/ +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static +#endif + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "msvc++" +#else +#define DUK_USE_COMPILER_STRING "msvc" +#endif + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) +#define DUK_USE_VARIADIC_MACROS +#elif defined(_MSC_VER) && (_MSC_VER >= 1400) +/* VS2005+ should have variadic macros even when they're not C99. */ +#define DUK_USE_VARIADIC_MACROS +#endif + +#undef DUK_USE_UNION_INITIALIZERS +#if defined(_MSC_VER) && (_MSC_VER >= 1800) +/* VS2013+ supports union initializers but there's a bug involving union-inside-struct: + * https://connect.microsoft.com/VisualStudio/feedback/details/805981 + * The bug was fixed (at least) in VS2015 so check for VS2015 for now: + * https://blogs.msdn.microsoft.com/vcblog/2015/07/01/c-compiler-front-end-fixes-in-vs2015/ + * Manually tested using VS2013, CL reports 18.00.31101, so enable for VS2013 too. + */ +#define DUK_USE_UNION_INITIALIZERS +#endif + +#undef DUK_USE_FLEX_C99 +#undef DUK_USE_FLEX_ZEROSIZE +#undef DUK_USE_FLEX_ONESIZE +#if defined(DUK_F_C99) +#define DUK_USE_FLEX_C99 +#else +#define DUK_USE_FLEX_ZEROSIZE +#endif + +#undef DUK_USE_GCC_PRAGMAS + +#define DUK_USE_PACK_MSVC_PRAGMA + +/* These have been tested from VS2008 onwards; may work in older VS versions + * too but not enabled by default. + */ +#if defined(_MSC_VER) && (_MSC_VER >= 1500) +#define DUK_NOINLINE __declspec(noinline) +#define DUK_INLINE __inline +#define DUK_ALWAYS_INLINE __forceinline +#endif + +#if defined(_MSC_VER) && (_MSC_VER >= 1900) +#define DUK_SNPRINTF snprintf +#define DUK_VSNPRINTF vsnprintf +#else +/* (v)snprintf() is missing before MSVC 2015. Note that _(v)snprintf() does + * NOT NUL terminate on truncation, but Duktape code never assumes that. + * http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010 + */ +#define DUK_SNPRINTF _snprintf +#define DUK_VSNPRINTF _vsnprintf +#endif +#elif defined(DUK_F_EMSCRIPTEN) +/* --- Emscripten --- */ +#define DUK_NORETURN(decl) decl __attribute__((noreturn)) + +#if defined(__clang__) && defined(__has_builtin) +#if __has_builtin(__builtin_unreachable) +#define DUK_UNREACHABLE() do { __builtin_unreachable(); } while (0) +#endif +#endif + +#define DUK_USE_BRANCH_HINTS +#define DUK_LIKELY(x) __builtin_expect((x), 1) +#define DUK_UNLIKELY(x) __builtin_expect((x), 0) + +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_NOINLINE __attribute__((noinline)) +#define DUK_INLINE inline +#define DUK_ALWAYS_INLINE inline __attribute__((always_inline)) +#endif + +#define DUK_EXTERNAL_DECL __attribute__ ((visibility("default"))) extern +#define DUK_EXTERNAL __attribute__ ((visibility("default"))) +#if defined(DUK_SINGLE_FILE) +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +/* Minimize warnings for unused internal functions with GCC >= 3.1.1 and + * Clang. Based on documentation it should suffice to have the attribute + * in the declaration only, but in practice some warnings are generated unless + * the attribute is also applied to the definition. + */ +#define DUK_INTERNAL_DECL static __attribute__ ((unused)) +#define DUK_INTERNAL static __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#endif +#else +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) +#endif +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static + +#define DUK_USE_COMPILER_STRING "emscripten" + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_USE_VARIADIC_MACROS +#endif + +#define DUK_USE_UNION_INITIALIZERS + +#undef DUK_USE_FLEX_C99 +#undef DUK_USE_FLEX_ZEROSIZE +#undef DUK_USE_FLEX_ONESIZE +#if defined(DUK_F_C99) +#define DUK_USE_FLEX_C99 +#else +#define DUK_USE_FLEX_ZEROSIZE +#endif + +#undef DUK_USE_GCC_PRAGMAS +#define DUK_USE_PACK_CLANG_ATTR +#elif defined(DUK_F_TINYC) +/* --- TinyC --- */ +#undef DUK_USE_BRANCH_HINTS + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "tinyc++" +#else +#define DUK_USE_COMPILER_STRING "tinyc" +#endif + +/* http://bellard.org/tcc/tcc-doc.html#SEC7 */ +#define DUK_USE_VARIADIC_MACROS + +#define DUK_USE_UNION_INITIALIZERS + +/* Most portable, wastes space */ +#define DUK_USE_FLEX_ONESIZE + +/* Most portable, potentially wastes space */ +#define DUK_USE_PACK_DUMMY_MEMBER +#elif defined(DUK_F_VBCC) +/* --- VBCC --- */ +#undef DUK_USE_BRANCH_HINTS + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "vbcc-c++" +#else +#define DUK_USE_COMPILER_STRING "vbcc" +#endif + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_USE_VARIADIC_MACROS +#endif + +/* VBCC supports C99 so check only for C99 for union initializer support. + * Designated union initializers would possibly work even without a C99 check. + */ +#undef DUK_USE_UNION_INITIALIZERS +#if defined(DUK_F_C99) +#define DUK_USE_UNION_INITIALIZERS +#endif + +#define DUK_USE_FLEX_ZEROSIZE +#define DUK_USE_PACK_DUMMY_MEMBER +#elif defined(DUK_F_BCC) +/* --- Bruce's C compiler --- */ +#undef DUK_USE_BRANCH_HINTS + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "bcc++" +#else +#define DUK_USE_COMPILER_STRING "bcc" +#endif + +/* Most portable */ +#undef DUK_USE_VARIADIC_MACROS + +/* Most portable, wastes space */ +#undef DUK_USE_UNION_INITIALIZERS + +/* Most portable, wastes space */ +#define DUK_USE_FLEX_ONESIZE + +/* Most portable, potentially wastes space */ +#define DUK_USE_PACK_DUMMY_MEMBER + +/* BCC, assume we're on x86. */ +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +#else +/* --- Generic --- */ +#undef DUK_USE_BRANCH_HINTS + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "generic-c++" +#else +#define DUK_USE_COMPILER_STRING "generic" +#endif + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_USE_VARIADIC_MACROS +#endif + +/* C++ doesn't have standard designated union initializers ({ .foo = 1 }). */ +#undef DUK_USE_UNION_INITIALIZERS +#if defined(DUK_F_C99) +#define DUK_USE_UNION_INITIALIZERS +#endif + +/* Most portable, wastes space */ +#define DUK_USE_FLEX_ONESIZE + +/* Most portable, potentially wastes space */ +#define DUK_USE_PACK_DUMMY_MEMBER +#endif /* autodetect compiler */ + +/* uclibc */ +#if defined(__UCLIBC__) +#define DUK_F_UCLIBC +#endif + +/* + * Wrapper typedefs and constants for integer types, also sanity check types. + * + * C99 typedefs are quite good but not always available, and we want to avoid + * forcibly redefining the C99 typedefs. So, there are Duktape wrappers for + * all C99 typedefs and Duktape code should only use these typedefs. Type + * detection when C99 is not supported is best effort and may end up detecting + * some types incorrectly. + * + * Pointer sizes are a portability problem: pointers to different types may + * have a different size and function pointers are very difficult to manage + * portably. + * + * http://en.wikipedia.org/wiki/C_data_types#Fixed-width_integer_types + * + * Note: there's an interesting corner case when trying to define minimum + * signed integer value constants which leads to the current workaround of + * defining e.g. -0x80000000 as (-0x7fffffffL - 1L). See doc/code-issues.txt + * for a longer discussion. + * + * Note: avoid typecasts and computations in macro integer constants as they + * can then no longer be used in macro relational expressions (such as + * #if DUK_SIZE_MAX < 0xffffffffUL). There is internal code which relies on + * being able to compare DUK_SIZE_MAX against a limit. + */ + +/* XXX: add feature options to force basic types from outside? */ + +#if !defined(INT_MAX) +#error INT_MAX not defined +#endif + +/* Check that architecture is two's complement, standard C allows e.g. + * INT_MIN to be -2**31+1 (instead of -2**31). + */ +#if defined(INT_MAX) && defined(INT_MIN) +#if INT_MAX != -(INT_MIN + 1) +#error platform does not seem complement of two +#endif +#else +#error cannot check complement of two +#endif + +/* Pointer size determination based on __WORDSIZE or architecture when + * that's not available. + */ +#if defined(DUK_F_X86) || defined(DUK_F_X32) || \ + defined(DUK_F_M68K) || defined(DUK_F_PPC32) || \ + defined(DUK_F_BCC) || \ + (defined(__WORDSIZE) && (__WORDSIZE == 32)) +#define DUK_F_32BIT_PTRS +#elif defined(DUK_F_X64) || \ + (defined(__WORDSIZE) && (__WORDSIZE == 64)) +#define DUK_F_64BIT_PTRS +#else +/* not sure, not needed with C99 anyway */ +#endif + +/* Intermediate define for 'have inttypes.h' */ +#undef DUK_F_HAVE_INTTYPES +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \ + !(defined(DUK_F_AMIGAOS) && defined(DUK_F_VBCC)) +/* vbcc + AmigaOS has C99 but no inttypes.h */ +#define DUK_F_HAVE_INTTYPES +#elif defined(__cplusplus) && (__cplusplus >= 201103L) +/* C++11 apparently ratified stdint.h */ +#define DUK_F_HAVE_INTTYPES +#endif + +/* Basic integer typedefs and limits, preferably from inttypes.h, otherwise + * through automatic detection. + */ +#if defined(DUK_F_HAVE_INTTYPES) +/* C99 or compatible */ + +#define DUK_F_HAVE_64BIT +#include + +typedef uint8_t duk_uint8_t; +typedef int8_t duk_int8_t; +typedef uint16_t duk_uint16_t; +typedef int16_t duk_int16_t; +typedef uint32_t duk_uint32_t; +typedef int32_t duk_int32_t; +typedef uint64_t duk_uint64_t; +typedef int64_t duk_int64_t; +typedef uint_least8_t duk_uint_least8_t; +typedef int_least8_t duk_int_least8_t; +typedef uint_least16_t duk_uint_least16_t; +typedef int_least16_t duk_int_least16_t; +typedef uint_least32_t duk_uint_least32_t; +typedef int_least32_t duk_int_least32_t; +typedef uint_least64_t duk_uint_least64_t; +typedef int_least64_t duk_int_least64_t; +typedef uint_fast8_t duk_uint_fast8_t; +typedef int_fast8_t duk_int_fast8_t; +typedef uint_fast16_t duk_uint_fast16_t; +typedef int_fast16_t duk_int_fast16_t; +typedef uint_fast32_t duk_uint_fast32_t; +typedef int_fast32_t duk_int_fast32_t; +typedef uint_fast64_t duk_uint_fast64_t; +typedef int_fast64_t duk_int_fast64_t; +typedef uintptr_t duk_uintptr_t; +typedef intptr_t duk_intptr_t; +typedef uintmax_t duk_uintmax_t; +typedef intmax_t duk_intmax_t; + +#define DUK_UINT8_MIN 0 +#define DUK_UINT8_MAX UINT8_MAX +#define DUK_INT8_MIN INT8_MIN +#define DUK_INT8_MAX INT8_MAX +#define DUK_UINT_LEAST8_MIN 0 +#define DUK_UINT_LEAST8_MAX UINT_LEAST8_MAX +#define DUK_INT_LEAST8_MIN INT_LEAST8_MIN +#define DUK_INT_LEAST8_MAX INT_LEAST8_MAX +#define DUK_UINT_FAST8_MIN 0 +#define DUK_UINT_FAST8_MAX UINT_FAST8_MAX +#define DUK_INT_FAST8_MIN INT_FAST8_MIN +#define DUK_INT_FAST8_MAX INT_FAST8_MAX +#define DUK_UINT16_MIN 0 +#define DUK_UINT16_MAX UINT16_MAX +#define DUK_INT16_MIN INT16_MIN +#define DUK_INT16_MAX INT16_MAX +#define DUK_UINT_LEAST16_MIN 0 +#define DUK_UINT_LEAST16_MAX UINT_LEAST16_MAX +#define DUK_INT_LEAST16_MIN INT_LEAST16_MIN +#define DUK_INT_LEAST16_MAX INT_LEAST16_MAX +#define DUK_UINT_FAST16_MIN 0 +#define DUK_UINT_FAST16_MAX UINT_FAST16_MAX +#define DUK_INT_FAST16_MIN INT_FAST16_MIN +#define DUK_INT_FAST16_MAX INT_FAST16_MAX +#define DUK_UINT32_MIN 0 +#define DUK_UINT32_MAX UINT32_MAX +#define DUK_INT32_MIN INT32_MIN +#define DUK_INT32_MAX INT32_MAX +#define DUK_UINT_LEAST32_MIN 0 +#define DUK_UINT_LEAST32_MAX UINT_LEAST32_MAX +#define DUK_INT_LEAST32_MIN INT_LEAST32_MIN +#define DUK_INT_LEAST32_MAX INT_LEAST32_MAX +#define DUK_UINT_FAST32_MIN 0 +#define DUK_UINT_FAST32_MAX UINT_FAST32_MAX +#define DUK_INT_FAST32_MIN INT_FAST32_MIN +#define DUK_INT_FAST32_MAX INT_FAST32_MAX +#define DUK_UINT64_MIN 0 +#define DUK_UINT64_MAX UINT64_MAX +#define DUK_INT64_MIN INT64_MIN +#define DUK_INT64_MAX INT64_MAX +#define DUK_UINT_LEAST64_MIN 0 +#define DUK_UINT_LEAST64_MAX UINT_LEAST64_MAX +#define DUK_INT_LEAST64_MIN INT_LEAST64_MIN +#define DUK_INT_LEAST64_MAX INT_LEAST64_MAX +#define DUK_UINT_FAST64_MIN 0 +#define DUK_UINT_FAST64_MAX UINT_FAST64_MAX +#define DUK_INT_FAST64_MIN INT_FAST64_MIN +#define DUK_INT_FAST64_MAX INT_FAST64_MAX + +#define DUK_UINTPTR_MIN 0 +#define DUK_UINTPTR_MAX UINTPTR_MAX +#define DUK_INTPTR_MIN INTPTR_MIN +#define DUK_INTPTR_MAX INTPTR_MAX + +#define DUK_UINTMAX_MIN 0 +#define DUK_UINTMAX_MAX UINTMAX_MAX +#define DUK_INTMAX_MIN INTMAX_MIN +#define DUK_INTMAX_MAX INTMAX_MAX + +#define DUK_SIZE_MIN 0 +#define DUK_SIZE_MAX SIZE_MAX +#undef DUK_SIZE_MAX_COMPUTED + +#else /* C99 types */ + +/* When C99 types are not available, we use heuristic detection to get + * the basic 8, 16, 32, and (possibly) 64 bit types. The fast/least + * types are then assumed to be exactly the same for now: these could + * be improved per platform but C99 types are very often now available. + * 64-bit types are not available on all platforms; this is OK at least + * on 32-bit platforms. + * + * This detection code is necessarily a bit hacky and can provide typedefs + * and defines that won't work correctly on some exotic platform. + */ + +#if (defined(CHAR_BIT) && (CHAR_BIT == 8)) || \ + (defined(UCHAR_MAX) && (UCHAR_MAX == 255)) +typedef unsigned char duk_uint8_t; +typedef signed char duk_int8_t; +#else +#error cannot detect 8-bit type +#endif + +#if defined(USHRT_MAX) && (USHRT_MAX == 65535UL) +typedef unsigned short duk_uint16_t; +typedef signed short duk_int16_t; +#elif defined(UINT_MAX) && (UINT_MAX == 65535UL) +/* On some platforms int is 16-bit but long is 32-bit (e.g. PureC) */ +typedef unsigned int duk_uint16_t; +typedef signed int duk_int16_t; +#else +#error cannot detect 16-bit type +#endif + +#if defined(UINT_MAX) && (UINT_MAX == 4294967295UL) +typedef unsigned int duk_uint32_t; +typedef signed int duk_int32_t; +#elif defined(ULONG_MAX) && (ULONG_MAX == 4294967295UL) +/* On some platforms int is 16-bit but long is 32-bit (e.g. PureC) */ +typedef unsigned long duk_uint32_t; +typedef signed long duk_int32_t; +#else +#error cannot detect 32-bit type +#endif + +/* 64-bit type detection is a bit tricky. + * + * ULLONG_MAX is a standard define. __LONG_LONG_MAX__ and __ULONG_LONG_MAX__ + * are used by at least GCC (even if system headers don't provide ULLONG_MAX). + * Some GCC variants may provide __LONG_LONG_MAX__ but not __ULONG_LONG_MAX__. + * + * ULL / LL constants are rejected / warned about by some compilers, even if + * the compiler has a 64-bit type and the compiler/system headers provide an + * unsupported constant (ULL/LL)! Try to avoid using ULL / LL constants. + * As a side effect we can only check that e.g. ULONG_MAX is larger than 32 + * bits but can't be sure it is exactly 64 bits. Self tests will catch such + * cases. + */ +#undef DUK_F_HAVE_64BIT +#if !defined(DUK_F_HAVE_64BIT) && defined(ULONG_MAX) +#if (ULONG_MAX > 4294967295UL) +#define DUK_F_HAVE_64BIT +typedef unsigned long duk_uint64_t; +typedef signed long duk_int64_t; +#endif +#endif +#if !defined(DUK_F_HAVE_64BIT) && defined(ULLONG_MAX) +#if (ULLONG_MAX > 4294967295UL) +#define DUK_F_HAVE_64BIT +typedef unsigned long long duk_uint64_t; +typedef signed long long duk_int64_t; +#endif +#endif +#if !defined(DUK_F_HAVE_64BIT) && defined(__ULONG_LONG_MAX__) +#if (__ULONG_LONG_MAX__ > 4294967295UL) +#define DUK_F_HAVE_64BIT +typedef unsigned long long duk_uint64_t; +typedef signed long long duk_int64_t; +#endif +#endif +#if !defined(DUK_F_HAVE_64BIT) && defined(__LONG_LONG_MAX__) +#if (__LONG_LONG_MAX__ > 2147483647L) +#define DUK_F_HAVE_64BIT +typedef unsigned long long duk_uint64_t; +typedef signed long long duk_int64_t; +#endif +#endif +#if !defined(DUK_F_HAVE_64BIT) && \ + (defined(DUK_F_MINGW) || defined(DUK_F_MSVC)) +/* Both MinGW and MSVC have a 64-bit type. */ +#define DUK_F_HAVE_64BIT +typedef unsigned long duk_uint64_t; +typedef signed long duk_int64_t; +#endif +#if !defined(DUK_F_HAVE_64BIT) +/* cannot detect 64-bit type, not always needed so don't error */ +#endif + +typedef duk_uint8_t duk_uint_least8_t; +typedef duk_int8_t duk_int_least8_t; +typedef duk_uint16_t duk_uint_least16_t; +typedef duk_int16_t duk_int_least16_t; +typedef duk_uint32_t duk_uint_least32_t; +typedef duk_int32_t duk_int_least32_t; +typedef duk_uint8_t duk_uint_fast8_t; +typedef duk_int8_t duk_int_fast8_t; +typedef duk_uint16_t duk_uint_fast16_t; +typedef duk_int16_t duk_int_fast16_t; +typedef duk_uint32_t duk_uint_fast32_t; +typedef duk_int32_t duk_int_fast32_t; +#if defined(DUK_F_HAVE_64BIT) +typedef duk_uint64_t duk_uint_least64_t; +typedef duk_int64_t duk_int_least64_t; +typedef duk_uint64_t duk_uint_fast64_t; +typedef duk_int64_t duk_int_fast64_t; +#endif +#if defined(DUK_F_HAVE_64BIT) +typedef duk_uint64_t duk_uintmax_t; +typedef duk_int64_t duk_intmax_t; +#else +typedef duk_uint32_t duk_uintmax_t; +typedef duk_int32_t duk_intmax_t; +#endif + +/* Note: the funny looking computations for signed minimum 16-bit, 32-bit, and + * 64-bit values are intentional as the obvious forms (e.g. -0x80000000L) are + * -not- portable. See code-issues.txt for a detailed discussion. + */ +#define DUK_UINT8_MIN 0UL +#define DUK_UINT8_MAX 0xffUL +#define DUK_INT8_MIN (-0x80L) +#define DUK_INT8_MAX 0x7fL +#define DUK_UINT_LEAST8_MIN 0UL +#define DUK_UINT_LEAST8_MAX 0xffUL +#define DUK_INT_LEAST8_MIN (-0x80L) +#define DUK_INT_LEAST8_MAX 0x7fL +#define DUK_UINT_FAST8_MIN 0UL +#define DUK_UINT_FAST8_MAX 0xffUL +#define DUK_INT_FAST8_MIN (-0x80L) +#define DUK_INT_FAST8_MAX 0x7fL +#define DUK_UINT16_MIN 0UL +#define DUK_UINT16_MAX 0xffffUL +#define DUK_INT16_MIN (-0x7fffL - 1L) +#define DUK_INT16_MAX 0x7fffL +#define DUK_UINT_LEAST16_MIN 0UL +#define DUK_UINT_LEAST16_MAX 0xffffUL +#define DUK_INT_LEAST16_MIN (-0x7fffL - 1L) +#define DUK_INT_LEAST16_MAX 0x7fffL +#define DUK_UINT_FAST16_MIN 0UL +#define DUK_UINT_FAST16_MAX 0xffffUL +#define DUK_INT_FAST16_MIN (-0x7fffL - 1L) +#define DUK_INT_FAST16_MAX 0x7fffL +#define DUK_UINT32_MIN 0UL +#define DUK_UINT32_MAX 0xffffffffUL +#define DUK_INT32_MIN (-0x7fffffffL - 1L) +#define DUK_INT32_MAX 0x7fffffffL +#define DUK_UINT_LEAST32_MIN 0UL +#define DUK_UINT_LEAST32_MAX 0xffffffffUL +#define DUK_INT_LEAST32_MIN (-0x7fffffffL - 1L) +#define DUK_INT_LEAST32_MAX 0x7fffffffL +#define DUK_UINT_FAST32_MIN 0UL +#define DUK_UINT_FAST32_MAX 0xffffffffUL +#define DUK_INT_FAST32_MIN (-0x7fffffffL - 1L) +#define DUK_INT_FAST32_MAX 0x7fffffffL + +/* 64-bit constants. Since LL / ULL constants are not always available, + * use computed values. These values can't be used in preprocessor + * comparisons; flag them as such. + */ +#if defined(DUK_F_HAVE_64BIT) +#define DUK_UINT64_MIN ((duk_uint64_t) 0) +#define DUK_UINT64_MAX ((duk_uint64_t) -1) +#define DUK_INT64_MIN ((duk_int64_t) (~(DUK_UINT64_MAX >> 1))) +#define DUK_INT64_MAX ((duk_int64_t) (DUK_UINT64_MAX >> 1)) +#define DUK_UINT_LEAST64_MIN DUK_UINT64_MIN +#define DUK_UINT_LEAST64_MAX DUK_UINT64_MAX +#define DUK_INT_LEAST64_MIN DUK_INT64_MIN +#define DUK_INT_LEAST64_MAX DUK_INT64_MAX +#define DUK_UINT_FAST64_MIN DUK_UINT64_MIN +#define DUK_UINT_FAST64_MAX DUK_UINT64_MAX +#define DUK_INT_FAST64_MIN DUK_INT64_MIN +#define DUK_INT_FAST64_MAX DUK_INT64_MAX +#define DUK_UINT64_MIN_COMPUTED +#define DUK_UINT64_MAX_COMPUTED +#define DUK_INT64_MIN_COMPUTED +#define DUK_INT64_MAX_COMPUTED +#define DUK_UINT_LEAST64_MIN_COMPUTED +#define DUK_UINT_LEAST64_MAX_COMPUTED +#define DUK_INT_LEAST64_MIN_COMPUTED +#define DUK_INT_LEAST64_MAX_COMPUTED +#define DUK_UINT_FAST64_MIN_COMPUTED +#define DUK_UINT_FAST64_MAX_COMPUTED +#define DUK_INT_FAST64_MIN_COMPUTED +#define DUK_INT_FAST64_MAX_COMPUTED +#endif + +#if defined(DUK_F_HAVE_64BIT) +#define DUK_UINTMAX_MIN DUK_UINT64_MIN +#define DUK_UINTMAX_MAX DUK_UINT64_MAX +#define DUK_INTMAX_MIN DUK_INT64_MIN +#define DUK_INTMAX_MAX DUK_INT64_MAX +#define DUK_UINTMAX_MIN_COMPUTED +#define DUK_UINTMAX_MAX_COMPUTED +#define DUK_INTMAX_MIN_COMPUTED +#define DUK_INTMAX_MAX_COMPUTED +#else +#define DUK_UINTMAX_MIN 0UL +#define DUK_UINTMAX_MAX 0xffffffffUL +#define DUK_INTMAX_MIN (-0x7fffffffL - 1L) +#define DUK_INTMAX_MAX 0x7fffffffL +#endif + +/* This detection is not very reliable. */ +#if defined(DUK_F_32BIT_PTRS) +typedef duk_int32_t duk_intptr_t; +typedef duk_uint32_t duk_uintptr_t; +#define DUK_UINTPTR_MIN DUK_UINT32_MIN +#define DUK_UINTPTR_MAX DUK_UINT32_MAX +#define DUK_INTPTR_MIN DUK_INT32_MIN +#define DUK_INTPTR_MAX DUK_INT32_MAX +#elif defined(DUK_F_64BIT_PTRS) && defined(DUK_F_HAVE_64BIT) +typedef duk_int64_t duk_intptr_t; +typedef duk_uint64_t duk_uintptr_t; +#define DUK_UINTPTR_MIN DUK_UINT64_MIN +#define DUK_UINTPTR_MAX DUK_UINT64_MAX +#define DUK_INTPTR_MIN DUK_INT64_MIN +#define DUK_INTPTR_MAX DUK_INT64_MAX +#define DUK_UINTPTR_MIN_COMPUTED +#define DUK_UINTPTR_MAX_COMPUTED +#define DUK_INTPTR_MIN_COMPUTED +#define DUK_INTPTR_MAX_COMPUTED +#else +#error cannot determine intptr type +#endif + +/* SIZE_MAX may be missing so use an approximate value for it. */ +#undef DUK_SIZE_MAX_COMPUTED +#if !defined(SIZE_MAX) +#define DUK_SIZE_MAX_COMPUTED +#define SIZE_MAX ((size_t) (-1)) +#endif +#define DUK_SIZE_MIN 0 +#define DUK_SIZE_MAX SIZE_MAX + +#endif /* C99 types */ + +/* A few types are assumed to always exist. */ +typedef size_t duk_size_t; +typedef ptrdiff_t duk_ptrdiff_t; + +/* The best type for an "all around int" in Duktape internals is "at least + * 32 bit signed integer" which is most convenient. Same for unsigned type. + * Prefer 'int' when large enough, as it is almost always a convenient type. + */ +#if defined(UINT_MAX) && (UINT_MAX >= 0xffffffffUL) +typedef int duk_int_t; +typedef unsigned int duk_uint_t; +#define DUK_INT_MIN INT_MIN +#define DUK_INT_MAX INT_MAX +#define DUK_UINT_MIN 0 +#define DUK_UINT_MAX UINT_MAX +#else +typedef duk_int_fast32_t duk_int_t; +typedef duk_uint_fast32_t duk_uint_t; +#define DUK_INT_MIN DUK_INT_FAST32_MIN +#define DUK_INT_MAX DUK_INT_FAST32_MAX +#define DUK_UINT_MIN DUK_UINT_FAST32_MIN +#define DUK_UINT_MAX DUK_UINT_FAST32_MAX +#endif + +/* Same as 'duk_int_t' but guaranteed to be a 'fast' variant if this + * distinction matters for the CPU. These types are used mainly in the + * executor where it might really matter. + */ +typedef duk_int_fast32_t duk_int_fast_t; +typedef duk_uint_fast32_t duk_uint_fast_t; +#define DUK_INT_FAST_MIN DUK_INT_FAST32_MIN +#define DUK_INT_FAST_MAX DUK_INT_FAST32_MAX +#define DUK_UINT_FAST_MIN DUK_UINT_FAST32_MIN +#define DUK_UINT_FAST_MAX DUK_UINT_FAST32_MAX + +/* Small integers (16 bits or more) can fall back to the 'int' type, but + * have a typedef so they are marked "small" explicitly. + */ +typedef int duk_small_int_t; +typedef unsigned int duk_small_uint_t; +#define DUK_SMALL_INT_MIN INT_MIN +#define DUK_SMALL_INT_MAX INT_MAX +#define DUK_SMALL_UINT_MIN 0 +#define DUK_SMALL_UINT_MAX UINT_MAX + +/* Fast variants of small integers, again for really fast paths like the + * executor. + */ +typedef duk_int_fast16_t duk_small_int_fast_t; +typedef duk_uint_fast16_t duk_small_uint_fast_t; +#define DUK_SMALL_INT_FAST_MIN DUK_INT_FAST16_MIN +#define DUK_SMALL_INT_FAST_MAX DUK_INT_FAST16_MAX +#define DUK_SMALL_UINT_FAST_MIN DUK_UINT_FAST16_MIN +#define DUK_SMALL_UINT_FAST_MAX DUK_UINT_FAST16_MAX + +/* Boolean values are represented with the platform 'int'. */ +typedef duk_small_int_t duk_bool_t; +#define DUK_BOOL_MIN DUK_SMALL_INT_MIN +#define DUK_BOOL_MAX DUK_SMALL_INT_MAX + +/* Index values must have at least 32-bit signed range. */ +typedef duk_int_t duk_idx_t; +#define DUK_IDX_MIN DUK_INT_MIN +#define DUK_IDX_MAX DUK_INT_MAX + +/* Unsigned index variant. */ +typedef duk_uint_t duk_uidx_t; +#define DUK_UIDX_MIN DUK_UINT_MIN +#define DUK_UIDX_MAX DUK_UINT_MAX + +/* Array index values, could be exact 32 bits. + * Currently no need for signed duk_arridx_t. + */ +typedef duk_uint_t duk_uarridx_t; +#define DUK_UARRIDX_MIN DUK_UINT_MIN +#define DUK_UARRIDX_MAX DUK_UINT_MAX + +/* Duktape/C function return value, platform int is enough for now to + * represent 0, 1, or negative error code. Must be compatible with + * assigning truth values (e.g. duk_ret_t rc = (foo == bar);). + */ +typedef duk_small_int_t duk_ret_t; +#define DUK_RET_MIN DUK_SMALL_INT_MIN +#define DUK_RET_MAX DUK_SMALL_INT_MAX + +/* Error codes are represented with platform int. High bits are used + * for flags and such, so 32 bits are needed. + */ +typedef duk_int_t duk_errcode_t; +#define DUK_ERRCODE_MIN DUK_INT_MIN +#define DUK_ERRCODE_MAX DUK_INT_MAX + +/* Codepoint type. Must be 32 bits or more because it is used also for + * internal codepoints. The type is signed because negative codepoints + * are used as internal markers (e.g. to mark EOF or missing argument). + * (X)UTF-8/CESU-8 encode/decode take and return an unsigned variant to + * ensure duk_uint32_t casts back and forth nicely. Almost everything + * else uses the signed one. + */ +typedef duk_int_t duk_codepoint_t; +typedef duk_uint_t duk_ucodepoint_t; +#define DUK_CODEPOINT_MIN DUK_INT_MIN +#define DUK_CODEPOINT_MAX DUK_INT_MAX +#define DUK_UCODEPOINT_MIN DUK_UINT_MIN +#define DUK_UCODEPOINT_MAX DUK_UINT_MAX + +/* IEEE float/double typedef. */ +typedef float duk_float_t; +typedef double duk_double_t; + +/* We're generally assuming that we're working on a platform with a 32-bit + * address space. If DUK_SIZE_MAX is a typecast value (which is necessary + * if SIZE_MAX is missing), the check must be avoided because the + * preprocessor can't do a comparison. + */ +#if !defined(DUK_SIZE_MAX) +#error DUK_SIZE_MAX is undefined, probably missing SIZE_MAX +#elif !defined(DUK_SIZE_MAX_COMPUTED) +#if DUK_SIZE_MAX < 0xffffffffUL +/* On some systems SIZE_MAX can be smaller than max unsigned 32-bit value + * which seems incorrect if size_t is (at least) an unsigned 32-bit type. + * However, it doesn't seem useful to error out compilation if this is the + * case. + */ +#endif +#endif + +/* Type for public API calls. */ +typedef struct duk_hthread duk_context; + +/* Check whether we should use 64-bit integers or not. + * + * Quite incomplete now. Use 64-bit types if detected (C99 or other detection) + * unless they are known to be unreliable. For instance, 64-bit types are + * available on VBCC but seem to misbehave. + */ +#if defined(DUK_F_HAVE_64BIT) && !defined(DUK_F_VBCC) +#define DUK_USE_64BIT_OPS +#else +#undef DUK_USE_64BIT_OPS +#endif + +/* + * Fill-ins for platform, architecture, and compiler + */ + +#if !defined(DUK_SETJMP) +#define DUK_JMPBUF_TYPE jmp_buf +#define DUK_SETJMP(jb) setjmp((jb)) +#define DUK_LONGJMP(jb) longjmp((jb), 1) +#endif + +#if 0 +/* sigsetjmp() alternative */ +#define DUK_JMPBUF_TYPE sigjmp_buf +#define DUK_SETJMP(jb) sigsetjmp((jb)) +#define DUK_LONGJMP(jb) siglongjmp((jb), 1) +#endif + +typedef FILE duk_file; +#if !defined(DUK_STDIN) +#define DUK_STDIN stdin +#endif +#if !defined(DUK_STDOUT) +#define DUK_STDOUT stdout +#endif +#if !defined(DUK_STDERR) +#define DUK_STDERR stderr +#endif + +/* Special naming to avoid conflict with e.g. DUK_FREE() in duk_heap.h + * (which is unfortunately named). May sometimes need replacement, e.g. + * some compilers don't handle zero length or NULL correctly in realloc(). + */ +#if !defined(DUK_ANSI_MALLOC) +#define DUK_ANSI_MALLOC malloc +#endif +#if !defined(DUK_ANSI_REALLOC) +#define DUK_ANSI_REALLOC realloc +#endif +#if !defined(DUK_ANSI_CALLOC) +#define DUK_ANSI_CALLOC calloc +#endif +#if !defined(DUK_ANSI_FREE) +#define DUK_ANSI_FREE free +#endif + +/* ANSI C (various versions) and some implementations require that the + * pointer arguments to memset(), memcpy(), and memmove() be valid values + * even when byte size is 0 (even a NULL pointer is considered invalid in + * this context). Zero-size operations as such are allowed, as long as their + * pointer arguments point to a valid memory area. The DUK_MEMSET(), + * DUK_MEMCPY(), and DUK_MEMMOVE() macros require this same behavior, i.e.: + * (1) pointers must be valid and non-NULL, (2) zero size must otherwise be + * allowed. If these are not fulfilled, a macro wrapper is needed. + * + * http://stackoverflow.com/questions/5243012/is-it-guaranteed-to-be-safe-to-perform-memcpy0-0-0 + * http://lists.cs.uiuc.edu/pipermail/llvmdev/2007-October/011065.html + * + * Not sure what's the required behavior when a pointer points just past the + * end of a buffer, which often happens in practice (e.g. zero size memmoves). + * For example, if allocation size is 3, the following pointer would not + * technically point to a valid memory byte: + * + * <-- alloc --> + * | 0 | 1 | 2 | ..... + * ^-- p=3, points after last valid byte (2) + */ +#if !defined(DUK_MEMCPY) +#if defined(DUK_F_UCLIBC) +/* Old uclibcs have a broken memcpy so use memmove instead (this is overly wide + * now on purpose): http://lists.uclibc.org/pipermail/uclibc-cvs/2008-October/025511.html + */ +#define DUK_MEMCPY memmove +#else +#define DUK_MEMCPY memcpy +#endif +#endif +#if !defined(DUK_MEMMOVE) +#define DUK_MEMMOVE memmove +#endif +#if !defined(DUK_MEMCMP) +#define DUK_MEMCMP memcmp +#endif +#if !defined(DUK_MEMSET) +#define DUK_MEMSET memset +#endif +#if !defined(DUK_STRLEN) +#define DUK_STRLEN strlen +#endif +#if !defined(DUK_STRCMP) +#define DUK_STRCMP strcmp +#endif +#if !defined(DUK_STRNCMP) +#define DUK_STRNCMP strncmp +#endif +#if !defined(DUK_PRINTF) +#define DUK_PRINTF printf +#endif +#if !defined(DUK_FPRINTF) +#define DUK_FPRINTF fprintf +#endif +#if !defined(DUK_SPRINTF) +#define DUK_SPRINTF sprintf +#endif +#if !defined(DUK_SNPRINTF) +/* snprintf() is technically not part of C89 but usually available. */ +#define DUK_SNPRINTF snprintf +#endif +#if !defined(DUK_VSPRINTF) +#define DUK_VSPRINTF vsprintf +#endif +#if !defined(DUK_VSNPRINTF) +/* vsnprintf() is technically not part of C89 but usually available. */ +#define DUK_VSNPRINTF vsnprintf +#endif +#if !defined(DUK_SSCANF) +#define DUK_SSCANF sscanf +#endif +#if !defined(DUK_VSSCANF) +#define DUK_VSSCANF vsscanf +#endif +#if !defined(DUK_FOPEN) +#define DUK_FOPEN fopen +#endif +#if !defined(DUK_FCLOSE) +#define DUK_FCLOSE fclose +#endif +#if !defined(DUK_FREAD) +#define DUK_FREAD fread +#endif +#if !defined(DUK_FWRITE) +#define DUK_FWRITE fwrite +#endif +#if !defined(DUK_FSEEK) +#define DUK_FSEEK fseek +#endif +#if !defined(DUK_FTELL) +#define DUK_FTELL ftell +#endif +#if !defined(DUK_FFLUSH) +#define DUK_FFLUSH fflush +#endif +#if !defined(DUK_FPUTC) +#define DUK_FPUTC fputc +#endif +#if !defined(DUK_MEMZERO) +#define DUK_MEMZERO(p,n) DUK_MEMSET((p), 0, (n)) +#endif +#if !defined(DUK_ABORT) +#define DUK_ABORT abort +#endif +#if !defined(DUK_EXIT) +#define DUK_EXIT exit +#endif + +#if !defined(DUK_DOUBLE_2TO32) +#define DUK_DOUBLE_2TO32 4294967296.0 +#endif +#if !defined(DUK_DOUBLE_2TO31) +#define DUK_DOUBLE_2TO31 2147483648.0 +#endif + +#if !defined(DUK_DOUBLE_INFINITY) +#undef DUK_USE_COMPUTED_INFINITY +#if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION < 40600) +/* GCC older than 4.6: avoid overflow warnings related to using INFINITY */ +#define DUK_DOUBLE_INFINITY (__builtin_inf()) +#elif defined(INFINITY) +#define DUK_DOUBLE_INFINITY ((double) INFINITY) +#elif !defined(DUK_F_VBCC) && !defined(DUK_F_MSVC) && !defined(DUK_F_BCC) +#define DUK_DOUBLE_INFINITY (1.0 / 0.0) +#else +/* In VBCC (1.0 / 0.0) results in a warning and 0.0 instead of infinity. + * Use a computed infinity (initialized when a heap is created at the + * latest). + */ +#define DUK_USE_COMPUTED_INFINITY +#define DUK_DOUBLE_INFINITY duk_computed_infinity +#endif +#endif + +#if !defined(DUK_DOUBLE_NAN) +#undef DUK_USE_COMPUTED_NAN +#if defined(NAN) +#define DUK_DOUBLE_NAN NAN +#elif !defined(DUK_F_VBCC) && !defined(DUK_F_MSVC) && !defined(DUK_F_BCC) +#define DUK_DOUBLE_NAN (0.0 / 0.0) +#else +/* In VBCC (0.0 / 0.0) results in a warning and 0.0 instead of NaN. + * In MSVC (VS2010 Express) (0.0 / 0.0) results in a compile error. + * Use a computed NaN (initialized when a heap is created at the + * latest). + */ +#define DUK_USE_COMPUTED_NAN +#define DUK_DOUBLE_NAN duk_computed_nan +#endif +#endif + +/* Many platforms are missing fpclassify() and friends, so use replacements + * if necessary. The replacement constants (FP_NAN etc) can be anything but + * match Linux constants now. + */ +#undef DUK_USE_REPL_FPCLASSIFY +#undef DUK_USE_REPL_SIGNBIT +#undef DUK_USE_REPL_ISFINITE +#undef DUK_USE_REPL_ISNAN +#undef DUK_USE_REPL_ISINF + +/* Complex condition broken into separate parts. */ +#undef DUK_F_USE_REPL_ALL +#if !(defined(FP_NAN) && defined(FP_INFINITE) && defined(FP_ZERO) && \ + defined(FP_SUBNORMAL) && defined(FP_NORMAL)) +/* Missing some obvious constants. */ +#define DUK_F_USE_REPL_ALL +#elif defined(DUK_F_AMIGAOS) && defined(DUK_F_VBCC) +/* VBCC is missing the built-ins even in C99 mode (perhaps a header issue). */ +#define DUK_F_USE_REPL_ALL +#elif defined(DUK_F_AMIGAOS) && defined(DUK_F_M68K) +/* AmigaOS + M68K seems to have math issues even when using GCC cross + * compilation. Use replacements for all AmigaOS versions on M68K + * regardless of compiler. + */ +#define DUK_F_USE_REPL_ALL +#elif defined(DUK_F_FREEBSD) && defined(DUK_F_CLANG) +/* Placeholder fix for (detection is wider than necessary): + * http://llvm.org/bugs/show_bug.cgi?id=17788 + */ +#define DUK_F_USE_REPL_ALL +#elif defined(DUK_F_UCLIBC) +/* At least some uclibc versions have broken floating point math. For + * example, fpclassify() can incorrectly classify certain NaN formats. + * To be safe, use replacements. + */ +#define DUK_F_USE_REPL_ALL +#endif + +#if defined(DUK_F_USE_REPL_ALL) +#define DUK_USE_REPL_FPCLASSIFY +#define DUK_USE_REPL_SIGNBIT +#define DUK_USE_REPL_ISFINITE +#define DUK_USE_REPL_ISNAN +#define DUK_USE_REPL_ISINF +#define DUK_FPCLASSIFY duk_repl_fpclassify +#define DUK_SIGNBIT duk_repl_signbit +#define DUK_ISFINITE duk_repl_isfinite +#define DUK_ISNAN duk_repl_isnan +#define DUK_ISINF duk_repl_isinf +#define DUK_FP_NAN 0 +#define DUK_FP_INFINITE 1 +#define DUK_FP_ZERO 2 +#define DUK_FP_SUBNORMAL 3 +#define DUK_FP_NORMAL 4 +#else +#define DUK_FPCLASSIFY fpclassify +#define DUK_SIGNBIT signbit +#define DUK_ISFINITE isfinite +#define DUK_ISNAN isnan +#define DUK_ISINF isinf +#define DUK_FP_NAN FP_NAN +#define DUK_FP_INFINITE FP_INFINITE +#define DUK_FP_ZERO FP_ZERO +#define DUK_FP_SUBNORMAL FP_SUBNORMAL +#define DUK_FP_NORMAL FP_NORMAL +#endif + +#if defined(DUK_F_USE_REPL_ALL) +#undef DUK_F_USE_REPL_ALL +#endif + +/* Some math functions are C99 only. This is also an issue with some + * embedded environments using uclibc where uclibc has been configured + * not to provide some functions. For now, use replacements whenever + * using uclibc. + */ +#undef DUK_USE_MATH_FMIN +#undef DUK_USE_MATH_FMAX +#undef DUK_USE_MATH_ROUND +#if defined(DUK_F_UCLIBC) +/* uclibc may be missing these */ +#elif defined(DUK_F_AMIGAOS) && defined(DUK_F_VBCC) +/* vbcc + AmigaOS may be missing these */ +#elif defined(DUK_F_MINT) +/* mint clib is missing these */ +#elif !defined(DUK_F_C99) && !defined(DUK_F_CPP11) +/* build is not C99 or C++11, play it safe */ +#else +/* C99 or C++11, no known issues */ +#define DUK_USE_MATH_FMIN +#define DUK_USE_MATH_FMAX +#define DUK_USE_MATH_ROUND +#endif + +/* These functions don't currently need replacement but are wrapped for + * completeness. Because these are used as function pointers, they need + * to be defined as concrete C functions (not macros). + */ +#if !defined(DUK_FABS) +#define DUK_FABS fabs +#endif +#if !defined(DUK_FMIN) +#define DUK_FMIN fmin +#endif +#if !defined(DUK_FMAX) +#define DUK_FMAX fmax +#endif +#if !defined(DUK_FLOOR) +#define DUK_FLOOR floor +#endif +#if !defined(DUK_CEIL) +#define DUK_CEIL ceil +#endif +#if !defined(DUK_FMOD) +#define DUK_FMOD fmod +#endif +#if !defined(DUK_POW) +#define DUK_POW pow +#endif +#if !defined(DUK_ACOS) +#define DUK_ACOS acos +#endif +#if !defined(DUK_ASIN) +#define DUK_ASIN asin +#endif +#if !defined(DUK_ATAN) +#define DUK_ATAN atan +#endif +#if !defined(DUK_ATAN2) +#define DUK_ATAN2 atan2 +#endif +#if !defined(DUK_SIN) +#define DUK_SIN sin +#endif +#if !defined(DUK_COS) +#define DUK_COS cos +#endif +#if !defined(DUK_TAN) +#define DUK_TAN tan +#endif +#if !defined(DUK_EXP) +#define DUK_EXP exp +#endif +#if !defined(DUK_LOG) +#define DUK_LOG log +#endif +#if !defined(DUK_SQRT) +#define DUK_SQRT sqrt +#endif + +/* NetBSD 6.0 x86 (at least) has a few problems with pow() semantics, + * see test-bug-netbsd-math-pow.js. Use NetBSD specific workaround. + * (This might be a wider problem; if so, generalize the define name.) + */ +#undef DUK_USE_POW_NETBSD_WORKAROUND +#if defined(DUK_F_NETBSD) +#define DUK_USE_POW_NETBSD_WORKAROUND +#endif + +/* Rely as little as possible on compiler behavior for NaN comparison, + * signed zero handling, etc. Currently never activated but may be needed + * for broken compilers. + */ +#undef DUK_USE_PARANOID_MATH + +/* There was a curious bug where test-bi-date-canceling.js would fail e.g. + * on 64-bit Ubuntu, gcc-4.8.1, -m32, and no -std=c99. Some date computations + * using doubles would be optimized which then broke some corner case tests. + * The problem goes away by adding 'volatile' to the datetime computations. + * Not sure what the actual triggering conditions are, but using this on + * non-C99 systems solves the known issues and has relatively little cost + * on other platforms. + */ +#undef DUK_USE_PARANOID_DATE_COMPUTATION +#if !defined(DUK_F_C99) +#define DUK_USE_PARANOID_DATE_COMPUTATION +#endif + +/* + * Byte order and double memory layout detection + * + * Endianness detection is a major portability hassle because the macros + * and headers are not standardized. There's even variance across UNIX + * platforms. Even with "standard" headers, details like underscore count + * varies between platforms, e.g. both __BYTE_ORDER and _BYTE_ORDER are used + * (Crossbridge has a single underscore, for instance). + * + * The checks below are structured with this in mind: several approaches are + * used, and at the end we check if any of them worked. This allows generic + * approaches to be tried first, and platform/compiler specific hacks tried + * last. As a last resort, the user can force a specific endianness, as it's + * not likely that automatic detection will work on the most exotic platforms. + * + * Duktape supports little and big endian machines. There's also support + * for a hybrid used by some ARM machines where integers are little endian + * but IEEE double values use a mixed order (12345678 -> 43218765). This + * byte order for doubles is referred to as "mixed endian". + */ + +/* For custom platforms allow user to define byteorder explicitly. + * Since endianness headers are not standardized, this is a useful + * workaround for custom platforms for which endianness detection + * is not directly supported. Perhaps custom hardware is used and + * user cannot submit upstream patches. + */ +#if defined(DUK_OPT_FORCE_BYTEORDER) +#undef DUK_USE_BYTEORDER +#if (DUK_OPT_FORCE_BYTEORDER == 1) +#define DUK_USE_BYTEORDER 1 +#elif (DUK_OPT_FORCE_BYTEORDER == 2) +#define DUK_USE_BYTEORDER 2 +#elif (DUK_OPT_FORCE_BYTEORDER == 3) +#define DUK_USE_BYTEORDER 3 +#else +#error invalid DUK_OPT_FORCE_BYTEORDER value +#endif +#endif /* DUK_OPT_FORCE_BYTEORDER */ + +/* GCC and Clang provide endianness defines as built-in predefines, with + * leading and trailing double underscores (e.g. __BYTE_ORDER__). See + * output of "make gccpredefs" and "make clangpredefs". Clang doesn't + * seem to provide __FLOAT_WORD_ORDER__; assume not mixed endian for clang. + * http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html + */ +#if !defined(DUK_USE_BYTEORDER) && defined(__BYTE_ORDER__) +#if defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) +#if defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__) +#define DUK_USE_BYTEORDER 1 +#elif defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__) +#define DUK_USE_BYTEORDER 2 +#elif !defined(__FLOAT_WORD_ORDER__) +/* Float word order not known, assume not a hybrid. */ +#define DUK_USE_BYTEORDER 1 +#else +/* Byte order is little endian but cannot determine IEEE double word order. */ +#endif /* float word order */ +#elif defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#if defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__) +#define DUK_USE_BYTEORDER 3 +#elif !defined(__FLOAT_WORD_ORDER__) +/* Float word order not known, assume not a hybrid. */ +#define DUK_USE_BYTEORDER 3 +#else +/* Byte order is big endian but cannot determine IEEE double word order. */ +#endif /* float word order */ +#else +/* Cannot determine byte order; __ORDER_PDP_ENDIAN__ is related to 32-bit + * integer ordering and is not relevant. + */ +#endif /* integer byte order */ +#endif /* !defined(DUK_USE_BYTEORDER) && defined(__BYTE_ORDER__) */ + +/* More or less standard endianness predefines provided by header files. + * The ARM hybrid case is detected by assuming that __FLOAT_WORD_ORDER + * will be big endian, see: http://lists.mysql.com/internals/443. + * On some platforms some defines may be present with an empty value which + * causes comparisons to fail: https://github.com/svaarala/duktape/issues/453. + */ +#if !defined(DUK_USE_BYTEORDER) +#if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN) || \ + defined(_BYTE_ORDER) && defined(_LITTLE_ENDIAN) && (_BYTE_ORDER == _LITTLE_ENDIAN) || \ + defined(__LITTLE_ENDIAN__) +#if defined(__FLOAT_WORD_ORDER) && defined(__LITTLE_ENDIAN) && (__FLOAT_WORD_ORDER == __LITTLE_ENDIAN) || \ + defined(_FLOAT_WORD_ORDER) && defined(_LITTLE_ENDIAN) && (_FLOAT_WORD_ORDER == _LITTLE_ENDIAN) +#define DUK_USE_BYTEORDER 1 +#elif defined(__FLOAT_WORD_ORDER) && defined(__BIG_ENDIAN) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN) || \ + defined(_FLOAT_WORD_ORDER) && defined(_BIG_ENDIAN) && (_FLOAT_WORD_ORDER == _BIG_ENDIAN) +#define DUK_USE_BYTEORDER 2 +#elif !defined(__FLOAT_WORD_ORDER) && !defined(_FLOAT_WORD_ORDER) +/* Float word order not known, assume not a hybrid. */ +#define DUK_USE_BYTEORDER 1 +#else +/* Byte order is little endian but cannot determine IEEE double word order. */ +#endif /* float word order */ +#elif defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN) || \ + defined(_BYTE_ORDER) && defined(_BIG_ENDIAN) && (_BYTE_ORDER == _BIG_ENDIAN) || \ + defined(__BIG_ENDIAN__) +#if defined(__FLOAT_WORD_ORDER) && defined(__BIG_ENDIAN) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN) || \ + defined(_FLOAT_WORD_ORDER) && defined(_BIG_ENDIAN) && (_FLOAT_WORD_ORDER == _BIG_ENDIAN) +#define DUK_USE_BYTEORDER 3 +#elif !defined(__FLOAT_WORD_ORDER) && !defined(_FLOAT_WORD_ORDER) +/* Float word order not known, assume not a hybrid. */ +#define DUK_USE_BYTEORDER 3 +#else +/* Byte order is big endian but cannot determine IEEE double word order. */ +#endif /* float word order */ +#else +/* Cannot determine byte order. */ +#endif /* integer byte order */ +#endif /* !defined(DUK_USE_BYTEORDER) */ + +/* QNX gcc cross compiler seems to define e.g. __LITTLEENDIAN__ or __BIGENDIAN__: + * $ /opt/qnx650/host/linux/x86/usr/bin/i486-pc-nto-qnx6.5.0-gcc -dM -E - > 24) | \ + ((((duk_uint32_t) (x)) >> 8) & 0xff00UL) | \ + ((((duk_uint32_t) (x)) << 8) & 0xff0000UL) | \ + (((duk_uint32_t) (x)) << 24)) +#endif +#if !defined(DUK_BSWAP16) +#define DUK_BSWAP16(x) \ + ((duk_uint16_t) (x) >> 8) | \ + ((duk_uint16_t) (x) << 8) +#endif + +/* DUK_USE_VARIADIC_MACROS: required from compilers, so no fill-in. */ +/* DUK_USE_UNION_INITIALIZERS: required from compilers, so no fill-in. */ + +#if !(defined(DUK_USE_FLEX_C99) || defined(DUK_USE_FLEX_ZEROSIZE) || defined(DUK_USE_FLEX_ONESIZE)) +#if defined(DUK_F_C99) +#define DUK_USE_FLEX_C99 +#else +#define DUK_USE_FLEX_ZEROSIZE /* Not standard but common enough */ +#endif +#endif + +#if !(defined(DUK_USE_PACK_GCC_ATTR) || defined(DUK_USE_PACK_CLANG_ATTR) || \ + defined(DUK_USE_PACK_MSVC_PRAGMA) || defined(DUK_USE_PACK_DUMMY_MEMBER)) +#define DUK_USE_PACK_DUMMY_MEMBER +#endif + +#if 0 /* not defined by default */ +#undef DUK_USE_GCC_PRAGMAS +#endif + +/* Workaround for GH-323: avoid inlining control when compiling from + * multiple sources, as it causes compiler portability trouble. + */ +#if !defined(DUK_SINGLE_FILE) +#undef DUK_NOINLINE +#undef DUK_INLINE +#undef DUK_ALWAYS_INLINE +#define DUK_NOINLINE /*nop*/ +#define DUK_INLINE /*nop*/ +#define DUK_ALWAYS_INLINE /*nop*/ +#endif + +/* + * Check whether or not a packed duk_tval representation is possible. + * What's basically required is that pointers are 32-bit values + * (sizeof(void *) == 4). Best effort check, not always accurate. + * If guess goes wrong, crashes may result; self tests also verify + * the guess. + */ + +/* Explicit marker needed; may be 'defined', 'undefined, 'or 'not provided'. */ +#if !defined(DUK_F_PACKED_TVAL_PROVIDED) +#undef DUK_F_PACKED_TVAL_POSSIBLE + +/* Strict C99 case: DUK_UINTPTR_MAX (= UINTPTR_MAX) should be very reliable */ +#if !defined(DUK_F_PACKED_TVAL_POSSIBLE) && defined(DUK_UINTPTR_MAX) +#if (DUK_UINTPTR_MAX <= 0xffffffffUL) +#define DUK_F_PACKED_TVAL_POSSIBLE +#endif +#endif + +/* Non-C99 case, still relying on DUK_UINTPTR_MAX, as long as it is not a computed value */ +#if !defined(DUK_F_PACKED_TVAL_POSSIBLE) && defined(DUK_UINTPTR_MAX) && !defined(DUK_UINTPTR_MAX_COMPUTED) +#if (DUK_UINTPTR_MAX <= 0xffffffffUL) +#define DUK_F_PACKED_TVAL_POSSIBLE +#endif +#endif + +/* DUK_SIZE_MAX (= SIZE_MAX) is often reliable */ +#if !defined(DUK_F_PACKED_TVAL_POSSIBLE) && defined(DUK_SIZE_MAX) && !defined(DUK_SIZE_MAX_COMPUTED) +#if (DUK_SIZE_MAX <= 0xffffffffUL) +#define DUK_F_PACKED_TVAL_POSSIBLE +#endif +#endif + +#undef DUK_USE_PACKED_TVAL +#if defined(DUK_F_PACKED_TVAL_POSSIBLE) +#define DUK_USE_PACKED_TVAL +#endif + +#undef DUK_F_PACKED_TVAL_POSSIBLE +#endif /* DUK_F_PACKED_TVAL_PROVIDED */ + +/* Feature option forcing. */ +#if defined(DUK_OPT_NO_PACKED_TVAL) +#undef DUK_USE_PACKED_TVAL +#elif defined(DUK_OPT_PACKED_TVAL) +#undef DUK_USE_PACKED_TVAL +#define DUK_USE_PACKED_TVAL +#endif +/* Object property allocation layout has implications for memory and code + * footprint and generated code size/speed. The best layout also depends + * on whether the platform has alignment requirements or benefits from + * having mostly aligned accesses. + */ +#undef DUK_USE_HOBJECT_LAYOUT_1 +#undef DUK_USE_HOBJECT_LAYOUT_2 +#undef DUK_USE_HOBJECT_LAYOUT_3 +#if (DUK_USE_ALIGN_BY == 1) +/* On platforms without any alignment issues, layout 1 is preferable + * because it compiles to slightly less code and provides direct access + * to property keys. + */ +#define DUK_USE_HOBJECT_LAYOUT_1 +#else +/* On other platforms use layout 2, which requires some padding but + * is a bit more natural than layout 3 in ordering the entries. Layout + * 3 is currently not used. + */ +#define DUK_USE_HOBJECT_LAYOUT_2 +#endif + +/* GCC/clang inaccurate math would break compliance and probably duk_tval, + * so refuse to compile. Relax this if -ffast-math is tested to work. + */ +#if defined(__FAST_MATH__) +#error __FAST_MATH__ defined, refusing to compile +#endif + +/* + * Autogenerated defaults + */ + +#undef DUK_USE_ASSERTIONS +#define DUK_USE_AUGMENT_ERROR_CREATE +#define DUK_USE_AUGMENT_ERROR_THROW +#define DUK_USE_AVOID_PLATFORM_FUNCPTRS +#define DUK_USE_BASE64_FASTPATH +#define DUK_USE_BROWSER_LIKE +#define DUK_USE_BUFFEROBJECT_SUPPORT +#undef DUK_USE_BUFLEN16 +#define DUK_USE_BUILTIN_INITJS +#define DUK_USE_BYTECODE_DUMP_SUPPORT +#define DUK_USE_COMMONJS_MODULES +#define DUK_USE_COMPILER_RECLIMIT 2500 +#undef DUK_USE_CPP_EXCEPTIONS +#undef DUK_USE_DATAPTR16 +#undef DUK_USE_DATAPTR_DEC16 +#undef DUK_USE_DATAPTR_ENC16 +#undef DUK_USE_DATE_FORMAT_STRING +#undef DUK_USE_DATE_GET_LOCAL_TZOFFSET +#undef DUK_USE_DATE_GET_NOW +#undef DUK_USE_DATE_PARSE_STRING +#undef DUK_USE_DATE_PRS_GETDATE +#undef DUK_USE_DDDPRINT +#undef DUK_USE_DDPRINT +#undef DUK_USE_DEBUG +#undef DUK_USE_DEBUGGER_DUMPHEAP +#undef DUK_USE_DEBUGGER_FWD_LOGGING +#undef DUK_USE_DEBUGGER_FWD_PRINTALERT +#undef DUK_USE_DEBUGGER_INSPECT +#undef DUK_USE_DEBUGGER_PAUSE_UNCAUGHT +#undef DUK_USE_DEBUGGER_SUPPORT +#define DUK_USE_DEBUGGER_THROW_NOTIFY +#undef DUK_USE_DEBUGGER_TRANSPORT_TORTURE +#define DUK_USE_DEBUG_BUFSIZE 65536L +#define DUK_USE_DOUBLE_LINKED_HEAP +#undef DUK_USE_DPRINT +#undef DUK_USE_DPRINT_COLORS +#undef DUK_USE_DPRINT_RDTSC +#define DUK_USE_ERRCREATE +#define DUK_USE_ERRTHROW +#define DUK_USE_ES6_OBJECT_PROTO_PROPERTY +#define DUK_USE_ES6_OBJECT_SETPROTOTYPEOF +#define DUK_USE_ES6_PROXY +#define DUK_USE_ES6_REGEXP_BRACES +#define DUK_USE_ESBC_LIMITS +#define DUK_USE_ESBC_MAX_BYTES 2147418112L +#define DUK_USE_ESBC_MAX_LINENUMBER 2147418112L +#undef DUK_USE_EXEC_FUN_LOCAL +#undef DUK_USE_EXEC_INDIRECT_BOUND_CHECK +#undef DUK_USE_EXEC_TIMEOUT_CHECK +#undef DUK_USE_EXPLICIT_NULL_INIT +#undef DUK_USE_EXTSTR_FREE +#undef DUK_USE_EXTSTR_INTERN_CHECK +#undef DUK_USE_FASTINT +#define DUK_USE_FAST_REFCOUNT_DEFAULT +#define DUK_USE_FILE_IO +#undef DUK_USE_FUNCPTR16 +#undef DUK_USE_FUNCPTR_DEC16 +#undef DUK_USE_FUNCPTR_ENC16 +#undef DUK_USE_GC_TORTURE +#undef DUK_USE_HEAPPTR16 +#undef DUK_USE_HEAPPTR_DEC16 +#undef DUK_USE_HEAPPTR_ENC16 +#define DUK_USE_HEX_FASTPATH +#define DUK_USE_HOBJECT_HASH_PART +#define DUK_USE_HSTRING_CLEN +#undef DUK_USE_HSTRING_EXTDATA +#define DUK_USE_IDCHAR_FASTPATH +#undef DUK_USE_INTERRUPT_COUNTER +#undef DUK_USE_INTERRUPT_DEBUG_FIXUP +#define DUK_USE_JC +#define DUK_USE_JSON_DECNUMBER_FASTPATH +#define DUK_USE_JSON_DECSTRING_FASTPATH +#define DUK_USE_JSON_DEC_RECLIMIT 1000 +#define DUK_USE_JSON_EATWHITE_FASTPATH +#define DUK_USE_JSON_ENC_RECLIMIT 1000 +#define DUK_USE_JSON_QUOTESTRING_FASTPATH +#undef DUK_USE_JSON_STRINGIFY_FASTPATH +#define DUK_USE_JX +#define DUK_USE_LEXER_SLIDING_WINDOW +#undef DUK_USE_LIGHTFUNC_BUILTINS +#undef DUK_USE_MARKANDSWEEP_FINALIZER_TORTURE +#define DUK_USE_MARK_AND_SWEEP +#define DUK_USE_MARK_AND_SWEEP_RECLIMIT 256 +#define DUK_USE_MATH_BUILTIN +#define DUK_USE_MS_STRINGTABLE_RESIZE +#define DUK_USE_NATIVE_CALL_RECLIMIT 1000 +#define DUK_USE_NONSTD_ARRAY_CONCAT_TRAILER +#define DUK_USE_NONSTD_ARRAY_MAP_TRAILER +#define DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT +#undef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY +#undef DUK_USE_NONSTD_FUNC_SOURCE_PROPERTY +#define DUK_USE_NONSTD_FUNC_STMT +#define DUK_USE_NONSTD_GETTER_KEY_ARGUMENT +#define DUK_USE_NONSTD_JSON_ESC_U2028_U2029 +#define DUK_USE_NONSTD_REGEXP_DOLLAR_ESCAPE +#define DUK_USE_NONSTD_SETTER_KEY_ARGUMENT +#define DUK_USE_NONSTD_STRING_FROMCHARCODE_32BIT +#undef DUK_USE_OBJSIZES16 +#define DUK_USE_OCTAL_SUPPORT +#define DUK_USE_PANIC_ABORT +#undef DUK_USE_PANIC_EXIT +#undef DUK_USE_PANIC_HANDLER +#undef DUK_USE_PANIC_SEGFAULT +#undef DUK_USE_PARANOID_ERRORS +#define DUK_USE_PC2LINE +#undef DUK_USE_PREFER_SIZE +#define DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS +#undef DUK_USE_REFCOUNT16 +#define DUK_USE_REFERENCE_COUNTING +#undef DUK_USE_REFZERO_FINALIZER_TORTURE +#undef DUK_USE_REGEXP_CANON_WORKAROUND +#define DUK_USE_REGEXP_COMPILER_RECLIMIT 10000 +#define DUK_USE_REGEXP_EXECUTOR_RECLIMIT 10000 +#define DUK_USE_REGEXP_SUPPORT +#undef DUK_USE_ROM_GLOBAL_CLONE +#undef DUK_USE_ROM_GLOBAL_INHERIT +#undef DUK_USE_ROM_OBJECTS +#define DUK_USE_ROM_PTRCOMP_FIRST 63488L +#undef DUK_USE_ROM_STRINGS +#define DUK_USE_SECTION_B +#undef DUK_USE_SELF_TESTS +#undef DUK_USE_SHUFFLE_TORTURE +#define DUK_USE_SOURCE_NONBMP +#undef DUK_USE_STRHASH16 +#undef DUK_USE_STRHASH_DENSE +#define DUK_USE_STRHASH_SKIP_SHIFT 5 +#define DUK_USE_STRICT_DECL +#undef DUK_USE_STRICT_UTF8_SOURCE +#undef DUK_USE_STRLEN16 +#undef DUK_USE_STRTAB_CHAIN +#undef DUK_USE_STRTAB_CHAIN_SIZE +#define DUK_USE_STRTAB_PROBE +#define DUK_USE_TAILCALL +#define DUK_USE_TARGET_INFO "unknown" +#define DUK_USE_TRACEBACKS +#define DUK_USE_TRACEBACK_DEPTH 10 +#define DUK_USE_USER_DECLARE() /* no user declarations */ +#undef DUK_USE_USER_INITJS +#undef DUK_USE_VALSTACK_UNSAFE +#define DUK_USE_VERBOSE_ERRORS +#define DUK_USE_VERBOSE_EXECUTOR_ERRORS +#define DUK_USE_VOLUNTARY_GC +#define DUK_USE_ZERO_BUFFER_DATA + +/* + * Alternative customization header + * + * If you want to modify the final DUK_USE_xxx flags directly (without + * using the available DUK_OPT_xxx flags), define DUK_OPT_HAVE_CUSTOM_H + * and tweak the final flags there. + */ + +#if defined(DUK_OPT_HAVE_CUSTOM_H) +#include "duk_custom.h" +#endif + +/* + * You may add overriding #define/#undef directives below for + * customization. You of course cannot un-#include or un-typedef + * anything; these require direct changes above. + */ + +/* __OVERRIDE_DEFINES__ */ + +/* + * Date provider selection + * + * User may define DUK_USE_DATE_GET_NOW() etc directly, in which case we'll + * rely on an external provider. If this is not done, revert to previous + * behavior and use Unix/Windows built-in provider. + */ + +#if defined(DUK_COMPILING_DUKTAPE) + +#if defined(DUK_USE_DATE_GET_NOW) +/* External provider already defined. */ +#elif defined(DUK_USE_DATE_NOW_GETTIMEOFDAY) +#define DUK_USE_DATE_GET_NOW(ctx) duk_bi_date_get_now_gettimeofday((ctx)) +#elif defined(DUK_USE_DATE_NOW_TIME) +#define DUK_USE_DATE_GET_NOW(ctx) duk_bi_date_get_now_time((ctx)) +#elif defined(DUK_USE_DATE_NOW_WINDOWS) +#define DUK_USE_DATE_GET_NOW(ctx) duk_bi_date_get_now_windows((ctx)) +#else +#error no provider for DUK_USE_DATE_GET_NOW() +#endif + +#if defined(DUK_USE_DATE_GET_LOCAL_TZOFFSET) +/* External provider already defined. */ +#elif defined(DUK_USE_DATE_TZO_GMTIME_R) || defined(DUK_USE_DATE_TZO_GMTIME) +#define DUK_USE_DATE_GET_LOCAL_TZOFFSET(d) duk_bi_date_get_local_tzoffset_gmtime((d)) +#elif defined(DUK_USE_DATE_TZO_WINDOWS) +#define DUK_USE_DATE_GET_LOCAL_TZOFFSET(d) duk_bi_date_get_local_tzoffset_windows((d)) +#else +#error no provider for DUK_USE_DATE_GET_LOCAL_TZOFFSET() +#endif + +#if defined(DUK_USE_DATE_PARSE_STRING) +/* External provider already defined. */ +#elif defined(DUK_USE_DATE_PRS_STRPTIME) +#define DUK_USE_DATE_PARSE_STRING(ctx,str) duk_bi_date_parse_string_strptime((ctx), (str)) +#elif defined(DUK_USE_DATE_PRS_GETDATE) +#define DUK_USE_DATE_PARSE_STRING(ctx,str) duk_bi_date_parse_string_getdate((ctx), (str)) +#else +/* No provider for DUK_USE_DATE_PARSE_STRING(), fall back to ISO 8601 only. */ +#endif + +#if defined(DUK_USE_DATE_FORMAT_STRING) +/* External provider already defined. */ +#elif defined(DUK_USE_DATE_FMT_STRFTIME) +#define DUK_USE_DATE_FORMAT_STRING(ctx,parts,tzoffset,flags) \ + duk_bi_date_format_parts_strftime((ctx), (parts), (tzoffset), (flags)) +#else +/* No provider for DUK_USE_DATE_FORMAT_STRING(), fall back to ISO 8601 only. */ +#endif + +#endif /* DUK_COMPILING_DUKTAPE */ + +/* + * Checks for legacy feature options (DUK_OPT_xxx) + */ + +#if defined(DUK_OPT_ASSERTIONS) +#error unsupported legacy feature option DUK_OPT_ASSERTIONS used, consider options: DUK_USE_ASSERTIONS +#endif +#if defined(DUK_OPT_BUFFEROBJECT_SUPPORT) +#error unsupported legacy feature option DUK_OPT_BUFFEROBJECT_SUPPORT used +#endif +#if defined(DUK_OPT_BUFLEN16) +#error unsupported legacy feature option DUK_OPT_BUFLEN16 used +#endif +#if defined(DUK_OPT_DATAPTR16) +#error unsupported legacy feature option DUK_OPT_DATAPTR16 used +#endif +#if defined(DUK_OPT_DATAPTR_DEC16) +#error unsupported legacy feature option DUK_OPT_DATAPTR_DEC16 used +#endif +#if defined(DUK_OPT_DATAPTR_ENC16) +#error unsupported legacy feature option DUK_OPT_DATAPTR_ENC16 used +#endif +#if defined(DUK_OPT_DDDPRINT) +#error unsupported legacy feature option DUK_OPT_DDDPRINT used +#endif +#if defined(DUK_OPT_DDPRINT) +#error unsupported legacy feature option DUK_OPT_DDPRINT used +#endif +#if defined(DUK_OPT_DEBUG) +#error unsupported legacy feature option DUK_OPT_DEBUG used +#endif +#if defined(DUK_OPT_DEBUGGER_DUMPHEAP) +#error unsupported legacy feature option DUK_OPT_DEBUGGER_DUMPHEAP used +#endif +#if defined(DUK_OPT_DEBUGGER_FWD_LOGGING) +#error unsupported legacy feature option DUK_OPT_DEBUGGER_FWD_LOGGING used +#endif +#if defined(DUK_OPT_DEBUGGER_FWD_PRINTALERT) +#error unsupported legacy feature option DUK_OPT_DEBUGGER_FWD_PRINTALERT used +#endif +#if defined(DUK_OPT_DEBUGGER_SUPPORT) +#error unsupported legacy feature option DUK_OPT_DEBUGGER_SUPPORT used +#endif +#if defined(DUK_OPT_DEBUGGER_TRANSPORT_TORTURE) +#error unsupported legacy feature option DUK_OPT_DEBUGGER_TRANSPORT_TORTURE used +#endif +#if defined(DUK_OPT_DEBUG_BUFSIZE) +#error unsupported legacy feature option DUK_OPT_DEBUG_BUFSIZE used +#endif +#if defined(DUK_OPT_DECLARE) +#error unsupported legacy feature option DUK_OPT_DECLARE used +#endif +#if defined(DUK_OPT_DEEP_C_STACK) +#error unsupported legacy feature option DUK_OPT_DEEP_C_STACK used +#endif +#if defined(DUK_OPT_DLL_BUILD) +#error unsupported legacy feature option DUK_OPT_DLL_BUILD used +#endif +#if defined(DUK_OPT_DPRINT) +#error unsupported legacy feature option DUK_OPT_DPRINT used +#endif +#if defined(DUK_OPT_DPRINT_COLORS) +#error unsupported legacy feature option DUK_OPT_DPRINT_COLORS used +#endif +#if defined(DUK_OPT_DPRINT_RDTSC) +#error unsupported legacy feature option DUK_OPT_DPRINT_RDTSC used +#endif +#if defined(DUK_OPT_EXEC_TIMEOUT_CHECK) +#error unsupported legacy feature option DUK_OPT_EXEC_TIMEOUT_CHECK used +#endif +#if defined(DUK_OPT_EXTERNAL_STRINGS) +#error unsupported legacy feature option DUK_OPT_EXTERNAL_STRINGS used, consider options: DUK_USE_HSTRING_EXTDATA +#endif +#if defined(DUK_OPT_EXTSTR_FREE) +#error unsupported legacy feature option DUK_OPT_EXTSTR_FREE used +#endif +#if defined(DUK_OPT_EXTSTR_INTERN_CHECK) +#error unsupported legacy feature option DUK_OPT_EXTSTR_INTERN_CHECK used +#endif +#if defined(DUK_OPT_FASTINT) +#error unsupported legacy feature option DUK_OPT_FASTINT used, consider options: DUK_USE_FASTINT +#endif +#if defined(DUK_OPT_FORCE_ALIGN) +#error unsupported legacy feature option DUK_OPT_FORCE_ALIGN used +#endif +#if defined(DUK_OPT_FORCE_BYTEORDER) +#error unsupported legacy feature option DUK_OPT_FORCE_BYTEORDER used +#endif +#if defined(DUK_OPT_FUNCPTR16) +#error unsupported legacy feature option DUK_OPT_FUNCPTR16 used +#endif +#if defined(DUK_OPT_FUNCPTR_DEC16) +#error unsupported legacy feature option DUK_OPT_FUNCPTR_DEC16 used +#endif +#if defined(DUK_OPT_FUNCPTR_ENC16) +#error unsupported legacy feature option DUK_OPT_FUNCPTR_ENC16 used +#endif +#if defined(DUK_OPT_FUNC_NONSTD_CALLER_PROPERTY) +#error unsupported legacy feature option DUK_OPT_FUNC_NONSTD_CALLER_PROPERTY used +#endif +#if defined(DUK_OPT_FUNC_NONSTD_SOURCE_PROPERTY) +#error unsupported legacy feature option DUK_OPT_FUNC_NONSTD_SOURCE_PROPERTY used +#endif +#if defined(DUK_OPT_GC_TORTURE) +#error unsupported legacy feature option DUK_OPT_GC_TORTURE used +#endif +#if defined(DUK_OPT_HAVE_CUSTOM_H) +#error unsupported legacy feature option DUK_OPT_HAVE_CUSTOM_H used +#endif +#if defined(DUK_OPT_HEAPPTR16) +#error unsupported legacy feature option DUK_OPT_HEAPPTR16 used +#endif +#if defined(DUK_OPT_HEAPPTR_DEC16) +#error unsupported legacy feature option DUK_OPT_HEAPPTR_DEC16 used +#endif +#if defined(DUK_OPT_HEAPPTR_ENC16) +#error unsupported legacy feature option DUK_OPT_HEAPPTR_ENC16 used +#endif +#if defined(DUK_OPT_INTERRUPT_COUNTER) +#error unsupported legacy feature option DUK_OPT_INTERRUPT_COUNTER used +#endif +#if defined(DUK_OPT_JSON_STRINGIFY_FASTPATH) +#error unsupported legacy feature option DUK_OPT_JSON_STRINGIFY_FASTPATH used +#endif +#if defined(DUK_OPT_LIGHTFUNC_BUILTINS) +#error unsupported legacy feature option DUK_OPT_LIGHTFUNC_BUILTINS used +#endif +#if defined(DUK_OPT_NONSTD_FUNC_CALLER_PROPERTY) +#error unsupported legacy feature option DUK_OPT_NONSTD_FUNC_CALLER_PROPERTY used +#endif +#if defined(DUK_OPT_NONSTD_FUNC_SOURCE_PROPERTY) +#error unsupported legacy feature option DUK_OPT_NONSTD_FUNC_SOURCE_PROPERTY used +#endif +#if defined(DUK_OPT_NO_ARRAY_SPLICE_NONSTD_DELCOUNT) +#error unsupported legacy feature option DUK_OPT_NO_ARRAY_SPLICE_NONSTD_DELCOUNT used +#endif +#if defined(DUK_OPT_NO_AUGMENT_ERRORS) +#error unsupported legacy feature option DUK_OPT_NO_AUGMENT_ERRORS used +#endif +#if defined(DUK_OPT_NO_BROWSER_LIKE) +#error unsupported legacy feature option DUK_OPT_NO_BROWSER_LIKE used +#endif +#if defined(DUK_OPT_NO_BUFFEROBJECT_SUPPORT) +#error unsupported legacy feature option DUK_OPT_NO_BUFFEROBJECT_SUPPORT used +#endif +#if defined(DUK_OPT_NO_BYTECODE_DUMP_SUPPORT) +#error unsupported legacy feature option DUK_OPT_NO_BYTECODE_DUMP_SUPPORT used, consider options: DUK_USE_BYTECODE_DUMP_SUPPORT +#endif +#if defined(DUK_OPT_NO_COMMONJS_MODULES) +#error unsupported legacy feature option DUK_OPT_NO_COMMONJS_MODULES used +#endif +#if defined(DUK_OPT_NO_ES6_OBJECT_PROTO_PROPERTY) +#error unsupported legacy feature option DUK_OPT_NO_ES6_OBJECT_PROTO_PROPERTY used +#endif +#if defined(DUK_OPT_NO_ES6_OBJECT_SETPROTOTYPEOF) +#error unsupported legacy feature option DUK_OPT_NO_ES6_OBJECT_SETPROTOTYPEOF used +#endif +#if defined(DUK_OPT_NO_ES6_PROXY) +#error unsupported legacy feature option DUK_OPT_NO_ES6_PROXY used +#endif +#if defined(DUK_OPT_NO_FILE_IO) +#error unsupported legacy feature option DUK_OPT_NO_FILE_IO used, consider options: DUK_USE_FILE_IO +#endif +#if defined(DUK_OPT_NO_FUNC_STMT) +#error unsupported legacy feature option DUK_OPT_NO_FUNC_STMT used +#endif +#if defined(DUK_OPT_NO_JC) +#error unsupported legacy feature option DUK_OPT_NO_JC used +#endif +#if defined(DUK_OPT_NO_JSONC) +#error unsupported legacy feature option DUK_OPT_NO_JSONC used +#endif +#if defined(DUK_OPT_NO_JSONX) +#error unsupported legacy feature option DUK_OPT_NO_JSONX used +#endif +#if defined(DUK_OPT_NO_JX) +#error unsupported legacy feature option DUK_OPT_NO_JX used +#endif +#if defined(DUK_OPT_NO_MARK_AND_SWEEP) +#error unsupported legacy feature option DUK_OPT_NO_MARK_AND_SWEEP used +#endif +#if defined(DUK_OPT_NO_MS_STRINGTABLE_RESIZE) +#error unsupported legacy feature option DUK_OPT_NO_MS_STRINGTABLE_RESIZE used +#endif +#if defined(DUK_OPT_NO_NONSTD_ACCESSOR_KEY_ARGUMENT) +#error unsupported legacy feature option DUK_OPT_NO_NONSTD_ACCESSOR_KEY_ARGUMENT used +#endif +#if defined(DUK_OPT_NO_NONSTD_ARRAY_CONCAT_TRAILER) +#error unsupported legacy feature option DUK_OPT_NO_NONSTD_ARRAY_CONCAT_TRAILER used +#endif +#if defined(DUK_OPT_NO_NONSTD_ARRAY_MAP_TRAILER) +#error unsupported legacy feature option DUK_OPT_NO_NONSTD_ARRAY_MAP_TRAILER used +#endif +#if defined(DUK_OPT_NO_NONSTD_ARRAY_SPLICE_DELCOUNT) +#error unsupported legacy feature option DUK_OPT_NO_NONSTD_ARRAY_SPLICE_DELCOUNT used +#endif +#if defined(DUK_OPT_NO_NONSTD_FUNC_STMT) +#error unsupported legacy feature option DUK_OPT_NO_NONSTD_FUNC_STMT used +#endif +#if defined(DUK_OPT_NO_NONSTD_JSON_ESC_U2028_U2029) +#error unsupported legacy feature option DUK_OPT_NO_NONSTD_JSON_ESC_U2028_U2029 used +#endif +#if defined(DUK_OPT_NO_NONSTD_STRING_FROMCHARCODE_32BIT) +#error unsupported legacy feature option DUK_OPT_NO_NONSTD_STRING_FROMCHARCODE_32BIT used +#endif +#if defined(DUK_OPT_NO_OBJECT_ES6_PROTO_PROPERTY) +#error unsupported legacy feature option DUK_OPT_NO_OBJECT_ES6_PROTO_PROPERTY used +#endif +#if defined(DUK_OPT_NO_OBJECT_ES6_SETPROTOTYPEOF) +#error unsupported legacy feature option DUK_OPT_NO_OBJECT_ES6_SETPROTOTYPEOF used +#endif +#if defined(DUK_OPT_NO_OCTAL_SUPPORT) +#error unsupported legacy feature option DUK_OPT_NO_OCTAL_SUPPORT used +#endif +#if defined(DUK_OPT_NO_PACKED_TVAL) +#error unsupported legacy feature option DUK_OPT_NO_PACKED_TVAL used +#endif +#if defined(DUK_OPT_NO_PC2LINE) +#error unsupported legacy feature option DUK_OPT_NO_PC2LINE used +#endif +#if defined(DUK_OPT_NO_REFERENCE_COUNTING) +#error unsupported legacy feature option DUK_OPT_NO_REFERENCE_COUNTING used, consider options: DUK_USE_REFERENCE_COUNTING +#endif +#if defined(DUK_OPT_NO_REGEXP_SUPPORT) +#error unsupported legacy feature option DUK_OPT_NO_REGEXP_SUPPORT used +#endif +#if defined(DUK_OPT_NO_SECTION_B) +#error unsupported legacy feature option DUK_OPT_NO_SECTION_B used +#endif +#if defined(DUK_OPT_NO_SOURCE_NONBMP) +#error unsupported legacy feature option DUK_OPT_NO_SOURCE_NONBMP used +#endif +#if defined(DUK_OPT_NO_STRICT_DECL) +#error unsupported legacy feature option DUK_OPT_NO_STRICT_DECL used +#endif +#if defined(DUK_OPT_NO_TRACEBACKS) +#error unsupported legacy feature option DUK_OPT_NO_TRACEBACKS used +#endif +#if defined(DUK_OPT_NO_VERBOSE_ERRORS) +#error unsupported legacy feature option DUK_OPT_NO_VERBOSE_ERRORS used +#endif +#if defined(DUK_OPT_NO_VOLUNTARY_GC) +#error unsupported legacy feature option DUK_OPT_NO_VOLUNTARY_GC used +#endif +#if defined(DUK_OPT_NO_ZERO_BUFFER_DATA) +#error unsupported legacy feature option DUK_OPT_NO_ZERO_BUFFER_DATA used +#endif +#if defined(DUK_OPT_OBJSIZES16) +#error unsupported legacy feature option DUK_OPT_OBJSIZES16 used +#endif +#if defined(DUK_OPT_PANIC_HANDLER) +#error unsupported legacy feature option DUK_OPT_PANIC_HANDLER used +#endif +#if defined(DUK_OPT_REFCOUNT16) +#error unsupported legacy feature option DUK_OPT_REFCOUNT16 used +#endif +#if defined(DUK_OPT_SEGFAULT_ON_PANIC) +#error unsupported legacy feature option DUK_OPT_SEGFAULT_ON_PANIC used +#endif +#if defined(DUK_OPT_SELF_TESTS) +#error unsupported legacy feature option DUK_OPT_SELF_TESTS used +#endif +#if defined(DUK_OPT_SETJMP) +#error unsupported legacy feature option DUK_OPT_SETJMP used +#endif +#if defined(DUK_OPT_SHUFFLE_TORTURE) +#error unsupported legacy feature option DUK_OPT_SHUFFLE_TORTURE used +#endif +#if defined(DUK_OPT_SIGSETJMP) +#error unsupported legacy feature option DUK_OPT_SIGSETJMP used +#endif +#if defined(DUK_OPT_STRHASH16) +#error unsupported legacy feature option DUK_OPT_STRHASH16 used +#endif +#if defined(DUK_OPT_STRICT_UTF8_SOURCE) +#error unsupported legacy feature option DUK_OPT_STRICT_UTF8_SOURCE used +#endif +#if defined(DUK_OPT_STRLEN16) +#error unsupported legacy feature option DUK_OPT_STRLEN16 used +#endif +#if defined(DUK_OPT_STRTAB_CHAIN) +#error unsupported legacy feature option DUK_OPT_STRTAB_CHAIN used +#endif +#if defined(DUK_OPT_STRTAB_CHAIN_SIZE) +#error unsupported legacy feature option DUK_OPT_STRTAB_CHAIN_SIZE used +#endif +#if defined(DUK_OPT_TARGET_INFO) +#error unsupported legacy feature option DUK_OPT_TARGET_INFO used +#endif +#if defined(DUK_OPT_TRACEBACK_DEPTH) +#error unsupported legacy feature option DUK_OPT_TRACEBACK_DEPTH used +#endif +#if defined(DUK_OPT_UNDERSCORE_SETJMP) +#error unsupported legacy feature option DUK_OPT_UNDERSCORE_SETJMP used +#endif +#if defined(DUK_OPT_USER_INITJS) +#error unsupported legacy feature option DUK_OPT_USER_INITJS used +#endif + +/* + * Checks for config option consistency (DUK_USE_xxx) + */ + +#if defined(DUK_USE_32BIT_PTRS) +#error unsupported config option used (option has been removed): DUK_USE_32BIT_PTRS +#endif +#if defined(DUK_USE_ALIGN_4) +#error unsupported config option used (option has been removed): DUK_USE_ALIGN_4 +#endif +#if defined(DUK_USE_ALIGN_8) +#error unsupported config option used (option has been removed): DUK_USE_ALIGN_8 +#endif +#if defined(DUK_USE_BYTEORDER_FORCED) +#error unsupported config option used (option has been removed): DUK_USE_BYTEORDER_FORCED +#endif +#if defined(DUK_USE_DATAPTR_DEC16) && !defined(DUK_USE_DATAPTR16) +#error config option DUK_USE_DATAPTR_DEC16 requires option DUK_USE_DATAPTR16 (which is missing) +#endif +#if defined(DUK_USE_DATAPTR_ENC16) && !defined(DUK_USE_DATAPTR16) +#error config option DUK_USE_DATAPTR_ENC16 requires option DUK_USE_DATAPTR16 (which is missing) +#endif +#if defined(DUK_USE_DEBUGGER_SUPPORT) && !defined(DUK_USE_INTERRUPT_COUNTER) +#error config option DUK_USE_DEBUGGER_SUPPORT requires option DUK_USE_INTERRUPT_COUNTER (which is missing) +#endif +#if defined(DUK_USE_DEEP_C_STACK) +#error unsupported config option used (option has been removed): DUK_USE_DEEP_C_STACK +#endif +#if defined(DUK_USE_DOUBLE_BE) +#error unsupported config option used (option has been removed): DUK_USE_DOUBLE_BE +#endif +#if defined(DUK_USE_DOUBLE_BE) && defined(DUK_USE_DOUBLE_LE) +#error config option DUK_USE_DOUBLE_BE conflicts with option DUK_USE_DOUBLE_LE (which is also defined) +#endif +#if defined(DUK_USE_DOUBLE_BE) && defined(DUK_USE_DOUBLE_ME) +#error config option DUK_USE_DOUBLE_BE conflicts with option DUK_USE_DOUBLE_ME (which is also defined) +#endif +#if defined(DUK_USE_DOUBLE_LE) +#error unsupported config option used (option has been removed): DUK_USE_DOUBLE_LE +#endif +#if defined(DUK_USE_DOUBLE_LE) && defined(DUK_USE_DOUBLE_BE) +#error config option DUK_USE_DOUBLE_LE conflicts with option DUK_USE_DOUBLE_BE (which is also defined) +#endif +#if defined(DUK_USE_DOUBLE_LE) && defined(DUK_USE_DOUBLE_ME) +#error config option DUK_USE_DOUBLE_LE conflicts with option DUK_USE_DOUBLE_ME (which is also defined) +#endif +#if defined(DUK_USE_DOUBLE_ME) +#error unsupported config option used (option has been removed): DUK_USE_DOUBLE_ME +#endif +#if defined(DUK_USE_DOUBLE_ME) && defined(DUK_USE_DOUBLE_LE) +#error config option DUK_USE_DOUBLE_ME conflicts with option DUK_USE_DOUBLE_LE (which is also defined) +#endif +#if defined(DUK_USE_DOUBLE_ME) && defined(DUK_USE_DOUBLE_BE) +#error config option DUK_USE_DOUBLE_ME conflicts with option DUK_USE_DOUBLE_BE (which is also defined) +#endif +#if defined(DUK_USE_DPRINT) && !defined(DUK_USE_DEBUG) +#error config option DUK_USE_DPRINT requires option DUK_USE_DEBUG (which is missing) +#endif +#if defined(DUK_USE_ESBC_MAX_BYTES) && !defined(DUK_USE_ESBC_LIMITS) +#error config option DUK_USE_ESBC_MAX_BYTES requires option DUK_USE_ESBC_LIMITS (which is missing) +#endif +#if defined(DUK_USE_ESBC_MAX_LINENUMBER) && !defined(DUK_USE_ESBC_LIMITS) +#error config option DUK_USE_ESBC_MAX_LINENUMBER requires option DUK_USE_ESBC_LIMITS (which is missing) +#endif +#if defined(DUK_USE_EXEC_TIMEOUT_CHECK) && !defined(DUK_USE_INTERRUPT_COUNTER) +#error config option DUK_USE_EXEC_TIMEOUT_CHECK requires option DUK_USE_INTERRUPT_COUNTER (which is missing) +#endif +#if defined(DUK_USE_EXTSTR_FREE) && !defined(DUK_USE_HSTRING_EXTDATA) +#error config option DUK_USE_EXTSTR_FREE requires option DUK_USE_HSTRING_EXTDATA (which is missing) +#endif +#if defined(DUK_USE_EXTSTR_INTERN_CHECK) && !defined(DUK_USE_HSTRING_EXTDATA) +#error config option DUK_USE_EXTSTR_INTERN_CHECK requires option DUK_USE_HSTRING_EXTDATA (which is missing) +#endif +#if defined(DUK_USE_FULL_TVAL) +#error unsupported config option used (option has been removed): DUK_USE_FULL_TVAL +#endif +#if defined(DUK_USE_FUNCPTR_DEC16) && !defined(DUK_USE_FUNCPTR16) +#error config option DUK_USE_FUNCPTR_DEC16 requires option DUK_USE_FUNCPTR16 (which is missing) +#endif +#if defined(DUK_USE_FUNCPTR_ENC16) && !defined(DUK_USE_FUNCPTR16) +#error config option DUK_USE_FUNCPTR_ENC16 requires option DUK_USE_FUNCPTR16 (which is missing) +#endif +#if defined(DUK_USE_HASHBYTES_UNALIGNED_U32_ACCESS) +#error unsupported config option used (option has been removed): DUK_USE_HASHBYTES_UNALIGNED_U32_ACCESS +#endif +#if defined(DUK_USE_HEAPPTR16) && defined(DUK_USE_DEBUG) +#error config option DUK_USE_HEAPPTR16 conflicts with option DUK_USE_DEBUG (which is also defined) +#endif +#if defined(DUK_USE_HEAPPTR_DEC16) && !defined(DUK_USE_HEAPPTR16) +#error config option DUK_USE_HEAPPTR_DEC16 requires option DUK_USE_HEAPPTR16 (which is missing) +#endif +#if defined(DUK_USE_HEAPPTR_ENC16) && !defined(DUK_USE_HEAPPTR16) +#error config option DUK_USE_HEAPPTR_ENC16 requires option DUK_USE_HEAPPTR16 (which is missing) +#endif +#if defined(DUK_USE_INTEGER_BE) +#error unsupported config option used (option has been removed): DUK_USE_INTEGER_BE +#endif +#if defined(DUK_USE_INTEGER_BE) && defined(DUK_USE_INTEGER_LE) +#error config option DUK_USE_INTEGER_BE conflicts with option DUK_USE_INTEGER_LE (which is also defined) +#endif +#if defined(DUK_USE_INTEGER_BE) && defined(DUK_USE_INTEGER_ME) +#error config option DUK_USE_INTEGER_BE conflicts with option DUK_USE_INTEGER_ME (which is also defined) +#endif +#if defined(DUK_USE_INTEGER_LE) +#error unsupported config option used (option has been removed): DUK_USE_INTEGER_LE +#endif +#if defined(DUK_USE_INTEGER_LE) && defined(DUK_USE_INTEGER_BE) +#error config option DUK_USE_INTEGER_LE conflicts with option DUK_USE_INTEGER_BE (which is also defined) +#endif +#if defined(DUK_USE_INTEGER_LE) && defined(DUK_USE_INTEGER_ME) +#error config option DUK_USE_INTEGER_LE conflicts with option DUK_USE_INTEGER_ME (which is also defined) +#endif +#if defined(DUK_USE_INTEGER_ME) +#error unsupported config option used (option has been removed): DUK_USE_INTEGER_ME +#endif +#if defined(DUK_USE_INTEGER_ME) && defined(DUK_USE_INTEGER_LE) +#error config option DUK_USE_INTEGER_ME conflicts with option DUK_USE_INTEGER_LE (which is also defined) +#endif +#if defined(DUK_USE_INTEGER_ME) && defined(DUK_USE_INTEGER_BE) +#error config option DUK_USE_INTEGER_ME conflicts with option DUK_USE_INTEGER_BE (which is also defined) +#endif +#if defined(DUK_USE_NO_DOUBLE_ALIASING_SELFTEST) +#error unsupported config option used (option has been removed): DUK_USE_NO_DOUBLE_ALIASING_SELFTEST +#endif +#if defined(DUK_USE_PACKED_TVAL_POSSIBLE) +#error unsupported config option used (option has been removed): DUK_USE_PACKED_TVAL_POSSIBLE +#endif +#if defined(DUK_USE_RDTSC) +#error unsupported config option used (option has been removed): DUK_USE_RDTSC +#endif +#if defined(DUK_USE_ROM_GLOBAL_CLONE) && !defined(DUK_USE_ROM_STRINGS) +#error config option DUK_USE_ROM_GLOBAL_CLONE requires option DUK_USE_ROM_STRINGS (which is missing) +#endif +#if defined(DUK_USE_ROM_GLOBAL_CLONE) && !defined(DUK_USE_ROM_OBJECTS) +#error config option DUK_USE_ROM_GLOBAL_CLONE requires option DUK_USE_ROM_OBJECTS (which is missing) +#endif +#if defined(DUK_USE_ROM_GLOBAL_CLONE) && defined(DUK_USE_ROM_GLOBAL_INHERIT) +#error config option DUK_USE_ROM_GLOBAL_CLONE conflicts with option DUK_USE_ROM_GLOBAL_INHERIT (which is also defined) +#endif +#if defined(DUK_USE_ROM_GLOBAL_INHERIT) && !defined(DUK_USE_ROM_STRINGS) +#error config option DUK_USE_ROM_GLOBAL_INHERIT requires option DUK_USE_ROM_STRINGS (which is missing) +#endif +#if defined(DUK_USE_ROM_GLOBAL_INHERIT) && !defined(DUK_USE_ROM_OBJECTS) +#error config option DUK_USE_ROM_GLOBAL_INHERIT requires option DUK_USE_ROM_OBJECTS (which is missing) +#endif +#if defined(DUK_USE_ROM_GLOBAL_INHERIT) && defined(DUK_USE_ROM_GLOBAL_CLONE) +#error config option DUK_USE_ROM_GLOBAL_INHERIT conflicts with option DUK_USE_ROM_GLOBAL_CLONE (which is also defined) +#endif +#if defined(DUK_USE_ROM_OBJECTS) && !defined(DUK_USE_ROM_STRINGS) +#error config option DUK_USE_ROM_OBJECTS requires option DUK_USE_ROM_STRINGS (which is missing) +#endif +#if defined(DUK_USE_ROM_STRINGS) && !defined(DUK_USE_ROM_OBJECTS) +#error config option DUK_USE_ROM_STRINGS requires option DUK_USE_ROM_OBJECTS (which is missing) +#endif +#if defined(DUK_USE_SETJMP) +#error unsupported config option used (option has been removed): DUK_USE_SETJMP +#endif +#if defined(DUK_USE_SIGSETJMP) +#error unsupported config option used (option has been removed): DUK_USE_SIGSETJMP +#endif +#if defined(DUK_USE_STRTAB_CHAIN_SIZE) && !defined(DUK_USE_STRTAB_CHAIN) +#error config option DUK_USE_STRTAB_CHAIN_SIZE requires option DUK_USE_STRTAB_CHAIN (which is missing) +#endif +#if defined(DUK_USE_TAILCALL) && defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY) +#error config option DUK_USE_TAILCALL conflicts with option DUK_USE_NONSTD_FUNC_CALLER_PROPERTY (which is also defined) +#endif +#if defined(DUK_USE_UNALIGNED_ACCESSES_POSSIBLE) +#error unsupported config option used (option has been removed): DUK_USE_UNALIGNED_ACCESSES_POSSIBLE +#endif +#if defined(DUK_USE_UNDERSCORE_SETJMP) +#error unsupported config option used (option has been removed): DUK_USE_UNDERSCORE_SETJMP +#endif + +#if defined(DUK_USE_CPP_EXCEPTIONS) && !defined(__cplusplus) +#error DUK_USE_CPP_EXCEPTIONS enabled but not compiling with a C++ compiler +#endif + +/* + * Convert DUK_USE_BYTEORDER, from whatever source, into currently used + * internal defines. If detection failed, #error out. + */ + +#if defined(DUK_USE_BYTEORDER) +#if (DUK_USE_BYTEORDER == 1) +#define DUK_USE_INTEGER_LE +#define DUK_USE_DOUBLE_LE +#elif (DUK_USE_BYTEORDER == 2) +#define DUK_USE_INTEGER_LE /* integer endianness is little on purpose */ +#define DUK_USE_DOUBLE_ME +#elif (DUK_USE_BYTEORDER == 3) +#define DUK_USE_INTEGER_BE +#define DUK_USE_DOUBLE_BE +#else +#error unsupported: byte order invalid +#endif /* byte order */ +#else +#error unsupported: byte order detection failed +#endif /* defined(DUK_USE_BYTEORDER) */ + +#endif /* DUK_CONFIG_H_INCLUDED */ diff --git a/src/civetweb/src/third_party/duktape-1.5.2/config/duk_config.h-modular-static b/src/civetweb/src/third_party/duktape-1.5.2/config/duk_config.h-modular-static new file mode 100644 index 000000000..b2b5a38a4 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/config/duk_config.h-modular-static @@ -0,0 +1,3415 @@ +/* + * duk_config.h configuration header generated by genconfig.py. + * + * Git commit: cad34ae155acb0846545ca6bf2d29f9463b22bbb + * Git describe: v1.5.2 + * Git branch: HEAD + * + * Supported platforms: + * - Mac OSX, iPhone, Darwin + * - OpenBSD + * - Generic BSD + * - Atari ST TOS + * - AmigaOS + * - Windows + * - Flashplayer (Crossbridge) + * - QNX + * - TI-Nspire + * - Emscripten + * - Linux + * - Solaris + * - Generic POSIX + * - Cygwin + * - Generic UNIX + * - Generic fallback + * + * Supported architectures: + * - x86 + * - x64 + * - x32 + * - ARM 32-bit + * - ARM 64-bit + * - MIPS 32-bit + * - MIPS 64-bit + * - PowerPC 32-bit + * - PowerPC 64-bit + * - SPARC 32-bit + * - SPARC 64-bit + * - SuperH + * - Motorola 68k + * - Emscripten + * - Generic + * + * Supported compilers: + * - Clang + * - GCC + * - MSVC + * - Emscripten + * - TinyC + * - VBCC + * - Bruce's C compiler + * - Generic + * + */ + +#if !defined(DUK_CONFIG_H_INCLUDED) +#define DUK_CONFIG_H_INCLUDED + +/* + * Intermediate helper defines + */ + +/* DLL build detection */ +#if defined(DUK_OPT_DLL_BUILD) +#define DUK_F_DLL_BUILD +#elif defined(DUK_OPT_NO_DLL_BUILD) +#undef DUK_F_DLL_BUILD +#else +/* not configured for DLL build */ +#undef DUK_F_DLL_BUILD +#endif + +/* Apple OSX, iOS */ +#if defined(__APPLE__) +#define DUK_F_APPLE +#endif + +/* OpenBSD */ +#if defined(__OpenBSD__) || defined(__OpenBSD) +#define DUK_F_OPENBSD +#endif + +/* NetBSD */ +#if defined(__NetBSD__) || defined(__NetBSD) +#define DUK_F_NETBSD +#endif + +/* FreeBSD */ +#if defined(__FreeBSD__) || defined(__FreeBSD) +#define DUK_F_FREEBSD +#endif + +/* BSD variant */ +#if defined(DUK_F_FREEBSD) || defined(DUK_F_NETBSD) || defined(DUK_F_OPENBSD) || \ + defined(__bsdi__) || defined(__DragonFly__) +#define DUK_F_BSD +#endif + +/* Atari ST TOS. __TOS__ defined by PureC. No platform define in VBCC + * apparently, so to use with VBCC user must define __TOS__ manually. + */ +#if defined(__TOS__) +#define DUK_F_TOS +#endif + +/* Motorola 68K. Not defined by VBCC, so user must define one of these + * manually when using VBCC. + */ +#if defined(__m68k__) || defined(M68000) || defined(__MC68K__) +#define DUK_F_M68K +#endif + +/* AmigaOS. Neither AMIGA nor __amigaos__ is defined on VBCC, so user must + * define 'AMIGA' manually when using VBCC. + */ +#if defined(AMIGA) || defined(__amigaos__) +#define DUK_F_AMIGAOS +#endif + +/* PowerPC */ +#if defined(__powerpc) || defined(__powerpc__) || defined(__PPC__) +#define DUK_F_PPC +#if defined(__PPC64__) || defined(__LP64__) || defined(_LP64) +#define DUK_F_PPC64 +#else +#define DUK_F_PPC32 +#endif +#endif + +/* Windows, both 32-bit and 64-bit */ +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || \ + defined(__WIN32__) || defined(__TOS_WIN__) || defined(__WINDOWS__) +#define DUK_F_WINDOWS +#if defined(_WIN64) || defined(WIN64) +#define DUK_F_WIN64 +#else +#define DUK_F_WIN32 +#endif +#endif + +/* Flash player (e.g. Crossbridge) */ +#if defined(__FLASHPLAYER__) +#define DUK_F_FLASHPLAYER +#endif + +/* QNX */ +#if defined(__QNX__) +#define DUK_F_QNX +#endif + +/* TI-Nspire (using Ndless) */ +#if defined(_TINSPIRE) +#define DUK_F_TINSPIRE +#endif + +/* Emscripten (provided explicitly by user), improve if possible */ +#if defined(EMSCRIPTEN) +#define DUK_F_EMSCRIPTEN +#endif + +/* BCC (Bruce's C compiler): this is a "torture target" for compilation */ +#if defined(__BCC__) || defined(__BCC_VERSION__) +#define DUK_F_BCC +#endif + +/* Linux */ +#if defined(__linux) || defined(__linux__) || defined(linux) +#define DUK_F_LINUX +#endif + +/* illumos / Solaris */ +#if defined(__sun) && defined(__SVR4) +#define DUK_F_SUN +#endif + +/* POSIX */ +#if defined(__posix) +#define DUK_F_POSIX +#endif + +/* Cygwin */ +#if defined(__CYGWIN__) +#define DUK_F_CYGWIN +#endif + +/* Generic Unix (includes Cygwin) */ +#if defined(__unix) || defined(__unix__) || defined(unix) || \ + defined(DUK_F_LINUX) || defined(DUK_F_BSD) +#define DUK_F_UNIX +#endif + +/* stdint.h not available */ +#if defined(DUK_F_WINDOWS) && defined(_MSC_VER) +#if (_MSC_VER < 1700) +/* VS2012+ has stdint.h, < VS2012 does not (but it's available for download). */ +#define DUK_F_NO_STDINT_H +#endif +#endif +#if !defined(DUK_F_NO_STDINT_H) && (defined(DUK_F_TOS) || defined(DUK_F_BCC)) +#define DUK_F_NO_STDINT_H +#endif + +/* C++ */ +#undef DUK_F_CPP +#if defined(__cplusplus) +#define DUK_F_CPP +#endif + +/* Intel x86 (32-bit), x64 (64-bit) or x32 (64-bit but 32-bit pointers), + * define only one of DUK_F_X86, DUK_F_X64, DUK_F_X32. + * https://sites.google.com/site/x32abi/ + */ +#if defined(__amd64__) || defined(__amd64) || \ + defined(__x86_64__) || defined(__x86_64) || \ + defined(_M_X64) || defined(_M_AMD64) +#if defined(__ILP32__) || defined(_ILP32) +#define DUK_F_X32 +#else +#define DUK_F_X64 +#endif +#elif defined(i386) || defined(__i386) || defined(__i386__) || \ + defined(__i486__) || defined(__i586__) || defined(__i686__) || \ + defined(__IA32__) || defined(_M_IX86) || defined(__X86__) || \ + defined(_X86_) || defined(__THW_INTEL__) || defined(__I86__) +#if defined(__LP64__) || defined(_LP64) +/* This should not really happen, but would indicate x64. */ +#define DUK_F_X64 +#else +#define DUK_F_X86 +#endif +#endif + +/* ARM */ +#if defined(__arm__) || defined(__thumb__) || defined(_ARM) || defined(_M_ARM) +#define DUK_F_ARM +#if defined(__LP64__) || defined(_LP64) || defined(__arm64) || defined(__arm64__) +#define DUK_F_ARM64 +#else +#define DUK_F_ARM32 +#endif +#endif + +/* MIPS. Related defines: __MIPSEB__, __MIPSEL__, __mips_isa_rev, __LP64__ */ +#if defined(__mips__) || defined(mips) || defined(_MIPS_ISA) || \ + defined(_R3000) || defined(_R4000) || defined(_R5900) || \ + defined(_MIPS_ISA_MIPS1) || defined(_MIPS_ISA_MIPS2) || \ + defined(_MIPS_ISA_MIPS3) || defined(_MIPS_ISA_MIPS4) || \ + defined(__mips) || defined(__MIPS__) +#define DUK_F_MIPS +#if defined(__LP64__) || defined(_LP64) || defined(__mips64) || \ + defined(__mips64__) || defined(__mips_n64) +#define DUK_F_MIPS64 +#else +#define DUK_F_MIPS32 +#endif +#endif + +/* SPARC */ +#if defined(sparc) || defined(__sparc) || defined(__sparc__) +#define DUK_F_SPARC +#if defined(__LP64__) || defined(_LP64) +#define DUK_F_SPARC64 +#else +#define DUK_F_SPARC32 +#endif +#endif + +/* SuperH */ +#if defined(__sh__) || \ + defined(__sh1__) || defined(__SH1__) || \ + defined(__sh2__) || defined(__SH2__) || \ + defined(__sh3__) || defined(__SH3__) || \ + defined(__sh4__) || defined(__SH4__) || \ + defined(__sh5__) || defined(__SH5__) +#define DUK_F_SUPERH +#endif + +/* Clang */ +#if defined(__clang__) +#define DUK_F_CLANG +#endif + +/* C99 or above */ +#undef DUK_F_C99 +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) +#define DUK_F_C99 +#endif + +/* C++11 or above */ +#undef DUK_F_CPP11 +#if defined(__cplusplus) && (__cplusplus >= 201103L) +#define DUK_F_CPP11 +#endif + +/* GCC. Clang also defines __GNUC__ so don't detect GCC if using Clang. */ +#if defined(__GNUC__) && !defined(__clang__) && !defined(DUK_F_CLANG) +#define DUK_F_GCC +#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) +/* Convenience, e.g. gcc 4.5.1 == 40501; http://stackoverflow.com/questions/6031819/emulating-gccs-builtin-unreachable */ +#define DUK_F_GCC_VERSION (__GNUC__ * 10000L + __GNUC_MINOR__ * 100L + __GNUC_PATCHLEVEL__) +#else +#error cannot figure out gcc version +#endif +#endif + +/* MinGW. Also GCC flags (DUK_F_GCC) are enabled now. */ +#if defined(__MINGW32__) || defined(__MINGW64__) +#define DUK_F_MINGW +#endif + +/* MSVC */ +#if defined(_MSC_VER) +/* MSVC preprocessor defines: http://msdn.microsoft.com/en-us/library/b0084kay.aspx + * _MSC_FULL_VER includes the build number, but it has at least two formats, see e.g. + * BOOST_MSVC_FULL_VER in http://www.boost.org/doc/libs/1_52_0/boost/config/compiler/visualc.hpp + */ +#define DUK_F_MSVC +#if defined(_MSC_FULL_VER) +#if (_MSC_FULL_VER > 100000000) +#define DUK_F_MSVC_FULL_VER _MSC_FULL_VER +#else +#define DUK_F_MSCV_FULL_VER (_MSC_FULL_VER * 10) +#endif +#endif +#endif /* _MSC_VER */ + +/* TinyC */ +#if defined(__TINYC__) +/* http://bellard.org/tcc/tcc-doc.html#SEC9 */ +#define DUK_F_TINYC +#endif + +/* VBCC */ +#if defined(__VBCC__) +#define DUK_F_VBCC +#endif + +/* Atari Mint */ +#if defined(__MINT__) +#define DUK_F_MINT +#endif + +/* + * Platform autodetection + */ + +/* Workaround for older C++ compilers before including , + * see e.g.: https://sourceware.org/bugzilla/show_bug.cgi?id=15366 + */ +#if defined(__cplusplus) && !defined(__STDC_LIMIT_MACROS) +#define __STDC_LIMIT_MACROS +#endif +#if defined(__cplusplus) && !defined(__STDC_CONSTANT_MACROS) +#define __STDC_CONSTANT_MACROS +#endif + +#if defined(DUK_F_APPLE) +/* --- Mac OSX, iPhone, Darwin --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +/* http://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor */ +#if TARGET_IPHONE_SIMULATOR +#define DUK_USE_OS_STRING "iphone-sim" +#elif TARGET_OS_IPHONE +#define DUK_USE_OS_STRING "iphone" +#elif TARGET_OS_MAC +#define DUK_USE_OS_STRING "osx" +#else +#define DUK_USE_OS_STRING "osx-unknown" +#endif + +/* Use _setjmp() on Apple by default, see GH-55. */ +#define DUK_JMPBUF_TYPE jmp_buf +#define DUK_SETJMP(jb) _setjmp((jb)) +#define DUK_LONGJMP(jb) _longjmp((jb), 1) +#elif defined(DUK_F_OPENBSD) +/* --- OpenBSD --- */ +/* http://www.monkey.org/openbsd/archive/ports/0401/msg00089.html */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "openbsd" +#elif defined(DUK_F_BSD) +/* --- Generic BSD --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "bsd" +#elif defined(DUK_F_TOS) +/* --- Atari ST TOS --- */ +#define DUK_USE_DATE_NOW_TIME +#define DUK_USE_DATE_TZO_GMTIME +/* no parsing (not an error) */ +#define DUK_USE_DATE_FMT_STRFTIME +#include + +#define DUK_USE_OS_STRING "tos" + +/* TOS on M68K is always big endian. */ +#if !defined(DUK_USE_BYTEORDER) && defined(DUK_F_M68K) +#define DUK_USE_BYTEORDER 3 +#endif +#elif defined(DUK_F_AMIGAOS) +/* --- AmigaOS --- */ +#if defined(DUK_F_M68K) +/* AmigaOS on M68k */ +#define DUK_USE_DATE_NOW_TIME +#define DUK_USE_DATE_TZO_GMTIME +/* no parsing (not an error) */ +#define DUK_USE_DATE_FMT_STRFTIME +#include +#elif defined(DUK_F_PPC) +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#ifndef UINTPTR_MAX +#define UINTPTR_MAX UINT_MAX +#endif +#else +#error AmigaOS but not M68K/PPC, not supported now +#endif + +#define DUK_USE_OS_STRING "amigaos" + +/* AmigaOS on M68K or PPC is always big endian. */ +#if !defined(DUK_USE_BYTEORDER) && (defined(DUK_F_M68K) || defined(DUK_F_PPC)) +#define DUK_USE_BYTEORDER 3 +#endif +#elif defined(DUK_F_WINDOWS) +/* --- Windows --- */ +/* Initial fix: disable secure CRT related warnings when compiling Duktape + * itself (must be defined before including Windows headers). Don't define + * for user code including duktape.h. + */ +#if defined(DUK_COMPILING_DUKTAPE) && !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +/* Windows 32-bit and 64-bit are currently the same. */ +/* MSVC does not have sys/param.h */ +#define DUK_USE_DATE_NOW_WINDOWS +#define DUK_USE_DATE_TZO_WINDOWS +/* Note: PRS and FMT are intentionally left undefined for now. This means + * there is no platform specific date parsing/formatting but there is still + * the ISO 8601 standard format. + */ +#if defined(DUK_COMPILING_DUKTAPE) +/* Only include when compiling Duktape to avoid polluting application build + * with a lot of unnecessary defines. + */ +#include +#endif + +#define DUK_USE_OS_STRING "windows" + +/* On Windows, assume we're little endian. Even Itanium which has a + * configurable endianness runs little endian in Windows. + */ +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +#elif defined(DUK_F_FLASHPLAYER) +/* --- Flashplayer (Crossbridge) --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "flashplayer" + +#if !defined(DUK_USE_BYTEORDER) && defined(DUK_F_FLASHPLAYER) +#define DUK_USE_BYTEORDER 1 +#endif +#elif defined(DUK_F_QNX) +/* --- QNX --- */ +#if defined(DUK_F_QNX) && defined(DUK_COMPILING_DUKTAPE) +/* See: /opt/qnx650/target/qnx6/usr/include/sys/platform.h */ +#define _XOPEN_SOURCE 600 +#define _POSIX_C_SOURCE 200112L +#endif + +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "qnx" +#elif defined(DUK_F_TINSPIRE) +/* --- TI-Nspire --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "tinspire" +#elif defined(DUK_F_EMSCRIPTEN) +/* --- Emscripten --- */ +#if defined(DUK_COMPILING_DUKTAPE) +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#ifndef _GNU_SOURCE +#define _GNU_SOURCE /* e.g. getdate_r */ +#endif +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE /* e.g. strptime */ +#endif +#endif /* DUK_COMPILING_DUKTAPE */ + +#include +#if defined(DUK_F_BCC) +/* no endian.h */ +#else +#include +#endif /* DUK_F_BCC */ +#include +#include +#include +#include + +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME + +#define DUK_USE_OS_STRING "emscripten" +#elif defined(DUK_F_LINUX) +/* --- Linux --- */ +#if defined(DUK_COMPILING_DUKTAPE) +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#ifndef _GNU_SOURCE +#define _GNU_SOURCE /* e.g. getdate_r */ +#endif +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE /* e.g. strptime */ +#endif +#endif /* DUK_COMPILING_DUKTAPE */ + +#include +#if defined(DUK_F_BCC) +/* no endian.h or stdint.h */ +#else +#include +#include +#endif /* DUK_F_BCC */ +#include +#include +#include + +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME + +#define DUK_USE_OS_STRING "linux" +#elif defined(DUK_F_SUN) +/* --- Solaris --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME + +#include +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "solaris" +#elif defined(DUK_F_POSIX) +/* --- Generic POSIX --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "posix" +#elif defined(DUK_F_CYGWIN) +/* --- Cygwin --- */ +/* don't use strptime() for now */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +#define DUK_JMPBUF_TYPE jmp_buf +#define DUK_SETJMP(jb) _setjmp((jb)) +#define DUK_LONGJMP(jb) _longjmp((jb), 1) + +#define DUK_USE_OS_STRING "windows" +#elif defined(DUK_F_UNIX) +/* --- Generic UNIX --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#define DUK_USE_OS_STRING "unknown" +#else +/* --- Generic fallback --- */ +/* The most portable current time provider is time(), but it only has a + * one second resolution. + */ +#define DUK_USE_DATE_NOW_TIME + +/* The most portable way to figure out local time offset is gmtime(), + * but it's not thread safe so use with caution. + */ +#define DUK_USE_DATE_TZO_GMTIME + +/* Avoid custom date parsing and formatting for portability. */ +#undef DUK_USE_DATE_PRS_STRPTIME +#undef DUK_USE_DATE_FMT_STRFTIME + +/* Rely on C89 headers only; time.h must be here. */ +#include + +#define DUK_USE_OS_STRING "unknown" +#endif /* autodetect platform */ + +/* Shared includes: C89 */ +#include +#include +#include +#include /* varargs */ +#include +#include /* e.g. ptrdiff_t */ +#include +#include + +/* date.h is omitted, and included per platform */ + +/* Shared includes: stdint.h is C99 */ +#if defined(DUK_F_NO_STDINT_H) +/* stdint.h not available */ +#else +/* Technically C99 (C++11) but found in many systems. On some systems + * __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS must be defined before + * including stdint.h (see above). + */ +#include +#endif + +#if defined(DUK_F_CPP) +#include /* std::exception */ +#endif + +/* + * Architecture autodetection + */ + +#if defined(DUK_F_X86) +/* --- x86 --- */ +#define DUK_USE_ARCH_STRING "x86" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +/* XXX: This is technically not guaranteed because it's possible to configure + * an x86 to require aligned accesses with Alignment Check (AC) flag. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 1 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_X64) +/* --- x64 --- */ +#define DUK_USE_ARCH_STRING "x64" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +/* XXX: This is technically not guaranteed because it's possible to configure + * an x86 to require aligned accesses with Alignment Check (AC) flag. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 1 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_X32) +/* --- x32 --- */ +#define DUK_USE_ARCH_STRING "x32" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +/* XXX: This is technically not guaranteed because it's possible to configure + * an x86 to require aligned accesses with Alignment Check (AC) flag. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 1 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_ARM32) +/* --- ARM 32-bit --- */ +#define DUK_USE_ARCH_STRING "arm32" +/* Byte order varies, so rely on autodetect. */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 4 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_ARM64) +/* --- ARM 64-bit --- */ +#define DUK_USE_ARCH_STRING "arm64" +/* Byte order varies, so rely on autodetect. */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_MIPS32) +/* --- MIPS 32-bit --- */ +#define DUK_USE_ARCH_STRING "mips32" +/* MIPS byte order varies so rely on autodetection. */ +/* Based on 'make checkalign' there are no alignment requirements on + * Linux MIPS except for doubles, which need align by 4. Alignment + * requirements vary based on target though. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 4 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_MIPS64) +/* --- MIPS 64-bit --- */ +#define DUK_USE_ARCH_STRING "mips64" +/* MIPS byte order varies so rely on autodetection. */ +/* Good default is a bit arbitrary because alignment requirements + * depend on target. See https://github.com/svaarala/duktape/issues/102. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_PPC32) +/* --- PowerPC 32-bit --- */ +#define DUK_USE_ARCH_STRING "ppc32" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 3 +#endif +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_PPC64) +/* --- PowerPC 64-bit --- */ +#define DUK_USE_ARCH_STRING "ppc64" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 3 +#endif +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_SPARC32) +/* --- SPARC 32-bit --- */ +#define DUK_USE_ARCH_STRING "sparc32" +/* SPARC byte order varies so rely on autodetection. */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_SPARC64) +/* --- SPARC 64-bit --- */ +#define DUK_USE_ARCH_STRING "sparc64" +/* SPARC byte order varies so rely on autodetection. */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_SUPERH) +/* --- SuperH --- */ +#define DUK_USE_ARCH_STRING "sh" +/* Byte order varies, rely on autodetection. */ +/* Based on 'make checkalign' there are no alignment requirements on + * Linux SH4, but align by 4 is probably a good basic default. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 4 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_M68K) +/* --- Motorola 68k --- */ +#define DUK_USE_ARCH_STRING "m68k" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 3 +#endif +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_EMSCRIPTEN) +/* --- Emscripten --- */ +#define DUK_USE_ARCH_STRING "emscripten" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#else +/* --- Generic --- */ +/* These are necessary wild guesses. */ +#define DUK_USE_ARCH_STRING "generic" +/* Rely on autodetection for byte order, alignment, and packed tval. */ +#endif /* autodetect architecture */ + +/* + * Compiler autodetection + */ + +#if defined(DUK_F_CLANG) +/* --- Clang --- */ +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +/* C99 / C++11 and above: rely on va_copy() which is required. */ +#define DUK_VA_COPY(dest,src) va_copy(dest,src) +#else +/* Clang: assume we have __va_copy() in non-C99 mode. */ +#define DUK_VA_COPY(dest,src) __va_copy(dest,src) +#endif + +#define DUK_NORETURN(decl) decl __attribute__((noreturn)) + +#if defined(__clang__) && defined(__has_builtin) +#if __has_builtin(__builtin_unreachable) +#define DUK_UNREACHABLE() do { __builtin_unreachable(); } while (0) +#endif +#endif + +#define DUK_USE_BRANCH_HINTS +#define DUK_LIKELY(x) __builtin_expect((x), 1) +#define DUK_UNLIKELY(x) __builtin_expect((x), 0) + +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_NOINLINE __attribute__((noinline)) +#define DUK_INLINE inline +#define DUK_ALWAYS_INLINE inline __attribute__((always_inline)) +#endif + +#if defined(DUK_F_DLL_BUILD) && defined(DUK_F_WINDOWS) +/* MSVC dllexport/dllimport: appropriate __declspec depends on whether we're + * compiling Duktape or the application. + */ +#if defined(DUK_COMPILING_DUKTAPE) +#define DUK_EXTERNAL_DECL extern __declspec(dllexport) +#define DUK_EXTERNAL __declspec(dllexport) +#else +#define DUK_EXTERNAL_DECL extern __declspec(dllimport) +#define DUK_EXTERNAL should_not_happen +#endif +#if defined(DUK_SINGLE_FILE) +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#else +#define DUK_INTERNAL_DECL extern +#define DUK_INTERNAL /*empty*/ +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static +#else +#define DUK_EXTERNAL_DECL __attribute__ ((visibility("default"))) extern +#define DUK_EXTERNAL __attribute__ ((visibility("default"))) +#if defined(DUK_SINGLE_FILE) +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +/* Minimize warnings for unused internal functions with GCC >= 3.1.1 and + * Clang. Based on documentation it should suffice to have the attribute + * in the declaration only, but in practice some warnings are generated unless + * the attribute is also applied to the definition. + */ +#define DUK_INTERNAL_DECL static __attribute__ ((unused)) +#define DUK_INTERNAL static __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#endif +#else +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) +#endif +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static +#endif + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "clang" +#else +#define DUK_USE_COMPILER_STRING "clang" +#endif + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_USE_VARIADIC_MACROS +#endif + +#define DUK_USE_UNION_INITIALIZERS + +#undef DUK_USE_FLEX_C99 +#undef DUK_USE_FLEX_ZEROSIZE +#undef DUK_USE_FLEX_ONESIZE +#if defined(DUK_F_C99) +#define DUK_USE_FLEX_C99 +#else +#define DUK_USE_FLEX_ZEROSIZE +#endif + +#undef DUK_USE_GCC_PRAGMAS +#define DUK_USE_PACK_CLANG_ATTR +#elif defined(DUK_F_GCC) +/* --- GCC --- */ +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +/* C99 / C++11 and above: rely on va_copy() which is required. */ +#define DUK_VA_COPY(dest,src) va_copy(dest,src) +#else +/* GCC: assume we have __va_copy() in non-C99 mode. */ +#define DUK_VA_COPY(dest,src) __va_copy(dest,src) +#endif + +#if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 20500L) +/* since gcc-2.5 */ +#define DUK_NORETURN(decl) decl __attribute__((noreturn)) +#endif + +#if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 40500L) +/* since gcc-4.5 */ +#define DUK_UNREACHABLE() do { __builtin_unreachable(); } while (0) +#endif + +#define DUK_USE_BRANCH_HINTS +#if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 40500L) +/* GCC: test not very accurate; enable only in relatively recent builds + * because of bugs in gcc-4.4 (http://lists.debian.org/debian-gcc/2010/04/msg00000.html) + */ +#define DUK_LIKELY(x) __builtin_expect((x), 1) +#define DUK_UNLIKELY(x) __builtin_expect((x), 0) +#endif + +#if (defined(DUK_F_C99) || defined(DUK_F_CPP11)) && \ + defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 30101) +#define DUK_NOINLINE __attribute__((noinline)) +#define DUK_INLINE inline +#define DUK_ALWAYS_INLINE inline __attribute__((always_inline)) +#endif + +#if defined(DUK_F_DLL_BUILD) && defined(DUK_F_WINDOWS) +/* MSVC dllexport/dllimport: appropriate __declspec depends on whether we're + * compiling Duktape or the application. + */ +#if defined(DUK_COMPILING_DUKTAPE) +#define DUK_EXTERNAL_DECL extern __declspec(dllexport) +#define DUK_EXTERNAL __declspec(dllexport) +#else +#define DUK_EXTERNAL_DECL extern __declspec(dllimport) +#define DUK_EXTERNAL should_not_happen +#endif +#if defined(DUK_SINGLE_FILE) +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#else +#define DUK_INTERNAL_DECL extern +#define DUK_INTERNAL /*empty*/ +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static +#elif defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 40000) +#define DUK_EXTERNAL_DECL __attribute__ ((visibility("default"))) extern +#define DUK_EXTERNAL __attribute__ ((visibility("default"))) +#if defined(DUK_SINGLE_FILE) +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +/* Minimize warnings for unused internal functions with GCC >= 3.1.1 and + * Clang. Based on documentation it should suffice to have the attribute + * in the declaration only, but in practice some warnings are generated unless + * the attribute is also applied to the definition. + */ +#define DUK_INTERNAL_DECL static __attribute__ ((unused)) +#define DUK_INTERNAL static __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#endif +#else +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) +#endif +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static +#endif + +#if defined(DUK_F_MINGW) +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "mingw++" +#else +#define DUK_USE_COMPILER_STRING "mingw" +#endif +#else +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "g++" +#else +#define DUK_USE_COMPILER_STRING "gcc" +#endif +#endif + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) || (defined(DUK_F_CPP11) && defined(__GNUC__)) +#define DUK_USE_VARIADIC_MACROS +#endif + +#define DUK_USE_UNION_INITIALIZERS + +#undef DUK_USE_FLEX_C99 +#undef DUK_USE_FLEX_ZEROSIZE +#undef DUK_USE_FLEX_ONESIZE +#if defined(DUK_F_C99) +#define DUK_USE_FLEX_C99 +#else +#define DUK_USE_FLEX_ZEROSIZE +#endif + +#if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 40600) +#define DUK_USE_GCC_PRAGMAS +#else +#undef DUK_USE_GCC_PRAGMAS +#endif + +#define DUK_USE_PACK_GCC_ATTR +#elif defined(DUK_F_MSVC) +/* --- MSVC --- */ +/* http://msdn.microsoft.com/en-us/library/aa235362(VS.60).aspx */ +#define DUK_NORETURN(decl) __declspec(noreturn) decl + +/* XXX: DUK_UNREACHABLE for msvc? */ + +#undef DUK_USE_BRANCH_HINTS + +/* XXX: DUK_LIKELY, DUK_UNLIKELY for msvc? */ +/* XXX: DUK_NOINLINE, DUK_INLINE, DUK_ALWAYS_INLINE for msvc? */ + +#if defined(DUK_F_DLL_BUILD) && defined(DUK_F_WINDOWS) +/* MSVC dllexport/dllimport: appropriate __declspec depends on whether we're + * compiling Duktape or the application. + */ +#if defined(DUK_COMPILING_DUKTAPE) +#define DUK_EXTERNAL_DECL extern __declspec(dllexport) +#define DUK_EXTERNAL __declspec(dllexport) +#else +#define DUK_EXTERNAL_DECL extern __declspec(dllimport) +#define DUK_EXTERNAL should_not_happen +#endif +#if defined(DUK_SINGLE_FILE) +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#else +#define DUK_INTERNAL_DECL extern +#define DUK_INTERNAL /*empty*/ +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static +#endif + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "msvc++" +#else +#define DUK_USE_COMPILER_STRING "msvc" +#endif + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) +#define DUK_USE_VARIADIC_MACROS +#elif defined(_MSC_VER) && (_MSC_VER >= 1400) +/* VS2005+ should have variadic macros even when they're not C99. */ +#define DUK_USE_VARIADIC_MACROS +#endif + +#undef DUK_USE_UNION_INITIALIZERS +#if defined(_MSC_VER) && (_MSC_VER >= 1800) +/* VS2013+ supports union initializers but there's a bug involving union-inside-struct: + * https://connect.microsoft.com/VisualStudio/feedback/details/805981 + * The bug was fixed (at least) in VS2015 so check for VS2015 for now: + * https://blogs.msdn.microsoft.com/vcblog/2015/07/01/c-compiler-front-end-fixes-in-vs2015/ + * Manually tested using VS2013, CL reports 18.00.31101, so enable for VS2013 too. + */ +#define DUK_USE_UNION_INITIALIZERS +#endif + +#undef DUK_USE_FLEX_C99 +#undef DUK_USE_FLEX_ZEROSIZE +#undef DUK_USE_FLEX_ONESIZE +#if defined(DUK_F_C99) +#define DUK_USE_FLEX_C99 +#else +#define DUK_USE_FLEX_ZEROSIZE +#endif + +#undef DUK_USE_GCC_PRAGMAS + +#define DUK_USE_PACK_MSVC_PRAGMA + +/* These have been tested from VS2008 onwards; may work in older VS versions + * too but not enabled by default. + */ +#if defined(_MSC_VER) && (_MSC_VER >= 1500) +#define DUK_NOINLINE __declspec(noinline) +#define DUK_INLINE __inline +#define DUK_ALWAYS_INLINE __forceinline +#endif + +#if defined(_MSC_VER) && (_MSC_VER >= 1900) +#define DUK_SNPRINTF snprintf +#define DUK_VSNPRINTF vsnprintf +#else +/* (v)snprintf() is missing before MSVC 2015. Note that _(v)snprintf() does + * NOT NUL terminate on truncation, but Duktape code never assumes that. + * http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010 + */ +#define DUK_SNPRINTF _snprintf +#define DUK_VSNPRINTF _vsnprintf +#endif +#elif defined(DUK_F_EMSCRIPTEN) +/* --- Emscripten --- */ +#define DUK_NORETURN(decl) decl __attribute__((noreturn)) + +#if defined(__clang__) && defined(__has_builtin) +#if __has_builtin(__builtin_unreachable) +#define DUK_UNREACHABLE() do { __builtin_unreachable(); } while (0) +#endif +#endif + +#define DUK_USE_BRANCH_HINTS +#define DUK_LIKELY(x) __builtin_expect((x), 1) +#define DUK_UNLIKELY(x) __builtin_expect((x), 0) + +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_NOINLINE __attribute__((noinline)) +#define DUK_INLINE inline +#define DUK_ALWAYS_INLINE inline __attribute__((always_inline)) +#endif + +#define DUK_EXTERNAL_DECL __attribute__ ((visibility("default"))) extern +#define DUK_EXTERNAL __attribute__ ((visibility("default"))) +#if defined(DUK_SINGLE_FILE) +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +/* Minimize warnings for unused internal functions with GCC >= 3.1.1 and + * Clang. Based on documentation it should suffice to have the attribute + * in the declaration only, but in practice some warnings are generated unless + * the attribute is also applied to the definition. + */ +#define DUK_INTERNAL_DECL static __attribute__ ((unused)) +#define DUK_INTERNAL static __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#endif +#else +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) +#endif +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static + +#define DUK_USE_COMPILER_STRING "emscripten" + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_USE_VARIADIC_MACROS +#endif + +#define DUK_USE_UNION_INITIALIZERS + +#undef DUK_USE_FLEX_C99 +#undef DUK_USE_FLEX_ZEROSIZE +#undef DUK_USE_FLEX_ONESIZE +#if defined(DUK_F_C99) +#define DUK_USE_FLEX_C99 +#else +#define DUK_USE_FLEX_ZEROSIZE +#endif + +#undef DUK_USE_GCC_PRAGMAS +#define DUK_USE_PACK_CLANG_ATTR +#elif defined(DUK_F_TINYC) +/* --- TinyC --- */ +#undef DUK_USE_BRANCH_HINTS + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "tinyc++" +#else +#define DUK_USE_COMPILER_STRING "tinyc" +#endif + +/* http://bellard.org/tcc/tcc-doc.html#SEC7 */ +#define DUK_USE_VARIADIC_MACROS + +#define DUK_USE_UNION_INITIALIZERS + +/* Most portable, wastes space */ +#define DUK_USE_FLEX_ONESIZE + +/* Most portable, potentially wastes space */ +#define DUK_USE_PACK_DUMMY_MEMBER +#elif defined(DUK_F_VBCC) +/* --- VBCC --- */ +#undef DUK_USE_BRANCH_HINTS + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "vbcc-c++" +#else +#define DUK_USE_COMPILER_STRING "vbcc" +#endif + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_USE_VARIADIC_MACROS +#endif + +/* VBCC supports C99 so check only for C99 for union initializer support. + * Designated union initializers would possibly work even without a C99 check. + */ +#undef DUK_USE_UNION_INITIALIZERS +#if defined(DUK_F_C99) +#define DUK_USE_UNION_INITIALIZERS +#endif + +#define DUK_USE_FLEX_ZEROSIZE +#define DUK_USE_PACK_DUMMY_MEMBER +#elif defined(DUK_F_BCC) +/* --- Bruce's C compiler --- */ +#undef DUK_USE_BRANCH_HINTS + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "bcc++" +#else +#define DUK_USE_COMPILER_STRING "bcc" +#endif + +/* Most portable */ +#undef DUK_USE_VARIADIC_MACROS + +/* Most portable, wastes space */ +#undef DUK_USE_UNION_INITIALIZERS + +/* Most portable, wastes space */ +#define DUK_USE_FLEX_ONESIZE + +/* Most portable, potentially wastes space */ +#define DUK_USE_PACK_DUMMY_MEMBER + +/* BCC, assume we're on x86. */ +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +#else +/* --- Generic --- */ +#undef DUK_USE_BRANCH_HINTS + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "generic-c++" +#else +#define DUK_USE_COMPILER_STRING "generic" +#endif + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_USE_VARIADIC_MACROS +#endif + +/* C++ doesn't have standard designated union initializers ({ .foo = 1 }). */ +#undef DUK_USE_UNION_INITIALIZERS +#if defined(DUK_F_C99) +#define DUK_USE_UNION_INITIALIZERS +#endif + +/* Most portable, wastes space */ +#define DUK_USE_FLEX_ONESIZE + +/* Most portable, potentially wastes space */ +#define DUK_USE_PACK_DUMMY_MEMBER +#endif /* autodetect compiler */ + +/* uclibc */ +#if defined(__UCLIBC__) +#define DUK_F_UCLIBC +#endif + +/* + * Wrapper typedefs and constants for integer types, also sanity check types. + * + * C99 typedefs are quite good but not always available, and we want to avoid + * forcibly redefining the C99 typedefs. So, there are Duktape wrappers for + * all C99 typedefs and Duktape code should only use these typedefs. Type + * detection when C99 is not supported is best effort and may end up detecting + * some types incorrectly. + * + * Pointer sizes are a portability problem: pointers to different types may + * have a different size and function pointers are very difficult to manage + * portably. + * + * http://en.wikipedia.org/wiki/C_data_types#Fixed-width_integer_types + * + * Note: there's an interesting corner case when trying to define minimum + * signed integer value constants which leads to the current workaround of + * defining e.g. -0x80000000 as (-0x7fffffffL - 1L). See doc/code-issues.txt + * for a longer discussion. + * + * Note: avoid typecasts and computations in macro integer constants as they + * can then no longer be used in macro relational expressions (such as + * #if DUK_SIZE_MAX < 0xffffffffUL). There is internal code which relies on + * being able to compare DUK_SIZE_MAX against a limit. + */ + +/* XXX: add feature options to force basic types from outside? */ + +#if !defined(INT_MAX) +#error INT_MAX not defined +#endif + +/* Check that architecture is two's complement, standard C allows e.g. + * INT_MIN to be -2**31+1 (instead of -2**31). + */ +#if defined(INT_MAX) && defined(INT_MIN) +#if INT_MAX != -(INT_MIN + 1) +#error platform does not seem complement of two +#endif +#else +#error cannot check complement of two +#endif + +/* Pointer size determination based on __WORDSIZE or architecture when + * that's not available. + */ +#if defined(DUK_F_X86) || defined(DUK_F_X32) || \ + defined(DUK_F_M68K) || defined(DUK_F_PPC32) || \ + defined(DUK_F_BCC) || \ + (defined(__WORDSIZE) && (__WORDSIZE == 32)) +#define DUK_F_32BIT_PTRS +#elif defined(DUK_F_X64) || \ + (defined(__WORDSIZE) && (__WORDSIZE == 64)) +#define DUK_F_64BIT_PTRS +#else +/* not sure, not needed with C99 anyway */ +#endif + +/* Intermediate define for 'have inttypes.h' */ +#undef DUK_F_HAVE_INTTYPES +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \ + !(defined(DUK_F_AMIGAOS) && defined(DUK_F_VBCC)) +/* vbcc + AmigaOS has C99 but no inttypes.h */ +#define DUK_F_HAVE_INTTYPES +#elif defined(__cplusplus) && (__cplusplus >= 201103L) +/* C++11 apparently ratified stdint.h */ +#define DUK_F_HAVE_INTTYPES +#endif + +/* Basic integer typedefs and limits, preferably from inttypes.h, otherwise + * through automatic detection. + */ +#if defined(DUK_F_HAVE_INTTYPES) +/* C99 or compatible */ + +#define DUK_F_HAVE_64BIT +#include + +typedef uint8_t duk_uint8_t; +typedef int8_t duk_int8_t; +typedef uint16_t duk_uint16_t; +typedef int16_t duk_int16_t; +typedef uint32_t duk_uint32_t; +typedef int32_t duk_int32_t; +typedef uint64_t duk_uint64_t; +typedef int64_t duk_int64_t; +typedef uint_least8_t duk_uint_least8_t; +typedef int_least8_t duk_int_least8_t; +typedef uint_least16_t duk_uint_least16_t; +typedef int_least16_t duk_int_least16_t; +typedef uint_least32_t duk_uint_least32_t; +typedef int_least32_t duk_int_least32_t; +typedef uint_least64_t duk_uint_least64_t; +typedef int_least64_t duk_int_least64_t; +typedef uint_fast8_t duk_uint_fast8_t; +typedef int_fast8_t duk_int_fast8_t; +typedef uint_fast16_t duk_uint_fast16_t; +typedef int_fast16_t duk_int_fast16_t; +typedef uint_fast32_t duk_uint_fast32_t; +typedef int_fast32_t duk_int_fast32_t; +typedef uint_fast64_t duk_uint_fast64_t; +typedef int_fast64_t duk_int_fast64_t; +typedef uintptr_t duk_uintptr_t; +typedef intptr_t duk_intptr_t; +typedef uintmax_t duk_uintmax_t; +typedef intmax_t duk_intmax_t; + +#define DUK_UINT8_MIN 0 +#define DUK_UINT8_MAX UINT8_MAX +#define DUK_INT8_MIN INT8_MIN +#define DUK_INT8_MAX INT8_MAX +#define DUK_UINT_LEAST8_MIN 0 +#define DUK_UINT_LEAST8_MAX UINT_LEAST8_MAX +#define DUK_INT_LEAST8_MIN INT_LEAST8_MIN +#define DUK_INT_LEAST8_MAX INT_LEAST8_MAX +#define DUK_UINT_FAST8_MIN 0 +#define DUK_UINT_FAST8_MAX UINT_FAST8_MAX +#define DUK_INT_FAST8_MIN INT_FAST8_MIN +#define DUK_INT_FAST8_MAX INT_FAST8_MAX +#define DUK_UINT16_MIN 0 +#define DUK_UINT16_MAX UINT16_MAX +#define DUK_INT16_MIN INT16_MIN +#define DUK_INT16_MAX INT16_MAX +#define DUK_UINT_LEAST16_MIN 0 +#define DUK_UINT_LEAST16_MAX UINT_LEAST16_MAX +#define DUK_INT_LEAST16_MIN INT_LEAST16_MIN +#define DUK_INT_LEAST16_MAX INT_LEAST16_MAX +#define DUK_UINT_FAST16_MIN 0 +#define DUK_UINT_FAST16_MAX UINT_FAST16_MAX +#define DUK_INT_FAST16_MIN INT_FAST16_MIN +#define DUK_INT_FAST16_MAX INT_FAST16_MAX +#define DUK_UINT32_MIN 0 +#define DUK_UINT32_MAX UINT32_MAX +#define DUK_INT32_MIN INT32_MIN +#define DUK_INT32_MAX INT32_MAX +#define DUK_UINT_LEAST32_MIN 0 +#define DUK_UINT_LEAST32_MAX UINT_LEAST32_MAX +#define DUK_INT_LEAST32_MIN INT_LEAST32_MIN +#define DUK_INT_LEAST32_MAX INT_LEAST32_MAX +#define DUK_UINT_FAST32_MIN 0 +#define DUK_UINT_FAST32_MAX UINT_FAST32_MAX +#define DUK_INT_FAST32_MIN INT_FAST32_MIN +#define DUK_INT_FAST32_MAX INT_FAST32_MAX +#define DUK_UINT64_MIN 0 +#define DUK_UINT64_MAX UINT64_MAX +#define DUK_INT64_MIN INT64_MIN +#define DUK_INT64_MAX INT64_MAX +#define DUK_UINT_LEAST64_MIN 0 +#define DUK_UINT_LEAST64_MAX UINT_LEAST64_MAX +#define DUK_INT_LEAST64_MIN INT_LEAST64_MIN +#define DUK_INT_LEAST64_MAX INT_LEAST64_MAX +#define DUK_UINT_FAST64_MIN 0 +#define DUK_UINT_FAST64_MAX UINT_FAST64_MAX +#define DUK_INT_FAST64_MIN INT_FAST64_MIN +#define DUK_INT_FAST64_MAX INT_FAST64_MAX + +#define DUK_UINTPTR_MIN 0 +#define DUK_UINTPTR_MAX UINTPTR_MAX +#define DUK_INTPTR_MIN INTPTR_MIN +#define DUK_INTPTR_MAX INTPTR_MAX + +#define DUK_UINTMAX_MIN 0 +#define DUK_UINTMAX_MAX UINTMAX_MAX +#define DUK_INTMAX_MIN INTMAX_MIN +#define DUK_INTMAX_MAX INTMAX_MAX + +#define DUK_SIZE_MIN 0 +#define DUK_SIZE_MAX SIZE_MAX +#undef DUK_SIZE_MAX_COMPUTED + +#else /* C99 types */ + +/* When C99 types are not available, we use heuristic detection to get + * the basic 8, 16, 32, and (possibly) 64 bit types. The fast/least + * types are then assumed to be exactly the same for now: these could + * be improved per platform but C99 types are very often now available. + * 64-bit types are not available on all platforms; this is OK at least + * on 32-bit platforms. + * + * This detection code is necessarily a bit hacky and can provide typedefs + * and defines that won't work correctly on some exotic platform. + */ + +#if (defined(CHAR_BIT) && (CHAR_BIT == 8)) || \ + (defined(UCHAR_MAX) && (UCHAR_MAX == 255)) +typedef unsigned char duk_uint8_t; +typedef signed char duk_int8_t; +#else +#error cannot detect 8-bit type +#endif + +#if defined(USHRT_MAX) && (USHRT_MAX == 65535UL) +typedef unsigned short duk_uint16_t; +typedef signed short duk_int16_t; +#elif defined(UINT_MAX) && (UINT_MAX == 65535UL) +/* On some platforms int is 16-bit but long is 32-bit (e.g. PureC) */ +typedef unsigned int duk_uint16_t; +typedef signed int duk_int16_t; +#else +#error cannot detect 16-bit type +#endif + +#if defined(UINT_MAX) && (UINT_MAX == 4294967295UL) +typedef unsigned int duk_uint32_t; +typedef signed int duk_int32_t; +#elif defined(ULONG_MAX) && (ULONG_MAX == 4294967295UL) +/* On some platforms int is 16-bit but long is 32-bit (e.g. PureC) */ +typedef unsigned long duk_uint32_t; +typedef signed long duk_int32_t; +#else +#error cannot detect 32-bit type +#endif + +/* 64-bit type detection is a bit tricky. + * + * ULLONG_MAX is a standard define. __LONG_LONG_MAX__ and __ULONG_LONG_MAX__ + * are used by at least GCC (even if system headers don't provide ULLONG_MAX). + * Some GCC variants may provide __LONG_LONG_MAX__ but not __ULONG_LONG_MAX__. + * + * ULL / LL constants are rejected / warned about by some compilers, even if + * the compiler has a 64-bit type and the compiler/system headers provide an + * unsupported constant (ULL/LL)! Try to avoid using ULL / LL constants. + * As a side effect we can only check that e.g. ULONG_MAX is larger than 32 + * bits but can't be sure it is exactly 64 bits. Self tests will catch such + * cases. + */ +#undef DUK_F_HAVE_64BIT +#if !defined(DUK_F_HAVE_64BIT) && defined(ULONG_MAX) +#if (ULONG_MAX > 4294967295UL) +#define DUK_F_HAVE_64BIT +typedef unsigned long duk_uint64_t; +typedef signed long duk_int64_t; +#endif +#endif +#if !defined(DUK_F_HAVE_64BIT) && defined(ULLONG_MAX) +#if (ULLONG_MAX > 4294967295UL) +#define DUK_F_HAVE_64BIT +typedef unsigned long long duk_uint64_t; +typedef signed long long duk_int64_t; +#endif +#endif +#if !defined(DUK_F_HAVE_64BIT) && defined(__ULONG_LONG_MAX__) +#if (__ULONG_LONG_MAX__ > 4294967295UL) +#define DUK_F_HAVE_64BIT +typedef unsigned long long duk_uint64_t; +typedef signed long long duk_int64_t; +#endif +#endif +#if !defined(DUK_F_HAVE_64BIT) && defined(__LONG_LONG_MAX__) +#if (__LONG_LONG_MAX__ > 2147483647L) +#define DUK_F_HAVE_64BIT +typedef unsigned long long duk_uint64_t; +typedef signed long long duk_int64_t; +#endif +#endif +#if !defined(DUK_F_HAVE_64BIT) && \ + (defined(DUK_F_MINGW) || defined(DUK_F_MSVC)) +/* Both MinGW and MSVC have a 64-bit type. */ +#define DUK_F_HAVE_64BIT +typedef unsigned long duk_uint64_t; +typedef signed long duk_int64_t; +#endif +#if !defined(DUK_F_HAVE_64BIT) +/* cannot detect 64-bit type, not always needed so don't error */ +#endif + +typedef duk_uint8_t duk_uint_least8_t; +typedef duk_int8_t duk_int_least8_t; +typedef duk_uint16_t duk_uint_least16_t; +typedef duk_int16_t duk_int_least16_t; +typedef duk_uint32_t duk_uint_least32_t; +typedef duk_int32_t duk_int_least32_t; +typedef duk_uint8_t duk_uint_fast8_t; +typedef duk_int8_t duk_int_fast8_t; +typedef duk_uint16_t duk_uint_fast16_t; +typedef duk_int16_t duk_int_fast16_t; +typedef duk_uint32_t duk_uint_fast32_t; +typedef duk_int32_t duk_int_fast32_t; +#if defined(DUK_F_HAVE_64BIT) +typedef duk_uint64_t duk_uint_least64_t; +typedef duk_int64_t duk_int_least64_t; +typedef duk_uint64_t duk_uint_fast64_t; +typedef duk_int64_t duk_int_fast64_t; +#endif +#if defined(DUK_F_HAVE_64BIT) +typedef duk_uint64_t duk_uintmax_t; +typedef duk_int64_t duk_intmax_t; +#else +typedef duk_uint32_t duk_uintmax_t; +typedef duk_int32_t duk_intmax_t; +#endif + +/* Note: the funny looking computations for signed minimum 16-bit, 32-bit, and + * 64-bit values are intentional as the obvious forms (e.g. -0x80000000L) are + * -not- portable. See code-issues.txt for a detailed discussion. + */ +#define DUK_UINT8_MIN 0UL +#define DUK_UINT8_MAX 0xffUL +#define DUK_INT8_MIN (-0x80L) +#define DUK_INT8_MAX 0x7fL +#define DUK_UINT_LEAST8_MIN 0UL +#define DUK_UINT_LEAST8_MAX 0xffUL +#define DUK_INT_LEAST8_MIN (-0x80L) +#define DUK_INT_LEAST8_MAX 0x7fL +#define DUK_UINT_FAST8_MIN 0UL +#define DUK_UINT_FAST8_MAX 0xffUL +#define DUK_INT_FAST8_MIN (-0x80L) +#define DUK_INT_FAST8_MAX 0x7fL +#define DUK_UINT16_MIN 0UL +#define DUK_UINT16_MAX 0xffffUL +#define DUK_INT16_MIN (-0x7fffL - 1L) +#define DUK_INT16_MAX 0x7fffL +#define DUK_UINT_LEAST16_MIN 0UL +#define DUK_UINT_LEAST16_MAX 0xffffUL +#define DUK_INT_LEAST16_MIN (-0x7fffL - 1L) +#define DUK_INT_LEAST16_MAX 0x7fffL +#define DUK_UINT_FAST16_MIN 0UL +#define DUK_UINT_FAST16_MAX 0xffffUL +#define DUK_INT_FAST16_MIN (-0x7fffL - 1L) +#define DUK_INT_FAST16_MAX 0x7fffL +#define DUK_UINT32_MIN 0UL +#define DUK_UINT32_MAX 0xffffffffUL +#define DUK_INT32_MIN (-0x7fffffffL - 1L) +#define DUK_INT32_MAX 0x7fffffffL +#define DUK_UINT_LEAST32_MIN 0UL +#define DUK_UINT_LEAST32_MAX 0xffffffffUL +#define DUK_INT_LEAST32_MIN (-0x7fffffffL - 1L) +#define DUK_INT_LEAST32_MAX 0x7fffffffL +#define DUK_UINT_FAST32_MIN 0UL +#define DUK_UINT_FAST32_MAX 0xffffffffUL +#define DUK_INT_FAST32_MIN (-0x7fffffffL - 1L) +#define DUK_INT_FAST32_MAX 0x7fffffffL + +/* 64-bit constants. Since LL / ULL constants are not always available, + * use computed values. These values can't be used in preprocessor + * comparisons; flag them as such. + */ +#if defined(DUK_F_HAVE_64BIT) +#define DUK_UINT64_MIN ((duk_uint64_t) 0) +#define DUK_UINT64_MAX ((duk_uint64_t) -1) +#define DUK_INT64_MIN ((duk_int64_t) (~(DUK_UINT64_MAX >> 1))) +#define DUK_INT64_MAX ((duk_int64_t) (DUK_UINT64_MAX >> 1)) +#define DUK_UINT_LEAST64_MIN DUK_UINT64_MIN +#define DUK_UINT_LEAST64_MAX DUK_UINT64_MAX +#define DUK_INT_LEAST64_MIN DUK_INT64_MIN +#define DUK_INT_LEAST64_MAX DUK_INT64_MAX +#define DUK_UINT_FAST64_MIN DUK_UINT64_MIN +#define DUK_UINT_FAST64_MAX DUK_UINT64_MAX +#define DUK_INT_FAST64_MIN DUK_INT64_MIN +#define DUK_INT_FAST64_MAX DUK_INT64_MAX +#define DUK_UINT64_MIN_COMPUTED +#define DUK_UINT64_MAX_COMPUTED +#define DUK_INT64_MIN_COMPUTED +#define DUK_INT64_MAX_COMPUTED +#define DUK_UINT_LEAST64_MIN_COMPUTED +#define DUK_UINT_LEAST64_MAX_COMPUTED +#define DUK_INT_LEAST64_MIN_COMPUTED +#define DUK_INT_LEAST64_MAX_COMPUTED +#define DUK_UINT_FAST64_MIN_COMPUTED +#define DUK_UINT_FAST64_MAX_COMPUTED +#define DUK_INT_FAST64_MIN_COMPUTED +#define DUK_INT_FAST64_MAX_COMPUTED +#endif + +#if defined(DUK_F_HAVE_64BIT) +#define DUK_UINTMAX_MIN DUK_UINT64_MIN +#define DUK_UINTMAX_MAX DUK_UINT64_MAX +#define DUK_INTMAX_MIN DUK_INT64_MIN +#define DUK_INTMAX_MAX DUK_INT64_MAX +#define DUK_UINTMAX_MIN_COMPUTED +#define DUK_UINTMAX_MAX_COMPUTED +#define DUK_INTMAX_MIN_COMPUTED +#define DUK_INTMAX_MAX_COMPUTED +#else +#define DUK_UINTMAX_MIN 0UL +#define DUK_UINTMAX_MAX 0xffffffffUL +#define DUK_INTMAX_MIN (-0x7fffffffL - 1L) +#define DUK_INTMAX_MAX 0x7fffffffL +#endif + +/* This detection is not very reliable. */ +#if defined(DUK_F_32BIT_PTRS) +typedef duk_int32_t duk_intptr_t; +typedef duk_uint32_t duk_uintptr_t; +#define DUK_UINTPTR_MIN DUK_UINT32_MIN +#define DUK_UINTPTR_MAX DUK_UINT32_MAX +#define DUK_INTPTR_MIN DUK_INT32_MIN +#define DUK_INTPTR_MAX DUK_INT32_MAX +#elif defined(DUK_F_64BIT_PTRS) && defined(DUK_F_HAVE_64BIT) +typedef duk_int64_t duk_intptr_t; +typedef duk_uint64_t duk_uintptr_t; +#define DUK_UINTPTR_MIN DUK_UINT64_MIN +#define DUK_UINTPTR_MAX DUK_UINT64_MAX +#define DUK_INTPTR_MIN DUK_INT64_MIN +#define DUK_INTPTR_MAX DUK_INT64_MAX +#define DUK_UINTPTR_MIN_COMPUTED +#define DUK_UINTPTR_MAX_COMPUTED +#define DUK_INTPTR_MIN_COMPUTED +#define DUK_INTPTR_MAX_COMPUTED +#else +#error cannot determine intptr type +#endif + +/* SIZE_MAX may be missing so use an approximate value for it. */ +#undef DUK_SIZE_MAX_COMPUTED +#if !defined(SIZE_MAX) +#define DUK_SIZE_MAX_COMPUTED +#define SIZE_MAX ((size_t) (-1)) +#endif +#define DUK_SIZE_MIN 0 +#define DUK_SIZE_MAX SIZE_MAX + +#endif /* C99 types */ + +/* A few types are assumed to always exist. */ +typedef size_t duk_size_t; +typedef ptrdiff_t duk_ptrdiff_t; + +/* The best type for an "all around int" in Duktape internals is "at least + * 32 bit signed integer" which is most convenient. Same for unsigned type. + * Prefer 'int' when large enough, as it is almost always a convenient type. + */ +#if defined(UINT_MAX) && (UINT_MAX >= 0xffffffffUL) +typedef int duk_int_t; +typedef unsigned int duk_uint_t; +#define DUK_INT_MIN INT_MIN +#define DUK_INT_MAX INT_MAX +#define DUK_UINT_MIN 0 +#define DUK_UINT_MAX UINT_MAX +#else +typedef duk_int_fast32_t duk_int_t; +typedef duk_uint_fast32_t duk_uint_t; +#define DUK_INT_MIN DUK_INT_FAST32_MIN +#define DUK_INT_MAX DUK_INT_FAST32_MAX +#define DUK_UINT_MIN DUK_UINT_FAST32_MIN +#define DUK_UINT_MAX DUK_UINT_FAST32_MAX +#endif + +/* Same as 'duk_int_t' but guaranteed to be a 'fast' variant if this + * distinction matters for the CPU. These types are used mainly in the + * executor where it might really matter. + */ +typedef duk_int_fast32_t duk_int_fast_t; +typedef duk_uint_fast32_t duk_uint_fast_t; +#define DUK_INT_FAST_MIN DUK_INT_FAST32_MIN +#define DUK_INT_FAST_MAX DUK_INT_FAST32_MAX +#define DUK_UINT_FAST_MIN DUK_UINT_FAST32_MIN +#define DUK_UINT_FAST_MAX DUK_UINT_FAST32_MAX + +/* Small integers (16 bits or more) can fall back to the 'int' type, but + * have a typedef so they are marked "small" explicitly. + */ +typedef int duk_small_int_t; +typedef unsigned int duk_small_uint_t; +#define DUK_SMALL_INT_MIN INT_MIN +#define DUK_SMALL_INT_MAX INT_MAX +#define DUK_SMALL_UINT_MIN 0 +#define DUK_SMALL_UINT_MAX UINT_MAX + +/* Fast variants of small integers, again for really fast paths like the + * executor. + */ +typedef duk_int_fast16_t duk_small_int_fast_t; +typedef duk_uint_fast16_t duk_small_uint_fast_t; +#define DUK_SMALL_INT_FAST_MIN DUK_INT_FAST16_MIN +#define DUK_SMALL_INT_FAST_MAX DUK_INT_FAST16_MAX +#define DUK_SMALL_UINT_FAST_MIN DUK_UINT_FAST16_MIN +#define DUK_SMALL_UINT_FAST_MAX DUK_UINT_FAST16_MAX + +/* Boolean values are represented with the platform 'int'. */ +typedef duk_small_int_t duk_bool_t; +#define DUK_BOOL_MIN DUK_SMALL_INT_MIN +#define DUK_BOOL_MAX DUK_SMALL_INT_MAX + +/* Index values must have at least 32-bit signed range. */ +typedef duk_int_t duk_idx_t; +#define DUK_IDX_MIN DUK_INT_MIN +#define DUK_IDX_MAX DUK_INT_MAX + +/* Unsigned index variant. */ +typedef duk_uint_t duk_uidx_t; +#define DUK_UIDX_MIN DUK_UINT_MIN +#define DUK_UIDX_MAX DUK_UINT_MAX + +/* Array index values, could be exact 32 bits. + * Currently no need for signed duk_arridx_t. + */ +typedef duk_uint_t duk_uarridx_t; +#define DUK_UARRIDX_MIN DUK_UINT_MIN +#define DUK_UARRIDX_MAX DUK_UINT_MAX + +/* Duktape/C function return value, platform int is enough for now to + * represent 0, 1, or negative error code. Must be compatible with + * assigning truth values (e.g. duk_ret_t rc = (foo == bar);). + */ +typedef duk_small_int_t duk_ret_t; +#define DUK_RET_MIN DUK_SMALL_INT_MIN +#define DUK_RET_MAX DUK_SMALL_INT_MAX + +/* Error codes are represented with platform int. High bits are used + * for flags and such, so 32 bits are needed. + */ +typedef duk_int_t duk_errcode_t; +#define DUK_ERRCODE_MIN DUK_INT_MIN +#define DUK_ERRCODE_MAX DUK_INT_MAX + +/* Codepoint type. Must be 32 bits or more because it is used also for + * internal codepoints. The type is signed because negative codepoints + * are used as internal markers (e.g. to mark EOF or missing argument). + * (X)UTF-8/CESU-8 encode/decode take and return an unsigned variant to + * ensure duk_uint32_t casts back and forth nicely. Almost everything + * else uses the signed one. + */ +typedef duk_int_t duk_codepoint_t; +typedef duk_uint_t duk_ucodepoint_t; +#define DUK_CODEPOINT_MIN DUK_INT_MIN +#define DUK_CODEPOINT_MAX DUK_INT_MAX +#define DUK_UCODEPOINT_MIN DUK_UINT_MIN +#define DUK_UCODEPOINT_MAX DUK_UINT_MAX + +/* IEEE float/double typedef. */ +typedef float duk_float_t; +typedef double duk_double_t; + +/* We're generally assuming that we're working on a platform with a 32-bit + * address space. If DUK_SIZE_MAX is a typecast value (which is necessary + * if SIZE_MAX is missing), the check must be avoided because the + * preprocessor can't do a comparison. + */ +#if !defined(DUK_SIZE_MAX) +#error DUK_SIZE_MAX is undefined, probably missing SIZE_MAX +#elif !defined(DUK_SIZE_MAX_COMPUTED) +#if DUK_SIZE_MAX < 0xffffffffUL +/* On some systems SIZE_MAX can be smaller than max unsigned 32-bit value + * which seems incorrect if size_t is (at least) an unsigned 32-bit type. + * However, it doesn't seem useful to error out compilation if this is the + * case. + */ +#endif +#endif + +/* Type for public API calls. */ +typedef struct duk_hthread duk_context; + +/* Check whether we should use 64-bit integers or not. + * + * Quite incomplete now. Use 64-bit types if detected (C99 or other detection) + * unless they are known to be unreliable. For instance, 64-bit types are + * available on VBCC but seem to misbehave. + */ +#if defined(DUK_F_HAVE_64BIT) && !defined(DUK_F_VBCC) +#define DUK_USE_64BIT_OPS +#else +#undef DUK_USE_64BIT_OPS +#endif + +/* + * Fill-ins for platform, architecture, and compiler + */ + +#if !defined(DUK_SETJMP) +#define DUK_JMPBUF_TYPE jmp_buf +#define DUK_SETJMP(jb) setjmp((jb)) +#define DUK_LONGJMP(jb) longjmp((jb), 1) +#endif + +#if 0 +/* sigsetjmp() alternative */ +#define DUK_JMPBUF_TYPE sigjmp_buf +#define DUK_SETJMP(jb) sigsetjmp((jb)) +#define DUK_LONGJMP(jb) siglongjmp((jb), 1) +#endif + +typedef FILE duk_file; +#if !defined(DUK_STDIN) +#define DUK_STDIN stdin +#endif +#if !defined(DUK_STDOUT) +#define DUK_STDOUT stdout +#endif +#if !defined(DUK_STDERR) +#define DUK_STDERR stderr +#endif + +/* Special naming to avoid conflict with e.g. DUK_FREE() in duk_heap.h + * (which is unfortunately named). May sometimes need replacement, e.g. + * some compilers don't handle zero length or NULL correctly in realloc(). + */ +#if !defined(DUK_ANSI_MALLOC) +#define DUK_ANSI_MALLOC malloc +#endif +#if !defined(DUK_ANSI_REALLOC) +#define DUK_ANSI_REALLOC realloc +#endif +#if !defined(DUK_ANSI_CALLOC) +#define DUK_ANSI_CALLOC calloc +#endif +#if !defined(DUK_ANSI_FREE) +#define DUK_ANSI_FREE free +#endif + +/* ANSI C (various versions) and some implementations require that the + * pointer arguments to memset(), memcpy(), and memmove() be valid values + * even when byte size is 0 (even a NULL pointer is considered invalid in + * this context). Zero-size operations as such are allowed, as long as their + * pointer arguments point to a valid memory area. The DUK_MEMSET(), + * DUK_MEMCPY(), and DUK_MEMMOVE() macros require this same behavior, i.e.: + * (1) pointers must be valid and non-NULL, (2) zero size must otherwise be + * allowed. If these are not fulfilled, a macro wrapper is needed. + * + * http://stackoverflow.com/questions/5243012/is-it-guaranteed-to-be-safe-to-perform-memcpy0-0-0 + * http://lists.cs.uiuc.edu/pipermail/llvmdev/2007-October/011065.html + * + * Not sure what's the required behavior when a pointer points just past the + * end of a buffer, which often happens in practice (e.g. zero size memmoves). + * For example, if allocation size is 3, the following pointer would not + * technically point to a valid memory byte: + * + * <-- alloc --> + * | 0 | 1 | 2 | ..... + * ^-- p=3, points after last valid byte (2) + */ +#if !defined(DUK_MEMCPY) +#if defined(DUK_F_UCLIBC) +/* Old uclibcs have a broken memcpy so use memmove instead (this is overly wide + * now on purpose): http://lists.uclibc.org/pipermail/uclibc-cvs/2008-October/025511.html + */ +#define DUK_MEMCPY memmove +#else +#define DUK_MEMCPY memcpy +#endif +#endif +#if !defined(DUK_MEMMOVE) +#define DUK_MEMMOVE memmove +#endif +#if !defined(DUK_MEMCMP) +#define DUK_MEMCMP memcmp +#endif +#if !defined(DUK_MEMSET) +#define DUK_MEMSET memset +#endif +#if !defined(DUK_STRLEN) +#define DUK_STRLEN strlen +#endif +#if !defined(DUK_STRCMP) +#define DUK_STRCMP strcmp +#endif +#if !defined(DUK_STRNCMP) +#define DUK_STRNCMP strncmp +#endif +#if !defined(DUK_PRINTF) +#define DUK_PRINTF printf +#endif +#if !defined(DUK_FPRINTF) +#define DUK_FPRINTF fprintf +#endif +#if !defined(DUK_SPRINTF) +#define DUK_SPRINTF sprintf +#endif +#if !defined(DUK_SNPRINTF) +/* snprintf() is technically not part of C89 but usually available. */ +#define DUK_SNPRINTF snprintf +#endif +#if !defined(DUK_VSPRINTF) +#define DUK_VSPRINTF vsprintf +#endif +#if !defined(DUK_VSNPRINTF) +/* vsnprintf() is technically not part of C89 but usually available. */ +#define DUK_VSNPRINTF vsnprintf +#endif +#if !defined(DUK_SSCANF) +#define DUK_SSCANF sscanf +#endif +#if !defined(DUK_VSSCANF) +#define DUK_VSSCANF vsscanf +#endif +#if !defined(DUK_FOPEN) +#define DUK_FOPEN fopen +#endif +#if !defined(DUK_FCLOSE) +#define DUK_FCLOSE fclose +#endif +#if !defined(DUK_FREAD) +#define DUK_FREAD fread +#endif +#if !defined(DUK_FWRITE) +#define DUK_FWRITE fwrite +#endif +#if !defined(DUK_FSEEK) +#define DUK_FSEEK fseek +#endif +#if !defined(DUK_FTELL) +#define DUK_FTELL ftell +#endif +#if !defined(DUK_FFLUSH) +#define DUK_FFLUSH fflush +#endif +#if !defined(DUK_FPUTC) +#define DUK_FPUTC fputc +#endif +#if !defined(DUK_MEMZERO) +#define DUK_MEMZERO(p,n) DUK_MEMSET((p), 0, (n)) +#endif +#if !defined(DUK_ABORT) +#define DUK_ABORT abort +#endif +#if !defined(DUK_EXIT) +#define DUK_EXIT exit +#endif + +#if !defined(DUK_DOUBLE_2TO32) +#define DUK_DOUBLE_2TO32 4294967296.0 +#endif +#if !defined(DUK_DOUBLE_2TO31) +#define DUK_DOUBLE_2TO31 2147483648.0 +#endif + +#if !defined(DUK_DOUBLE_INFINITY) +#undef DUK_USE_COMPUTED_INFINITY +#if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION < 40600) +/* GCC older than 4.6: avoid overflow warnings related to using INFINITY */ +#define DUK_DOUBLE_INFINITY (__builtin_inf()) +#elif defined(INFINITY) +#define DUK_DOUBLE_INFINITY ((double) INFINITY) +#elif !defined(DUK_F_VBCC) && !defined(DUK_F_MSVC) && !defined(DUK_F_BCC) +#define DUK_DOUBLE_INFINITY (1.0 / 0.0) +#else +/* In VBCC (1.0 / 0.0) results in a warning and 0.0 instead of infinity. + * Use a computed infinity (initialized when a heap is created at the + * latest). + */ +#define DUK_USE_COMPUTED_INFINITY +#define DUK_DOUBLE_INFINITY duk_computed_infinity +#endif +#endif + +#if !defined(DUK_DOUBLE_NAN) +#undef DUK_USE_COMPUTED_NAN +#if defined(NAN) +#define DUK_DOUBLE_NAN NAN +#elif !defined(DUK_F_VBCC) && !defined(DUK_F_MSVC) && !defined(DUK_F_BCC) +#define DUK_DOUBLE_NAN (0.0 / 0.0) +#else +/* In VBCC (0.0 / 0.0) results in a warning and 0.0 instead of NaN. + * In MSVC (VS2010 Express) (0.0 / 0.0) results in a compile error. + * Use a computed NaN (initialized when a heap is created at the + * latest). + */ +#define DUK_USE_COMPUTED_NAN +#define DUK_DOUBLE_NAN duk_computed_nan +#endif +#endif + +/* Many platforms are missing fpclassify() and friends, so use replacements + * if necessary. The replacement constants (FP_NAN etc) can be anything but + * match Linux constants now. + */ +#undef DUK_USE_REPL_FPCLASSIFY +#undef DUK_USE_REPL_SIGNBIT +#undef DUK_USE_REPL_ISFINITE +#undef DUK_USE_REPL_ISNAN +#undef DUK_USE_REPL_ISINF + +/* Complex condition broken into separate parts. */ +#undef DUK_F_USE_REPL_ALL +#if !(defined(FP_NAN) && defined(FP_INFINITE) && defined(FP_ZERO) && \ + defined(FP_SUBNORMAL) && defined(FP_NORMAL)) +/* Missing some obvious constants. */ +#define DUK_F_USE_REPL_ALL +#elif defined(DUK_F_AMIGAOS) && defined(DUK_F_VBCC) +/* VBCC is missing the built-ins even in C99 mode (perhaps a header issue). */ +#define DUK_F_USE_REPL_ALL +#elif defined(DUK_F_AMIGAOS) && defined(DUK_F_M68K) +/* AmigaOS + M68K seems to have math issues even when using GCC cross + * compilation. Use replacements for all AmigaOS versions on M68K + * regardless of compiler. + */ +#define DUK_F_USE_REPL_ALL +#elif defined(DUK_F_FREEBSD) && defined(DUK_F_CLANG) +/* Placeholder fix for (detection is wider than necessary): + * http://llvm.org/bugs/show_bug.cgi?id=17788 + */ +#define DUK_F_USE_REPL_ALL +#elif defined(DUK_F_UCLIBC) +/* At least some uclibc versions have broken floating point math. For + * example, fpclassify() can incorrectly classify certain NaN formats. + * To be safe, use replacements. + */ +#define DUK_F_USE_REPL_ALL +#endif + +#if defined(DUK_F_USE_REPL_ALL) +#define DUK_USE_REPL_FPCLASSIFY +#define DUK_USE_REPL_SIGNBIT +#define DUK_USE_REPL_ISFINITE +#define DUK_USE_REPL_ISNAN +#define DUK_USE_REPL_ISINF +#define DUK_FPCLASSIFY duk_repl_fpclassify +#define DUK_SIGNBIT duk_repl_signbit +#define DUK_ISFINITE duk_repl_isfinite +#define DUK_ISNAN duk_repl_isnan +#define DUK_ISINF duk_repl_isinf +#define DUK_FP_NAN 0 +#define DUK_FP_INFINITE 1 +#define DUK_FP_ZERO 2 +#define DUK_FP_SUBNORMAL 3 +#define DUK_FP_NORMAL 4 +#else +#define DUK_FPCLASSIFY fpclassify +#define DUK_SIGNBIT signbit +#define DUK_ISFINITE isfinite +#define DUK_ISNAN isnan +#define DUK_ISINF isinf +#define DUK_FP_NAN FP_NAN +#define DUK_FP_INFINITE FP_INFINITE +#define DUK_FP_ZERO FP_ZERO +#define DUK_FP_SUBNORMAL FP_SUBNORMAL +#define DUK_FP_NORMAL FP_NORMAL +#endif + +#if defined(DUK_F_USE_REPL_ALL) +#undef DUK_F_USE_REPL_ALL +#endif + +/* Some math functions are C99 only. This is also an issue with some + * embedded environments using uclibc where uclibc has been configured + * not to provide some functions. For now, use replacements whenever + * using uclibc. + */ +#undef DUK_USE_MATH_FMIN +#undef DUK_USE_MATH_FMAX +#undef DUK_USE_MATH_ROUND +#if defined(DUK_F_UCLIBC) +/* uclibc may be missing these */ +#elif defined(DUK_F_AMIGAOS) && defined(DUK_F_VBCC) +/* vbcc + AmigaOS may be missing these */ +#elif defined(DUK_F_MINT) +/* mint clib is missing these */ +#elif !defined(DUK_F_C99) && !defined(DUK_F_CPP11) +/* build is not C99 or C++11, play it safe */ +#else +/* C99 or C++11, no known issues */ +#define DUK_USE_MATH_FMIN +#define DUK_USE_MATH_FMAX +#define DUK_USE_MATH_ROUND +#endif + +/* These functions don't currently need replacement but are wrapped for + * completeness. Because these are used as function pointers, they need + * to be defined as concrete C functions (not macros). + */ +#if !defined(DUK_FABS) +#define DUK_FABS fabs +#endif +#if !defined(DUK_FMIN) +#define DUK_FMIN fmin +#endif +#if !defined(DUK_FMAX) +#define DUK_FMAX fmax +#endif +#if !defined(DUK_FLOOR) +#define DUK_FLOOR floor +#endif +#if !defined(DUK_CEIL) +#define DUK_CEIL ceil +#endif +#if !defined(DUK_FMOD) +#define DUK_FMOD fmod +#endif +#if !defined(DUK_POW) +#define DUK_POW pow +#endif +#if !defined(DUK_ACOS) +#define DUK_ACOS acos +#endif +#if !defined(DUK_ASIN) +#define DUK_ASIN asin +#endif +#if !defined(DUK_ATAN) +#define DUK_ATAN atan +#endif +#if !defined(DUK_ATAN2) +#define DUK_ATAN2 atan2 +#endif +#if !defined(DUK_SIN) +#define DUK_SIN sin +#endif +#if !defined(DUK_COS) +#define DUK_COS cos +#endif +#if !defined(DUK_TAN) +#define DUK_TAN tan +#endif +#if !defined(DUK_EXP) +#define DUK_EXP exp +#endif +#if !defined(DUK_LOG) +#define DUK_LOG log +#endif +#if !defined(DUK_SQRT) +#define DUK_SQRT sqrt +#endif + +/* NetBSD 6.0 x86 (at least) has a few problems with pow() semantics, + * see test-bug-netbsd-math-pow.js. Use NetBSD specific workaround. + * (This might be a wider problem; if so, generalize the define name.) + */ +#undef DUK_USE_POW_NETBSD_WORKAROUND +#if defined(DUK_F_NETBSD) +#define DUK_USE_POW_NETBSD_WORKAROUND +#endif + +/* Rely as little as possible on compiler behavior for NaN comparison, + * signed zero handling, etc. Currently never activated but may be needed + * for broken compilers. + */ +#undef DUK_USE_PARANOID_MATH + +/* There was a curious bug where test-bi-date-canceling.js would fail e.g. + * on 64-bit Ubuntu, gcc-4.8.1, -m32, and no -std=c99. Some date computations + * using doubles would be optimized which then broke some corner case tests. + * The problem goes away by adding 'volatile' to the datetime computations. + * Not sure what the actual triggering conditions are, but using this on + * non-C99 systems solves the known issues and has relatively little cost + * on other platforms. + */ +#undef DUK_USE_PARANOID_DATE_COMPUTATION +#if !defined(DUK_F_C99) +#define DUK_USE_PARANOID_DATE_COMPUTATION +#endif + +/* + * Byte order and double memory layout detection + * + * Endianness detection is a major portability hassle because the macros + * and headers are not standardized. There's even variance across UNIX + * platforms. Even with "standard" headers, details like underscore count + * varies between platforms, e.g. both __BYTE_ORDER and _BYTE_ORDER are used + * (Crossbridge has a single underscore, for instance). + * + * The checks below are structured with this in mind: several approaches are + * used, and at the end we check if any of them worked. This allows generic + * approaches to be tried first, and platform/compiler specific hacks tried + * last. As a last resort, the user can force a specific endianness, as it's + * not likely that automatic detection will work on the most exotic platforms. + * + * Duktape supports little and big endian machines. There's also support + * for a hybrid used by some ARM machines where integers are little endian + * but IEEE double values use a mixed order (12345678 -> 43218765). This + * byte order for doubles is referred to as "mixed endian". + */ + +/* For custom platforms allow user to define byteorder explicitly. + * Since endianness headers are not standardized, this is a useful + * workaround for custom platforms for which endianness detection + * is not directly supported. Perhaps custom hardware is used and + * user cannot submit upstream patches. + */ +#if defined(DUK_OPT_FORCE_BYTEORDER) +#undef DUK_USE_BYTEORDER +#if (DUK_OPT_FORCE_BYTEORDER == 1) +#define DUK_USE_BYTEORDER 1 +#elif (DUK_OPT_FORCE_BYTEORDER == 2) +#define DUK_USE_BYTEORDER 2 +#elif (DUK_OPT_FORCE_BYTEORDER == 3) +#define DUK_USE_BYTEORDER 3 +#else +#error invalid DUK_OPT_FORCE_BYTEORDER value +#endif +#endif /* DUK_OPT_FORCE_BYTEORDER */ + +/* GCC and Clang provide endianness defines as built-in predefines, with + * leading and trailing double underscores (e.g. __BYTE_ORDER__). See + * output of "make gccpredefs" and "make clangpredefs". Clang doesn't + * seem to provide __FLOAT_WORD_ORDER__; assume not mixed endian for clang. + * http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html + */ +#if !defined(DUK_USE_BYTEORDER) && defined(__BYTE_ORDER__) +#if defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) +#if defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__) +#define DUK_USE_BYTEORDER 1 +#elif defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__) +#define DUK_USE_BYTEORDER 2 +#elif !defined(__FLOAT_WORD_ORDER__) +/* Float word order not known, assume not a hybrid. */ +#define DUK_USE_BYTEORDER 1 +#else +/* Byte order is little endian but cannot determine IEEE double word order. */ +#endif /* float word order */ +#elif defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#if defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__) +#define DUK_USE_BYTEORDER 3 +#elif !defined(__FLOAT_WORD_ORDER__) +/* Float word order not known, assume not a hybrid. */ +#define DUK_USE_BYTEORDER 3 +#else +/* Byte order is big endian but cannot determine IEEE double word order. */ +#endif /* float word order */ +#else +/* Cannot determine byte order; __ORDER_PDP_ENDIAN__ is related to 32-bit + * integer ordering and is not relevant. + */ +#endif /* integer byte order */ +#endif /* !defined(DUK_USE_BYTEORDER) && defined(__BYTE_ORDER__) */ + +/* More or less standard endianness predefines provided by header files. + * The ARM hybrid case is detected by assuming that __FLOAT_WORD_ORDER + * will be big endian, see: http://lists.mysql.com/internals/443. + * On some platforms some defines may be present with an empty value which + * causes comparisons to fail: https://github.com/svaarala/duktape/issues/453. + */ +#if !defined(DUK_USE_BYTEORDER) +#if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN) || \ + defined(_BYTE_ORDER) && defined(_LITTLE_ENDIAN) && (_BYTE_ORDER == _LITTLE_ENDIAN) || \ + defined(__LITTLE_ENDIAN__) +#if defined(__FLOAT_WORD_ORDER) && defined(__LITTLE_ENDIAN) && (__FLOAT_WORD_ORDER == __LITTLE_ENDIAN) || \ + defined(_FLOAT_WORD_ORDER) && defined(_LITTLE_ENDIAN) && (_FLOAT_WORD_ORDER == _LITTLE_ENDIAN) +#define DUK_USE_BYTEORDER 1 +#elif defined(__FLOAT_WORD_ORDER) && defined(__BIG_ENDIAN) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN) || \ + defined(_FLOAT_WORD_ORDER) && defined(_BIG_ENDIAN) && (_FLOAT_WORD_ORDER == _BIG_ENDIAN) +#define DUK_USE_BYTEORDER 2 +#elif !defined(__FLOAT_WORD_ORDER) && !defined(_FLOAT_WORD_ORDER) +/* Float word order not known, assume not a hybrid. */ +#define DUK_USE_BYTEORDER 1 +#else +/* Byte order is little endian but cannot determine IEEE double word order. */ +#endif /* float word order */ +#elif defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN) || \ + defined(_BYTE_ORDER) && defined(_BIG_ENDIAN) && (_BYTE_ORDER == _BIG_ENDIAN) || \ + defined(__BIG_ENDIAN__) +#if defined(__FLOAT_WORD_ORDER) && defined(__BIG_ENDIAN) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN) || \ + defined(_FLOAT_WORD_ORDER) && defined(_BIG_ENDIAN) && (_FLOAT_WORD_ORDER == _BIG_ENDIAN) +#define DUK_USE_BYTEORDER 3 +#elif !defined(__FLOAT_WORD_ORDER) && !defined(_FLOAT_WORD_ORDER) +/* Float word order not known, assume not a hybrid. */ +#define DUK_USE_BYTEORDER 3 +#else +/* Byte order is big endian but cannot determine IEEE double word order. */ +#endif /* float word order */ +#else +/* Cannot determine byte order. */ +#endif /* integer byte order */ +#endif /* !defined(DUK_USE_BYTEORDER) */ + +/* QNX gcc cross compiler seems to define e.g. __LITTLEENDIAN__ or __BIGENDIAN__: + * $ /opt/qnx650/host/linux/x86/usr/bin/i486-pc-nto-qnx6.5.0-gcc -dM -E - > 24) | \ + ((((duk_uint32_t) (x)) >> 8) & 0xff00UL) | \ + ((((duk_uint32_t) (x)) << 8) & 0xff0000UL) | \ + (((duk_uint32_t) (x)) << 24)) +#endif +#if !defined(DUK_BSWAP16) +#define DUK_BSWAP16(x) \ + ((duk_uint16_t) (x) >> 8) | \ + ((duk_uint16_t) (x) << 8) +#endif + +/* DUK_USE_VARIADIC_MACROS: required from compilers, so no fill-in. */ +/* DUK_USE_UNION_INITIALIZERS: required from compilers, so no fill-in. */ + +#if !(defined(DUK_USE_FLEX_C99) || defined(DUK_USE_FLEX_ZEROSIZE) || defined(DUK_USE_FLEX_ONESIZE)) +#if defined(DUK_F_C99) +#define DUK_USE_FLEX_C99 +#else +#define DUK_USE_FLEX_ZEROSIZE /* Not standard but common enough */ +#endif +#endif + +#if !(defined(DUK_USE_PACK_GCC_ATTR) || defined(DUK_USE_PACK_CLANG_ATTR) || \ + defined(DUK_USE_PACK_MSVC_PRAGMA) || defined(DUK_USE_PACK_DUMMY_MEMBER)) +#define DUK_USE_PACK_DUMMY_MEMBER +#endif + +#if 0 /* not defined by default */ +#undef DUK_USE_GCC_PRAGMAS +#endif + +/* Workaround for GH-323: avoid inlining control when compiling from + * multiple sources, as it causes compiler portability trouble. + */ +#if !defined(DUK_SINGLE_FILE) +#undef DUK_NOINLINE +#undef DUK_INLINE +#undef DUK_ALWAYS_INLINE +#define DUK_NOINLINE /*nop*/ +#define DUK_INLINE /*nop*/ +#define DUK_ALWAYS_INLINE /*nop*/ +#endif + +/* + * Check whether or not a packed duk_tval representation is possible. + * What's basically required is that pointers are 32-bit values + * (sizeof(void *) == 4). Best effort check, not always accurate. + * If guess goes wrong, crashes may result; self tests also verify + * the guess. + */ + +/* Explicit marker needed; may be 'defined', 'undefined, 'or 'not provided'. */ +#if !defined(DUK_F_PACKED_TVAL_PROVIDED) +#undef DUK_F_PACKED_TVAL_POSSIBLE + +/* Strict C99 case: DUK_UINTPTR_MAX (= UINTPTR_MAX) should be very reliable */ +#if !defined(DUK_F_PACKED_TVAL_POSSIBLE) && defined(DUK_UINTPTR_MAX) +#if (DUK_UINTPTR_MAX <= 0xffffffffUL) +#define DUK_F_PACKED_TVAL_POSSIBLE +#endif +#endif + +/* Non-C99 case, still relying on DUK_UINTPTR_MAX, as long as it is not a computed value */ +#if !defined(DUK_F_PACKED_TVAL_POSSIBLE) && defined(DUK_UINTPTR_MAX) && !defined(DUK_UINTPTR_MAX_COMPUTED) +#if (DUK_UINTPTR_MAX <= 0xffffffffUL) +#define DUK_F_PACKED_TVAL_POSSIBLE +#endif +#endif + +/* DUK_SIZE_MAX (= SIZE_MAX) is often reliable */ +#if !defined(DUK_F_PACKED_TVAL_POSSIBLE) && defined(DUK_SIZE_MAX) && !defined(DUK_SIZE_MAX_COMPUTED) +#if (DUK_SIZE_MAX <= 0xffffffffUL) +#define DUK_F_PACKED_TVAL_POSSIBLE +#endif +#endif + +#undef DUK_USE_PACKED_TVAL +#if defined(DUK_F_PACKED_TVAL_POSSIBLE) +#define DUK_USE_PACKED_TVAL +#endif + +#undef DUK_F_PACKED_TVAL_POSSIBLE +#endif /* DUK_F_PACKED_TVAL_PROVIDED */ + +/* Feature option forcing. */ +#if defined(DUK_OPT_NO_PACKED_TVAL) +#undef DUK_USE_PACKED_TVAL +#elif defined(DUK_OPT_PACKED_TVAL) +#undef DUK_USE_PACKED_TVAL +#define DUK_USE_PACKED_TVAL +#endif +/* Object property allocation layout has implications for memory and code + * footprint and generated code size/speed. The best layout also depends + * on whether the platform has alignment requirements or benefits from + * having mostly aligned accesses. + */ +#undef DUK_USE_HOBJECT_LAYOUT_1 +#undef DUK_USE_HOBJECT_LAYOUT_2 +#undef DUK_USE_HOBJECT_LAYOUT_3 +#if (DUK_USE_ALIGN_BY == 1) +/* On platforms without any alignment issues, layout 1 is preferable + * because it compiles to slightly less code and provides direct access + * to property keys. + */ +#define DUK_USE_HOBJECT_LAYOUT_1 +#else +/* On other platforms use layout 2, which requires some padding but + * is a bit more natural than layout 3 in ordering the entries. Layout + * 3 is currently not used. + */ +#define DUK_USE_HOBJECT_LAYOUT_2 +#endif + +/* GCC/clang inaccurate math would break compliance and probably duk_tval, + * so refuse to compile. Relax this if -ffast-math is tested to work. + */ +#if defined(__FAST_MATH__) +#error __FAST_MATH__ defined, refusing to compile +#endif + +/* + * Autogenerated defaults + */ + +#undef DUK_USE_ASSERTIONS +#define DUK_USE_AUGMENT_ERROR_CREATE +#define DUK_USE_AUGMENT_ERROR_THROW +#define DUK_USE_AVOID_PLATFORM_FUNCPTRS +#define DUK_USE_BASE64_FASTPATH +#define DUK_USE_BROWSER_LIKE +#define DUK_USE_BUFFEROBJECT_SUPPORT +#undef DUK_USE_BUFLEN16 +#define DUK_USE_BUILTIN_INITJS +#define DUK_USE_BYTECODE_DUMP_SUPPORT +#define DUK_USE_COMMONJS_MODULES +#define DUK_USE_COMPILER_RECLIMIT 2500 +#undef DUK_USE_CPP_EXCEPTIONS +#undef DUK_USE_DATAPTR16 +#undef DUK_USE_DATAPTR_DEC16 +#undef DUK_USE_DATAPTR_ENC16 +#undef DUK_USE_DATE_FORMAT_STRING +#undef DUK_USE_DATE_GET_LOCAL_TZOFFSET +#undef DUK_USE_DATE_GET_NOW +#undef DUK_USE_DATE_PARSE_STRING +#undef DUK_USE_DATE_PRS_GETDATE +#undef DUK_USE_DDDPRINT +#undef DUK_USE_DDPRINT +#undef DUK_USE_DEBUG +#undef DUK_USE_DEBUGGER_DUMPHEAP +#undef DUK_USE_DEBUGGER_FWD_LOGGING +#undef DUK_USE_DEBUGGER_FWD_PRINTALERT +#undef DUK_USE_DEBUGGER_INSPECT +#undef DUK_USE_DEBUGGER_PAUSE_UNCAUGHT +#undef DUK_USE_DEBUGGER_SUPPORT +#define DUK_USE_DEBUGGER_THROW_NOTIFY +#undef DUK_USE_DEBUGGER_TRANSPORT_TORTURE +#define DUK_USE_DEBUG_BUFSIZE 65536L +#define DUK_USE_DOUBLE_LINKED_HEAP +#undef DUK_USE_DPRINT +#undef DUK_USE_DPRINT_COLORS +#undef DUK_USE_DPRINT_RDTSC +#define DUK_USE_ERRCREATE +#define DUK_USE_ERRTHROW +#define DUK_USE_ES6_OBJECT_PROTO_PROPERTY +#define DUK_USE_ES6_OBJECT_SETPROTOTYPEOF +#define DUK_USE_ES6_PROXY +#define DUK_USE_ES6_REGEXP_BRACES +#define DUK_USE_ESBC_LIMITS +#define DUK_USE_ESBC_MAX_BYTES 2147418112L +#define DUK_USE_ESBC_MAX_LINENUMBER 2147418112L +#undef DUK_USE_EXEC_FUN_LOCAL +#undef DUK_USE_EXEC_INDIRECT_BOUND_CHECK +#undef DUK_USE_EXEC_TIMEOUT_CHECK +#undef DUK_USE_EXPLICIT_NULL_INIT +#undef DUK_USE_EXTSTR_FREE +#undef DUK_USE_EXTSTR_INTERN_CHECK +#undef DUK_USE_FASTINT +#define DUK_USE_FAST_REFCOUNT_DEFAULT +#define DUK_USE_FILE_IO +#undef DUK_USE_FUNCPTR16 +#undef DUK_USE_FUNCPTR_DEC16 +#undef DUK_USE_FUNCPTR_ENC16 +#undef DUK_USE_GC_TORTURE +#undef DUK_USE_HEAPPTR16 +#undef DUK_USE_HEAPPTR_DEC16 +#undef DUK_USE_HEAPPTR_ENC16 +#define DUK_USE_HEX_FASTPATH +#define DUK_USE_HOBJECT_HASH_PART +#define DUK_USE_HSTRING_CLEN +#undef DUK_USE_HSTRING_EXTDATA +#define DUK_USE_IDCHAR_FASTPATH +#undef DUK_USE_INTERRUPT_COUNTER +#undef DUK_USE_INTERRUPT_DEBUG_FIXUP +#define DUK_USE_JC +#define DUK_USE_JSON_DECNUMBER_FASTPATH +#define DUK_USE_JSON_DECSTRING_FASTPATH +#define DUK_USE_JSON_DEC_RECLIMIT 1000 +#define DUK_USE_JSON_EATWHITE_FASTPATH +#define DUK_USE_JSON_ENC_RECLIMIT 1000 +#define DUK_USE_JSON_QUOTESTRING_FASTPATH +#undef DUK_USE_JSON_STRINGIFY_FASTPATH +#define DUK_USE_JX +#define DUK_USE_LEXER_SLIDING_WINDOW +#undef DUK_USE_LIGHTFUNC_BUILTINS +#undef DUK_USE_MARKANDSWEEP_FINALIZER_TORTURE +#define DUK_USE_MARK_AND_SWEEP +#define DUK_USE_MARK_AND_SWEEP_RECLIMIT 256 +#define DUK_USE_MATH_BUILTIN +#define DUK_USE_MS_STRINGTABLE_RESIZE +#define DUK_USE_NATIVE_CALL_RECLIMIT 1000 +#define DUK_USE_NONSTD_ARRAY_CONCAT_TRAILER +#define DUK_USE_NONSTD_ARRAY_MAP_TRAILER +#define DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT +#undef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY +#undef DUK_USE_NONSTD_FUNC_SOURCE_PROPERTY +#define DUK_USE_NONSTD_FUNC_STMT +#define DUK_USE_NONSTD_GETTER_KEY_ARGUMENT +#define DUK_USE_NONSTD_JSON_ESC_U2028_U2029 +#define DUK_USE_NONSTD_REGEXP_DOLLAR_ESCAPE +#define DUK_USE_NONSTD_SETTER_KEY_ARGUMENT +#define DUK_USE_NONSTD_STRING_FROMCHARCODE_32BIT +#undef DUK_USE_OBJSIZES16 +#define DUK_USE_OCTAL_SUPPORT +#define DUK_USE_PANIC_ABORT +#undef DUK_USE_PANIC_EXIT +#undef DUK_USE_PANIC_HANDLER +#undef DUK_USE_PANIC_SEGFAULT +#undef DUK_USE_PARANOID_ERRORS +#define DUK_USE_PC2LINE +#undef DUK_USE_PREFER_SIZE +#define DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS +#undef DUK_USE_REFCOUNT16 +#define DUK_USE_REFERENCE_COUNTING +#undef DUK_USE_REFZERO_FINALIZER_TORTURE +#undef DUK_USE_REGEXP_CANON_WORKAROUND +#define DUK_USE_REGEXP_COMPILER_RECLIMIT 10000 +#define DUK_USE_REGEXP_EXECUTOR_RECLIMIT 10000 +#define DUK_USE_REGEXP_SUPPORT +#undef DUK_USE_ROM_GLOBAL_CLONE +#undef DUK_USE_ROM_GLOBAL_INHERIT +#undef DUK_USE_ROM_OBJECTS +#define DUK_USE_ROM_PTRCOMP_FIRST 63488L +#undef DUK_USE_ROM_STRINGS +#define DUK_USE_SECTION_B +#undef DUK_USE_SELF_TESTS +#undef DUK_USE_SHUFFLE_TORTURE +#define DUK_USE_SOURCE_NONBMP +#undef DUK_USE_STRHASH16 +#undef DUK_USE_STRHASH_DENSE +#define DUK_USE_STRHASH_SKIP_SHIFT 5 +#define DUK_USE_STRICT_DECL +#undef DUK_USE_STRICT_UTF8_SOURCE +#undef DUK_USE_STRLEN16 +#undef DUK_USE_STRTAB_CHAIN +#undef DUK_USE_STRTAB_CHAIN_SIZE +#define DUK_USE_STRTAB_PROBE +#define DUK_USE_TAILCALL +#define DUK_USE_TARGET_INFO "unknown" +#define DUK_USE_TRACEBACKS +#define DUK_USE_TRACEBACK_DEPTH 10 +#define DUK_USE_USER_DECLARE() /* no user declarations */ +#undef DUK_USE_USER_INITJS +#undef DUK_USE_VALSTACK_UNSAFE +#define DUK_USE_VERBOSE_ERRORS +#define DUK_USE_VERBOSE_EXECUTOR_ERRORS +#define DUK_USE_VOLUNTARY_GC +#define DUK_USE_ZERO_BUFFER_DATA + +/* + * Alternative customization header + * + * If you want to modify the final DUK_USE_xxx flags directly (without + * using the available DUK_OPT_xxx flags), define DUK_OPT_HAVE_CUSTOM_H + * and tweak the final flags there. + */ + +#if defined(DUK_OPT_HAVE_CUSTOM_H) +#include "duk_custom.h" +#endif + +/* + * You may add overriding #define/#undef directives below for + * customization. You of course cannot un-#include or un-typedef + * anything; these require direct changes above. + */ + +/* __OVERRIDE_DEFINES__ */ + +/* + * Date provider selection + * + * User may define DUK_USE_DATE_GET_NOW() etc directly, in which case we'll + * rely on an external provider. If this is not done, revert to previous + * behavior and use Unix/Windows built-in provider. + */ + +#if defined(DUK_COMPILING_DUKTAPE) + +#if defined(DUK_USE_DATE_GET_NOW) +/* External provider already defined. */ +#elif defined(DUK_USE_DATE_NOW_GETTIMEOFDAY) +#define DUK_USE_DATE_GET_NOW(ctx) duk_bi_date_get_now_gettimeofday((ctx)) +#elif defined(DUK_USE_DATE_NOW_TIME) +#define DUK_USE_DATE_GET_NOW(ctx) duk_bi_date_get_now_time((ctx)) +#elif defined(DUK_USE_DATE_NOW_WINDOWS) +#define DUK_USE_DATE_GET_NOW(ctx) duk_bi_date_get_now_windows((ctx)) +#else +#error no provider for DUK_USE_DATE_GET_NOW() +#endif + +#if defined(DUK_USE_DATE_GET_LOCAL_TZOFFSET) +/* External provider already defined. */ +#elif defined(DUK_USE_DATE_TZO_GMTIME_R) || defined(DUK_USE_DATE_TZO_GMTIME) +#define DUK_USE_DATE_GET_LOCAL_TZOFFSET(d) duk_bi_date_get_local_tzoffset_gmtime((d)) +#elif defined(DUK_USE_DATE_TZO_WINDOWS) +#define DUK_USE_DATE_GET_LOCAL_TZOFFSET(d) duk_bi_date_get_local_tzoffset_windows((d)) +#else +#error no provider for DUK_USE_DATE_GET_LOCAL_TZOFFSET() +#endif + +#if defined(DUK_USE_DATE_PARSE_STRING) +/* External provider already defined. */ +#elif defined(DUK_USE_DATE_PRS_STRPTIME) +#define DUK_USE_DATE_PARSE_STRING(ctx,str) duk_bi_date_parse_string_strptime((ctx), (str)) +#elif defined(DUK_USE_DATE_PRS_GETDATE) +#define DUK_USE_DATE_PARSE_STRING(ctx,str) duk_bi_date_parse_string_getdate((ctx), (str)) +#else +/* No provider for DUK_USE_DATE_PARSE_STRING(), fall back to ISO 8601 only. */ +#endif + +#if defined(DUK_USE_DATE_FORMAT_STRING) +/* External provider already defined. */ +#elif defined(DUK_USE_DATE_FMT_STRFTIME) +#define DUK_USE_DATE_FORMAT_STRING(ctx,parts,tzoffset,flags) \ + duk_bi_date_format_parts_strftime((ctx), (parts), (tzoffset), (flags)) +#else +/* No provider for DUK_USE_DATE_FORMAT_STRING(), fall back to ISO 8601 only. */ +#endif + +#endif /* DUK_COMPILING_DUKTAPE */ + +/* + * Checks for legacy feature options (DUK_OPT_xxx) + */ + +#if defined(DUK_OPT_ASSERTIONS) +#error unsupported legacy feature option DUK_OPT_ASSERTIONS used, consider options: DUK_USE_ASSERTIONS +#endif +#if defined(DUK_OPT_BUFFEROBJECT_SUPPORT) +#error unsupported legacy feature option DUK_OPT_BUFFEROBJECT_SUPPORT used +#endif +#if defined(DUK_OPT_BUFLEN16) +#error unsupported legacy feature option DUK_OPT_BUFLEN16 used +#endif +#if defined(DUK_OPT_DATAPTR16) +#error unsupported legacy feature option DUK_OPT_DATAPTR16 used +#endif +#if defined(DUK_OPT_DATAPTR_DEC16) +#error unsupported legacy feature option DUK_OPT_DATAPTR_DEC16 used +#endif +#if defined(DUK_OPT_DATAPTR_ENC16) +#error unsupported legacy feature option DUK_OPT_DATAPTR_ENC16 used +#endif +#if defined(DUK_OPT_DDDPRINT) +#error unsupported legacy feature option DUK_OPT_DDDPRINT used +#endif +#if defined(DUK_OPT_DDPRINT) +#error unsupported legacy feature option DUK_OPT_DDPRINT used +#endif +#if defined(DUK_OPT_DEBUG) +#error unsupported legacy feature option DUK_OPT_DEBUG used +#endif +#if defined(DUK_OPT_DEBUGGER_DUMPHEAP) +#error unsupported legacy feature option DUK_OPT_DEBUGGER_DUMPHEAP used +#endif +#if defined(DUK_OPT_DEBUGGER_FWD_LOGGING) +#error unsupported legacy feature option DUK_OPT_DEBUGGER_FWD_LOGGING used +#endif +#if defined(DUK_OPT_DEBUGGER_FWD_PRINTALERT) +#error unsupported legacy feature option DUK_OPT_DEBUGGER_FWD_PRINTALERT used +#endif +#if defined(DUK_OPT_DEBUGGER_SUPPORT) +#error unsupported legacy feature option DUK_OPT_DEBUGGER_SUPPORT used +#endif +#if defined(DUK_OPT_DEBUGGER_TRANSPORT_TORTURE) +#error unsupported legacy feature option DUK_OPT_DEBUGGER_TRANSPORT_TORTURE used +#endif +#if defined(DUK_OPT_DEBUG_BUFSIZE) +#error unsupported legacy feature option DUK_OPT_DEBUG_BUFSIZE used +#endif +#if defined(DUK_OPT_DECLARE) +#error unsupported legacy feature option DUK_OPT_DECLARE used +#endif +#if defined(DUK_OPT_DEEP_C_STACK) +#error unsupported legacy feature option DUK_OPT_DEEP_C_STACK used +#endif +#if defined(DUK_OPT_DLL_BUILD) +#error unsupported legacy feature option DUK_OPT_DLL_BUILD used +#endif +#if defined(DUK_OPT_DPRINT) +#error unsupported legacy feature option DUK_OPT_DPRINT used +#endif +#if defined(DUK_OPT_DPRINT_COLORS) +#error unsupported legacy feature option DUK_OPT_DPRINT_COLORS used +#endif +#if defined(DUK_OPT_DPRINT_RDTSC) +#error unsupported legacy feature option DUK_OPT_DPRINT_RDTSC used +#endif +#if defined(DUK_OPT_EXEC_TIMEOUT_CHECK) +#error unsupported legacy feature option DUK_OPT_EXEC_TIMEOUT_CHECK used +#endif +#if defined(DUK_OPT_EXTERNAL_STRINGS) +#error unsupported legacy feature option DUK_OPT_EXTERNAL_STRINGS used, consider options: DUK_USE_HSTRING_EXTDATA +#endif +#if defined(DUK_OPT_EXTSTR_FREE) +#error unsupported legacy feature option DUK_OPT_EXTSTR_FREE used +#endif +#if defined(DUK_OPT_EXTSTR_INTERN_CHECK) +#error unsupported legacy feature option DUK_OPT_EXTSTR_INTERN_CHECK used +#endif +#if defined(DUK_OPT_FASTINT) +#error unsupported legacy feature option DUK_OPT_FASTINT used, consider options: DUK_USE_FASTINT +#endif +#if defined(DUK_OPT_FORCE_ALIGN) +#error unsupported legacy feature option DUK_OPT_FORCE_ALIGN used +#endif +#if defined(DUK_OPT_FORCE_BYTEORDER) +#error unsupported legacy feature option DUK_OPT_FORCE_BYTEORDER used +#endif +#if defined(DUK_OPT_FUNCPTR16) +#error unsupported legacy feature option DUK_OPT_FUNCPTR16 used +#endif +#if defined(DUK_OPT_FUNCPTR_DEC16) +#error unsupported legacy feature option DUK_OPT_FUNCPTR_DEC16 used +#endif +#if defined(DUK_OPT_FUNCPTR_ENC16) +#error unsupported legacy feature option DUK_OPT_FUNCPTR_ENC16 used +#endif +#if defined(DUK_OPT_FUNC_NONSTD_CALLER_PROPERTY) +#error unsupported legacy feature option DUK_OPT_FUNC_NONSTD_CALLER_PROPERTY used +#endif +#if defined(DUK_OPT_FUNC_NONSTD_SOURCE_PROPERTY) +#error unsupported legacy feature option DUK_OPT_FUNC_NONSTD_SOURCE_PROPERTY used +#endif +#if defined(DUK_OPT_GC_TORTURE) +#error unsupported legacy feature option DUK_OPT_GC_TORTURE used +#endif +#if defined(DUK_OPT_HAVE_CUSTOM_H) +#error unsupported legacy feature option DUK_OPT_HAVE_CUSTOM_H used +#endif +#if defined(DUK_OPT_HEAPPTR16) +#error unsupported legacy feature option DUK_OPT_HEAPPTR16 used +#endif +#if defined(DUK_OPT_HEAPPTR_DEC16) +#error unsupported legacy feature option DUK_OPT_HEAPPTR_DEC16 used +#endif +#if defined(DUK_OPT_HEAPPTR_ENC16) +#error unsupported legacy feature option DUK_OPT_HEAPPTR_ENC16 used +#endif +#if defined(DUK_OPT_INTERRUPT_COUNTER) +#error unsupported legacy feature option DUK_OPT_INTERRUPT_COUNTER used +#endif +#if defined(DUK_OPT_JSON_STRINGIFY_FASTPATH) +#error unsupported legacy feature option DUK_OPT_JSON_STRINGIFY_FASTPATH used +#endif +#if defined(DUK_OPT_LIGHTFUNC_BUILTINS) +#error unsupported legacy feature option DUK_OPT_LIGHTFUNC_BUILTINS used +#endif +#if defined(DUK_OPT_NONSTD_FUNC_CALLER_PROPERTY) +#error unsupported legacy feature option DUK_OPT_NONSTD_FUNC_CALLER_PROPERTY used +#endif +#if defined(DUK_OPT_NONSTD_FUNC_SOURCE_PROPERTY) +#error unsupported legacy feature option DUK_OPT_NONSTD_FUNC_SOURCE_PROPERTY used +#endif +#if defined(DUK_OPT_NO_ARRAY_SPLICE_NONSTD_DELCOUNT) +#error unsupported legacy feature option DUK_OPT_NO_ARRAY_SPLICE_NONSTD_DELCOUNT used +#endif +#if defined(DUK_OPT_NO_AUGMENT_ERRORS) +#error unsupported legacy feature option DUK_OPT_NO_AUGMENT_ERRORS used +#endif +#if defined(DUK_OPT_NO_BROWSER_LIKE) +#error unsupported legacy feature option DUK_OPT_NO_BROWSER_LIKE used +#endif +#if defined(DUK_OPT_NO_BUFFEROBJECT_SUPPORT) +#error unsupported legacy feature option DUK_OPT_NO_BUFFEROBJECT_SUPPORT used +#endif +#if defined(DUK_OPT_NO_BYTECODE_DUMP_SUPPORT) +#error unsupported legacy feature option DUK_OPT_NO_BYTECODE_DUMP_SUPPORT used, consider options: DUK_USE_BYTECODE_DUMP_SUPPORT +#endif +#if defined(DUK_OPT_NO_COMMONJS_MODULES) +#error unsupported legacy feature option DUK_OPT_NO_COMMONJS_MODULES used +#endif +#if defined(DUK_OPT_NO_ES6_OBJECT_PROTO_PROPERTY) +#error unsupported legacy feature option DUK_OPT_NO_ES6_OBJECT_PROTO_PROPERTY used +#endif +#if defined(DUK_OPT_NO_ES6_OBJECT_SETPROTOTYPEOF) +#error unsupported legacy feature option DUK_OPT_NO_ES6_OBJECT_SETPROTOTYPEOF used +#endif +#if defined(DUK_OPT_NO_ES6_PROXY) +#error unsupported legacy feature option DUK_OPT_NO_ES6_PROXY used +#endif +#if defined(DUK_OPT_NO_FILE_IO) +#error unsupported legacy feature option DUK_OPT_NO_FILE_IO used, consider options: DUK_USE_FILE_IO +#endif +#if defined(DUK_OPT_NO_FUNC_STMT) +#error unsupported legacy feature option DUK_OPT_NO_FUNC_STMT used +#endif +#if defined(DUK_OPT_NO_JC) +#error unsupported legacy feature option DUK_OPT_NO_JC used +#endif +#if defined(DUK_OPT_NO_JSONC) +#error unsupported legacy feature option DUK_OPT_NO_JSONC used +#endif +#if defined(DUK_OPT_NO_JSONX) +#error unsupported legacy feature option DUK_OPT_NO_JSONX used +#endif +#if defined(DUK_OPT_NO_JX) +#error unsupported legacy feature option DUK_OPT_NO_JX used +#endif +#if defined(DUK_OPT_NO_MARK_AND_SWEEP) +#error unsupported legacy feature option DUK_OPT_NO_MARK_AND_SWEEP used +#endif +#if defined(DUK_OPT_NO_MS_STRINGTABLE_RESIZE) +#error unsupported legacy feature option DUK_OPT_NO_MS_STRINGTABLE_RESIZE used +#endif +#if defined(DUK_OPT_NO_NONSTD_ACCESSOR_KEY_ARGUMENT) +#error unsupported legacy feature option DUK_OPT_NO_NONSTD_ACCESSOR_KEY_ARGUMENT used +#endif +#if defined(DUK_OPT_NO_NONSTD_ARRAY_CONCAT_TRAILER) +#error unsupported legacy feature option DUK_OPT_NO_NONSTD_ARRAY_CONCAT_TRAILER used +#endif +#if defined(DUK_OPT_NO_NONSTD_ARRAY_MAP_TRAILER) +#error unsupported legacy feature option DUK_OPT_NO_NONSTD_ARRAY_MAP_TRAILER used +#endif +#if defined(DUK_OPT_NO_NONSTD_ARRAY_SPLICE_DELCOUNT) +#error unsupported legacy feature option DUK_OPT_NO_NONSTD_ARRAY_SPLICE_DELCOUNT used +#endif +#if defined(DUK_OPT_NO_NONSTD_FUNC_STMT) +#error unsupported legacy feature option DUK_OPT_NO_NONSTD_FUNC_STMT used +#endif +#if defined(DUK_OPT_NO_NONSTD_JSON_ESC_U2028_U2029) +#error unsupported legacy feature option DUK_OPT_NO_NONSTD_JSON_ESC_U2028_U2029 used +#endif +#if defined(DUK_OPT_NO_NONSTD_STRING_FROMCHARCODE_32BIT) +#error unsupported legacy feature option DUK_OPT_NO_NONSTD_STRING_FROMCHARCODE_32BIT used +#endif +#if defined(DUK_OPT_NO_OBJECT_ES6_PROTO_PROPERTY) +#error unsupported legacy feature option DUK_OPT_NO_OBJECT_ES6_PROTO_PROPERTY used +#endif +#if defined(DUK_OPT_NO_OBJECT_ES6_SETPROTOTYPEOF) +#error unsupported legacy feature option DUK_OPT_NO_OBJECT_ES6_SETPROTOTYPEOF used +#endif +#if defined(DUK_OPT_NO_OCTAL_SUPPORT) +#error unsupported legacy feature option DUK_OPT_NO_OCTAL_SUPPORT used +#endif +#if defined(DUK_OPT_NO_PACKED_TVAL) +#error unsupported legacy feature option DUK_OPT_NO_PACKED_TVAL used +#endif +#if defined(DUK_OPT_NO_PC2LINE) +#error unsupported legacy feature option DUK_OPT_NO_PC2LINE used +#endif +#if defined(DUK_OPT_NO_REFERENCE_COUNTING) +#error unsupported legacy feature option DUK_OPT_NO_REFERENCE_COUNTING used, consider options: DUK_USE_REFERENCE_COUNTING +#endif +#if defined(DUK_OPT_NO_REGEXP_SUPPORT) +#error unsupported legacy feature option DUK_OPT_NO_REGEXP_SUPPORT used +#endif +#if defined(DUK_OPT_NO_SECTION_B) +#error unsupported legacy feature option DUK_OPT_NO_SECTION_B used +#endif +#if defined(DUK_OPT_NO_SOURCE_NONBMP) +#error unsupported legacy feature option DUK_OPT_NO_SOURCE_NONBMP used +#endif +#if defined(DUK_OPT_NO_STRICT_DECL) +#error unsupported legacy feature option DUK_OPT_NO_STRICT_DECL used +#endif +#if defined(DUK_OPT_NO_TRACEBACKS) +#error unsupported legacy feature option DUK_OPT_NO_TRACEBACKS used +#endif +#if defined(DUK_OPT_NO_VERBOSE_ERRORS) +#error unsupported legacy feature option DUK_OPT_NO_VERBOSE_ERRORS used +#endif +#if defined(DUK_OPT_NO_VOLUNTARY_GC) +#error unsupported legacy feature option DUK_OPT_NO_VOLUNTARY_GC used +#endif +#if defined(DUK_OPT_NO_ZERO_BUFFER_DATA) +#error unsupported legacy feature option DUK_OPT_NO_ZERO_BUFFER_DATA used +#endif +#if defined(DUK_OPT_OBJSIZES16) +#error unsupported legacy feature option DUK_OPT_OBJSIZES16 used +#endif +#if defined(DUK_OPT_PANIC_HANDLER) +#error unsupported legacy feature option DUK_OPT_PANIC_HANDLER used +#endif +#if defined(DUK_OPT_REFCOUNT16) +#error unsupported legacy feature option DUK_OPT_REFCOUNT16 used +#endif +#if defined(DUK_OPT_SEGFAULT_ON_PANIC) +#error unsupported legacy feature option DUK_OPT_SEGFAULT_ON_PANIC used +#endif +#if defined(DUK_OPT_SELF_TESTS) +#error unsupported legacy feature option DUK_OPT_SELF_TESTS used +#endif +#if defined(DUK_OPT_SETJMP) +#error unsupported legacy feature option DUK_OPT_SETJMP used +#endif +#if defined(DUK_OPT_SHUFFLE_TORTURE) +#error unsupported legacy feature option DUK_OPT_SHUFFLE_TORTURE used +#endif +#if defined(DUK_OPT_SIGSETJMP) +#error unsupported legacy feature option DUK_OPT_SIGSETJMP used +#endif +#if defined(DUK_OPT_STRHASH16) +#error unsupported legacy feature option DUK_OPT_STRHASH16 used +#endif +#if defined(DUK_OPT_STRICT_UTF8_SOURCE) +#error unsupported legacy feature option DUK_OPT_STRICT_UTF8_SOURCE used +#endif +#if defined(DUK_OPT_STRLEN16) +#error unsupported legacy feature option DUK_OPT_STRLEN16 used +#endif +#if defined(DUK_OPT_STRTAB_CHAIN) +#error unsupported legacy feature option DUK_OPT_STRTAB_CHAIN used +#endif +#if defined(DUK_OPT_STRTAB_CHAIN_SIZE) +#error unsupported legacy feature option DUK_OPT_STRTAB_CHAIN_SIZE used +#endif +#if defined(DUK_OPT_TARGET_INFO) +#error unsupported legacy feature option DUK_OPT_TARGET_INFO used +#endif +#if defined(DUK_OPT_TRACEBACK_DEPTH) +#error unsupported legacy feature option DUK_OPT_TRACEBACK_DEPTH used +#endif +#if defined(DUK_OPT_UNDERSCORE_SETJMP) +#error unsupported legacy feature option DUK_OPT_UNDERSCORE_SETJMP used +#endif +#if defined(DUK_OPT_USER_INITJS) +#error unsupported legacy feature option DUK_OPT_USER_INITJS used +#endif + +/* + * Checks for config option consistency (DUK_USE_xxx) + */ + +#if defined(DUK_USE_32BIT_PTRS) +#error unsupported config option used (option has been removed): DUK_USE_32BIT_PTRS +#endif +#if defined(DUK_USE_ALIGN_4) +#error unsupported config option used (option has been removed): DUK_USE_ALIGN_4 +#endif +#if defined(DUK_USE_ALIGN_8) +#error unsupported config option used (option has been removed): DUK_USE_ALIGN_8 +#endif +#if defined(DUK_USE_BYTEORDER_FORCED) +#error unsupported config option used (option has been removed): DUK_USE_BYTEORDER_FORCED +#endif +#if defined(DUK_USE_DATAPTR_DEC16) && !defined(DUK_USE_DATAPTR16) +#error config option DUK_USE_DATAPTR_DEC16 requires option DUK_USE_DATAPTR16 (which is missing) +#endif +#if defined(DUK_USE_DATAPTR_ENC16) && !defined(DUK_USE_DATAPTR16) +#error config option DUK_USE_DATAPTR_ENC16 requires option DUK_USE_DATAPTR16 (which is missing) +#endif +#if defined(DUK_USE_DEBUGGER_SUPPORT) && !defined(DUK_USE_INTERRUPT_COUNTER) +#error config option DUK_USE_DEBUGGER_SUPPORT requires option DUK_USE_INTERRUPT_COUNTER (which is missing) +#endif +#if defined(DUK_USE_DEEP_C_STACK) +#error unsupported config option used (option has been removed): DUK_USE_DEEP_C_STACK +#endif +#if defined(DUK_USE_DOUBLE_BE) +#error unsupported config option used (option has been removed): DUK_USE_DOUBLE_BE +#endif +#if defined(DUK_USE_DOUBLE_BE) && defined(DUK_USE_DOUBLE_LE) +#error config option DUK_USE_DOUBLE_BE conflicts with option DUK_USE_DOUBLE_LE (which is also defined) +#endif +#if defined(DUK_USE_DOUBLE_BE) && defined(DUK_USE_DOUBLE_ME) +#error config option DUK_USE_DOUBLE_BE conflicts with option DUK_USE_DOUBLE_ME (which is also defined) +#endif +#if defined(DUK_USE_DOUBLE_LE) +#error unsupported config option used (option has been removed): DUK_USE_DOUBLE_LE +#endif +#if defined(DUK_USE_DOUBLE_LE) && defined(DUK_USE_DOUBLE_BE) +#error config option DUK_USE_DOUBLE_LE conflicts with option DUK_USE_DOUBLE_BE (which is also defined) +#endif +#if defined(DUK_USE_DOUBLE_LE) && defined(DUK_USE_DOUBLE_ME) +#error config option DUK_USE_DOUBLE_LE conflicts with option DUK_USE_DOUBLE_ME (which is also defined) +#endif +#if defined(DUK_USE_DOUBLE_ME) +#error unsupported config option used (option has been removed): DUK_USE_DOUBLE_ME +#endif +#if defined(DUK_USE_DOUBLE_ME) && defined(DUK_USE_DOUBLE_LE) +#error config option DUK_USE_DOUBLE_ME conflicts with option DUK_USE_DOUBLE_LE (which is also defined) +#endif +#if defined(DUK_USE_DOUBLE_ME) && defined(DUK_USE_DOUBLE_BE) +#error config option DUK_USE_DOUBLE_ME conflicts with option DUK_USE_DOUBLE_BE (which is also defined) +#endif +#if defined(DUK_USE_DPRINT) && !defined(DUK_USE_DEBUG) +#error config option DUK_USE_DPRINT requires option DUK_USE_DEBUG (which is missing) +#endif +#if defined(DUK_USE_ESBC_MAX_BYTES) && !defined(DUK_USE_ESBC_LIMITS) +#error config option DUK_USE_ESBC_MAX_BYTES requires option DUK_USE_ESBC_LIMITS (which is missing) +#endif +#if defined(DUK_USE_ESBC_MAX_LINENUMBER) && !defined(DUK_USE_ESBC_LIMITS) +#error config option DUK_USE_ESBC_MAX_LINENUMBER requires option DUK_USE_ESBC_LIMITS (which is missing) +#endif +#if defined(DUK_USE_EXEC_TIMEOUT_CHECK) && !defined(DUK_USE_INTERRUPT_COUNTER) +#error config option DUK_USE_EXEC_TIMEOUT_CHECK requires option DUK_USE_INTERRUPT_COUNTER (which is missing) +#endif +#if defined(DUK_USE_EXTSTR_FREE) && !defined(DUK_USE_HSTRING_EXTDATA) +#error config option DUK_USE_EXTSTR_FREE requires option DUK_USE_HSTRING_EXTDATA (which is missing) +#endif +#if defined(DUK_USE_EXTSTR_INTERN_CHECK) && !defined(DUK_USE_HSTRING_EXTDATA) +#error config option DUK_USE_EXTSTR_INTERN_CHECK requires option DUK_USE_HSTRING_EXTDATA (which is missing) +#endif +#if defined(DUK_USE_FULL_TVAL) +#error unsupported config option used (option has been removed): DUK_USE_FULL_TVAL +#endif +#if defined(DUK_USE_FUNCPTR_DEC16) && !defined(DUK_USE_FUNCPTR16) +#error config option DUK_USE_FUNCPTR_DEC16 requires option DUK_USE_FUNCPTR16 (which is missing) +#endif +#if defined(DUK_USE_FUNCPTR_ENC16) && !defined(DUK_USE_FUNCPTR16) +#error config option DUK_USE_FUNCPTR_ENC16 requires option DUK_USE_FUNCPTR16 (which is missing) +#endif +#if defined(DUK_USE_HASHBYTES_UNALIGNED_U32_ACCESS) +#error unsupported config option used (option has been removed): DUK_USE_HASHBYTES_UNALIGNED_U32_ACCESS +#endif +#if defined(DUK_USE_HEAPPTR16) && defined(DUK_USE_DEBUG) +#error config option DUK_USE_HEAPPTR16 conflicts with option DUK_USE_DEBUG (which is also defined) +#endif +#if defined(DUK_USE_HEAPPTR_DEC16) && !defined(DUK_USE_HEAPPTR16) +#error config option DUK_USE_HEAPPTR_DEC16 requires option DUK_USE_HEAPPTR16 (which is missing) +#endif +#if defined(DUK_USE_HEAPPTR_ENC16) && !defined(DUK_USE_HEAPPTR16) +#error config option DUK_USE_HEAPPTR_ENC16 requires option DUK_USE_HEAPPTR16 (which is missing) +#endif +#if defined(DUK_USE_INTEGER_BE) +#error unsupported config option used (option has been removed): DUK_USE_INTEGER_BE +#endif +#if defined(DUK_USE_INTEGER_BE) && defined(DUK_USE_INTEGER_LE) +#error config option DUK_USE_INTEGER_BE conflicts with option DUK_USE_INTEGER_LE (which is also defined) +#endif +#if defined(DUK_USE_INTEGER_BE) && defined(DUK_USE_INTEGER_ME) +#error config option DUK_USE_INTEGER_BE conflicts with option DUK_USE_INTEGER_ME (which is also defined) +#endif +#if defined(DUK_USE_INTEGER_LE) +#error unsupported config option used (option has been removed): DUK_USE_INTEGER_LE +#endif +#if defined(DUK_USE_INTEGER_LE) && defined(DUK_USE_INTEGER_BE) +#error config option DUK_USE_INTEGER_LE conflicts with option DUK_USE_INTEGER_BE (which is also defined) +#endif +#if defined(DUK_USE_INTEGER_LE) && defined(DUK_USE_INTEGER_ME) +#error config option DUK_USE_INTEGER_LE conflicts with option DUK_USE_INTEGER_ME (which is also defined) +#endif +#if defined(DUK_USE_INTEGER_ME) +#error unsupported config option used (option has been removed): DUK_USE_INTEGER_ME +#endif +#if defined(DUK_USE_INTEGER_ME) && defined(DUK_USE_INTEGER_LE) +#error config option DUK_USE_INTEGER_ME conflicts with option DUK_USE_INTEGER_LE (which is also defined) +#endif +#if defined(DUK_USE_INTEGER_ME) && defined(DUK_USE_INTEGER_BE) +#error config option DUK_USE_INTEGER_ME conflicts with option DUK_USE_INTEGER_BE (which is also defined) +#endif +#if defined(DUK_USE_NO_DOUBLE_ALIASING_SELFTEST) +#error unsupported config option used (option has been removed): DUK_USE_NO_DOUBLE_ALIASING_SELFTEST +#endif +#if defined(DUK_USE_PACKED_TVAL_POSSIBLE) +#error unsupported config option used (option has been removed): DUK_USE_PACKED_TVAL_POSSIBLE +#endif +#if defined(DUK_USE_RDTSC) +#error unsupported config option used (option has been removed): DUK_USE_RDTSC +#endif +#if defined(DUK_USE_ROM_GLOBAL_CLONE) && !defined(DUK_USE_ROM_STRINGS) +#error config option DUK_USE_ROM_GLOBAL_CLONE requires option DUK_USE_ROM_STRINGS (which is missing) +#endif +#if defined(DUK_USE_ROM_GLOBAL_CLONE) && !defined(DUK_USE_ROM_OBJECTS) +#error config option DUK_USE_ROM_GLOBAL_CLONE requires option DUK_USE_ROM_OBJECTS (which is missing) +#endif +#if defined(DUK_USE_ROM_GLOBAL_CLONE) && defined(DUK_USE_ROM_GLOBAL_INHERIT) +#error config option DUK_USE_ROM_GLOBAL_CLONE conflicts with option DUK_USE_ROM_GLOBAL_INHERIT (which is also defined) +#endif +#if defined(DUK_USE_ROM_GLOBAL_INHERIT) && !defined(DUK_USE_ROM_STRINGS) +#error config option DUK_USE_ROM_GLOBAL_INHERIT requires option DUK_USE_ROM_STRINGS (which is missing) +#endif +#if defined(DUK_USE_ROM_GLOBAL_INHERIT) && !defined(DUK_USE_ROM_OBJECTS) +#error config option DUK_USE_ROM_GLOBAL_INHERIT requires option DUK_USE_ROM_OBJECTS (which is missing) +#endif +#if defined(DUK_USE_ROM_GLOBAL_INHERIT) && defined(DUK_USE_ROM_GLOBAL_CLONE) +#error config option DUK_USE_ROM_GLOBAL_INHERIT conflicts with option DUK_USE_ROM_GLOBAL_CLONE (which is also defined) +#endif +#if defined(DUK_USE_ROM_OBJECTS) && !defined(DUK_USE_ROM_STRINGS) +#error config option DUK_USE_ROM_OBJECTS requires option DUK_USE_ROM_STRINGS (which is missing) +#endif +#if defined(DUK_USE_ROM_STRINGS) && !defined(DUK_USE_ROM_OBJECTS) +#error config option DUK_USE_ROM_STRINGS requires option DUK_USE_ROM_OBJECTS (which is missing) +#endif +#if defined(DUK_USE_SETJMP) +#error unsupported config option used (option has been removed): DUK_USE_SETJMP +#endif +#if defined(DUK_USE_SIGSETJMP) +#error unsupported config option used (option has been removed): DUK_USE_SIGSETJMP +#endif +#if defined(DUK_USE_STRTAB_CHAIN_SIZE) && !defined(DUK_USE_STRTAB_CHAIN) +#error config option DUK_USE_STRTAB_CHAIN_SIZE requires option DUK_USE_STRTAB_CHAIN (which is missing) +#endif +#if defined(DUK_USE_TAILCALL) && defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY) +#error config option DUK_USE_TAILCALL conflicts with option DUK_USE_NONSTD_FUNC_CALLER_PROPERTY (which is also defined) +#endif +#if defined(DUK_USE_UNALIGNED_ACCESSES_POSSIBLE) +#error unsupported config option used (option has been removed): DUK_USE_UNALIGNED_ACCESSES_POSSIBLE +#endif +#if defined(DUK_USE_UNDERSCORE_SETJMP) +#error unsupported config option used (option has been removed): DUK_USE_UNDERSCORE_SETJMP +#endif + +#if defined(DUK_USE_CPP_EXCEPTIONS) && !defined(__cplusplus) +#error DUK_USE_CPP_EXCEPTIONS enabled but not compiling with a C++ compiler +#endif + +/* + * Convert DUK_USE_BYTEORDER, from whatever source, into currently used + * internal defines. If detection failed, #error out. + */ + +#if defined(DUK_USE_BYTEORDER) +#if (DUK_USE_BYTEORDER == 1) +#define DUK_USE_INTEGER_LE +#define DUK_USE_DOUBLE_LE +#elif (DUK_USE_BYTEORDER == 2) +#define DUK_USE_INTEGER_LE /* integer endianness is little on purpose */ +#define DUK_USE_DOUBLE_ME +#elif (DUK_USE_BYTEORDER == 3) +#define DUK_USE_INTEGER_BE +#define DUK_USE_DOUBLE_BE +#else +#error unsupported: byte order invalid +#endif /* byte order */ +#else +#error unsupported: byte order detection failed +#endif /* defined(DUK_USE_BYTEORDER) */ + +#endif /* DUK_CONFIG_H_INCLUDED */ diff --git a/src/civetweb/src/third_party/duktape-1.5.2/config/genconfig.py b/src/civetweb/src/third_party/duktape-1.5.2/config/genconfig.py new file mode 100644 index 000000000..9c996481a --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/config/genconfig.py @@ -0,0 +1,1537 @@ +#!/usr/bin/env python2 +# +# Process Duktape option metadata and produce various useful outputs: +# +# - duk_config.h with specific or autodetected platform, compiler, and +# architecture; forced options; sanity checks; etc +# - option documentation for Duktape 1.x feature options (DUK_OPT_xxx) +# - option documentation for Duktape 1.x/2.x config options (DUK_USE_xxx) +# +# Genconfig tries to build all outputs based on modular metadata, so that +# managing a large number of config options (which is hard to avoid given +# the wide range of targets Duktape supports) remains maintainable. +# +# Genconfig does *not* try to support all exotic platforms out there. +# Instead, the goal is to allow the metadata to be extended, or to provide +# a reasonable starting point for manual duk_config.h tweaking. +# +# For Duktape 1.3 release the main goal was to autogenerate a Duktape 1.2 +# compatible "autodetect" header from legacy snippets, with other outputs +# being experimental. For Duktape 1.4 duk_config.h is always created from +# modular sources. +# + +import os +import sys +import re +import json +import yaml +import optparse +import tarfile +import tempfile +import atexit +import shutil +try: + from StringIO import StringIO +except ImportError: + from io import StringIO + +# +# Globals holding scanned metadata, helper snippets, etc +# + +# Metadata to scan from config files. +use_defs = None +use_defs_list = None +opt_defs = None +opt_defs_list = None +use_tags = None +use_tags_list = None +tags_meta = None +required_use_meta_keys = [ + 'define', + 'introduced', + 'default', + 'tags', + 'description' +] +allowed_use_meta_keys = [ + 'define', + 'feature_enables', + 'feature_disables', + 'feature_snippet', + 'feature_no_default', + 'related_feature_defines', + 'introduced', + 'deprecated', + 'removed', + 'unused', + 'requires', + 'conflicts', + 'related', + 'default', + 'tags', + 'description', +] +required_opt_meta_keys = [ + 'define', + 'introduced', + 'tags', + 'description' +] +allowed_opt_meta_keys = [ + 'define', + 'introduced', + 'deprecated', + 'removed', + 'unused', + 'requires', + 'conflicts', + 'related', + 'tags', + 'description' +] + +# Preferred tag order for option documentation. +doc_tag_order = [ + 'portability', + 'memory', + 'lowmemory', + 'ecmascript', + 'execution', + 'debugger', + 'debug', + 'development' +] + +# Preferred tag order for generated C header files. +header_tag_order = doc_tag_order + +# Helper headers snippets. +helper_snippets = None + +# Assume these provides come from outside. +assumed_provides = { + 'DUK_SINGLE_FILE': True, # compiling Duktape from a single source file (duktape.c) version + 'DUK_COMPILING_DUKTAPE': True, # compiling Duktape (not user application) + 'DUK_CONFIG_H_INCLUDED': True, # artifact, include guard +} + +# Platform files must provide at least these (additional checks +# in validate_platform_file()). Fill-ins provide missing optionals. +platform_required_provides = [ + 'DUK_USE_OS_STRING' # must be #define'd +] + +# Architecture files must provide at least these (additional checks +# in validate_architecture_file()). Fill-ins provide missing optionals. +architecture_required_provides = [ + 'DUK_USE_ARCH_STRING' +] + +# Compiler files must provide at least these (additional checks +# in validate_compiler_file()). Fill-ins provide missing optionals. +compiler_required_provides = [ + # Compilers need a lot of defines; missing defines are automatically + # filled in with defaults (which are mostly compiler independent), so + # the requires define list is not very large. + + 'DUK_USE_COMPILER_STRING', # must be #define'd + 'DUK_USE_BRANCH_HINTS', # may be #undef'd, as long as provided + 'DUK_USE_VARIADIC_MACROS', # may be #undef'd, as long as provided + 'DUK_USE_UNION_INITIALIZERS' # may be #undef'd, as long as provided +] + +# +# Miscellaneous helpers +# + +def get_auto_delete_tempdir(): + tmpdir = tempfile.mkdtemp(suffix='-genconfig') + def _f(dirname): + #print('Deleting temporary directory: %r' % dirname) + if os.path.isdir(dirname) and '-genconfig' in dirname: + shutil.rmtree(dirname) + atexit.register(_f, tmpdir) + return tmpdir + +def strip_comments_from_lines(lines): + # Not exact but close enough. Doesn't handle string literals etc, + # but these are not a concrete issue for scanning preprocessor + # #define references. + # + # Comment contents are stripped of any DUK_ prefixed text to avoid + # incorrect requires/provides detection. Other comment text is kept; + # in particular a "/* redefine */" comment must remain intact here. + # (The 'redefine' hack is not actively needed now.) + # + # Avoid Python 2.6 vs. Python 2.7 argument differences. + + def censor(x): + return re.sub(re.compile('DUK_\w+', re.MULTILINE), 'xxx', x.group(0)) + + tmp = '\n'.join(lines) + tmp = re.sub(re.compile('/\*.*?\*/', re.MULTILINE | re.DOTALL), censor, tmp) + tmp = re.sub(re.compile('//.*?$', re.MULTILINE), censor, tmp) + return tmp.split('\n') + +# Header snippet representation: lines, provides defines, requires defines. +re_line_provides = re.compile(r'^#(?:define|undef)\s+(\w+).*$') +re_line_requires = re.compile(r'(DUK_[A-Z0-9_]+)') # uppercase only, don't match DUK_USE_xxx for example +class Snippet: + lines = None # lines of text and/or snippets + provides = None # map from define to 'True' for now + requires = None # map from define to 'True' for now + + def __init__(self, lines, provides=None, requires=None, autoscan_requires=True, autoscan_provides=True): + self.lines = [] + if not isinstance(lines, list): + raise Exception('Snippet constructor must be a list (not e.g. a string): %s' % repr(lines)) + for line in lines: + if isinstance(line, str): + self.lines.append(line) + elif isinstance(line, unicode): + self.lines.append(line.encode('utf-8')) + else: + raise Exception('invalid line: %r' % line) + self.provides = {} + if provides is not None: + for k in provides.keys(): + self.provides[k] = True + self.requires = {} + if requires is not None: + for k in requires.keys(): + self.requires[k] = True + + stripped_lines = strip_comments_from_lines(lines) + # for line in stripped_lines: print(line) + + for line in stripped_lines: + # Careful with order, snippet may self-reference its own + # defines in which case there's no outward dependency. + # (This is not 100% because the order of require/provide + # matters and this is not handled now.) + # + # Also, some snippets may #undef/#define another define but + # they don't "provide" the define as such. Such redefinitions + # are marked "/* redefine */" in the snippets. They're best + # avoided (and not currently needed in Duktape 1.4.0). + + if autoscan_provides: + m = re_line_provides.match(line) + if m is not None and '/* redefine */' not in line and \ + len(m.group(1)) > 0 and m.group(1)[-1] != '_': + # Don't allow e.g. DUK_USE_ which results from matching DUK_USE_xxx + #print('PROVIDES: %r' % m.group(1)) + self.provides[m.group(1)] = True + if autoscan_requires: + matches = re.findall(re_line_requires, line) + for m in matches: + if len(m) > 0 and m[-1] == '_': + # Don't allow e.g. DUK_USE_ which results from matching DUK_USE_xxx + pass + elif m[:7] == 'DUK_OPT': + # DUK_OPT_xxx always come from outside + pass + elif m[:7] == 'DUK_USE': + # DUK_USE_xxx are internal and they should not be 'requirements' + pass + elif self.provides.has_key(m): + # Snippet provides it's own require; omit + pass + else: + #print('REQUIRES: %r' % m) + self.requires[m] = True + + def fromFile(cls, filename): + lines = [] + with open(filename, 'rb') as f: + for line in f: + if line[-1] == '\n': + line = line[:-1] + if line[:8] == '#snippet': + m = re.match(r'#snippet\s+"(.*?)"', line) + # XXX: better plumbing for lookup path + sub_fn = os.path.normpath(os.path.join(filename, '..', '..', 'header-snippets', m.group(1))) + #print('#snippet ' + sub_fn) + sn = Snippet.fromFile(sub_fn) + lines += sn.lines + else: + lines.append(line) + return Snippet(lines, autoscan_requires=True, autoscan_provides=True) + fromFile = classmethod(fromFile) + + def merge(cls, snippets): + ret = Snippet([], [], []) + for s in snippets: + ret.lines += s.lines + for k in s.provides.keys(): + ret.provides[k] = True + for k in s.requires.keys(): + ret.requires[k] = True + return ret + merge = classmethod(merge) + +# Helper for building a text file from individual lines, injected files, etc. +# Inserted values are converted to Snippets so that their provides/requires +# information can be tracked. When non-C outputs are created, these will be +# bogus but ignored. +class FileBuilder: + vals = None # snippet list + base_dir = None + use_cpp_warning = False + + def __init__(self, base_dir=None, use_cpp_warning=False): + self.vals = [] + self.base_dir = base_dir + self.use_cpp_warning = use_cpp_warning + + def line(self, line): + self.vals.append(Snippet([ line ])) + + def lines(self, lines): + if len(lines) > 0 and lines[-1] == '\n': + lines = lines[:-1] # strip last newline to avoid empty line + self.vals.append(Snippet(lines.split('\n'))) + + def empty(self): + self.vals.append(Snippet([ '' ])) + + def rst_heading(self, title, char, doubled=False): + tmp = [] + if doubled: + tmp.append(char * len(title)) + tmp.append(title) + tmp.append(char * len(title)) + self.vals.append(Snippet(tmp)) + + def snippet_relative(self, fn): + sn = Snippet.fromFile(os.path.join(self.base_dir, fn)) + self.vals.append(sn) + return sn + + def snippet_absolute(self, fn): + sn = Snippet.fromFile(fn) + self.vals.append(sn) + return sn + + def cpp_error(self, msg): + # XXX: assume no newlines etc + self.vals.append(Snippet([ '#error %s' % msg ])) + + def cpp_warning(self, msg): + # XXX: assume no newlines etc + # XXX: support compiler specific warning mechanisms + if self.use_cpp_warning: + # C preprocessor '#warning' is often supported + self.vals.append(Snippet([ '#warning %s' % msg ])) + else: + self.vals.append(Snippet([ '/* WARNING: %s */' % msg ])) + + def cpp_warning_or_error(self, msg, is_error=True): + if is_error: + self.cpp_error(msg) + else: + self.cpp_warning(msg) + + def chdr_comment_line(self, msg): + self.vals.append(Snippet([ '/* %s */' % msg ])) + + def chdr_block_heading(self, msg): + lines = [] + lines.append('') + lines.append('/*') + lines.append(' * ' + msg) + lines.append(' */') + lines.append('') + self.vals.append(Snippet(lines)) + + def join(self): + tmp = [] + for line in self.vals: + if not isinstance(line, object): + raise Exception('self.vals must be all snippets') + for x in line.lines: # x is a Snippet + tmp.append(x) + return '\n'.join(tmp) + + def fill_dependencies_for_snippets(self, idx_deps): + fill_dependencies_for_snippets(self.vals, idx_deps) + +# Insert missing define dependencies into index 'idx_deps' repeatedly +# until no unsatisfied dependencies exist. This is used to pull in +# the required DUK_F_xxx helper defines without pulling them all in. +# The resolution mechanism also ensures dependencies are pulled in the +# correct order, i.e. DUK_F_xxx helpers may depend on each other (as +# long as there are no circular dependencies). +# +# XXX: this can be simplified a lot +def fill_dependencies_for_snippets(snippets, idx_deps): + # graph[A] = [ B, ... ] <-> B, ... provide something A requires. + graph = {} + snlist = [] + resolved = [] # for printing only + + def add(sn): + if sn in snlist: + return # already present + snlist.append(sn) + + to_add = [] + + for k in sn.requires.keys(): + if assumed_provides.has_key(k): + continue + + found = False + for sn2 in snlist: + if sn2.provides.has_key(k): + if not graph.has_key(sn): + graph[sn] = [] + graph[sn].append(sn2) + found = True # at least one other node provides 'k' + + if not found: + #print('Resolving %r' % k) + resolved.append(k) + + # Find a header snippet which provides the missing define. + # Some DUK_F_xxx files provide multiple defines, so we don't + # necessarily know the snippet filename here. + + sn_req = None + for sn2 in helper_snippets: + if sn2.provides.has_key(k): + sn_req = sn2 + break + if sn_req is None: + print(repr(sn.lines)) + raise Exception('cannot resolve missing require: %r' % k) + + # Snippet may have further unresolved provides; add recursively + to_add.append(sn_req) + + if not graph.has_key(sn): + graph[sn] = [] + graph[sn].append(sn_req) + + for sn in to_add: + add(sn) + + # Add original snippets. This fills in the required nodes + # recursively. + for sn in snippets: + add(sn) + + # Figure out fill-ins by looking for snippets not in original + # list and without any unserialized dependent nodes. + handled = {} + for sn in snippets: + handled[sn] = True + keepgoing = True + while keepgoing: + keepgoing = False + for sn in snlist: + if handled.has_key(sn): + continue + + success = True + for dep in graph.get(sn, []): + if not handled.has_key(dep): + success = False + if success: + snippets.insert(idx_deps, sn) + idx_deps += 1 + snippets.insert(idx_deps, Snippet([ '' ])) + idx_deps += 1 + handled[sn] = True + keepgoing = True + break + + # XXX: detect and handle loops cleanly + for sn in snlist: + if handled.has_key(sn): + continue + print('UNHANDLED KEY') + print('PROVIDES: %r' % sn.provides) + print('REQUIRES: %r' % sn.requires) + print('\n'.join(sn.lines)) + +# print(repr(graph)) +# print(repr(snlist)) +# print('Resolved helper defines: %r' % resolved) + print('Resolved %d helper defines' % len(resolved)) + +def serialize_snippet_list(snippets): + ret = [] + + emitted_provides = {} + for k in assumed_provides.keys(): + emitted_provides[k] = True + + for sn in snippets: + ret += sn.lines + for k in sn.provides.keys(): + emitted_provides[k] = True + for k in sn.requires.keys(): + if not emitted_provides.has_key(k): + # XXX: conditional warning, happens in some normal cases + #print('WARNING: define %r required, not provided so far' % k) + pass + + return '\n'.join(ret) + +def remove_duplicate_newlines(x): + ret = [] + empty = False + for line in x.split('\n'): + if line == '': + if empty: + pass + else: + ret.append(line) + empty = True + else: + empty = False + ret.append(line) + return '\n'.join(ret) + +def scan_use_defs(dirname): + global use_defs, use_defs_list + use_defs = {} + use_defs_list = [] + + for fn in os.listdir(dirname): + root, ext = os.path.splitext(fn) + if not root.startswith('DUK_USE_') or ext != '.yaml': + continue + with open(os.path.join(dirname, fn), 'rb') as f: + doc = yaml.load(f) + if doc.get('example', False): + continue + if doc.get('unimplemented', False): + print('WARNING: unimplemented: %s' % fn) + continue + dockeys = doc.keys() + for k in dockeys: + if not k in allowed_use_meta_keys: + print('WARNING: unknown key %s in metadata file %s' % (k, fn)) + for k in required_use_meta_keys: + if not k in dockeys: + print('WARNING: missing key %s in metadata file %s' % (k, fn)) + + use_defs[doc['define']] = doc + + keys = use_defs.keys() + keys.sort() + for k in keys: + use_defs_list.append(use_defs[k]) + +def scan_opt_defs(dirname): + global opt_defs, opt_defs_list + opt_defs = {} + opt_defs_list = [] + + for fn in os.listdir(dirname): + root, ext = os.path.splitext(fn) + if not root.startswith('DUK_OPT_') or ext != '.yaml': + continue + with open(os.path.join(dirname, fn), 'rb') as f: + doc = yaml.load(f) + if doc.get('example', False): + continue + if doc.get('unimplemented', False): + print('WARNING: unimplemented: %s' % fn) + continue + dockeys = doc.keys() + for k in dockeys: + if not k in allowed_opt_meta_keys: + print('WARNING: unknown key %s in metadata file %s' % (k, fn)) + for k in required_opt_meta_keys: + if not k in dockeys: + print('WARNING: missing key %s in metadata file %s' % (k, fn)) + + opt_defs[doc['define']] = doc + + keys = opt_defs.keys() + keys.sort() + for k in keys: + opt_defs_list.append(opt_defs[k]) + +def scan_use_tags(): + global use_tags, use_tags_list + use_tags = {} + + for doc in use_defs_list: + for tag in doc.get('tags', []): + use_tags[tag] = True + + use_tags_list = use_tags.keys() + use_tags_list.sort() + +def scan_tags_meta(filename): + global tags_meta + + with open(filename, 'rb') as f: + tags_meta = yaml.load(f) + +def scan_helper_snippets(dirname): # DUK_F_xxx snippets + global helper_snippets + helper_snippets = [] + + for fn in os.listdir(dirname): + if (fn[0:6] != 'DUK_F_'): + continue + #print('Autoscanning snippet: %s' % fn) + helper_snippets.append(Snippet.fromFile(os.path.join(dirname, fn))) + +def get_opt_defs(removed=True, deprecated=True, unused=True): + ret = [] + for doc in opt_defs_list: + # XXX: aware of target version + if removed == False and doc.get('removed', None) is not None: + continue + if deprecated == False and doc.get('deprecated', None) is not None: + continue + if unused == False and doc.get('unused', False) == True: + continue + ret.append(doc) + return ret + +def get_use_defs(removed=True, deprecated=True, unused=True): + ret = [] + for doc in use_defs_list: + # XXX: aware of target version + if removed == False and doc.get('removed', None) is not None: + continue + if deprecated == False and doc.get('deprecated', None) is not None: + continue + if unused == False and doc.get('unused', False) == True: + continue + ret.append(doc) + return ret + +def validate_platform_file(filename): + sn = Snippet.fromFile(filename) + + for req in platform_required_provides: + if req not in sn.provides: + raise Exception('Platform %s is missing %s' % (filename, req)) + + # DUK_SETJMP, DUK_LONGJMP, DUK_JMPBUF_TYPE are optional, fill-in + # provides if none defined. + +def validate_architecture_file(filename): + sn = Snippet.fromFile(filename) + + for req in architecture_required_provides: + if req not in sn.provides: + raise Exception('Architecture %s is missing %s' % (filename, req)) + + # Byte order and alignment defines are allowed to be missing, + # a fill-in will handle them. This is necessary because for + # some architecture byte order and/or alignment may vary between + # targets and may be software configurable. + + # XXX: require automatic detection to be signaled? + # e.g. define DUK_USE_ALIGN_BY -1 + # define DUK_USE_BYTE_ORDER -1 + +def validate_compiler_file(filename): + sn = Snippet.fromFile(filename) + + for req in compiler_required_provides: + if req not in sn.provides: + raise Exception('Compiler %s is missing %s' % (filename, req)) + +def get_tag_title(tag): + meta = tags_meta.get(tag, None) + if meta is None: + return tag + else: + return meta.get('title', tag) + +def get_tag_description(tag): + meta = tags_meta.get(tag, None) + if meta is None: + return None + else: + return meta.get('description', None) + +def get_tag_list_with_preferred_order(preferred): + tags = [] + + # Preferred tags first + for tag in preferred: + if tag not in tags: + tags.append(tag) + + # Remaining tags in alphabetic order + for tag in use_tags_list: + if tag not in tags: + tags.append(tag) + + #print('Effective tag order: %r' % tags) + return tags + +def rst_format(text): + # XXX: placeholder, need to decide on markup conventions for YAML files + ret = [] + for para in text.split('\n'): + if para == '': + continue + ret.append(para) + return '\n\n'.join(ret) + +def cint_encode(x): + if not isinstance(x, (int, long)): + raise Exception('invalid input: %r' % x) + + # XXX: unsigned constants? + if x > 0x7fffffff or x < -0x80000000: + return '%dLL' % x + elif x > 0x7fff or x < -0x8000: + return '%dL' % x + else: + return '%d' % x + +def cstr_encode(x): + if isinstance(x, unicode): + x = x.encode('utf-8') + if not isinstance(x, str): + raise Exception('invalid input: %r' % x) + + res = '"' + term = False + has_terms = False + for c in x: + if term: + # Avoid ambiguous hex escapes + res += '" "' + term = False + has_terms = True + o = ord(c) + if o < 0x20 or o > 0x7e or c in '"\\': + res += '\\x%02x' % o + term = True + else: + res += c + res += '"' + + if has_terms: + res = '(' + res + ')' + + return res + +# +# Autogeneration of option documentation +# + +# Shared helper to generate DUK_OPT_xxx and DUK_USE_xxx documentation. +# XXX: unfinished placeholder +def generate_option_documentation(opts, opt_list=None, rst_title=None, include_default=False): + ret = FileBuilder(use_cpp_warning=opts.use_cpp_warning) + + tags = get_tag_list_with_preferred_order(doc_tag_order) + + title = rst_title + ret.rst_heading(title, '=', doubled=True) + + handled = {} + + for tag in tags: + first = True + + for doc in opt_list: + if tag != doc['tags'][0]: # sort under primary tag + continue + dname = doc['define'] + desc = doc.get('description', None) + + if handled.has_key(dname): + raise Exception('define handled twice, should not happen: %r' % dname) + handled[dname] = True + + if first: # emit tag heading only if there are subsections + ret.empty() + ret.rst_heading(get_tag_title(tag), '=') + + tag_desc = get_tag_description(tag) + if tag_desc is not None: + ret.empty() + ret.line(rst_format(tag_desc)) + first = False + + ret.empty() + ret.rst_heading(dname, '-') + + if desc is not None: + ret.empty() + ret.line(rst_format(desc)) + + if include_default: + ret.empty() + ret.line('Default: ``' + str(doc['default']) + '``') # XXX: rst or other format + + for doc in opt_list: + dname = doc['define'] + if not handled.has_key(dname): + raise Exception('unhandled define (maybe missing from tags list?): %r' % dname) + + ret.empty() + return ret.join() + +def generate_feature_option_documentation(opts): + defs = get_opt_defs() + return generate_option_documentation(opts, opt_list=defs, rst_title='Duktape feature options', include_default=False) + +def generate_config_option_documentation(opts): + defs = get_use_defs() + return generate_option_documentation(opts, opt_list=defs, rst_title='Duktape config options', include_default=True) + +# +# Helpers for duk_config.h generation +# + +def get_forced_options(opts): + # Forced options, last occurrence wins (allows a base config file to be + # overridden by a more specific one). + forced_opts = {} + for val in opts.force_options_yaml: + doc = yaml.load(StringIO(val)) + for k in doc.keys(): + if use_defs.has_key(k): + pass # key is known + else: + print('WARNING: option override key %s not defined in metadata, ignoring' % k) + forced_opts[k] = doc[k] # shallow copy + + if len(forced_opts.keys()) > 0: + print('Overrides: %s' % json.dumps(forced_opts)) + + return forced_opts + +# Emit a default #define / #undef for an option based on +# a config option metadata node (parsed YAML doc). +def emit_default_from_config_meta(ret, doc, forced_opts, undef_done): + defname = doc['define'] + defval = forced_opts.get(defname, doc['default']) + + # NOTE: careful with Python equality, e.g. "0 == False" is true. + if isinstance(defval, bool) and defval == True: + ret.line('#define ' + defname) + elif isinstance(defval, bool) and defval == False: + if not undef_done: + ret.line('#undef ' + defname) + else: + # Default value is false, and caller has emitted + # an unconditional #undef, so don't emit a duplicate + pass + elif isinstance(defval, (int, long)): + # integer value + ret.line('#define ' + defname + ' ' + cint_encode(defval)) + elif isinstance(defval, (str, unicode)): + # verbatim value + ret.line('#define ' + defname + ' ' + defval) + elif isinstance(defval, dict): + if defval.has_key('verbatim'): + # verbatim text for the entire line + ret.line(defval['verbatim']) + elif defval.has_key('string'): + # C string value + ret.line('#define ' + defname + ' ' + cstr_encode(defval['string'])) + else: + raise Exception('unsupported value for option %s: %r' % (defname, defval)) + else: + raise Exception('unsupported value for option %s: %r' % (defname, defval)) + +# Add a header snippet for detecting presence of DUK_OPT_xxx feature +# options which will be removed in Duktape 2.x. +def add_legacy_feature_option_checks(opts, ret): + ret.chdr_block_heading('Checks for legacy feature options (DUK_OPT_xxx)') + ret.empty() + + defs = [] + for doc in get_opt_defs(): + if doc['define'] not in defs: + defs.append(doc['define']) + for doc in get_opt_defs(): + for dname in doc.get('related_feature_defines', []): + if dname not in defs: + defs.append(dname) + defs.sort() + + for optname in defs: + suggested = [] + for doc in get_use_defs(): + if optname in doc.get('related_feature_defines', []): + suggested.append(doc['define']) + ret.line('#if defined(%s)' % optname) + if len(suggested) > 0: + ret.cpp_warning_or_error('unsupported legacy feature option %s used, consider options: %s' % (optname, ', '.join(suggested)), opts.sanity_strict) + else: + ret.cpp_warning_or_error('unsupported legacy feature option %s used' % optname, opts.sanity_strict) + ret.line('#endif') + + ret.empty() + +# Add a header snippet for checking consistency of DUK_USE_xxx config +# options, e.g. inconsistent options, invalid option values. +def add_config_option_checks(opts, ret): + ret.chdr_block_heading('Checks for config option consistency (DUK_USE_xxx)') + ret.empty() + + defs = [] + for doc in get_use_defs(): + if doc['define'] not in defs: + defs.append(doc['define']) + defs.sort() + + for optname in defs: + doc = use_defs[optname] + dname = doc['define'] + + # XXX: more checks + + if doc.get('removed', None) is not None: + ret.line('#if defined(%s)' % dname) + ret.cpp_warning_or_error('unsupported config option used (option has been removed): %s' % dname, opts.sanity_strict) + ret.line('#endif') + elif doc.get('deprecated', None) is not None: + ret.line('#if defined(%s)' % dname) + ret.cpp_warning_or_error('unsupported config option used (option has been deprecated): %s' % dname, opts.sanity_strict) + ret.line('#endif') + + for req in doc.get('requires', []): + ret.line('#if defined(%s) && !defined(%s)' % (dname, req)) + ret.cpp_warning_or_error('config option %s requires option %s (which is missing)' % (dname, req), opts.sanity_strict) + ret.line('#endif') + + for req in doc.get('conflicts', []): + ret.line('#if defined(%s) && defined(%s)' % (dname, req)) + ret.cpp_warning_or_error('config option %s conflicts with option %s (which is also defined)' % (dname, req), opts.sanity_strict) + ret.line('#endif') + + ret.empty() + ret.snippet_relative('cpp_exception_sanity.h.in') + ret.empty() + +# Add a header snippet for providing a __OVERRIDE_DEFINES__ section. +def add_override_defines_section(opts, ret): + ret.empty() + ret.line('/*') + ret.line(' * You may add overriding #define/#undef directives below for') + ret.line(' * customization. You of course cannot un-#include or un-typedef') + ret.line(' * anything; these require direct changes above.') + ret.line(' */') + ret.empty() + ret.line('/* __OVERRIDE_DEFINES__ */') + ret.empty() + +# Add automatic DUK_OPT_XXX and DUK_OPT_NO_XXX handling for backwards +# compatibility with Duktape 1.2 and before. +def add_feature_option_handling(opts, ret, forced_opts, already_provided_keys): + ret.chdr_block_heading('Feature option handling') + + for doc in get_use_defs(removed=False, deprecated=False, unused=False): + # If a related feature option exists, it can be used to force + # enable/disable the target feature. If neither feature option + # (DUK_OPT_xxx or DUK_OPT_NO_xxx) is given, revert to default. + + config_define = doc['define'] + + feature_define = None + feature_no_define = None + inverted = False + if doc.has_key('feature_enables'): + feature_define = doc['feature_enables'] + elif doc.has_key('feature_disables'): + feature_define = doc['feature_disables'] + inverted = True + else: + pass + + if feature_define is not None: + feature_no_define = 'DUK_OPT_NO_' + feature_define[8:] + ret.line('#if defined(%s)' % feature_define) + if inverted: + ret.line('#undef %s' % config_define) + else: + ret.line('#define %s' % config_define) + ret.line('#elif defined(%s)' % feature_no_define) + if inverted: + ret.line('#define %s' % config_define) + else: + ret.line('#undef %s' % config_define) + ret.line('#else') + undef_done = False + + # For some options like DUK_OPT_PACKED_TVAL the default comes + # from platform definition. + if doc.get('feature_no_default', False): + print('Skip default for option %s' % config_define) + ret.line('/* Already provided above */') + elif already_provided_keys.has_key(config_define): + # This is a fallback in case config option metadata is wrong. + print('Skip default for option %s (already provided but not flagged in metadata!)' % config_define) + ret.line('/* Already provided above */') + else: + emit_default_from_config_meta(ret, doc, forced_opts, undef_done) + ret.line('#endif') + elif doc.has_key('feature_snippet'): + ret.lines(doc['feature_snippet']) + else: + pass + + ret.empty() + + ret.empty() + +# Development time helper: add DUK_ACTIVE which provides a runtime C string +# indicating what DUK_USE_xxx config options are active at run time. This +# is useful in genconfig development so that one can e.g. diff the active +# run time options of two headers. This is intended just for genconfig +# development and is not available in normal headers. +def add_duk_active_defines_macro(ret): + ret.chdr_block_heading('DUK_ACTIVE_DEFINES macro (development only)') + + idx = 0 + for doc in get_use_defs(): + defname = doc['define'] + + ret.line('#if defined(%s)' % defname) + ret.line('#define DUK_ACTIVE_DEF%d " %s"' % (idx, defname)) + ret.line('#else') + ret.line('#define DUK_ACTIVE_DEF%d ""' % idx) + ret.line('#endif') + + idx += 1 + + tmp = [] + for i in xrange(idx): + tmp.append('DUK_ACTIVE_DEF%d' % i) + + ret.line('#define DUK_ACTIVE_DEFINES ("Active: ["' + ' '.join(tmp) + ' " ]")') + +# +# duk_config.h generation +# + +# Generate a duk_config.h where platform, architecture, and compiler are +# all either autodetected or specified by user. +# +# Autodetection is based on a configured list of supported platforms, +# architectures, and compilers. For example, platforms.yaml defines the +# supported platforms and provides a helper define (DUK_F_xxx) to use for +# detecting that platform, and names the header snippet to provide the +# platform-specific definitions. Necessary dependencies (DUK_F_xxx) are +# automatically pulled in. +# +# Automatic "fill ins" are used for mandatory platform, architecture, and +# compiler defines which have a reasonable portable default. This reduces +# e.g. compiler-specific define count because there are a lot compiler +# macros which have a good default. +def generate_duk_config_header(opts, meta_dir): + ret = FileBuilder(base_dir=os.path.join(meta_dir, 'header-snippets'), \ + use_cpp_warning=opts.use_cpp_warning) + + forced_opts = get_forced_options(opts) + + platforms = None + with open(os.path.join(meta_dir, 'platforms.yaml'), 'rb') as f: + platforms = yaml.load(f) + architectures = None + with open(os.path.join(meta_dir, 'architectures.yaml'), 'rb') as f: + architectures = yaml.load(f) + compilers = None + with open(os.path.join(meta_dir, 'compilers.yaml'), 'rb') as f: + compilers = yaml.load(f) + + # XXX: indicate feature option support, sanity checks enabled, etc + # in general summary of options, perhaps genconfig command line? + + ret.line('/*') + ret.line(' * duk_config.h configuration header generated by genconfig.py.') + ret.line(' *') + ret.line(' * Git commit: %s' % opts.git_commit or 'n/a') + ret.line(' * Git describe: %s' % opts.git_describe or 'n/a') + ret.line(' * Git branch: %s' % opts.git_branch or 'n/a') + ret.line(' *') + if opts.platform is not None: + ret.line(' * Platform: ' + opts.platform) + else: + ret.line(' * Supported platforms:') + for platf in platforms['autodetect']: + ret.line(' * - %s' % platf.get('name', platf.get('check'))) + ret.line(' *') + if opts.architecture is not None: + ret.line(' * Architecture: ' + opts.architecture) + else: + ret.line(' * Supported architectures:') + for arch in architectures['autodetect']: + ret.line(' * - %s' % arch.get('name', arch.get('check'))) + ret.line(' *') + if opts.compiler is not None: + ret.line(' * Compiler: ' + opts.compiler) + else: + ret.line(' * Supported compilers:') + for comp in compilers['autodetect']: + ret.line(' * - %s' % comp.get('name', comp.get('check'))) + ret.line(' *') + ret.line(' */') + ret.empty() + ret.line('#if !defined(DUK_CONFIG_H_INCLUDED)') + ret.line('#define DUK_CONFIG_H_INCLUDED') + ret.empty() + + ret.chdr_block_heading('Intermediate helper defines') + + # DLL build affects visibility attributes on Windows but unfortunately + # cannot be detected automatically from preprocessor defines or such. + # DLL build status is hidden behind DUK_F_DLL_BUILD and there are two + # ways for that to be set: + # + # - Duktape 1.3 backwards compatible DUK_OPT_DLL_BUILD + # - Genconfig --dll option + ret.chdr_comment_line('DLL build detection') + ret.line('#if defined(DUK_OPT_DLL_BUILD)') + ret.line('#define DUK_F_DLL_BUILD') + ret.line('#elif defined(DUK_OPT_NO_DLL_BUILD)') + ret.line('#undef DUK_F_DLL_BUILD') + ret.line('#else') + if opts.dll: + ret.line('/* configured for DLL build */') + ret.line('#define DUK_F_DLL_BUILD') + else: + ret.line('/* not configured for DLL build */') + ret.line('#undef DUK_F_DLL_BUILD') + ret.line('#endif') + ret.empty() + + idx_deps = len(ret.vals) # position where to emit DUK_F_xxx dependencies + + # Feature selection, system include, Date provider + # Most #include statements are here + + if opts.platform is not None: + ret.chdr_block_heading('Platform: ' + opts.platform) + + ret.snippet_relative('platform_cppextras.h.in') + ret.empty() + + # XXX: better to lookup platforms metadata + include = 'platform_%s.h.in' % opts.platform + abs_fn = os.path.join(meta_dir, 'platforms', include) + validate_platform_file(abs_fn) + ret.snippet_absolute(abs_fn) + else: + ret.chdr_block_heading('Platform autodetection') + + ret.snippet_relative('platform_cppextras.h.in') + ret.empty() + + for idx, platf in enumerate(platforms['autodetect']): + check = platf.get('check', None) + include = platf['include'] + abs_fn = os.path.join(meta_dir, 'platforms', include) + + validate_platform_file(abs_fn) + + if idx == 0: + ret.line('#if defined(%s)' % check) + else: + if check is None: + ret.line('#else') + else: + ret.line('#elif defined(%s)' % check) + ret.line('/* --- %s --- */' % platf.get('name', '???')) + ret.snippet_absolute(abs_fn) + ret.line('#endif /* autodetect platform */') + + ret.empty() + ret.snippet_relative('platform_sharedincludes.h.in') + ret.empty() + + byteorder_provided_by_all = True # byteorder provided by all architecture files + alignment_provided_by_all = True # alignment provided by all architecture files + packedtval_provided_by_all = True # packed tval provided by all architecture files + + if opts.architecture is not None: + ret.chdr_block_heading('Architecture: ' + opts.architecture) + + # XXX: better to lookup architectures metadata + include = 'architecture_%s.h.in' % opts.architecture + abs_fn = os.path.join(meta_dir, 'architectures', include) + validate_architecture_file(abs_fn) + sn = ret.snippet_absolute(abs_fn) + if not sn.provides.get('DUK_USE_BYTEORDER', False): + byteorder_provided_by_all = False + if not sn.provides.get('DUK_USE_ALIGN_BY', False): + alignment_provided_by_all = False + if sn.provides.get('DUK_USE_PACKED_TVAL', False): + ret.line('#define DUK_F_PACKED_TVAL_PROVIDED') # signal to fillin + else: + packedtval_provided_by_all = False + else: + ret.chdr_block_heading('Architecture autodetection') + + for idx, arch in enumerate(architectures['autodetect']): + check = arch.get('check', None) + include = arch['include'] + abs_fn = os.path.join(meta_dir, 'architectures', include) + + validate_architecture_file(abs_fn) + + if idx == 0: + ret.line('#if defined(%s)' % check) + else: + if check is None: + ret.line('#else') + else: + ret.line('#elif defined(%s)' % check) + ret.line('/* --- %s --- */' % arch.get('name', '???')) + sn = ret.snippet_absolute(abs_fn) + if not sn.provides.get('DUK_USE_BYTEORDER', False): + byteorder_provided_by_all = False + if not sn.provides.get('DUK_USE_ALIGN_BY', False): + alignment_provided_by_all = False + if sn.provides.get('DUK_USE_PACKED_TVAL', False): + ret.line('#define DUK_F_PACKED_TVAL_PROVIDED') # signal to fillin + else: + packedtval_provided_by_all = False + ret.line('#endif /* autodetect architecture */') + + ret.empty() + + if opts.compiler is not None: + ret.chdr_block_heading('Compiler: ' + opts.compiler) + + # XXX: better to lookup compilers metadata + include = 'compiler_%s.h.in' % opts.compiler + abs_fn = os.path.join(meta_dir, 'compilers', include) + validate_compiler_file(abs_fn) + sn = ret.snippet_absolute(abs_fn) + else: + ret.chdr_block_heading('Compiler autodetection') + + for idx, comp in enumerate(compilers['autodetect']): + check = comp.get('check', None) + include = comp['include'] + abs_fn = os.path.join(meta_dir, 'compilers', include) + + validate_compiler_file(abs_fn) + + if idx == 0: + ret.line('#if defined(%s)' % check) + else: + if check is None: + ret.line('#else') + else: + ret.line('#elif defined(%s)' % check) + ret.line('/* --- %s --- */' % comp.get('name', '???')) + sn = ret.snippet_absolute(abs_fn) + ret.line('#endif /* autodetect compiler */') + + ret.empty() + + # DUK_F_UCLIBC is special because __UCLIBC__ is provided by an #include + # file, so the check must happen after platform includes. It'd be nice + # for this to be automatic (e.g. DUK_F_UCLIBC.h.in could indicate the + # dependency somehow). + + ret.snippet_absolute(os.path.join(meta_dir, 'helper-snippets', 'DUK_F_UCLIBC.h.in')) + ret.empty() + + # XXX: platform/compiler could provide types; if so, need some signaling + # defines like DUK_F_TYPEDEFS_DEFINED + + # Number types + if opts.c99_types_only: + ret.snippet_relative('types1.h.in') + ret.line('/* C99 types assumed */') + ret.snippet_relative('types_c99.h.in') + ret.empty() + else: + ret.snippet_relative('types1.h.in') + ret.line('#if defined(DUK_F_HAVE_INTTYPES)') + ret.line('/* C99 or compatible */') + ret.empty() + ret.snippet_relative('types_c99.h.in') + ret.empty() + ret.line('#else /* C99 types */') + ret.empty() + ret.snippet_relative('types_legacy.h.in') + ret.empty() + ret.line('#endif /* C99 types */') + ret.empty() + ret.snippet_relative('types2.h.in') + ret.empty() + ret.snippet_relative('64bitops.h.in') + ret.empty() + + # Platform, architecture, compiler fillins. These are after all + # detection so that e.g. DUK_SPRINTF() can be provided by platform + # or compiler before trying a fill-in. + + ret.chdr_block_heading('Fill-ins for platform, architecture, and compiler') + + ret.snippet_relative('platform_fillins.h.in') + ret.empty() + ret.snippet_relative('architecture_fillins.h.in') + if not byteorder_provided_by_all: + ret.empty() + ret.snippet_relative('byteorder_fillin.h.in') + if not alignment_provided_by_all: + ret.empty() + ret.snippet_relative('alignment_fillin.h.in') + ret.empty() + ret.snippet_relative('compiler_fillins.h.in') + ret.empty() + ret.snippet_relative('inline_workaround.h.in') + ret.empty() + if not packedtval_provided_by_all: + ret.empty() + ret.snippet_relative('packed_tval_fillin.h.in') + + # Object layout + ret.snippet_relative('object_layout.h.in') + ret.empty() + + # Detect and reject 'fast math' + ret.snippet_relative('reject_fast_math.h.in') + ret.empty() + + # Automatic DUK_OPT_xxx feature option handling + if opts.support_feature_options: + print('Autogenerating feature option (DUK_OPT_xxx) support') + tmp = Snippet(ret.join().split('\n')) + add_feature_option_handling(opts, ret, forced_opts, tmp.provides) + + # Emit forced options. If a corresponding option is already defined + # by a snippet above, #undef it first. + + tmp = Snippet(ret.join().split('\n')) + first_forced = True + for doc in get_use_defs(removed=not opts.omit_removed_config_options, + deprecated=not opts.omit_deprecated_config_options, + unused=not opts.omit_unused_config_options): + defname = doc['define'] + + if not forced_opts.has_key(defname): + continue + + if not doc.has_key('default'): + raise Exception('config option %s is missing default value' % defname) + + if first_forced: + ret.chdr_block_heading('Forced options') + first_forced = False + + undef_done = False + if tmp.provides.has_key(defname): + ret.line('#undef ' + defname) + undef_done = True + + emit_default_from_config_meta(ret, doc, forced_opts, undef_done) + + ret.empty() + + # If manually-edited snippets don't #define or #undef a certain + # config option, emit a default value here. This is useful to + # fill-in for new config options not covered by manual snippets + # (which is intentional). + + tmp = Snippet(ret.join().split('\n')) + need = {} + for doc in get_use_defs(removed=False): + need[doc['define']] = True + for k in tmp.provides.keys(): + if need.has_key(k): + del need[k] + need_keys = sorted(need.keys()) + + if len(need_keys) > 0: + ret.chdr_block_heading('Autogenerated defaults') + + for k in need_keys: + #print('config option %s not covered by manual snippets, emitting default automatically' % k) + emit_default_from_config_meta(ret, use_defs[k], {}, False) + + ret.empty() + + ret.snippet_relative('custom_header.h.in') + ret.empty() + + if len(opts.fixup_header_lines) > 0: + ret.chdr_block_heading('Fixups') + for line in opts.fixup_header_lines: + ret.line(line) + ret.empty() + + add_override_defines_section(opts, ret) + + # Date provider snippet is after custom header and overrides, so that + # the user may define e.g. DUK_USE_DATE_NOW_GETTIMEOFDAY in their + # custom header. + ret.snippet_relative('date_provider.h.in') + ret.empty() + + ret.fill_dependencies_for_snippets(idx_deps) + + if opts.emit_legacy_feature_check: + add_legacy_feature_option_checks(opts, ret) + if opts.emit_config_sanity_check: + add_config_option_checks(opts, ret) + if opts.add_active_defines_macro: + add_duk_active_defines_macro(ret) + + # Derived defines (DUK_USE_INTEGER_LE, etc) from DUK_USE_BYTEORDER. + # Duktape internals currently rely on the derived defines. This is + # after sanity checks because the derived defines are marked removed. + ret.snippet_relative('byteorder_derived.h.in') + ret.empty() + + ret.line('#endif /* DUK_CONFIG_H_INCLUDED */') + ret.empty() # for trailing newline + return remove_duplicate_newlines(ret.join()) + +# +# Main +# + +def main(): + # Forced options from multiple sources are gathered into a shared list + # so that the override order remains the same as on the command line. + force_options_yaml = [] + def add_force_option_yaml(option, opt, value, parser): + # XXX: check that YAML parses + force_options_yaml.append(value) + def add_force_option_file(option, opt, value, parser): + # XXX: check that YAML parses + with open(value, 'rb') as f: + force_options_yaml.append(f.read()) + def add_force_option_define(option, opt, value, parser): + tmp = value.split('=') + if len(tmp) == 1: + doc = { tmp[0]: True } + elif len(tmp) == 2: + doc = { tmp[0]: tmp[1] } + else: + raise Exception('invalid option value: %r' % value) + force_options_yaml.append(yaml.safe_dump(doc)) + def add_force_option_undefine(option, opt, value, parser): + tmp = value.split('=') + if len(tmp) == 1: + doc = { tmp[0]: False } + else: + raise Exception('invalid option value: %r' % value) + force_options_yaml.append(yaml.safe_dump(doc)) + + fixup_header_lines = [] + def add_fixup_header_line(option, opt, value, parser): + fixup_header_lines.append(value) + def add_fixup_header_file(option, opt, value, parser): + with open(value, 'rb') as f: + for line in f: + if line[-1] == '\n': + line = line[:-1] + fixup_header_lines.append(line) + + commands = [ + 'duk-config-header', + 'feature-documentation', + 'config-documentation' + ] + parser = optparse.OptionParser( + usage='Usage: %prog [options] COMMAND', + description='Generate a duk_config.h or config option documentation based on config metadata.', + epilog='COMMAND can be one of: ' + ', '.join(commands) + '.' + ) + + parser.add_option('--metadata', dest='metadata', default=None, help='metadata directory or metadata tar.gz file') + parser.add_option('--output', dest='output', default=None, help='output filename for C header or RST documentation file') + parser.add_option('--platform', dest='platform', default=None, help='platform (for "barebones-header" command)') + parser.add_option('--compiler', dest='compiler', default=None, help='compiler (for "barebones-header" command)') + parser.add_option('--architecture', dest='architecture', default=None, help='architecture (for "barebones-header" command)') + parser.add_option('--c99-types-only', dest='c99_types_only', action='store_true', default=False, help='assume C99 types, no legacy type detection') + parser.add_option('--dll', dest='dll', action='store_true', default=False, help='dll build of Duktape, affects symbol visibility macros especially on Windows') + parser.add_option('--support-feature-options', dest='support_feature_options', action='store_true', default=False, help='support DUK_OPT_xxx feature options in duk_config.h') + parser.add_option('--emit-legacy-feature-check', dest='emit_legacy_feature_check', action='store_true', default=False, help='emit preprocessor checks to reject legacy feature options (DUK_OPT_xxx)') + parser.add_option('--emit-config-sanity-check', dest='emit_config_sanity_check', action='store_true', default=False, help='emit preprocessor checks for config option consistency (DUK_OPT_xxx)') + parser.add_option('--omit-removed-config-options', dest='omit_removed_config_options', action='store_true', default=False, help='omit removed config options from generated headers') + parser.add_option('--omit-deprecated-config-options', dest='omit_deprecated_config_options', action='store_true', default=False, help='omit deprecated config options from generated headers') + parser.add_option('--omit-unused-config-options', dest='omit_unused_config_options', action='store_true', default=False, help='omit unused config options from generated headers') + parser.add_option('--add-active-defines-macro', dest='add_active_defines_macro', action='store_true', default=False, help='add DUK_ACTIVE_DEFINES macro, for development only') + parser.add_option('--define', type='string', dest='force_options_yaml', action='callback', callback=add_force_option_define, default=force_options_yaml, help='force #define option using a C compiler like syntax, e.g. "--define DUK_USE_DEEP_C_STACK" or "--define DUK_USE_TRACEBACK_DEPTH=10"') + parser.add_option('-D', type='string', dest='force_options_yaml', action='callback', callback=add_force_option_define, default=force_options_yaml, help='synonym for --define, e.g. "-DDUK_USE_DEEP_C_STACK" or "-DDUK_USE_TRACEBACK_DEPTH=10"') + parser.add_option('--undefine', type='string', dest='force_options_yaml', action='callback', callback=add_force_option_undefine, default=force_options_yaml, help='force #undef option using a C compiler like syntax, e.g. "--undefine DUK_USE_DEEP_C_STACK"') + parser.add_option('-U', type='string', dest='force_options_yaml', action='callback', callback=add_force_option_undefine, default=force_options_yaml, help='synonym for --undefine, e.g. "-UDUK_USE_DEEP_C_STACK"') + parser.add_option('--option-yaml', type='string', dest='force_options_yaml', action='callback', callback=add_force_option_yaml, default=force_options_yaml, help='force option(s) using inline YAML (e.g. --option-yaml "DUK_USE_DEEP_C_STACK: true")') + parser.add_option('--option-file', type='string', dest='force_options_yaml', action='callback', callback=add_force_option_file, default=force_options_yaml, help='YAML file(s) providing config option overrides') + parser.add_option('--fixup-file', type='string', dest='fixup_header_lines', action='callback', callback=add_fixup_header_file, default=fixup_header_lines, help='C header snippet file(s) to be appended to generated header, useful for manual option fixups') + parser.add_option('--fixup-line', type='string', dest='fixup_header_lines', action='callback', callback=add_fixup_header_line, default=fixup_header_lines, help='C header fixup line to be appended to generated header (e.g. --fixup-line "#define DUK_USE_FASTINT")') + parser.add_option('--sanity-warning', dest='sanity_strict', action='store_false', default=True, help='emit a warning instead of #error for option sanity check issues') + parser.add_option('--use-cpp-warning', dest='use_cpp_warning', action='store_true', default=False, help='emit a (non-portable) #warning when appropriate') + parser.add_option('--git-commit', dest='git_commit', default=None, help='git commit hash to be included in header comments') + parser.add_option('--git-describe', dest='git_describe', default=None, help='git describe string to be included in header comments') + parser.add_option('--git-branch', dest='git_branch', default=None, help='git branch string to be included in header comments') + (opts, args) = parser.parse_args() + + meta_dir = opts.metadata + if opts.metadata is None: + if os.path.isfile(os.path.join('.', 'genconfig_metadata.tar.gz')): + opts.metadata = 'genconfig_metadata.tar.gz' + elif os.path.isdir(os.path.join('.', 'config-options')): + opts.metadata = '.' + + if opts.metadata is not None and os.path.isdir(opts.metadata): + meta_dir = opts.metadata + metadata_src_text = 'Using metadata directory: %r' % meta_dir + elif opts.metadata is not None and os.path.isfile(opts.metadata) and tarfile.is_tarfile(opts.metadata): + meta_dir = get_auto_delete_tempdir() + tar = tarfile.open(name=opts.metadata, mode='r:*') + tar.extractall(path=meta_dir) + metadata_src_text = 'Using metadata tar file %r, unpacked to directory: %r' % (opts.metadata, meta_dir) + else: + raise Exception('metadata source must be a directory or a tar.gz file') + + scan_helper_snippets(os.path.join(meta_dir, 'helper-snippets')) + scan_use_defs(os.path.join(meta_dir, 'config-options')) + scan_opt_defs(os.path.join(meta_dir, 'feature-options')) + scan_use_tags() + scan_tags_meta(os.path.join(meta_dir, 'tags.yaml')) + print('%s, scanned %d DUK_OPT_xxx, %d DUK_USE_XXX, %d helper snippets' % \ + (metadata_src_text, len(opt_defs.keys()), len(use_defs.keys()), len(helper_snippets))) + #print('Tags: %r' % use_tags_list) + + if len(args) == 0: + raise Exception('missing command') + cmd = args[0] + + # Compatibility with Duktape 1.3 + if cmd == 'autodetect-header': + cmd = 'duk-config-header' + if cmd == 'barebones-header': + cmd = 'duk-config-header' + + if cmd == 'duk-config-header': + # Generate a duk_config.h header with platform, compiler, and + # architecture either autodetected (default) or specified by + # user. Support for autogenerated DUK_OPT_xxx flags is also + # selected by user. + result = generate_duk_config_header(opts, meta_dir) + with open(opts.output, 'wb') as f: + f.write(result) + elif cmd == 'feature-documentation': + result = generate_feature_option_documentation(opts) + with open(opts.output, 'wb') as f: + f.write(result) + elif cmd == 'config-documentation': + result = generate_config_option_documentation(opts) + with open(opts.output, 'wb') as f: + f.write(result) + else: + raise Exception('invalid command: %r' % cmd) + +if __name__ == '__main__': + main() diff --git a/src/civetweb/src/third_party/duktape-1.5.2/config/genconfig_metadata.tar.gz b/src/civetweb/src/third_party/duktape-1.5.2/config/genconfig_metadata.tar.gz new file mode 100644 index 000000000..c34b2ef10 Binary files /dev/null and b/src/civetweb/src/third_party/duktape-1.5.2/config/genconfig_metadata.tar.gz differ diff --git a/src/civetweb/src/third_party/duktape-1.5.2/debugger/Makefile b/src/civetweb/src/third_party/duktape-1.5.2/debugger/Makefile new file mode 100644 index 000000000..50740b549 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/debugger/Makefile @@ -0,0 +1,101 @@ +NODE:=$(shell which nodejs node | head -1) +DUKLUV:=dukluv + +# Try to get a useful default --source-dirs which works both in the Duktape +# repo and in the distributable. We don't want to add '..' because it would +# scan a lot of undesired files in the Duktape repo (e.g. test262 testcases). +ifeq ($(wildcard ../tests/ecmascript/*.js),) +SOURCEDIRS:=../ +else +SOURCEDIRS:=../tests/ecmascript +endif + +.PHONY: all +all: run + +.PHONY: run +run: node_modules static/socket.io-1.2.0.js static/jquery-1.11.1.min.js static/reset.css static/jquery-ui.min.js static/jquery-ui.min.css static/images + "$(NODE)" duk_debug.js --source-dirs=$(SOURCEDIRS) + +rundebug: node_modules static/socket.io-1.2.0.js static/jquery-1.11.1.min.js static/reset.css static/jquery-ui.min.js static/jquery-ui.min.css static/images + "$(NODE)" duk_debug.js --source-dirs=$(SOURCEDIRS) --verbose --log-messages /tmp/dukdebug-messages --dump-debug-pretty /tmp/dukdebug-pretty + +.PHONY: runproxynodejs +runproxynodejs: node_modules static/socket.io-1.2.0.js static/jquery-1.11.1.min.js static/reset.css static/jquery-ui.min.js static/jquery-ui.min.css static/images + @echo "Running Node.js based debug proxy" + "$(NODE)" duk_debug.js --json-proxy + +.PHONY: runproxydukluv +runproxydukluv: duk_debug_meta.json + @echo "Running Dukluv based debug proxy (you may need to edit DUKLUV in the Makefile)" + "$(DUKLUV)" duk_debug_proxy.js --log-level 2 --metadata duk_debug_meta.json --readable-numbers + +.PHONY: runproxy +runproxy: runproxydukluv + +.PHONY: clean +clean: + @rm -f static/socket.io-1.2.0.js + @rm -f static/jquery-1.11.1.min.js + @rm -f static/jquery.syntaxhighlighter.min.js + @rm -f static/jquery.snippet.min.js + @rm -f static/jquery.snippet.min.css + @rm -f static/prefixfree.min.js + @rm -f static/reset.css + @rm -f static/jquery-ui.min.js + @rm -f static/jquery-ui.min.css + @rm -rf static/images + @rm -f jquery-ui-1.11.2.zip + @rm -rf jquery-ui-1.11.2 + @rm -rf node_modules + @rm -f duk_debug_meta.json + +node_modules: + npm install + +duk_debug_meta.json: + python merge_debug_meta.py --output $@ \ + --class-names duk_classnames.yaml \ + --opcodes duk_opcodes.yaml \ + --debug-commands duk_debugcommands.yaml \ + --debug-errors duk_debugerrors.yaml + +static/socket.io-1.2.0.js: + wget -O $@ http://cdn.socket.io/socket.io-1.2.0.js + +static/jquery-1.11.1.min.js: + wget -O $@ http://code.jquery.com/jquery-1.11.1.min.js + +# http://balupton.github.io/jquery-syntaxhighlighter/demo/ +static/jquery.syntaxhighlighter.min.js: + wget -O $@ http://balupton.github.com/jquery-syntaxhighlighter/scripts/jquery.syntaxhighlighter.min.js + +# http://steamdev.com/snippet/ +static/jquery.snippet.min.js: + wget -O $@ http://steamdev.com/snippet/js/jquery.snippet.min.js +static/jquery.snippet.min.css: + wget -O $@ http://steamdev.com/snippet/css/jquery.snippet.min.css + +# http://prismjs.com/ +# http://prismjs.com/plugins/line-highlight/ +# +# XXX: prism download manually? + +# https://raw.github.com/LeaVerou/prefixfree/gh-pages/prefixfree.min.js +static/prefixfree.min.js: + wget -O $@ https://raw.github.com/LeaVerou/prefixfree/gh-pages/prefixfree.min.js + +# http://meyerweb.com/eric/tools/css/reset/ +static/reset.css: + wget -O $@ http://meyerweb.com/eric/tools/css/reset/reset.css + +jquery-ui-1.11.2.zip: + wget -O $@ http://jqueryui.com/resources/download/jquery-ui-1.11.2.zip +jquery-ui-1.11.2: jquery-ui-1.11.2.zip + unzip $< +static/jquery-ui.min.js: jquery-ui-1.11.2 + cp jquery-ui-1.11.2/jquery-ui.min.js $@ +static/jquery-ui.min.css: jquery-ui-1.11.2 + cp jquery-ui-1.11.2/jquery-ui.min.css $@ +static/images: jquery-ui-1.11.2 + cp -r jquery-ui-1.11.2/images static/ diff --git a/src/civetweb/src/third_party/duktape-1.5.2/debugger/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/debugger/README.rst new file mode 100644 index 000000000..f60b3506a --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/debugger/README.rst @@ -0,0 +1,384 @@ +========================================= +Duktape debug client and JSON debug proxy +========================================= + +Overview +======== + +Debugger web UI which connects to the Duktape command line tool or any other +target supporting the example TCP transport (``examples/debug-trans-socket``) +on Unix and Windows. + +Also provides a JSON debug proxy with a JSON mapping for the Duktape debug +protocol. + +For detailed documentation of the debugger internals, see `debugger.rst`__. + +__ https://github.com/svaarala/duktape/blob/master/doc/debugger.rst + +Using the debugger web UI +========================= + +Some prerequisites: + +* You'll need Node.js v0.10.x or newer. Older Node.js versions don't support + the required packages. + +Compile Duktape command line tool with debugger support (for further options +see http://wiki.duktape.org/FeatureOptions.html): + +* ``DUK_OPT_DEBUGGER_SUPPORT`` + +* ``DUK_OPT_INTERRUPT_COUNTER`` + +* ``DUK_CMDLINE_DEBUGGER_SUPPORT`` + +The source distributable contains a Makefile to build a "duk" command with +debugger support:: + + $ cd + $ make -f Makefile.dukdebug + +The Duktape Git repo "duk" target has debugger support enabled by default:: + + $ make clean duk + +Start Duktape command line tool so that it waits for a debugger connection:: + + # For now we need to be in the directory containing the source files + # executed so that the 'fileName' properties of functions will match + # that on the debug client. + + # Using source distributable + $ cd + $ ./duk --debugger mandel.js + + # Using Duktape Git repo + $ cd /tests/ecmascript/ + $ ../../duk --debugger test-dev-mandel2-func.js + +Start the web UI:: + + # Must be in 'debugger' directory. + + $ cd debugger/ + $ make # runs 'node duk_debug.js' + +Once the required packages are installed, the NodeJS debug client will be +up and running. Open the following in your browser and start debugging: + +* http://localhost:9092/ + +The debug client automatically attaches to the debug target on startup. +If you start the debug target later, you'll need to click "Attach" in the +web UI. + +Using the JSON debug proxy +========================== + +There are two JSON debug proxy implementations: one implemented in DukLuv +and another in Node.js. + +DukLuv JSON proxy +----------------- + +DukLuv (https://github.com/creationix/dukluv) is a small and portable event +loop based on LibUV and Duktape with MIT license (like Duktape). As such it's +easy to embed in a custom debug client: you just include the DukLuv executable +and the JSON proxy source file in your debug client. + +Install DukLuv: + +* Ensure ``cmake`` is installed + +* ``git clone https://github.com/creationix/dukluv.git`` + +* ``git submodule init; git submodule update`` + +* ``make`` + +* Binary should appear in: + + - ``./build/dukluv`` on Linux + + - ``.\build\Debug\dukluv.exe`` on Windows + +Run the proxy:: + + # Using Makefile; autogenerates duk_debug_meta.json + # (You may need to edit DUKLUV in Makefile to point to your DukLuv) + $ make runproxydukluv + + # Manually: see "dukluv duk_debug_proxy.js --help" for help + $ .../path/to/dukluv duk_debug_proxy.js + +Start Duktape command line (or whatever your target is):: + + $ cd /tests/ecmascript/ + $ ../../duk --debugger test-dev-mandel2-func.js + +Now connect to the proxy using e.g. telnet:: + + $ telnet localhost 9093 + +The proxy will then connect to the target and you can start issuing commands:: + + $ telnet localhost 9093 + Trying 127.0.0.1... + Connected to localhost. + Escape character is '^]'. + {"notify":"_TargetConnecting","args":["127.0.0.1",9091]} + {"notify":"_TargetConnected","args":["1 10499 v1.4.0-140-gc9a6c7c duk command built from Duktape repo"]} + {"notify":"Status","command":1,"args":[1,"test-dev-mandel2-func.js","global",58,0]} + {"request":"BasicInfo"} + {"reply":true,"args":[10499,"v1.4.0-140-gc9a6c7c","duk command built from Duktape repo",1]} + {"request":"Eval","args":["print('Hello world!'); 123;"]} + {"notify":"Print","command":2,"args":["Hello world!\n"]} + {"reply":true,"args":[0,{"type":"number","data":"405ec00000000000"}]} + [...] + +The proxy log provides dumps both JSON and dvalue binary traffic which is +quite useful in development:: + + $ make runproxydukluv + Running Dukluv based debug proxy + "dukluv" duk_debug_proxy.js --log-level 2 --metadata duk_debug_meta.json + 2016-02-17T13:59:42.308Z INF Proxy: Read proxy metadata from duk_debug_meta.json + 2016-02-17T13:59:42.325Z INF Proxy: Listening for incoming JSON debug connection on 0.0.0.0:9093, target is 127.0.0.1:9091 + 2016-02-17T13:59:47.994Z INF Proxy: JSON proxy client connected + 2016-02-17T13:59:47.994Z INF Proxy: Connecting to debug target at 127.0.0.1:9091 + 2016-02-17T13:59:47.994Z INF Proxy: PROXY --> CLIENT: {"notify":"_TargetConnecting","args":["127.0.0.1",9091]} + 2016-02-17T13:59:47.994Z INF Proxy: Connected to debug target at 127.0.0.1:9091 + 2016-02-17T13:59:48.003Z INF Proxy: PROXY --> CLIENT: {"notify":"_TargetConnected","args":["1 10499 v1.4.0-140-gc9a6c7c duk command built from Duktape repo"]} + 2016-02-17T13:59:48.003Z INF Proxy: Target handshake: {"line":"1 10499 v1.4.0-140-gc9a6c7c duk command built from Duktape repo","protocolVersion":1,"text":"10499 v1.4.0-140-gc9a6c7c duk command built from Duktape repo","dukVersion":"1","dukGitDescribe":"10499","targetString":"v1.4.0-140-gc9a6c7c"} + 2016-02-17T13:59:48.151Z INF Proxy: PROXY <-- TARGET: |04| + 2016-02-17T13:59:48.152Z INF Proxy: PROXY <-- TARGET: |81| + 2016-02-17T13:59:48.152Z INF Proxy: PROXY <-- TARGET: |81| + 2016-02-17T13:59:48.160Z INF Proxy: PROXY <-- TARGET: |78746573742d6465762d6d616e64656c322d66756e632e6a73| + 2016-02-17T13:59:48.161Z INF Proxy: PROXY <-- TARGET: |66676c6f62616c| + 2016-02-17T13:59:48.165Z INF Proxy: PROXY <-- TARGET: |ba| + 2016-02-17T13:59:48.165Z INF Proxy: PROXY <-- TARGET: |80| + 2016-02-17T13:59:48.165Z INF Proxy: PROXY <-- TARGET: |00| + 2016-02-17T13:59:48.165Z INF Proxy: PROXY --> CLIENT: {"notify":"Status","command":1,"args":[1,"test-dev-mandel2-func.js","global",58,0]} + 2016-02-17T13:59:51.289Z INF Proxy: PROXY <-- CLIENT: {"request":"BasicInfo"} + 2016-02-17T13:59:51.289Z INF Proxy: PROXY --> TARGET: |01| + 2016-02-17T13:59:51.289Z INF Proxy: PROXY --> TARGET: |90| + 2016-02-17T13:59:51.289Z INF Proxy: PROXY --> TARGET: |00| + 2016-02-17T13:59:51.291Z INF Proxy: PROXY <-- TARGET: |02| + 2016-02-17T13:59:51.291Z INF Proxy: PROXY <-- TARGET: |e903| + 2016-02-17T13:59:51.292Z INF Proxy: PROXY <-- TARGET: |7376312e342e302d3134302d6763396136633763| + 2016-02-17T13:59:51.293Z INF Proxy: PROXY <-- TARGET: |12002364756b20636f6d6d616e64206275696c742066726f6d2044756b74617065207265706f| + 2016-02-17T13:59:51.293Z INF Proxy: PROXY <-- TARGET: |81| + 2016-02-17T13:59:51.293Z INF Proxy: PROXY <-- TARGET: |00| + 2016-02-17T13:59:51.293Z INF Proxy: PROXY --> CLIENT: {"reply":true,"args":[10499,"v1.4.0-140-gc9a6c7c","duk command built from Duktape repo",1]} + 2016-02-17T14:00:06.105Z INF Proxy: PROXY <-- CLIENT: {"request":"Eval","args":["print('Hello world!'); 123;"]} + 2016-02-17T14:00:06.105Z INF Proxy: PROXY --> TARGET: |01| + 2016-02-17T14:00:06.105Z INF Proxy: PROXY --> TARGET: |9e| + 2016-02-17T14:00:06.105Z INF Proxy: PROXY --> TARGET: |7b7072696e74282748656c6c6f20776f726c642127293b203132333b| + 2016-02-17T14:00:06.105Z INF Proxy: PROXY --> TARGET: |00| + 2016-02-17T14:00:06.167Z INF Proxy: PROXY <-- TARGET: |04| + 2016-02-17T14:00:06.167Z INF Proxy: PROXY <-- TARGET: |82| + 2016-02-17T14:00:06.167Z INF Proxy: PROXY <-- TARGET: |6d48656c6c6f20776f726c64210a| + 2016-02-17T14:00:06.168Z INF Proxy: PROXY <-- TARGET: |00| + 2016-02-17T14:00:06.168Z INF Proxy: PROXY --> CLIENT: {"notify":"Print","command":2,"args":["Hello world!\n"]} + 2016-02-17T14:00:06.171Z INF Proxy: PROXY <-- TARGET: |02| + 2016-02-17T14:00:06.171Z INF Proxy: PROXY <-- TARGET: |80| + 2016-02-17T14:00:06.173Z INF Proxy: PROXY <-- TARGET: |1a405ec00000000000| + 2016-02-17T14:00:06.173Z INF Proxy: PROXY <-- TARGET: |00| + 2016-02-17T14:00:06.174Z INF Proxy: PROXY --> CLIENT: {"reply":true,"args":[0,{"type":"number","data":"405ec00000000000"}]} + [...] + +Node.js JSON proxy +------------------ + +A Node.js-based JSON debug proxy is also provided by ``duk_debug.js``:: + + # Same prerequisites as for running the debug client + $ make runproxynodejs + +Start Duktape command line (or whatever your target is):: + + $ cd /tests/ecmascript/ + $ ../../duk --debugger test-dev-mandel2-func.js + +You can then connect to localhost:9093 and interact with the proxy. +Here's an example session using telnet and manually typed in commands +The ``-->`` (send) and ``<--`` (receiver) markers have been added for +readability and are not part of the stream:: + + $ telnet localhost 9093 + Trying 127.0.0.1... + Connected to localhost. + Escape character is '^]'. + <-- {"notify":"_TargetConnected","args":["1 10199 v1.1.0-275-gbd4d610-dirty duk command built from Duktape repo"]} + <-- {"notify":"Status","command":1,"args":[1,"test-dev-mandel2-func.js","global",58,0]} + --> {"request":"BasicInfo"} + <-- {"reply":true,"args":[10199,"v1.1.0-275-gbd4d610-dirty","duk command built from Duktape repo",1]} + --> {"request":"Eval", "args":[ "print(Math.PI)" ]} + <-- {"notify":"Print","command":2,"args":["3.141592653589793\n"]} + <-- {"reply":true,"args":[0,{"type":"undefined"}]} + --> {"request":"Resume"} + <-- {"reply":true,"args":[]} + <-- {"notify":"Status","command":1,"args":[0,"test-dev-mandel2-func.js","global",58,0]} + <-- {"notify":"Status","command":1,"args":[0,"test-dev-mandel2-func.js","global",58,0]} + <-- {"notify":"Print","command":2,"args":["................................................................................\n"]} + <-- {"notify":"Print","command":2,"args":["................................................................................\n"]} + <-- {"notify":"Print","command":2,"args":["................................................................................\n"]} + [...] + <-- {"notify":"_Disconnecting"} + +A telnet connection allows you to experiment with debug commands by simply +copy-pasting debug commands to the telnet session. This is useful even if +you decide to implement the binary protocol directly. + +The debug target used by the proxy can be configured with ``duk_debug.js`` +command line options. + +Source search path +================== + +The NodeJS debug client needs to be able to find source code files matching +code running on the target ("duk" command line). **The filenames used on the +target and on the debug client must match exactly**, because e.g. breakpoints +are targeted based on the 'fileName' property of Function objects. + +The search path can be set using the ``--source-dirs`` option given to +``duk_debug.js``, with the default search paths including only +``../tests/ecmascript/``. + +The default search path means that if a function on the target has fileName +``foo/bar.js`` it would be loaded from (relative to the duk_debug.js working +directory, ``debugger/``):: + + ../tests/ecmascript/foo/bar.js + +Similarly, if the filesystem contained:: + + ../tests/ecmascript/baz/quux.js + +the web UI dropdown would show ``baz/quux.js``. If you selected that file +and added a breakpoint, the breakpoint fileName sent to the debug target +would be ``baz/quux.js``. + +.. note:: There's much to improve in the search path. For instance, it'd + be nice to add a certain path to search but exclude files based + on paths and patterns, etc. + +Architecture +============ + +:: + + +-------------------+ + | Web browser | [debug UI] + +-------------------+ + | + | http (port 9092) + | socket.io + v + +-------------------+ + | duk_debug.js | [debug client] + +-------------------+ + | /\ + | || + +----------||---- [example tcp transport] (port 9091) + | || (application provides concrete transport) + | || + | ||---- [debug protocol stream] + | || (between debug client and Duktape) + | || + + - - | - - - - -|| - - + + : v || : +  : +-------------||-+ : [target] + : | application || | : + : +-------------||-+ : + : ^ || : + : | || : [debug API] + : +----------||-------- debug transport callbacks + : | || : (read, write, peek, read/write flush) + : | || : implemented by application + : | \/ : + : +----------------+ : + : | Duktape | : + : +----------------+ : + + - - - - - - - - - - - + + +The debug transport is application specific: + +* Duktape command line ("duk") and this debug client use an **example** TCP + transport as a concrete example. + +* It is entirely up to the application to come up with the most suitable + transport for its environment. Different mechanisms will be needed for + Wi-Fi, serial, etc. + +The debug protocol running inside the transport is transport independent: + +* The debug protocol is documented in ``doc/debugger.rst``. + +* This debug client provides further concrete examples and clarifications + on how the protocol can be used. + +Using a custom transport +======================== + +Quite possibly your target device cannot use the example TCP transport and +you need to implement your own transport. You'll need to implement your +custom transport both for the target device and for the debug client. + +Target device +------------- + +Implement the debug transport callbacks needed by ``duk_debugger_attach()``. + +See ``doc/debugger.rst`` for details and ``examples/debug-trans-socket`` +for example running code for a TCP transport. + +Debug client alternative 1: duk_debug.js + custom TCP proxy +----------------------------------------------------------- + +If you don't want to change ``duk_debug.js`` you can implement a TCP proxy +which accepts a TCP connection from ``duk_debug.js`` and then uses your +custom transport to talk to the target:: + + +--------------+ TCP +-------+ custom +--------+ + | duk_debug.js | ------> | proxy | ---------> | target | + +--------------+ +-------+ +--------+ + +This is a straightforward option and a proxy can be used with other debug +clients too (perhaps custom scripts talking to the target etc). + +You could also use netcat and implement your proxy so that it talks to +``duk_debug.js`` using stdin/stdout. + +Debug client alternative 2: duk_debug.js + custom NodeJS stream +--------------------------------------------------------------- + +To make ``duk_debug.js`` use a custom transport you need to: + +* Implement your own transport as NodeJS stream. You can add it directly to + ``duk_debug.js`` but it's probably easiest to use a separate module so that + the diff to ``duk_debug.js`` stays minimal. + +* Change ``duk_debug.js`` to use the custom transport instead of a TCP + stream. Search for "CUSTOMTRANSPORT" in ``duk_debug.js``. + +See: + +* http://nodejs.org/api/stream.html + +* https://github.com/substack/stream-handbook + +Debug client alternative 3: custom debug client +----------------------------------------------- + +You can also implement your own debug client and debug UI with support for +your custom transport. + +You'll also need to implement the client part of the Duktape debugger +protocol. See ``doc/debugger.rst`` for the specification and ``duk_debug.js`` +for example running code which should illustrate the protocol in more detail. + +The JSON debug proxy allows you to implement a debug client without needing +to implement the Duktape binary debug protocol. The JSON protocol provides +a roughly 1:1 mapping to the binary protocol but with an easier syntax. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_classnames.yaml b/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_classnames.yaml new file mode 100644 index 000000000..9fd5098a5 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_classnames.yaml @@ -0,0 +1,32 @@ +# Must match C header. +class_names: + - unused + - Arguments + - Array + - Boolean + - Date + - Error + - Function + - JSON + - Math + - Number + - Object + - RegExp + - String + - global + - ObjEnv + - DecEnv + - Buffer + - Pointer + - Thread + - ArrayBuffer + - DataView + - Int8Array + - Uint8Array + - Uint8ClampedArray + - Int16Array + - Uint16Array + - Int32Array + - Uint32Array + - Float32Array + - Float64Array diff --git a/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debug.js b/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debug.js new file mode 100644 index 000000000..ab0623fa7 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debug.js @@ -0,0 +1,2473 @@ +/* + * Minimal debug web console for Duktape command line tool + * + * See debugger/README.rst. + * + * The web UI socket.io communication can easily become a bottleneck and + * it's important to ensure that the web UI remains responsive. Basic rate + * limiting mechanisms (token buckets, suppressing identical messages, etc) + * are used here now. Ideally the web UI would pull data on its own terms + * which would provide natural rate limiting. + * + * Promises are used to structure callback chains. + * + * https://github.com/petkaantonov/bluebird + * https://github.com/petkaantonov/bluebird/blob/master/API.md + * https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns + */ + +var Promise = require('bluebird'); +var events = require('events'); +var stream = require('stream'); +var path = require('path'); +var fs = require('fs'); +var net = require('net'); +var byline = require('byline'); +var util = require('util'); +var readline = require('readline'); +var sprintf = require('sprintf').sprintf; +var utf8 = require('utf8'); +var wrench = require('wrench'); // https://github.com/ryanmcgrath/wrench-js +var yaml = require('yamljs'); + +// Command line options (defaults here, overwritten if necessary) +var optTargetHost = '127.0.0.1'; +var optTargetPort = 9091; +var optHttpPort = 9092; +var optJsonProxyPort = 9093; +var optJsonProxy = false; +var optSourceSearchDirs = [ '../tests/ecmascript' ]; +var optDumpDebugRead = null; +var optDumpDebugWrite = null; +var optDumpDebugPretty = null; +var optLogMessages = false; + +// Constants +var UI_MESSAGE_CLIPLEN = 128; +var LOCALS_CLIPLEN = 64; +var EVAL_CLIPLEN = 4096; +var GETVAR_CLIPLEN = 4096; + +// Commands initiated by Duktape +var CMD_STATUS = 0x01; +var CMD_PRINT = 0x02; +var CMD_ALERT = 0x03; +var CMD_LOG = 0x04; +var CMD_THROW = 0x05; +var CMD_DETACHING = 0x06; + +// Commands initiated by the debug client (= us) +var CMD_BASICINFO = 0x10; +var CMD_TRIGGERSTATUS = 0x11; +var CMD_PAUSE = 0x12; +var CMD_RESUME = 0x13; +var CMD_STEPINTO = 0x14; +var CMD_STEPOVER = 0x15; +var CMD_STEPOUT = 0x16; +var CMD_LISTBREAK = 0x17; +var CMD_ADDBREAK = 0x18; +var CMD_DELBREAK = 0x19; +var CMD_GETVAR = 0x1a; +var CMD_PUTVAR = 0x1b; +var CMD_GETCALLSTACK = 0x1c; +var CMD_GETLOCALS = 0x1d; +var CMD_EVAL = 0x1e; +var CMD_DETACH = 0x1f; +var CMD_DUMPHEAP = 0x20; +var CMD_GETBYTECODE = 0x21; + +// Errors +var ERR_UNKNOWN = 0x00; +var ERR_UNSUPPORTED = 0x01; +var ERR_TOOMANY = 0x02; +var ERR_NOTFOUND = 0x03; + +// Marker objects for special protocol values +var DVAL_EOM = { type: 'eom' }; +var DVAL_REQ = { type: 'req' }; +var DVAL_REP = { type: 'rep' }; +var DVAL_ERR = { type: 'err' }; +var DVAL_NFY = { type: 'nfy' }; + +// String map for commands (debug dumping). A single map works (instead of +// separate maps for each direction) because command numbers don't currently +// overlap. So merge the YAML metadata. +var debugCommandMeta = yaml.load('duk_debugcommands.yaml'); +var debugCommandNames = []; // list of command names, merged client/target +debugCommandMeta.target_commands.forEach(function (k, i) { + debugCommandNames[i] = k; +}); +debugCommandMeta.client_commands.forEach(function (k, i) { // override + debugCommandNames[i] = k; +}); +var debugCommandNumbers = {}; // map from (merged) command name to number +debugCommandNames.forEach(function (k, i) { + debugCommandNumbers[k] = i; +}); + +// Duktape heaphdr type constants, must match C headers +var DUK_HTYPE_STRING = 1; +var DUK_HTYPE_OBJECT = 2; +var DUK_HTYPE_BUFFER = 3; + +// Duktape internal class numbers, must match C headers +var dukClassNameMeta = yaml.load('duk_classnames.yaml'); +var dukClassNames = dukClassNameMeta.class_names; + +// Bytecode opcode/extraop metadata +var dukOpcodes = yaml.load('duk_opcodes.yaml'); +if (dukOpcodes.opcodes.length != 64) { + throw new Error('opcode metadata length incorrect'); +} +if (dukOpcodes.extra.length != 256) { + throw new Error('extraop metadata length incorrect'); +} + +/* + * Miscellaneous helpers + */ + +var nybbles = '0123456789abcdef'; + +/* Convert a buffer into a string using Unicode codepoints U+0000...U+00FF. + * This is the NodeJS 'binary' encoding, but since it's being deprecated, + * reimplement it here. We need to avoid parsing strings as e.g. UTF-8: + * although Duktape strings are usually UTF-8/CESU-8 that's not always the + * case, e.g. for internal strings. Buffer values are also represented as + * strings in the debug protocol, so we must deal accurately with arbitrary + * byte arrays. + */ +function bufferToDebugString(buf) { + var cp = []; + var i, n; + +/* + // This fails with "RangeError: Maximum call stack size exceeded" for some + // reason, so use a much slower variant. + + for (i = 0, n = buf.length; i < n; i++) { + cp[i] = buf[i]; + } + + return String.fromCharCode.apply(String, cp); +*/ + + for (i = 0, n = buf.length; i < n; i++) { + cp[i] = String.fromCharCode(buf[i]); + } + + return cp.join(''); +} + +/* Write a string into a buffer interpreting codepoints U+0000...U+00FF + * as bytes. Drop higher bits. + */ +function writeDebugStringToBuffer(str, buf, off) { + var i, n; + + for (i = 0, n = str.length; i < n; i++) { + buf[off + i] = str.charCodeAt(i) & 0xff; // truncate higher bits + } +} + +/* Encode an ordinary Unicode string into a dvalue compatible format, i.e. + * into a byte array represented as codepoints U+0000...U+00FF. Concretely, + * encode with UTF-8 and then represent the bytes with U+0000...U+00FF. + */ +function stringToDebugString(str) { + return utf8.encode(str); +} + +/* Pretty print a dvalue. Useful for dumping etc. */ +function prettyDebugValue(x) { + if (typeof x === 'object' && x !== null) { + if (x.type === 'eom') { + return 'EOM'; + } else if (x.type === 'req') { + return 'REQ'; + } else if (x.type === 'rep') { + return 'REP'; + } else if (x.type === 'err') { + return 'ERR'; + } else if (x.type === 'nfy') { + return 'NFY'; + } + } + return JSON.stringify(x); +} + +/* Pretty print a number for UI usage. Types and values should be easy to + * read and typing should be obvious. For numbers, support Infinity, NaN, + * and signed zeroes properly. + */ +function prettyUiNumber(x) { + if (x === 1 / 0) { return 'Infinity'; } + if (x === -1 / 0) { return '-Infinity'; } + if (Number.isNaN(x)) { return 'NaN'; } + if (x === 0 && 1 / x > 0) { return '0'; } + if (x === 0 && 1 / x < 0) { return '-0'; } + return x.toString(); +} + +/* Pretty print a dvalue string (bytes represented as U+0000...U+00FF) + * for UI usage. Try UTF-8 decoding to get a nice Unicode string (JSON + * encoded) but if that fails, ensure that bytes are encoded transparently. + * The result is a quoted string with a special quote marker for a "raw" + * string when UTF-8 decoding fails. Very long strings are optionally + * clipped. + */ +function prettyUiString(x, cliplen) { + var ret; + + if (typeof x !== 'string') { + throw new Error('invalid input to prettyUiString: ' + typeof x); + } + try { + // Here utf8.decode() is better than decoding using NodeJS buffer + // operations because we want strict UTF-8 interpretation. + ret = JSON.stringify(utf8.decode(x)); + } catch (e) { + // When we fall back to representing bytes, indicate that the string + // is "raw" with a 'r"' prefix (a somewhat arbitrary convention). + // U+0022 = ", U+0027 = ' + ret = 'r"' + x.replace(/[\u0022\u0027\u0000-\u001f\u0080-\uffff]/g, function (match) { + var cp = match.charCodeAt(0); + return '\\x' + nybbles[(cp >> 4) & 0x0f] + nybbles[cp & 0x0f]; + }) + '"'; + } + + if (cliplen && ret.length > cliplen) { + ret = ret.substring(0, cliplen) + '...'; // trailing '"' intentionally missing + } + return ret; +} + +/* Pretty print a dvalue string (bytes represented as U+0000...U+00FF) + * for UI usage without quotes. + */ +function prettyUiStringUnquoted(x, cliplen) { + var ret; + + if (typeof x !== 'string') { + throw new Error('invalid input to prettyUiStringUnquoted: ' + typeof x); + } + + try { + // Here utf8.decode() is better than decoding using NodeJS buffer + // operations because we want strict UTF-8 interpretation. + + // XXX: unprintable characters etc? In some UI cases we'd want to + // e.g. escape newlines and in others not. + ret = utf8.decode(x); + } catch (e) { + // For the unquoted version we don't need to escape single or double + // quotes. + ret = x.replace(/[\u0000-\u001f\u0080-\uffff]/g, function (match) { + var cp = match.charCodeAt(0); + return '\\x' + nybbles[(cp >> 4) & 0x0f] + nybbles[cp & 0x0f]; + }); + } + + if (cliplen && ret.length > cliplen) { + ret = ret.substring(0, cliplen) + '...'; + } + return ret; +} + +/* Pretty print a dvalue for UI usage. Everything comes out as a ready-to-use + * string. + * + * XXX: Currently the debug client formats all values for UI use. A better + * solution would be to pass values in typed form and let the UI format them, + * so that styling etc. could take typing into account. + */ +function prettyUiDebugValue(x, cliplen) { + if (typeof x === 'object' && x !== null) { + // Note: typeof null === 'object', so null special case explicitly + if (x.type === 'eom') { + return 'EOM'; + } else if (x.type === 'req') { + return 'REQ'; + } else if (x.type === 'rep') { + return 'REP'; + } else if (x.type === 'err') { + return 'ERR'; + } else if (x.type === 'nfy') { + return 'NFY'; + } else if (x.type === 'unused') { + return 'unused'; + } else if (x.type === 'undefined') { + return 'undefined'; + } else if (x.type === 'buffer') { + return '|' + x.data + '|'; + } else if (x.type === 'object') { + return '[object ' + (dukClassNames[x.class] || ('class ' + x.class)) + ' ' + x.pointer + ']'; + } else if (x.type === 'pointer') { + return ''; + } else if (x.type === 'lightfunc') { + return ''; + } else if (x.type === 'number') { + // duk_tval number, any IEEE double + var tmp = new Buffer(x.data, 'hex'); // decode into hex + var val = tmp.readDoubleBE(0); // big endian ieee double + return prettyUiNumber(val); + } + } else if (x === null) { + return 'null'; + } else if (typeof x === 'boolean') { + return x ? 'true' : 'false'; + } else if (typeof x === 'string') { + return prettyUiString(x, cliplen); + } else if (typeof x === 'number') { + // Debug protocol integer + return prettyUiNumber(x); + } + + // We shouldn't come here, but if we do, JSON is a reasonable default. + return JSON.stringify(x); +} + +/* Pretty print a debugger message given as an array of parsed dvalues. + * Result should be a pure ASCII one-liner. + */ +function prettyDebugMessage(msg) { + return msg.map(prettyDebugValue).join(' '); +} + +/* Pretty print a debugger command. */ +function prettyDebugCommand(cmd) { + return debugCommandNames[cmd] || String(cmd); +} + +/* Decode and normalize source file contents: UTF-8, tabs to 8, + * CR LF to LF. + */ +function decodeAndNormalizeSource(data) { + var tmp; + var lines, line, repl; + var i, n; + var j, m; + + try { + tmp = data.toString('utf8'); + } catch (e) { + console.log('Failed to UTF-8 decode source file, ignoring: ' + e); + tmp = String(data); + } + + lines = tmp.split(/\r?\n/); + for (i = 0, n = lines.length; i < n; i++) { + line = lines[i]; + if (/\t/.test(line)) { + repl = ''; + for (j = 0, m = line.length; j < m; j++) { + if (line.charAt(j) === '\t') { + repl += ' '; + while ((repl.length % 8) != 0) { + repl += ' '; + } + } else { + repl += line.charAt(j); + } + } + lines[i] = repl; + } + } + + // XXX: normalize last newline (i.e. force a newline if contents don't + // end with a newline)? + + return lines.join('\n'); +} + +/* Token bucket rate limiter for a given callback. Calling code calls + * trigger() to request 'cb' to be called, and the rate limiter ensures + * that 'cb' is not called too often. + */ +function RateLimited(tokens, rate, cb) { + var _this = this; + this.maxTokens = tokens; + this.tokens = this.maxTokens; + this.rate = rate; + this.cb = cb; + this.delayedCb = false; + + // Right now the implementation is setInterval-based, but could also be + // made timerless. There are so few rate limited resources that this + // doesn't matter in practice. + + this.tokenAdder = setInterval(function () { + if (_this.tokens < _this.maxTokens) { + _this.tokens++; + } + if (_this.delayedCb) { + _this.delayedCb = false; + _this.tokens--; + _this.cb(); + } + }, this.rate); +} +RateLimited.prototype.trigger = function () { + if (this.tokens > 0) { + this.tokens--; + this.cb(); + } else { + this.delayedCb = true; + } +}; + +/* + * Source file manager + * + * Scan the list of search directories for Ecmascript source files and + * build an index of them. Provides a mechanism to find a source file + * based on a raw 'fileName' property provided by the debug target, and + * to provide a file list for the web UI. + * + * NOTE: it's tempting to do loose matching for filenames, but this does + * not work in practice. Filenames must match 1:1 with the debug target + * so that e.g. breakpoints assigned based on filenames found from the + * search paths will match 1:1 on the debug target. If this is not the + * case, breakpoints won't work as expected. + */ + +function SourceFileManager(directories) { + this.directories = directories; + this.extensions = { '.js': true, '.jsm': true }; + this.files; +} + +SourceFileManager.prototype.scan = function () { + var _this = this; + var fileMap = {}; // absFn -> true + var files; + + this.directories.forEach(function (dir) { + console.log('Scanning source files: ' + dir); + try { + wrench.readdirSyncRecursive(dir).forEach(function (fn) { + var absFn = path.normalize(path.join(dir, fn)); // './foo/bar.js' -> 'foo/bar.js' + var ent; + + if (fs.existsSync(absFn) && + fs.lstatSync(absFn).isFile() && + _this.extensions[path.extname(fn)]) { + // We want the fileMap to contain the filename relative to + // the search dir root. + fileMap[fn] = true; + } + }); + } catch (e) { + console.log('Failed to scan ' + dir + ': ' + e); + } + }); + + files = Object.keys(fileMap); + files.sort(); + this.files = files; + + console.log('Found ' + files.length + ' source files in ' + this.directories.length + ' search directories'); +}; + +SourceFileManager.prototype.getFiles = function () { + return this.files; +}; + +SourceFileManager.prototype.search = function (fileName) { + var _this = this; + + // Loose matching is tempting but counterproductive: filenames must + // match 1:1 between the debug client and the debug target for e.g. + // breakpoints to work as expected. Note that a breakpoint may be + // assigned by selecting a file from a dropdown populated by scanning + // the filesystem for available sources and there's no way of knowing + // if the debug target uses the exact same name. + + function tryLookup() { + var i, fn, data; + + for (i = 0; i < _this.directories.length; i++) { + fn = path.join(_this.directories[i], fileName); + if (fs.existsSync(fn) && fs.lstatSync(fn).isFile()) { + data = fs.readFileSync(fn); // Raw bytes + return decodeAndNormalizeSource(data); // Unicode string + } + } + return null; + } + + return tryLookup(fileName); +}; + +/* + * Debug protocol parser + * + * The debug protocol parser is an EventEmitter which parses debug messages + * from an input stream and emits 'debug-message' events for completed + * messages ending in an EOM. The parser also provides debug dumping, stream + * logging functionality, and statistics gathering functionality. + * + * This parser is used to parse both incoming and outgoing messages. For + * outgoing messages the only function is to validate and debug dump the + * messages we're about to send. The downside of dumping at this low level + * is that we can't match request and reply/error messages here. + * + * http://www.sitepoint.com/nodejs-events-and-eventemitter/ + */ + +function DebugProtocolParser(inputStream, + protocolVersion, + rawDumpFileName, + textDumpFileName, + textDumpFilePrefix, + hexDumpConsolePrefix, + textDumpConsolePrefix) { + var _this = this; + this.inputStream = inputStream; + this.closed = false; // stream is closed/broken, don't parse anymore + this.bytes = 0; + this.dvalues = 0; + this.messages = 0; + this.requests = 0; + this.prevBytes = 0; + this.bytesPerSec = 0; + this.statsTimer = null; + this.readableNumberValue = true; + + events.EventEmitter.call(this); + + var buf = new Buffer(0); // accumulate data + var msg = []; // accumulated message until EOM + var versionIdentification; + + var statsInterval = 2000; + var statsIntervalSec = statsInterval / 1000; + this.statsTimer = setInterval(function () { + _this.bytesPerSec = (_this.bytes - _this.prevBytes) / statsIntervalSec; + _this.prevBytes = _this.bytes; + _this.emit('stats-update'); + }, statsInterval); + + function consume(n) { + var tmp = new Buffer(buf.length - n); + buf.copy(tmp, 0, n); + buf = tmp; + } + + inputStream.on('data', function (data) { + var i, n, x, v, gotValue, len, t, tmpbuf, verstr; + var prettyMsg; + + if (_this.closed || !_this.inputStream) { + console.log('Ignoring incoming data from closed input stream, len ' + data.length); + return; + } + + _this.bytes += data.length; + if (rawDumpFileName) { + fs.appendFileSync(rawDumpFileName, data); + } + if (hexDumpConsolePrefix) { + console.log(hexDumpConsolePrefix + data.toString('hex')); + } + + buf = Buffer.concat([ buf, data ]); + + // Protocol version handling. When dumping an output stream, the + // caller gives a non-null protocolVersion so we don't read one here. + if (protocolVersion == null) { + if (buf.length > 1024) { + _this.emit('transport-error', 'Parse error (version identification too long), dropping connection'); + _this.close(); + return; + } + + for (i = 0, n = buf.length; i < n; i++) { + if (buf[i] == 0x0a) { + tmpbuf = new Buffer(i); + buf.copy(tmpbuf, 0, 0, i); + consume(i + 1); + verstr = tmpbuf.toString('utf-8'); + t = verstr.split(' '); + protocolVersion = Number(t[0]); + versionIdentification = verstr; + + _this.emit('protocol-version', { + protocolVersion: protocolVersion, + versionIdentification: versionIdentification + }); + break; + } + } + + if (protocolVersion == null) { + // Still waiting for version identification to complete. + return; + } + } + + // Parse complete dvalues (quite inefficient now) by trial parsing. + // Consume a value only when it's fully present in 'buf'. + // See doc/debugger.rst for format description. + + while (buf.length > 0) { + x = buf[0]; + v = undefined; + gotValue = false; // used to flag special values like undefined + + if (x >= 0xc0) { + // 0xc0...0xff: integers 0-16383 + if (buf.length >= 2) { + v = ((x - 0xc0) << 8) + buf[1]; + consume(2); + } + } else if (x >= 0x80) { + // 0x80...0xbf: integers 0-63 + v = x - 0x80; + consume(1); + } else if (x >= 0x60) { + // 0x60...0x7f: strings with length 0-31 + len = x - 0x60; + if (buf.length >= 1 + len) { + v = new Buffer(len); + buf.copy(v, 0, 1, 1 + len); + v = bufferToDebugString(v); + consume(1 + len); + } + } else { + switch (x) { + case 0x00: v = DVAL_EOM; consume(1); break; + case 0x01: v = DVAL_REQ; consume(1); break; + case 0x02: v = DVAL_REP; consume(1); break; + case 0x03: v = DVAL_ERR; consume(1); break; + case 0x04: v = DVAL_NFY; consume(1); break; + case 0x10: // 4-byte signed integer + if (buf.length >= 5) { + v = buf.readInt32BE(1); + consume(5); + } + break; + case 0x11: // 4-byte string + if (buf.length >= 5) { + len = buf.readUInt32BE(1); + if (buf.length >= 5 + len) { + v = new Buffer(len); + buf.copy(v, 0, 5, 5 + len); + v = bufferToDebugString(v); + consume(5 + len); + } + } + break; + case 0x12: // 2-byte string + if (buf.length >= 3) { + len = buf.readUInt16BE(1); + if (buf.length >= 3 + len) { + v = new Buffer(len); + buf.copy(v, 0, 3, 3 + len); + v = bufferToDebugString(v); + consume(3 + len); + } + } + break; + case 0x13: // 4-byte buffer + if (buf.length >= 5) { + len = buf.readUInt32BE(1); + if (buf.length >= 5 + len) { + v = new Buffer(len); + buf.copy(v, 0, 5, 5 + len); + v = { type: 'buffer', data: v.toString('hex') }; + consume(5 + len); + // Value could be a Node.js buffer directly, but + // we prefer all dvalues to be JSON compatible + } + } + break; + case 0x14: // 2-byte buffer + if (buf.length >= 3) { + len = buf.readUInt16BE(1); + if (buf.length >= 3 + len) { + v = new Buffer(len); + buf.copy(v, 0, 3, 3 + len); + v = { type: 'buffer', data: v.toString('hex') }; + consume(3 + len); + // Value could be a Node.js buffer directly, but + // we prefer all dvalues to be JSON compatible + } + } + break; + case 0x15: // unused/none + v = { type: 'unused' }; + consume(1); + break; + case 0x16: // undefined + v = { type: 'undefined' }; + gotValue = true; // indicate 'v' is actually set + consume(1); + break; + case 0x17: // null + v = null; + gotValue = true; // indicate 'v' is actually set + consume(1); + break; + case 0x18: // true + v = true; + consume(1); + break; + case 0x19: // false + v = false; + consume(1); + break; + case 0x1a: // number (IEEE double), big endian + if (buf.length >= 9) { + v = new Buffer(8); + buf.copy(v, 0, 1, 9); + v = { type: 'number', data: v.toString('hex') }; + + if (_this.readableNumberValue) { + // The value key should not be used programmatically, + // it is just there to make the dumps more readable. + v.value = buf.readDoubleBE(1); + } + consume(9); + } + break; + case 0x1b: // object + if (buf.length >= 3) { + len = buf[2]; + if (buf.length >= 3 + len) { + v = new Buffer(len); + buf.copy(v, 0, 3, 3 + len); + v = { type: 'object', 'class': buf[1], pointer: v.toString('hex') }; + consume(3 + len); + } + } + break; + case 0x1c: // pointer + if (buf.length >= 2) { + len = buf[1]; + if (buf.length >= 2 + len) { + v = new Buffer(len); + buf.copy(v, 0, 2, 2 + len); + v = { type: 'pointer', pointer: v.toString('hex') }; + consume(2 + len); + } + } + break; + case 0x1d: // lightfunc + if (buf.length >= 4) { + len = buf[3]; + if (buf.length >= 4 + len) { + v = new Buffer(len); + buf.copy(v, 0, 4, 4 + len); + v = { type: 'lightfunc', flags: buf.readUInt16BE(1), pointer: v.toString('hex') }; + consume(4 + len); + } + } + break; + case 0x1e: // heapptr + if (buf.length >= 2) { + len = buf[1]; + if (buf.length >= 2 + len) { + v = new Buffer(len); + buf.copy(v, 0, 2, 2 + len); + v = { type: 'heapptr', pointer: v.toString('hex') }; + consume(2 + len); + } + } + break; + default: + _this.emit('transport-error', 'Parse error, dropping connection'); + _this.close(); + } + } + + if (typeof v === 'undefined' && !gotValue) { + break; + } + msg.push(v); + _this.dvalues++; + + // Could emit a 'debug-value' event here, but that's not necessary + // because the receiver will just collect statistics which can also + // be done using the finished message. + + if (v === DVAL_EOM) { + _this.messages++; + + if (textDumpFileName || textDumpConsolePrefix) { + prettyMsg = prettyDebugMessage(msg); + if (textDumpFileName) { + fs.appendFileSync(textDumpFileName, (textDumpFilePrefix || '') + prettyMsg + '\n'); + } + if (textDumpConsolePrefix) { + console.log(textDumpConsolePrefix + prettyMsg); + } + } + + _this.emit('debug-message', msg); + msg = []; // new object, old may be in circulation for a while + } + } + }); + + // Not all streams will emit this. + inputStream.on('error', function (err) { + _this.emit('transport-error', err); + _this.close(); + }); + + // Not all streams will emit this. + inputStream.on('close', function () { + _this.close(); + }); +} +DebugProtocolParser.prototype = Object.create(events.EventEmitter.prototype); + +DebugProtocolParser.prototype.close = function () { + // Although the underlying transport may not have a close() or destroy() + // method or even a 'close' event, this method is always available and + // will generate a 'transport-close'. + // + // The caller is responsible for closing the underlying stream if that + // is necessary. + + if (this.closed) { return; } + + this.closed = true; + if (this.statsTimer) { + clearInterval(this.statsTimer); + this.statsTimer = null; + } + this.emit('transport-close'); +}; + +/* + * Debugger output formatting + */ + +function formatDebugValue(v) { + var buf, dec, len; + + // See doc/debugger.rst for format description. + + if (typeof v === 'object' && v !== null) { + // Note: typeof null === 'object', so null special case explicitly + if (v.type === 'eom') { + return new Buffer([ 0x00 ]); + } else if (v.type === 'req') { + return new Buffer([ 0x01 ]); + } else if (v.type === 'rep') { + return new Buffer([ 0x02 ]); + } else if (v.type === 'err') { + return new Buffer([ 0x03 ]); + } else if (v.type === 'nfy') { + return new Buffer([ 0x04 ]); + } else if (v.type === 'unused') { + return new Buffer([ 0x15 ]); + } else if (v.type === 'undefined') { + return new Buffer([ 0x16 ]); + } else if (v.type === 'number') { + dec = new Buffer(v.data, 'hex'); + len = dec.length; + if (len !== 8) { + throw new TypeError('value cannot be converted to dvalue: ' + JSON.stringify(v)); + } + buf = new Buffer(1 + len); + buf[0] = 0x1a; + dec.copy(buf, 1); + return buf; + } else if (v.type === 'buffer') { + dec = new Buffer(v.data, 'hex'); + len = dec.length; + if (len <= 0xffff) { + buf = new Buffer(3 + len); + buf[0] = 0x14; + buf[1] = (len >> 8) & 0xff; + buf[2] = (len >> 0) & 0xff; + dec.copy(buf, 3); + return buf; + } else { + buf = new Buffer(5 + len); + buf[0] = 0x13; + buf[1] = (len >> 24) & 0xff; + buf[2] = (len >> 16) & 0xff; + buf[3] = (len >> 8) & 0xff; + buf[4] = (len >> 0) & 0xff; + dec.copy(buf, 5); + return buf; + } + } else if (v.type === 'object') { + dec = new Buffer(v.pointer, 'hex'); + len = dec.length; + buf = new Buffer(3 + len); + buf[0] = 0x1b; + buf[1] = v.class; + buf[2] = len; + dec.copy(buf, 3); + return buf; + } else if (v.type === 'pointer') { + dec = new Buffer(v.pointer, 'hex'); + len = dec.length; + buf = new Buffer(2 + len); + buf[0] = 0x1c; + buf[1] = len; + dec.copy(buf, 2); + return buf; + } else if (v.type === 'lightfunc') { + dec = new Buffer(v.pointer, 'hex'); + len = dec.length; + buf = new Buffer(4 + len); + buf[0] = 0x1d; + buf[1] = (v.flags >> 8) & 0xff; + buf[2] = v.flags & 0xff; + buf[3] = len; + dec.copy(buf, 4); + return buf; + } else if (v.type === 'heapptr') { + dec = new Buffer(v.pointer, 'hex'); + len = dec.length; + buf = new Buffer(2 + len); + buf[0] = 0x1e; + buf[1] = len; + dec.copy(buf, 2); + return buf; + } + } else if (v === null) { + return new Buffer([ 0x17 ]); + } else if (typeof v === 'boolean') { + return new Buffer([ v ? 0x18 : 0x19 ]); + } else if (typeof v === 'number') { + if (Math.floor(v) === v && /* whole */ + (v !== 0 || 1 / v > 0) && /* not negative zero */ + v >= -0x80000000 && v <= 0x7fffffff) { + // Represented signed 32-bit integers as plain integers. + // Debugger code expects this for all fields that are not + // duk_tval representations (e.g. command numbers and such). + if (v >= 0x00 && v <= 0x3f) { + return new Buffer([ 0x80 + v ]); + } else if (v >= 0x0000 && v <= 0x3fff) { + return new Buffer([ 0xc0 + (v >> 8), v & 0xff ]); + } else if (v >= -0x80000000 && v <= 0x7fffffff) { + return new Buffer([ 0x10, + (v >> 24) & 0xff, + (v >> 16) & 0xff, + (v >> 8) & 0xff, + (v >> 0) & 0xff ]); + } else { + throw new Error('internal error when encoding integer to dvalue: ' + v); + } + } else { + // Represent non-integers as IEEE double dvalues + buf = new Buffer(1 + 8); + buf[0] = 0x1a; + buf.writeDoubleBE(v, 1); + return buf; + } + } else if (typeof v === 'string') { + if (v.length < 0 || v.length > 0xffffffff) { + // Not possible in practice. + throw new TypeError('cannot convert to dvalue, invalid string length: ' + v.length); + } + if (v.length <= 0x1f) { + buf = new Buffer(1 + v.length); + buf[0] = 0x60 + v.length; + writeDebugStringToBuffer(v, buf, 1); + return buf; + } else if (v.length <= 0xffff) { + buf = new Buffer(3 + v.length); + buf[0] = 0x12; + buf[1] = (v.length >> 8) & 0xff; + buf[2] = (v.length >> 0) & 0xff; + writeDebugStringToBuffer(v, buf, 3); + return buf; + } else { + buf = new Buffer(5 + v.length); + buf[0] = 0x11; + buf[1] = (v.length >> 24) & 0xff; + buf[2] = (v.length >> 16) & 0xff; + buf[3] = (v.length >> 8) & 0xff; + buf[4] = (v.length >> 0) & 0xff; + writeDebugStringToBuffer(v, buf, 5); + return buf; + } + } + + // Shouldn't come here. + throw new TypeError('value cannot be converted to dvalue: ' + JSON.stringify(v)); +} + +/* + * Debugger implementation + * + * A debugger instance communicates with the debug target and maintains + * persistent debug state so that the current state can be resent to the + * socket.io client (web UI) if it reconnects. Whenever the debugger state + * is changed an event is generated. The socket.io handler will listen to + * state change events and push the necessary updates to the web UI, often + * in a rate limited fashion or using a client pull to ensure the web UI + * is not overloaded. + * + * The debugger instance assumes that if the debug protocol connection is + * re-established, it is always to the same target. There is no separate + * abstraction for a debugger session. + */ + +function Debugger() { + events.EventEmitter.call(this); + + this.web = null; // web UI singleton + this.targetStream = null; // transport connection to target + this.outputPassThroughStream = null; // dummy passthrough for message dumping + this.inputParser = null; // parser for incoming debug messages + this.outputParser = null; // parser for outgoing debug messages (stats, dumping) + this.protocolVersion = null; + this.dukVersion = null; + this.dukGitDescribe = null; + this.targetInfo = null; + this.attached = false; + this.handshook = false; + this.reqQueue = null; + this.stats = { // stats for current debug connection + rxBytes: 0, rxDvalues: 0, rxMessages: 0, rxBytesPerSec: 0, + txBytes: 0, txDvalues: 0, txMessages: 0, txBytesPerSec: 0 + }; + this.execStatus = { + attached: false, + state: 'detached', + fileName: '', + funcName: '', + line: 0, + pc: 0 + }; + this.breakpoints = []; + this.callstack = []; + this.locals = []; + this.messageLines = []; + this.messageScrollBack = 100; +} +Debugger.prototype = events.EventEmitter.prototype; + +Debugger.prototype.decodeBytecodeFromBuffer = function (buf, consts, funcs) { + var i, j, n, m, ins, pc; + var res = []; + var op, str, args, comments; + + // XXX: add constants inline to preformatted output (e.g. for strings, + // add a short escaped snippet as a comment on the line after the + // compact argument list). + + for (i = 0, n = buf.length; i < n; i += 4) { + pc = i / 4; + + // shift forces unsigned + if (this.endianness === 'little') { + ins = buf.readInt32LE(i) >>> 0; + } else { + ins = buf.readInt32BE(i) >>> 0; + } + + op = dukOpcodes.opcodes[ins & 0x3f]; + if (op.extra) { + op = dukOpcodes.extra[(ins >> 6) & 0xff]; + } + + args = []; + comments = []; + if (op.args) { + for (j = 0, m = op.args.length; j < m; j++) { + switch (op.args[j]) { + case 'A_R': args.push('r' + ((ins >>> 6) & 0xff)); break; + case 'A_RI': args.push('r' + ((ins >>> 6) & 0xff) + '(indirect)'); break; + case 'A_C': args.push('c' + ((ins >>> 6) & 0xff)); break; + case 'A_H': args.push('0x' + ((ins >>> 6) & 0xff).toString(16)); break; + case 'A_I': args.push(((ins >>> 6) & 0xff).toString(10)); break; + case 'A_B': args.push(((ins >>> 6) & 0xff) ? 'true' : 'false'); break; + case 'B_RC': args.push((ins & (1 << 22) ? 'c' : 'r') + ((ins >>> 14) & 0x0ff)); break; + case 'B_R': args.push('r' + ((ins >>> 14) & 0x1ff)); break; + case 'B_RI': args.push('r' + ((ins >>> 14) & 0x1ff) + '(indirect)'); break; + case 'B_C': args.push('c' + ((ins >>> 14) & 0x1ff)); break; + case 'B_H': args.push('0x' + ((ins >>> 14) & 0x1ff).toString(16)); break; + case 'B_I': args.push(((ins >>> 14) & 0x1ff).toString(10)); break; + case 'C_RC': args.push((ins & (1 << 31) ? 'c' : 'r') + ((ins >>> 23) & 0x0ff)); break; + case 'C_R': args.push('r' + ((ins >>> 23) & 0x1ff)); break; + case 'C_RI': args.push('r' + ((ins >>> 23) & 0x1ff) + '(indirect)'); break; + case 'C_C': args.push('c' + ((ins >>> 23) & 0x1ff)); break; + case 'C_H': args.push('0x' + ((ins >>> 23) & 0x1ff).toString(16)); break; + case 'C_I': args.push(((ins >>> 23) & 0x1ff).toString(10)); break; + case 'BC_R': args.push('r' + ((ins >>> 14) & 0x3ffff)); break; + case 'BC_C': args.push('c' + ((ins >>> 14) & 0x3ffff)); break; + case 'BC_H': args.push('0x' + ((ins >>> 14) & 0x3ffff).toString(16)); break; + case 'BC_I': args.push(((ins >>> 14) & 0x3ffff).toString(10)); break; + case 'ABC_H': args.push(((ins >>> 6) & 0x03ffffff).toString(16)); break; + case 'ABC_I': args.push(((ins >>> 6) & 0x03ffffff).toString(10)); break; + case 'BC_LDINT': args.push(((ins >>> 14) & 0x3ffff) - (1 << 17)); break; + case 'BC_LDINTX': args.push(((ins >>> 14) & 0x3ffff) - 0); break; // no bias in LDINTX + case 'ABC_JUMP': { + var pc_add = ((ins >>> 6) & 0x03ffffff) - (1 << 25) + 1; // pc is preincremented before adding + var pc_dst = pc + pc_add; + args.push(pc_dst + ' (' + (pc_add >= 0 ? '+' : '') + pc_add + ')'); + break; + } + default: args.push('?'); break; + } + } + } + if (op.flags) { + for (j = 0, m = op.flags.length; j < m; j++) { + if (ins & op.flags[j].mask) { + comments.push(op.flags[j].name); + } + } + } + + if (args.length > 0) { + str = sprintf('%05d %08x %-10s %s', pc, ins, op.name, args.join(', ')); + } else { + str = sprintf('%05d %08x %-10s', pc, ins, op.name); + } + if (comments.length > 0) { + str = sprintf('%-44s ; %s', str, comments.join(', ')); + } + + res.push({ + str: str, + ins: ins + }); + } + + return res; +}; + +Debugger.prototype.uiMessage = function (type, val) { + var msg; + if (typeof type === 'object') { + msg = type; + } else if (typeof type === 'string') { + msg = { type: type, message: val }; + } else { + throw new TypeError('invalid ui message: ' + type); + } + this.messageLines.push(msg); + while (this.messageLines.length > this.messageScrollBack) { + this.messageLines.shift(); + } + this.emit('ui-message-update'); // just trigger a sync, gets rate limited +}; + +Debugger.prototype.sendRequest = function (msg) { + var _this = this; + return new Promise(function (resolve, reject) { + var dvals = []; + var dval; + var data; + var i; + + if (!_this.attached || !_this.handshook || !_this.reqQueue || !_this.targetStream) { + throw new Error('invalid state for sendRequest'); + } + + for (i = 0; i < msg.length; i++) { + try { + dval = formatDebugValue(msg[i]); + } catch (e) { + console.log('Failed to format dvalue, dropping connection: ' + e); + console.log(e.stack || e); + _this.targetStream.destroy(); + throw new Error('failed to format dvalue'); + } + dvals.push(dval); + } + + data = Buffer.concat(dvals); + + _this.targetStream.write(data); + _this.outputPassThroughStream.write(data); // stats and dumping + + if (optLogMessages) { + console.log('Request ' + prettyDebugCommand(msg[1]) + ': ' + prettyDebugMessage(msg)); + } + + if (!_this.reqQueue) { + throw new Error('no reqQueue'); + } + + _this.reqQueue.push({ + reqMsg: msg, + reqCmd: msg[1], + resolveCb: resolve, + rejectCb: reject + }); + }); +}; + +Debugger.prototype.sendBasicInfoRequest = function () { + var _this = this; + return this.sendRequest([ DVAL_REQ, CMD_BASICINFO, DVAL_EOM ]).then(function (msg) { + _this.dukVersion = msg[1]; + _this.dukGitDescribe = msg[2]; + _this.targetInfo = msg[3]; + _this.endianness = { 1: 'little', 2: 'mixed', 3: 'big' }[msg[4]] || 'unknown'; + _this.emit('basic-info-update'); + return msg; + }); +}; + +Debugger.prototype.sendGetVarRequest = function (varname, level) { + var _this = this; + return this.sendRequest([ DVAL_REQ, CMD_GETVAR, varname, (typeof level === 'number' ? level : -1), DVAL_EOM ]).then(function (msg) { + return { found: msg[1] === 1, value: msg[2] }; + }); +}; + +Debugger.prototype.sendPutVarRequest = function (varname, varvalue, level) { + var _this = this; + return this.sendRequest([ DVAL_REQ, CMD_PUTVAR, varname, varvalue, (typeof level === 'number' ? level : -1), DVAL_EOM ]); +}; + +Debugger.prototype.sendInvalidCommandTestRequest = function () { + // Intentional invalid command + var _this = this; + return this.sendRequest([ DVAL_REQ, 0xdeadbeef, DVAL_EOM ]); +}; + +Debugger.prototype.sendStatusRequest = function () { + // Send a status request to trigger a status notify, result is ignored: + // target sends a status notify instead of a meaningful reply + var _this = this; + return this.sendRequest([ DVAL_REQ, CMD_TRIGGERSTATUS, DVAL_EOM ]); +}; + +Debugger.prototype.sendBreakpointListRequest = function () { + var _this = this; + return this.sendRequest([ DVAL_REQ, CMD_LISTBREAK, DVAL_EOM ]).then(function (msg) { + var i, n; + var breakpts = []; + + for (i = 1, n = msg.length - 1; i < n; i += 2) { + breakpts.push({ fileName: msg[i], lineNumber: msg[i + 1] }); + } + + _this.breakpoints = breakpts; + _this.emit('breakpoints-update'); + return msg; + }); +}; + +Debugger.prototype.sendGetLocalsRequest = function (level) { + var _this = this; + return this.sendRequest([ DVAL_REQ, CMD_GETLOCALS, (typeof level === 'number' ? level : -1), DVAL_EOM ]).then(function (msg) { + var i; + var locals = []; + + for (i = 1; i <= msg.length - 2; i += 2) { + // XXX: do pretty printing in debug client for now + locals.push({ key: msg[i], value: prettyUiDebugValue(msg[i + 1], LOCALS_CLIPLEN) }); + } + + _this.locals = locals; + _this.emit('locals-update'); + return msg; + }); +}; + +Debugger.prototype.sendGetCallStackRequest = function () { + var _this = this; + return this.sendRequest([ DVAL_REQ, CMD_GETCALLSTACK, DVAL_EOM ]).then(function (msg) { + var i; + var stack = []; + + for (i = 1; i + 3 <= msg.length - 1; i += 4) { + stack.push({ + fileName: msg[i], + funcName: msg[i + 1], + lineNumber: msg[i + 2], + pc: msg[i + 3] + }); + } + + _this.callstack = stack; + _this.emit('callstack-update'); + return msg; + }); +}; + +Debugger.prototype.sendStepIntoRequest = function () { + var _this = this; + return this.sendRequest([ DVAL_REQ, CMD_STEPINTO, DVAL_EOM ]); +}; + +Debugger.prototype.sendStepOverRequest = function () { + var _this = this; + return this.sendRequest([ DVAL_REQ, CMD_STEPOVER, DVAL_EOM ]); +}; + +Debugger.prototype.sendStepOutRequest = function () { + var _this = this; + return this.sendRequest([ DVAL_REQ, CMD_STEPOUT, DVAL_EOM ]); +}; + +Debugger.prototype.sendPauseRequest = function () { + var _this = this; + return this.sendRequest([ DVAL_REQ, CMD_PAUSE, DVAL_EOM ]); +}; + +Debugger.prototype.sendResumeRequest = function () { + var _this = this; + return this.sendRequest([ DVAL_REQ, CMD_RESUME, DVAL_EOM ]); +}; + +Debugger.prototype.sendEvalRequest = function (evalInput, level) { + var _this = this; + return this.sendRequest([ DVAL_REQ, CMD_EVAL, evalInput, (typeof level === 'number' ? level : -1), DVAL_EOM ]).then(function (msg) { + return { error: msg[1] === 1 /*error*/, value: msg[2] }; + }); +}; + +Debugger.prototype.sendDetachRequest = function () { + var _this = this; + return this.sendRequest([ DVAL_REQ, CMD_DETACH, DVAL_EOM ]); +}; + +Debugger.prototype.sendDumpHeapRequest = function () { + var _this = this; + + return this.sendRequest([ DVAL_REQ, CMD_DUMPHEAP, DVAL_EOM ]).then(function (msg) { + var res = {}; + var objs = []; + var i, j, n, m, o, prop; + + res.type = 'heapDump'; + res.heapObjects = objs; + + for (i = 1, n = msg.length - 1; i < n; /*nop*/) { + o = {}; + o.ptr = msg[i++]; + o.type = msg[i++]; + o.flags = msg[i++] >>> 0; /* unsigned */ + o.refc = msg[i++]; + + if (o.type === DUK_HTYPE_STRING) { + o.blen = msg[i++]; + o.clen = msg[i++]; + o.hash = msg[i++] >>> 0; /* unsigned */ + o.data = msg[i++]; + } else if (o.type === DUK_HTYPE_BUFFER) { + o.len = msg[i++]; + o.data = msg[i++]; + } else if (o.type === DUK_HTYPE_OBJECT) { + o['class'] = msg[i++]; + o.proto = msg[i++]; + o.esize = msg[i++]; + o.enext = msg[i++]; + o.asize = msg[i++]; + o.hsize = msg[i++]; + o.props = []; + for (j = 0, m = o.enext; j < m; j++) { + prop = {}; + prop.flags = msg[i++]; + prop.key = msg[i++]; + prop.accessor = (msg[i++] == 1); + if (prop.accessor) { + prop.getter = msg[i++]; + prop.setter = msg[i++]; + } else { + prop.value = msg[i++]; + } + o.props.push(prop); + } + o.array = []; + for (j = 0, m = o.asize; j < m; j++) { + prop = {}; + prop.value = msg[i++]; + o.array.push(prop); + } + } else { + console.log('invalid htype: ' + o.type + ', disconnect'); + _this.disconnectDebugger(); + throw new Error('invalid htype'); + return; + } + + objs.push(o); + } + + return res; + }); +}; + +Debugger.prototype.sendGetBytecodeRequest = function () { + var _this = this; + + return this.sendRequest([ DVAL_REQ, CMD_GETBYTECODE, DVAL_EOM ]).then(function (msg) { + var idx = 1; + var nconst; + var nfunc; + var val; + var buf; + var i, n; + var consts = []; + var funcs = []; + var bcode; + var preformatted; + var ret; + var idxPreformattedInstructions; + + //console.log(JSON.stringify(msg)); + + nconst = msg[idx++]; + for (i = 0; i < nconst; i++) { + val = msg[idx++]; + consts.push(val); + } + + nfunc = msg[idx++]; + for (i = 0; i < nfunc; i++) { + val = msg[idx++]; + funcs.push(val); + } + val = msg[idx++]; + + // Right now bytecode is a string containing a direct dump of the + // bytecode in target endianness. Decode here so that the web UI + // doesn't need to. + + buf = new Buffer(val.length); + writeDebugStringToBuffer(val, buf, 0); + bcode = _this.decodeBytecodeFromBuffer(buf, consts, funcs); + + preformatted = []; + consts.forEach(function (v, i) { + preformatted.push('; c' + i + ' ' + JSON.stringify(v)); + }); + preformatted.push(''); + idxPreformattedInstructions = preformatted.length; + bcode.forEach(function (v) { + preformatted.push(v.str); + }); + preformatted = preformatted.join('\n') + '\n'; + + ret = { + constants: consts, + functions: funcs, + bytecode: bcode, + preformatted: preformatted, + idxPreformattedInstructions: idxPreformattedInstructions + }; + + return ret; + }); +}; + +Debugger.prototype.changeBreakpoint = function (fileName, lineNumber, mode) { + var _this = this; + + return this.sendRequest([ DVAL_REQ, CMD_LISTBREAK, DVAL_EOM ]).then(function (msg) { + var i, n; + var breakpts = []; + var deleted = false; + + // Up-to-date list of breakpoints on target + for (i = 1, n = msg.length - 1; i < n; i += 2) { + breakpts.push({ fileName: msg[i], lineNumber: msg[i + 1] }); + } + + // Delete matching breakpoints in reverse order so that indices + // remain valid. We do this for all operations so that duplicates + // are eliminated if present. + for (i = breakpts.length - 1; i >= 0; i--) { + var bp = breakpts[i]; + if (mode === 'deleteall' || (bp.fileName === fileName && bp.lineNumber === lineNumber)) { + deleted = true; + _this.sendRequest([ DVAL_REQ, CMD_DELBREAK, i, DVAL_EOM ], function (msg) { + // nop + }, function (err) { + // nop + }); + } + } + + // Technically we should wait for each delbreak reply but because + // target processes the requests in order, it doesn't matter. + if ((mode === 'add') || (mode === 'toggle' && !deleted)) { + _this.sendRequest([ DVAL_REQ, CMD_ADDBREAK, fileName, lineNumber, DVAL_EOM ], function (msg) { + // nop + }, function (err) { + _this.uiMessage('debugger-info', 'Failed to add breakpoint: ' + err); + }); + } + + // Read final, effective breakpoints from the target + _this.sendBreakpointListRequest(); + }); +}; + +Debugger.prototype.disconnectDebugger = function () { + if (this.targetStream) { + // We require a destroy() method from the actual target stream + this.targetStream.destroy(); + this.targetStream = null; + } + if (this.inputParser) { + this.inputParser.close(); + this.inputParser = null; + } + if (this.outputPassThroughStream) { + // There is no close() or destroy() for a passthrough stream, so just + // close the outputParser which will cancel timers etc. + } + if (this.outputParser) { + this.outputParser.close(); + this.outputParser = null; + } + + this.attached = false; + this.handshook = false; + this.reqQueue = null; + this.execStatus = { + attached: false, + state: 'detached', + fileName: '', + funcName: '', + line: 0, + pc: 0 + }; +}; + +Debugger.prototype.connectDebugger = function () { + var _this = this; + + this.disconnectDebugger(); // close previous target connection + + // CUSTOMTRANSPORT: to use a custom transport, change this.targetStream to + // use your custom transport. + + console.log('Connecting to ' + optTargetHost + ':' + optTargetPort + '...'); + this.targetStream = new net.Socket(); + this.targetStream.connect(optTargetPort, optTargetHost, function () { + console.log('Debug transport connected'); + _this.attached = true; + _this.reqQueue = []; + _this.uiMessage('debugger-info', 'Debug transport connected'); + }); + + this.inputParser = new DebugProtocolParser( + this.targetStream, + null, + optDumpDebugRead, + optDumpDebugPretty, + optDumpDebugPretty ? 'Recv: ' : null, + null, + null // console logging is done at a higher level to match request/response + ); + + // Use a PassThrough stream to debug dump and get stats for output messages. + // Simply write outgoing data to both the targetStream and this passthrough + // separately. + this.outputPassThroughStream = stream.PassThrough(); + this.outputParser = new DebugProtocolParser( + this.outputPassThroughStream, + 1, + optDumpDebugWrite, + optDumpDebugPretty, + optDumpDebugPretty ? 'Send: ' : null, + null, + null // console logging is done at a higher level to match request/response + ); + + this.inputParser.on('transport-close', function () { + _this.uiMessage('debugger-info', 'Debug transport closed'); + _this.disconnectDebugger(); + _this.emit('exec-status-update'); + _this.emit('detached'); + }); + + this.inputParser.on('transport-error', function (err) { + _this.uiMessage('debugger-info', 'Debug transport error: ' + err); + _this.disconnectDebugger(); + }); + + this.inputParser.on('protocol-version', function (msg) { + var ver = msg.protocolVersion; + console.log('Debug version identification:', msg.versionIdentification); + _this.protocolVersion = ver; + _this.uiMessage('debugger-info', 'Debug version identification: ' + msg.versionIdentification); + if (ver !== 1) { + _this.uiMessage('debugger-info', 'Protocol version ' + ver + ' unsupported, dropping connection'); + _this.targetStream.destroy(); + } else { + _this.uiMessage('debugger-info', 'Debug protocol version: ' + ver); + _this.handshook = true; + _this.execStatus = { + attached: true, + state: 'attached', + fileName: '', + funcName: '', + line: 0, + pc: 0 + }; + _this.emit('exec-status-update'); + _this.emit('attached'); // inform web UI + + // Fetch basic info right away + _this.sendBasicInfoRequest(); + } + }); + + this.inputParser.on('debug-message', function (msg) { + _this.processDebugMessage(msg); + }); + + this.inputParser.on('stats-update', function () { + _this.stats.rxBytes = this.bytes; + _this.stats.rxDvalues = this.dvalues; + _this.stats.rxMessages = this.messages; + _this.stats.rxBytesPerSec = this.bytesPerSec; + _this.emit('debug-stats-update'); + }); + + this.outputParser.on('stats-update', function () { + _this.stats.txBytes = this.bytes; + _this.stats.txDvalues = this.dvalues; + _this.stats.txMessages = this.messages; + _this.stats.txBytesPerSec = this.bytesPerSec; + _this.emit('debug-stats-update'); + }); +}; + +Debugger.prototype.processDebugMessage = function (msg) { + var req; + var prevState, newState; + var err; + + if (msg[0] === DVAL_REQ) { + // No actual requests sent by the target right now (just notifys). + console.log('Unsolicited reply message, dropping connection: ' + prettyDebugMessage(msg)); + } else if (msg[0] === DVAL_REP) { + if (this.reqQueue.length <= 0) { + console.log('Unsolicited reply message, dropping connection: ' + prettyDebugMessage(msg)); + this.targetStream.destroy(); + } + req = this.reqQueue.shift(); + + if (optLogMessages) { + console.log('Reply for ' + prettyDebugCommand(req.reqCmd) + ': ' + prettyDebugMessage(msg)); + } + + if (req.resolveCb) { + req.resolveCb(msg); + } else { + // nop: no callback + } + } else if (msg[0] === DVAL_ERR) { + if (this.reqQueue.length <= 0) { + console.log('Unsolicited error message, dropping connection: ' + prettyDebugMessage(msg)); + this.targetStream.destroy(); + } + err = new Error(String(msg[2]) + ' (code ' + String(msg[1]) + ')'); + err.errorCode = msg[1] || 0; + req = this.reqQueue.shift(); + + if (optLogMessages) { + console.log('Error for ' + prettyDebugCommand(req.reqCmd) + ': ' + prettyDebugMessage(msg)); + } + + if (req.rejectCb) { + req.rejectCb(err); + } else { + // nop: no callback + } + } else if (msg[0] === DVAL_NFY) { + if (optLogMessages) { + console.log('Notify ' + prettyDebugCommand(msg[1]) + ': ' + prettyDebugMessage(msg)); + } + + if (msg[1] === CMD_STATUS) { + prevState = this.execStatus.state; + newState = msg[2] === 0 ? 'running' : 'paused'; + this.execStatus = { + attached: true, + state: newState, + fileName: msg[3], + funcName: msg[4], + line: msg[5], + pc: msg[6] + }; + + if (prevState !== newState && newState === 'paused') { + // update run state now that we're paused + this.sendBreakpointListRequest(); + this.sendGetLocalsRequest(); + this.sendGetCallStackRequest(); + } + + this.emit('exec-status-update'); + } else if (msg[1] === CMD_PRINT) { + this.uiMessage('print', prettyUiStringUnquoted(msg[2], UI_MESSAGE_CLIPLEN)); + } else if (msg[1] === CMD_ALERT) { + this.uiMessage('alert', prettyUiStringUnquoted(msg[2], UI_MESSAGE_CLIPLEN)); + } else if (msg[1] === CMD_LOG) { + this.uiMessage({ type: 'log', level: msg[2], message: prettyUiStringUnquoted(msg[3], UI_MESSAGE_CLIPLEN) }); + } else if (msg[1] === CMD_THROW) { + this.uiMessage({ type: 'throw', fatal: msg[2], message: (msg[2] ? 'UNCAUGHT: ' : 'THROW: ') + prettyUiStringUnquoted(msg[3], UI_MESSAGE_CLIPLEN), fileName: msg[4], lineNumber: msg[5] }); + } else if (msg[1] === CMD_DETACHING) { + this.uiMessage({ type: 'detaching', reason: msg[2], message: 'DETACH: ' + (msg.length >= 5 ? prettyUiStringUnquoted(msg[3]) : 'detaching') }); + } else { + // Ignore unknown notify messages + console.log('Unknown notify, ignoring: ' + prettyDebugMessage(msg)); + + //this.targetStream.destroy(); + } + } else { + console.log('Invalid initial dvalue, dropping connection: ' + prettyDebugMessage(msg)); + this.targetStream.destroy(); + } +}; + +Debugger.prototype.run = function () { + var _this = this; + + // Initial debugger connection + + this.connectDebugger(); + + // Poll various state items when running + + var sendRound = 0; + var statusPending = false; + var bplistPending = false; + var localsPending = false; + var callStackPending = false; + + setInterval(function () { + if (_this.execStatus.state !== 'running') { + return; + } + + // Could also check for an empty request queue, but that's probably + // too strict? + + // Pending flags are used to avoid requesting the same thing twice + // while a previous request is pending. The flag-based approach is + // quite awkward. Rework to use promises. + + switch (sendRound) { + case 0: + if (!statusPending) { + statusPending = true; + _this.sendStatusRequest().finally(function () { statusPending = false; }); + } + break; + case 1: + if (!bplistPending) { + bplistPending = true; + _this.sendBreakpointListRequest().finally(function () { bplistPending = false; }); + } + break; + case 2: + if (!localsPending) { + localsPending = true; + _this.sendGetLocalsRequest().finally(function () { localsPending = false; }); + } + break; + case 3: + if (!callStackPending) { + callStackPending = true; + _this.sendGetCallStackRequest().finally(function () { callStackPending = false; }); + } + break; + } + sendRound = (sendRound + 1) % 4; + }, 500); +}; + +/* + * Express setup and socket.io + */ + +function DebugWebServer() { + this.dbg = null; // debugger singleton + this.socket = null; // current socket (or null) + this.keepaliveTimer = null; + this.uiMessageLimiter = null; + this.cachedJson = {}; // cache to avoid resending identical data + this.sourceFileManager = new SourceFileManager(optSourceSearchDirs); + this.sourceFileManager.scan(); +} + +DebugWebServer.prototype.handleSourcePost = function (req, res) { + var fileName = req.body && req.body.fileName; + var fileData; + + console.log('Source request: ' + fileName); + + if (typeof fileName !== 'string') { + res.status(500).send('invalid request'); + return; + } + fileData = this.sourceFileManager.search(fileName, optSourceSearchDirs); + if (typeof fileData !== 'string') { + res.status(404).send('not found'); + return; + } + res.status(200).send(fileData); // UTF-8 +}; + +DebugWebServer.prototype.handleSourceListPost = function (req, res) { + console.log('Source list request'); + + var files = this.sourceFileManager.getFiles(); + res.header('Content-Type', 'application/json'); + res.status(200).json(files); +}; + +DebugWebServer.prototype.handleHeapDumpGet = function (req, res) { + console.log('Heap dump get'); + + this.dbg.sendDumpHeapRequest().then(function (val) { + res.header('Content-Type', 'application/json'); + //res.status(200).json(val); + res.status(200).send(JSON.stringify(val, null, 4)); + }).catch(function (err) { + res.status(500).send('Failed to get heap dump: ' + (err.stack || err)); + }); +}; + +DebugWebServer.prototype.run = function () { + var _this = this; + + var express = require('express'); + var bodyParser = require('body-parser'); + var app = express(); + var http = require('http').Server(app); + var io = require('socket.io')(http); + + app.use(bodyParser.json()); + app.post('/source', this.handleSourcePost.bind(this)); + app.post('/sourceList', this.handleSourceListPost.bind(this)); + app.get('/heapDump.json', this.handleHeapDumpGet.bind(this)); + app.use('/', express.static(__dirname + '/static')); + + http.listen(optHttpPort, function () { + console.log('Listening on *:' + optHttpPort); + }); + + io.on('connection', this.handleNewSocketIoConnection.bind(this)); + + this.dbg.on('attached', function () { + console.log('Debugger attached'); + }); + + this.dbg.on('detached', function () { + console.log('Debugger detached'); + }); + + this.dbg.on('debug-stats-update', function () { + _this.debugStatsLimiter.trigger(); + }); + + this.dbg.on('ui-message-update', function () { + // Explicit rate limiter because this is a source of a lot of traffic. + _this.uiMessageLimiter.trigger(); + }); + + this.dbg.on('basic-info-update', function () { + _this.emitBasicInfo(); + }); + + this.dbg.on('breakpoints-update', function () { + _this.emitBreakpoints(); + }); + + this.dbg.on('exec-status-update', function () { + // Explicit rate limiter because this is a source of a lot of traffic. + _this.execStatusLimiter.trigger(); + }); + + this.dbg.on('locals-update', function () { + _this.emitLocals(); + }); + + this.dbg.on('callstack-update', function () { + _this.emitCallStack(); + }); + + this.uiMessageLimiter = new RateLimited(10, 1000, this.uiMessageLimiterCallback.bind(this)); + this.execStatusLimiter = new RateLimited(50, 500, this.execStatusLimiterCallback.bind(this)); + this.debugStatsLimiter = new RateLimited(1, 2000, this.debugStatsLimiterCallback.bind(this)); + + this.keepaliveTimer = setInterval(this.emitKeepalive.bind(this), 30000); +}; + +DebugWebServer.prototype.handleNewSocketIoConnection = function (socket) { + var _this = this; + + console.log('Socket.io connected'); + if (this.socket) { + console.log('Closing previous socket.io socket'); + this.socket.emit('replaced'); + } + this.socket = socket; + + this.emitKeepalive(); + + socket.on('disconnect', function () { + console.log('Socket.io disconnected'); + if (_this.socket === socket) { + _this.socket = null; + } + }); + + socket.on('keepalive', function (msg) { + // nop + }); + + socket.on('attach', function (msg) { + if (_this.dbg.targetStream) { + console.log('Attach request when debugger already has a connection, ignoring'); + } else { + _this.dbg.connectDebugger(); + } + }); + + socket.on('detach', function (msg) { + // Try to detach cleanly, timeout if no response + Promise.any([ + _this.dbg.sendDetachRequest(), + Promise.delay(3000) + ]).finally(function () { + _this.dbg.disconnectDebugger(); + }); + }); + + socket.on('stepinto', function (msg) { + _this.dbg.sendStepIntoRequest(); + }); + + socket.on('stepover', function (msg) { + _this.dbg.sendStepOverRequest(); + }); + + socket.on('stepout', function (msg) { + _this.dbg.sendStepOutRequest(); + }); + + socket.on('pause', function (msg) { + _this.dbg.sendPauseRequest(); + }); + + socket.on('resume', function (msg) { + _this.dbg.sendResumeRequest(); + }); + + socket.on('eval', function (msg) { + // msg.input is a proper Unicode strings here, and needs to be + // converted into a protocol string (U+0000...U+00FF). + var input = stringToDebugString(msg.input); + _this.dbg.sendEvalRequest(input, msg.level).then(function (v) { + socket.emit('eval-result', { error: v.error, result: prettyUiDebugValue(v.value, EVAL_CLIPLEN) }); + }); + + // An eval call quite possibly changes the local variables so always + // re-read locals afterwards. We don't need to wait for Eval to + // complete here; the requests will pipeline automatically and be + // executed in order. + + // XXX: move this to the web UI so that the UI can control what + // locals are listed (or perhaps show locals for all levels with + // an expandable tree view). + _this.dbg.sendGetLocalsRequest(); + }); + + socket.on('getvar', function (msg) { + // msg.varname is a proper Unicode strings here, and needs to be + // converted into a protocol string (U+0000...U+00FF). + var varname = stringToDebugString(msg.varname); + _this.dbg.sendGetVarRequest(varname, msg.level) + .then(function (v) { + socket.emit('getvar-result', { found: v.found, result: prettyUiDebugValue(v.value, GETVAR_CLIPLEN) }); + }); + }); + + socket.on('putvar', function (msg) { + // msg.varname and msg.varvalue are proper Unicode strings here, they + // need to be converted into protocol strings (U+0000...U+00FF). + var varname = stringToDebugString(msg.varname); + var varvalue = msg.varvalue; + + // varvalue is JSON parsed by the web UI for now, need special string + // encoding here. + if (typeof varvalue === 'string') { + varvalue = stringToDebugString(msg.varvalue); + } + + _this.dbg.sendPutVarRequest(varname, varvalue, msg.level) + .then(function (v) { + console.log('putvar done'); // XXX: signal success to UI? + }); + + // A PutVar call quite possibly changes the local variables so always + // re-read locals afterwards. We don't need to wait for PutVar to + // complete here; the requests will pipeline automatically and be + // executed in order. + + // XXX: make the client do this? + _this.dbg.sendGetLocalsRequest(); + }); + + socket.on('add-breakpoint', function (msg) { + _this.dbg.changeBreakpoint(msg.fileName, msg.lineNumber, 'add'); + }); + + socket.on('delete-breakpoint', function (msg) { + _this.dbg.changeBreakpoint(msg.fileName, msg.lineNumber, 'delete'); + }); + + socket.on('toggle-breakpoint', function (msg) { + _this.dbg.changeBreakpoint(msg.fileName, msg.lineNumber, 'toggle'); + }); + + socket.on('delete-all-breakpoints', function (msg) { + _this.dbg.changeBreakpoint(null, null, 'deleteall'); + }); + + socket.on('get-bytecode', function (msg) { + _this.dbg.sendGetBytecodeRequest().then(function (res) { + socket.emit('bytecode', res); + }); + }); + + // Resend all debugger state for new client + this.cachedJson = {}; // clear client state cache + this.emitBasicInfo(); + this.emitStats(); + this.emitExecStatus(); + this.emitUiMessages(); + this.emitBreakpoints(); + this.emitCallStack(); + this.emitLocals(); +}; + +// Check if 'msg' would encode to the same JSON which was previously sent +// to the web client. The caller then avoid resending unnecessary stuff. +DebugWebServer.prototype.cachedJsonCheck = function (cacheKey, msg) { + var newJson = JSON.stringify(msg); + if (this.cachedJson[cacheKey] === newJson) { + return true; // cached + } + this.cachedJson[cacheKey] = newJson; + return false; // not cached, send (cache already updated) +}; + +DebugWebServer.prototype.uiMessageLimiterCallback = function () { + this.emitUiMessages(); +}; + +DebugWebServer.prototype.execStatusLimiterCallback = function () { + this.emitExecStatus(); +}; + +DebugWebServer.prototype.debugStatsLimiterCallback = function () { + this.emitStats(); +}; + +DebugWebServer.prototype.emitKeepalive = function () { + if (!this.socket) { return; } + + this.socket.emit('keepalive', { nodeVersion: process.version }); +}; + +DebugWebServer.prototype.emitBasicInfo = function () { + if (!this.socket) { return; } + + var newMsg = { + duk_version: this.dbg.dukVersion, + duk_git_describe: this.dbg.dukGitDescribe, + target_info: this.dbg.targetInfo, + endianness: this.dbg.endianness + }; + if (this.cachedJsonCheck('basic-info', newMsg)) { + return; + } + this.socket.emit('basic-info', newMsg); +}; + +DebugWebServer.prototype.emitStats = function () { + if (!this.socket) { return; } + + this.socket.emit('debug-stats', this.dbg.stats); +}; + +DebugWebServer.prototype.emitExecStatus = function () { + if (!this.socket) { return; } + + var newMsg = this.dbg.execStatus; + if (this.cachedJsonCheck('exec-status', newMsg)) { + return; + } + this.socket.emit('exec-status', newMsg); +}; + +DebugWebServer.prototype.emitUiMessages = function () { + if (!this.socket) { return; } + + var newMsg = this.dbg.messageLines; + if (this.cachedJsonCheck('output-lines', newMsg)) { + return; + } + this.socket.emit('output-lines', newMsg); +}; + +DebugWebServer.prototype.emitBreakpoints = function () { + if (!this.socket) { return; } + + var newMsg = { breakpoints: this.dbg.breakpoints }; + if (this.cachedJsonCheck('breakpoints', newMsg)) { + return; + } + this.socket.emit('breakpoints', newMsg); +}; + +DebugWebServer.prototype.emitCallStack = function () { + if (!this.socket) { return; } + + var newMsg = { callstack: this.dbg.callstack }; + if (this.cachedJsonCheck('callstack', newMsg)) { + return; + } + this.socket.emit('callstack', newMsg); +}; + +DebugWebServer.prototype.emitLocals = function () { + if (!this.socket) { return; } + + var newMsg = { locals: this.dbg.locals }; + if (this.cachedJsonCheck('locals', newMsg)) { + return; + } + this.socket.emit('locals', newMsg); +}; + +/* + * JSON debug proxy + */ + +function DebugProxy(serverPort) { + this.serverPort = serverPort; + this.server = null; + this.socket = null; + this.targetStream = null; + this.inputParser = null; + + // preformatted dvalues + this.dval_eom = formatDebugValue(DVAL_EOM); + this.dval_req = formatDebugValue(DVAL_REQ); + this.dval_rep = formatDebugValue(DVAL_REP); + this.dval_nfy = formatDebugValue(DVAL_NFY); + this.dval_err = formatDebugValue(DVAL_ERR); +} + +DebugProxy.prototype.determineCommandNumber = function (cmdName, cmdNumber) { + var ret; + if (typeof cmdName === 'string') { + ret = debugCommandNumbers[cmdName]; + } else if (typeof cmdName === 'number') { + ret = cmdName; + } + ret = ret || cmdNumber; + if (typeof ret !== 'number') { + throw Error('cannot figure out command number for "' + cmdName + '" (' + cmdNumber + ')'); + } + return ret; +}; + +DebugProxy.prototype.commandNumberToString = function (id) { + return debugCommandNames[id] || String(id); +}; + +DebugProxy.prototype.formatDvalues = function (args) { + if (!args) { + return []; + } + return args.map(function (v) { + return formatDebugValue(v); + }); +}; + +DebugProxy.prototype.writeJson = function (val) { + this.socket.write(JSON.stringify(val) + '\n'); +}; + +DebugProxy.prototype.writeJsonSafe = function (val) { + try { + this.writeJson(val); + } catch (e) { + console.log('Failed to write JSON in writeJsonSafe, ignoring: ' + e); + } +}; + +DebugProxy.prototype.disconnectJsonClient = function () { + if (this.socket) { + this.socket.destroy(); + this.socket = null; + } +}; + +DebugProxy.prototype.disconnectTarget = function () { + if (this.inputParser) { + this.inputParser.close(); + this.inputParser = null; + } + if (this.targetStream) { + this.targetStream.destroy(); + this.targetStream = null; + } +}; + +DebugProxy.prototype.run = function () { + var _this = this; + + console.log('Waiting for client connections on port ' + this.serverPort); + this.server = net.createServer(function (socket) { + console.log('JSON proxy client connected'); + + _this.disconnectJsonClient(); + _this.disconnectTarget(); + + // A byline-parser is simple and good enough for now (assume + // compact JSON with no newlines). + var socketByline = byline(socket); + _this.socket = socket; + + socketByline.on('data', function (line) { + try { + // console.log('Received json proxy input line: ' + line.toString('utf8')); + var msg = JSON.parse(line.toString('utf8')); + var first_dval; + var args_dvalues = _this.formatDvalues(msg.args); + var last_dval = _this.dval_eom; + var cmd; + + if (msg.request) { + // "request" can be a string or "true" + first_dval = _this.dval_req; + cmd = _this.determineCommandNumber(msg.request, msg.command); + } else if (msg.reply) { + first_dval = _this.dval_rep; + } else if (msg.notify) { + // "notify" can be a string or "true" + first_dval = _this.dval_nfy; + cmd = _this.determineCommandNumber(msg.notify, msg.command); + } else if (msg.error) { + first_dval = _this.dval_err; + } else { + throw new Error('Invalid input JSON message: ' + JSON.stringify(msg)); + } + + _this.targetStream.write(first_dval); + if (cmd) { + _this.targetStream.write(formatDebugValue(cmd)); + } + args_dvalues.forEach(function (v) { + _this.targetStream.write(v); + }); + _this.targetStream.write(last_dval); + } catch (e) { + console.log(e); + + _this.writeJsonSafe({ + notify: '_Error', + args: [ 'Failed to handle input json message: ' + e ] + }); + + _this.disconnectJsonClient(); + _this.disconnectTarget(); + } + }); + + _this.connectToTarget(); + }).listen(this.serverPort); +}; + +DebugProxy.prototype.connectToTarget = function () { + var _this = this; + + console.log('Connecting to ' + optTargetHost + ':' + optTargetPort + '...'); + this.targetStream = new net.Socket(); + this.targetStream.connect(optTargetPort, optTargetHost, function () { + console.log('Debug transport connected'); + }); + + this.inputParser = new DebugProtocolParser( + this.targetStream, + null, + optDumpDebugRead, + optDumpDebugPretty, + optDumpDebugPretty ? 'Recv: ' : null, + null, + null // console logging is done at a higher level to match request/response + ); + + // Don't add a 'value' key to numbers. + this.inputParser.readableNumberValue = false; + + this.inputParser.on('transport-close', function () { + console.log('Debug transport closed'); + + _this.writeJsonSafe({ + notify: '_Disconnecting' + }); + + _this.disconnectJsonClient(); + _this.disconnectTarget(); + }); + + this.inputParser.on('transport-error', function (err) { + console.log('Debug transport error', err); + + _this.writeJsonSafe({ + notify: '_Error', + args: [ String(err) ] + }); + }); + + this.inputParser.on('protocol-version', function (msg) { + var ver = msg.protocolVersion; + console.log('Debug version identification:', msg.versionIdentification); + + _this.writeJson({ + notify: '_TargetConnected', + args: [ msg.versionIdentification ] // raw identification string + }); + + if (ver !== 1) { + console.log('Protocol version ' + ver + ' unsupported, dropping connection'); + } + }); + + this.inputParser.on('debug-message', function (msg) { + var t; + + //console.log(msg); + + if (typeof msg[0] !== 'object' || msg[0] === null) { + throw new Error('unexpected initial dvalue: ' + msg[0]); + } else if (msg[0].type === 'eom') { + throw new Error('unexpected initial dvalue: ' + msg[0]); + } else if (msg[0].type === 'req') { + if (typeof msg[1] !== 'number') { + throw new Error('unexpected request command number: ' + msg[1]); + } + t = { + request: _this.commandNumberToString(msg[1]), + command: msg[1], + args: msg.slice(2, msg.length - 1) + }; + _this.writeJson(t); + } else if (msg[0].type === 'rep') { + t = { + reply: true, + args: msg.slice(1, msg.length - 1) + }; + _this.writeJson(t); + } else if (msg[0].type === 'err') { + t = { + error: true, + args: msg.slice(1, msg.length - 1) + }; + _this.writeJson(t); + } else if (msg[0].type === 'nfy') { + if (typeof msg[1] !== 'number') { + throw new Error('unexpected notify command number: ' + msg[1]); + } + t = { + notify: _this.commandNumberToString(msg[1]), + command: msg[1], + args: msg.slice(2, msg.length - 1) + }; + _this.writeJson(t); + } else { + throw new Error('unexpected initial dvalue: ' + msg[0]); + } + }); + + this.inputParser.on('stats-update', function () { + }); +}; + +/* + * Command line parsing and initialization + */ + +function main() { + console.log('((o) Duktape debugger'); + + // Parse arguments. + + var argv = require('minimist')(process.argv.slice(2)); + //console.dir(argv); + if (argv['target-host']) { + optTargetHost = argv['target-host']; + } + if (argv['target-port']) { + optTargetPort = argv['target-port']; + } + if (argv['http-port']) { + optHttpPort = argv['http-port']; + } + if (argv['json-proxy-port']) { + optJsonProxyPort = argv['json-proxy-port']; + } + if (argv['json-proxy']) { + optJsonProxy = argv['json-proxy']; + } + if (argv['source-dirs']) { + optSourceSearchDirs = argv['source-dirs'].split(path.delimiter); + } + if (argv['dump-debug-read']) { + optDumpDebugRead = argv['dump-debug-read']; + } + if (argv['dump-debug-write']) { + optDumpDebugWrite = argv['dump-debug-write']; + } + if (argv['dump-debug-pretty']) { + optDumpDebugPretty = argv['dump-debug-pretty']; + } + if (argv['log-messages']) { + optLogMessages = true; + } + + // Dump effective options. Also provides a list of option names. + + console.log(''); + console.log('Effective options:'); + console.log(' --target-host: ' + optTargetHost); + console.log(' --target-port: ' + optTargetPort); + console.log(' --http-port: ' + optHttpPort); + console.log(' --json-proxy-port: ' + optJsonProxyPort); + console.log(' --json-proxy: ' + optJsonProxy); + console.log(' --source-dirs: ' + optSourceSearchDirs.join(' ')); + console.log(' --dump-debug-read: ' + optDumpDebugRead); + console.log(' --dump-debug-write: ' + optDumpDebugWrite); + console.log(' --dump-debug-pretty: ' + optDumpDebugPretty); + console.log(' --log-messages: ' + optLogMessages); + console.log(''); + + // Create debugger and web UI singletons, tie them together and + // start them. + + if (optJsonProxy) { + console.log('Starting in JSON proxy mode, JSON port: ' + optJsonProxyPort); + + var prx = new DebugProxy(optJsonProxyPort); + prx.run(); + } else { + var dbg = new Debugger(); + var web = new DebugWebServer(); + dbg.web = web; + web.dbg = dbg; + dbg.run(); + web.run(); + } +} + +main(); diff --git a/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debug_meta.json b/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debug_meta.json new file mode 100644 index 000000000..41de1c2f8 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debug_meta.json @@ -0,0 +1,1497 @@ +{ + "opcodes": [ + { + "args": [ + "A_R", + "BC_R" + ], + "name": "LDREG" + }, + { + "args": [ + "A_R", + "BC_R" + ], + "name": "STREG" + }, + { + "args": [ + "A_R", + "BC_C" + ], + "name": "LDCONST" + }, + { + "args": [ + "A_R", + "BC_LDINT" + ], + "name": "LDINT" + }, + { + "args": [ + "A_R", + "BC_LDINTX" + ], + "name": "LDINTX" + }, + { + "args": [ + "A_R", + "B_R", + "C_I" + ], + "name": "MPUTOBJ" + }, + { + "args": [ + "A_R", + "B_RI", + "C_I" + ], + "name": "MPUTOBJI" + }, + { + "args": [ + "A_R", + "B_R", + "C_I" + ], + "name": "MPUTARR" + }, + { + "args": [ + "A_R", + "B_RI", + "C_I" + ], + "name": "MPUTARRI" + }, + { + "args": [ + "B_R", + "C_I" + ], + "name": "NEW" + }, + { + "args": [ + "B_RI", + "C_I" + ], + "name": "NEWI" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "REGEXP" + }, + { + "args": [ + "A_R", + "B_R" + ], + "name": "CSREG" + }, + { + "args": [ + "A_RI", + "B_R" + ], + "name": "CSREGI" + }, + { + "args": [ + "A_R", + "BC_C" + ], + "name": "GETVAR" + }, + { + "args": [ + "A_R", + "BC_C" + ], + "name": "PUTVAR" + }, + { + "args": [ + "A_H", + "B_RC", + "C_RC" + ], + "flags": [ + { + "mask": 64, + "name": "writable" + }, + { + "mask": 128, + "name": "enumerable" + }, + { + "mask": 256, + "name": "configurable" + }, + { + "mask": 512, + "name": "accessor" + }, + { + "mask": 1024, + "name": "undef_value" + }, + { + "mask": 2048, + "name": "func_decl" + } + ], + "name": "DECLVAR" + }, + { + "args": [ + "A_R", + "B_RC" + ], + "name": "DELVAR" + }, + { + "args": [ + "A_R", + "B_RC" + ], + "name": "CSVAR" + }, + { + "args": [ + "A_RI", + "B_RC" + ], + "name": "CSVARI" + }, + { + "args": [ + "A_R", + "BC_I" + ], + "name": "CLOSURE" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "GETPROP" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "PUTPROP" + }, + { + "args": [ + "A_R", + "B_R", + "C_RC" + ], + "name": "DELPROP" + }, + { + "args": [ + "A_R", + "B_R", + "C_RC" + ], + "name": "CSPROP" + }, + { + "args": [ + "A_RI", + "B_R", + "C_RC" + ], + "name": "CSPROPI" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "ADD" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "SUB" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "MUL" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "DIV" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "MOD" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "BAND" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "BOR" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "BXOR" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "BASL" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "BLSR" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "BASR" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "EQ" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "NEQ" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "SEQ" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "SNEQ" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "GT" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "GE" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "LT" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "LE" + }, + { + "args": [ + "A_B", + "B_RC" + ], + "name": "IF" + }, + { + "args": [ + "ABC_JUMP" + ], + "name": "JUMP" + }, + { + "args": [ + "A_H", + "B_RC" + ], + "flags": [ + { + "mask": 64, + "name": "have_retval" + } + ], + "name": "RETURN" + }, + { + "args": [ + "A_H", + "B_R", + "C_I" + ], + "flags": [ + { + "mask": 64, + "name": "tailcall" + }, + { + "mask": 128, + "name": "evalcall" + } + ], + "name": "CALL" + }, + { + "args": [ + "A_H", + "B_RI", + "C_I" + ], + "name": "CALLI" + }, + { + "args": [ + "A_H", + "BC_R" + ], + "flags": [ + { + "mask": 64, + "name": "have_catch" + }, + { + "mask": 128, + "name": "have_finally" + }, + { + "mask": 256, + "name": "catch_binding" + }, + { + "mask": 512, + "name": "with_binding" + } + ], + "name": "TRYCATCH" + }, + { + "name": "EXTRA", + "extra": true + }, + { + "args": [ + "A_R", + "BC_R" + ], + "name": "PREINCR" + }, + { + "args": [ + "A_R", + "BC_R" + ], + "name": "PREDECR" + }, + { + "args": [ + "A_R", + "BC_R" + ], + "name": "POSTINCR" + }, + { + "args": [ + "A_R", + "BC_R" + ], + "name": "POSTDECR" + }, + { + "args": [ + "A_R", + "BC_C" + ], + "name": "PREINCV" + }, + { + "args": [ + "A_R", + "BC_C" + ], + "name": "PREDECV" + }, + { + "args": [ + "A_R", + "BC_C" + ], + "name": "POSTINCV" + }, + { + "args": [ + "A_R", + "BC_C" + ], + "name": "POSTDECV" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "PREINCP" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "PREDECP" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "POSTINCP" + }, + { + "args": [ + "A_R", + "B_RC", + "C_RC" + ], + "name": "POSTDECP" + } + ], + "client_commands": [ + "Reserved_0", + "Status", + "Print", + "Alert", + "Log", + "Throw", + "Detaching", + "AppNotify" + ], + "extra": [ + { + "name": "NOP" + }, + { + "args": [ + "BC_I" + ], + "name": "INVALID" + }, + { + "args": [ + "BC_R" + ], + "name": "LDTHIS" + }, + { + "args": [ + "BC_R" + ], + "name": "LDUNDEF" + }, + { + "args": [ + "BC_R" + ], + "name": "LDNULL" + }, + { + "args": [ + "BC_R" + ], + "name": "LDTRUE" + }, + { + "args": [ + "BC_R" + ], + "name": "LDFALSE" + }, + { + "args": [ + "B_R" + ], + "name": "NEWOBJ" + }, + { + "args": [ + "B_R" + ], + "name": "NEWARR" + }, + { + "args": [ + "B_R", + "C_R" + ], + "name": "SETALEN" + }, + { + "args": [ + "BC_R" + ], + "name": "TYPEOF" + }, + { + "args": [ + "B_R", + "C_RC" + ], + "name": "TYPEOFID" + }, + { + "args": [ + "B_R", + "C_R" + ], + "name": "INITENUM" + }, + { + "args": [ + "B_R", + "C_R" + ], + "name": "NEXTENUM" + }, + { + "args": [ + "B_R", + "C_R" + ], + "name": "INITSET" + }, + { + "args": [ + "B_R", + "C_RI" + ], + "name": "INITSETI" + }, + { + "args": [ + "B_R", + "C_RI" + ], + "name": "INITGET" + }, + { + "args": [ + "B_R", + "C_RI" + ], + "name": "INITGETI" + }, + { + "name": "ENDTRY" + }, + { + "name": "ENDCATCH" + }, + { + "name": "ENDFIN" + }, + { + "args": [ + "BC_R" + ], + "name": "THROW" + }, + { + "name": "INVLHS" + }, + { + "args": [ + "BC_R" + ], + "name": "UNM" + }, + { + "args": [ + "BC_R" + ], + "name": "UNP" + }, + { + "name": "DEBUGGER" + }, + { + "args": [ + "BC_I" + ], + "name": "BREAK" + }, + { + "args": [ + "BC_I" + ], + "name": "CONTINUE" + }, + { + "args": [ + "BC_R" + ], + "name": "BNOT" + }, + { + "args": [ + "BC_R" + ], + "name": "LNOT" + }, + { + "args": [ + "B_R", + "C_RC" + ], + "name": "INSTOF" + }, + { + "args": [ + "B_R", + "C_RC" + ], + "name": "IN" + }, + { + "args": [ + "BC_I" + ], + "name": "LABEL" + }, + { + "args": [ + "BC_I" + ], + "name": "ENDLABEL" + }, + { + "name": "EXTRA34" + }, + { + "name": "EXTRA35" + }, + { + "name": "EXTRA36" + }, + { + "name": "EXTRA37" + }, + { + "name": "EXTRA38" + }, + { + "name": "EXTRA39" + }, + { + "name": "EXTRA40" + }, + { + "name": "EXTRA41" + }, + { + "name": "EXTRA42" + }, + { + "name": "EXTRA43" + }, + { + "name": "EXTRA44" + }, + { + "name": "EXTRA45" + }, + { + "name": "EXTRA46" + }, + { + "name": "EXTRA47" + }, + { + "name": "EXTRA48" + }, + { + "name": "EXTRA49" + }, + { + "name": "EXTRA50" + }, + { + "name": "EXTRA51" + }, + { + "name": "EXTRA52" + }, + { + "name": "EXTRA53" + }, + { + "name": "EXTRA54" + }, + { + "name": "EXTRA55" + }, + { + "name": "EXTRA56" + }, + { + "name": "EXTRA57" + }, + { + "name": "EXTRA58" + }, + { + "name": "EXTRA59" + }, + { + "name": "EXTRA60" + }, + { + "name": "EXTRA61" + }, + { + "name": "EXTRA62" + }, + { + "name": "EXTRA63" + }, + { + "name": "EXTRA64" + }, + { + "name": "EXTRA65" + }, + { + "name": "EXTRA66" + }, + { + "name": "EXTRA67" + }, + { + "name": "EXTRA68" + }, + { + "name": "EXTRA69" + }, + { + "name": "EXTRA70" + }, + { + "name": "EXTRA71" + }, + { + "name": "EXTRA72" + }, + { + "name": "EXTRA73" + }, + { + "name": "EXTRA74" + }, + { + "name": "EXTRA75" + }, + { + "name": "EXTRA76" + }, + { + "name": "EXTRA77" + }, + { + "name": "EXTRA78" + }, + { + "name": "EXTRA79" + }, + { + "name": "EXTRA80" + }, + { + "name": "EXTRA81" + }, + { + "name": "EXTRA82" + }, + { + "name": "EXTRA83" + }, + { + "name": "EXTRA84" + }, + { + "name": "EXTRA85" + }, + { + "name": "EXTRA86" + }, + { + "name": "EXTRA87" + }, + { + "name": "EXTRA88" + }, + { + "name": "EXTRA89" + }, + { + "name": "EXTRA90" + }, + { + "name": "EXTRA91" + }, + { + "name": "EXTRA92" + }, + { + "name": "EXTRA93" + }, + { + "name": "EXTRA94" + }, + { + "name": "EXTRA95" + }, + { + "name": "EXTRA96" + }, + { + "name": "EXTRA97" + }, + { + "name": "EXTRA98" + }, + { + "name": "EXTRA99" + }, + { + "name": "EXTRA100" + }, + { + "name": "EXTRA101" + }, + { + "name": "EXTRA102" + }, + { + "name": "EXTRA103" + }, + { + "name": "EXTRA104" + }, + { + "name": "EXTRA105" + }, + { + "name": "EXTRA106" + }, + { + "name": "EXTRA107" + }, + { + "name": "EXTRA108" + }, + { + "name": "EXTRA109" + }, + { + "name": "EXTRA110" + }, + { + "name": "EXTRA111" + }, + { + "name": "EXTRA112" + }, + { + "name": "EXTRA113" + }, + { + "name": "EXTRA114" + }, + { + "name": "EXTRA115" + }, + { + "name": "EXTRA116" + }, + { + "name": "EXTRA117" + }, + { + "name": "EXTRA118" + }, + { + "name": "EXTRA119" + }, + { + "name": "EXTRA120" + }, + { + "name": "EXTRA121" + }, + { + "name": "EXTRA122" + }, + { + "name": "EXTRA123" + }, + { + "name": "EXTRA124" + }, + { + "name": "EXTRA125" + }, + { + "name": "EXTRA126" + }, + { + "name": "EXTRA127" + }, + { + "name": "EXTRA128" + }, + { + "name": "EXTRA129" + }, + { + "name": "EXTRA130" + }, + { + "name": "EXTRA131" + }, + { + "name": "EXTRA132" + }, + { + "name": "EXTRA133" + }, + { + "name": "EXTRA134" + }, + { + "name": "EXTRA135" + }, + { + "name": "EXTRA136" + }, + { + "name": "EXTRA137" + }, + { + "name": "EXTRA138" + }, + { + "name": "EXTRA139" + }, + { + "name": "EXTRA140" + }, + { + "name": "EXTRA141" + }, + { + "name": "EXTRA142" + }, + { + "name": "EXTRA143" + }, + { + "name": "EXTRA144" + }, + { + "name": "EXTRA145" + }, + { + "name": "EXTRA146" + }, + { + "name": "EXTRA147" + }, + { + "name": "EXTRA148" + }, + { + "name": "EXTRA149" + }, + { + "name": "EXTRA150" + }, + { + "name": "EXTRA151" + }, + { + "name": "EXTRA152" + }, + { + "name": "EXTRA153" + }, + { + "name": "EXTRA154" + }, + { + "name": "EXTRA155" + }, + { + "name": "EXTRA156" + }, + { + "name": "EXTRA157" + }, + { + "name": "EXTRA158" + }, + { + "name": "EXTRA159" + }, + { + "name": "EXTRA160" + }, + { + "name": "EXTRA161" + }, + { + "name": "EXTRA162" + }, + { + "name": "EXTRA163" + }, + { + "name": "EXTRA164" + }, + { + "name": "EXTRA165" + }, + { + "name": "EXTRA166" + }, + { + "name": "EXTRA167" + }, + { + "name": "EXTRA168" + }, + { + "name": "EXTRA169" + }, + { + "name": "EXTRA170" + }, + { + "name": "EXTRA171" + }, + { + "name": "EXTRA172" + }, + { + "name": "EXTRA173" + }, + { + "name": "EXTRA174" + }, + { + "name": "EXTRA175" + }, + { + "name": "EXTRA176" + }, + { + "name": "EXTRA177" + }, + { + "name": "EXTRA178" + }, + { + "name": "EXTRA179" + }, + { + "name": "EXTRA180" + }, + { + "name": "EXTRA181" + }, + { + "name": "EXTRA182" + }, + { + "name": "EXTRA183" + }, + { + "name": "EXTRA184" + }, + { + "name": "EXTRA185" + }, + { + "name": "EXTRA186" + }, + { + "name": "EXTRA187" + }, + { + "name": "EXTRA188" + }, + { + "name": "EXTRA189" + }, + { + "name": "EXTRA190" + }, + { + "name": "EXTRA191" + }, + { + "name": "EXTRA192" + }, + { + "name": "EXTRA193" + }, + { + "name": "EXTRA194" + }, + { + "name": "EXTRA195" + }, + { + "name": "EXTRA196" + }, + { + "name": "EXTRA197" + }, + { + "name": "EXTRA198" + }, + { + "name": "EXTRA199" + }, + { + "name": "EXTRA200" + }, + { + "name": "EXTRA201" + }, + { + "name": "EXTRA202" + }, + { + "name": "EXTRA203" + }, + { + "name": "EXTRA204" + }, + { + "name": "EXTRA205" + }, + { + "name": "EXTRA206" + }, + { + "name": "EXTRA207" + }, + { + "name": "EXTRA208" + }, + { + "name": "EXTRA209" + }, + { + "name": "EXTRA210" + }, + { + "name": "EXTRA211" + }, + { + "name": "EXTRA212" + }, + { + "name": "EXTRA213" + }, + { + "name": "EXTRA214" + }, + { + "name": "EXTRA215" + }, + { + "name": "EXTRA216" + }, + { + "name": "EXTRA217" + }, + { + "name": "EXTRA218" + }, + { + "name": "EXTRA219" + }, + { + "name": "EXTRA220" + }, + { + "name": "EXTRA221" + }, + { + "name": "EXTRA222" + }, + { + "name": "EXTRA223" + }, + { + "name": "EXTRA224" + }, + { + "name": "EXTRA225" + }, + { + "name": "EXTRA226" + }, + { + "name": "EXTRA227" + }, + { + "name": "EXTRA228" + }, + { + "name": "EXTRA229" + }, + { + "name": "EXTRA230" + }, + { + "name": "EXTRA231" + }, + { + "name": "EXTRA232" + }, + { + "name": "EXTRA233" + }, + { + "name": "EXTRA234" + }, + { + "name": "EXTRA235" + }, + { + "name": "EXTRA236" + }, + { + "name": "EXTRA237" + }, + { + "name": "EXTRA238" + }, + { + "name": "EXTRA239" + }, + { + "name": "EXTRA240" + }, + { + "name": "EXTRA241" + }, + { + "name": "EXTRA242" + }, + { + "name": "EXTRA243" + }, + { + "name": "EXTRA244" + }, + { + "name": "EXTRA245" + }, + { + "name": "EXTRA246" + }, + { + "name": "EXTRA247" + }, + { + "name": "EXTRA248" + }, + { + "name": "EXTRA249" + }, + { + "name": "EXTRA250" + }, + { + "name": "EXTRA251" + }, + { + "name": "EXTRA252" + }, + { + "name": "EXTRA253" + }, + { + "name": "EXTRA254" + }, + { + "name": "EXTRA255" + } + ], + "target_commands": [ + "Reserved_0", + "Reserved_1", + "Reserved_2", + "Reserved_3", + "Reserved_4", + "Reserved_5", + "Reserved_6", + "Reserved_7", + "Reserved_8", + "Reserved_9", + "Reserved_10", + "Reserved_11", + "Reserved_12", + "Reserved_13", + "Reserved_14", + "Reserved_15", + "BasicInfo", + "TriggerStatus", + "Pause", + "Resume", + "StepInto", + "StepOver", + "StepOut", + "ListBreak", + "AddBreak", + "DelBreak", + "GetVar", + "PutVar", + "GetCallStack", + "GetLocals", + "Eval", + "Detach", + "DumpHeap", + "GetBytecode", + "AppRequest", + "GetHeapObjInfo", + "GetObjPropDesc", + "GetObjPropDescRange" + ], + "error_codes": [ + "Unknown", + "UnsupportedCommand", + "TooMany", + "NotFound", + "ApplicationError" + ], + "class_names": [ + "unused", + "Arguments", + "Array", + "Boolean", + "Date", + "Error", + "Function", + "JSON", + "Math", + "Number", + "Object", + "RegExp", + "String", + "global", + "ObjEnv", + "DecEnv", + "Buffer", + "Pointer", + "Thread", + "ArrayBuffer", + "DataView", + "Int8Array", + "Uint8Array", + "Uint8ClampedArray", + "Int16Array", + "Uint16Array", + "Int32Array", + "Uint32Array", + "Float32Array", + "Float64Array" + ] +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debug_proxy.js b/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debug_proxy.js new file mode 100644 index 000000000..10ba6145d --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debug_proxy.js @@ -0,0 +1,1029 @@ +/* + * JSON debug proxy written in DukLuv + * + * This single file JSON debug proxy implementation is an alternative to the + * Node.js-based proxy in duk_debug.js. DukLuv is a much smaller dependency + * than Node.js so embedding DukLuv in a debug client is easier. + */ + +'use strict'; + +// XXX: Code assumes uv.write() will write fully. This is not necessarily +// true; should add support for partial writes (or at least failing when +// a partial write occurs). + +var log = new Duktape.Logger('Proxy'); // default logger +//log.l = 0; // enable debug and trace logging + +/* + * Config + */ + +var serverHost = '0.0.0.0'; +var serverPort = 9093; +var targetHost = '127.0.0.1'; +var targetPort = 9091; +var singleConnection = false; +var readableNumberValue = false; +var lenientJsonParse = false; +var jxParse = false; +var metadataFile = null; +var metadata = {}; +var TORTURE = false; // for manual testing of binary/json parsing robustness + +/* + * Detect missing 'var' declarations + */ + +// Prevent new bindings on global object. This detects missing 'var' +// declarations, e.g. "x = 123;" in a function without declaring it. +var global = new Function('return this;')(); +log.debug('Preventing extensions on global object'); +log.debug('Global is extensible:', Object.isExtensible(global)); +Object.preventExtensions(global); +log.debug('Global is extensible:', Object.isExtensible(global)); + +/* + * Misc helpers + */ + +function plainBufferCopy(typedarray) { + // This is still pretty awkward in Duktape 1.4.x. + // Argument may be a "slice" and we want a copy of the slice + // (not the full underlying buffer). + + var u8 = new Uint8Array(typedarray.length); + u8.set(typedarray); // make a copy, ensuring there's no slice offset + return Duktape.Buffer(u8); // get underlying plain buffer +} + +function isObject(x) { + // Note that typeof null === 'object'. + return (typeof x === 'object' && x !== null); +} + +function readFully(filename, cb) { + uv.fs_open(metadataFile, 'r', 0, function (handle, err) { + var fileOff = 0; + var data = new Uint8Array(256); + var dataOff = 0; + + if (err) { + return cb(null, err); + } + function readCb(buf, err) { + var res; + var newData; + + log.debug('Read callback:', buf.length, err); + if (err) { + uv.fs_close(handle); + return cb(null, err); + } + if (buf.length == 0) { + uv.fs_close(handle); + res = new Uint8Array(dataOff); + res.set(data.subarray(0, dataOff)); + res = Duktape.Buffer(res); // plain buffer + log.debug('Read', res.length, 'bytes from', filename); + return cb(res, null); + } + while (data.length - dataOff < buf.length) { + log.debug('Resize file read buffer:', data.length, '->', data.length * 2); + newData = new Uint8Array(data.length * 2); + newData.set(data); + data = newData; + } + data.set(new Uint8Array(buf), dataOff); + dataOff += buf.length; + fileOff += buf.length; + uv.fs_read(handle, 4096, fileOff, readCb); + } + uv.fs_read(handle, 4096, fileOff, readCb); + }); +} + +/* + * JSON proxy server + * + * Accepts an incoming JSON proxy client and connects to a debug target, + * tying the two connections together. Supports both a single connection + * and a persistent mode. + */ + +function JsonProxyServer(host, port) { + this.name = 'JsonProxyServer'; + this.handle = uv.new_tcp(); + uv.tcp_bind(this.handle, host, port); + uv.listen(this.handle, 128, this.onConnection.bind(this)); +} + +JsonProxyServer.prototype.onConnection = function onConnection(err) { + if (err) { + log.error('JSON proxy onConnection error:', err); + return; + } + log.info('JSON proxy client connected'); // XXX: it'd be nice to log remote peer host:port + + var jsonSock = new JsonConnHandler(this); + var targSock = new TargetConnHandler(this); + jsonSock.targetHandler = targSock; + targSock.jsonHandler = jsonSock; + uv.accept(this.handle, jsonSock.handle); + + log.info('Connecting to debug target at', targetHost + ':' + targetPort); + jsonSock.writeJson({ notify: '_TargetConnecting', args: [ targetHost, targetPort ] }); + uv.tcp_connect(targSock.handle, targetHost, targetPort, targSock.onConnect.bind(targSock)); + + if (singleConnection) { + log.info('Single connection mode, stop listening for more connections'); + uv.shutdown(this.handle); + uv.read_stop(this.handle); // unnecessary but just in case + uv.close(this.handle); + this.handle = null; + } +}; + +JsonProxyServer.prototype.onProxyClientDisconnected = function onProxyClientDisconnected() { + // When this is invoked the proxy connection and the target connection + // have both been closed. + if (singleConnection) { + log.info('Proxy connection finished (single connection mode: we should be exiting now)'); + } else { + log.info('Proxy connection finished (persistent mode: wait for more connections)'); + } +}; + +/* + * JSON connection handler + */ + +function JsonConnHandler(server) { + var i, n; + + this.name = 'JsonConnHandler'; + this.server = server; + this.handle = uv.new_tcp(); + this.incoming = new Uint8Array(4096); + this.incomingOffset = 0; + this.targetHandler = null; + + this.commandNumberLookup = {}; + if (metadata && metadata.target_commands) { + for (i = 0, n = metadata.target_commands.length; i < n; i++) { + this.commandNumberLookup[metadata.target_commands[i]] = i; + } + } +} + +JsonConnHandler.prototype.finish = function finish(msg) { + var args; + + if (!this.handle) { + log.info('JsonConnHandler already disconnected, ignore finish()'); + return; + } + log.info('JsonConnHandler finished:', msg); + try { + args = msg ? [ msg ] : void 0; + this.writeJson({ notify: '_Disconnecting', args: args }); + } catch (e) { + log.info('Failed to write _Disconnecting notify, ignoring:', e); + } + uv.shutdown(this.handle); + uv.read_stop(this.handle); + uv.close(this.handle); + this.handle = null; + + this.targetHandler.finish(msg); // disconnect target too (if not already disconnected) + + this.server.onProxyClientDisconnected(); +}; + +JsonConnHandler.prototype.onRead = function onRead(err, data) { + var newIncoming; + var msg; + var errmsg; + var tmpBuf; + + log.trace('Received data from JSON socket, err:', err, 'data length:', data ? data.length : 'null'); + + if (err) { + errmsg = 'Error reading data from JSON debug client: ' + err; + this.finish(errmsg); + return; + } + if (data) { + // Feed the data one byte at a time when torture testing. + if (TORTURE && data.length > 1) { + for (var i = 0; i < data.length; i++) { + tmpBuf = Duktape.Buffer(1); + tmpBuf[0] = data[i]; + this.onRead(null, tmpBuf); + } + return; + } + + // Receive data into 'incoming', resizing as necessary. + while (data.length > this.incoming.length - this.incomingOffset) { + newIncoming = new Uint8Array(this.incoming.length * 1.3 + 16); + newIncoming.set(this.incoming); + this.incoming = newIncoming; + log.debug('Resize incoming JSON buffer to ' + this.incoming.length); + } + this.incoming.set(new Uint8Array(data), this.incomingOffset); + this.incomingOffset += data.length; + + // Trial parse JSON message(s). + while (true) { + msg = this.trialParseJsonMessage(); + if (!msg) { + break; + } + try { + this.dispatchJsonMessage(msg); + } catch (e) { + errmsg = 'JSON message dispatch failed: ' + e; + this.writeJson({ notify: '_Error', args: [ errmsg ] }); + if (lenientJsonParse) { + log.warn('JSON message dispatch failed (lenient mode, ignoring):', e); + } else { + log.warn('JSON message dispatch failed (dropping connection):', e); + this.finish(errmsg); + } + } + } + } else { + this.finish('JSON proxy client disconnected'); + } +}; + +JsonConnHandler.prototype.writeJson = function writeJson(msg) { + log.info('PROXY --> CLIENT:', JSON.stringify(msg)); + if (this.handle) { + uv.write(this.handle, JSON.stringify(msg) + '\n'); + } +}; + +JsonConnHandler.prototype.handleDebugMessage = function handleDebugMessage(dvalues) { + var msg = {}; + var idx = 0; + var cmd; + + if (dvalues.length <= 0) { + throw new Error('invalid dvalues list: length <= 0'); + } + var x = dvalues[idx++]; + if (!isObject(x)) { + throw new Error('invalid initial dvalue: ' + Duktape.enc('jx', dvalues)); + } + if (x.type === 'req') { + cmd = dvalues[idx++]; + if (typeof cmd !== 'number') { + throw new Error('invalid command: ' + Duktape.enc('jx', cmd)); + } + msg.request = this.determineCommandName(cmd) || true; + msg.command = cmd; + } else if (x.type === 'rep') { + msg.reply = true; + } else if (x.type === 'err') { + msg.error = true; + } else if (x.type === 'nfy') { + cmd = dvalues[idx++]; + if (typeof cmd !== 'number') { + throw new Error('invalid command: ' + Duktape.enc('jx', cmd)); + } + msg.notify = this.determineCommandName(cmd) || true; + msg.command = cmd; + } else { + throw new Error('invalid initial dvalue: ' + Duktape.enc('jx', dvalues)); + } + + for (; idx < dvalues.length - 1; idx++) { + if (!msg.args) { + msg.args = []; + } + msg.args.push(dvalues[idx]); + } + + if (!isObject(dvalues[idx]) || dvalues[idx].type !== 'eom') { + throw new Error('invalid final dvalue: ' + Duktape.enc('jx', dvalues)); + } + + this.writeJson(msg); +}; + +JsonConnHandler.prototype.determineCommandName = function determineCommandName(cmd) { + if (!(metadata && metadata.client_commands)) { + return; + } + return metadata.client_commands[cmd]; +}; + +JsonConnHandler.prototype.trialParseJsonMessage = function trialParseJsonMessage() { + var buf = this.incoming; + var avail = this.incomingOffset; + var i; + var msg, str, errmsg; + + for (i = 0; i < avail; i++) { + if (buf[i] == 0x0a) { + str = String(plainBufferCopy(buf.subarray(0, i))); + try { + if (jxParse) { + msg = Duktape.dec('jx', str); + } else { + msg = JSON.parse(str); + } + } catch (e) { + // In lenient mode if JSON parse fails just send back an _Error + // and ignore the line (useful for initial development). + // + // In non-lenient mode drop the connection here; if the failed line + // was a request the client is expecting a reply/error message back + // (otherwise it may go out of sync) but we can't send a synthetic + // one (as we can't parse the request). + errmsg = 'JSON parse failed for: ' + JSON.stringify(str) + ': ' + e; + this.writeJson({ notify: '_Error', args: [ errmsg ] }); + if (lenientJsonParse) { + log.warn('JSON parse failed (lenient mode, ignoring):', e); + } else { + log.warn('JSON parse failed (dropping connection):', e); + this.finish(errmsg); + } + } + + this.incoming.set(this.incoming.subarray(i + 1)); + this.incomingOffset -= i + 1; + return msg; + } + } +}; + +JsonConnHandler.prototype.dispatchJsonMessage = function dispatchJsonMessage(msg) { + var cmd; + var dvalues = []; + var i, n; + + log.info('PROXY <-- CLIENT:', JSON.stringify(msg)); + + // Parse message type, determine initial marker for binary message. + if (msg.request) { + cmd = this.determineCommandNumber(msg.request, msg.command); + dvalues.push(new Uint8Array([ 0x01 ])); + dvalues.push(this.encodeJsonDvalue(cmd)); + } else if (msg.reply) { + dvalues.push(new Uint8Array([ 0x02 ])); + } else if (msg.notify) { + cmd = this.determineCommandNumber(msg.notify, msg.command); + dvalues.push(new Uint8Array([ 0x04 ])); + dvalues.push(this.encodeJsonDvalue(cmd)); + } else if (msg.error) { + dvalues.push(new Uint8Array([ 0x03 ])); + } else { + throw new Error('invalid input JSON message: ' + JSON.stringify(msg)); + } + + // Encode arguments into dvalues. + for (i = 0, n = (msg.args ? msg.args.length : 0); i < n; i++) { + dvalues.push(this.encodeJsonDvalue(msg.args[i])); + } + + // Add an EOM, and write out the dvalues to the debug target. + dvalues.push(new Uint8Array([ 0x00 ])); + for (i = 0, n = dvalues.length; i < n; i++) { + this.targetHandler.writeBinary(dvalues[i]); + } +}; + +JsonConnHandler.prototype.determineCommandNumber = function determineCommandNumber(name, val) { + var res; + + if (typeof name === 'string') { + res = this.commandNumberLookup[name]; + if (!res) { + log.info('Unknown command name: ' + name + ', command number: ' + val); + } + } else if (typeof name === 'number') { + res = name; + } else if (name !== true) { + throw new Error('invalid command name (must be string, number, or "true"): ' + name); + } + if (typeof res === 'undefined' && typeof val === 'undefined') { + throw new Error('cannot determine command number from name: ' + name); + } + if (typeof val !== 'number' && typeof val !== 'undefined') { + throw new Error('invalid command number: ' + val); + } + res = res || val; + return res; +}; + +JsonConnHandler.prototype.writeDebugStringToBuffer = function writeDebugStringToBuffer(v, buf, off) { + var i, n; + + for (i = 0, n = v.length; i < n; i++) { + buf[off + i] = v.charCodeAt(i) & 0xff; // truncate higher bits + } +}; + +JsonConnHandler.prototype.encodeJsonDvalue = function encodeJsonDvalue(v) { + var buf, dec, len, dv; + + if (isObject(v)) { + if (v.type === 'eom') { + return new Uint8Array([ 0x00 ]); + } else if (v.type === 'req') { + return new Uint8Array([ 0x01 ]); + } else if (v.type === 'rep') { + return new Uint8Array([ 0x02 ]); + } else if (v.type === 'err') { + return new Uint8Array([ 0x03 ]); + } else if (v.type === 'nfy') { + return new Uint8Array([ 0x04 ]); + } else if (v.type === 'unused') { + return new Uint8Array([ 0x15 ]); + } else if (v.type === 'undefined') { + return new Uint8Array([ 0x16 ]); + } else if (v.type === 'number') { + dec = Duktape.dec('hex', v.data); + len = dec.length; + if (len !== 8) { + throw new TypeError('value cannot be converted to dvalue: ' + JSON.stringify(v)); + } + buf = new Uint8Array(1 + len); + buf[0] = 0x1a; + buf.set(new Uint8Array(dec), 1); + return buf; + } else if (v.type === 'buffer') { + dec = Duktape.dec('hex', v.data); + len = dec.length; + if (len <= 0xffff) { + buf = new Uint8Array(3 + len); + buf[0] = 0x14; + buf[1] = (len >> 8) & 0xff; + buf[2] = (len >> 0) & 0xff; + buf.set(new Uint8Arrau(dec), 3); + return buf; + } else { + buf = new Uint8Array(5 + len); + buf[0] = 0x13; + buf[1] = (len >> 24) & 0xff; + buf[2] = (len >> 16) & 0xff; + buf[3] = (len >> 8) & 0xff; + buf[4] = (len >> 0) & 0xff; + buf.set(new Uint8Array(dec), 5); + return buf; + } + } else if (v.type === 'object') { + dec = Duktape.dec('hex', v.pointer); + len = dec.length; + buf = new Uint8Array(3 + len); + buf[0] = 0x1b; + buf[1] = v.class; + buf[2] = len; + buf.set(new Uint8Array(dec), 3); + return buf; + } else if (v.type === 'pointer') { + dec = Duktape.dec('hex', v.pointer); + len = dec.length; + buf = new Uint8Array(2 + len); + buf[0] = 0x1c; + buf[1] = len; + buf.set(new Uint8Array(dec), 2); + return buf; + } else if (v.type === 'lightfunc') { + dec = Duktape.dec('hex', v.pointer); + len = dec.length; + buf = new Uint8Array(4 + len); + buf[0] = 0x1d; + buf[1] = (v.flags >> 8) & 0xff; + buf[2] = v.flags & 0xff; + buf[3] = len; + buf.set(new Uint8Array(dec), 4); + return buf; + } else if (v.type === 'heapptr') { + dec = Duktape.dec('hex', v.pointer); + len = dec.length; + buf = new Uint8Array(2 + len); + buf[0] = 0x1e; + buf[1] = len; + buf.set(new Uint8Array(dec), 2); + return buf; + } + } else if (v === null) { + return new Uint8Array([ 0x17 ]); + } else if (typeof v === 'boolean') { + return new Uint8Array([ v ? 0x18 : 0x19 ]); + } else if (typeof v === 'number') { + if (Math.floor(v) === v && /* whole */ + (v !== 0 || 1 / v > 0) && /* not negative zero */ + v >= -0x80000000 && v <= 0x7fffffff) { + // Represented signed 32-bit integers as plain integers. + // Debugger code expects this for all fields that are not + // duk_tval representations (e.g. command numbers and such). + if (v >= 0x00 && v <= 0x3f) { + return new Uint8Array([ 0x80 + v ]); + } else if (v >= 0x0000 && v <= 0x3fff) { + return new Uint8Array([ 0xc0 + (v >> 8), v & 0xff ]); + } else if (v >= -0x80000000 && v <= 0x7fffffff) { + return new Uint8Array([ 0x10, + (v >> 24) & 0xff, + (v >> 16) & 0xff, + (v >> 8) & 0xff, + (v >> 0) & 0xff ]); + } else { + throw new Error('internal error when encoding integer to dvalue: ' + v); + } + } else { + // Represent non-integers as IEEE double dvalues. + buf = new Uint8Array(1 + 8); + buf[0] = 0x1a; + new DataView(buf).setFloat64(1, v, false); + return buf; + } + } else if (typeof v === 'string') { + if (v.length < 0 || v.length > 0xffffffff) { + // Not possible in practice. + throw new TypeError('cannot convert to dvalue, invalid string length: ' + v.length); + } + if (v.length <= 0x1f) { + buf = new Uint8Array(1 + v.length); + buf[0] = 0x60 + v.length; + this.writeDebugStringToBuffer(v, buf, 1); + return buf; + } else if (v.length <= 0xffff) { + buf = new Uint8Array(3 + v.length); + buf[0] = 0x12; + buf[1] = (v.length >> 8) & 0xff; + buf[2] = (v.length >> 0) & 0xff; + this.writeDebugStringToBuffer(v, buf, 3); + return buf; + } else { + buf = new Uint8Array(5 + v.length); + buf[0] = 0x11; + buf[1] = (v.length >> 24) & 0xff; + buf[2] = (v.length >> 16) & 0xff; + buf[3] = (v.length >> 8) & 0xff; + buf[4] = (v.length >> 0) & 0xff; + this.writeDebugStringToBuffer(v, buf, 5); + return buf; + } + } + + throw new TypeError('value cannot be converted to dvalue: ' + JSON.stringify(v)); +}; + +/* + * Target binary connection handler + */ + +function TargetConnHandler(server) { + this.name = 'TargetConnHandler'; + this.server = server; + this.handle = uv.new_tcp(); + this.jsonHandler = null; + this.incoming = new Uint8Array(4096); + this.incomingOffset = 0; + this.dvalues = []; +} + +TargetConnHandler.prototype.finish = function finish(msg) { + if (!this.handle) { + log.info('TargetConnHandler already disconnected, ignore finish()'); + return; + } + log.info('TargetConnHandler finished:', msg); + + this.jsonHandler.writeJson({ notify: '_TargetDisconnected' }); + + // XXX: write a notify to target? + + uv.shutdown(this.handle); + uv.read_stop(this.handle); + uv.close(this.handle); + this.handle = null; + + this.jsonHandler.finish(msg); // disconnect JSON client too (if not already disconnected) +}; + +TargetConnHandler.prototype.onConnect = function onConnect(err) { + var errmsg; + + if (err) { + errmsg = 'Failed to connect to target: ' + err; + log.warn(errmsg); + this.jsonHandler.writeJson({ notify: '_Error', args: [ String(err) ] }); + this.finish(errmsg); + return; + } + + // Once we're connected to the target, start read both binary and JSON + // input. We don't want to read JSON input before this so that we can + // always translate incoming messages to dvalues and write them out + // without queueing. Any pending JSON messages will be queued by the + // OS instead. + + log.info('Connected to debug target at', targetHost + ':' + targetPort); + uv.read_start(this.jsonHandler.handle, this.jsonHandler.onRead.bind(this.jsonHandler)); + uv.read_start(this.handle, this.onRead.bind(this)); +}; + +TargetConnHandler.prototype.writeBinary = function writeBinary(buf) { + var plain = plainBufferCopy(buf); + log.info('PROXY --> TARGET:', Duktape.enc('jx', plain)); + if (this.handle) { + uv.write(this.handle, plain); + } +}; + +TargetConnHandler.prototype.onRead = function onRead(err, data) { + var res; + var errmsg; + var tmpBuf; + var newIncoming; + + log.trace('Received data from target socket, err:', err, 'data length:', data ? data.length : 'null'); + + if (err) { + errmsg = 'Error reading data from debug target: ' + err; + this.finish(errmsg); + return; + } + + if (data) { + // Feed the data one byte at a time when torture testing. + if (TORTURE && data.length > 1) { + for (var i = 0; i < data.length; i++) { + tmpBuf = Duktape.Buffer(1); + tmpBuf[0] = data[i]; + this.onRead(null, tmpBuf); + } + return; + } + + // Receive data into 'incoming', resizing as necessary. + while (data.length > this.incoming.length - this.incomingOffset) { + newIncoming = new Uint8Array(this.incoming.length * 1.3 + 16); + newIncoming.set(this.incoming); + this.incoming = newIncoming; + log.debug('Resize incoming binary buffer to ' + this.incoming.length); + } + this.incoming.set(new Uint8Array(data), this.incomingOffset); + this.incomingOffset += data.length; + + // Trial parse handshake unless done. + if (!this.handshake) { + this.trialParseHandshake(); + } + + // Trial parse dvalue(s) and debug messages. + if (this.handshake) { + for (;;) { + res = this.trialParseDvalue(); + if (!res) { + break; + } + log.trace('Got dvalue:', Duktape.enc('jx', res.dvalue)); + this.dvalues.push(res.dvalue); + if (isObject(res.dvalue) && res.dvalue.type === 'eom') { + try { + this.jsonHandler.handleDebugMessage(this.dvalues); + this.dvalues = []; + } catch (e) { + errmsg = 'JSON message handling failed: ' + e; + this.jsonHandler.writeJson({ notify: '_Error', args: [ errmsg ] }); + if (lenientJsonParse) { + log.warn('JSON message handling failed (lenient mode, ignoring):', e); + } else { + log.warn('JSON message handling failed (dropping connection):', e); + this.finish(errmsg); + } + } + } + } + } + } else { + log.info('Target disconnected'); + this.finish('Target disconnected'); + } +}; + +TargetConnHandler.prototype.trialParseHandshake = function trialParseHandshake() { + var buf = this.incoming; + var avail = this.incomingOffset; + var i; + var msg; + var m; + var protocolVersion; + + for (i = 0; i < avail; i++) { + if (buf[i] == 0x0a) { + msg = String(plainBufferCopy(buf.subarray(0, i))); + this.incoming.set(this.incoming.subarray(i + 1)); + this.incomingOffset -= i + 1; + + // Generic handshake format: only relies on initial version field. + m = /^(\d+) (.*)$/.exec(msg) || {}; + protocolVersion = +m[1]; + this.handshake = { + line: msg, + protocolVersion: protocolVersion, + text: m[2] + }; + + // More detailed v1 handshake line. + if (protocolVersion === 1) { + m = /^(\d+) (\d+) (.*?) (.*?) (.*)$/.exec(msg) || {}; + this.handshake.dukVersion = m[1]; + this.handshake.dukGitDescribe = m[2]; + this.handshake.targetString = m[3]; + } + + this.jsonHandler.writeJson({ notify: '_TargetConnected', args: [ msg ] }); + + log.info('Target handshake: ' + JSON.stringify(this.handshake)); + return; + } + } +}; + +TargetConnHandler.prototype.bufferToDebugString = function bufferToDebugString(buf) { + return String.fromCharCode.apply(null, buf); +}; + +TargetConnHandler.prototype.trialParseDvalue = function trialParseDvalue() { + var _this = this; + var buf = this.incoming; + var avail = this.incomingOffset; + var v; + var gotValue = false; // explicit flag for e.g. v === undefined + var dv = new DataView(buf); + var tmp; + var x; + var len; + + function consume(n) { + log.info('PROXY <-- TARGET:', Duktape.enc('jx', _this.incoming.subarray(0, n))); + _this.incoming.set(_this.incoming.subarray(n)); + _this.incomingOffset -= n; + } + + x = buf[0]; + if (avail <= 0) { + ; + } else if (x >= 0xc0) { + // 0xc0...0xff: integers 0-16383 + if (avail >= 2) { + v = ((x - 0xc0) << 8) + buf[1]; + consume(2); + } + } else if (x >= 0x80) { + // 0x80...0xbf: integers 0-63 + v = x - 0x80; + consume(1); + } else if (x >= 0x60) { + // 0x60...0x7f: strings with length 0-31 + len = x - 0x60; + if (avail >= 1 + len) { + v = new Uint8Array(len); + v.set(buf.subarray(1, 1 + len)); + v = this.bufferToDebugString(v); + consume(1 + len); + } + } else { + switch (x) { + case 0x00: consume(1); v = { type: 'eom' }; break; + case 0x01: consume(1); v = { type: 'req' }; break; + case 0x02: consume(1); v = { type: 'rep' }; break; + case 0x03: consume(1); v = { type: 'err' }; break; + case 0x04: consume(1); v = { type: 'nfy' }; break; + case 0x10: // 4-byte signed integer + if (avail >= 5) { + v = dv.getInt32(1, false); + consume(5); + } + break; + case 0x11: // 4-byte string + if (avail >= 5) { + len = dv.getUint32(1, false); + if (avail >= 5 + len) { + v = new Uint8Array(len); + v.set(buf.subarray(5, 5 + len)); + v = this.bufferToDebugString(v); + consume(5 + len); + } + } + break; + case 0x12: // 2-byte string + if (avail >= 3) { + len = dv.getUint16(1, false); + if (avail >= 3 + len) { + v = new Uint8Array(len); + v.set(buf.subarray(3, 3 + len)); + v = this.bufferToDebugString(v); + consume(3 + len); + } + } + break; + case 0x13: // 4-byte buffer + if (avail >= 5) { + len = dv.getUint32(1, false); + if (avail >= 5 + len) { + v = new Uint8Array(len); + v.set(buf.subarray(5, 5 + len)); + v = { type: 'buffer', data: Duktape.enc('hex', Duktape.Buffer(v)) }; + consume(5 + len); + } + } + break; + case 0x14: // 2-byte buffer + if (avail >= 3) { + len = dv.getUint16(1, false); + if (avail >= 3 + len) { + v = new Uint8Array(len); + v.set(buf.subarray(3, 3 + len)); + v = { type: 'buffer', data: Duktape.enc('hex', Duktape.Buffer(v)) }; + consume(3 + len); + } + } + break; + case 0x15: // unused/none + v = { type: 'unused' }; + consume(1); + break; + case 0x16: // undefined + v = { type: 'undefined' }; + gotValue = true; // indicate 'v' is actually set + consume(1); + break; + case 0x17: // null + v = null; + gotValue = true; // indicate 'v' is actually set + consume(1); + break; + case 0x18: // true + v = true; + consume(1); + break; + case 0x19: // false + v = false; + consume(1); + break; + case 0x1a: // number (IEEE double), big endian + if (avail >= 9) { + tmp = new Uint8Array(8); + tmp.set(buf.subarray(1, 9)); + v = { type: 'number', data: Duktape.enc('hex', Duktape.Buffer(tmp)) }; + if (readableNumberValue) { + // The value key should not be used programmatically, + // it is just there to make the dumps more readable. + v.value = new DataView(tmp.buffer).getFloat64(0, false); + } + consume(9); + } + break; + case 0x1b: // object + if (avail >= 3) { + len = buf[2]; + if (avail >= 3 + len) { + v = new Uint8Array(len); + v.set(buf.subarray(3, 3 + len)); + v = { type: 'object', 'class': buf[1], pointer: Duktape.enc('hex', Duktape.Buffer(v)) }; + consume(3 + len); + } + } + break; + case 0x1c: // pointer + if (avail >= 2) { + len = buf[1]; + if (avail >= 2 + len) { + v = new Uint8Array(len); + v.set(buf.subarray(2, 2 + len)); + v = { type: 'pointer', pointer: Duktape.enc('hex', Duktape.Buffer(v)) }; + consume(2 + len); + } + } + break; + case 0x1d: // lightfunc + if (avail >= 4) { + len = buf[3]; + if (avail >= 4 + len) { + v = new Uint8Array(len); + v.set(buf.subarray(4, 4 + len)); + v = { type: 'lightfunc', flags: dv.getUint16(1, false), pointer: Duktape.enc('hex', Duktape.Buffer(v)) }; + consume(4 + len); + } + } + break; + case 0x1e: // heapptr + if (avail >= 2) { + len = buf[1]; + if (avail >= 2 + len) { + v = new Uint8Array(len); + v.set(buf.subarray(2, 2 + len)); + v = { type: 'heapptr', pointer: Duktape.enc('hex', Duktape.Buffer(v)) }; + consume(2 + len); + } + } + break; + default: + throw new Error('failed parse initial byte: ' + buf[0]); + } + } + + if (typeof v !== 'undefined' || gotValue) { + return { dvalue: v }; + } +}; + +/* + * Main + */ + +function main() { + var argv = typeof uv.argv === 'function' ? uv.argv() : []; + var i; + for (i = 2; i < argv.length; i++) { // skip dukluv and script name + if (argv[i] == '--help') { + print('Usage: dukluv ' + argv[1] + ' [option]+'); + print(''); + print(' --server-host HOST JSON proxy server listen address'); + print(' --server-port PORT JSON proxy server listen port'); + print(' --target-host HOST Debug target address'); + print(' --target-port PORT Debug target port'); + print(' --metadata FILE Proxy metadata file (usually named duk_debug_meta.json)'); + print(' --log-level LEVEL Set log level, default is 2; 0=trace, 1=debug, 2=info, 3=warn, etc'); + print(' --single Run a single proxy connection and exit (default: persist for multiple connections)'); + print(' --readable-numbers Add a non-programmatic "value" key for IEEE doubles help readability'); + print(' --lenient Ignore (with warning) invalid JSON without dropping connection'); + print(' --jx-parse Parse JSON proxy input with JX, useful when testing manually'); + print(''); + return; // don't register any sockets/timers etc to exit + } else if (argv[i] == '--single') { + singleConnection = true; + continue; + } else if (argv[i] == '--readable-numbers') { + readableNumberValue = true; + continue; + } else if (argv[i] == '--lenient') { + lenientJsonParse = true; + continue; + } else if (argv[i] == '--jx-parse') { + jxParse = true; + continue; + } + if (i >= argv.length - 1) { + throw new Error('missing option value for ' + argv[i]); + } + if (argv[i] == '--server-host') { + serverHost = argv[i + 1]; + i++; + } else if (argv[i] == '--server-port') { + serverPort = Math.floor(+argv[i + 1]); + i++; + } else if (argv[i] == '--target-host') { + targetHost = argv[i + 1]; + i++; + } else if (argv[i] == '--target-port') { + targetPort = Math.floor(+argv[i + 1]); + i++; + } else if (argv[i] == '--metadata') { + metadataFile = argv[i + 1]; + i++; + } else if (argv[i] == '--log-level') { + log.l = Math.floor(+argv[i + 1]); + i++; + } else { + throw new Error('invalid option ' + argv[i]); + } + } + + function runServer() { + var serverSocket = new JsonProxyServer(serverHost, serverPort); + var connMode = singleConnection ? 'single connection mode' : 'persistent connection mode'; + log.info('Listening for incoming JSON debug connection on ' + serverHost + ':' + serverPort + + ', target is ' + targetHost + ':' + targetPort + ', ' + connMode); + } + + if (metadataFile) { + log.info('Read proxy metadata from', metadataFile); + readFully(metadataFile, function (data, err) { + if (err) { + log.error('Failed to load metadata:', err); + throw err; + } + try { + metadata = JSON.parse(String(data)); + } catch (e) { + log.error('Failed to parse JSON metadata from ' + metadataFile + ': ' + e); + throw e; + } + runServer(); + }); + } else { + runServer(); + } +} + +main(); diff --git a/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debugcommands.yaml b/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debugcommands.yaml new file mode 100644 index 000000000..14555c189 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debugcommands.yaml @@ -0,0 +1,52 @@ +# Debug request/notify command names provided by the debug client. +# These are concretely notify names now. +client_commands: + - Reserved_0 + - Status + - Print + - Alert + - Log + - Throw + - Detaching + - AppNotify + +# Debug request/notify command names provided by the debug target (Duktape). +target_commands: + - Reserved_0 + - Reserved_1 + - Reserved_2 + - Reserved_3 + - Reserved_4 + - Reserved_5 + - Reserved_6 + - Reserved_7 + - Reserved_8 + - Reserved_9 + - Reserved_10 + - Reserved_11 + - Reserved_12 + - Reserved_13 + - Reserved_14 + - Reserved_15 + - BasicInfo + - TriggerStatus + - Pause + - Resume + - StepInto + - StepOver + - StepOut + - ListBreak + - AddBreak + - DelBreak + - GetVar + - PutVar + - GetCallStack + - GetLocals + - Eval + - Detach + - DumpHeap + - GetBytecode + - AppRequest + - GetHeapObjInfo + - GetObjPropDesc + - GetObjPropDescRange diff --git a/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debugerrors.yaml b/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debugerrors.yaml new file mode 100644 index 000000000..500265f7b --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_debugerrors.yaml @@ -0,0 +1,6 @@ +error_codes: + - Unknown + - UnsupportedCommand + - TooMany + - NotFound + - ApplicationError diff --git a/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_opcodes.yaml b/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_opcodes.yaml new file mode 100644 index 000000000..67dbefb75 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/debugger/duk_opcodes.yaml @@ -0,0 +1,658 @@ +# Duktape opcode metadata for debugger. +# - See duk_debug.js for the argument formats (A_R etc). +# - Flag bits are for the whole instruction as a 32-bit integer, +# they are not field shifted +# +# NOTE: Use YAML comments only on comment-only lines (not trailing content): +# Node.js 'yamljs' seems to refuse parsing trailing comments in some cases. + +opcodes: + - name: LDREG + args: + - A_R + - BC_R + - name: STREG + args: + - A_R + - BC_R + - name: LDCONST + args: + - A_R + - BC_C + - name: LDINT + args: + - A_R + - BC_LDINT + - name: LDINTX + args: + - A_R + - BC_LDINTX + - name: MPUTOBJ + args: + - A_R + - B_R + - C_I + - name: MPUTOBJI + args: + - A_R + - B_RI + - C_I + - name: MPUTARR + args: + - A_R + - B_R + - C_I + - name: MPUTARRI + args: + - A_R + - B_RI + - C_I + - name: NEW + args: + - B_R + - C_I + - name: NEWI + args: + - B_RI + - C_I + - name: REGEXP + args: + - A_R + - B_RC + - C_RC + - name: CSREG + args: + - A_R + - B_R + - name: CSREGI + args: + - A_RI + - B_R + - name: GETVAR + args: + - A_R + - BC_C + - name: PUTVAR + args: + - A_R + - BC_C + - name: DECLVAR + args: + - A_H + - B_RC + - C_RC + flags: + - mask: 0x40 + name: writable + - mask: 0x80 + name: enumerable + - mask: 0x100 + name: configurable + - mask: 0x200 + name: accessor + - mask: 0x400 + name: undef_value + - mask: 0x800 + name: func_decl + - name: DELVAR + args: + - A_R + - B_RC + - name: CSVAR + args: + - A_R + - B_RC + - name: CSVARI + args: + - A_RI + - B_RC + - name: CLOSURE + args: + - A_R + - BC_I + - name: GETPROP + args: + - A_R + - B_RC + - C_RC + - name: PUTPROP + args: + - A_R + - B_RC + - C_RC + - name: DELPROP + args: + - A_R + - B_R + - C_RC + - name: CSPROP + args: + - A_R + - B_R + - C_RC + - name: CSPROPI + args: + - A_RI + - B_R + - C_RC + - name: ADD + args: + - A_R + - B_RC + - C_RC + - name: SUB + args: + - A_R + - B_RC + - C_RC + - name: MUL + args: + - A_R + - B_RC + - C_RC + - name: DIV + args: + - A_R + - B_RC + - C_RC + - name: MOD + args: + - A_R + - B_RC + - C_RC + - name: BAND + args: + - A_R + - B_RC + - C_RC + - name: BOR + args: + - A_R + - B_RC + - C_RC + - name: BXOR + args: + - A_R + - B_RC + - C_RC + - name: BASL + args: + - A_R + - B_RC + - C_RC + - name: BLSR + args: + - A_R + - B_RC + - C_RC + - name: BASR + args: + - A_R + - B_RC + - C_RC + - name: EQ + args: + - A_R + - B_RC + - C_RC + - name: NEQ + args: + - A_R + - B_RC + - C_RC + - name: SEQ + args: + - A_R + - B_RC + - C_RC + - name: SNEQ + args: + - A_R + - B_RC + - C_RC + - name: GT + args: + - A_R + - B_RC + - C_RC + - name: GE + args: + - A_R + - B_RC + - C_RC + - name: LT + args: + - A_R + - B_RC + - C_RC + - name: LE + args: + - A_R + - B_RC + - C_RC + - name: IF + args: + - A_B + - B_RC + - name: JUMP + args: + - ABC_JUMP + - name: RETURN + args: + - A_H + - B_RC + flags: + - mask: 0x40 + name: have_retval + - name: CALL + args: + - A_H + - B_R + - C_I + flags: + - mask: 0x40 + name: tailcall + - mask: 0x80 + name: evalcall + - name: CALLI + args: + - A_H + - B_RI + - C_I + - name: TRYCATCH + args: + - A_H + # base register for two consecutive regs (base_reg + 0, base_reg + 1) used for two things: + # - input: either 'with' target register or catch varname constant (base_reg + 0), depending on flags + # - output: when caught, catch value (base_reg + 0) and type (base_reg + 1) + - BC_R + flags: + - mask: 0x40 + name: have_catch + - mask: 0x80 + name: have_finally + - mask: 0x100 + name: catch_binding + - mask: 0x200 + name: with_binding + - name: EXTRA + extra: true + - name: PREINCR + args: + - A_R + - BC_R + - name: PREDECR + args: + - A_R + - BC_R + - name: POSTINCR + args: + - A_R + - BC_R + - name: POSTDECR + args: + - A_R + - BC_R + - name: PREINCV + args: + - A_R + - BC_C + - name: PREDECV + args: + - A_R + - BC_C + - name: POSTINCV + args: + - A_R + - BC_C + - name: POSTDECV + args: + - A_R + - BC_C + - name: PREINCP + args: + - A_R + - B_RC + - C_RC + - name: PREDECP + args: + - A_R + - B_RC + - C_RC + - name: POSTINCP + args: + - A_R + - B_RC + - C_RC + - name: POSTDECP + args: + - A_R + - B_RC + - C_RC + +extra: + - name: NOP + - name: INVALID + args: + - BC_I + - name: LDTHIS + args: + - BC_R + - name: LDUNDEF + args: + - BC_R + - name: LDNULL + args: + - BC_R + - name: LDTRUE + args: + - BC_R + - name: LDFALSE + args: + - BC_R + - name: NEWOBJ + args: + # XXX: extend to BC? + - B_R + - name: NEWARR + args: + # XXX: extend to BC? + - B_R + - name: SETALEN + args: + - B_R + - C_R + - name: TYPEOF + args: + - BC_R + - name: TYPEOFID + args: + - B_R + # maybe changed to C_C later + - C_RC + - name: INITENUM + args: + - B_R + - C_R + - name: NEXTENUM + args: + - B_R + - C_R + - name: INITSET + args: + - B_R + - C_R + - name: INITSETI + args: + - B_R + - C_RI + - name: INITGET + args: + - B_R + - C_RI + - name: INITGETI + args: + - B_R + - C_RI + - name: ENDTRY + - name: ENDCATCH + - name: ENDFIN + - name: THROW + args: + - BC_R + - name: INVLHS + - name: UNM + args: + - BC_R + - name: UNP + args: + - BC_R + - name: DEBUGGER + - name: BREAK + args: + - BC_I + - name: CONTINUE + args: + - BC_I + - name: BNOT + args: + - BC_R + - name: LNOT + args: + - BC_R + - name: INSTOF + args: + - B_R + - C_RC + - name: IN + args: + - B_R + - C_RC + - name: LABEL + args: + - BC_I + - name: ENDLABEL + args: + - BC_I + - name: EXTRA34 + - name: EXTRA35 + - name: EXTRA36 + - name: EXTRA37 + - name: EXTRA38 + - name: EXTRA39 + - name: EXTRA40 + - name: EXTRA41 + - name: EXTRA42 + - name: EXTRA43 + - name: EXTRA44 + - name: EXTRA45 + - name: EXTRA46 + - name: EXTRA47 + - name: EXTRA48 + - name: EXTRA49 + - name: EXTRA50 + - name: EXTRA51 + - name: EXTRA52 + - name: EXTRA53 + - name: EXTRA54 + - name: EXTRA55 + - name: EXTRA56 + - name: EXTRA57 + - name: EXTRA58 + - name: EXTRA59 + - name: EXTRA60 + - name: EXTRA61 + - name: EXTRA62 + - name: EXTRA63 + - name: EXTRA64 + - name: EXTRA65 + - name: EXTRA66 + - name: EXTRA67 + - name: EXTRA68 + - name: EXTRA69 + - name: EXTRA70 + - name: EXTRA71 + - name: EXTRA72 + - name: EXTRA73 + - name: EXTRA74 + - name: EXTRA75 + - name: EXTRA76 + - name: EXTRA77 + - name: EXTRA78 + - name: EXTRA79 + - name: EXTRA80 + - name: EXTRA81 + - name: EXTRA82 + - name: EXTRA83 + - name: EXTRA84 + - name: EXTRA85 + - name: EXTRA86 + - name: EXTRA87 + - name: EXTRA88 + - name: EXTRA89 + - name: EXTRA90 + - name: EXTRA91 + - name: EXTRA92 + - name: EXTRA93 + - name: EXTRA94 + - name: EXTRA95 + - name: EXTRA96 + - name: EXTRA97 + - name: EXTRA98 + - name: EXTRA99 + - name: EXTRA100 + - name: EXTRA101 + - name: EXTRA102 + - name: EXTRA103 + - name: EXTRA104 + - name: EXTRA105 + - name: EXTRA106 + - name: EXTRA107 + - name: EXTRA108 + - name: EXTRA109 + - name: EXTRA110 + - name: EXTRA111 + - name: EXTRA112 + - name: EXTRA113 + - name: EXTRA114 + - name: EXTRA115 + - name: EXTRA116 + - name: EXTRA117 + - name: EXTRA118 + - name: EXTRA119 + - name: EXTRA120 + - name: EXTRA121 + - name: EXTRA122 + - name: EXTRA123 + - name: EXTRA124 + - name: EXTRA125 + - name: EXTRA126 + - name: EXTRA127 + - name: EXTRA128 + - name: EXTRA129 + - name: EXTRA130 + - name: EXTRA131 + - name: EXTRA132 + - name: EXTRA133 + - name: EXTRA134 + - name: EXTRA135 + - name: EXTRA136 + - name: EXTRA137 + - name: EXTRA138 + - name: EXTRA139 + - name: EXTRA140 + - name: EXTRA141 + - name: EXTRA142 + - name: EXTRA143 + - name: EXTRA144 + - name: EXTRA145 + - name: EXTRA146 + - name: EXTRA147 + - name: EXTRA148 + - name: EXTRA149 + - name: EXTRA150 + - name: EXTRA151 + - name: EXTRA152 + - name: EXTRA153 + - name: EXTRA154 + - name: EXTRA155 + - name: EXTRA156 + - name: EXTRA157 + - name: EXTRA158 + - name: EXTRA159 + - name: EXTRA160 + - name: EXTRA161 + - name: EXTRA162 + - name: EXTRA163 + - name: EXTRA164 + - name: EXTRA165 + - name: EXTRA166 + - name: EXTRA167 + - name: EXTRA168 + - name: EXTRA169 + - name: EXTRA170 + - name: EXTRA171 + - name: EXTRA172 + - name: EXTRA173 + - name: EXTRA174 + - name: EXTRA175 + - name: EXTRA176 + - name: EXTRA177 + - name: EXTRA178 + - name: EXTRA179 + - name: EXTRA180 + - name: EXTRA181 + - name: EXTRA182 + - name: EXTRA183 + - name: EXTRA184 + - name: EXTRA185 + - name: EXTRA186 + - name: EXTRA187 + - name: EXTRA188 + - name: EXTRA189 + - name: EXTRA190 + - name: EXTRA191 + - name: EXTRA192 + - name: EXTRA193 + - name: EXTRA194 + - name: EXTRA195 + - name: EXTRA196 + - name: EXTRA197 + - name: EXTRA198 + - name: EXTRA199 + - name: EXTRA200 + - name: EXTRA201 + - name: EXTRA202 + - name: EXTRA203 + - name: EXTRA204 + - name: EXTRA205 + - name: EXTRA206 + - name: EXTRA207 + - name: EXTRA208 + - name: EXTRA209 + - name: EXTRA210 + - name: EXTRA211 + - name: EXTRA212 + - name: EXTRA213 + - name: EXTRA214 + - name: EXTRA215 + - name: EXTRA216 + - name: EXTRA217 + - name: EXTRA218 + - name: EXTRA219 + - name: EXTRA220 + - name: EXTRA221 + - name: EXTRA222 + - name: EXTRA223 + - name: EXTRA224 + - name: EXTRA225 + - name: EXTRA226 + - name: EXTRA227 + - name: EXTRA228 + - name: EXTRA229 + - name: EXTRA230 + - name: EXTRA231 + - name: EXTRA232 + - name: EXTRA233 + - name: EXTRA234 + - name: EXTRA235 + - name: EXTRA236 + - name: EXTRA237 + - name: EXTRA238 + - name: EXTRA239 + - name: EXTRA240 + - name: EXTRA241 + - name: EXTRA242 + - name: EXTRA243 + - name: EXTRA244 + - name: EXTRA245 + - name: EXTRA246 + - name: EXTRA247 + - name: EXTRA248 + - name: EXTRA249 + - name: EXTRA250 + - name: EXTRA251 + - name: EXTRA252 + - name: EXTRA253 + - name: EXTRA254 + - name: EXTRA255 diff --git a/src/civetweb/src/third_party/duktape-1.5.2/debugger/merge_debug_meta.py b/src/civetweb/src/third_party/duktape-1.5.2/debugger/merge_debug_meta.py new file mode 100644 index 000000000..ba9b38e3d --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/debugger/merge_debug_meta.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python2 +# +# Merge debugger YAML metadata files and output a merged JSON metadata file. +# + +import os, sys, json, yaml +import optparse + +if __name__ == '__main__': + parser = optparse.OptionParser() + parser.add_option('--output', dest='output', default=None, help='output JSON filename') + parser.add_option('--class-names', dest='class_names', help='YAML metadata for class names') + parser.add_option('--debug-commands', dest='debug_commands', help='YAML metadata for debug commands') + parser.add_option('--debug-errors', dest='debug_errors', help='YAML metadata for debug protocol error codes') + parser.add_option('--opcodes', dest='opcodes', help='YAML metadata for opcodes') + (opts, args) = parser.parse_args() + + res = {} + def merge(fn): + with open(fn, 'rb') as f: + doc = yaml.load(f) + for k in doc.keys(): + res[k] = doc[k] + + merge(opts.class_names) + merge(opts.debug_commands) + merge(opts.debug_errors) + merge(opts.opcodes) + + with open(opts.output, 'wb') as f: + f.write(json.dumps(res, indent=4) + '\n') + print('Wrote merged debugger metadata to ' + str(opts.output)) diff --git a/src/civetweb/src/third_party/duktape-1.5.2/debugger/package.json b/src/civetweb/src/third_party/duktape-1.5.2/debugger/package.json new file mode 100644 index 000000000..961657448 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/debugger/package.json @@ -0,0 +1,27 @@ +{ + "name": "duk-debug", + "version": "0.1.0", + "description": "Duktape debugger", + "author": { + "name": "Sami Vaarala", + "email": "sami.vaarala@iki.fi" + }, + "dependencies": { + "bluebird": "~2.6.4", + "minimist": "~1.1.0", + "express": "~4.10.1", + "body-parser": "~1.9.3", + "socket.io": "~1.2.1", + "utf8": "~2.0.0", + "wrench": "~1.5.8", + "sprintf": "~0.1.5", + "events": "~1.0.2", + "stream": "0.0.2", + "readline": "0.0.5", + "util": "~0.10.3", + "http": "0.0.0", + "yamljs": "~0.2.1", + "byline": "~4.2.1" + }, + "main": "duk_debug.js" +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/debugger/static/index.html b/src/civetweb/src/third_party/duktape-1.5.2/debugger/static/index.html new file mode 100644 index 000000000..5e5e2e05f --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/debugger/static/index.html @@ -0,0 +1,96 @@ + + + + + + + +Duktape debugger + + + +
+((o) Duktape debugger +
+ +
+ +
+ + + + + +
+
+
+
+ + + + + +
+ +
+

+// No source loaded
+
+
+
+ +
+
+
?
+
?
?
+
+
+
(output from script, print() and alert() calls)
+
+
+ +
+
+
(callstack)
+
+
+
(locals)
+
+
+
(breakpoints)
+
+
+ watch (eval on pause) +
+ +
+
+
+ +
+ + + +
+

Duktape debugger is a web UI for debugging Ecmascript on a target device.

+

This web UI talks to a NodeJS debug server using socket.io. +The debug server talks to the target device using the Duktape debug protocol +(see debugger.rst).

+
+ +
+

+
+ + + + + + + diff --git a/src/civetweb/src/third_party/duktape-1.5.2/debugger/static/style.css b/src/civetweb/src/third_party/duktape-1.5.2/debugger/static/style.css new file mode 100644 index 000000000..a1059478f --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/debugger/static/style.css @@ -0,0 +1,516 @@ +// http://stackoverflow.com/questions/71074/how-to-remove-firefoxs-dotted-outline-on-buttons-as-well-as-links/3844452#3844452 +:focus { + outline: none; +} +::-moz-focus-inner { + border: 0; +} + +@keyframes pulsate { + from { opacity: 1; } + to { opacity: 0.25; } +} + +#part-header { + background: #444444; + color: #ffffff; + font: 24pt monospace; + border-bottom: 2px solid #cccccc; + padding: 20px 0px 20px 10px; +} + +/* http://css-tricks.com/snippets/css/a-guide-to-flexbox/ */ +#part-middle { + display: flex; + flex-direction: row; + flex-wrap: nowrap; + justify-content: space-between; + align-items: stretch; + align-content: stretch; + + min-height: 800px; + + border-top: 1px solid #ffffff; + padding: 8px; + margin-top: 2px; +} +#left-area { + flex: 0 0 11em; + margin-right: 20px; + margin-bottom: 10px; +} +#center-area { + flex: 1 1 0; + margin-bottom: 10px; +} +#right-area { + flex: 0 0 40em; + margin-left: 20px; + margin-bottom: 10px; +} + +#part-footer { + clear: both; + border-top: 2px solid #bbbbbb; + background: #eeeeee; + color: #555555; + text-align: center; + padding-top: 12px; + padding-bottom: 12px; + line-height: 1.5; +} + +#exec-status { + margin-top: 25px; + margin-bottom: 25px; +} +#exec-state { + display: inline-block; + vertical-align: middle; +} +#exec-other { + display: inline-block; + vertical-align: middle; + font-size: 125%; +} +#current-state { + background: #228822; + color: #ffffff; + font: 16pt; + padding: 6pt; + border: 5px solid #228822; + border-radius: 10px; + font-size: 200%; + font-weight: bold; + margin-right: 10px; +} +#current-state.notrunning { + background: #882222; + border: 5px solid #882222; + border-radius: 10px; + animation: pulsate 0.7s cubic-bezier(0.75, 0, 0.75, 1) infinite alternate; +} +#exec-other:hover { + text-decoration: underline; + color: #9999ff; +} + +#left-area button { + display: inline-block; + width: 100%; + min-width: 8em; + background: #226622; + color: #ffffff; + font: 16pt sans-serif; + font-weight: bold; + text-decoration: none; + margin: 10px 0 0 0; + padding: 0.4em; + border: 2px solid #000000; + border-radius: 4px; +} +#left-area button a { + color: #ffffff; + text-decoration: none; +} +#left-area button:hover { + background: #55aa55; +} +#left-area button:disabled { + background: #555555; + color: #888888; +} +#left-area button:disabled a { + background: #555555; + color: #888888; +} + +#pause-button.pending { + background: #5555ff; + animation: pulsate 0.2s cubic-bezier(0.75, 0, 0.75, 1) infinite alternate; +} + +#attach-button { +} +#attach-button.enabled { + animation: pulsate 0.7s cubic-bezier(0.75, 0, 0.75, 1) infinite alternate; +} + +.duktape-exec-line { + outline: 2px solid red; + background: #550000; +} +.duktape-break-line { + outline: 2px solid white; +} + +#output { + font: 9pt monospace; + color: #000000; + border: 2px solid #cccccc; + border-radius: 5px; + padding: 3px; + height: 30ex; + overflow: scroll; + overflow-x: auto; + overflow-y: scroll; + white-space: pre; +} +#output .alert { + color: #ff0000; +} +/* Default color (should be overridden by level) */ +#output .log { + color: #00ff00; +} +/* Trace */ +#output .loglevel0 { + color: #cccccc; +} +/* Debug */ +#output .loglevel1 { + color: #cccccc; +} +/* Info */ +#output .loglevel2 { + color: #888888; + font-weight: bold; +} +/* Warn */ +#output .loglevel3 { + color: #ff4444; + font-weight: bold; +} +/* Error */ +#output .loglevel4 { + color: #ff0000; + font-weight: bold; +} +/* Fatal */ +#output .loglevel5 { + background: #000000; + color: #ff0000; + font-weight: bold; +} +#output .debugger-info { + color: #880000; + font-weight: bold; + font-style: italic; +} +#output .debugger-debug { + color: #888888; + font-weight: bold; + font-style: italic; +} + +#callstack { + font: 9pt monospace; + color: #000000; + margin-top: 10px; + border: 2px solid #cccccc; + border-radius: 5px; + padding: 3px; + height: 14ex; + overflow: scroll; + overflow-x: auto; + overflow-y: scroll; + white-space: pre; +} +#callstack div:nth-child(2n) { + background: #eeeeee; +} +#callstack .func { +} +#callstack .rest { + float: right; + color: #6666ff; +} +#callstack .rest:hover { + text-decoration: underline; + color: #9999ff; +} + +#locals { + font: 9pt monospace; + color: #000000; + margin-top: 10px; + border: 2px solid #cccccc; + border-radius: 5px; + padding: 10px; + height: 30ex; + overflow: scroll; + overflow-x: auto; + overflow-y: scroll; + white-space: pre; +} +#locals div:nth-child(2n) { + background: #eeeeee; +} +#locals .key { +} +#locals .value { + float: right; + color: #888888; +} + +#breakpoints { + color: #000000; + margin-top: 10px; + border: 2px solid #cccccc; + border-radius: 5px; + padding: 3px; + height: 15ex; + overflow: scroll; + overflow-x: auto; + overflow-y: scroll; + white-space: pre; +} +#breakpoints div { + margin: 2px 0 2px 0; +} +#breakpoints div:nth-child(2n) { + background: #eeeeee; +} +#breakpoints a { + font: 9pt monospace; + color: #6666ff; +} +#breakpoints a:hover { + text-decoration: underline; + color: #9999ff; +} +.breakpoint-line { + clear: both; + padding-top: 2px; + padding-bottom: 2px; +} +#add-breakpoint-file { + font: 10pt monospace; + width: 10em; + padding: 5px; +} +#add-breakpoint-line { + font: 10pt monospace; + width: 3em; + margin-left: 3px; + padding: 5px; +} +#delete-all-breakpoints-button { + float: right; + font: 10pt sans-serif; + padding: 5px; + border: 1px solid #888888; + background: #ddffdd; + color: #000000; +} +#delete-all-breakpoints-button:hover { + background: #f8fff8; +} +#delete-all-breakpoints-button:disabled { + background: #dddddd; + color: #444444; +} +#add-breakpoint-button { + font: 10pt sans-serif; + margin-left: 10px; + padding: 5px; + border: 1px solid #888888; + background: #ddffdd; + color: #000000; +} +#add-breakpoint-button:hover { + background: #f8fff8; +} +#add-breakpoint-button:disabled { + background: #dddddd; + color: #444444; +} +#breakpoint-hint { + color: #aaaaaa; + font-style: italic; + margin-left: 10px; +} +.delete-breakpoint-button { + float: right; + display: inline; + font: 9pt sans-serif; + padding: 3px; + border: none; + background: none; + color: #6666ff; +} +.delete-breakpoint-button { + font: 9pt sans-serif; +} +.delete-breakpoint-button:hover { + text-decoration: underline; + color: #9999ff; +} +.delete-breakpoint-button:disabled { + color: #888888; +} + +#about-dialog p { + margin: 10px 0 10px 0; +} + +#bytecode-dialog p { + margin: 10px 0 10px 0; +} +#bytecode-dialog pre { + font: 10pt monospace; + color: #000000; +} +#bytecode-dialog div.highlight { + background: #888888; + color: #ffffff; +} + +#eval { + color: #000000; + margin-top: 10px; + border: 2px solid #cccccc; + border-radius: 5px; + padding: 3px; + height: 30ex; + overflow: scroll; + overflow-x: auto; + overflow-y: scroll; + white-space: pre; +} +#eval-input { + display: inline; + font: 10pt monospace; + width: 20em; + padding: 5px; +} +#eval-button { + display: inline; + margin-left: 10px; + padding: 5px; + border: 1px solid #888888; + font: 10pt sans-serif; + background: #ddffdd; + color: #000000; +} +#eval-button { +} +#eval-button:hover { + background: #f8fff8; +} +#eval-button:disabled { + background: #dddddd; + color: #444444; +} +#eval-button.pending { + background: #5555ff; + animation: pulsate 0.2s cubic-bezier(0.75, 0, 0.75, 1) infinite alternate; +} +#eval-watch { + margin-left: 20px; + vertical-align: middle; +} +#eval-output { + font: 10pt monospace; + white-space: pre; + padding: 5px; + border: 1px solid #888888; + min-height: 4ex; + margin-top: 5px; +} + +#varname-input { + font: 10pt monospace; + width: 10em; + padding: 5px; +} +#varvalue-input { + margin-left: 10px; + font: 10pt monospace; + width: 20em; + padding: 5px; +} +#getvar-button, +#putvar-button { + display: inline; + float: right; + margin-left: 10px; + padding: 5px; + border: 1px solid #888888; + font: 10pt sans-serif; + background: #ddffdd; + color: #000000; +} +#getvar-button:hover, +#putvar-button:hover { + background: #f8fff8; +} +#getvar-button:disabled, +#putvar-button:disabled { + background: #dddddd; + color: #444444; +} +#var-output { + font: 10pt monospace; + white-space: pre; + padding: 5px; + border: 1px solid #888888; + min-height: 4ex; + margin-top: 5px; +} + +#source-pre { + margin-top: 10px; + border: 2px solid #cccccc; + border-radius: 5px; + height: 400px; + overflow: scroll; + overflow-x: auto; + overflow-y: scroll; +} +#source-pre.running { + background: #eeeeee; + color: #888888; +} +#source-pre.running #source-code { + background: #eeeeee; + color: #888888; +} +#source-filename { + font-size: 125%; + color: #888888; +} +code.sourcecode { + counter-reset: source-line; +} +code.sourcecode div { + font: 10pt monospace; + padding: 2px 5px 2px 5px; + white-space: pre; + border-bottom: 1px solid #eeeeee; +} +code.sourcecode div:before { + display: inline-block; + content: counter(source-line); + counter-increment: source-line; + width: 4em; + color: #888888; + text-align: right; + margin-right: 20px; +} +code.sourcecode div.breakpoint:before { + margin-right: 0px; + border-right: 20px solid #ff0000; +} +code.sourcecode div.highlight { + background: #aaaaaa; + color: #000000; +} +code.sourcecode div.execution { + background: #000000; + color: #ffffff; +} + +#source-select { + margin-top: 5px; +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/debugger/static/webui.js b/src/civetweb/src/third_party/duktape-1.5.2/debugger/static/webui.js new file mode 100644 index 000000000..d400c6c35 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/debugger/static/webui.js @@ -0,0 +1,785 @@ +/* + * Duktape debugger web client + * + * Talks to the NodeJS server using socket.io. + * + * http://unixpapa.com/js/key.html + */ + +// Update interval for custom source highlighting. +var SOURCE_UPDATE_INTERVAL = 350; + +// Source view +var activeFileName = null; // file that we want to be loaded in source view +var activeLine = null; // scroll to line once file has been loaded +var activeHighlight = null; // line that we want to highlight (if any) +var loadedFileName = null; // currently loaded (shown) file +var loadedLineCount = 0; // currently loaded file line count +var loadedFileExecuting = false; // true if currFileName (loosely) matches loadedFileName +var loadedLinePending = null; // if set, scroll loaded file to requested line +var highlightLine = null; // highlight line +var sourceEditedLines = []; // line numbers which have been modified + // (added classes etc, tracked for removing) +var sourceUpdateInterval = null; // timer for updating source view +var sourceFetchXhr = null; // current AJAX request for fetching a source file (if any) +var forceButtonUpdate = false; // hack to reset button states +var bytecodeDialogOpen = false; // bytecode dialog active +var bytecodeIdxHighlight = null; // index of currently highlighted line (or null) +var bytecodeIdxInstr = 0; // index to first line of bytecode instructions + +// Execution state +var prevState = null; // previous execution state ('paused', 'running', etc) +var prevAttached = null; // previous debugger attached state (true, false, null) +var currFileName = null; // current filename being executed +var currFuncName = null; // current function name being executed +var currLine = 0; // current line being executed +var currPc = 0; // current bytecode PC being executed +var currState = 0; // current execution state ('paused', 'running', 'detached', etc) +var currAttached = false; // current debugger attached state (true or false) +var currLocals = []; // current local variables +var currCallstack = []; // current callstack (from top to bottom) +var currBreakpoints = []; // current breakpoints +var startedRunning = 0; // timestamp when last started running (if running) + // (used to grey out the source file if running for long enough) + +/* + * Helpers + */ + +function formatBytes(x) { + if (x < 1024) { + return String(x) + ' bytes'; + } else if (x < 1024 * 1024) { + return (x / 1024).toPrecision(3) + ' kB'; + } else { + return (x / (1024 * 1024)).toPrecision(3) + ' MB'; + } +} + +/* + * Source view periodic update handling + */ + +function doSourceUpdate() { + var elem; + + // Remove previously added custom classes + sourceEditedLines.forEach(function (linenum) { + elem = $('#source-code div')[linenum - 1]; + if (elem) { + elem.classList.remove('breakpoint'); + elem.classList.remove('execution'); + elem.classList.remove('highlight'); + } + }); + sourceEditedLines.length = 0; + + // If we're executing the file shown, highlight current line + if (loadedFileExecuting) { + elem = $('#source-code div')[currLine - 1]; + if (elem) { + sourceEditedLines.push(currLine); + elem.classList.add('execution'); + } + } + + // Add breakpoints + currBreakpoints.forEach(function (bp) { + if (bp.fileName === loadedFileName) { + elem = $('#source-code div')[bp.lineNumber - 1]; + if (elem) { + sourceEditedLines.push(bp.lineNumber); + elem.classList.add('breakpoint'); + } + } + }); + + if (highlightLine !== null) { + elem = $('#source-code div')[highlightLine - 1]; + if (elem) { + sourceEditedLines.push(highlightLine); + elem.classList.add('highlight'); + } + } + + // Bytecode dialog highlight + if (loadedFileExecuting && bytecodeDialogOpen && bytecodeIdxHighlight !== bytecodeIdxInstr + currPc) { + if (typeof bytecodeIdxHighlight === 'number') { + $('#bytecode-preformatted div')[bytecodeIdxHighlight].classList.remove('highlight'); + } + bytecodeIdxHighlight = bytecodeIdxInstr + currPc; + $('#bytecode-preformatted div')[bytecodeIdxHighlight].classList.add('highlight'); + } + + // If no-one requested us to scroll to a specific line, finish. + if (loadedLinePending == null) { + return; + } + + var reqLine = loadedLinePending; + loadedLinePending = null; + + // Scroll to requested line. This is not very clean, so a better solution + // should be found: + // https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollIntoView + // http://erraticdev.blogspot.fi/2011/02/jquery-scroll-into-view-plugin-with.html + // http://flesler.blogspot.fi/2007/10/jqueryscrollto.html + var tmpLine = Math.max(reqLine - 5, 0); + elem = $('#source-code div')[tmpLine]; + if (elem) { + elem.scrollIntoView(); + } +} + +// Source is updated periodically. Other code can also call doSourceUpdate() +// directly if an immediate update is needed. +sourceUpdateInterval = setInterval(doSourceUpdate, SOURCE_UPDATE_INTERVAL); + +/* + * UI update handling when exec-status update arrives + */ + +function doUiUpdate() { + var now = Date.now(); + + // Note: loadedFileName can be either from target or from server, but they + // must match exactly. We could do a loose match here, but exact matches + // are needed for proper breakpoint handling anyway. + loadedFileExecuting = (loadedFileName === currFileName); + + // If we just started running, store a timestamp so we can grey out the + // source view only if we execute long enough (i.e. we're not just + // stepping). + if (currState !== prevState && currState === 'running') { + startedRunning = now; + } + + // If we just became paused, check for eval watch + if (currState !== prevState && currState === 'paused') { + if ($('#eval-watch').is(':checked')) { + submitEval(); // don't clear eval input + } + } + + // Update current execution state + if (currFileName === '' && currLine === 0) { + $('#current-fileline').text(''); + } else { + $('#current-fileline').text(String(currFileName) + ':' + String(currLine)); + } + if (currFuncName === '' && currPc === 0) { + $('#current-funcpc').text(''); + } else { + $('#current-funcpc').text(String(currFuncName) + '() pc ' + String(currPc)); + } + $('#current-state').text(String(currState)); + + // Update buttons + if (currState !== prevState || currAttached !== prevAttached || forceButtonUpdate) { + $('#stepinto-button').prop('disabled', !currAttached || currState !== 'paused'); + $('#stepover-button').prop('disabled', !currAttached || currState !== 'paused'); + $('#stepout-button').prop('disabled', !currAttached || currState !== 'paused'); + $('#resume-button').prop('disabled', !currAttached || currState !== 'paused'); + $('#pause-button').prop('disabled', !currAttached || currState !== 'running'); + $('#attach-button').prop('disabled', currAttached); + if (currAttached) { + $('#attach-button').removeClass('enabled'); + } else { + $('#attach-button').addClass('enabled'); + } + $('#detach-button').prop('disabled', !currAttached); + $('#eval-button').prop('disabled', !currAttached); + $('#add-breakpoint-button').prop('disabled', !currAttached); + $('#delete-all-breakpoints-button').prop('disabled', !currAttached); + $('.delete-breakpoint-button').prop('disabled', !currAttached); + $('#putvar-button').prop('disabled', !currAttached); + $('#getvar-button').prop('disabled', !currAttached); + $('#heap-dump-download-button').prop('disabled', !currAttached); + } + if (currState !== 'running' || forceButtonUpdate) { + // Remove pending highlight once we're no longer running. + $('#pause-button').removeClass('pending'); + $('#eval-button').removeClass('pending'); + } + forceButtonUpdate = false; + + // Make source window grey when running for a longer time, use a small + // delay to avoid flashing grey when stepping. + if (currState === 'running' && now - startedRunning >= 500) { + $('#source-pre').removeClass('notrunning'); + $('#current-state').removeClass('notrunning'); + } else { + $('#source-pre').addClass('notrunning'); + $('#current-state').addClass('notrunning'); + } + + // Force source view to match currFileName only when running or when + // just became paused (from running or detached). + var fetchSource = false; + if (typeof currFileName === 'string') { + if (currState === 'running' || + (prevState !== 'paused' && currState === 'paused') || + (currAttached !== prevAttached)) { + if (activeFileName !== currFileName) { + fetchSource = true; + activeFileName = currFileName; + activeLine = currLine; + activeHighlight = null; + requestSourceRefetch(); + } + } + } + + // Force line update (scrollTop) only when running or just became paused. + // Otherwise let user browse and scroll source files freely. + if (!fetchSource) { + if ((prevState !== 'paused' && currState === 'paused') || + currState === 'running') { + loadedLinePending = currLine || 0; + } + } +} + +/* + * Init socket.io and add handlers + */ + +var socket = io(); // returns a Manager + +setInterval(function () { + socket.emit('keepalive', { + userAgent: (navigator || {}).userAgent + }); +}, 30000); + +socket.on('connect', function () { + $('#socketio-info').text('connected'); + currState = 'connected'; + + fetchSourceList(); +}); +socket.on('disconnect', function () { + $('#socketio-info').text('not connected'); + currState = 'disconnected'; +}); +socket.on('reconnecting', function () { + $('#socketio-info').text('reconnecting'); + currState = 'reconnecting'; +}); +socket.on('error', function (err) { + $('#socketio-info').text(err); +}); + +socket.on('replaced', function () { + // XXX: how to minimize the chance we'll further communciate with the + // server or reconnect to it? socket.reconnection()? + + // We'd like to window.close() here but can't (not allowed from scripts). + // Alert is the next best thing. + alert('Debugger connection replaced by a new one, do you have multiple tabs open? If so, please close this tab.'); +}); + +socket.on('keepalive', function (msg) { + // Not really interesting in the UI + // $('#server-info').text(new Date() + ': ' + JSON.stringify(msg)); +}); + +socket.on('basic-info', function (msg) { + $('#duk-version').text(String(msg.duk_version)); + $('#duk-git-describe').text(String(msg.duk_git_describe)); + $('#target-info').text(String(msg.target_info)); + $('#endianness').text(String(msg.endianness)); +}); + +socket.on('exec-status', function (msg) { + // Not 100% reliable if callstack has several functions of the same name + if (bytecodeDialogOpen && (currFileName != msg.fileName || currFuncName != msg.funcName)) { + socket.emit('get-bytecode', {}); + } + + currFileName = msg.fileName; + currFuncName = msg.funcName; + currLine = msg.line; + currPc = msg.pc; + currState = msg.state; + currAttached = msg.attached; + + // Duktape now restricts execution status updates quite effectively so + // there's no need to rate limit UI updates now. + + doUiUpdate(); + + prevState = currState; + prevAttached = currAttached; +}); + +// Update the "console" output based on lines sent by the server. The server +// rate limits these updates to keep the browser load under control. Even +// better would be for the client to pull this (and other stuff) on its own. +socket.on('output-lines', function (msg) { + var elem = $('#output'); + var i, n, ent; + + elem.empty(); + for (i = 0, n = msg.length; i < n; i++) { + ent = msg[i]; + if (ent.type === 'print') { + elem.append($('
').text(ent.message)); + } else if (ent.type === 'alert') { + elem.append($('
').text(ent.message)); + } else if (ent.type === 'log') { + elem.append($('
').text(ent.message)); + } else if (ent.type === 'debugger-info') { + elem.append($('
').text(ent.message)); + } else if (ent.type === 'debugger-debug') { + elem.append($('
').text(ent.message)); + } else { + elem.append($('
').text(ent.message)); + } + } + + // http://stackoverflow.com/questions/14918787/jquery-scroll-to-bottom-of-div-even-after-it-updates + // Stop queued animations so that we always scroll quickly to bottom + $('#output').stop(true); + $('#output').animate({ scrollTop: $('#output')[0].scrollHeight}, 1000); +}); + +socket.on('callstack', function (msg) { + var elem = $('#callstack'); + var s1, s2, div; + + currCallstack = msg.callstack; + + elem.empty(); + msg.callstack.forEach(function (e) { + s1 = $('').text(e.fileName + ':' + e.lineNumber + ' (pc ' + e.pc + ')'); // float + s1.on('click', function () { + activeFileName = e.fileName; + activeLine = e.lineNumber || 1; + activeHighlight = activeLine; + requestSourceRefetch(); + }); + s2 = $('').text(e.funcName + '()'); + div = $('
'); + div.append(s1); + div.append(s2); + elem.append(div); + }); +}); + +socket.on('locals', function (msg) { + var elem = $('#locals'); + var s1, s2, div; + var i, n, e; + + currLocals = msg.locals; + + elem.empty(); + for (i = 0, n = msg.locals.length; i < n; i++) { + e = msg.locals[i]; + s1 = $('').text(e.value); // float + s2 = $('').text(e.key); + div = $('
'); + div.append(s1); + div.append(s2); + elem.append(div); + } +}); + +socket.on('debug-stats', function (msg) { + $('#debug-rx-bytes').text(formatBytes(msg.rxBytes)); + $('#debug-rx-dvalues').text(msg.rxDvalues); + $('#debug-rx-messages').text(msg.rxMessages); + $('#debug-rx-kbrate').text((msg.rxBytesPerSec / 1024).toFixed(2)); + $('#debug-tx-bytes').text(formatBytes(msg.txBytes)); + $('#debug-tx-dvalues').text(msg.txDvalues); + $('#debug-tx-messages').text(msg.txMessages); + $('#debug-tx-kbrate').text((msg.txBytesPerSec / 1024).toFixed(2)); +}); + +socket.on('breakpoints', function (msg) { + var elem = $('#breakpoints'); + var div; + var sub; + + currBreakpoints = msg.breakpoints; + + elem.empty(); + + // First line is special + div = $('
'); + sub = $('').text('Delete all breakpoints'); + sub.on('click', function () { + socket.emit('delete-all-breakpoints'); + }); + div.append(sub); + sub = $('').val('file.js'); + div.append(sub); + sub = $('').text(':'); + div.append(sub); + sub = $('').val('123'); + div.append(sub); + sub = $('').text('Add breakpoint'); + sub.on('click', function () { + socket.emit('add-breakpoint', { + fileName: $('#add-breakpoint-file').val(), + lineNumber: Number($('#add-breakpoint-line').val()) + }); + }); + div.append(sub); + sub = $('').text('or dblclick source'); + div.append(sub); + elem.append(div); + + // Active breakpoints follow + msg.breakpoints.forEach(function (bp) { + var div; + var sub; + + div = $('
'); + sub = $('').text('Delete'); + sub.on('click', function () { + socket.emit('delete-breakpoint', { + fileName: bp.fileName, + lineNumber: bp.lineNumber + }); + }); + div.append(sub); + sub = $('').text((bp.fileName || '?') + ':' + (bp.lineNumber || 0)); + sub.on('click', function () { + activeFileName = bp.fileName || ''; + activeLine = bp.lineNumber || 1; + activeHighlight = activeLine; + requestSourceRefetch(); + }); + div.append(sub); + elem.append(div); + }); + + forceButtonUpdate = true; + doUiUpdate(); +}); + +socket.on('eval-result', function (msg) { + $('#eval-output').text((msg.error ? 'ERROR: ' : '') + msg.result); + + // Remove eval button "pulsating" glow when we get a result + $('#eval-button').removeClass('pending'); +}); + +socket.on('getvar-result', function (msg) { + $('#var-output').text(msg.found ? msg.result : 'NOTFOUND'); +}); + +socket.on('bytecode', function (msg) { + var elem, div; + var div; + + elem = $('#bytecode-preformatted'); + elem.empty(); + + msg.preformatted.split('\n').forEach(function (line, idx) { + div = $('
'); + div.text(line); + elem.append(div); + }); + + bytecodeIdxHighlight = null; + bytecodeIdxInstr = msg.idxPreformattedInstructions; +}); + +$('#stepinto-button').click(function () { + socket.emit('stepinto', {}); +}); + +$('#stepover-button').click(function () { + socket.emit('stepover', {}); +}); + +$('#stepout-button').click(function () { + socket.emit('stepout', {}); +}); + +$('#pause-button').click(function () { + socket.emit('pause', {}); + + // Pause may take seconds to complete so indicate it is pending. + $('#pause-button').addClass('pending'); +}); + +$('#resume-button').click(function () { + socket.emit('resume', {}); +}); + +$('#attach-button').click(function () { + socket.emit('attach', {}); +}); + +$('#detach-button').click(function () { + socket.emit('detach', {}); +}); + +$('#about-button').click(function () { + $('#about-dialog').dialog('open'); +}); + +$('#show-bytecode-button').click(function () { + bytecodeDialogOpen = true; + $('#bytecode-dialog').dialog('open'); + + elem = $('#bytecode-preformatted'); + elem.empty().text('Loading bytecode...'); + + socket.emit('get-bytecode', {}); +}); + +function submitEval() { + socket.emit('eval', { input: $('#eval-input').val() }); + + // Eval may take seconds to complete so indicate it is pending. + $('#eval-button').addClass('pending'); +} + +$('#eval-button').click(function () { + submitEval(); + $('#eval-input').val(''); +}); + +$('#getvar-button').click(function () { + socket.emit('getvar', { varname: $('#varname-input').val() }); +}); + +$('#putvar-button').click(function () { + // The variable value is parsed as JSON right now, but it'd be better to + // also be able to parse buffer values etc. + var val = JSON.parse($('#varvalue-input').val()); + socket.emit('putvar', { varname: $('#varname-input').val(), varvalue: val }); +}); + +$('#source-code').dblclick(function (event) { + var target = event.target; + var elems = $('#source-code div'); + var i, n; + var line = 0; + + // XXX: any faster way; elems doesn't have e.g. indexOf() + for (i = 0, n = elems.length; i < n; i++) { + if (target === elems[i]) { + line = i + 1; + } + } + + socket.emit('toggle-breakpoint', { + fileName: loadedFileName, + lineNumber: line + }); +}); + +function setSourceText(data) { + var elem, div; + + elem = $('#source-code'); + elem.empty(); + data.split('\n').forEach(function (line) { + div = $('
'); + div.text(line); + elem.append(div); + }); + + sourceEditedLines = []; +} + +function setSourceSelect(fileName) { + var elem; + var i, n, t; + + if (fileName == null) { + $('#source-select').val('__none__'); + return; + } + + elem = $('#source-select option'); + for (i = 0, n = elem.length; i < n; i++) { + // Exact match is required. + t = $(elem[i]).val(); + if (t === fileName) { + $('#source-select').val(t); + return; + } + } +} + +/* + * AJAX request handling to fetch source files + */ + +function requestSourceRefetch() { + // If previous update is pending, abort and start a new one. + if (sourceFetchXhr) { + sourceFetchXhr.abort(); + sourceFetchXhr = null; + } + + // Make copies of the requested file/line so that we have the proper + // values in case they've changed. + var fileName = activeFileName; + var lineNumber = activeLine; + + // AJAX request for the source. + sourceFetchXhr = $.ajax({ + type: 'POST', + url: '/source', + data: JSON.stringify({ fileName: fileName }), + contentType: 'application/json', + success: function (data, status, jqxhr) { + var elem; + + sourceFetchXhr = null; + + loadedFileName = fileName; + loadedLineCount = data.split('\n').length; // XXX: ignore issue with last empty line for now + loadedFileExecuting = (loadedFileName === currFileName); + setSourceText(data); + setSourceSelect(fileName); + loadedLinePending = activeLine || 1; + highlightLine = activeHighlight; // may be null + activeLine = null; + activeHighlight = null; + doSourceUpdate(); + + // XXX: hacky transition, make source change visible + $('#source-pre').fadeTo('fast', 0.25, function () { + $('#source-pre').fadeTo('fast', 1.0); + }); + }, + error: function (jqxhr, status, err) { + // Not worth alerting about because source fetch errors happen + // all the time, e.g. for dynamically evaluated code. + + sourceFetchXhr = null; + + // XXX: prevent retry of no-such-file by negative caching? + loadedFileName = fileName; + loadedLineCount = 1; + loadedFileExecuting = false; + setSourceText('// Cannot load source file: ' + fileName); + setSourceSelect(null); + loadedLinePending = 1; + activeLine = null; + activeHighlight = null; + doSourceUpdate(); + + // XXX: error transition here + $('#source-pre').fadeTo('fast', 0.25, function () { + $('#source-pre').fadeTo('fast', 1.0); + }); + }, + dataType: 'text' + }); +} + +/* + * AJAX request for fetching the source list + */ + +function fetchSourceList() { + $.ajax({ + type: 'POST', + url: '/sourceList', + data: JSON.stringify({}), + contentType: 'application/json', + success: function (data, status, jqxhr) { + var elem = $('#source-select'); + + data = JSON.parse(data); + + elem.empty(); + var opt = $('').attr({ 'value': '__none__' }).text('No source file selected'); + elem.append(opt); + data.forEach(function (ent) { + var opt = $('').attr({ 'value': ent }).text(ent); + elem.append(opt); + }); + elem.change(function () { + activeFileName = elem.val(); + activeLine = 1; + requestSourceRefetch(); + }); + }, + error: function (jqxhr, status, err) { + // This is worth alerting about as the UI is somewhat unusable + // if we don't get a source list. + + alert('Failed to load source list: ' + err); + }, + dataType: 'text' + }); +} + +/* + * Initialization + */ + +$(document).ready(function () { + var showAbout = true; + + // About dialog, shown automatically on first startup. + $('#about-dialog').dialog({ + autoOpen: false, + hide: 'fade', // puff + show: 'fade', // slide, puff + width: 500, + height: 300 + }); + + // Bytecode dialog + $('#bytecode-dialog').dialog({ + autoOpen: false, + hide: 'fade', // puff + show: 'fade', // slide, puff + width: 700, + height: 600, + close: function () { + bytecodeDialogOpen = false; + bytecodeIdxHighlight = null; + bytecodeIdxInstr = 0; + } + }); + + // http://diveintohtml5.info/storage.html + if (typeof localStorage !== 'undefined') { + if (localStorage.getItem('about-shown')) { + showAbout = false; + } else { + localStorage.setItem('about-shown', 'yes'); + } + } + if (showAbout) { + $('#about-dialog').dialog('open'); + } + + // onclick handler for exec status text + function loadCurrFunc() { + activeFileName = currFileName; + activeLine = currLine; + requestSourceRefetch(); + } + $('#exec-other').on('click', loadCurrFunc); + + // Enter handling for eval input + // https://forum.jquery.com/topic/bind-html-input-to-enter-key-keypress + $('#eval-input').keypress(function (event) { + if (event.keyCode == 13) { + submitEval(); + $('#eval-input').val(''); + } + }); + + // Eval watch handling + $('#eval-watch').change(function () { + // nop + }); + + forceButtonUpdate = true; + doUiUpdate(); +}); diff --git a/src/civetweb/src/third_party/duktape-1.5.2/duk_build_meta.json b/src/civetweb/src/third_party/duktape-1.5.2/duk_build_meta.json new file mode 100644 index 000000000..c2e80bb2f --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/duk_build_meta.json @@ -0,0 +1,1349 @@ +{ + "builtin_strings": [ + "Undefined", + "Null", + "Arguments", + "Object", + "Function", + "Array", + "String", + "Boolean", + "Number", + "Date", + "RegExp", + "Error", + "Math", + "JSON", + "", + "ArrayBuffer", + "DataView", + "Int8Array", + "Uint8Array", + "Uint8ClampedArray", + "Int16Array", + "Uint16Array", + "Int32Array", + "Uint32Array", + "Float32Array", + "Float64Array", + "global", + "ObjEnv", + "DecEnv", + "Buffer", + "Pointer", + "Thread", + "eval", + "defineProperty", + "value", + "writable", + "configurable", + "enumerable", + "join", + "toLocaleString", + "valueOf", + "toUTCString", + "toISOString", + "toGMTString", + "source", + "ignoreCase", + "multiline", + "lastIndex", + "(?:)", + "index", + "prototype", + "constructor", + "message", + "boolean", + "number", + "string", + "object", + "undefined", + "NaN", + "Infinity", + "-Infinity", + "-0", + ",", + " ", + "\n ", + "[...]", + "Invalid Date", + "arguments", + "callee", + "caller", + "has", + "get", + "deleteProperty", + "enumerate", + "ownKeys", + "setPrototypeOf", + "__proto__", + "require", + "id", + "exports", + "filename", + "toString", + "toJSON", + "type", + "data", + "length", + "byteLength", + "byteOffset", + "BYTES_PER_ELEMENT", + "set", + "stack", + "pc", + "lineNumber", + "\u00ffTracedata", + "name", + "fileName", + "buffer", + "pointer", + "\u00ffValue", + "\u00ffNext", + "\u00ffBytecode", + "\u00ffFormals", + "\u00ffVarmap", + "\u00ffLexenv", + "\u00ffVarenv", + "\u00ffSource", + "\u00ffPc2line", + "\u00ffArgs", + "\u00ffMap", + "\u00ffFinalizer", + "\u00ffHandler", + "\u00ffCallee", + "\u00ffThread", + "\u00ffRegbase", + "\u00ffTarget", + "\u00ffThis", + "compile", + "input", + "errCreate", + "errThrow", + "modSearch", + "modLoaded", + "env", + "hex", + "base64", + "jx", + "jc", + "resume", + "fmt", + "raw", + "trace", + "debug", + "info", + "warn", + "error", + "fatal", + "n", + "l", + "clog", + "toLogString", + "{\"_undef\":true}", + "{\"_nan\":true}", + "{\"_inf\":true}", + "{\"_ninf\":true}", + "{\"_func\":true}", + "{_func:true}", + "break", + "case", + "catch", + "continue", + "debugger", + "default", + "delete", + "do", + "else", + "finally", + "for", + "function", + "if", + "in", + "instanceof", + "new", + "return", + "switch", + "this", + "throw", + "try", + "typeof", + "var", + "const", + "void", + "while", + "with", + "class", + "enum", + "export", + "extends", + "import", + "super", + "null", + "true", + "false", + "implements", + "interface", + "let", + "package", + "private", + "protected", + "public", + "static", + "yield" + ], + "builtin_strings_base64": [ + "VW5kZWZpbmVk", + "TnVsbA==", + "QXJndW1lbnRz", + "T2JqZWN0", + "RnVuY3Rpb24=", + "QXJyYXk=", + "U3RyaW5n", + "Qm9vbGVhbg==", + "TnVtYmVy", + "RGF0ZQ==", + "UmVnRXhw", + "RXJyb3I=", + "TWF0aA==", + "SlNPTg==", + "", + "QXJyYXlCdWZmZXI=", + "RGF0YVZpZXc=", + "SW50OEFycmF5", + "VWludDhBcnJheQ==", + "VWludDhDbGFtcGVkQXJyYXk=", + "SW50MTZBcnJheQ==", + "VWludDE2QXJyYXk=", + "SW50MzJBcnJheQ==", + "VWludDMyQXJyYXk=", + "RmxvYXQzMkFycmF5", + "RmxvYXQ2NEFycmF5", + "Z2xvYmFs", + "T2JqRW52", + "RGVjRW52", + "QnVmZmVy", + "UG9pbnRlcg==", + "VGhyZWFk", + "ZXZhbA==", + "ZGVmaW5lUHJvcGVydHk=", + "dmFsdWU=", + "d3JpdGFibGU=", + "Y29uZmlndXJhYmxl", + "ZW51bWVyYWJsZQ==", + "am9pbg==", + "dG9Mb2NhbGVTdHJpbmc=", + "dmFsdWVPZg==", + "dG9VVENTdHJpbmc=", + "dG9JU09TdHJpbmc=", + "dG9HTVRTdHJpbmc=", + "c291cmNl", + "aWdub3JlQ2FzZQ==", + "bXVsdGlsaW5l", + "bGFzdEluZGV4", + "KD86KQ==", + "aW5kZXg=", + "cHJvdG90eXBl", + "Y29uc3RydWN0b3I=", + "bWVzc2FnZQ==", + "Ym9vbGVhbg==", + "bnVtYmVy", + "c3RyaW5n", + "b2JqZWN0", + "dW5kZWZpbmVk", + "TmFO", + "SW5maW5pdHk=", + "LUluZmluaXR5", + "LTA=", + "LA==", + "IA==", + "CiAgICA=", + "Wy4uLl0=", + "SW52YWxpZCBEYXRl", + "YXJndW1lbnRz", + "Y2FsbGVl", + "Y2FsbGVy", + "aGFz", + "Z2V0", + "ZGVsZXRlUHJvcGVydHk=", + "ZW51bWVyYXRl", + "b3duS2V5cw==", + "c2V0UHJvdG90eXBlT2Y=", + "X19wcm90b19f", + "cmVxdWlyZQ==", + "aWQ=", + "ZXhwb3J0cw==", + "ZmlsZW5hbWU=", + "dG9TdHJpbmc=", + "dG9KU09O", + "dHlwZQ==", + "ZGF0YQ==", + "bGVuZ3Ro", + "Ynl0ZUxlbmd0aA==", + "Ynl0ZU9mZnNldA==", + "QllURVNfUEVSX0VMRU1FTlQ=", + "c2V0", + "c3RhY2s=", + "cGM=", + "bGluZU51bWJlcg==", + "/1RyYWNlZGF0YQ==", + "bmFtZQ==", + "ZmlsZU5hbWU=", + "YnVmZmVy", + "cG9pbnRlcg==", + "/1ZhbHVl", + "/05leHQ=", + "/0J5dGVjb2Rl", + "/0Zvcm1hbHM=", + "/1Zhcm1hcA==", + "/0xleGVudg==", + "/1ZhcmVudg==", + "/1NvdXJjZQ==", + "/1BjMmxpbmU=", + "/0FyZ3M=", + "/01hcA==", + "/0ZpbmFsaXplcg==", + "/0hhbmRsZXI=", + "/0NhbGxlZQ==", + "/1RocmVhZA==", + "/1JlZ2Jhc2U=", + "/1RhcmdldA==", + "/1RoaXM=", + "Y29tcGlsZQ==", + "aW5wdXQ=", + "ZXJyQ3JlYXRl", + "ZXJyVGhyb3c=", + "bW9kU2VhcmNo", + "bW9kTG9hZGVk", + "ZW52", + "aGV4", + "YmFzZTY0", + "ang=", + "amM=", + "cmVzdW1l", + "Zm10", + "cmF3", + "dHJhY2U=", + "ZGVidWc=", + "aW5mbw==", + "d2Fybg==", + "ZXJyb3I=", + "ZmF0YWw=", + "bg==", + "bA==", + "Y2xvZw==", + "dG9Mb2dTdHJpbmc=", + "eyJfdW5kZWYiOnRydWV9", + "eyJfbmFuIjp0cnVlfQ==", + "eyJfaW5mIjp0cnVlfQ==", + "eyJfbmluZiI6dHJ1ZX0=", + "eyJfZnVuYyI6dHJ1ZX0=", + "e19mdW5jOnRydWV9", + "YnJlYWs=", + "Y2FzZQ==", + "Y2F0Y2g=", + "Y29udGludWU=", + "ZGVidWdnZXI=", + "ZGVmYXVsdA==", + "ZGVsZXRl", + "ZG8=", + "ZWxzZQ==", + "ZmluYWxseQ==", + "Zm9y", + "ZnVuY3Rpb24=", + "aWY=", + "aW4=", + "aW5zdGFuY2VvZg==", + "bmV3", + "cmV0dXJu", + "c3dpdGNo", + "dGhpcw==", + "dGhyb3c=", + "dHJ5", + "dHlwZW9m", + "dmFy", + "Y29uc3Q=", + "dm9pZA==", + "d2hpbGU=", + "d2l0aA==", + "Y2xhc3M=", + "ZW51bQ==", + "ZXhwb3J0", + "ZXh0ZW5kcw==", + "aW1wb3J0", + "c3VwZXI=", + "bnVsbA==", + "dHJ1ZQ==", + "ZmFsc2U=", + "aW1wbGVtZW50cw==", + "aW50ZXJmYWNl", + "bGV0", + "cGFja2FnZQ==", + "cHJpdmF0ZQ==", + "cHJvdGVjdGVk", + "cHVibGlj", + "c3RhdGlj", + "eWllbGQ=" + ], + "builtin_strings_info": [ + { + "base64": "VW5kZWZpbmVk", + "define": "DUK_STRIDX_UC_UNDEFINED", + "plain": "Undefined" + }, + { + "base64": "TnVsbA==", + "define": "DUK_STRIDX_UC_NULL", + "plain": "Null" + }, + { + "base64": "QXJndW1lbnRz", + "define": "DUK_STRIDX_UC_ARGUMENTS", + "plain": "Arguments" + }, + { + "base64": "T2JqZWN0", + "define": "DUK_STRIDX_UC_OBJECT", + "plain": "Object" + }, + { + "base64": "RnVuY3Rpb24=", + "define": "DUK_STRIDX_UC_FUNCTION", + "plain": "Function" + }, + { + "base64": "QXJyYXk=", + "define": "DUK_STRIDX_ARRAY", + "plain": "Array" + }, + { + "base64": "U3RyaW5n", + "define": "DUK_STRIDX_UC_STRING", + "plain": "String" + }, + { + "base64": "Qm9vbGVhbg==", + "define": "DUK_STRIDX_UC_BOOLEAN", + "plain": "Boolean" + }, + { + "base64": "TnVtYmVy", + "define": "DUK_STRIDX_UC_NUMBER", + "plain": "Number" + }, + { + "base64": "RGF0ZQ==", + "define": "DUK_STRIDX_DATE", + "plain": "Date" + }, + { + "base64": "UmVnRXhw", + "define": "DUK_STRIDX_REG_EXP", + "plain": "RegExp" + }, + { + "base64": "RXJyb3I=", + "define": "DUK_STRIDX_UC_ERROR", + "plain": "Error" + }, + { + "base64": "TWF0aA==", + "define": "DUK_STRIDX_MATH", + "plain": "Math" + }, + { + "base64": "SlNPTg==", + "define": "DUK_STRIDX_JSON", + "plain": "JSON" + }, + { + "base64": "", + "define": "DUK_STRIDX_EMPTY_STRING", + "plain": "" + }, + { + "base64": "QXJyYXlCdWZmZXI=", + "define": "DUK_STRIDX_ARRAY_BUFFER", + "plain": "ArrayBuffer" + }, + { + "base64": "RGF0YVZpZXc=", + "define": "DUK_STRIDX_DATA_VIEW", + "plain": "DataView" + }, + { + "base64": "SW50OEFycmF5", + "define": "DUK_STRIDX_INT8_ARRAY", + "plain": "Int8Array" + }, + { + "base64": "VWludDhBcnJheQ==", + "define": "DUK_STRIDX_UINT8_ARRAY", + "plain": "Uint8Array" + }, + { + "base64": "VWludDhDbGFtcGVkQXJyYXk=", + "define": "DUK_STRIDX_UINT8_CLAMPED_ARRAY", + "plain": "Uint8ClampedArray" + }, + { + "base64": "SW50MTZBcnJheQ==", + "define": "DUK_STRIDX_INT16_ARRAY", + "plain": "Int16Array" + }, + { + "base64": "VWludDE2QXJyYXk=", + "define": "DUK_STRIDX_UINT16_ARRAY", + "plain": "Uint16Array" + }, + { + "base64": "SW50MzJBcnJheQ==", + "define": "DUK_STRIDX_INT32_ARRAY", + "plain": "Int32Array" + }, + { + "base64": "VWludDMyQXJyYXk=", + "define": "DUK_STRIDX_UINT32_ARRAY", + "plain": "Uint32Array" + }, + { + "base64": "RmxvYXQzMkFycmF5", + "define": "DUK_STRIDX_FLOAT32_ARRAY", + "plain": "Float32Array" + }, + { + "base64": "RmxvYXQ2NEFycmF5", + "define": "DUK_STRIDX_FLOAT64_ARRAY", + "plain": "Float64Array" + }, + { + "base64": "Z2xvYmFs", + "define": "DUK_STRIDX_GLOBAL", + "plain": "global" + }, + { + "base64": "T2JqRW52", + "define": "DUK_STRIDX_OBJ_ENV", + "plain": "ObjEnv" + }, + { + "base64": "RGVjRW52", + "define": "DUK_STRIDX_DEC_ENV", + "plain": "DecEnv" + }, + { + "base64": "QnVmZmVy", + "define": "DUK_STRIDX_UC_BUFFER", + "plain": "Buffer" + }, + { + "base64": "UG9pbnRlcg==", + "define": "DUK_STRIDX_UC_POINTER", + "plain": "Pointer" + }, + { + "base64": "VGhyZWFk", + "define": "DUK_STRIDX_UC_THREAD", + "plain": "Thread" + }, + { + "base64": "ZXZhbA==", + "define": "DUK_STRIDX_EVAL", + "plain": "eval" + }, + { + "base64": "ZGVmaW5lUHJvcGVydHk=", + "define": "DUK_STRIDX_DEFINE_PROPERTY", + "plain": "defineProperty" + }, + { + "base64": "dmFsdWU=", + "define": "DUK_STRIDX_VALUE", + "plain": "value" + }, + { + "base64": "d3JpdGFibGU=", + "define": "DUK_STRIDX_WRITABLE", + "plain": "writable" + }, + { + "base64": "Y29uZmlndXJhYmxl", + "define": "DUK_STRIDX_CONFIGURABLE", + "plain": "configurable" + }, + { + "base64": "ZW51bWVyYWJsZQ==", + "define": "DUK_STRIDX_ENUMERABLE", + "plain": "enumerable" + }, + { + "base64": "am9pbg==", + "define": "DUK_STRIDX_JOIN", + "plain": "join" + }, + { + "base64": "dG9Mb2NhbGVTdHJpbmc=", + "define": "DUK_STRIDX_TO_LOCALE_STRING", + "plain": "toLocaleString" + }, + { + "base64": "dmFsdWVPZg==", + "define": "DUK_STRIDX_VALUE_OF", + "plain": "valueOf" + }, + { + "base64": "dG9VVENTdHJpbmc=", + "define": "DUK_STRIDX_TO_UTC_STRING", + "plain": "toUTCString" + }, + { + "base64": "dG9JU09TdHJpbmc=", + "define": "DUK_STRIDX_TO_ISO_STRING", + "plain": "toISOString" + }, + { + "base64": "dG9HTVRTdHJpbmc=", + "define": "DUK_STRIDX_TO_GMT_STRING", + "plain": "toGMTString" + }, + { + "base64": "c291cmNl", + "define": "DUK_STRIDX_SOURCE", + "plain": "source" + }, + { + "base64": "aWdub3JlQ2FzZQ==", + "define": "DUK_STRIDX_IGNORE_CASE", + "plain": "ignoreCase" + }, + { + "base64": "bXVsdGlsaW5l", + "define": "DUK_STRIDX_MULTILINE", + "plain": "multiline" + }, + { + "base64": "bGFzdEluZGV4", + "define": "DUK_STRIDX_LAST_INDEX", + "plain": "lastIndex" + }, + { + "base64": "KD86KQ==", + "define": "DUK_STRIDX_ESCAPED_EMPTY_REGEXP", + "plain": "(?:)" + }, + { + "base64": "aW5kZXg=", + "define": "DUK_STRIDX_INDEX", + "plain": "index" + }, + { + "base64": "cHJvdG90eXBl", + "define": "DUK_STRIDX_PROTOTYPE", + "plain": "prototype" + }, + { + "base64": "Y29uc3RydWN0b3I=", + "define": "DUK_STRIDX_CONSTRUCTOR", + "plain": "constructor" + }, + { + "base64": "bWVzc2FnZQ==", + "define": "DUK_STRIDX_MESSAGE", + "plain": "message" + }, + { + "base64": "Ym9vbGVhbg==", + "define": "DUK_STRIDX_LC_BOOLEAN", + "plain": "boolean" + }, + { + "base64": "bnVtYmVy", + "define": "DUK_STRIDX_LC_NUMBER", + "plain": "number" + }, + { + "base64": "c3RyaW5n", + "define": "DUK_STRIDX_LC_STRING", + "plain": "string" + }, + { + "base64": "b2JqZWN0", + "define": "DUK_STRIDX_LC_OBJECT", + "plain": "object" + }, + { + "base64": "dW5kZWZpbmVk", + "define": "DUK_STRIDX_LC_UNDEFINED", + "plain": "undefined" + }, + { + "base64": "TmFO", + "define": "DUK_STRIDX_NAN", + "plain": "NaN" + }, + { + "base64": "SW5maW5pdHk=", + "define": "DUK_STRIDX_INFINITY", + "plain": "Infinity" + }, + { + "base64": "LUluZmluaXR5", + "define": "DUK_STRIDX_MINUS_INFINITY", + "plain": "-Infinity" + }, + { + "base64": "LTA=", + "define": "DUK_STRIDX_MINUS_ZERO", + "plain": "-0" + }, + { + "base64": "LA==", + "define": "DUK_STRIDX_COMMA", + "plain": "," + }, + { + "base64": "IA==", + "define": "DUK_STRIDX_SPACE", + "plain": " " + }, + { + "base64": "CiAgICA=", + "define": "DUK_STRIDX_NEWLINE_4SPACE", + "plain": "\n " + }, + { + "base64": "Wy4uLl0=", + "define": "DUK_STRIDX_BRACKETED_ELLIPSIS", + "plain": "[...]" + }, + { + "base64": "SW52YWxpZCBEYXRl", + "define": "DUK_STRIDX_INVALID_DATE", + "plain": "Invalid Date" + }, + { + "base64": "YXJndW1lbnRz", + "define": "DUK_STRIDX_LC_ARGUMENTS", + "plain": "arguments" + }, + { + "base64": "Y2FsbGVl", + "define": "DUK_STRIDX_CALLEE", + "plain": "callee" + }, + { + "base64": "Y2FsbGVy", + "define": "DUK_STRIDX_CALLER", + "plain": "caller" + }, + { + "base64": "aGFz", + "define": "DUK_STRIDX_HAS", + "plain": "has" + }, + { + "base64": "Z2V0", + "define": "DUK_STRIDX_GET", + "plain": "get" + }, + { + "base64": "ZGVsZXRlUHJvcGVydHk=", + "define": "DUK_STRIDX_DELETE_PROPERTY", + "plain": "deleteProperty" + }, + { + "base64": "ZW51bWVyYXRl", + "define": "DUK_STRIDX_ENUMERATE", + "plain": "enumerate" + }, + { + "base64": "b3duS2V5cw==", + "define": "DUK_STRIDX_OWN_KEYS", + "plain": "ownKeys" + }, + { + "base64": "c2V0UHJvdG90eXBlT2Y=", + "define": "DUK_STRIDX_SET_PROTOTYPE_OF", + "plain": "setPrototypeOf" + }, + { + "base64": "X19wcm90b19f", + "define": "DUK_STRIDX___PROTO__", + "plain": "__proto__" + }, + { + "base64": "cmVxdWlyZQ==", + "define": "DUK_STRIDX_REQUIRE", + "plain": "require" + }, + { + "base64": "aWQ=", + "define": "DUK_STRIDX_ID", + "plain": "id" + }, + { + "base64": "ZXhwb3J0cw==", + "define": "DUK_STRIDX_EXPORTS", + "plain": "exports" + }, + { + "base64": "ZmlsZW5hbWU=", + "define": "DUK_STRIDX_FILENAME", + "plain": "filename" + }, + { + "base64": "dG9TdHJpbmc=", + "define": "DUK_STRIDX_TO_STRING", + "plain": "toString" + }, + { + "base64": "dG9KU09O", + "define": "DUK_STRIDX_TO_JSON", + "plain": "toJSON" + }, + { + "base64": "dHlwZQ==", + "define": "DUK_STRIDX_TYPE", + "plain": "type" + }, + { + "base64": "ZGF0YQ==", + "define": "DUK_STRIDX_DATA", + "plain": "data" + }, + { + "base64": "bGVuZ3Ro", + "define": "DUK_STRIDX_LENGTH", + "plain": "length" + }, + { + "base64": "Ynl0ZUxlbmd0aA==", + "define": "DUK_STRIDX_BYTE_LENGTH", + "plain": "byteLength" + }, + { + "base64": "Ynl0ZU9mZnNldA==", + "define": "DUK_STRIDX_BYTE_OFFSET", + "plain": "byteOffset" + }, + { + "base64": "QllURVNfUEVSX0VMRU1FTlQ=", + "define": "DUK_STRIDX_BYTES_PER_ELEMENT", + "plain": "BYTES_PER_ELEMENT" + }, + { + "base64": "c2V0", + "define": "DUK_STRIDX_SET", + "plain": "set" + }, + { + "base64": "c3RhY2s=", + "define": "DUK_STRIDX_STACK", + "plain": "stack" + }, + { + "base64": "cGM=", + "define": "DUK_STRIDX_PC", + "plain": "pc" + }, + { + "base64": "bGluZU51bWJlcg==", + "define": "DUK_STRIDX_LINE_NUMBER", + "plain": "lineNumber" + }, + { + "base64": "/1RyYWNlZGF0YQ==", + "define": "DUK_STRIDX_INT_TRACEDATA", + "plain": "\u00ffTracedata" + }, + { + "base64": "bmFtZQ==", + "define": "DUK_STRIDX_NAME", + "plain": "name" + }, + { + "base64": "ZmlsZU5hbWU=", + "define": "DUK_STRIDX_FILE_NAME", + "plain": "fileName" + }, + { + "base64": "YnVmZmVy", + "define": "DUK_STRIDX_LC_BUFFER", + "plain": "buffer" + }, + { + "base64": "cG9pbnRlcg==", + "define": "DUK_STRIDX_LC_POINTER", + "plain": "pointer" + }, + { + "base64": "/1ZhbHVl", + "define": "DUK_STRIDX_INT_VALUE", + "plain": "\u00ffValue" + }, + { + "base64": "/05leHQ=", + "define": "DUK_STRIDX_INT_NEXT", + "plain": "\u00ffNext" + }, + { + "base64": "/0J5dGVjb2Rl", + "define": "DUK_STRIDX_INT_BYTECODE", + "plain": "\u00ffBytecode" + }, + { + "base64": "/0Zvcm1hbHM=", + "define": "DUK_STRIDX_INT_FORMALS", + "plain": "\u00ffFormals" + }, + { + "base64": "/1Zhcm1hcA==", + "define": "DUK_STRIDX_INT_VARMAP", + "plain": "\u00ffVarmap" + }, + { + "base64": "/0xleGVudg==", + "define": "DUK_STRIDX_INT_LEXENV", + "plain": "\u00ffLexenv" + }, + { + "base64": "/1ZhcmVudg==", + "define": "DUK_STRIDX_INT_VARENV", + "plain": "\u00ffVarenv" + }, + { + "base64": "/1NvdXJjZQ==", + "define": "DUK_STRIDX_INT_SOURCE", + "plain": "\u00ffSource" + }, + { + "base64": "/1BjMmxpbmU=", + "define": "DUK_STRIDX_INT_PC2LINE", + "plain": "\u00ffPc2line" + }, + { + "base64": "/0FyZ3M=", + "define": "DUK_STRIDX_INT_ARGS", + "plain": "\u00ffArgs" + }, + { + "base64": "/01hcA==", + "define": "DUK_STRIDX_INT_MAP", + "plain": "\u00ffMap" + }, + { + "base64": "/0ZpbmFsaXplcg==", + "define": "DUK_STRIDX_INT_FINALIZER", + "plain": "\u00ffFinalizer" + }, + { + "base64": "/0hhbmRsZXI=", + "define": "DUK_STRIDX_INT_HANDLER", + "plain": "\u00ffHandler" + }, + { + "base64": "/0NhbGxlZQ==", + "define": "DUK_STRIDX_INT_CALLEE", + "plain": "\u00ffCallee" + }, + { + "base64": "/1RocmVhZA==", + "define": "DUK_STRIDX_INT_THREAD", + "plain": "\u00ffThread" + }, + { + "base64": "/1JlZ2Jhc2U=", + "define": "DUK_STRIDX_INT_REGBASE", + "plain": "\u00ffRegbase" + }, + { + "base64": "/1RhcmdldA==", + "define": "DUK_STRIDX_INT_TARGET", + "plain": "\u00ffTarget" + }, + { + "base64": "/1RoaXM=", + "define": "DUK_STRIDX_INT_THIS", + "plain": "\u00ffThis" + }, + { + "base64": "Y29tcGlsZQ==", + "define": "DUK_STRIDX_COMPILE", + "plain": "compile" + }, + { + "base64": "aW5wdXQ=", + "define": "DUK_STRIDX_INPUT", + "plain": "input" + }, + { + "base64": "ZXJyQ3JlYXRl", + "define": "DUK_STRIDX_ERR_CREATE", + "plain": "errCreate" + }, + { + "base64": "ZXJyVGhyb3c=", + "define": "DUK_STRIDX_ERR_THROW", + "plain": "errThrow" + }, + { + "base64": "bW9kU2VhcmNo", + "define": "DUK_STRIDX_MOD_SEARCH", + "plain": "modSearch" + }, + { + "base64": "bW9kTG9hZGVk", + "define": "DUK_STRIDX_MOD_LOADED", + "plain": "modLoaded" + }, + { + "base64": "ZW52", + "define": "DUK_STRIDX_ENV", + "plain": "env" + }, + { + "base64": "aGV4", + "define": "DUK_STRIDX_HEX", + "plain": "hex" + }, + { + "base64": "YmFzZTY0", + "define": "DUK_STRIDX_BASE64", + "plain": "base64" + }, + { + "base64": "ang=", + "define": "DUK_STRIDX_JX", + "plain": "jx" + }, + { + "base64": "amM=", + "define": "DUK_STRIDX_JC", + "plain": "jc" + }, + { + "base64": "cmVzdW1l", + "define": "DUK_STRIDX_RESUME", + "plain": "resume" + }, + { + "base64": "Zm10", + "define": "DUK_STRIDX_FMT", + "plain": "fmt" + }, + { + "base64": "cmF3", + "define": "DUK_STRIDX_RAW", + "plain": "raw" + }, + { + "base64": "dHJhY2U=", + "define": "DUK_STRIDX_LC_TRACE", + "plain": "trace" + }, + { + "base64": "ZGVidWc=", + "define": "DUK_STRIDX_LC_DEBUG", + "plain": "debug" + }, + { + "base64": "aW5mbw==", + "define": "DUK_STRIDX_LC_INFO", + "plain": "info" + }, + { + "base64": "d2Fybg==", + "define": "DUK_STRIDX_LC_WARN", + "plain": "warn" + }, + { + "base64": "ZXJyb3I=", + "define": "DUK_STRIDX_LC_ERROR", + "plain": "error" + }, + { + "base64": "ZmF0YWw=", + "define": "DUK_STRIDX_LC_FATAL", + "plain": "fatal" + }, + { + "base64": "bg==", + "define": "DUK_STRIDX_LC_N", + "plain": "n" + }, + { + "base64": "bA==", + "define": "DUK_STRIDX_LC_L", + "plain": "l" + }, + { + "base64": "Y2xvZw==", + "define": "DUK_STRIDX_CLOG", + "plain": "clog" + }, + { + "base64": "dG9Mb2dTdHJpbmc=", + "define": "DUK_STRIDX_TO_LOG_STRING", + "plain": "toLogString" + }, + { + "base64": "eyJfdW5kZWYiOnRydWV9", + "define": "DUK_STRIDX_JSON_EXT_UNDEFINED", + "plain": "{\"_undef\":true}" + }, + { + "base64": "eyJfbmFuIjp0cnVlfQ==", + "define": "DUK_STRIDX_JSON_EXT_NAN", + "plain": "{\"_nan\":true}" + }, + { + "base64": "eyJfaW5mIjp0cnVlfQ==", + "define": "DUK_STRIDX_JSON_EXT_POSINF", + "plain": "{\"_inf\":true}" + }, + { + "base64": "eyJfbmluZiI6dHJ1ZX0=", + "define": "DUK_STRIDX_JSON_EXT_NEGINF", + "plain": "{\"_ninf\":true}" + }, + { + "base64": "eyJfZnVuYyI6dHJ1ZX0=", + "define": "DUK_STRIDX_JSON_EXT_FUNCTION1", + "plain": "{\"_func\":true}" + }, + { + "base64": "e19mdW5jOnRydWV9", + "define": "DUK_STRIDX_JSON_EXT_FUNCTION2", + "plain": "{_func:true}" + }, + { + "base64": "YnJlYWs=", + "define": "DUK_STRIDX_BREAK", + "plain": "break" + }, + { + "base64": "Y2FzZQ==", + "define": "DUK_STRIDX_CASE", + "plain": "case" + }, + { + "base64": "Y2F0Y2g=", + "define": "DUK_STRIDX_CATCH", + "plain": "catch" + }, + { + "base64": "Y29udGludWU=", + "define": "DUK_STRIDX_CONTINUE", + "plain": "continue" + }, + { + "base64": "ZGVidWdnZXI=", + "define": "DUK_STRIDX_DEBUGGER", + "plain": "debugger" + }, + { + "base64": "ZGVmYXVsdA==", + "define": "DUK_STRIDX_DEFAULT", + "plain": "default" + }, + { + "base64": "ZGVsZXRl", + "define": "DUK_STRIDX_DELETE", + "plain": "delete" + }, + { + "base64": "ZG8=", + "define": "DUK_STRIDX_DO", + "plain": "do" + }, + { + "base64": "ZWxzZQ==", + "define": "DUK_STRIDX_ELSE", + "plain": "else" + }, + { + "base64": "ZmluYWxseQ==", + "define": "DUK_STRIDX_FINALLY", + "plain": "finally" + }, + { + "base64": "Zm9y", + "define": "DUK_STRIDX_FOR", + "plain": "for" + }, + { + "base64": "ZnVuY3Rpb24=", + "define": "DUK_STRIDX_LC_FUNCTION", + "plain": "function" + }, + { + "base64": "aWY=", + "define": "DUK_STRIDX_IF", + "plain": "if" + }, + { + "base64": "aW4=", + "define": "DUK_STRIDX_IN", + "plain": "in" + }, + { + "base64": "aW5zdGFuY2VvZg==", + "define": "DUK_STRIDX_INSTANCEOF", + "plain": "instanceof" + }, + { + "base64": "bmV3", + "define": "DUK_STRIDX_NEW", + "plain": "new" + }, + { + "base64": "cmV0dXJu", + "define": "DUK_STRIDX_RETURN", + "plain": "return" + }, + { + "base64": "c3dpdGNo", + "define": "DUK_STRIDX_SWITCH", + "plain": "switch" + }, + { + "base64": "dGhpcw==", + "define": "DUK_STRIDX_THIS", + "plain": "this" + }, + { + "base64": "dGhyb3c=", + "define": "DUK_STRIDX_THROW", + "plain": "throw" + }, + { + "base64": "dHJ5", + "define": "DUK_STRIDX_TRY", + "plain": "try" + }, + { + "base64": "dHlwZW9m", + "define": "DUK_STRIDX_TYPEOF", + "plain": "typeof" + }, + { + "base64": "dmFy", + "define": "DUK_STRIDX_VAR", + "plain": "var" + }, + { + "base64": "Y29uc3Q=", + "define": "DUK_STRIDX_CONST", + "plain": "const" + }, + { + "base64": "dm9pZA==", + "define": "DUK_STRIDX_VOID", + "plain": "void" + }, + { + "base64": "d2hpbGU=", + "define": "DUK_STRIDX_WHILE", + "plain": "while" + }, + { + "base64": "d2l0aA==", + "define": "DUK_STRIDX_WITH", + "plain": "with" + }, + { + "base64": "Y2xhc3M=", + "define": "DUK_STRIDX_CLASS", + "plain": "class" + }, + { + "base64": "ZW51bQ==", + "define": "DUK_STRIDX_ENUM", + "plain": "enum" + }, + { + "base64": "ZXhwb3J0", + "define": "DUK_STRIDX_EXPORT", + "plain": "export" + }, + { + "base64": "ZXh0ZW5kcw==", + "define": "DUK_STRIDX_EXTENDS", + "plain": "extends" + }, + { + "base64": "aW1wb3J0", + "define": "DUK_STRIDX_IMPORT", + "plain": "import" + }, + { + "base64": "c3VwZXI=", + "define": "DUK_STRIDX_SUPER", + "plain": "super" + }, + { + "base64": "bnVsbA==", + "define": "DUK_STRIDX_LC_NULL", + "plain": "null" + }, + { + "base64": "dHJ1ZQ==", + "define": "DUK_STRIDX_TRUE", + "plain": "true" + }, + { + "base64": "ZmFsc2U=", + "define": "DUK_STRIDX_FALSE", + "plain": "false" + }, + { + "base64": "aW1wbGVtZW50cw==", + "define": "DUK_STRIDX_IMPLEMENTS", + "plain": "implements" + }, + { + "base64": "aW50ZXJmYWNl", + "define": "DUK_STRIDX_INTERFACE", + "plain": "interface" + }, + { + "base64": "bGV0", + "define": "DUK_STRIDX_LET", + "plain": "let" + }, + { + "base64": "cGFja2FnZQ==", + "define": "DUK_STRIDX_PACKAGE", + "plain": "package" + }, + { + "base64": "cHJpdmF0ZQ==", + "define": "DUK_STRIDX_PRIVATE", + "plain": "private" + }, + { + "base64": "cHJvdGVjdGVk", + "define": "DUK_STRIDX_PROTECTED", + "plain": "protected" + }, + { + "base64": "cHVibGlj", + "define": "DUK_STRIDX_PUBLIC", + "plain": "public" + }, + { + "base64": "c3RhdGlj", + "define": "DUK_STRIDX_STATIC", + "plain": "static" + }, + { + "base64": "eWllbGQ=", + "define": "DUK_STRIDX_YIELD", + "plain": "yield" + } + ], + "comment": "Metadata for Duktape build", + "duk_version": 10502, + "duk_version_string": "1.5.2", + "git_describe": "v1.5.2" +} \ No newline at end of file diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/README.rst new file mode 100644 index 000000000..f2544328e --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/README.rst @@ -0,0 +1,10 @@ +================ +Duktape examples +================ + +Examples for using Duktape. These support user documentation and are +intended as informative illustrations only. + +Examples are unmaintained and are not production quality code. Bugs are +not not necessarily fixed, unless the bug makes the example misleading +as documentation. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-hybrid/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-hybrid/README.rst new file mode 100644 index 000000000..ed63a13cf --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-hybrid/README.rst @@ -0,0 +1,10 @@ +===================== +Hybrid pool allocator +===================== + +Example allocator that tries to satisfy memory allocations for small sizes +from a set of fixed pools, but always falls back to malloc/realloc/free if +a larger size is requested or the pools have been exhausted. + +This may be useful to reduce memory churn when the platform allocator does +not handle allocations for a lot of small memory areas efficiently. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-hybrid/duk_alloc_hybrid.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-hybrid/duk_alloc_hybrid.c new file mode 100644 index 000000000..9235b8577 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-hybrid/duk_alloc_hybrid.c @@ -0,0 +1,293 @@ +/* + * Example memory allocator with pool allocation for small sizes and + * fallback into malloc/realloc/free for larger sizes or when the pools + * are exhausted. + * + * Useful to reduce memory churn or work around a platform allocator + * that doesn't handle a lot of small allocations efficiently. + */ + +#include "duktape.h" +#include +#include +#include +#include + +/* Define to enable some debug printfs. */ +/* #define DUK_ALLOC_HYBRID_DEBUG */ + +typedef struct { + size_t size; + int count; +} pool_size_spec; + +static pool_size_spec pool_sizes[] = { + { 32, 1024 }, + { 48, 2048 }, + { 64, 2048 }, + { 128, 2048 }, + { 256, 512 }, + { 1024, 64 }, + { 2048, 32 } +}; + +#define NUM_POOLS (sizeof(pool_sizes) / sizeof(pool_size_spec)) + +/* This must fit into the smallest pool entry. */ +struct pool_free_entry; +typedef struct pool_free_entry pool_free_entry; +struct pool_free_entry { + pool_free_entry *next; +}; + +typedef struct { + pool_free_entry *free; + char *alloc_start; + char *alloc_end; + size_t size; + int count; +} pool_header; + +typedef struct { + pool_header headers[NUM_POOLS]; + size_t pool_max_size; + char *alloc_start; + char *alloc_end; +} pool_state; + +#define ADDR_IN_STATE_ALLOC(st,p) \ + ((char *) (p) >= (st)->alloc_start && (char *) (p) < (st)->alloc_end) +#define ADDR_IN_HEADER_ALLOC(hdr,p) \ + ((char *) (p) >= (hdr)->alloc_start && (char *) (p) < (hdr)->alloc_end) + +#ifdef DUK_ALLOC_HYBRID_DEBUG +static void dump_pool_state(pool_state *st) { + pool_free_entry *free; + int free_len; + int i; + + printf("=== Pool state: st=%p\n", (void *) st); + for (i = 0; i < (int) NUM_POOLS; i++) { + pool_header *hdr = st->headers + i; + + for (free = hdr->free, free_len = 0; free != NULL; free = free->next) { + free_len++; + } + + printf("[%d]: size %ld, count %ld, used %ld, free list len %ld\n", + i, (long) hdr->size, (long) hdr->count, + (long) (hdr->count - free_len), + (long) free_len); + } +} +#else +static void dump_pool_state(pool_state *st) { + (void) st; +} +#endif + +void *duk_alloc_hybrid_init(void) { + pool_state *st; + size_t total_size, max_size; + int i, j; + char *p; + + st = (pool_state *) malloc(sizeof(pool_state)); + if (!st) { + return NULL; + } + memset((void *) st, 0, sizeof(pool_state)); + st->alloc_start = NULL; + st->alloc_end = NULL; + + for (i = 0, total_size = 0, max_size = 0; i < (int) NUM_POOLS; i++) { +#ifdef DUK_ALLOC_HYBRID_DEBUG + printf("Pool %d: size %ld, count %ld\n", i, (long) pool_sizes[i].size, (long) pool_sizes[i].count); +#endif + total_size += pool_sizes[i].size * pool_sizes[i].count; + if (pool_sizes[i].size > max_size) { + max_size = pool_sizes[i].size; + } + } +#ifdef DUK_ALLOC_HYBRID_DEBUG + printf("Total size %ld, max pool size %ld\n", (long) total_size, (long) max_size); +#endif + + st->alloc_start = (char *) malloc(total_size); + if (!st->alloc_start) { + free(st); + return NULL; + } + st->alloc_end = st->alloc_start + total_size; + st->pool_max_size = max_size; + memset((void *) st->alloc_start, 0, total_size); + + for (i = 0, p = st->alloc_start; i < (int) NUM_POOLS; i++) { + pool_header *hdr = st->headers + i; + + hdr->alloc_start = p; + hdr->alloc_end = p + pool_sizes[i].size * pool_sizes[i].count; + hdr->free = (pool_free_entry *) (void *) p; + hdr->size = pool_sizes[i].size; + hdr->count = pool_sizes[i].count; + + for (j = 0; j < pool_sizes[i].count; j++) { + pool_free_entry *ent = (pool_free_entry *) (void *) p; + if (j == pool_sizes[i].count - 1) { + ent->next = (pool_free_entry *) NULL; + } else { + ent->next = (pool_free_entry *) (void *) (p + pool_sizes[i].size); + } + p += pool_sizes[i].size; + } + } + + dump_pool_state(st); + + /* Use 'st' as udata. */ + return (void *) st; +} + +void *duk_alloc_hybrid(void *udata, duk_size_t size) { + pool_state *st = (pool_state *) udata; + int i; + void *new_ptr; + +#if 0 + dump_pool_state(st); +#endif + + if (size == 0) { + return NULL; + } + if (size > st->pool_max_size) { +#ifdef DUK_ALLOC_HYBRID_DEBUG + printf("alloc fallback: %ld\n", (long) size); +#endif + return malloc(size); + } + + for (i = 0; i < (int) NUM_POOLS; i++) { + pool_header *hdr = st->headers + i; + if (hdr->size < size) { + continue; + } + + if (hdr->free) { +#if 0 + printf("alloc from pool: %ld -> pool size %ld\n", (long) size, (long) hdr->size); +#endif + new_ptr = (void *) hdr->free; + hdr->free = hdr->free->next; + return new_ptr; + } else { +#ifdef DUK_ALLOC_HYBRID_DEBUG + printf("alloc out of pool entries: %ld -> pool size %ld\n", (long) size, (long) hdr->size); +#endif + break; + } + } + +#ifdef DUK_ALLOC_HYBRID_DEBUG + printf("alloc fallback (out of pool): %ld\n", (long) size); +#endif + return malloc(size); +} + +void *duk_realloc_hybrid(void *udata, void *ptr, duk_size_t size) { + pool_state *st = (pool_state *) udata; + void *new_ptr; + int i; + +#if 0 + dump_pool_state(st); +#endif + + if (ADDR_IN_STATE_ALLOC(st, ptr)) { + /* 'ptr' cannot be NULL. */ + for (i = 0; i < (int) NUM_POOLS; i++) { + pool_header *hdr = st->headers + i; + if (ADDR_IN_HEADER_ALLOC(hdr, ptr)) { + if (size <= hdr->size) { + /* Still fits, no shrink support. */ +#if 0 + printf("realloc original from pool: still fits, size %ld, pool size %ld\n", + (long) size, (long) hdr->size); +#endif + return ptr; + } + + new_ptr = duk_alloc_hybrid(udata, size); + if (!new_ptr) { +#ifdef DUK_ALLOC_HYBRID_DEBUG + printf("realloc original from pool: needed larger size, failed to alloc\n"); +#endif + return NULL; + } + memcpy(new_ptr, ptr, hdr->size); + + ((pool_free_entry *) ptr)->next = hdr->free; + hdr->free = (pool_free_entry *) ptr; +#if 0 + printf("realloc original from pool: size %ld, pool size %ld\n", (long) size, (long) hdr->size); +#endif + return new_ptr; + } + } +#ifdef DUK_ALLOC_HYBRID_DEBUG + printf("NEVER HERE\n"); +#endif + return NULL; + } else if (ptr != NULL) { + if (size == 0) { + free(ptr); + return NULL; + } else { +#ifdef DUK_ALLOC_HYBRID_DEBUG + printf("realloc fallback: size %ld\n", (long) size); +#endif + return realloc(ptr, size); + } + } else { +#if 0 + printf("realloc NULL ptr, call alloc: %ld\n", (long) size); +#endif + return duk_alloc_hybrid(udata, size); + } +} + +void duk_free_hybrid(void *udata, void *ptr) { + pool_state *st = (pool_state *) udata; + int i; + +#if 0 + dump_pool_state(st); +#endif + + if (!ADDR_IN_STATE_ALLOC(st, ptr)) { + if (ptr == NULL) { + return; + } +#if 0 + printf("free out of pool: %p\n", (void *) ptr); +#endif + free(ptr); + return; + } + + for (i = 0; i < (int) NUM_POOLS; i++) { + pool_header *hdr = st->headers + i; + if (ADDR_IN_HEADER_ALLOC(hdr, ptr)) { + ((pool_free_entry *) ptr)->next = hdr->free; + hdr->free = (pool_free_entry *) ptr; +#if 0 + printf("free from pool: %p\n", ptr); +#endif + return; + } + } + +#ifdef DUK_ALLOC_HYBRID_DEBUG + printf("NEVER HERE\n"); +#endif +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-hybrid/duk_alloc_hybrid.h b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-hybrid/duk_alloc_hybrid.h new file mode 100644 index 000000000..0d631d1d7 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-hybrid/duk_alloc_hybrid.h @@ -0,0 +1,11 @@ +#ifndef DUK_ALLOC_HYBRID_H_INCLUDED +#define DUK_ALLOC_HYBRID_H_INCLUDED + +#include "duktape.h" + +void *duk_alloc_hybrid_init(void); +void *duk_alloc_hybrid(void *udata, duk_size_t size); +void *duk_realloc_hybrid(void *udata, void *ptr, duk_size_t size); +void duk_free_hybrid(void *udata, void *ptr); + +#endif /* DUK_ALLOC_HYBRID_H_INCLUDED */ diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/README.rst new file mode 100644 index 000000000..97c1a32ad --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/README.rst @@ -0,0 +1,7 @@ +====================== +Allocator with logging +====================== + +Example allocator that writes all memory alloc/realloc/free calls into a +log file so that memory usage can replayed later. This is useful to e.g. +optimize pool sizes. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/duk_alloc_logging.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/duk_alloc_logging.c new file mode 100644 index 000000000..a89babf92 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/duk_alloc_logging.c @@ -0,0 +1,138 @@ +/* + * Example memory allocator with machine parseable logging. + * + * Also sizes for reallocs and frees are logged so that the memory + * behavior can be essentially replayed to accurately determine e.g. + * optimal pool sizes for a pooled allocator. + * + * Allocation structure: + * + * [ alloc_hdr | user area ] + * + * ^ ^ + * | `--- pointer returned to Duktape + * `--- underlying malloc ptr + */ + +#include "duktape.h" +#include +#include +#include +#include + +#define ALLOC_LOG_FILE "/tmp/duk-alloc-log.txt" + +typedef struct { + /* The double value in the union is there to ensure alignment is + * good for IEEE doubles too. In many 32-bit environments 4 bytes + * would be sufficiently aligned and the double value is unnecessary. + */ + union { + size_t sz; + double d; + } u; +} alloc_hdr; + +static FILE *log_file = NULL; + +static void write_log(const char *fmt, ...) { + va_list ap; + + if (!log_file) { + log_file = fopen(ALLOC_LOG_FILE, "wb"); + if (!log_file) { + return; + } + } + + va_start(ap, fmt); + vfprintf(log_file, fmt, ap); + va_end(ap); +} + +void *duk_alloc_logging(void *udata, duk_size_t size) { + alloc_hdr *hdr; + void *ret; + + (void) udata; /* Suppress warning. */ + + if (size == 0) { + write_log("A NULL %ld\n", (long) size); + return NULL; + } + + hdr = (alloc_hdr *) malloc(size + sizeof(alloc_hdr)); + if (!hdr) { + write_log("A FAIL %ld\n", (long) size); + return NULL; + } + hdr->u.sz = size; + ret = (void *) (hdr + 1); + write_log("A %p %ld\n", ret, (long) size); + return ret; +} + +void *duk_realloc_logging(void *udata, void *ptr, duk_size_t size) { + alloc_hdr *hdr; + size_t old_size; + void *t; + void *ret; + + (void) udata; /* Suppress warning. */ + + /* Handle the ptr-NULL vs. size-zero cases explicitly to minimize + * platform assumptions. You can get away with much less in specific + * well-behaving environments. + */ + + if (ptr) { + hdr = (alloc_hdr *) (void *) ((unsigned char *) ptr - sizeof(alloc_hdr)); + old_size = hdr->u.sz; + + if (size == 0) { + write_log("R %p %ld NULL 0\n", ptr, (long) old_size); + free((void *) hdr); + return NULL; + } else { + t = realloc((void *) hdr, size + sizeof(alloc_hdr)); + if (!t) { + write_log("R %p %ld FAIL %ld\n", ptr, (long) old_size, (long) size); + return NULL; + } + hdr = (alloc_hdr *) t; + hdr->u.sz = size; + ret = (void *) (hdr + 1); + write_log("R %p %ld %p %ld\n", ptr, (long) old_size, ret, (long) size); + return ret; + } + } else { + if (size == 0) { + write_log("R NULL 0 NULL 0\n"); + return NULL; + } else { + hdr = (alloc_hdr *) malloc(size + sizeof(alloc_hdr)); + if (!hdr) { + write_log("R NULL 0 FAIL %ld\n", (long) size); + return NULL; + } + hdr->u.sz = size; + ret = (void *) (hdr + 1); + write_log("R NULL 0 %p %ld\n", ret, (long) size); + return ret; + } + } +} + +void duk_free_logging(void *udata, void *ptr) { + alloc_hdr *hdr; + + (void) udata; /* Suppress warning. */ + + if (!ptr) { + write_log("F NULL 0\n"); + return; + } + hdr = (alloc_hdr *) (void *) ((unsigned char *) ptr - sizeof(alloc_hdr)); + write_log("F %p %ld\n", ptr, (long) hdr->u.sz); + free((void *) hdr); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/duk_alloc_logging.h b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/duk_alloc_logging.h new file mode 100644 index 000000000..670035313 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/duk_alloc_logging.h @@ -0,0 +1,10 @@ +#ifndef DUK_ALLOC_LOGGING_H_INCLUDED +#define DUK_ALLOC_LOGGING_H_INCLUDED + +#include "duktape.h" + +void *duk_alloc_logging(void *udata, duk_size_t size); +void *duk_realloc_logging(void *udata, void *ptr, duk_size_t size); +void duk_free_logging(void *udata, void *ptr); + +#endif /* DUK_ALLOC_LOGGING_H_INCLUDED */ diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/log2gnuplot.py b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/log2gnuplot.py new file mode 100644 index 000000000..0528259dc --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/log2gnuplot.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python2 +# +# Analyze allocator logs and write total-bytes-in-use after every +# operation to stdout. The output can be gnuplotted as: +# +# $ python log2gnuplot.py /tmp/output.txt +# $ gnuplot +# > plot "output.txt" with lines +# + +import os +import sys + +def main(): + allocated = 0 + + for line in sys.stdin: + line = line.strip() + parts = line.split(' ') + + # A ptr/NULL/FAIL size + # F ptr/NULL size + # R ptr/NULL oldsize ptr/NULL/FAIL newsize + + # Note: ajduk doesn't log oldsize (uses -1 instead) + + if parts[0] == 'A': + if parts[1] != 'NULL' and parts[1] != 'FAIL': + allocated += long(parts[2]) + elif parts[0] == 'F': + allocated -= long(parts[2]) + elif parts[0] == 'R': + allocated -= long(parts[2]) + if parts[3] != 'NULL' and parts[3] != 'FAIL': + allocated += long(parts[4]) + print(allocated) + + print(allocated) + +if __name__ == '__main__': + main() diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-torture/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-torture/README.rst new file mode 100644 index 000000000..f3278bbf4 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-torture/README.rst @@ -0,0 +1,10 @@ +========================================== +Allocator with memory wiping and red zones +========================================== + +Example allocator that wipes memory on free and checks that no out-of-bounds +writes have been made to bytes just before and after the allocated area. + +Valgrind is a better tool for detecting these memory issues, but it's not +available for all targets so you can use something like this to detect +memory lifecycle or out-of-bounds issues. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-torture/duk_alloc_torture.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-torture/duk_alloc_torture.c new file mode 100644 index 000000000..abca2f755 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-torture/duk_alloc_torture.c @@ -0,0 +1,182 @@ +/* + * Example torture memory allocator with memory wiping and check for + * out-of-bounds writes. + * + * Allocation structure: + * + * [ alloc_hdr | red zone before | user area | red zone after ] + * + * ^ ^ + * | `--- pointer returned to Duktape + * `--- underlying malloc ptr + */ + +#include "duktape.h" +#include +#include +#include +#include + +#define RED_ZONE_SIZE 16 +#define RED_ZONE_BYTE 0x5a +#define INIT_BYTE 0xa5 +#define WIPE_BYTE 0x27 + +typedef struct { + /* The double value in the union is there to ensure alignment is + * good for IEEE doubles too. In many 32-bit environments 4 bytes + * would be sufficiently aligned and the double value is unnecessary. + */ + union { + size_t sz; + double d; + } u; +} alloc_hdr; + +static void check_red_zone(alloc_hdr *hdr) { + size_t size; + int i; + int err; + unsigned char *p; + unsigned char *userptr; + + size = hdr->u.sz; + userptr = (unsigned char *) hdr + sizeof(alloc_hdr) + RED_ZONE_SIZE; + + err = 0; + p = (unsigned char *) hdr + sizeof(alloc_hdr); + for (i = 0; i < RED_ZONE_SIZE; i++) { + if (p[i] != RED_ZONE_BYTE) { + err = 1; + } + } + if (err) { + fprintf(stderr, "RED ZONE CORRUPTED BEFORE ALLOC: hdr=%p ptr=%p size=%ld\n", + (void *) hdr, (void *) userptr, (long) size); + fflush(stderr); + } + + err = 0; + p = (unsigned char *) hdr + sizeof(alloc_hdr) + RED_ZONE_SIZE + size; + for (i = 0; i < RED_ZONE_SIZE; i++) { + if (p[i] != RED_ZONE_BYTE) { + err = 1; + } + } + if (err) { + fprintf(stderr, "RED ZONE CORRUPTED AFTER ALLOC: hdr=%p ptr=%p size=%ld\n", + (void *) hdr, (void *) userptr, (long) size); + fflush(stderr); + } +} + +void *duk_alloc_torture(void *udata, duk_size_t size) { + unsigned char *p; + + (void) udata; /* Suppress warning. */ + + if (size == 0) { + return NULL; + } + + p = (unsigned char *) malloc(size + sizeof(alloc_hdr) + 2 * RED_ZONE_SIZE); + if (!p) { + return NULL; + } + + ((alloc_hdr *) (void *) p)->u.sz = size; + p += sizeof(alloc_hdr); + memset((void *) p, RED_ZONE_BYTE, RED_ZONE_SIZE); + p += RED_ZONE_SIZE; + memset((void *) p, INIT_BYTE, size); + p += size; + memset((void *) p, RED_ZONE_BYTE, RED_ZONE_SIZE); + p -= size; + return (void *) p; +} + +void *duk_realloc_torture(void *udata, void *ptr, duk_size_t size) { + unsigned char *p, *old_p; + size_t old_size; + + (void) udata; /* Suppress warning. */ + + /* Handle the ptr-NULL vs. size-zero cases explicitly to minimize + * platform assumptions. You can get away with much less in specific + * well-behaving environments. + */ + + if (ptr) { + old_p = (unsigned char *) ptr - sizeof(alloc_hdr) - RED_ZONE_SIZE; + old_size = ((alloc_hdr *) (void *) old_p)->u.sz; + check_red_zone((alloc_hdr *) (void *) old_p); + + if (size == 0) { + memset((void *) old_p, WIPE_BYTE, old_size + sizeof(alloc_hdr) + 2 * RED_ZONE_SIZE); + free((void *) old_p); + return NULL; + } else { + /* Force address change on every realloc. */ + p = (unsigned char *) malloc(size + sizeof(alloc_hdr) + 2 * RED_ZONE_SIZE); + if (!p) { + return NULL; + } + + ((alloc_hdr *) (void *) p)->u.sz = size; + p += sizeof(alloc_hdr); + memset((void *) p, RED_ZONE_BYTE, RED_ZONE_SIZE); + p += RED_ZONE_SIZE; + if (size > old_size) { + memcpy((void *) p, (void *) (old_p + sizeof(alloc_hdr) + RED_ZONE_SIZE), old_size); + memset((void *) (p + old_size), INIT_BYTE, size - old_size); + } else { + memcpy((void *) p, (void *) (old_p + sizeof(alloc_hdr) + RED_ZONE_SIZE), size); + } + p += size; + memset((void *) p, RED_ZONE_BYTE, RED_ZONE_SIZE); + p -= size; + + memset((void *) old_p, WIPE_BYTE, old_size + sizeof(alloc_hdr) + 2 * RED_ZONE_SIZE); + free((void *) old_p); + + return (void *) p; + } + } else { + if (size == 0) { + return NULL; + } else { + p = (unsigned char *) malloc(size + sizeof(alloc_hdr) + 2 * RED_ZONE_SIZE); + if (!p) { + return NULL; + } + + ((alloc_hdr *) (void *) p)->u.sz = size; + p += sizeof(alloc_hdr); + memset((void *) p, RED_ZONE_BYTE, RED_ZONE_SIZE); + p += RED_ZONE_SIZE; + memset((void *) p, INIT_BYTE, size); + p += size; + memset((void *) p, RED_ZONE_BYTE, RED_ZONE_SIZE); + p -= size; + return (void *) p; + } + } +} + +void duk_free_torture(void *udata, void *ptr) { + unsigned char *p; + size_t old_size; + + (void) udata; /* Suppress warning. */ + + if (!ptr) { + return; + } + + p = (unsigned char *) ptr - sizeof(alloc_hdr) - RED_ZONE_SIZE; + old_size = ((alloc_hdr *) (void *) p)->u.sz; + + check_red_zone((alloc_hdr *) (void *) p); + memset((void *) p, WIPE_BYTE, old_size + sizeof(alloc_hdr) + 2 * RED_ZONE_SIZE); + free((void *) p); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-torture/duk_alloc_torture.h b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-torture/duk_alloc_torture.h new file mode 100644 index 000000000..a12153a2a --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/alloc-torture/duk_alloc_torture.h @@ -0,0 +1,10 @@ +#ifndef DUK_ALLOC_TORTURE_H_INCLUDED +#define DUK_ALLOC_TORTURE_H_INCLUDED + +#include "duktape.h" + +void *duk_alloc_torture(void *udata, duk_size_t size); +void *duk_realloc_torture(void *udata, void *ptr, duk_size_t size); +void duk_free_torture(void *udata, void *ptr); + +#endif /* DUK_ALLOC_TORTURE_H_INCLUDED */ diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/cmdline/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/cmdline/README.rst new file mode 100644 index 000000000..e0c6bcebe --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/cmdline/README.rst @@ -0,0 +1,6 @@ +==================== +Duktape command line +==================== + +Ecmascript command line execution tool, useful for running Ecmascript code +from a file, stdin, or interactively. Also used by automatic testing. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/cmdline/duk_cmdline.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/cmdline/duk_cmdline.c new file mode 100644 index 000000000..ea5af55e8 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/cmdline/duk_cmdline.c @@ -0,0 +1,1463 @@ +/* + * Command line execution tool. Useful for test cases and manual testing. + * + * To enable linenoise and other fancy stuff, compile with -DDUK_CMDLINE_FANCY. + * It is not the default to maximize portability. You can also compile in + * support for example allocators, grep for DUK_CMDLINE_*. + */ + +/* Helper define to enable a feature set; can also use separate defines. */ +#if defined(DUK_CMDLINE_FANCY) +#define DUK_CMDLINE_LINENOISE +#define DUK_CMDLINE_LINENOISE_COMPLETION +#define DUK_CMDLINE_RLIMIT +#define DUK_CMDLINE_SIGNAL +#endif + +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || \ + defined(WIN64) || defined(_WIN64) || defined(__WIN64__) +/* Suppress warnings about plain fopen() etc. */ +#define _CRT_SECURE_NO_WARNINGS +#if defined(_MSC_VER) && (_MSC_VER < 1900) +/* Workaround for snprintf() missing in older MSVC versions. + * Note that _snprintf() may not NUL terminate the string, but + * this difference does not matter here as a NUL terminator is + * always explicitly added. + */ +#define snprintf _snprintf +#endif +#endif + +#define GREET_CODE(variant) \ + "print('((o) Duktape" variant " ' + " \ + "Math.floor(Duktape.version / 10000) + '.' + " \ + "Math.floor(Duktape.version / 100) % 100 + '.' + " \ + "Duktape.version % 100" \ + ", '(" DUK_GIT_DESCRIBE ")');" + +#include +#include +#include +#if defined(DUK_CMDLINE_SIGNAL) +#include +#endif +#if defined(DUK_CMDLINE_RLIMIT) +#include +#endif +#if defined(DUK_CMDLINE_LINENOISE) +#include "linenoise.h" +#endif +#if defined(DUK_CMDLINE_FILEIO) +#include +#endif +#if defined(EMSCRIPTEN) +#include +#endif +#if defined(DUK_CMDLINE_ALLOC_LOGGING) +#include "duk_alloc_logging.h" +#endif +#if defined(DUK_CMDLINE_ALLOC_TORTURE) +#include "duk_alloc_torture.h" +#endif +#if defined(DUK_CMDLINE_ALLOC_HYBRID) +#include "duk_alloc_hybrid.h" +#endif +#include "duktape.h" + +#if defined(DUK_CMDLINE_AJSHEAP) +/* Defined in duk_cmdline_ajduk.c or alljoyn.js headers. */ +void ajsheap_init(void); +void ajsheap_free(void); +void ajsheap_dump(void); +void ajsheap_register(duk_context *ctx); +void ajsheap_start_exec_timeout(void); +void ajsheap_clear_exec_timeout(void); +void *ajsheap_alloc_wrapped(void *udata, duk_size_t size); +void *ajsheap_realloc_wrapped(void *udata, void *ptr, duk_size_t size); +void ajsheap_free_wrapped(void *udata, void *ptr); +void *AJS_Alloc(void *udata, duk_size_t size); +void *AJS_Realloc(void *udata, void *ptr, duk_size_t size); +void AJS_Free(void *udata, void *ptr); +#endif + +#if defined(DUK_CMDLINE_DEBUGGER_SUPPORT) +#include "duk_trans_socket.h" +#endif + +#define MEM_LIMIT_NORMAL (128*1024*1024) /* 128 MB */ +#define MEM_LIMIT_HIGH (2047*1024*1024) /* ~2 GB */ +#define LINEBUF_SIZE 65536 + +static int main_argc = 0; +static char **main_argv = NULL; +static int interactive_mode = 0; +#if defined(DUK_CMDLINE_DEBUGGER_SUPPORT) +static int debugger_reattach = 0; +#endif + +/* + * Misc helpers + */ + +#if defined(DUK_CMDLINE_RLIMIT) +static void set_resource_limits(rlim_t mem_limit_value) { + int rc; + struct rlimit lim; + + rc = getrlimit(RLIMIT_AS, &lim); + if (rc != 0) { + fprintf(stderr, "Warning: cannot read RLIMIT_AS\n"); + return; + } + + if (lim.rlim_max < mem_limit_value) { + fprintf(stderr, "Warning: rlim_max < mem_limit_value (%d < %d)\n", (int) lim.rlim_max, (int) mem_limit_value); + return; + } + + lim.rlim_cur = mem_limit_value; + lim.rlim_max = mem_limit_value; + + rc = setrlimit(RLIMIT_AS, &lim); + if (rc != 0) { + fprintf(stderr, "Warning: setrlimit failed\n"); + return; + } + +#if 0 + fprintf(stderr, "Set RLIMIT_AS to %d\n", (int) mem_limit_value); +#endif +} +#endif /* DUK_CMDLINE_RLIMIT */ + +#if defined(DUK_CMDLINE_SIGNAL) +static void my_sighandler(int x) { + fprintf(stderr, "Got signal %d\n", x); + fflush(stderr); +} +static void set_sigint_handler(void) { + (void) signal(SIGINT, my_sighandler); + (void) signal(SIGPIPE, SIG_IGN); /* avoid SIGPIPE killing process */ +} +#endif /* DUK_CMDLINE_SIGNAL */ + +static int get_stack_raw(duk_context *ctx) { + if (!duk_is_object(ctx, -1)) { + return 1; + } + if (!duk_has_prop_string(ctx, -1, "stack")) { + return 1; + } + if (!duk_is_error(ctx, -1)) { + /* Not an Error instance, don't read "stack". */ + return 1; + } + + duk_get_prop_string(ctx, -1, "stack"); /* caller coerces */ + duk_remove(ctx, -2); + return 1; +} + +/* Print error to stderr and pop error. */ +static void print_pop_error(duk_context *ctx, FILE *f) { + /* Print error objects with a stack trace specially. + * Note that getting the stack trace may throw an error + * so this also needs to be safe call wrapped. + */ + (void) duk_safe_call(ctx, get_stack_raw, 1 /*nargs*/, 1 /*nrets*/); + fprintf(f, "%s\n", duk_safe_to_string(ctx, -1)); + fflush(f); + duk_pop(ctx); +} + +static int wrapped_compile_execute(duk_context *ctx) { + const char *src_data; + duk_size_t src_len; + int comp_flags; + + /* XXX: Here it'd be nice to get some stats for the compilation result + * when a suitable command line is given (e.g. code size, constant + * count, function count. These are available internally but not through + * the public API. + */ + + /* Use duk_compile_lstring_filename() variant which avoids interning + * the source code. This only really matters for low memory environments. + */ + + /* [ ... bytecode_filename src_data src_len filename ] */ + + src_data = (const char *) duk_require_pointer(ctx, -3); + src_len = (duk_size_t) duk_require_uint(ctx, -2); + + if (src_data != NULL && src_len >= 2 && src_data[0] == (char) 0xff) { + /* Bytecode. */ + duk_push_lstring(ctx, src_data, src_len); + duk_to_buffer(ctx, -1, NULL); + duk_load_function(ctx); + } else { + /* Source code. */ + comp_flags = 0; + duk_compile_lstring_filename(ctx, comp_flags, src_data, src_len); + } + + /* [ ... bytecode_filename src_data src_len function ] */ + + /* Optional bytecode dump. */ + if (duk_is_string(ctx, -4)) { + FILE *f; + void *bc_ptr; + duk_size_t bc_len; + size_t wrote; + char fnbuf[256]; + const char *filename; + + duk_dup_top(ctx); + duk_dump_function(ctx); + bc_ptr = duk_require_buffer(ctx, -1, &bc_len); + filename = duk_require_string(ctx, -5); +#if defined(EMSCRIPTEN) + if (filename[0] == '/') { + snprintf(fnbuf, sizeof(fnbuf), "%s", filename); + } else { + snprintf(fnbuf, sizeof(fnbuf), "/working/%s", filename); + } +#else + snprintf(fnbuf, sizeof(fnbuf), "%s", filename); +#endif + fnbuf[sizeof(fnbuf) - 1] = (char) 0; + + f = fopen(fnbuf, "wb"); + if (!f) { + duk_error(ctx, DUK_ERR_ERROR, "failed to open bytecode output file"); + } + wrote = fwrite(bc_ptr, 1, (size_t) bc_len, f); /* XXX: handle partial writes */ + (void) fclose(f); + if (wrote != bc_len) { + duk_error(ctx, DUK_ERR_ERROR, "failed to write all bytecode"); + } + + return 0; /* duk_safe_call() cleans up */ + } + +#if 0 + /* Manual test for bytecode dump/load cycle: dump and load before + * execution. Enable manually, then run "make qecmatest" for a + * reasonably good coverage of different functions and programs. + */ + duk_dump_function(ctx); + duk_load_function(ctx); +#endif + +#if defined(DUK_CMDLINE_AJSHEAP) + ajsheap_start_exec_timeout(); +#endif + + duk_push_global_object(ctx); /* 'this' binding */ + duk_call_method(ctx, 0); + +#if defined(DUK_CMDLINE_AJSHEAP) + ajsheap_clear_exec_timeout(); +#endif + + if (interactive_mode) { + /* + * In interactive mode, write to stdout so output won't + * interleave as easily. + * + * NOTE: the ToString() coercion may fail in some cases; + * for instance, if you evaluate: + * + * ( {valueOf: function() {return {}}, + * toString: function() {return {}}}); + * + * The error is: + * + * TypeError: failed to coerce with [[DefaultValue]] + * duk_api.c:1420 + * + * These are handled now by the caller which also has stack + * trace printing support. User code can print out errors + * safely using duk_safe_to_string(). + */ + + fprintf(stdout, "= %s\n", duk_to_string(ctx, -1)); + fflush(stdout); + } else { + /* In non-interactive mode, success results are not written at all. + * It is important that the result value is not string coerced, + * as the string coercion may cause an error in some cases. + */ + } + + return 0; /* duk_safe_call() cleans up */ +} + +/* + * Minimal Linenoise completion support + */ + +#if defined(DUK_CMDLINE_LINENOISE_COMPLETION) +static duk_context *completion_ctx; + +static int completion_idpart(unsigned char c) { + /* Very simplified "is identifier part" check. */ + if ((c >= (unsigned char) 'a' && c <= (unsigned char) 'z') || + (c >= (unsigned char) 'A' && c <= (unsigned char) 'Z') || + (c >= (unsigned char) '0' && c <= (unsigned char) '9') || + c == (unsigned char) '$' || c == (unsigned char) '_') { + return 1; + } + return 0; +} + +static int completion_digit(unsigned char c) { + return (c >= (unsigned char) '0' && c <= (unsigned char) '9'); +} + +static duk_ret_t linenoise_completion_lookup(duk_context *ctx) { + duk_size_t len; + const char *orig; + const unsigned char *p; + const unsigned char *p_curr; + const unsigned char *p_end; + const char *key; + const char *prefix; + linenoiseCompletions *lc; + duk_idx_t idx_obj; + + orig = duk_require_string(ctx, -3); + p_curr = (const unsigned char *) duk_require_lstring(ctx, -2, &len); + p_end = p_curr + len; + lc = duk_require_pointer(ctx, -1); + + duk_push_global_object(ctx); + idx_obj = duk_require_top_index(ctx); + + while (p_curr <= p_end) { + /* p_curr == p_end allowed on purpose, to handle 'Math.' for example. */ + p = p_curr; + while (p < p_end && p[0] != (unsigned char) '.') { + p++; + } + /* 'p' points to a NUL (p == p_end) or a period. */ + prefix = duk_push_lstring(ctx, (const char *) p_curr, (duk_size_t) (p - p_curr)); + +#if 0 + fprintf(stderr, "Completion check: '%s'\n", prefix); + fflush(stderr); +#endif + + if (p == p_end) { + /* 'idx_obj' points to the object matching the last + * full component, use [p_curr,p[ as a filter for + * that object. + */ + + duk_enum(ctx, idx_obj, DUK_ENUM_INCLUDE_NONENUMERABLE); + while (duk_next(ctx, -1, 0 /*get_value*/)) { + key = duk_get_string(ctx, -1); +#if 0 + fprintf(stderr, "Key: %s\n", key ? key : ""); + fflush(stderr); +#endif + if (!key) { + /* Should never happen, just in case. */ + goto next; + } + + /* Ignore array index keys: usually not desirable, and would + * also require ['0'] quoting. + */ + if (completion_digit(key[0])) { + goto next; + } + + /* XXX: There's no key quoting now, it would require replacing the + * last component with a ['foo\nbar'] style lookup when appropriate. + */ + + if (strlen(prefix) == 0) { + /* Partial ends in a period, e.g. 'Math.' -> complete all Math properties. */ + duk_push_string(ctx, orig); /* original, e.g. 'Math.' */ + duk_push_string(ctx, key); + duk_concat(ctx, 2); + linenoiseAddCompletion(lc, duk_require_string(ctx, -1)); + duk_pop(ctx); + } else if (prefix && strcmp(key, prefix) == 0) { + /* Full completion, add a period, e.g. input 'Math' -> 'Math.'. */ + duk_push_string(ctx, orig); /* original, including partial last component */ + duk_push_string(ctx, "."); + duk_concat(ctx, 2); + linenoiseAddCompletion(lc, duk_require_string(ctx, -1)); + duk_pop(ctx); + } else if (prefix && strncmp(key, prefix, strlen(prefix)) == 0) { + /* Last component is partial, complete. */ + duk_push_string(ctx, orig); /* original, including partial last component */ + duk_push_string(ctx, key + strlen(prefix)); /* completion to last component */ + duk_concat(ctx, 2); + linenoiseAddCompletion(lc, duk_require_string(ctx, -1)); + duk_pop(ctx); + } + + next: + duk_pop(ctx); + } + return 0; + } else { + if (duk_get_prop(ctx, idx_obj)) { + duk_to_object(ctx, -1); /* for properties of plain strings etc */ + duk_replace(ctx, idx_obj); + p_curr = p + 1; + } else { + /* Not found. */ + return 0; + } + } + } + + return 0; +} + +static void linenoise_completion(const char *buf, linenoiseCompletions *lc) { + duk_context *ctx; + const unsigned char *p_start; + const unsigned char *p_end; + const unsigned char *p; + duk_int_t rc; + + if (!buf) { + return; + } + ctx = completion_ctx; + if (!ctx) { + return; + } + + p_start = (const unsigned char *) buf; + p_end = (const unsigned char *) (buf + strlen(buf)); + p = p_end; + + /* Scan backwards for a maximal string which looks like a property + * chain (e.g. foo.bar.quux). + */ + + while (--p >= p_start) { + if (p[0] == (unsigned char) '.') { + if (p <= p_start) { + break; + } + if (!completion_idpart(p[-1])) { + /* Catches e.g. 'foo..bar' -> we want 'bar' only. */ + break; + } + } else if (!completion_idpart(p[0])) { + break; + } + } + /* 'p' will either be p_start - 1 (ran out of buffer) or point to + * the first offending character. + */ + p++; + if (p < p_start || p >= p_end) { + return; /* should never happen, but just in case */ + } + + /* 'p' now points to a string of the form 'foo.bar.quux'. Look up + * all the components except the last; treat the last component as + * a partial name which is used as a filter for the previous full + * component. All lookups are from the global object now. + */ + +#if 0 + fprintf(stderr, "Completion starting point: '%s'\n", p); + fflush(stderr); +#endif + + duk_push_string(ctx, (const char *) buf); + duk_push_lstring(ctx, (const char *) p, (duk_size_t) (p_end - p)); + duk_push_pointer(ctx, (void *) lc); + + rc = duk_safe_call(ctx, linenoise_completion_lookup, 3 /*nargs*/, 1 /*nrets*/); + if (rc != DUK_EXEC_SUCCESS) { + fprintf(stderr, "Completion handling failure: %s\n", duk_safe_to_string(ctx, -1)); + } + duk_pop(ctx); +} +#endif /* DUK_CMDLINE_LINENOISE_COMPLETION */ + +/* + * Execute from file handle etc + */ + +static int handle_fh(duk_context *ctx, FILE *f, const char *filename, const char *bytecode_filename) { + char *buf = NULL; + size_t bufsz; + size_t bufoff; + size_t got; + int rc; + int retval = -1; + + buf = (char *) malloc(1024); + if (!buf) { + goto error; + } + bufsz = 1024; + bufoff = 0; + + /* Read until EOF, avoid fseek/stat because it won't work with stdin. */ + for (;;) { + size_t avail; + + avail = bufsz - bufoff; + if (avail < 1024) { + size_t newsz; + char *buf_new; +#if 0 + fprintf(stderr, "resizing read buffer: %ld -> %ld\n", (long) bufsz, (long) (bufsz * 2)); +#endif + newsz = bufsz + (bufsz >> 2) + 1024; /* +25% and some extra */ + buf_new = (char *) realloc(buf, newsz); + if (!buf_new) { + goto error; + } + buf = buf_new; + bufsz = newsz; + } + + avail = bufsz - bufoff; +#if 0 + fprintf(stderr, "reading input: buf=%p bufsz=%ld bufoff=%ld avail=%ld\n", + (void *) buf, (long) bufsz, (long) bufoff, (long) avail); +#endif + + got = fread((void *) (buf + bufoff), (size_t) 1, avail, f); +#if 0 + fprintf(stderr, "got=%ld\n", (long) got); +#endif + if (got == 0) { + break; + } + bufoff += got; + + /* Emscripten specific: stdin EOF doesn't work as expected. + * Instead, when 'emduk' is executed using Node.js, a file + * piped to stdin repeats (!). Detect that repeat and cut off + * the stdin read. Ensure the loop repeats enough times to + * avoid detecting spurious loops. + * + * This only seems to work for inputs up to 256 bytes long. + */ +#if defined(EMSCRIPTEN) + if (bufoff >= 16384) { + size_t i, j, nloops; + int looped = 0; + + for (i = 16; i < bufoff / 8; i++) { + int ok; + + nloops = bufoff / i; + ok = 1; + for (j = 1; j < nloops; j++) { + if (memcmp((void *) buf, (void *) (buf + i * j), i) != 0) { + ok = 0; + break; + } + } + if (ok) { + fprintf(stderr, "emscripten workaround: detect looping at index %ld, verified with %ld loops\n", (long) i, (long) (nloops - 1)); + bufoff = i; + looped = 1; + break; + } + } + + if (looped) { + break; + } + } +#endif + } + + duk_push_string(ctx, bytecode_filename); + duk_push_pointer(ctx, (void *) buf); + duk_push_uint(ctx, (duk_uint_t) bufoff); + duk_push_string(ctx, filename); + + interactive_mode = 0; /* global */ + + rc = duk_safe_call(ctx, wrapped_compile_execute, 4 /*nargs*/, 1 /*nret*/); + +#if defined(DUK_CMDLINE_AJSHEAP) + ajsheap_clear_exec_timeout(); +#endif + + free(buf); + buf = NULL; + + if (rc != DUK_EXEC_SUCCESS) { + print_pop_error(ctx, stderr); + goto error; + } else { + duk_pop(ctx); + retval = 0; + } + /* fall thru */ + + cleanup: + if (buf) { + free(buf); + buf = NULL; + } + return retval; + + error: + fprintf(stderr, "error in executing file %s\n", filename); + fflush(stderr); + goto cleanup; +} + +static int handle_file(duk_context *ctx, const char *filename, const char *bytecode_filename) { + FILE *f = NULL; + int retval; + char fnbuf[256]; + + /* Example of sending an application specific debugger notification. */ + duk_push_string(ctx, "DebuggerHandleFile"); + duk_push_string(ctx, filename); + duk_debugger_notify(ctx, 2); + +#if defined(EMSCRIPTEN) + if (filename[0] == '/') { + snprintf(fnbuf, sizeof(fnbuf), "%s", filename); + } else { + snprintf(fnbuf, sizeof(fnbuf), "/working/%s", filename); + } +#else + snprintf(fnbuf, sizeof(fnbuf), "%s", filename); +#endif + fnbuf[sizeof(fnbuf) - 1] = (char) 0; + + f = fopen(fnbuf, "rb"); + if (!f) { + fprintf(stderr, "failed to open source file: %s\n", filename); + fflush(stderr); + goto error; + } + + retval = handle_fh(ctx, f, filename, bytecode_filename); + + fclose(f); + return retval; + + error: + return -1; +} + +static int handle_eval(duk_context *ctx, char *code) { + int rc; + int retval = -1; + + duk_push_pointer(ctx, (void *) code); + duk_push_uint(ctx, (duk_uint_t) strlen(code)); + duk_push_string(ctx, "eval"); + + interactive_mode = 0; /* global */ + + rc = duk_safe_call(ctx, wrapped_compile_execute, 3 /*nargs*/, 1 /*nret*/); + +#if defined(DUK_CMDLINE_AJSHEAP) + ajsheap_clear_exec_timeout(); +#endif + + if (rc != DUK_EXEC_SUCCESS) { + print_pop_error(ctx, stderr); + } else { + duk_pop(ctx); + retval = 0; + } + + return retval; +} + +#if defined(DUK_CMDLINE_LINENOISE) +static int handle_interactive(duk_context *ctx) { + const char *prompt = "duk> "; + char *buffer = NULL; + int retval = 0; + int rc; + + duk_eval_string(ctx, GREET_CODE(" [linenoise]")); + duk_pop(ctx); + + linenoiseSetMultiLine(1); + linenoiseHistorySetMaxLen(64); +#if defined(DUK_CMDLINE_LINENOISE_COMPLETION) + linenoiseSetCompletionCallback(linenoise_completion); +#endif + + for (;;) { + if (buffer) { + linenoiseFree(buffer); + buffer = NULL; + } + +#if defined(DUK_CMDLINE_LINENOISE_COMPLETION) + completion_ctx = ctx; +#endif + buffer = linenoise(prompt); +#if defined(DUK_CMDLINE_LINENOISE_COMPLETION) + completion_ctx = NULL; +#endif + + if (!buffer) { + break; + } + + if (buffer && buffer[0] != (char) 0) { + linenoiseHistoryAdd(buffer); + } + + duk_push_pointer(ctx, (void *) buffer); + duk_push_uint(ctx, (duk_uint_t) strlen(buffer)); + duk_push_string(ctx, "input"); + + interactive_mode = 1; /* global */ + + rc = duk_safe_call(ctx, wrapped_compile_execute, 3 /*nargs*/, 1 /*nret*/); + +#if defined(DUK_CMDLINE_AJSHEAP) + ajsheap_clear_exec_timeout(); +#endif + + if (buffer) { + linenoiseFree(buffer); + buffer = NULL; + } + + if (rc != DUK_EXEC_SUCCESS) { + /* in interactive mode, write to stdout */ + print_pop_error(ctx, stdout); + retval = -1; /* an error 'taints' the execution */ + } else { + duk_pop(ctx); + } + } + + if (buffer) { + linenoiseFree(buffer); + buffer = NULL; + } + + return retval; +} +#else /* DUK_CMDLINE_LINENOISE */ +static int handle_interactive(duk_context *ctx) { + const char *prompt = "duk> "; + char *buffer = NULL; + int retval = 0; + int rc; + int got_eof = 0; + + duk_eval_string(ctx, GREET_CODE("")); + duk_pop(ctx); + + buffer = (char *) malloc(LINEBUF_SIZE); + if (!buffer) { + fprintf(stderr, "failed to allocated a line buffer\n"); + fflush(stderr); + retval = -1; + goto done; + } + + while (!got_eof) { + size_t idx = 0; + + fwrite(prompt, 1, strlen(prompt), stdout); + fflush(stdout); + + for (;;) { + int c = fgetc(stdin); + if (c == EOF) { + got_eof = 1; + break; + } else if (c == '\n') { + break; + } else if (idx >= LINEBUF_SIZE) { + fprintf(stderr, "line too long\n"); + fflush(stderr); + retval = -1; + goto done; + } else { + buffer[idx++] = (char) c; + } + } + + duk_push_pointer(ctx, (void *) buffer); + duk_push_uint(ctx, (duk_uint_t) idx); + duk_push_string(ctx, "input"); + + interactive_mode = 1; /* global */ + + rc = duk_safe_call(ctx, wrapped_compile_execute, 3 /*nargs*/, 1 /*nret*/); + +#if defined(DUK_CMDLINE_AJSHEAP) + ajsheap_clear_exec_timeout(); +#endif + + if (rc != DUK_EXEC_SUCCESS) { + /* in interactive mode, write to stdout */ + print_pop_error(ctx, stdout); + retval = -1; /* an error 'taints' the execution */ + } else { + duk_pop(ctx); + } + } + + done: + if (buffer) { + free(buffer); + buffer = NULL; + } + + return retval; +} +#endif /* DUK_CMDLINE_LINENOISE */ + +/* + * Simple file read/write bindings + */ + +#if defined(DUK_CMDLINE_FILEIO) +static duk_ret_t fileio_read_file(duk_context *ctx) { + const char *fn; + char *buf; + size_t len; + size_t off; + int rc; + FILE *f; + + fn = duk_require_string(ctx, 0); + f = fopen(fn, "rb"); + if (!f) { + duk_error(ctx, DUK_ERR_TYPE_ERROR, "cannot open file %s for reading, errno %ld: %s", + fn, (long) errno, strerror(errno)); + } + + rc = fseek(f, 0, SEEK_END); + if (rc < 0) { + (void) fclose(f); + duk_error(ctx, DUK_ERR_TYPE_ERROR, "fseek() failed for %s, errno %ld: %s", + fn, (long) errno, strerror(errno)); + } + len = (size_t) ftell(f); + rc = fseek(f, 0, SEEK_SET); + if (rc < 0) { + (void) fclose(f); + duk_error(ctx, DUK_ERR_TYPE_ERROR, "fseek() failed for %s, errno %ld: %s", + fn, (long) errno, strerror(errno)); + } + + buf = (char *) duk_push_fixed_buffer(ctx, (duk_size_t) len); + for (off = 0; off < len;) { + size_t got; + got = fread((void *) (buf + off), 1, len - off, f); + if (ferror(f)) { + (void) fclose(f); + duk_error(ctx, DUK_ERR_TYPE_ERROR, "error while reading %s", fn); + } + if (got == 0) { + if (feof(f)) { + break; + } else { + (void) fclose(f); + duk_error(ctx, DUK_ERR_TYPE_ERROR, "error while reading %s", fn); + } + } + off += got; + } + + if (f) { + (void) fclose(f); + } + + return 1; +} + +static duk_ret_t fileio_write_file(duk_context *ctx) { + const char *fn; + const char *buf; + size_t len; + size_t off; + FILE *f; + + fn = duk_require_string(ctx, 0); + f = fopen(fn, "wb"); + if (!f) { + duk_error(ctx, DUK_ERR_TYPE_ERROR, "cannot open file %s for writing, errno %ld: %s", + fn, (long) errno, strerror(errno)); + } + + len = 0; + buf = (char *) duk_to_buffer(ctx, 1, &len); + for (off = 0; off < len;) { + size_t got; + got = fwrite((const void *) (buf + off), 1, len - off, f); + if (ferror(f)) { + (void) fclose(f); + duk_error(ctx, DUK_ERR_TYPE_ERROR, "error while writing %s", fn); + } + if (got == 0) { + (void) fclose(f); + duk_error(ctx, DUK_ERR_TYPE_ERROR, "error while writing %s", fn); + } + off += got; + } + + if (f) { + (void) fclose(f); + } + + return 0; +} +#endif /* DUK_CMDLINE_FILEIO */ + +/* + * Duktape heap lifecycle + */ + +#if defined(DUK_CMDLINE_DEBUGGER_SUPPORT) +static duk_idx_t debugger_request(duk_context *ctx, void *udata, duk_idx_t nvalues) { + const char *cmd; + int i; + + (void) udata; + + if (nvalues < 1) { + duk_push_string(ctx, "missing AppRequest argument(s)"); + return -1; + } + + cmd = duk_get_string(ctx, -nvalues + 0); + + if (cmd && strcmp(cmd, "CommandLine") == 0) { + if (!duk_check_stack(ctx, main_argc)) { + /* Callback should avoid errors for now, so use + * duk_check_stack() rather than duk_require_stack(). + */ + duk_push_string(ctx, "failed to extend stack"); + return -1; + } + for (i = 0; i < main_argc; i++) { + duk_push_string(ctx, main_argv[i]); + } + return main_argc; + } + duk_push_sprintf(ctx, "command not supported"); + return -1; +} + +static void debugger_detached(void *udata) { + duk_context *ctx = (duk_context *) udata; + (void) ctx; + fprintf(stderr, "Debugger detached, udata: %p\n", (void *) udata); + fflush(stderr); + + /* Ensure socket is closed even when detach is initiated by Duktape + * rather than debug client. + */ + duk_trans_socket_finish(); + + if (debugger_reattach) { + /* For automatic reattach testing. */ + duk_trans_socket_init(); + duk_trans_socket_waitconn(); + fprintf(stderr, "Debugger reconnected, call duk_debugger_attach()\n"); + fflush(stderr); +#if 0 + /* This is not necessary but should be harmless. */ + duk_debugger_detach(ctx); +#endif + duk_debugger_attach_custom(ctx, + duk_trans_socket_read_cb, + duk_trans_socket_write_cb, + duk_trans_socket_peek_cb, + duk_trans_socket_read_flush_cb, + duk_trans_socket_write_flush_cb, + debugger_request, + debugger_detached, + (void *) ctx); + } +} +#endif + +#define ALLOC_DEFAULT 0 +#define ALLOC_LOGGING 1 +#define ALLOC_TORTURE 2 +#define ALLOC_HYBRID 3 +#define ALLOC_AJSHEAP 4 + +static duk_context *create_duktape_heap(int alloc_provider, int debugger, int ajsheap_log) { + duk_context *ctx; + + (void) ajsheap_log; /* suppress warning */ + + ctx = NULL; + if (!ctx && alloc_provider == ALLOC_LOGGING) { +#if defined(DUK_CMDLINE_ALLOC_LOGGING) + ctx = duk_create_heap(duk_alloc_logging, + duk_realloc_logging, + duk_free_logging, + (void *) 0xdeadbeef, + NULL); +#else + fprintf(stderr, "Warning: option --alloc-logging ignored, no logging allocator support\n"); + fflush(stderr); +#endif + } + if (!ctx && alloc_provider == ALLOC_TORTURE) { +#if defined(DUK_CMDLINE_ALLOC_TORTURE) + ctx = duk_create_heap(duk_alloc_torture, + duk_realloc_torture, + duk_free_torture, + (void *) 0xdeadbeef, + NULL); +#else + fprintf(stderr, "Warning: option --alloc-torture ignored, no torture allocator support\n"); + fflush(stderr); +#endif + } + if (!ctx && alloc_provider == ALLOC_HYBRID) { +#if defined(DUK_CMDLINE_ALLOC_HYBRID) + void *udata = duk_alloc_hybrid_init(); + if (!udata) { + fprintf(stderr, "Failed to init hybrid allocator\n"); + fflush(stderr); + } else { + ctx = duk_create_heap(duk_alloc_hybrid, + duk_realloc_hybrid, + duk_free_hybrid, + udata, + NULL); + } +#else + fprintf(stderr, "Warning: option --alloc-hybrid ignored, no hybrid allocator support\n"); + fflush(stderr); +#endif + } + if (!ctx && alloc_provider == ALLOC_AJSHEAP) { +#if defined(DUK_CMDLINE_AJSHEAP) + ajsheap_init(); + + ctx = duk_create_heap( + ajsheap_log ? ajsheap_alloc_wrapped : AJS_Alloc, + ajsheap_log ? ajsheap_realloc_wrapped : AJS_Realloc, + ajsheap_log ? ajsheap_free_wrapped : AJS_Free, + (void *) 0xdeadbeef, /* heap_udata: ignored by AjsHeap, use as marker */ + NULL + ); /* fatal_handler */ +#else + fprintf(stderr, "Warning: option --alloc-ajsheap ignored, no ajsheap allocator support\n"); + fflush(stderr); +#endif + } + if (!ctx && alloc_provider == ALLOC_DEFAULT) { + ctx = duk_create_heap_default(); + } + + if (!ctx) { + fprintf(stderr, "Failed to create Duktape heap\n"); + fflush(stderr); + exit(-1); + } + +#if defined(DUK_CMDLINE_AJSHEAP) + if (alloc_provider == ALLOC_AJSHEAP) { + fprintf(stdout, "Pool dump after heap creation\n"); + ajsheap_dump(); + } +#endif + +#if defined(DUK_CMDLINE_AJSHEAP) + if (alloc_provider == ALLOC_AJSHEAP) { + ajsheap_register(ctx); + } +#endif + +#if defined(DUK_CMDLINE_FILEIO) + duk_push_c_function(ctx, fileio_read_file, 1 /*nargs*/); + duk_put_global_string(ctx, "readFile"); + duk_push_c_function(ctx, fileio_write_file, 2 /*nargs*/); + duk_put_global_string(ctx, "writeFile"); +#endif + + if (debugger) { +#if defined(DUK_CMDLINE_DEBUGGER_SUPPORT) + fprintf(stderr, "Debugger enabled, create socket and wait for connection\n"); + fflush(stderr); + duk_trans_socket_init(); + duk_trans_socket_waitconn(); + fprintf(stderr, "Debugger connected, call duk_debugger_attach() and then execute requested file(s)/eval\n"); + fflush(stderr); + duk_debugger_attach_custom(ctx, + duk_trans_socket_read_cb, + duk_trans_socket_write_cb, + duk_trans_socket_peek_cb, + duk_trans_socket_read_flush_cb, + duk_trans_socket_write_flush_cb, + debugger_request, + debugger_detached, + (void *) ctx); +#else + fprintf(stderr, "Warning: option --debugger ignored, no debugger support\n"); + fflush(stderr); +#endif + } + +#if 0 + /* Manual test for duk_debugger_cooperate() */ + { + for (i = 0; i < 60; i++) { + printf("cooperate: %d\n", i); + usleep(1000000); + duk_debugger_cooperate(ctx); + } + } +#endif + + return ctx; +} + +static void destroy_duktape_heap(duk_context *ctx, int alloc_provider) { + (void) alloc_provider; + +#if defined(DUK_CMDLINE_AJSHEAP) + if (alloc_provider == ALLOC_AJSHEAP) { + fprintf(stdout, "Pool dump before duk_destroy_heap(), before forced gc\n"); + ajsheap_dump(); + + duk_gc(ctx, 0); + + fprintf(stdout, "Pool dump before duk_destroy_heap(), after forced gc\n"); + ajsheap_dump(); + } +#endif + + if (ctx) { + duk_destroy_heap(ctx); + } + +#if defined(DUK_CMDLINE_AJSHEAP) + if (alloc_provider == ALLOC_AJSHEAP) { + fprintf(stdout, "Pool dump after duk_destroy_heap() (should have zero allocs)\n"); + ajsheap_dump(); + } + ajsheap_free(); +#endif +} + +/* + * Main + */ + +int main(int argc, char *argv[]) { + duk_context *ctx = NULL; + int retval = 0; + int have_files = 0; + int have_eval = 0; + int interactive = 0; + int memlimit_high = 1; + int alloc_provider = ALLOC_DEFAULT; + int ajsheap_log = 0; + int debugger = 0; + int recreate_heap = 0; + int no_heap_destroy = 0; + int verbose = 0; + int run_stdin = 0; + const char *compile_filename = NULL; + int i; + + main_argc = argc; + main_argv = (char **) argv; + +#if defined(EMSCRIPTEN) + /* Try to use NODEFS to provide access to local files. Mount the + * CWD as /working, and then prepend "/working/" to relative native + * paths in file calls to get something that works reasonably for + * relative paths. Emscripten doesn't support replacing virtual + * "/" with host "/" (the default MEMFS at "/" can't be unmounted) + * but we can mount "/tmp" as host "/tmp" to allow testcase runs. + * + * https://kripken.github.io/emscripten-site/docs/api_reference/Filesystem-API.html#filesystem-api-nodefs + * https://github.com/kripken/emscripten/blob/master/tests/fs/test_nodefs_rw.c + */ + EM_ASM( + /* At the moment it's not possible to replace the default MEMFS mounted at '/': + * https://github.com/kripken/emscripten/issues/2040 + * https://github.com/kripken/emscripten/blob/incoming/src/library_fs.js#L1341-L1358 + */ + /* + try { + FS.unmount("/"); + } catch (e) { + console.log("Failed to unmount default '/' MEMFS mount: " + e); + } + */ + try { + FS.mkdir("/working"); + FS.mount(NODEFS, { root: "." }, "/working"); + } catch (e) { + console.log("Failed to mount NODEFS /working: " + e); + } + /* A virtual '/tmp' exists by default: + * https://gist.github.com/evanw/e6be28094f34451bd5bd#file-temp-js-L3806-L3809 + */ + /* + try { + FS.mkdir("/tmp"); + } catch (e) { + console.log("Failed to create virtual /tmp: " + e); + } + */ + try { + FS.mount(NODEFS, { root: "/tmp" }, "/tmp"); + } catch (e) { + console.log("Failed to mount NODEFS /tmp: " + e); + } + ); +#endif /* EMSCRIPTEN */ + +#if defined(DUK_CMDLINE_AJSHEAP) + alloc_provider = ALLOC_AJSHEAP; +#endif + (void) ajsheap_log; + + /* + * Signal handling setup + */ + +#if defined(DUK_CMDLINE_SIGNAL) + set_sigint_handler(); + + /* This is useful at the global level; libraries should avoid SIGPIPE though */ + /*signal(SIGPIPE, SIG_IGN);*/ +#endif + + /* + * Parse options + */ + + for (i = 1; i < argc; i++) { + char *arg = argv[i]; + if (!arg) { + goto usage; + } + if (strcmp(arg, "--restrict-memory") == 0) { + memlimit_high = 0; + } else if (strcmp(arg, "-i") == 0) { + interactive = 1; + } else if (strcmp(arg, "-c") == 0) { + if (i == argc - 1) { + goto usage; + } + i++; + compile_filename = argv[i]; + } else if (strcmp(arg, "-e") == 0) { + have_eval = 1; + if (i == argc - 1) { + goto usage; + } + i++; /* skip code */ + } else if (strcmp(arg, "--alloc-default") == 0) { + alloc_provider = ALLOC_DEFAULT; + } else if (strcmp(arg, "--alloc-logging") == 0) { + alloc_provider = ALLOC_LOGGING; + } else if (strcmp(arg, "--alloc-torture") == 0) { + alloc_provider = ALLOC_TORTURE; + } else if (strcmp(arg, "--alloc-hybrid") == 0) { + alloc_provider = ALLOC_HYBRID; + } else if (strcmp(arg, "--alloc-ajsheap") == 0) { + alloc_provider = ALLOC_AJSHEAP; + } else if (strcmp(arg, "--ajsheap-log") == 0) { + ajsheap_log = 1; + } else if (strcmp(arg, "--debugger") == 0) { + debugger = 1; +#if defined(DUK_CMDLINE_DEBUGGER_SUPPORT) + } else if (strcmp(arg, "--reattach") == 0) { + debugger_reattach = 1; +#endif + } else if (strcmp(arg, "--recreate-heap") == 0) { + recreate_heap = 1; + } else if (strcmp(arg, "--no-heap-destroy") == 0) { + no_heap_destroy = 1; + } else if (strcmp(arg, "--verbose") == 0) { + verbose = 1; + } else if (strcmp(arg, "--run-stdin") == 0) { + run_stdin = 1; + } else if (strlen(arg) >= 1 && arg[0] == '-') { + goto usage; + } else { + have_files = 1; + } + } + if (!have_files && !have_eval && !run_stdin) { + interactive = 1; + } + + /* + * Memory limit + */ + +#if defined(DUK_CMDLINE_RLIMIT) + set_resource_limits(memlimit_high ? MEM_LIMIT_HIGH : MEM_LIMIT_NORMAL); +#else + if (memlimit_high == 0) { + fprintf(stderr, "Warning: option --restrict-memory ignored, no rlimit support\n"); + fflush(stderr); + } +#endif + + /* + * Create heap + */ + + ctx = create_duktape_heap(alloc_provider, debugger, ajsheap_log); + + /* + * Execute any argument file(s) + */ + + for (i = 1; i < argc; i++) { + char *arg = argv[i]; + if (!arg) { + continue; + } else if (strlen(arg) == 2 && strcmp(arg, "-e") == 0) { + /* Here we know the eval arg exists but check anyway */ + if (i == argc - 1) { + retval = 1; + goto cleanup; + } + if (handle_eval(ctx, argv[i + 1]) != 0) { + retval = 1; + goto cleanup; + } + i++; /* skip code */ + continue; + } else if (strlen(arg) == 2 && strcmp(arg, "-c") == 0) { + i++; /* skip filename */ + continue; + } else if (strlen(arg) >= 1 && arg[0] == '-') { + continue; + } + + if (verbose) { + fprintf(stderr, "*** Executing file: %s\n", arg); + fflush(stderr); + } + + if (handle_file(ctx, arg, compile_filename) != 0) { + retval = 1; + goto cleanup; + } + + if (recreate_heap) { + if (verbose) { + fprintf(stderr, "*** Recreating heap...\n"); + fflush(stderr); + } + + destroy_duktape_heap(ctx, alloc_provider); + ctx = create_duktape_heap(alloc_provider, debugger, ajsheap_log); + } + } + + if (run_stdin) { + /* Running stdin like a full file (reading all lines before + * compiling) is useful with emduk: + * cat test.js | ./emduk --run-stdin + */ + if (handle_fh(ctx, stdin, "stdin", compile_filename) != 0) { + retval = 1; + goto cleanup; + } + + if (recreate_heap) { + if (verbose) { + fprintf(stderr, "*** Recreating heap...\n"); + fflush(stderr); + } + + destroy_duktape_heap(ctx, alloc_provider); + ctx = create_duktape_heap(alloc_provider, debugger, ajsheap_log); + } + } + + /* + * Enter interactive mode if options indicate it + */ + + if (interactive) { + if (handle_interactive(ctx) != 0) { + retval = 1; + goto cleanup; + } + } + + /* + * Cleanup and exit + */ + + cleanup: + if (interactive) { + fprintf(stderr, "Cleaning up...\n"); + fflush(stderr); + } + + if (ctx && no_heap_destroy) { + duk_gc(ctx, 0); + } + if (ctx && !no_heap_destroy) { + destroy_duktape_heap(ctx, alloc_provider); + } + ctx = NULL; + + return retval; + + /* + * Usage + */ + + usage: + fprintf(stderr, "Usage: duk [options] []\n" + "\n" + " -i enter interactive mode after executing argument file(s) / eval code\n" + " -e CODE evaluate code\n" + " -c FILE compile into bytecode (use with only one file argument)\n" + " --run-stdin treat stdin like a file, i.e. compile full input (not line by line)\n" + " --verbose verbose messages to stderr\n" + " --restrict-memory use lower memory limit (used by test runner)\n" + " --alloc-default use Duktape default allocator\n" +#if defined(DUK_CMDLINE_ALLOC_LOGGING) + " --alloc-logging use logging allocator (writes to /tmp)\n" +#endif +#if defined(DUK_CMDLINE_ALLOC_TORTURE) + " --alloc-torture use torture allocator\n" +#endif +#if defined(DUK_CMDLINE_ALLOC_HYBRID) + " --alloc-hybrid use hybrid allocator\n" +#endif +#if defined(DUK_CMDLINE_AJSHEAP) + " --alloc-ajsheap use ajsheap allocator (enabled by default with 'ajduk')\n" + " --ajsheap-log write alloc log to /tmp/ajduk-alloc-log.txt\n" +#endif +#if defined(DUK_CMDLINE_DEBUGGER_SUPPORT) + " --debugger start example debugger\n" + " --reattach automatically reattach debugger on detach\n" +#endif + " --recreate-heap recreate heap after every file\n" + " --no-heap-destroy force GC, but don't destroy heap at end (leak testing)\n" + "\n" + "If is omitted, interactive mode is started automatically.\n"); + fflush(stderr); + exit(1); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/cmdline/duk_cmdline_ajduk.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/cmdline/duk_cmdline_ajduk.c new file mode 100644 index 000000000..ae28ede3f --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/cmdline/duk_cmdline_ajduk.c @@ -0,0 +1,1008 @@ +/* + * 'ajduk' specific functionality, examples for low memory techniques + */ + +#ifdef DUK_CMDLINE_AJSHEAP + +#include +#include +#include +#include +#include "ajs.h" +#include "ajs_heap.h" +#include "duktape.h" + +extern uint8_t dbgHEAPDUMP; + +#if defined(DUK_USE_ROM_OBJECTS) && defined(DUK_USE_HEAPPTR16) +/* Pointer compression with ROM strings/objects: + * + * For now, use DUK_USE_ROM_OBJECTS to signal the need for compressed ROM + * pointers. DUK_USE_ROM_PTRCOMP_FIRST is provided for the ROM pointer + * compression range minimum to avoid duplication in user code. + */ +#if 0 /* This extern declaration is provided by duktape.h, array provided by duktape.c. */ +extern const void * const duk_rom_compressed_pointers[]; +#endif +static const void *duk__romptr_low = NULL; +static const void *duk__romptr_high = NULL; +#define DUK__ROMPTR_COMPRESSION +#define DUK__ROMPTR_FIRST DUK_USE_ROM_PTRCOMP_FIRST +#endif + +/* + * Helpers + */ + +static void *ajduk__lose_const(const void *ptr) { + /* Somewhat portable way of losing a const without warnings. + * Another approach is to cast through intptr_t, but that + * type is not always available. + */ + union { + const void *p; + void *q; + } u; + u.p = ptr; + return u.q; +} + +static void safe_print_chars(const char *p, duk_size_t len, int until_nul) { + duk_size_t i; + + printf("\""); + for (i = 0; i < len; i++) { + unsigned char x = (unsigned char) p[i]; + if (until_nul && x == 0U) { + break; + } + if (x < 0x20 || x >= 0x7e || x == '"' || x == '\'' || x == '\\') { + printf("\\x%02x", (int) x); + } else { + printf("%c", (char) x); + } + } + printf("\""); +} + +/* + * Heap initialization when using AllJoyn.js pool allocator (without any + * other AllJoyn.js integration). This serves as an example of how to + * integrate Duktape with a pool allocator and is useful for low memory + * testing. + * + * The pool sizes are not optimized here. The sizes are chosen so that + * you can look at the high water mark (hwm) and use counts (use) and see + * how much allocations are needed for each pool size. To optimize pool + * sizes more accurately, you can use --alloc-logging and inspect the memory + * allocation log which provides exact byte counts etc. + * + * https://git.allseenalliance.org/cgit/core/alljoyn-js.git + * https://git.allseenalliance.org/cgit/core/alljoyn-js.git/tree/ajs.c + */ + +static const AJS_HeapConfig ajsheap_config[] = { + { 8, 10, AJS_POOL_BORROW, 0 }, + { 12, 600, AJS_POOL_BORROW, 0 }, + { 16, 300, AJS_POOL_BORROW, 0 }, + { 20, 300, AJS_POOL_BORROW, 0 }, + { 24, 300, AJS_POOL_BORROW, 0 }, + { 28, 150, AJS_POOL_BORROW, 0 }, + { 32, 150, AJS_POOL_BORROW, 0 }, + { 40, 150, AJS_POOL_BORROW, 0 }, + { 48, 50, AJS_POOL_BORROW, 0 }, + { 52, 50, AJS_POOL_BORROW, 0 }, + { 56, 50, AJS_POOL_BORROW, 0 }, + { 60, 50, AJS_POOL_BORROW, 0 }, + { 64, 50, AJS_POOL_BORROW, 0 }, + { 128, 80, AJS_POOL_BORROW, 0 }, + { 256, 16, AJS_POOL_BORROW, 0 }, + { 320, 1, AJS_POOL_BORROW, 0 }, + { 392, 1, AJS_POOL_BORROW, 0 }, /* duk_hthread, with heap ptr compression, ROM strings+objects */ + { 512, 16, AJS_POOL_BORROW, 0 }, + { 964, 1, AJS_POOL_BORROW, 0 }, /* duk_heap, with heap ptr compression, ROM strings+objects */ + { 1024, 6, AJS_POOL_BORROW, 0 }, + { 1344, 1, AJS_POOL_BORROW, 0 }, /* duk_heap, with heap ptr compression, RAM strings+objects */ + { 2048, 5, AJS_POOL_BORROW, 0 }, + { 4096, 3, 0, 0 }, + { 8192, 3, 0, 0 }, + { 16384, 1, 0, 0 }, + { 32768, 1, 0, 0 } +}; + +uint8_t *ajsheap_ram = NULL; + +void ajsheap_init(void) { + size_t heap_sz[1]; + uint8_t *heap_array[1]; + uint8_t num_pools, i; + AJ_Status ret; + + num_pools = (uint8_t) (sizeof(ajsheap_config) / sizeof(AJS_HeapConfig)); + heap_sz[0] = AJS_HeapRequired(ajsheap_config, /* heapConfig */ + num_pools, /* numPools */ + 0); /* heapNum */ + ajsheap_ram = (uint8_t *) malloc(heap_sz[0]); + if (ajsheap_ram == NULL) { + fprintf(stderr, "Failed to allocate AJS heap\n"); + fflush(stderr); + exit(1); + } + heap_array[0] = ajsheap_ram; + + fprintf(stderr, "Allocated AJS heap of %ld bytes, pools:", (long) heap_sz[0]); + for (i = 0; i < num_pools; i++) { + fprintf(stderr, " (sz:%ld,num:%ld,brw:%ld,idx:%ld)", + (long) ajsheap_config[i].size, (long) ajsheap_config[i].entries, + (long) ajsheap_config[i].borrow, (long) ajsheap_config[i].heapIndex); + } + fprintf(stderr, "\n"); + fflush(stderr); + + ret = AJS_HeapInit((void **) heap_array, /* heap */ + (size_t *) heap_sz, /* heapSz */ + ajsheap_config, /* heapConfig */ + num_pools, /* numPools */ + 1); /* numHeaps */ + fprintf(stderr, "AJS_HeapInit() -> %ld\n", (long) ret); + fflush(stderr); + + /* Enable heap dumps */ + dbgHEAPDUMP = 1; + +#if defined(DUK__ROMPTR_COMPRESSION) + /* Scan ROM pointer range for faster detection of "is 'p' a ROM pointer" + * later on. + */ + if (1) { + const void * const * ptrs = (const void * const *) duk_rom_compressed_pointers; + duk__romptr_low = duk__romptr_high = (const void *) *ptrs; + while (*ptrs) { + if (*ptrs > duk__romptr_high) { + duk__romptr_high = (const void *) *ptrs; + } + if (*ptrs < duk__romptr_low) { + duk__romptr_low = (const void *) *ptrs; + } + ptrs++; + } + fprintf(stderr, "romptrs: low=%p high=%p\n", + (const void *) duk__romptr_low, (const void *) duk__romptr_high); + fflush(stderr); + } +#endif +} + +void ajsheap_free(void) { + if (ajsheap_ram != NULL) { + free(ajsheap_ram); + ajsheap_ram = NULL; + } +} + +/* AjsHeap.dump(), allows Ecmascript code to dump heap status at suitable + * points. + */ +duk_ret_t ajsheap_dump_binding(duk_context *ctx) { + AJS_HeapDump(); + fflush(stdout); + return 0; +} + +void ajsheap_dump(void) { + AJS_HeapDump(); + fflush(stdout); +} + +void ajsheap_register(duk_context *ctx) { + duk_push_object(ctx); + duk_push_c_function(ctx, ajsheap_dump_binding, 0); + duk_put_prop_string(ctx, -2, "dump"); + duk_put_global_string(ctx, "AjsHeap"); +} + +/* + * Wrapped ajs_heap.c alloc functions + * + * Used to write an alloc log. + */ + +static FILE *ajsheap_alloc_log = NULL; + +static void ajsheap_write_alloc_log(const char *fmt, ...) { + va_list ap; + char buf[256]; + + va_start(ap, fmt); + vsnprintf(buf, sizeof(buf), fmt, ap); + buf[sizeof(buf) - 1] = (char) 0; + va_end(ap); + + if (ajsheap_alloc_log == NULL) { + ajsheap_alloc_log = fopen("/tmp/ajduk-alloc-log.txt", "wb"); + if (ajsheap_alloc_log == NULL) { + fprintf(stderr, "WARNING: failed to write alloc log, ignoring\n"); + fflush(stderr); + return; + } + } + + (void) fwrite((const void *) buf, 1, strlen(buf), ajsheap_alloc_log); + (void) fflush(ajsheap_alloc_log); +} + +void *ajsheap_alloc_wrapped(void *udata, duk_size_t size) { + void *ret = AJS_Alloc(udata, size); + if (size > 0 && ret == NULL) { + ajsheap_write_alloc_log("A FAIL %ld\n", (long) size); + } else if (ret == NULL) { + ajsheap_write_alloc_log("A NULL %ld\n", (long) size); + } else { + ajsheap_write_alloc_log("A %p %ld\n", ret, (long) size); + } + return ret; +} + +void *ajsheap_realloc_wrapped(void *udata, void *ptr, duk_size_t size) { + void *ret = AJS_Realloc(udata, ptr, size); + if (size > 0 && ret == NULL) { + if (ptr == NULL) { + ajsheap_write_alloc_log("R NULL -1 FAIL %ld\n", (long) size); + } else { + ajsheap_write_alloc_log("R %p -1 FAIL %ld\n", ptr, (long) size); + } + } else if (ret == NULL) { + if (ptr == NULL) { + ajsheap_write_alloc_log("R NULL -1 NULL %ld\n", (long) size); + } else { + ajsheap_write_alloc_log("R %p -1 NULL %ld\n", ptr, (long) size); + } + } else { + if (ptr == NULL) { + ajsheap_write_alloc_log("R NULL -1 %p %ld\n", ret, (long) size); + } else { + ajsheap_write_alloc_log("R %p -1 %p %ld\n", ptr, ret, (long) size); + } + } + return ret; +} + +void ajsheap_free_wrapped(void *udata, void *ptr) { + AJS_Free(udata, ptr); + if (ptr == NULL) { + } else { + ajsheap_write_alloc_log("F %p -1\n", ptr); + } +} + +/* + * Example pointer compression functions. + * + * 'base' is chosen so that no non-NULL pointer results in a zero result + * which is reserved for NULL pointers. + */ + +duk_uint16_t ajsheap_enc16(void *ud, void *p) { + duk_uint32_t ret; + char *base = (char *) ajsheap_ram - 4; + +#if defined(DUK__ROMPTR_COMPRESSION) + if (p >= duk__romptr_low && p <= duk__romptr_high) { + /* The if-condition should be the fastest possible check + * for "is 'p' in ROM?". If pointer is in ROM, we'd like + * to compress it quickly. Here we just scan a ~1K array + * which is very bad for performance and for illustration + * only. + */ + const void * const * ptrs = duk_rom_compressed_pointers; + while (*ptrs) { + if (*ptrs == p) { + ret = DUK__ROMPTR_FIRST + (ptrs - duk_rom_compressed_pointers); +#if 0 + fprintf(stderr, "ajsheap_enc16: rom pointer: %p -> 0x%04lx\n", (void *) p, (long) ret); + fflush(stderr); +#endif + return (duk_uint16_t) ret; + } + ptrs++; + } + + /* We should really never be here: Duktape should only be + * compressing pointers which are in the ROM compressed + * pointers list, which are known at 'make dist' time. + * We go on, causing a pointer compression error. + */ + fprintf(stderr, "ajsheap_enc16: rom pointer: %p could not be compressed, should never happen\n", (void *) p); + fflush(stderr); + } +#endif + + /* Userdata is not needed in this case but would be useful if heap + * pointer compression were used for multiple heaps. The userdata + * allows the callback to distinguish between heaps and their base + * pointers. + * + * If not needed, the userdata can be left out during compilation + * by simply ignoring the userdata argument of the pointer encode + * and decode macros. It is kept here so that any bugs in actually + * providing the value inside Duktape are revealed during compilation. + */ + (void) ud; +#if 1 + /* Ensure that we always get the heap_udata given in heap creation. + * (Useful for Duktape development, not needed for user programs.) + */ + if (ud != (void *) 0xdeadbeef) { + fprintf(stderr, "invalid udata for ajsheap_enc16: %p\n", ud); + fflush(stderr); + } +#endif + + if (p == NULL) { + ret = 0; + } else { + ret = (duk_uint32_t) (((char *) p - base) >> 2); + } +#if 0 + printf("ajsheap_enc16: %p -> %u\n", p, (unsigned int) ret); +#endif + if (ret > 0xffffUL) { + fprintf(stderr, "Failed to compress pointer: %p (ret was %ld)\n", (void *) p, (long) ret); + fflush(stderr); + abort(); + } +#if defined(DUK__ROMPTR_COMPRESSION) + if (ret >= DUK__ROMPTR_FIRST) { + fprintf(stderr, "Failed to compress pointer, in 16-bit range but matches romptr range: %p (ret was %ld)\n", (void *) p, (long) ret); + fflush(stderr); + abort(); + } +#endif + return (duk_uint16_t) ret; +} + +void *ajsheap_dec16(void *ud, duk_uint16_t x) { + void *ret; + char *base = (char *) ajsheap_ram - 4; + +#if defined(DUK__ROMPTR_COMPRESSION) + if (x >= DUK__ROMPTR_FIRST) { + /* This is a blind lookup, could check index validity. + * Duktape should never decompress a pointer which would + * be out-of-bounds here. + */ + ret = (void *) ajduk__lose_const(duk_rom_compressed_pointers[x - DUK__ROMPTR_FIRST]); +#if 0 + fprintf(stderr, "ajsheap_dec16: rom pointer: 0x%04lx -> %p\n", (long) x, ret); + fflush(stderr); +#endif + return ret; + } +#endif + + /* See userdata discussion in ajsheap_enc16(). */ + (void) ud; +#if 1 + /* Ensure that we always get the heap_udata given in heap creation. */ + if (ud != (void *) 0xdeadbeef) { + fprintf(stderr, "invalid udata for ajsheap_dec16: %p\n", ud); + fflush(stderr); + } +#endif + + if (x == 0) { + ret = NULL; + } else { + ret = (void *) (base + (((duk_uint32_t) x) << 2)); + } +#if 0 + printf("ajsheap_dec16: %u -> %p\n", (unsigned int) x, ret); +#endif + return ret; +} + +/* + * Simplified example of an external strings strategy where incoming strings + * are written sequentially into a fixed, memory mapped flash area. + * + * The example first scans if the string is already in the flash (which may + * happen if the same string is interned multiple times), then adds it to + * flash if there is space. + * + * This example is too slow to be used in a real world application: there + * should be e.g. a hash table to quickly check for strings that are already + * present in the string data (similarly to how string interning works in + * Duktape itself). + */ + +static uint8_t ajsheap_strdata[65536]; +static size_t ajsheap_strdata_used = 0; + +const void *ajsheap_extstr_check_1(const void *ptr, duk_size_t len) { + uint8_t *p, *p_end; + uint8_t initial; + uint8_t *ret; + size_t left; + + (void) safe_print_chars; /* potentially unused */ + + if (len <= 3) { + /* It's not worth it to make very small strings external, as + * they would take the same space anyway. Also avoids zero + * length degenerate case. + */ + return NULL; + } + + /* + * Check if we already have the string. Be careful to compare for + * NUL terminator too, it is NOT present in 'ptr'. This algorithm + * is too simplistic and way too slow for actual use. + */ + + initial = ((const uint8_t *) ptr)[0]; + for (p = ajsheap_strdata, p_end = p + ajsheap_strdata_used; p != p_end; p++) { + if (*p != initial) { + continue; + } + left = (size_t) (p_end - p); + if (left >= len + 1 && + memcmp(p, ptr, len) == 0 && + p[len] == 0) { + ret = p; +#if 0 + printf("ajsheap_extstr_check_1: ptr=%p, len=%ld ", + (void *) ptr, (long) len); + safe_print_chars((const char *) ptr, len, 0 /*until_nul*/); + printf(" -> existing %p (used=%ld)\n", + (void *) ret, (long) ajsheap_strdata_used); +#endif + return ret; + } + } + + /* + * Not present yet, check if we have space. Again, be careful to + * ensure there is space for a NUL following the input data. + */ + + if (ajsheap_strdata_used + len + 1 > sizeof(ajsheap_strdata)) { +#if 0 + printf("ajsheap_extstr_check_1: ptr=%p, len=%ld ", (void *) ptr, (long) len); + safe_print_chars((const char *) ptr, len, 0 /*until_nul*/); + printf(" -> no space (used=%ld)\n", (long) ajsheap_strdata_used); +#endif + return NULL; + } + + /* + * There is space, add the string to our collection, being careful + * to append the NUL. + */ + + ret = ajsheap_strdata + ajsheap_strdata_used; + memcpy(ret, ptr, len); + ret[len] = (uint8_t) 0; + ajsheap_strdata_used += len + 1; + +#if 0 + printf("ajsheap_extstr_check_1: ptr=%p, len=%ld -> ", (void *) ptr, (long) len); + safe_print_chars((const char *) ptr, len, 0 /*until_nul*/); + printf(" -> %p (used=%ld)\n", (void *) ret, (long) ajsheap_strdata_used); +#endif + return (const void *) ret; +} + +void ajsheap_extstr_free_1(const void *ptr) { + (void) ptr; +#if 0 + printf("ajsheap_extstr_free_1: freeing extstr %p -> ", ptr); + safe_print_chars((const char *) ptr, DUK_SIZE_MAX, 1 /*until_nul*/); + printf("\n"); +#endif +} + +/* + * Simplified example of an external strings strategy where a set of strings + * is gathered during application compile time and baked into the application + * binary. + * + * Duktape built-in strings are available from duk_build_meta.json, see + * util/duk_meta_to_strarray.py. There may also be a lot of application + * specific strings, e.g. those used by application specific APIs. These + * must be gathered through some other means, see e.g. util/scan_strings.py. + */ + +static const char *strdata_duk_builtin_strings[] = { + /* + * These strings are from util/duk_meta_to_strarray.py + */ + + "Logger", + "Thread", + "Pointer", + "Buffer", + "DecEnv", + "ObjEnv", + "", + "global", + "Arguments", + "JSON", + "Math", + "Error", + "RegExp", + "Date", + "Number", + "Boolean", + "String", + "Array", + "Function", + "Object", + "Null", + "Undefined", + "{_func:true}", + "{\x22" "_func\x22" ":true}", + "{\x22" "_ninf\x22" ":true}", + "{\x22" "_inf\x22" ":true}", + "{\x22" "_nan\x22" ":true}", + "{\x22" "_undef\x22" ":true}", + "toLogString", + "clog", + "l", + "n", + "fatal", + "error", + "warn", + "debug", + "trace", + "raw", + "fmt", + "current", + "resume", + "compact", + "jc", + "jx", + "base64", + "hex", + "dec", + "enc", + "fin", + "gc", + "act", + "info", + "version", + "env", + "modLoaded", + "modSearch", + "errThrow", + "errCreate", + "compile", + "\xff" "Regbase", + "\xff" "Thread", + "\xff" "Handler", + "\xff" "Finalizer", + "\xff" "Callee", + "\xff" "Map", + "\xff" "Args", + "\xff" "This", + "\xff" "Pc2line", + "\xff" "Source", + "\xff" "Varenv", + "\xff" "Lexenv", + "\xff" "Varmap", + "\xff" "Formals", + "\xff" "Bytecode", + "\xff" "Next", + "\xff" "Target", + "\xff" "Value", + "pointer", + "buffer", + "\xff" "Tracedata", + "lineNumber", + "fileName", + "pc", + "stack", + "ThrowTypeError", + "Duktape", + "id", + "require", + "__proto__", + "setPrototypeOf", + "ownKeys", + "enumerate", + "deleteProperty", + "has", + "Proxy", + "callee", + "Invalid Date", + "[...]", + "\x0a" "\x09", + " ", + ",", + "-0", + "+0", + "0", + "-Infinity", + "+Infinity", + "Infinity", + "object", + "string", + "number", + "boolean", + "undefined", + "stringify", + "tan", + "sqrt", + "sin", + "round", + "random", + "pow", + "min", + "max", + "log", + "floor", + "exp", + "cos", + "ceil", + "atan2", + "atan", + "asin", + "acos", + "abs", + "SQRT2", + "SQRT1_2", + "PI", + "LOG10E", + "LOG2E", + "LN2", + "LN10", + "E", + "message", + "name", + "input", + "index", + "(?:)", + "lastIndex", + "multiline", + "ignoreCase", + "source", + "test", + "exec", + "toGMTString", + "setYear", + "getYear", + "toJSON", + "toISOString", + "toUTCString", + "setUTCFullYear", + "setFullYear", + "setUTCMonth", + "setMonth", + "setUTCDate", + "setDate", + "setUTCHours", + "setHours", + "setUTCMinutes", + "setMinutes", + "setUTCSeconds", + "setSeconds", + "setUTCMilliseconds", + "setMilliseconds", + "setTime", + "getTimezoneOffset", + "getUTCMilliseconds", + "getMilliseconds", + "getUTCSeconds", + "getSeconds", + "getUTCMinutes", + "getMinutes", + "getUTCHours", + "getHours", + "getUTCDay", + "getDay", + "getUTCDate", + "getDate", + "getUTCMonth", + "getMonth", + "getUTCFullYear", + "getFullYear", + "getTime", + "toLocaleTimeString", + "toLocaleDateString", + "toTimeString", + "toDateString", + "now", + "UTC", + "parse", + "toPrecision", + "toExponential", + "toFixed", + "POSITIVE_INFINITY", + "NEGATIVE_INFINITY", + "NaN", + "MIN_VALUE", + "MAX_VALUE", + "substr", + "trim", + "toLocaleUpperCase", + "toUpperCase", + "toLocaleLowerCase", + "toLowerCase", + "substring", + "split", + "search", + "replace", + "match", + "localeCompare", + "charCodeAt", + "charAt", + "fromCharCode", + "reduceRight", + "reduce", + "filter", + "map", + "forEach", + "some", + "every", + "lastIndexOf", + "indexOf", + "unshift", + "splice", + "sort", + "slice", + "shift", + "reverse", + "push", + "pop", + "join", + "concat", + "isArray", + "arguments", + "caller", + "bind", + "call", + "apply", + "propertyIsEnumerable", + "isPrototypeOf", + "hasOwnProperty", + "valueOf", + "toLocaleString", + "toString", + "constructor", + "set", + "get", + "enumerable", + "configurable", + "writable", + "value", + "keys", + "isExtensible", + "isFrozen", + "isSealed", + "preventExtensions", + "freeze", + "seal", + "defineProperties", + "defineProperty", + "create", + "getOwnPropertyNames", + "getOwnPropertyDescriptor", + "getPrototypeOf", + "prototype", + "length", + "alert", + "print", + "unescape", + "escape", + "encodeURIComponent", + "encodeURI", + "decodeURIComponent", + "decodeURI", + "isFinite", + "isNaN", + "parseFloat", + "parseInt", + "eval", + "URIError", + "TypeError", + "SyntaxError", + "ReferenceError", + "RangeError", + "EvalError", + "break", + "case", + "catch", + "continue", + "debugger", + "default", + "delete", + "do", + "else", + "finally", + "for", + "function", + "if", + "in", + "instanceof", + "new", + "return", + "switch", + "this", + "throw", + "try", + "typeof", + "var", + "void", + "while", + "with", + "class", + "const", + "enum", + "export", + "extends", + "import", + "super", + "null", + "true", + "false", + "implements", + "interface", + "let", + "package", + "private", + "protected", + "public", + "static", + "yield", + + /* + * These strings are manually added, and would be gathered in some + * application specific manner. + */ + + "foo", + "bar", + "quux", + "enableFrob", + "disableFrob" + /* ... */ +}; + +const void *ajsheap_extstr_check_2(const void *ptr, duk_size_t len) { + int i, n; + + (void) safe_print_chars; /* potentially unused */ + + /* Linear scan. An actual implementation would need some acceleration + * structure, e.g. select a sublist based on first character. + * + * NOTE: input string (behind 'ptr' with 'len' bytes) DOES NOT have a + * trailing NUL character. Any strings returned from this function + * MUST have a trailing NUL character. + */ + + n = (int) (sizeof(strdata_duk_builtin_strings) / sizeof(const char *)); + for (i = 0; i < n; i++) { + const char *str; + + str = strdata_duk_builtin_strings[i]; + if (strlen(str) == len && memcmp(ptr, (const void *) str, len) == 0) { +#if 0 + printf("ajsheap_extstr_check_2: ptr=%p, len=%ld ", + (void *) ptr, (long) len); + safe_print_chars((const char *) ptr, len, 0 /*until_nul*/); + printf(" -> constant string index %ld\n", (long) i); +#endif + return (void *) ajduk__lose_const(strdata_duk_builtin_strings[i]); + } + } + +#if 0 + printf("ajsheap_extstr_check_2: ptr=%p, len=%ld ", + (void *) ptr, (long) len); + safe_print_chars((const char *) ptr, len, 0 /*until_nul*/); + printf(" -> not found\n"); +#endif + return NULL; +} + +void ajsheap_extstr_free_2(const void *ptr) { + (void) ptr; +#if 0 + printf("ajsheap_extstr_free_2: freeing extstr %p -> ", ptr); + safe_print_chars((const char *) ptr, DUK_SIZE_MAX, 1 /*until_nul*/); + printf("\n"); +#endif +} + +/* + * External strings strategy intended for valgrind testing: external strings + * are allocated using malloc()/free() so that valgrind can be used to ensure + * that strings are e.g. freed exactly once. + */ + +const void *ajsheap_extstr_check_3(const void *ptr, duk_size_t len) { + duk_uint8_t *ret; + + (void) safe_print_chars; /* potentially unused */ + + ret = malloc((size_t) len + 1); + if (ret == NULL) { +#if 0 + printf("ajsheap_extstr_check_3: ptr=%p, len=%ld ", + (void *) ptr, (long) len); + safe_print_chars((const char *) ptr, len, 0 /*until_nul*/); + printf(" -> malloc failed, return NULL\n"); +#endif + return (const void *) NULL; + } + + if (len > 0) { + memcpy((void *) ret, ptr, (size_t) len); + } + ret[len] = (duk_uint8_t) 0; + +#if 0 + printf("ajsheap_extstr_check_3: ptr=%p, len=%ld ", + (void *) ptr, (long) len); + safe_print_chars((const char *) ptr, len, 0 /*until_nul*/); + printf(" -> %p\n", (void *) ret); +#endif + return (const void *) ret; +} + +void ajsheap_extstr_free_3(const void *ptr) { + (void) ptr; +#if 0 + printf("ajsheap_extstr_free_3: freeing extstr %p -> ", ptr); + safe_print_chars((const char *) ptr, DUK_SIZE_MAX, 1 /*until_nul*/); + printf("\n"); +#endif + free((void *) ajduk__lose_const(ptr)); +} + +/* + * Execution timeout example + */ + +#define AJSHEAP_EXEC_TIMEOUT 5 /* seconds */ + +static time_t curr_pcall_start = 0; +static long exec_timeout_check_counter = 0; + +void ajsheap_start_exec_timeout(void) { + curr_pcall_start = time(NULL); +} + +void ajsheap_clear_exec_timeout(void) { + curr_pcall_start = 0; +} + +duk_bool_t ajsheap_exec_timeout_check(void *udata) { + time_t now = time(NULL); + time_t diff = now - curr_pcall_start; + + (void) udata; /* not needed */ + + exec_timeout_check_counter++; +#if 0 + printf("exec timeout check %ld: now=%ld, start=%ld, diff=%ld\n", + (long) exec_timeout_check_counter, (long) now, (long) curr_pcall_start, (long) diff); + fflush(stdout); +#endif + + if (curr_pcall_start == 0) { + /* protected call not yet running */ + return 0; + } + if (diff > AJSHEAP_EXEC_TIMEOUT) { + return 1; + } + return 0; +} + +#else /* DUK_CMDLINE_AJSHEAP */ + +int ajs_dummy = 0; /* to avoid empty source file */ + +#endif /* DUK_CMDLINE_AJSHEAP */ diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/README.rst new file mode 100644 index 000000000..98b53d260 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/README.rst @@ -0,0 +1,8 @@ +Codepage conversion example +=========================== + +Example of how to convert an 8-bit input string (e.g. ISO-8859-1 or Windows +codepage 1252) into CESU-8 without using an external library like iconv. + +This is useful e.g. when compiling non-UTF-8 source code which cannot be +converted to UTF-8 (CESU-8) at build time. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/duk_codepage_conv.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/duk_codepage_conv.c new file mode 100644 index 000000000..932e9a606 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/duk_codepage_conv.c @@ -0,0 +1,54 @@ +/* + * Convert an 8-bit input string (e.g. ISO-8859-1) into CESU-8. + * Calling code supplies the "code page" as a 256-entry array of + * codepoints for the conversion. + * + * This is useful when input data is in non-UTF-8 format and must + * be converted at runtime, e.g. when compiling non-UTF-8 source + * code. Another alternative is to use e.g. iconv. + */ + +#include "duktape.h" + +/* Decode an 8-bit string using 'codepage' into Unicode codepoints and + * re-encode into CESU-8. Codepage argument must point to a 256-entry + * table. Only supports BMP (codepoints U+0000 to U+FFFF). + */ +void duk_decode_string_codepage(duk_context *ctx, const char *str, size_t len, unsigned int *codepage) { + unsigned char *tmp; + size_t tmplen, i; + unsigned char *p; + unsigned int cp; + + tmplen = 3 * len; /* max expansion is 1 input byte -> 3 output bytes */ + if (tmplen / 3 != len) { + /* Temporary buffer length wraps. */ + duk_error(ctx, DUK_ERR_RANGE_ERROR, "input string too long"); + return; + } + + tmp = (unsigned char *) duk_push_fixed_buffer(ctx, tmplen); + + for (i = 0, p = tmp; i < len; i++) { + cp = codepage[((unsigned char *) str)[i]] & 0xffffUL; + if (cp < 0x80UL) { + *p++ = (unsigned char) cp; + } else if (cp < 0x800UL) { + *p++ = (unsigned char) (0xc0 + ((cp >> 6) & 0x1f)); + *p++ = (unsigned char) (0x80 + (cp & 0x3f)); + } else { + /* In CESU-8 all codepoints in [0x0000,0xFFFF] are + * allowed, including surrogates. + */ + *p++ = (unsigned char) (0xe0 + ((cp >> 12) & 0x0f)); + *p++ = (unsigned char) (0x80 + ((cp >> 6) & 0x3f)); + *p++ = (unsigned char) (0x80 + (cp & 0x3f)); + } + } + + duk_push_lstring(ctx, (const char *) tmp, (duk_size_t) (p - tmp)); + + /* [ ... tmp res ] */ + + duk_remove(ctx, -2); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/duk_codepage_conv.h b/src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/duk_codepage_conv.h new file mode 100644 index 000000000..d2705a0db --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/duk_codepage_conv.h @@ -0,0 +1,8 @@ +#ifndef DUK_CODEPAGE_CONV_H_INCLUDED +#define DUK_CODEPAGE_CONV_H_INCLUDED + +#include "duktape.h" + +void duk_decode_string_codepage(duk_context *ctx, const char *str, size_t len, unsigned int *codepage); + +#endif /* DUK_CODEPAGE_CONV_H_INCLUDED */ diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/test.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/test.c new file mode 100644 index 000000000..c34299a82 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/codepage-conv/test.c @@ -0,0 +1,286 @@ +#include "duktape.h" +#include "duk_codepage_conv.h" + +/* http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT */ +unsigned int cp1252[256] = { + (unsigned int) 0x0000, + (unsigned int) 0x0001, + (unsigned int) 0x0002, + (unsigned int) 0x0003, + (unsigned int) 0x0004, + (unsigned int) 0x0005, + (unsigned int) 0x0006, + (unsigned int) 0x0007, + (unsigned int) 0x0008, + (unsigned int) 0x0009, + (unsigned int) 0x000A, + (unsigned int) 0x000B, + (unsigned int) 0x000C, + (unsigned int) 0x000D, + (unsigned int) 0x000E, + (unsigned int) 0x000F, + (unsigned int) 0x0010, + (unsigned int) 0x0011, + (unsigned int) 0x0012, + (unsigned int) 0x0013, + (unsigned int) 0x0014, + (unsigned int) 0x0015, + (unsigned int) 0x0016, + (unsigned int) 0x0017, + (unsigned int) 0x0018, + (unsigned int) 0x0019, + (unsigned int) 0x001A, + (unsigned int) 0x001B, + (unsigned int) 0x001C, + (unsigned int) 0x001D, + (unsigned int) 0x001E, + (unsigned int) 0x001F, + (unsigned int) 0x0020, + (unsigned int) 0x0021, + (unsigned int) 0x0022, + (unsigned int) 0x0023, + (unsigned int) 0x0024, + (unsigned int) 0x0025, + (unsigned int) 0x0026, + (unsigned int) 0x0027, + (unsigned int) 0x0028, + (unsigned int) 0x0029, + (unsigned int) 0x002A, + (unsigned int) 0x002B, + (unsigned int) 0x002C, + (unsigned int) 0x002D, + (unsigned int) 0x002E, + (unsigned int) 0x002F, + (unsigned int) 0x0030, + (unsigned int) 0x0031, + (unsigned int) 0x0032, + (unsigned int) 0x0033, + (unsigned int) 0x0034, + (unsigned int) 0x0035, + (unsigned int) 0x0036, + (unsigned int) 0x0037, + (unsigned int) 0x0038, + (unsigned int) 0x0039, + (unsigned int) 0x003A, + (unsigned int) 0x003B, + (unsigned int) 0x003C, + (unsigned int) 0x003D, + (unsigned int) 0x003E, + (unsigned int) 0x003F, + (unsigned int) 0x0040, + (unsigned int) 0x0041, + (unsigned int) 0x0042, + (unsigned int) 0x0043, + (unsigned int) 0x0044, + (unsigned int) 0x0045, + (unsigned int) 0x0046, + (unsigned int) 0x0047, + (unsigned int) 0x0048, + (unsigned int) 0x0049, + (unsigned int) 0x004A, + (unsigned int) 0x004B, + (unsigned int) 0x004C, + (unsigned int) 0x004D, + (unsigned int) 0x004E, + (unsigned int) 0x004F, + (unsigned int) 0x0050, + (unsigned int) 0x0051, + (unsigned int) 0x0052, + (unsigned int) 0x0053, + (unsigned int) 0x0054, + (unsigned int) 0x0055, + (unsigned int) 0x0056, + (unsigned int) 0x0057, + (unsigned int) 0x0058, + (unsigned int) 0x0059, + (unsigned int) 0x005A, + (unsigned int) 0x005B, + (unsigned int) 0x005C, + (unsigned int) 0x005D, + (unsigned int) 0x005E, + (unsigned int) 0x005F, + (unsigned int) 0x0060, + (unsigned int) 0x0061, + (unsigned int) 0x0062, + (unsigned int) 0x0063, + (unsigned int) 0x0064, + (unsigned int) 0x0065, + (unsigned int) 0x0066, + (unsigned int) 0x0067, + (unsigned int) 0x0068, + (unsigned int) 0x0069, + (unsigned int) 0x006A, + (unsigned int) 0x006B, + (unsigned int) 0x006C, + (unsigned int) 0x006D, + (unsigned int) 0x006E, + (unsigned int) 0x006F, + (unsigned int) 0x0070, + (unsigned int) 0x0071, + (unsigned int) 0x0072, + (unsigned int) 0x0073, + (unsigned int) 0x0074, + (unsigned int) 0x0075, + (unsigned int) 0x0076, + (unsigned int) 0x0077, + (unsigned int) 0x0078, + (unsigned int) 0x0079, + (unsigned int) 0x007A, + (unsigned int) 0x007B, + (unsigned int) 0x007C, + (unsigned int) 0x007D, + (unsigned int) 0x007E, + (unsigned int) 0x007F, + (unsigned int) 0x20AC, + (unsigned int) 0xFFFD, /* undefined */ + (unsigned int) 0x201A, + (unsigned int) 0x0192, + (unsigned int) 0x201E, + (unsigned int) 0x2026, + (unsigned int) 0x2020, + (unsigned int) 0x2021, + (unsigned int) 0x02C6, + (unsigned int) 0x2030, + (unsigned int) 0x0160, + (unsigned int) 0x2039, + (unsigned int) 0x0152, + (unsigned int) 0xFFFD, /* undefined */ + (unsigned int) 0x017D, + (unsigned int) 0xFFFD, /* undefined */ + (unsigned int) 0xFFFD, /* undefined */ + (unsigned int) 0x2018, + (unsigned int) 0x2019, + (unsigned int) 0x201C, + (unsigned int) 0x201D, + (unsigned int) 0x2022, + (unsigned int) 0x2013, + (unsigned int) 0x2014, + (unsigned int) 0x02DC, + (unsigned int) 0x2122, + (unsigned int) 0x0161, + (unsigned int) 0x203A, + (unsigned int) 0x0153, + (unsigned int) 0xFFFD, /* undefined */ + (unsigned int) 0x017E, + (unsigned int) 0x0178, + (unsigned int) 0x00A0, + (unsigned int) 0x00A1, + (unsigned int) 0x00A2, + (unsigned int) 0x00A3, + (unsigned int) 0x00A4, + (unsigned int) 0x00A5, + (unsigned int) 0x00A6, + (unsigned int) 0x00A7, + (unsigned int) 0x00A8, + (unsigned int) 0x00A9, + (unsigned int) 0x00AA, + (unsigned int) 0x00AB, + (unsigned int) 0x00AC, + (unsigned int) 0x00AD, + (unsigned int) 0x00AE, + (unsigned int) 0x00AF, + (unsigned int) 0x00B0, + (unsigned int) 0x00B1, + (unsigned int) 0x00B2, + (unsigned int) 0x00B3, + (unsigned int) 0x00B4, + (unsigned int) 0x00B5, + (unsigned int) 0x00B6, + (unsigned int) 0x00B7, + (unsigned int) 0x00B8, + (unsigned int) 0x00B9, + (unsigned int) 0x00BA, + (unsigned int) 0x00BB, + (unsigned int) 0x00BC, + (unsigned int) 0x00BD, + (unsigned int) 0x00BE, + (unsigned int) 0x00BF, + (unsigned int) 0x00C0, + (unsigned int) 0x00C1, + (unsigned int) 0x00C2, + (unsigned int) 0x00C3, + (unsigned int) 0x00C4, + (unsigned int) 0x00C5, + (unsigned int) 0x00C6, + (unsigned int) 0x00C7, + (unsigned int) 0x00C8, + (unsigned int) 0x00C9, + (unsigned int) 0x00CA, + (unsigned int) 0x00CB, + (unsigned int) 0x00CC, + (unsigned int) 0x00CD, + (unsigned int) 0x00CE, + (unsigned int) 0x00CF, + (unsigned int) 0x00D0, + (unsigned int) 0x00D1, + (unsigned int) 0x00D2, + (unsigned int) 0x00D3, + (unsigned int) 0x00D4, + (unsigned int) 0x00D5, + (unsigned int) 0x00D6, + (unsigned int) 0x00D7, + (unsigned int) 0x00D8, + (unsigned int) 0x00D9, + (unsigned int) 0x00DA, + (unsigned int) 0x00DB, + (unsigned int) 0x00DC, + (unsigned int) 0x00DD, + (unsigned int) 0x00DE, + (unsigned int) 0x00DF, + (unsigned int) 0x00E0, + (unsigned int) 0x00E1, + (unsigned int) 0x00E2, + (unsigned int) 0x00E3, + (unsigned int) 0x00E4, + (unsigned int) 0x00E5, + (unsigned int) 0x00E6, + (unsigned int) 0x00E7, + (unsigned int) 0x00E8, + (unsigned int) 0x00E9, + (unsigned int) 0x00EA, + (unsigned int) 0x00EB, + (unsigned int) 0x00EC, + (unsigned int) 0x00ED, + (unsigned int) 0x00EE, + (unsigned int) 0x00EF, + (unsigned int) 0x00F0, + (unsigned int) 0x00F1, + (unsigned int) 0x00F2, + (unsigned int) 0x00F3, + (unsigned int) 0x00F4, + (unsigned int) 0x00F5, + (unsigned int) 0x00F6, + (unsigned int) 0x00F7, + (unsigned int) 0x00F8, + (unsigned int) 0x00F9, + (unsigned int) 0x00FA, + (unsigned int) 0x00FB, + (unsigned int) 0x00FC, + (unsigned int) 0x00FD, + (unsigned int) 0x00FE, + (unsigned int) 0x00FF +}; + +/* Exercise all 3 byte lengths: any ASCII character is 1 byte, 0xFC maps to + * U+00FC which is 2 bytes, and 0x80 maps to U+20AC which is 3 bytes. + */ +static const char *example_source = "print('Hello w\xfcrld - \x80');"; + +/* Example: compile and run test source encoded in Windows codepage 1252. */ +int main(int argc, char *argv[]) { + duk_context *ctx; + + (void) argc; (void) argv; + + ctx = duk_create_heap_default(); + if (!ctx) { + printf("Failed to create Duktape heap.\n"); + return 1; + } + + duk_decode_string_codepage(ctx, example_source, strlen(example_source), cp1252); + duk_eval_noresult(ctx); + + duk_destroy_heap(ctx); + return 0; +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/README.rst new file mode 100644 index 000000000..f14752263 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/README.rst @@ -0,0 +1,10 @@ +===================== +Coffeescript examples +===================== + +A few tests to see how CoffeeScript works with Duktape. Just convert the +Coffeescript files to Javascript with the ``Makefile.coffee`` in the +distributable, or manually:: + + $ coffee -c hello.coffee + $ cat hello.js diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/globals.coffee b/src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/globals.coffee new file mode 100644 index 000000000..25773cd8c --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/globals.coffee @@ -0,0 +1,7 @@ + +print '*** All globals' +print(name) for name in Object.getOwnPropertyNames(this) + +print '*** Globals with a short name (<= 8 chars)' +print(name) for name in Object.getOwnPropertyNames(this) when name.length <= 8 + diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/hello.coffee b/src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/hello.coffee new file mode 100644 index 000000000..088ed8d7a --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/hello.coffee @@ -0,0 +1,2 @@ +print 'Hello world!' +print 'version: ' + Duktape.version diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/mandel.coffee b/src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/mandel.coffee new file mode 100644 index 000000000..8e3e170fe --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/coffee/mandel.coffee @@ -0,0 +1,28 @@ +mandel = (x0, y0, x1, y1, w, h, maxiter) -> + [dx, dy] = [(x1 - x0) / w, (y1 - y0) / h] + res = [] + + y = y0 + for yc in [0..h-1] + x = x0 + for xc in [0..w-1] + [xx, yy] = [x, y] + c = '*' + for i in [0..maxiter-1] + # (xx+i*yy)^2 + (x+i*y) = xx^2 + i*2*xx*yy - yy^2 + x + i*y + # = (xx^2 - yy^2 + x) + i*(2*xx*yy + y) + [xx2, yy2] = [xx*xx, yy*yy] + if xx2 + yy2 >= 4.0 + c = '.' + break + [xx, yy] = [xx2 - yy2 + x, 2*xx*yy + y] + res.push(c) + x += dx + res.push('\n') + y += dy + + print(res.join('')) + return + +mandel(-2, 2, 2, -2, 200, 100, 1000) + diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/cpp-exceptions/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/cpp-exceptions/README.rst new file mode 100644 index 000000000..bbc26a037 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/cpp-exceptions/README.rst @@ -0,0 +1,29 @@ +========================================= +C++ exceptions for long control transfers +========================================= + +Normally Duktape uses ``setjmp()`` / ``longjmp()`` or their variants for +internal long control transfers. One downside of these functions is that +C++ automatic destructors (scope-based resource management, SBRM, a special +case of RAII) in Duktape/C functions won't be executed which is awkward for +C++ programmers. + +When ``DUK_USE_CPP_EXCEPTIONS`` (``DUK_OPT_CPP_EXCEPTIONS``) is defined, and +both Duktape and application code is compiled using a C++ compiler, Duktape +uses C++ ``try-catch`` and ``throw`` for internal long control transfers. +This allows automatic destructors to run as expected. The config option is +not enabled by default because C++ exceptions are sometimes disabled even +when a C++ compiler is used (e.g. for performance reasons). + +The ``cpp_exceptions.cpp`` example illustrates how C++ exceptions can be +used in Duktape/C functions at the moment: + +* Duktape uses C++ try/catch/throw internally; this is not visible to user + code directly. + +* Automatic destructors (scope-based resource management) work as expected. + +* C++ exceptions can be used in Duktape/C functions normally, but user + exceptions must be caught before they reach Duktape. If this is not + done, such exceptions are caught by Duktape and converted to API errors + (in other words, they won't propagate "through" Duktape at the moment). diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/cpp-exceptions/cpp_exceptions.cpp b/src/civetweb/src/third_party/duktape-1.5.2/examples/cpp-exceptions/cpp_exceptions.cpp new file mode 100644 index 000000000..6fb3194da --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/cpp-exceptions/cpp_exceptions.cpp @@ -0,0 +1,274 @@ +/* + * Example of how to use DUK_USE_CPP_EXCEPTIONS to support automatic + * variables (e.g. destructor calls) in Duktape/C functions. + * + * Compile with -DDUK_OPT_CPP_EXCEPTIONS: + * + * $ g++ -otest -DDUK_OPT_CPP_EXCEPTIONS -I/src/ \ + * /src/duktape.c cpp_exceptions.cpp -lm + * + * or ensure duk_config.h has DUK_USE_CPP_EXCEPTIONS enabled using + * genconfig. When executed you should see something like: + * + * $ ./test + * my_class instance created + * my_class instance destroyed <== destructor gets called + * --> rc=1 (SyntaxError: parse error (line 1)) + * [...] + * + * Duktape uses a custom exception class (duk_internal_exception) which + * doesn't inherit from any base class, so that catching any base classes + * in user code won't accidentally catch exceptions thrown by Duktape. + */ + +#if !defined(__cplusplus) +#error compile using a c++ compiler +#endif + +#include +#include +#include "duktape.h" + +#if defined(__cplusplus) && (__cplusplus >= 201103L) +#define NOEXCEPT noexcept +#else +#define NOEXCEPT throw() +#endif + +/* + * Example class with a destructor + */ + +class my_class { + public: + my_class(); + ~my_class(); +}; + +my_class::my_class() { + printf("my_class instance created\n"); +} + +my_class::~my_class() { + printf("my_class instance destroyed\n"); +} + +/* + * SyntaxError caused by eval exits Duktape/C function but destructors + * are executed. + */ + +duk_ret_t test1(duk_context *ctx) { + my_class myclass; + + duk_eval_string(ctx, "aiee="); + + return 0; +} + +/* + * You can use C++ exceptions inside Duktape/C functions for your own + * purposes but you should catch them before they propagate to Duktape. + */ + +duk_ret_t test2(duk_context *ctx) { + my_class myclass; + + try { + throw 123; + } catch (int myvalue) { + printf("Caught: %d\n", myvalue); + } + + return 0; +} + +/* + * If you let your own C++ exceptions propagate out of a Duktape/C function + * it will be caught by Duktape and considered a programming error. Duktape + * will catch the exception and convert it to a Duktape error. + * + * This may be allowed in a later version once all the implications have been + * worked out. + */ + +duk_ret_t test3(duk_context *ctx) { + my_class myclass; + + throw 123; /* ERROR: exception propagated to Duktape */ + + return 0; +} + +/* + * Same as above, but if the exception inherits from std::exception, it's + * "what()" will be included in the error message. + */ + +class my_exception : public std::exception { + virtual const char *what() const NOEXCEPT { + return "my_exception"; + } +}; + +duk_ret_t test4(duk_context *ctx) { + my_class myclass; + my_exception myexc; + + throw myexc; /* ERROR: exception propagated to Duktape */ + + return 0; +} + +/* + * Same as above, but if the exception inherits from std::exception with + * a NULL what(). Duktape will describe the error as 'unknown' if so. + */ + +class my_exception2 : public std::exception { + virtual const char *what() const NOEXCEPT { + return NULL; + } +}; + +duk_ret_t test5(duk_context *ctx) { + my_class myclass; + my_exception2 myexc; + + throw myexc; /* ERROR: exception propagated to Duktape */ + + return 0; +} + +int main(int argc, char *argv[]) { + duk_context *ctx = duk_create_heap_default(); + duk_int_t rc; + + (void) argc; (void) argv; /* suppress warning */ + + printf("*** test1 - duk_pcall()\n"); + duk_push_c_function(ctx, test1, 0); + rc = duk_pcall(ctx, 0); + printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); + duk_pop(ctx); + printf("\n"); + + printf("*** test1 - duk_safe_call()\n"); + rc = duk_safe_call(ctx, test1, 0, 1); + printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); + duk_pop(ctx); + printf("\n"); + + printf("*** test1 - ecmascript try-catch\n"); + duk_push_c_function(ctx, test1, 0); + duk_put_global_string(ctx, "test1"); + duk_eval_string_noresult(ctx, + "try {\n" + " test1();\n" + "} catch (e) {\n" + " print(e.stack || e);\n" + "}\n"); + printf("\n"); + + printf("*** test2 - duk_pcall()\n"); + duk_push_c_function(ctx, test2, 0); + rc = duk_pcall(ctx, 0); + printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); + duk_pop(ctx); + printf("\n"); + + printf("*** test2 - duk_safe_call()\n"); + rc = duk_safe_call(ctx, test2, 0, 1); + printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); + duk_pop(ctx); + printf("\n"); + + printf("*** test2 - ecmascript try-catch\n"); + duk_push_c_function(ctx, test2, 0); + duk_put_global_string(ctx, "test2"); + duk_eval_string_noresult(ctx, + "try {\n" + " test2();\n" + "} catch (e) {\n" + " print(e.stack || e);\n" + "}\n"); + printf("\n"); + + printf("*** test3 - duk_pcall()\n"); + duk_push_c_function(ctx, test3, 0); + rc = duk_pcall(ctx, 0); + printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); + duk_pop(ctx); + printf("\n"); + + printf("*** test3 - duk_safe_call()\n"); + rc = duk_safe_call(ctx, test3, 0, 1); + printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); + duk_pop(ctx); + printf("\n"); + + printf("*** test3 - ecmascript try-catch\n"); + duk_push_c_function(ctx, test3, 0); + duk_put_global_string(ctx, "test3"); + duk_eval_string_noresult(ctx, + "try {\n" + " test3();\n" + "} catch (e) {\n" + " print(e.stack || e);\n" + "}\n"); + printf("\n"); + + printf("*** test4 - duk_pcall()\n"); + duk_push_c_function(ctx, test4, 0); + rc = duk_pcall(ctx, 0); + printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); + duk_pop(ctx); + printf("\n"); + + printf("*** test4 - duk_safe_call()\n"); + rc = duk_safe_call(ctx, test4, 0, 1); + printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); + duk_pop(ctx); + printf("\n"); + + printf("*** test4 - ecmascript try-catch\n"); + duk_push_c_function(ctx, test4, 0); + duk_put_global_string(ctx, "test4"); + duk_eval_string_noresult(ctx, + "try {\n" + " test4();\n" + "} catch (e) {\n" + " print(e.stack || e);\n" + "}\n"); + printf("\n"); + + printf("*** test5 - duk_pcall()\n"); + duk_push_c_function(ctx, test5, 0); + rc = duk_pcall(ctx, 0); + printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); + duk_pop(ctx); + printf("\n"); + + printf("*** test5 - duk_safe_call()\n"); + rc = duk_safe_call(ctx, test5, 0, 1); + printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); + duk_pop(ctx); + printf("\n"); + + printf("*** test5 - ecmascript try-catch\n"); + duk_push_c_function(ctx, test5, 0); + duk_put_global_string(ctx, "test5"); + duk_eval_string_noresult(ctx, + "try {\n" + " test5();\n" + "} catch (e) {\n" + " print(e.stack || e);\n" + "}\n"); + printf("\n"); + + printf("*** done\n"); + + duk_destroy_heap(ctx); + + return 0; +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/Makefile b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/Makefile new file mode 100644 index 000000000..9d9d0b588 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/Makefile @@ -0,0 +1,17 @@ +# Set DUKTAPE_SRC to 'src' dir of Duktape distributable. +# The default is for the dist environment. +DUKTAPE_SRC=../../src +DUKTAPE_OPTS= +DUKTAPE_OPTS+=-DDUK_OPT_ASSERTIONS +DUKTAPE_OPTS+=-DDUK_OPT_DEBUGGER_SUPPORT -DDUK_OPT_INTERRUPT_COUNTER +DUKTAPE_OPTS+=-DDUK_OPT_DEBUGGER_FWD_PRINTALERT +DUKTAPE_OPTS+=-DDUK_OPT_DEBUGGER_DUMPHEAP +#DUKTAPE_OPTS+=-DDUK_OPT_DEBUGGER_TRANSPORT_TORTURE +TRANS_OPTS= +#TRANS_OPTS+=-DDEBUG_PRINTS + +test: test.c duk_trans_dvalue.c duk_trans_dvalue.h + echo $(DUKTAPE_SRC) + gcc -O0 -g -ggdb -Wall -Wextra -std=c99 -o test -I$(DUKTAPE_SRC) -I. \ + $(DUKTAPE_OPTS) $(TRANS_OPTS) \ + $(DUKTAPE_SRC)/duktape.c duk_trans_dvalue.c test.c -lm diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/README.rst new file mode 100644 index 000000000..86b2bb595 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/README.rst @@ -0,0 +1,8 @@ +=========================================================== +Debug transport with local debug protocol encoding/decoding +=========================================================== + +This example implements a debug transport which decodes/encodes the Duktape +debug protocol locally into a more easy to use C interface, which is useful +for debug clients implemented locally on the target. The example also +demonstrates how to trial parse dvalues in C. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/duk_trans_dvalue.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/duk_trans_dvalue.c new file mode 100644 index 000000000..8470a5f49 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/duk_trans_dvalue.c @@ -0,0 +1,1239 @@ +/* + * Example debug transport with a local debug message encoder/decoder. + * + * Provides a "received dvalue" callback for a fully parsed dvalue (user + * code frees dvalue) and a "cooperate" callback for e.g. UI integration. + * There are a few other callbacks. See test.c for usage examples. + * + * This transport implementation is not multithreaded which means that: + * + * - Callbacks to "received dvalue" callback come from the Duktape thread, + * either during normal execution or from duk_debugger_cooperate(). + * + * - Calls into duk_trans_dvalue_send() must be made from the callbacks + * provided (e.g. "received dvalue" or "cooperate") which use the active + * Duktape thread. + * + * - The only exception to this is when Duktape is idle: you can then call + * duk_trans_dvalue_send() from any thread (only one thread at a time). + * When you next call into Duktape or call duk_debugger_cooperate(), the + * queued data will be read and processed by Duktape. + * + * There are functions for creating and freeing values; internally they use + * malloc() and free() for memory management. Duktape heap alloc functions + * are not used to minimize disturbances to the Duktape heap under debugging. + * + * Doesn't depend on C99 types; assumes "int" is at least 32 bits, and makes + * a few assumptions about format specifiers. + */ + +#include +#include +#include + +#include "duktape.h" +#include "duk_trans_dvalue.h" + +/* Define to enable debug prints to stderr. */ +#if 0 +#define DEBUG_PRINTS +#endif + +/* Define to enable error prints to stderr. */ +#if 1 +#define ERROR_PRINTS +#endif + +/* + * Dvalue handling + */ + +duk_dvalue *duk_dvalue_alloc(void) { + duk_dvalue *dv = (duk_dvalue *) malloc(sizeof(duk_dvalue)); + if (dv) { + memset((void *) dv, 0, sizeof(duk_dvalue)); + dv->buf = NULL; + } + return dv; +} + +void duk_dvalue_free(duk_dvalue *dv) { + if (dv) { + free(dv->buf); /* tolerates NULL */ + dv->buf = NULL; + free(dv); + } +} + +static void duk__dvalue_bufesc(duk_dvalue *dv, char *buf, size_t maxbytes, int stresc) { + size_t i, limit; + + *buf = (char) 0; + limit = dv->len > maxbytes ? maxbytes : dv->len; + for (i = 0; i < limit; i++) { + unsigned char c = dv->buf[i]; + if (stresc) { + if (c >= 0x20 && c <= 0x7e && c != (char) '"' && c != (char) '\'') { + sprintf(buf, "%c", c); + buf++; + } else { + sprintf(buf, "\\x%02x", (unsigned int) c); + buf += 4; + } + } else { + sprintf(buf, "%02x", (unsigned int) c); + buf += 2; + } + } + if (dv->len > maxbytes) { + sprintf(buf, "..."); + buf += 3; + } +} + +/* Caller must provide a buffer at least DUK_DVALUE_TOSTRING_BUFLEN in size. */ +void duk_dvalue_to_string(duk_dvalue *dv, char *buf) { + char hexbuf[32 * 4 + 4]; /* 32 hex encoded or \xXX escaped bytes, possible "...", NUL */ + + if (!dv) { + sprintf(buf, "NULL"); + return; + } + + switch (dv->tag) { + case DUK_DVALUE_EOM: + sprintf(buf, "EOM"); + break; + case DUK_DVALUE_REQ: + sprintf(buf, "REQ"); + break; + case DUK_DVALUE_REP: + sprintf(buf, "REP"); + break; + case DUK_DVALUE_ERR: + sprintf(buf, "ERR"); + break; + case DUK_DVALUE_NFY: + sprintf(buf, "NFY"); + break; + case DUK_DVALUE_INTEGER: + sprintf(buf, "%d", dv->i); + break; + case DUK_DVALUE_STRING: + duk__dvalue_bufesc(dv, hexbuf, 32, 1); + sprintf(buf, "str:%ld:\"%s\"", (long) dv->len, hexbuf); + break; + case DUK_DVALUE_BUFFER: + duk__dvalue_bufesc(dv, hexbuf, 32, 0); + sprintf(buf, "buf:%ld:%s", (long) dv->len, hexbuf); + break; + case DUK_DVALUE_UNUSED: + sprintf(buf, "undefined"); + break; + case DUK_DVALUE_UNDEFINED: + sprintf(buf, "undefined"); + break; + case DUK_DVALUE_NULL: + sprintf(buf, "null"); + break; + case DUK_DVALUE_TRUE: + sprintf(buf, "true"); + break; + case DUK_DVALUE_FALSE: + sprintf(buf, "false"); + break; + case DUK_DVALUE_NUMBER: + if (fpclassify(dv->d) == FP_ZERO) { + if (signbit(dv->d)) { + sprintf(buf, "-0"); + } else { + sprintf(buf, "0"); + } + } else { + sprintf(buf, "%lg", dv->d); + } + break; + case DUK_DVALUE_OBJECT: + duk__dvalue_bufesc(dv, hexbuf, 32, 0); + sprintf(buf, "obj:%d:%s", (int) dv->i, hexbuf); + break; + case DUK_DVALUE_POINTER: + duk__dvalue_bufesc(dv, hexbuf, 32, 0); + sprintf(buf, "ptr:%s", hexbuf); + break; + case DUK_DVALUE_LIGHTFUNC: + duk__dvalue_bufesc(dv, hexbuf, 32, 0); + sprintf(buf, "lfunc:%04x:%s", (unsigned int) dv->i, hexbuf); + break; + case DUK_DVALUE_HEAPPTR: + duk__dvalue_bufesc(dv, hexbuf, 32, 0); + sprintf(buf, "heapptr:%s", hexbuf); + break; + default: + sprintf(buf, "unknown:%d", (int) dv->tag); + } +} + +duk_dvalue *duk_dvalue_make_tag(int tag) { + duk_dvalue *dv = duk_dvalue_alloc(); + if (!dv) { return NULL; } + dv->tag = tag; + return dv; +} + +duk_dvalue *duk_dvalue_make_tag_int(int tag, int intval) { + duk_dvalue *dv = duk_dvalue_alloc(); + if (!dv) { return NULL; } + dv->tag = tag; + dv->i = intval; + return dv; +} + +duk_dvalue *duk_dvalue_make_tag_double(int tag, double dblval) { + duk_dvalue *dv = duk_dvalue_alloc(); + if (!dv) { return NULL; } + dv->tag = tag; + dv->d = dblval; + return dv; +} + +duk_dvalue *duk_dvalue_make_tag_data(int tag, const char *buf, size_t len) { + unsigned char *p; + duk_dvalue *dv = duk_dvalue_alloc(); + if (!dv) { return NULL; } + /* Alloc size is len + 1 so that a NUL terminator is always + * guaranteed which is convenient, e.g. you can printf() the + * value safely. + */ + p = (unsigned char *) malloc(len + 1); + if (!p) { + free(dv); + return NULL; + } + memcpy((void *) p, (const void *) buf, len); + p[len] = (unsigned char) 0; + dv->tag = tag; + dv->buf = p; + dv->len = len; + return dv; +} + +duk_dvalue *duk_dvalue_make_tag_int_data(int tag, int intval, const char *buf, size_t len) { + duk_dvalue *dv = duk_dvalue_make_tag_data(tag, buf, len); + if (!dv) { return NULL; } + dv->i = intval; + return dv; +} + +/* + * Dvalue transport handling + */ + +static void duk__trans_dvalue_double_byteswap(duk_trans_dvalue_ctx *ctx, volatile unsigned char *p) { + unsigned char t; + + /* Portable IEEE double byteswap. Relies on runtime detection of + * host endianness. + */ + + if (ctx->double_byteorder == 0) { + /* little endian */ + t = p[0]; p[0] = p[7]; p[7] = t; + t = p[1]; p[1] = p[6]; p[6] = t; + t = p[2]; p[2] = p[5]; p[5] = t; + t = p[3]; p[3] = p[4]; p[4] = t; + } else if (ctx->double_byteorder == 1) { + /* big endian: ok as is */ + ; + } else { + /* mixed endian */ + t = p[0]; p[0] = p[3]; p[3] = t; + t = p[1]; p[1] = p[2]; p[2] = t; + t = p[4]; p[4] = p[7]; p[7] = t; + t = p[5]; p[5] = p[6]; p[6] = t; + } +} + +static unsigned int duk__trans_dvalue_parse_u32(duk_trans_dvalue_ctx *ctx, unsigned char *p) { + /* Integers are network endian, read back into host format in + * a portable manner. + */ + (void) ctx; + return (((unsigned int) p[0]) << 24) + + (((unsigned int) p[1]) << 16) + + (((unsigned int) p[2]) << 8) + + (((unsigned int) p[3]) << 0); +} + +static int duk__trans_dvalue_parse_i32(duk_trans_dvalue_ctx *ctx, unsigned char *p) { + /* Portable sign handling, doesn't assume 'int' is exactly 32 bits + * like a direct cast would. + */ + unsigned int tmp = duk__trans_dvalue_parse_u32(ctx, p); + if (tmp & 0x80000000UL) { + return -((int) ((tmp ^ 0xffffffffUL) + 1UL)); + } else { + return tmp; + } +} + +static unsigned int duk__trans_dvalue_parse_u16(duk_trans_dvalue_ctx *ctx, unsigned char *p) { + /* Integers are network endian, read back into host format. */ + (void) ctx; + return (((unsigned int) p[0]) << 8) + + (((unsigned int) p[1]) << 0); +} + +static double duk__trans_dvalue_parse_double(duk_trans_dvalue_ctx *ctx, unsigned char *p) { + /* IEEE doubles are network endian, read back into host format. */ + volatile union { + double d; + unsigned char b[8]; + } u; + memcpy((void *) u.b, (const void *) p, 8); + duk__trans_dvalue_double_byteswap(ctx, u.b); + return u.d; +} + +static unsigned char *duk__trans_dvalue_encode_u32(duk_trans_dvalue_ctx *ctx, unsigned char *p, unsigned int val) { + /* Integers are written in network endian format. */ + (void) ctx; + *p++ = (unsigned char) ((val >> 24) & 0xff); + *p++ = (unsigned char) ((val >> 16) & 0xff); + *p++ = (unsigned char) ((val >> 8) & 0xff); + *p++ = (unsigned char) (val & 0xff); + return p; +} + +static unsigned char *duk__trans_dvalue_encode_i32(duk_trans_dvalue_ctx *ctx, unsigned char *p, int val) { + return duk__trans_dvalue_encode_u32(ctx, p, (unsigned int) val & 0xffffffffUL); +} + +static unsigned char *duk__trans_dvalue_encode_u16(duk_trans_dvalue_ctx *ctx, unsigned char *p, unsigned int val) { + /* Integers are written in network endian format. */ + (void) ctx; + *p++ = (unsigned char) ((val >> 8) & 0xff); + *p++ = (unsigned char) (val & 0xff); + return p; +} + +static unsigned char *duk__trans_dvalue_encode_double(duk_trans_dvalue_ctx *ctx, unsigned char *p, double val) { + /* IEEE doubles are written in network endian format. */ + volatile union { + double d; + unsigned char b[8]; + } u; + u.d = val; + duk__trans_dvalue_double_byteswap(ctx, u.b); + memcpy((void *) p, (const void *) u.b, 8); + p += 8; + return p; +} + +static unsigned char *duk__trans_buffer_ensure(duk_trans_buffer *dbuf, size_t space) { + size_t avail; + size_t used; + size_t new_size; + void *new_alloc; + + used = dbuf->write_offset; + avail = dbuf->alloc_size - dbuf->write_offset; + + if (avail >= space) { + if (avail - space > 256) { + /* Too big, resize so that we reclaim memory if we have just + * received a large string/buffer value. + */ + goto do_realloc; + } + } else { + /* Too small, resize. */ + goto do_realloc; + } + + return dbuf->base + dbuf->write_offset; + + do_realloc: + new_size = used + space + 256; /* some extra to reduce resizes */ + new_alloc = realloc(dbuf->base, new_size); + if (new_alloc) { + dbuf->base = (unsigned char *) new_alloc; + dbuf->alloc_size = new_size; +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: resized buffer %p to %ld bytes, read_offset=%ld, write_offset=%ld\n", + __func__, (void *) dbuf, (long) new_size, (long) dbuf->read_offset, (long) dbuf->write_offset); + fflush(stderr); +#endif + return dbuf->base + dbuf->write_offset; + } else { + return NULL; + } +} + +/* When read_offset is large enough, "rebase" buffer by deleting already + * read data and updating offsets. + */ +static void duk__trans_buffer_rebase(duk_trans_buffer *dbuf) { + if (dbuf->read_offset > 64) { +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: rebasing buffer %p, read_offset=%ld, write_offset=%ld\n", + __func__, (void *) dbuf, (long) dbuf->read_offset, (long) dbuf->write_offset); + fflush(stderr); +#endif + if (dbuf->write_offset > dbuf->read_offset) { + memmove((void *) dbuf->base, (const void *) (dbuf->base + dbuf->read_offset), dbuf->write_offset - dbuf->read_offset); + } + dbuf->write_offset -= dbuf->read_offset; + dbuf->read_offset = 0; + } +} + +duk_trans_dvalue_ctx *duk_trans_dvalue_init(void) { + volatile union { + double d; + unsigned char b[8]; + } u; + duk_trans_dvalue_ctx *ctx = NULL; + + ctx = (duk_trans_dvalue_ctx *) malloc(sizeof(duk_trans_dvalue_ctx)); + if (!ctx) { goto fail; } + memset((void *) ctx, 0, sizeof(duk_trans_dvalue_ctx)); + ctx->received = NULL; + ctx->cooperate = NULL; + ctx->handshake = NULL; + ctx->detached = NULL; + ctx->send_buf.base = NULL; + ctx->recv_buf.base = NULL; + + ctx->send_buf.base = malloc(256); + if (!ctx->send_buf.base) { goto fail; } + ctx->send_buf.alloc_size = 256; + + ctx->recv_buf.base = malloc(256); + if (!ctx->recv_buf.base) { goto fail; } + ctx->recv_buf.alloc_size = 256; + + /* IEEE double byte order, detect at run time (could also use + * preprocessor defines but that's verbose to make portable). + * + * >>> struct.unpack('>d', '1122334455667788'.decode('hex')) + * (3.841412024471731e-226,) + * >>> struct.unpack('>d', '8877665544332211'.decode('hex')) + * (-7.086876636573014e-268,) + * >>> struct.unpack('>d', '4433221188776655'.decode('hex')) + * (3.5294303071877444e+20,) + */ + u.b[0] = 0x11; u.b[1] = 0x22; u.b[2] = 0x33; u.b[3] = 0x44; + u.b[4] = 0x55; u.b[5] = 0x66; u.b[6] = 0x77; u.b[7] = 0x88; + if (u.d < 0.0) { + ctx->double_byteorder = 0; /* little endian */ + } else if (u.d < 1.0) { + ctx->double_byteorder = 1; /* big endian */ + } else { + ctx->double_byteorder = 2; /* mixed endian (arm) */ + } +#if defined(DEBUG_PRINTS) + fprintf(stderr, "double endianness test value is %lg -> byteorder %d\n", + u.d, ctx->double_byteorder); + fflush(stderr); +#endif + + return ctx; + + fail: + if (ctx) { + free(ctx->recv_buf.base); /* tolerates NULL */ + free(ctx->send_buf.base); /* tolerates NULL */ + free(ctx); + } + return NULL; +} + +void duk_trans_dvalue_free(duk_trans_dvalue_ctx *ctx) { + if (ctx) { + free(ctx->send_buf.base); /* tolerates NULL */ + free(ctx->recv_buf.base); /* tolerates NULL */ + free(ctx); + } +} + +void duk_trans_dvalue_send(duk_trans_dvalue_ctx *ctx, duk_dvalue *dv) { + unsigned char *p; + + /* Convert argument dvalue into Duktape debug protocol format. + * Literal constants are used here for the debug protocol, + * e.g. initial byte 0x02 is REP, see doc/debugger.rst. + */ + +#if defined(DEBUG_PRINTS) + { + char buf[DUK_DVALUE_TOSTRING_BUFLEN]; + duk_dvalue_to_string(dv, buf); + fprintf(stderr, "%s: sending dvalue: %s\n", __func__, buf); + fflush(stderr); + } +#endif + + switch (dv->tag) { + case DUK_DVALUE_EOM: { + p = duk__trans_buffer_ensure(&ctx->send_buf, 1); + if (!p) { goto alloc_error; } + *p++ = 0x00; + ctx->send_buf.write_offset += 1; + break; + } + case DUK_DVALUE_REQ: { + p = duk__trans_buffer_ensure(&ctx->send_buf, 1); + if (!p) { goto alloc_error; } + *p++ = 0x01; + ctx->send_buf.write_offset += 1; + break; + } + case DUK_DVALUE_REP: { + p = duk__trans_buffer_ensure(&ctx->send_buf, 1); + if (!p) { goto alloc_error; } + *p++ = 0x02; + ctx->send_buf.write_offset += 1; + break; + } + case DUK_DVALUE_ERR: { + p = duk__trans_buffer_ensure(&ctx->send_buf, 1); + if (!p) { goto alloc_error; } + *p++ = 0x03; + ctx->send_buf.write_offset += 1; + break; + } + case DUK_DVALUE_NFY: { + p = duk__trans_buffer_ensure(&ctx->send_buf, 1); + if (!p) { goto alloc_error; } + *p++ = 0x04; + ctx->send_buf.write_offset += 1; + break; + } + case DUK_DVALUE_INTEGER: { + int i = dv->i; + if (i >= 0 && i <= 63) { + p = duk__trans_buffer_ensure(&ctx->send_buf, 1); + if (!p) { goto alloc_error; } + *p++ = (unsigned char) (0x80 + i); + ctx->send_buf.write_offset += 1; + } else if (i >= 0 && i <= 16383L) { + p = duk__trans_buffer_ensure(&ctx->send_buf, 2); + if (!p) { goto alloc_error; } + *p++ = (unsigned char) (0xc0 + (i >> 8)); + *p++ = (unsigned char) (i & 0xff); + ctx->send_buf.write_offset += 2; + } else if (i >= -0x80000000L && i <= 0x7fffffffL) { /* Harmless warning on some platforms (re: range) */ + p = duk__trans_buffer_ensure(&ctx->send_buf, 5); + if (!p) { goto alloc_error; } + *p++ = 0x10; + p = duk__trans_dvalue_encode_i32(ctx, p, i); + ctx->send_buf.write_offset += 5; + } else { + goto dvalue_error; + } + break; + } + case DUK_DVALUE_STRING: { + size_t i = dv->len; + if (i <= 0x1fUL) { + p = duk__trans_buffer_ensure(&ctx->send_buf, 1 + i); + if (!p) { goto alloc_error; } + *p++ = (unsigned char) (0x60 + i); + memcpy((void *) p, (const void *) dv->buf, i); + p += i; + ctx->send_buf.write_offset += 1 + i; + } else if (i <= 0xffffUL) { + p = duk__trans_buffer_ensure(&ctx->send_buf, 3 + i); + if (!p) { goto alloc_error; } + *p++ = 0x12; + p = duk__trans_dvalue_encode_u16(ctx, p, (unsigned int) i); + memcpy((void *) p, (const void *) dv->buf, i); + p += i; + ctx->send_buf.write_offset += 3 + i; + } else if (i <= 0xffffffffUL) { + p = duk__trans_buffer_ensure(&ctx->send_buf, 5 + i); + if (!p) { goto alloc_error; } + *p++ = 0x11; + p = duk__trans_dvalue_encode_u32(ctx, p, (unsigned int) i); + memcpy((void *) p, (const void *) dv->buf, i); + p += i; + ctx->send_buf.write_offset += 5 + i; + } else { + goto dvalue_error; + } + break; + } + case DUK_DVALUE_BUFFER: { + size_t i = dv->len; + if (i <= 0xffffUL) { + p = duk__trans_buffer_ensure(&ctx->send_buf, 3 + i); + if (!p) { goto alloc_error; } + *p++ = 0x14; + p = duk__trans_dvalue_encode_u16(ctx, p, (unsigned int) i); + memcpy((void *) p, (const void *) dv->buf, i); + p += i; + ctx->send_buf.write_offset += 3 + i; + } else if (i <= 0xffffffffUL) { + p = duk__trans_buffer_ensure(&ctx->send_buf, 5 + i); + if (!p) { goto alloc_error; } + *p++ = 0x13; + p = duk__trans_dvalue_encode_u32(ctx, p, (unsigned int) i); + memcpy((void *) p, (const void *) dv->buf, i); + p += i; + ctx->send_buf.write_offset += 5 + i; + } else { + goto dvalue_error; + } + break; + } + case DUK_DVALUE_UNUSED: { + p = duk__trans_buffer_ensure(&ctx->send_buf, 1); + if (!p) { goto alloc_error; } + *p++ = 0x15; + ctx->send_buf.write_offset += 1; + break; + } + case DUK_DVALUE_UNDEFINED: { + p = duk__trans_buffer_ensure(&ctx->send_buf, 1); + if (!p) { goto alloc_error; } + *p++ = 0x16; + ctx->send_buf.write_offset += 1; + break; + } + case DUK_DVALUE_NULL: { + p = duk__trans_buffer_ensure(&ctx->send_buf, 1); + if (!p) { goto alloc_error; } + *p++ = 0x17; + ctx->send_buf.write_offset += 1; + break; + } + case DUK_DVALUE_TRUE: { + p = duk__trans_buffer_ensure(&ctx->send_buf, 1); + if (!p) { goto alloc_error; } + *p++ = 0x18; + ctx->send_buf.write_offset += 1; + break; + } + case DUK_DVALUE_FALSE: { + p = duk__trans_buffer_ensure(&ctx->send_buf, 1); + if (!p) { goto alloc_error; } + *p++ = 0x19; + ctx->send_buf.write_offset += 1; + break; + } + case DUK_DVALUE_NUMBER: { + p = duk__trans_buffer_ensure(&ctx->send_buf, 9); + if (!p) { goto alloc_error; } + *p++ = 0x1a; + p = duk__trans_dvalue_encode_double(ctx, p, dv->d); + ctx->send_buf.write_offset += 9; + break; + } + case DUK_DVALUE_OBJECT: { + size_t i = dv->len; + if (i <= 0xffUL && dv->i >= 0 && dv->i <= 0xffL) { + p = duk__trans_buffer_ensure(&ctx->send_buf, 3 + i); + if (!p) { goto alloc_error; } + *p++ = 0x1b; + *p++ = (unsigned char) dv->i; + *p++ = (unsigned char) i; + memcpy((void *) p, (const void *) dv->buf, i); + ctx->send_buf.write_offset += 3 + i; + } else { + goto dvalue_error; + } + break; + } + case DUK_DVALUE_POINTER: { + size_t i = dv->len; + if (i <= 0xffUL) { + p = duk__trans_buffer_ensure(&ctx->send_buf, 2 + i); + if (!p) { goto alloc_error; } + *p++ = 0x1c; + *p++ = (unsigned char) i; + memcpy((void *) p, (const void *) dv->buf, i); + ctx->send_buf.write_offset += 2 + i; + } else { + goto dvalue_error; + } + break; + } + case DUK_DVALUE_LIGHTFUNC: { + size_t i = dv->len; + if (i <= 0xffUL && dv->i >= 0 && dv->i <= 0xffffL) { + p = duk__trans_buffer_ensure(&ctx->send_buf, 4 + i); + if (!p) { goto alloc_error; } + *p++ = 0x1d; + p = duk__trans_dvalue_encode_u16(ctx, p, (unsigned int) dv->i); + *p++ = (unsigned char) i; + memcpy((void *) p, (const void *) dv->buf, i); + ctx->send_buf.write_offset += 4 + i; + } else { + goto dvalue_error; + } + break; + } + case DUK_DVALUE_HEAPPTR: { + size_t i = dv->len; + if (i <= 0xffUL) { + p = duk__trans_buffer_ensure(&ctx->send_buf, 2 + i); + if (!p) { goto alloc_error; } + *p++ = 0x1e; + *p++ = (unsigned char) i; + memcpy((void *) p, (const void *) dv->buf, i); + ctx->send_buf.write_offset += 2 + i; + } else { + goto dvalue_error; + } + break; + } + default: { + goto dvalue_error; + } + } /* end switch */ + + return; + + dvalue_error: +#if defined(ERROR_PRINTS) + fprintf(stderr, "%s: internal error, argument dvalue is invalid\n", __func__); + fflush(stdout); +#endif + return; + + alloc_error: +#if defined(ERROR_PRINTS) + fprintf(stderr, "%s: internal error, failed to allocate space for write\n", __func__); + fflush(stdout); +#endif + return; +} + +static void duk__trans_dvalue_send_and_free(duk_trans_dvalue_ctx *ctx, duk_dvalue *dv) { + if (!dv) { return; } + duk_trans_dvalue_send(ctx, dv); + duk_dvalue_free(dv); +} + +void duk_trans_dvalue_send_eom(duk_trans_dvalue_ctx *ctx) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag(DUK_DVALUE_EOM)); +} + +void duk_trans_dvalue_send_req(duk_trans_dvalue_ctx *ctx) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag(DUK_DVALUE_REQ)); +} + +void duk_trans_dvalue_send_rep(duk_trans_dvalue_ctx *ctx) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag(DUK_DVALUE_REP)); +} + +void duk_trans_dvalue_send_err(duk_trans_dvalue_ctx *ctx) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag(DUK_DVALUE_ERR)); +} + +void duk_trans_dvalue_send_nfy(duk_trans_dvalue_ctx *ctx) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag(DUK_DVALUE_NFY)); +} + +void duk_trans_dvalue_send_integer(duk_trans_dvalue_ctx *ctx, int val) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag_int(DUK_DVALUE_INTEGER, val)); +} + +void duk_trans_dvalue_send_string(duk_trans_dvalue_ctx *ctx, const char *str) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag_data(DUK_DVALUE_STRING, str, strlen(str))); +} + +void duk_trans_dvalue_send_lstring(duk_trans_dvalue_ctx *ctx, const char *str, size_t len) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag_data(DUK_DVALUE_STRING, str, len)); +} + +void duk_trans_dvalue_send_buffer(duk_trans_dvalue_ctx *ctx, const char *buf, size_t len) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag_data(DUK_DVALUE_BUFFER, buf, len)); +} + +void duk_trans_dvalue_send_unused(duk_trans_dvalue_ctx *ctx) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag(DUK_DVALUE_UNUSED)); +} + +void duk_trans_dvalue_send_undefined(duk_trans_dvalue_ctx *ctx) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag(DUK_DVALUE_UNDEFINED)); +} + +void duk_trans_dvalue_send_null(duk_trans_dvalue_ctx *ctx) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag(DUK_DVALUE_NULL)); +} + +void duk_trans_dvalue_send_true(duk_trans_dvalue_ctx *ctx) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag(DUK_DVALUE_TRUE)); +} + +void duk_trans_dvalue_send_false(duk_trans_dvalue_ctx *ctx) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag(DUK_DVALUE_FALSE)); +} + +void duk_trans_dvalue_send_number(duk_trans_dvalue_ctx *ctx, double val) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag_double(DUK_DVALUE_NUMBER, val)); +} + +void duk_trans_dvalue_send_object(duk_trans_dvalue_ctx *ctx, int classnum, const char *ptr_data, size_t ptr_len) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag_int_data(DUK_DVALUE_OBJECT, classnum, ptr_data, ptr_len)); +} + +void duk_trans_dvalue_send_pointer(duk_trans_dvalue_ctx *ctx, const char *ptr_data, size_t ptr_len) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag_data(DUK_DVALUE_POINTER, ptr_data, ptr_len)); +} + +void duk_trans_dvalue_send_lightfunc(duk_trans_dvalue_ctx *ctx, int lf_flags, const char *ptr_data, size_t ptr_len) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag_int_data(DUK_DVALUE_LIGHTFUNC, lf_flags, ptr_data, ptr_len)); +} + +void duk_trans_dvalue_send_heapptr(duk_trans_dvalue_ctx *ctx, const char *ptr_data, size_t ptr_len) { + duk__trans_dvalue_send_and_free(ctx, duk_dvalue_make_tag_data(DUK_DVALUE_HEAPPTR, ptr_data, ptr_len)); +} + +void duk_trans_dvalue_send_req_cmd(duk_trans_dvalue_ctx *ctx, int cmd) { + duk_trans_dvalue_send_req(ctx); + duk_trans_dvalue_send_integer(ctx, cmd); +} + +static duk_dvalue *duk__trans_trial_parse_dvalue(duk_trans_dvalue_ctx *ctx) { + unsigned char *p; + size_t len; + unsigned char ib; + duk_dvalue *dv; + size_t datalen; + + p = ctx->recv_buf.base + ctx->recv_buf.read_offset; + len = ctx->recv_buf.write_offset - ctx->recv_buf.read_offset; + + if (len == 0) { + return NULL; + } + ib = p[0]; + +#if defined(DEBUG_PRINTS) + { + size_t i; + fprintf(stderr, "%s: parsing dvalue, window:", __func__); + for (i = 0; i < 16; i++) { + if (i < len) { + fprintf(stderr, " %02x", (unsigned int) p[i]); + } else { + fprintf(stderr, " ??"); + } + } + fprintf(stderr, " (length %ld, read_offset %ld, write_offset %ld, alloc_size %ld)\n", + (long) len, (long) ctx->recv_buf.read_offset, (long) ctx->recv_buf.write_offset, + (long) ctx->recv_buf.alloc_size); + fflush(stderr); + } +#endif + + if (ib <= 0x1fU) { + /* 0x00 ... 0x1f */ + switch (ib) { + case 0x00: { + ctx->recv_buf.read_offset += 1; + dv = duk_dvalue_make_tag(DUK_DVALUE_EOM); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x01: { + ctx->recv_buf.read_offset += 1; + dv = duk_dvalue_make_tag(DUK_DVALUE_REQ); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x02: { + ctx->recv_buf.read_offset += 1; + dv = duk_dvalue_make_tag(DUK_DVALUE_REP); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x03: { + ctx->recv_buf.read_offset += 1; + dv = duk_dvalue_make_tag(DUK_DVALUE_ERR); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x04: { + ctx->recv_buf.read_offset += 1; + dv = duk_dvalue_make_tag(DUK_DVALUE_NFY); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x10: { + int intval; + if (len < 5) { goto partial; } + intval = duk__trans_dvalue_parse_i32(ctx, p + 1); + ctx->recv_buf.read_offset += 5; + dv = duk_dvalue_make_tag_int(DUK_DVALUE_INTEGER, intval); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x11: { + if (len < 5) { goto partial; } + datalen = (size_t) duk__trans_dvalue_parse_u32(ctx, p + 1); + if (len < 5 + datalen) { goto partial; } + ctx->recv_buf.read_offset += 5 + datalen; + dv = duk_dvalue_make_tag_data(DUK_DVALUE_STRING, (const char *) (p + 5), datalen); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x12: { + if (len < 3) { goto partial; } + datalen = (size_t) duk__trans_dvalue_parse_u16(ctx, p + 1); + if (len < 3 + datalen) { goto partial; } + ctx->recv_buf.read_offset += 3 + datalen; + dv = duk_dvalue_make_tag_data(DUK_DVALUE_STRING, (const char *) (p + 3), datalen); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x13: { + if (len < 5) { goto partial; } + datalen = (size_t) duk__trans_dvalue_parse_u32(ctx, p + 1); + if (len < 5 + datalen) { goto partial; } + ctx->recv_buf.read_offset += 5 + datalen; + dv = duk_dvalue_make_tag_data(DUK_DVALUE_BUFFER, (const char *) (p + 5), datalen); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x14: { + if (len < 3) { goto partial; } + datalen = (size_t) duk__trans_dvalue_parse_u16(ctx, p + 1); + if (len < 3 + datalen) { goto partial; } + ctx->recv_buf.read_offset += 3 + datalen; + dv = duk_dvalue_make_tag_data(DUK_DVALUE_BUFFER, (const char *) (p + 3), datalen); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x15: { + ctx->recv_buf.read_offset += 1; + dv = duk_dvalue_make_tag(DUK_DVALUE_UNUSED); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x16: { + ctx->recv_buf.read_offset += 1; + dv = duk_dvalue_make_tag(DUK_DVALUE_UNDEFINED); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x17: { + ctx->recv_buf.read_offset += 1; + dv = duk_dvalue_make_tag(DUK_DVALUE_NULL); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x18: { + ctx->recv_buf.read_offset += 1; + dv = duk_dvalue_make_tag(DUK_DVALUE_TRUE); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x19: { + ctx->recv_buf.read_offset += 1; + dv = duk_dvalue_make_tag(DUK_DVALUE_FALSE); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x1a: { + double dblval; + if (len < 9) { goto partial; } + dblval = duk__trans_dvalue_parse_double(ctx, p + 1); + ctx->recv_buf.read_offset += 9; + dv = duk_dvalue_make_tag_double(DUK_DVALUE_NUMBER, dblval); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x1b: { + int classnum; + if (len < 3) { goto partial; } + datalen = (size_t) p[2]; + if (len < 3 + datalen) { goto partial; } + classnum = (int) p[1]; + ctx->recv_buf.read_offset += 3 + datalen; + dv = duk_dvalue_make_tag_int_data(DUK_DVALUE_OBJECT, classnum, (const char *) (p + 3), datalen); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x1c: { + if (len < 2) { goto partial; } + datalen = (size_t) p[1]; + if (len < 2 + datalen) { goto partial; } + ctx->recv_buf.read_offset += 2 + datalen; + dv = duk_dvalue_make_tag_data(DUK_DVALUE_POINTER, (const char *) (p + 2), datalen); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x1d: { + int lf_flags; + if (len < 4) { goto partial; } + datalen = (size_t) p[3]; + if (len < 4 + datalen) { goto partial; } + lf_flags = (int) duk__trans_dvalue_parse_u16(ctx, p + 1); + ctx->recv_buf.read_offset += 4 + datalen; + dv = duk_dvalue_make_tag_int_data(DUK_DVALUE_LIGHTFUNC, lf_flags, (const char *) (p + 4), datalen); + if (!dv) { goto alloc_error; } + return dv; + } + case 0x1e: { + if (len < 2) { goto partial; } + datalen = (size_t) p[1]; + if (len < 2 + datalen) { goto partial; } + ctx->recv_buf.read_offset += 2 + datalen; + dv = duk_dvalue_make_tag_data(DUK_DVALUE_HEAPPTR, (const char *) (p + 2), datalen); + if (!dv) { goto alloc_error; } + return dv; + } + default: { + goto format_error; + } + } /* end switch */ + } else if (ib <= 0x5fU) { + /* 0x20 ... 0x5f */ + goto format_error; + } else if (ib <= 0x7fU) { + /* 0x60 ... 0x7f */ + datalen = (size_t) (ib - 0x60U); + if (len < 1 + datalen) { goto partial; } + ctx->recv_buf.read_offset += 1 + datalen; + dv = duk_dvalue_make_tag_data(DUK_DVALUE_STRING, (const char *) (p + 1), datalen); + if (!dv) { goto alloc_error; } + return dv; + } else if (ib <= 0xbfU) { + /* 0x80 ... 0xbf */ + int intval; + intval = (int) (ib - 0x80U); + ctx->recv_buf.read_offset += 1; + dv = duk_dvalue_make_tag_int(DUK_DVALUE_INTEGER, intval); + if (!dv) { goto alloc_error; } + return dv; + } else { + /* 0xc0 ... 0xff */ + int intval; + if (len < 2) { goto partial; } + intval = (((int) (ib - 0xc0U)) << 8) + (int) p[1]; + ctx->recv_buf.read_offset += 2; + dv = duk_dvalue_make_tag_int(DUK_DVALUE_INTEGER, intval); + if (!dv) { goto alloc_error; } + return dv; + } + + /* never here */ + + partial: + return NULL; + + alloc_error: +#if defined(ERROR_PRINTS) + fprintf(stderr, "%s: internal error, cannot allocate space for dvalue\n", __func__); + fflush(stdout); +#endif + return NULL; + + format_error: +#if defined(ERROR_PRINTS) + fprintf(stderr, "%s: internal error, dvalue format error\n", __func__); + fflush(stdout); +#endif + return NULL; +} + +static duk_dvalue *duk__trans_trial_parse_handshake(duk_trans_dvalue_ctx *ctx) { + unsigned char *p; + size_t len; + duk_dvalue *dv; + size_t i; + + p = ctx->recv_buf.base + ctx->recv_buf.read_offset; + len = ctx->recv_buf.write_offset - ctx->recv_buf.read_offset; + + for (i = 0; i < len; i++) { + if (p[i] == 0x0a) { + /* Handshake line is returned as a dvalue for convenience; it's + * not actually a part of the dvalue phase of the protocol. + */ + ctx->recv_buf.read_offset += i + 1; + dv = duk_dvalue_make_tag_data(DUK_DVALUE_STRING, (const char *) p, i); + if (!dv) { goto alloc_error; } + return dv; + } + } + + return NULL; + + alloc_error: +#if defined(ERROR_PRINTS) + fprintf(stderr, "%s: internal error, cannot allocate space for handshake line\n", __func__); + fflush(stdout); +#endif + return NULL; +} + +static void duk__trans_call_cooperate(duk_trans_dvalue_ctx *ctx, int block) { + if (ctx->cooperate) { + ctx->cooperate(ctx, block); + } +} + +static void duk__trans_call_received(duk_trans_dvalue_ctx *ctx, duk_dvalue *dv) { + if (ctx->received) { + ctx->received(ctx, dv); + } +} + +static void duk__trans_call_handshake(duk_trans_dvalue_ctx *ctx, const char *line) { + if (ctx->handshake) { + ctx->handshake(ctx, line); + } +} + +static void duk__trans_call_detached(duk_trans_dvalue_ctx *ctx) { + if (ctx->detached) { + ctx->detached(ctx); + } +} + +/* + * Duktape callbacks + */ + +duk_size_t duk_trans_dvalue_read_cb(void *udata, char *buffer, duk_size_t length) { + duk_trans_dvalue_ctx *ctx = (duk_trans_dvalue_ctx *) udata; + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: %p %p %ld\n", __func__, udata, (void *) buffer, (long) length); + fflush(stderr); +#endif + + duk__trans_call_cooperate(ctx, 0); + + for (;;) { + size_t avail, now; + + avail = (size_t) (ctx->send_buf.write_offset - ctx->send_buf.read_offset); + if (avail == 0) { + /* Must cooperate until user callback provides data. From + * Duktape's perspective we MUST block until data is received. + */ + duk__trans_call_cooperate(ctx, 1); + } else { + now = avail; + if (now > length) { + now = length; + } + memcpy((void *) buffer, (const void *) (ctx->send_buf.base + ctx->send_buf.read_offset), now); + duk__trans_buffer_rebase(&ctx->send_buf); + ctx->send_buf.read_offset += now; + return now; + } + } +} + +duk_size_t duk_trans_dvalue_write_cb(void *udata, const char *buffer, duk_size_t length) { + duk_trans_dvalue_ctx *ctx = (duk_trans_dvalue_ctx *) udata; + unsigned char *p; + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: %p %p %ld\n", __func__, udata, (void *) buffer, (long) length); + fflush(stderr); +#endif + + duk__trans_call_cooperate(ctx, 0); + + /* Append data. */ + duk__trans_buffer_rebase(&ctx->recv_buf); + p = duk__trans_buffer_ensure(&ctx->recv_buf, length); + memcpy((void *) p, (const void *) buffer, (size_t) length); + ctx->recv_buf.write_offset += length; + + /* Trial parse handshake line or dvalue(s). */ + if (!ctx->handshake_done) { + duk_dvalue *dv = duk__trans_trial_parse_handshake(ctx); + if (dv) { + /* Handshake line is available for caller for the + * duration of the callback, and must not be freed + * by the caller. + */ + duk__trans_call_handshake(ctx, (const char *) dv->buf); +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: handshake ok\n", __func__); + fflush(stderr); +#endif + duk_dvalue_free(dv); + ctx->handshake_done = 1; + } + } + if (ctx->handshake_done) { + for (;;) { + duk_dvalue *dv = duk__trans_trial_parse_dvalue(ctx); + if (dv) { +#if defined(DEBUG_PRINTS) + { + char buf[DUK_DVALUE_TOSTRING_BUFLEN]; + duk_dvalue_to_string(dv, buf); + fprintf(stderr, "%s: received dvalue: %s\n", __func__, buf); + fflush(stderr); + } +#endif + + duk__trans_call_received(ctx, dv); + } else { + break; + } + } + } + + duk__trans_call_cooperate(ctx, 0); /* just in case, if dvalues changed something */ + + return length; +} + +duk_size_t duk_trans_dvalue_peek_cb(void *udata) { + duk_trans_dvalue_ctx *ctx = (duk_trans_dvalue_ctx *) udata; + size_t avail; + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: %p\n", __func__, udata); + fflush(stderr); +#endif + + duk__trans_call_cooperate(ctx, 0); + avail = (size_t) (ctx->send_buf.write_offset - ctx->send_buf.read_offset); + return (duk_size_t) avail; +} + +void duk_trans_dvalue_read_flush_cb(void *udata) { + duk_trans_dvalue_ctx *ctx = (duk_trans_dvalue_ctx *) udata; + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: %p\n", __func__, udata); + fflush(stderr); +#endif + + duk__trans_call_cooperate(ctx, 0); +} + +void duk_trans_dvalue_write_flush_cb(void *udata) { + duk_trans_dvalue_ctx *ctx = (duk_trans_dvalue_ctx *) udata; + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: %p\n", __func__, udata); + fflush(stderr); +#endif + + duk__trans_call_cooperate(ctx, 0); +} + +void duk_trans_dvalue_detached_cb(void *udata) { + duk_trans_dvalue_ctx *ctx = (duk_trans_dvalue_ctx *) udata; + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: %p\n", __func__, udata); + fflush(stderr); +#endif + + duk__trans_call_detached(ctx); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/duk_trans_dvalue.h b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/duk_trans_dvalue.h new file mode 100644 index 000000000..e0ba731be --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/duk_trans_dvalue.h @@ -0,0 +1,113 @@ +#ifndef DUK_TRANS_DVALUE_H_INCLUDED +#define DUK_TRANS_DVALUE_H_INCLUDED + +#include "duktape.h" + +typedef struct duk_dvalue duk_dvalue; +typedef struct duk_trans_buffer duk_trans_buffer; +typedef struct duk_trans_dvalue_ctx duk_trans_dvalue_ctx; + +typedef void (*duk_trans_dvalue_received_function)(duk_trans_dvalue_ctx *ctx, duk_dvalue *dv); +typedef void (*duk_trans_dvalue_cooperate_function)(duk_trans_dvalue_ctx *ctx, int block); +typedef void (*duk_trans_dvalue_handshake_function)(duk_trans_dvalue_ctx *ctx, const char *handshake_line); +typedef void (*duk_trans_dvalue_detached_function)(duk_trans_dvalue_ctx *ctx); + +/* struct duk_dvalue 'tag' values, note that these have nothing to do with + * Duktape debug protocol inital byte. Struct fields used with the type + * are noted next to the define. + */ +#define DUK_DVALUE_EOM 1 /* no fields */ +#define DUK_DVALUE_REQ 2 /* no fields */ +#define DUK_DVALUE_REP 3 /* no fields */ +#define DUK_DVALUE_ERR 4 /* no fields */ +#define DUK_DVALUE_NFY 5 /* no fields */ +#define DUK_DVALUE_INTEGER 6 /* i: 32-bit signed integer */ +#define DUK_DVALUE_STRING 7 /* buf: string data, len: string length */ +#define DUK_DVALUE_BUFFER 8 /* buf: buffer data, len: buffer length */ +#define DUK_DVALUE_UNUSED 9 /* no fields */ +#define DUK_DVALUE_UNDEFINED 10 /* no fields */ +#define DUK_DVALUE_NULL 11 /* no fields */ +#define DUK_DVALUE_TRUE 12 /* no fields */ +#define DUK_DVALUE_FALSE 13 /* no fields */ +#define DUK_DVALUE_NUMBER 14 /* d: ieee double */ +#define DUK_DVALUE_OBJECT 15 /* i: class number, buf: pointer data, len: pointer length */ +#define DUK_DVALUE_POINTER 16 /* buf: pointer data, len: pointer length */ +#define DUK_DVALUE_LIGHTFUNC 17 /* i: lightfunc flags, buf: pointer data, len: pointer length */ +#define DUK_DVALUE_HEAPPTR 18 /* buf: pointer data, len: pointer length */ + +struct duk_dvalue { + /* Could use a union for the value but the gain would be relatively small. */ + int tag; + int i; + double d; + size_t len; + unsigned char *buf; +}; + +struct duk_trans_buffer { + unsigned char *base; + size_t write_offset; + size_t read_offset; + size_t alloc_size; +}; + +struct duk_trans_dvalue_ctx { + duk_trans_dvalue_received_function received; + duk_trans_dvalue_cooperate_function cooperate; + duk_trans_dvalue_handshake_function handshake; + duk_trans_dvalue_detached_function detached; + duk_trans_buffer send_buf; /* sending towards Duktape (duktape read callback) */ + duk_trans_buffer recv_buf; /* receiving from Duktape (duktape write callback) */ + int handshake_done; + int double_byteorder; /* 0=little endian, 1=big endian, 2=mixed endian */ +}; + +/* Buffer size needed by duk_dvalue_to_string(). */ +#define DUK_DVALUE_TOSTRING_BUFLEN 256 + +/* Dvalue handling. */ +duk_dvalue *duk_dvalue_alloc(void); +void duk_dvalue_free(duk_dvalue *dv); +void duk_dvalue_to_string(duk_dvalue *dv, char *buf); +duk_dvalue *duk_dvalue_make_tag(int tag); +duk_dvalue *duk_dvalue_make_tag_int(int tag, int intval); +duk_dvalue *duk_dvalue_make_tag_double(int tag, double dblval); +duk_dvalue *duk_dvalue_make_tag_data(int tag, const char *buf, size_t len); +duk_dvalue *duk_dvalue_make_tag_int_data(int tag, int intval, const char *buf, size_t len); + +/* Initializing and freeing the transport context. */ +duk_trans_dvalue_ctx *duk_trans_dvalue_init(void); +void duk_trans_dvalue_free(duk_trans_dvalue_ctx *ctx); + +/* Sending dvalues towards Duktape. */ +void duk_trans_dvalue_send(duk_trans_dvalue_ctx *ctx, duk_dvalue *dv); +void duk_trans_dvalue_send_eom(duk_trans_dvalue_ctx *ctx); +void duk_trans_dvalue_send_req(duk_trans_dvalue_ctx *ctx); +void duk_trans_dvalue_send_rep(duk_trans_dvalue_ctx *ctx); +void duk_trans_dvalue_send_err(duk_trans_dvalue_ctx *ctx); +void duk_trans_dvalue_send_nfy(duk_trans_dvalue_ctx *ctx); +void duk_trans_dvalue_send_integer(duk_trans_dvalue_ctx *ctx, int val); +void duk_trans_dvalue_send_string(duk_trans_dvalue_ctx *ctx, const char *str); +void duk_trans_dvalue_send_lstring(duk_trans_dvalue_ctx *ctx, const char *str, size_t len); +void duk_trans_dvalue_send_buffer(duk_trans_dvalue_ctx *ctx, const char *buf, size_t len); +void duk_trans_dvalue_send_unused(duk_trans_dvalue_ctx *ctx); +void duk_trans_dvalue_send_undefined(duk_trans_dvalue_ctx *ctx); +void duk_trans_dvalue_send_null(duk_trans_dvalue_ctx *ctx); +void duk_trans_dvalue_send_true(duk_trans_dvalue_ctx *ctx); +void duk_trans_dvalue_send_false(duk_trans_dvalue_ctx *ctx); +void duk_trans_dvalue_send_number(duk_trans_dvalue_ctx *ctx, double val); +void duk_trans_dvalue_send_object(duk_trans_dvalue_ctx *ctx, int classnum, const char *ptr_data, size_t ptr_len); +void duk_trans_dvalue_send_pointer(duk_trans_dvalue_ctx *ctx, const char *ptr_data, size_t ptr_len); +void duk_trans_dvalue_send_lightfunc(duk_trans_dvalue_ctx *ctx, int lf_flags, const char *ptr_data, size_t ptr_len); +void duk_trans_dvalue_send_heapptr(duk_trans_dvalue_ctx *ctx, const char *ptr_data, size_t ptr_len); +void duk_trans_dvalue_send_req_cmd(duk_trans_dvalue_ctx *ctx, int cmd); + +/* Duktape debug callbacks provided by the transport. */ +duk_size_t duk_trans_dvalue_read_cb(void *udata, char *buffer, duk_size_t length); +duk_size_t duk_trans_dvalue_write_cb(void *udata, const char *buffer, duk_size_t length); +duk_size_t duk_trans_dvalue_peek_cb(void *udata); +void duk_trans_dvalue_read_flush_cb(void *udata); +void duk_trans_dvalue_write_flush_cb(void *udata); +void duk_trans_dvalue_detached_cb(void *udata); + +#endif /* DUK_TRANS_DVALUE_H_INCLUDED */ diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/test.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/test.c new file mode 100644 index 000000000..7830ec203 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-dvalue/test.c @@ -0,0 +1,236 @@ +/* + * Example program using the dvalue debug transport. + */ + +#include +#include + +#include "duktape.h" +#include "duk_trans_dvalue.h" + +void my_cooperate(duk_trans_dvalue_ctx *ctx, int block) { + static int first_blocked = 1; + + if (!block) { + /* Duktape is not blocked; you can cooperate with e.g. a user + * interface here and send dvalues to Duktape, but don't block. + */ + return; + } + + /* Duktape is blocked on a read and won't continue until debug + * command(s) are sent. + * + * Normally you'd enter your own event loop here, and process + * events until something needs to be sent to Duktape. For + * example, the user might press a "Step over" button in the + * UI which would cause dvalues to be sent. You can then + * return from this callback. + * + * The code below sends some example messages for testing the + * dvalue handling of the transport. + * + * If you create dvalues manually and send them using + * duk_trans_dvalue_send(), you must free the dvalues after + * the send call returns using duk_dvalue_free(). + */ + + if (first_blocked) { + char *tmp; + int i; + + /* First time Duktape becomes blocked, send DumpHeap which + * exercises a lot of parsing code. + * + * NOTE: Valgrind may complain about reading uninitialized + * bytes. This is caused by the DumpHeap command writing out + * verbatim duk_tval values which are intentionally not + * always fully initialized for performance reasons. + */ + first_blocked = 0; + + fprintf(stderr, "Duktape is blocked, send DumpHeap\n"); + fflush(stderr); + + duk_trans_dvalue_send_req(ctx); + duk_trans_dvalue_send_integer(ctx, 0x20); /* DumpHeap */ + duk_trans_dvalue_send_eom(ctx); + + /* Also send a dummy TriggerStatus request with trailing dvalues + * ignored by Duktape; Duktape will parse the dvalues to be able to + * skip them, so that the dvalue encoding is exercised. + */ + + tmp = malloc(100000); /* long buffer, >= 65536 chars */ + for (i = 0; i < 100000; i++) { + tmp[i] = (char) i; + } + duk_trans_dvalue_send_req(ctx); + duk_trans_dvalue_send_integer(ctx, 0x11); /* TriggerStatus */ + duk_trans_dvalue_send_string(ctx, "dummy"); /* short, <= 31 chars */ + duk_trans_dvalue_send_string(ctx, "123456789012345678901234567890foobar"); /* medium, >= 32 chars */ + duk_trans_dvalue_send_lstring(ctx, (const char *) tmp, 65535UL); + duk_trans_dvalue_send_lstring(ctx, (const char *) tmp, 65536UL); + duk_trans_dvalue_send_lstring(ctx, (const char *) tmp, 100000UL); + duk_trans_dvalue_send_buffer(ctx, (const char *) tmp, 255U); + duk_trans_dvalue_send_buffer(ctx, (const char *) tmp, 65535UL); + duk_trans_dvalue_send_buffer(ctx, (const char *) tmp, 65536UL); + duk_trans_dvalue_send_buffer(ctx, (const char *) tmp, 100000UL); + duk_trans_dvalue_send_unused(ctx); + duk_trans_dvalue_send_undefined(ctx); + duk_trans_dvalue_send_null(ctx); + duk_trans_dvalue_send_true(ctx); + duk_trans_dvalue_send_false(ctx); + duk_trans_dvalue_send_number(ctx, 123.456); + duk_trans_dvalue_send_object(ctx, 12 /*classnum*/, (const char *) tmp, 8); /* fake ptr len */ + duk_trans_dvalue_send_pointer(ctx, (const char *) tmp, 8); /* fake ptr len */ + duk_trans_dvalue_send_lightfunc(ctx, 0xdabc /*lf_flags*/, (const char *) tmp, 8); /* fake ptr len */ + duk_trans_dvalue_send_heapptr(ctx, (const char *) tmp, 8); /* fake ptr len */ + + duk_trans_dvalue_send_eom(ctx); + } + + fprintf(stderr, "Duktape is blocked, send Eval and StepInto to resume execution\n"); + fflush(stderr); + + /* duk_trans_dvalue_send_req_cmd() sends a REQ dvalue followed by + * an integer dvalue (command) for convenience. + */ + + duk_trans_dvalue_send_req_cmd(ctx, 0x1e); /* 0x1e = Eval */ + duk_trans_dvalue_send_string(ctx, "evalMe"); + duk_trans_dvalue_send_eom(ctx); + + duk_trans_dvalue_send_req_cmd(ctx, 0x14); /* 0x14 = StepOver */ + duk_trans_dvalue_send_eom(ctx); +} + +void my_received(duk_trans_dvalue_ctx *ctx, duk_dvalue *dv) { + char buf[DUK_DVALUE_TOSTRING_BUFLEN]; + (void) ctx; + + duk_dvalue_to_string(dv, buf); + fprintf(stderr, "Received dvalue: %s\n", buf); + fflush(stderr); + + /* Here a normal debug client would wait for dvalues until an EOM + * dvalue was received (which completes a debug message). The + * debug message would then be handled, possibly causing UI changes + * and/or causing debug commands to be sent to Duktape. + * + * The callback is responsible for eventually freeing the dvalue. + * Here we free it immediately, but an actual client would probably + * gather dvalues into an array or linked list to handle when the + * debug message was complete. + */ + + duk_dvalue_free(dv); +} + +void my_handshake(duk_trans_dvalue_ctx *ctx, const char *line) { + (void) ctx; + + /* The Duktape handshake line is given in 'line' (without LF). + * The 'line' argument can be accessed for the duration of the + * callback (read only). Don't free 'line' here, the transport + * handles that. + */ + + fprintf(stderr, "Received handshake line: '%s'\n", line); + fflush(stderr); +} + +void my_detached(duk_trans_dvalue_ctx *ctx) { + (void) ctx; + + /* Detached call forwarded as is. */ + + fprintf(stderr, "Debug transport detached\n"); + fflush(stderr); +} + +int main(int argc, char *argv[]) { + duk_context *ctx; + duk_trans_dvalue_ctx *trans_ctx; + int exitval = 0; + + (void) argc; (void) argv; /* suppress warning */ + + ctx = duk_create_heap_default(); + if (!ctx) { + fprintf(stderr, "Failed to create Duktape heap\n"); + fflush(stderr); + exitval = 1; + goto cleanup; + } + + trans_ctx = duk_trans_dvalue_init(); + if (!trans_ctx) { + fprintf(stderr, "Failed to create debug transport context\n"); + fflush(stderr); + exitval = 1; + goto cleanup; + } + trans_ctx->cooperate = my_cooperate; + trans_ctx->received = my_received; + trans_ctx->handshake = my_handshake; + trans_ctx->detached = my_detached; + + /* Attach debugger; this will fail with a fatal error here unless + * debugger support is compiled in. To fail more gracefully, call + * this under a duk_safe_call() to catch the error. + */ + duk_debugger_attach(ctx, + duk_trans_dvalue_read_cb, + duk_trans_dvalue_write_cb, + duk_trans_dvalue_peek_cb, + duk_trans_dvalue_read_flush_cb, + duk_trans_dvalue_write_flush_cb, + duk_trans_dvalue_detached_cb, + (void *) trans_ctx); + + fprintf(stderr, "Debugger attached, running eval\n"); + fflush(stderr); + + /* Evaluate simple test code, callbacks will "step over" until end. + * + * The test code here is just for exercising the debug transport. + * The 'evalMe' variable is evaluated (using debugger command Eval) + * before every step to force different dvalues to be carried over + * the transport. + */ + + duk_eval_string(ctx, + "var evalMe;\n" + "\n" + "print('Hello world!');\n" + "[ undefined, null, true, false, 123, -123, 123.1, 0, -0, 1/0, 0/0, -1/0, \n" + " 'foo', Duktape.Buffer('bar'), Duktape.Pointer('dummy'), Math.cos, \n" + "].forEach(function (val) {\n" + " print(val);\n" + " evalMe = val;\n" + "});\n" + "\n" + "var str = 'xxx'\n" + "for (i = 0; i < 10; i++) {\n" + " print(i, str);\n" + " evalMe = str;\n" + " evalMe = Duktape.Buffer(str);\n" + " str = str + str;\n" + "}\n" + ); + duk_pop(ctx); + + duk_debugger_detach(ctx); + + cleanup: + if (trans_ctx) { + duk_trans_dvalue_free(trans_ctx); + trans_ctx = NULL; + } + if (ctx) { + duk_destroy_heap(ctx); + } + + return exitval; +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/README.rst new file mode 100644 index 000000000..78522cddb --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/README.rst @@ -0,0 +1,17 @@ +================================================ +Debug transport using a simple socket connection +================================================ + +This example implements an example debug transport which uses a Linux or +Windows TCP server socket on the debug target. + +Files: + +* ``duk_trans_socket.h``: header file for the transport, used for both Linux + and Windows socket variants. + +* ``duk_trans_socket_unix.c``: implementation for Linux/Unix. + +* ``duk_trans_socket_windows.c``: implementation for Windows. + +Compile either Unix or Windows source file only. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/duk_trans_socket.h b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/duk_trans_socket.h new file mode 100644 index 000000000..43f4a34de --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/duk_trans_socket.h @@ -0,0 +1,15 @@ +#ifndef DUK_TRANS_SOCKET_H_INCLUDED +#define DUK_TRANS_SOCKET_H_INCLUDED + +#include "duktape.h" + +void duk_trans_socket_init(void); +void duk_trans_socket_finish(void); +void duk_trans_socket_waitconn(void); +duk_size_t duk_trans_socket_read_cb(void *udata, char *buffer, duk_size_t length); +duk_size_t duk_trans_socket_write_cb(void *udata, const char *buffer, duk_size_t length); +duk_size_t duk_trans_socket_peek_cb(void *udata); +void duk_trans_socket_read_flush_cb(void *udata); +void duk_trans_socket_write_flush_cb(void *udata); + +#endif /* DUK_TRANS_SOCKET_H_INCLUDED */ diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/duk_trans_socket_unix.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/duk_trans_socket_unix.c new file mode 100644 index 000000000..ac74de2bf --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/duk_trans_socket_unix.c @@ -0,0 +1,340 @@ +/* + * Example debug transport using a Linux/Unix TCP socket + * + * Provides a TCP server socket which a debug client can connect to. + * After that data is just passed through. + */ + +#include +#include +#include +#include +#include +#include +#include +#include "duktape.h" + +#if !defined(DUK_DEBUG_PORT) +#define DUK_DEBUG_PORT 9091 +#endif + +#if 0 +#define DEBUG_PRINTS +#endif + +static int server_sock = -1; +static int client_sock = -1; + +/* + * Transport init and finish + */ + +void duk_trans_socket_init(void) { + struct sockaddr_in addr; + int on; + + server_sock = socket(AF_INET, SOCK_STREAM, 0); + if (server_sock < 0) { + fprintf(stderr, "%s: failed to create server socket: %s\n", + __FILE__, strerror(errno)); + fflush(stderr); + goto fail; + } + + on = 1; + if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(on)) < 0) { + fprintf(stderr, "%s: failed to set SO_REUSEADDR for server socket: %s\n", + __FILE__, strerror(errno)); + fflush(stderr); + goto fail; + } + + memset((void *) &addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = INADDR_ANY; + addr.sin_port = htons(DUK_DEBUG_PORT); + + if (bind(server_sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + fprintf(stderr, "%s: failed to bind server socket: %s\n", + __FILE__, strerror(errno)); + fflush(stderr); + goto fail; + } + + listen(server_sock, 1 /*backlog*/); + return; + + fail: + if (server_sock >= 0) { + (void) close(server_sock); + server_sock = -1; + } +} + +void duk_trans_socket_finish(void) { + if (client_sock >= 0) { + (void) close(client_sock); + client_sock = -1; + } + if (server_sock >= 0) { + (void) close(server_sock); + server_sock = -1; + } +} + +void duk_trans_socket_waitconn(void) { + struct sockaddr_in addr; + socklen_t sz; + + if (server_sock < 0) { + fprintf(stderr, "%s: no server socket, skip waiting for connection\n", + __FILE__); + fflush(stderr); + return; + } + if (client_sock >= 0) { + (void) close(client_sock); + client_sock = -1; + } + + fprintf(stderr, "Waiting for debug connection on port %d\n", (int) DUK_DEBUG_PORT); + fflush(stderr); + + sz = (socklen_t) sizeof(addr); + client_sock = accept(server_sock, (struct sockaddr *) &addr, &sz); + if (client_sock < 0) { + fprintf(stderr, "%s: accept() failed, skip waiting for connection: %s\n", + __FILE__, strerror(errno)); + fflush(stderr); + goto fail; + } + + fprintf(stderr, "Debug connection established\n"); + fflush(stderr); + + /* XXX: For now, close the listen socket because we won't accept new + * connections anyway. A better implementation would allow multiple + * debug attaches. + */ + + if (server_sock >= 0) { + (void) close(server_sock); + server_sock = -1; + } + return; + + fail: + if (client_sock >= 0) { + (void) close(client_sock); + client_sock = -1; + } +} + +/* + * Duktape callbacks + */ + +/* Duktape debug transport callback: (possibly partial) read. */ +duk_size_t duk_trans_socket_read_cb(void *udata, char *buffer, duk_size_t length) { + ssize_t ret; + + (void) udata; /* not needed by the example */ + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: udata=%p, buffer=%p, length=%ld\n", + __func__, (void *) udata, (void *) buffer, (long) length); + fflush(stderr); +#endif + + if (client_sock < 0) { + return 0; + } + + if (length == 0) { + /* This shouldn't happen. */ + fprintf(stderr, "%s: read request length == 0, closing connection\n", + __FILE__); + fflush(stderr); + goto fail; + } + + if (buffer == NULL) { + /* This shouldn't happen. */ + fprintf(stderr, "%s: read request buffer == NULL, closing connection\n", + __FILE__); + fflush(stderr); + goto fail; + } + + /* In a production quality implementation there would be a sanity + * timeout here to recover from "black hole" disconnects. + */ + + ret = read(client_sock, (void *) buffer, (size_t) length); + if (ret < 0) { + fprintf(stderr, "%s: debug read failed, closing connection: %s\n", + __FILE__, strerror(errno)); + fflush(stderr); + goto fail; + } else if (ret == 0) { + fprintf(stderr, "%s: debug read failed, ret == 0 (EOF), closing connection\n", + __FILE__); + fflush(stderr); + goto fail; + } else if (ret > (ssize_t) length) { + fprintf(stderr, "%s: debug read failed, ret too large (%ld > %ld), closing connection\n", + __FILE__, (long) ret, (long) length); + fflush(stderr); + goto fail; + } + + return (duk_size_t) ret; + + fail: + if (client_sock >= 0) { + (void) close(client_sock); + client_sock = -1; + } + return 0; +} + +/* Duktape debug transport callback: (possibly partial) write. */ +duk_size_t duk_trans_socket_write_cb(void *udata, const char *buffer, duk_size_t length) { + ssize_t ret; + + (void) udata; /* not needed by the example */ + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: udata=%p, buffer=%p, length=%ld\n", + __func__, (void *) udata, (const void *) buffer, (long) length); + fflush(stderr); +#endif + + if (client_sock < 0) { + return 0; + } + + if (length == 0) { + /* This shouldn't happen. */ + fprintf(stderr, "%s: write request length == 0, closing connection\n", + __FILE__); + fflush(stderr); + goto fail; + } + + if (buffer == NULL) { + /* This shouldn't happen. */ + fprintf(stderr, "%s: write request buffer == NULL, closing connection\n", + __FILE__); + fflush(stderr); + goto fail; + } + + /* In a production quality implementation there would be a sanity + * timeout here to recover from "black hole" disconnects. + */ + + ret = write(client_sock, (const void *) buffer, (size_t) length); + if (ret <= 0 || ret > (ssize_t) length) { + fprintf(stderr, "%s: debug write failed, closing connection: %s\n", + __FILE__, strerror(errno)); + fflush(stderr); + goto fail; + } + + return (duk_size_t) ret; + + fail: + if (client_sock >= 0) { + (void) close(client_sock); + client_sock = -1; + } + return 0; +} + +duk_size_t duk_trans_socket_peek_cb(void *udata) { + struct pollfd fds[1]; + int poll_rc; + + (void) udata; /* not needed by the example */ + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: udata=%p\n", __func__, (void *) udata); + fflush(stderr); +#endif + + if (client_sock < 0) { + return 0; + } + + fds[0].fd = client_sock; + fds[0].events = POLLIN; + fds[0].revents = 0; + + poll_rc = poll(fds, 1, 0); + if (poll_rc < 0) { + fprintf(stderr, "%s: poll returned < 0, closing connection: %s\n", + __FILE__, strerror(errno)); + fflush(stderr); + goto fail; /* also returns 0, which is correct */ + } else if (poll_rc > 1) { + fprintf(stderr, "%s: poll returned > 1, treating like 1\n", + __FILE__); + fflush(stderr); + return 1; /* should never happen */ + } else if (poll_rc == 0) { + return 0; /* nothing to read */ + } else { + return 1; /* something to read */ + } + + fail: + if (client_sock >= 0) { + (void) close(client_sock); + client_sock = -1; + } + return 0; +} + +void duk_trans_socket_read_flush_cb(void *udata) { + (void) udata; /* not needed by the example */ + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: udata=%p\n", __func__, (void *) udata); + fflush(stderr); +#endif + + /* Read flush: Duktape may not be making any more read calls at this + * time. If the transport maintains a receive window, it can use a + * read flush as a signal to update the window status to the remote + * peer. A read flush is guaranteed to occur before Duktape stops + * reading for a while; it may occur in other situations as well so + * it's not a 100% reliable indication. + */ + + /* This TCP transport requires no read flush handling so ignore. + * You can also pass a NULL to duk_debugger_attach() and not + * implement this callback at all. + */ +} + +void duk_trans_socket_write_flush_cb(void *udata) { + (void) udata; /* not needed by the example */ + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: udata=%p\n", __func__, (void *) udata); + fflush(stderr); +#endif + + /* Write flush. If the transport combines multiple writes + * before actually sending, a write flush is an indication + * to write out any pending bytes: Duktape may not be doing + * any more writes on this occasion. + */ + + /* This TCP transport requires no write flush handling so ignore. + * You can also pass a NULL to duk_debugger_attach() and not + * implement this callback at all. + */ + return; +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/duk_trans_socket_windows.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/duk_trans_socket_windows.c new file mode 100644 index 000000000..e92ac2660 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/debug-trans-socket/duk_trans_socket_windows.c @@ -0,0 +1,414 @@ +/* + * Example debug transport using a Windows TCP socket + * + * Provides a TCP server socket which a debug client can connect to. + * After that data is just passed through. + * + * https://msdn.microsoft.com/en-us/library/windows/desktop/ms737593(v=vs.85).aspx + * + * Compiling 'duk' with debugger support using MSVC (Visual Studio): + * + * > cl /W3 /O2 /Feduk.exe + * /DDUK_OPT_DEBUGGER_SUPPORT /DDUK_OPT_INTERRUPT_COUNTER + * /DDUK_CMDLINE_DEBUGGER_SUPPORT + * /Iexamples\debug-trans-socket /Isrc + * examples\cmdline\duk_cmdline.c + * examples\debug-trans-socket\duk_trans_socket_windows.c + * src\duktape.c + * + * With MinGW: + * + * $ gcc -oduk.exe -Wall -O2 \ + * -DDUK_OPT_DEBUGGER_SUPPORT -DDUK_OPT_INTERRUPT_COUNTER \ + * -DDUK_CMDLINE_DEBUGGER_SUPPORT \ + * -Iexamples/debug-trans-socket -Isrc \ + * examples/cmdline/duk_cmdline.c \ + * examples/debug-trans-socket/duk_trans_socket_windows.c \ + * src/duktape.c -lm -lws2_32 + */ + +#undef UNICODE +#if !defined(WIN32_LEAN_AND_MEAN) +#define WIN32_LEAN_AND_MEAN +#endif + +/* MinGW workaround for missing getaddrinfo() etc: + * http://programmingrants.blogspot.fi/2009/09/tips-on-undefined-reference-to.html + */ +#if defined(__MINGW32__) || defined(__MINGW64__) +#if !defined(_WIN32_WINNT) +#define _WIN32_WINNT 0x0501 +#endif +#endif + +#include +#include +#include +#include +#include +#include "duktape.h" + +#if defined(_MSC_VER) +#pragma comment (lib, "Ws2_32.lib") +#endif + +#if !defined(DUK_DEBUG_PORT) +#define DUK_DEBUG_PORT 9091 +#endif +#if !defined(DUK_DEBUG_ADDRESS) +#define DUK_DEBUG_ADDRESS "0.0.0.0" +#endif +#define DUK__STRINGIFY_HELPER(x) #x +#define DUK__STRINGIFY(x) DUK__STRINGIFY_HELPER(x) + +#if 0 +#define DEBUG_PRINTS +#endif + +static SOCKET server_sock = INVALID_SOCKET; +static SOCKET client_sock = INVALID_SOCKET; +static int wsa_inited = 0; + +/* + * Transport init and finish + */ + +void duk_trans_socket_init(void) { + WSADATA wsa_data; + struct addrinfo hints; + struct addrinfo *result = NULL; + int rc; + + memset((void *) &wsa_data, 0, sizeof(wsa_data)); + memset((void *) &hints, 0, sizeof(hints)); + + rc = WSAStartup(MAKEWORD(2, 2), &wsa_data); + if (rc != 0) { + fprintf(stderr, "%s: WSAStartup() failed: %d\n", __FILE__, rc); + fflush(stderr); + goto fail; + } + wsa_inited = 1; + + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + hints.ai_flags = AI_PASSIVE; + + rc = getaddrinfo(DUK_DEBUG_ADDRESS, DUK__STRINGIFY(DUK_DEBUG_PORT), &hints, &result); + if (rc != 0) { + fprintf(stderr, "%s: getaddrinfo() failed: %d\n", __FILE__, rc); + fflush(stderr); + goto fail; + } + + server_sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol); + if (server_sock == INVALID_SOCKET) { + fprintf(stderr, "%s: socket() failed with error: %ld\n", + __FILE__, (long) WSAGetLastError()); + fflush(stderr); + goto fail; + } + + rc = bind(server_sock, result->ai_addr, (int) result->ai_addrlen); + if (rc == SOCKET_ERROR) { + fprintf(stderr, "%s: bind() failed with error: %ld\n", + __FILE__, (long) WSAGetLastError()); + fflush(stderr); + goto fail; + } + + rc = listen(server_sock, SOMAXCONN); + if (rc == SOCKET_ERROR) { + fprintf(stderr, "%s: listen() failed with error: %ld\n", + __FILE__, (long) WSAGetLastError()); + fflush(stderr); + goto fail; + } + + if (result != NULL) { + freeaddrinfo(result); + result = NULL; + } + return; + + fail: + if (result != NULL) { + freeaddrinfo(result); + result = NULL; + } + if (server_sock != INVALID_SOCKET) { + (void) closesocket(server_sock); + server_sock = INVALID_SOCKET; + } + if (wsa_inited) { + WSACleanup(); + wsa_inited = 0; + } +} + +void duk_trans_socket_finish(void) { + if (client_sock != INVALID_SOCKET) { + (void) closesocket(client_sock); + client_sock = INVALID_SOCKET; + } + if (server_sock != INVALID_SOCKET) { + (void) closesocket(server_sock); + server_sock = INVALID_SOCKET; + } + if (wsa_inited) { + WSACleanup(); + wsa_inited = 0; + } +} + +void duk_trans_socket_waitconn(void) { + if (server_sock == INVALID_SOCKET) { + fprintf(stderr, "%s: no server socket, skip waiting for connection\n", + __FILE__); + fflush(stderr); + return; + } + if (client_sock != INVALID_SOCKET) { + (void) closesocket(client_sock); + client_sock = INVALID_SOCKET; + } + + fprintf(stderr, "Waiting for debug connection on port %d\n", (int) DUK_DEBUG_PORT); + fflush(stderr); + + client_sock = accept(server_sock, NULL, NULL); + if (client_sock == INVALID_SOCKET) { + fprintf(stderr, "%s: accept() failed with error %ld, skip waiting for connection\n", + __FILE__, (long) WSAGetLastError()); + fflush(stderr); + goto fail; + } + + fprintf(stderr, "Debug connection established\n"); + fflush(stderr); + + /* XXX: For now, close the listen socket because we won't accept new + * connections anyway. A better implementation would allow multiple + * debug attaches. + */ + + if (server_sock != INVALID_SOCKET) { + (void) closesocket(server_sock); + server_sock = INVALID_SOCKET; + } + return; + + fail: + if (client_sock != INVALID_SOCKET) { + (void) closesocket(client_sock); + client_sock = INVALID_SOCKET; + } +} + +/* + * Duktape callbacks + */ + +/* Duktape debug transport callback: (possibly partial) read. */ +duk_size_t duk_trans_socket_read_cb(void *udata, char *buffer, duk_size_t length) { + int ret; + + (void) udata; /* not needed by the example */ + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: udata=%p, buffer=%p, length=%ld\n", + __FUNCTION__, (void *) udata, (void *) buffer, (long) length); + fflush(stderr); +#endif + + if (client_sock == INVALID_SOCKET) { + return 0; + } + + if (length == 0) { + /* This shouldn't happen. */ + fprintf(stderr, "%s: read request length == 0, closing connection\n", + __FILE__); + fflush(stderr); + goto fail; + } + + if (buffer == NULL) { + /* This shouldn't happen. */ + fprintf(stderr, "%s: read request buffer == NULL, closing connection\n", + __FILE__); + fflush(stderr); + goto fail; + } + + /* In a production quality implementation there would be a sanity + * timeout here to recover from "black hole" disconnects. + */ + + ret = recv(client_sock, (void *) buffer, (int) length, 0); + if (ret < 0) { + fprintf(stderr, "%s: debug read failed, error %d, closing connection\n", + __FILE__, ret); + fflush(stderr); + goto fail; + } else if (ret == 0) { + fprintf(stderr, "%s: debug read failed, ret == 0 (EOF), closing connection\n", + __FILE__); + fflush(stderr); + goto fail; + } else if (ret > (int) length) { + fprintf(stderr, "%s: debug read failed, ret too large (%ld > %ld), closing connection\n", + __FILE__, (long) ret, (long) length); + fflush(stderr); + goto fail; + } + + return (duk_size_t) ret; + + fail: + if (client_sock != INVALID_SOCKET) { + (void) closesocket(client_sock); + client_sock = INVALID_SOCKET; + } + return 0; +} + +/* Duktape debug transport callback: (possibly partial) write. */ +duk_size_t duk_trans_socket_write_cb(void *udata, const char *buffer, duk_size_t length) { + int ret; + + (void) udata; /* not needed by the example */ + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: udata=%p, buffer=%p, length=%ld\n", + __FUNCTION__, (void *) udata, (const void *) buffer, (long) length); + fflush(stderr); +#endif + + if (client_sock == INVALID_SOCKET) { + return 0; + } + + if (length == 0) { + /* This shouldn't happen. */ + fprintf(stderr, "%s: write request length == 0, closing connection\n", + __FILE__); + fflush(stderr); + goto fail; + } + + if (buffer == NULL) { + /* This shouldn't happen. */ + fprintf(stderr, "%s: write request buffer == NULL, closing connection\n", + __FILE__); + fflush(stderr); + goto fail; + } + + /* In a production quality implementation there would be a sanity + * timeout here to recover from "black hole" disconnects. + */ + + ret = send(client_sock, (const void *) buffer, (int) length, 0); + if (ret <= 0 || ret > (int) length) { + fprintf(stderr, "%s: debug write failed, ret %d, closing connection\n", + __FILE__, ret); + fflush(stderr); + goto fail; + } + + return (duk_size_t) ret; + + fail: + if (client_sock != INVALID_SOCKET) { + (void) closesocket(INVALID_SOCKET); + client_sock = INVALID_SOCKET; + } + return 0; +} + +duk_size_t duk_trans_socket_peek_cb(void *udata) { + u_long avail; + int rc; + + (void) udata; /* not needed by the example */ + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: udata=%p\n", __FUNCTION__, (void *) udata); + fflush(stderr); +#endif + + if (client_sock == INVALID_SOCKET) { + return 0; + } + + avail = 0; + rc = ioctlsocket(client_sock, FIONREAD, &avail); + if (rc != 0) { + fprintf(stderr, "%s: ioctlsocket() returned %d, closing connection\n", + __FILE__, rc); + fflush(stderr); + goto fail; /* also returns 0, which is correct */ + } else { + if (avail == 0) { + return 0; /* nothing to read */ + } else { + return 1; /* something to read */ + } + } + /* never here */ + + fail: + if (client_sock != INVALID_SOCKET) { + (void) closesocket(client_sock); + client_sock = INVALID_SOCKET; + } + return 0; +} + +void duk_trans_socket_read_flush_cb(void *udata) { + (void) udata; /* not needed by the example */ + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: udata=%p\n", __FUNCTION__, (void *) udata); + fflush(stderr); +#endif + + /* Read flush: Duktape may not be making any more read calls at this + * time. If the transport maintains a receive window, it can use a + * read flush as a signal to update the window status to the remote + * peer. A read flush is guaranteed to occur before Duktape stops + * reading for a while; it may occur in other situations as well so + * it's not a 100% reliable indication. + */ + + /* This TCP transport requires no read flush handling so ignore. + * You can also pass a NULL to duk_debugger_attach() and not + * implement this callback at all. + */ +} + +void duk_trans_socket_write_flush_cb(void *udata) { + (void) udata; /* not needed by the example */ + +#if defined(DEBUG_PRINTS) + fprintf(stderr, "%s: udata=%p\n", __FUNCTION__, (void *) udata); + fflush(stderr); +#endif + + /* Write flush. If the transport combines multiple writes + * before actually sending, a write flush is an indication + * to write out any pending bytes: Duktape may not be doing + * any more writes on this occasion. + */ + + /* This TCP transport requires no write flush handling so ignore. + * You can also pass a NULL to duk_debugger_attach() and not + * implement this callback at all. + */ + return; +} + +#undef DUK__STRINGIFY_HELPER +#undef DUK__STRINGIFY diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/dummy-date-provider/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/dummy-date-provider/README.rst new file mode 100644 index 000000000..bc62779f1 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/dummy-date-provider/README.rst @@ -0,0 +1,5 @@ +==================================== +Dummy external Date provider example +==================================== + +This example implements a dummy, minimal external Date provider. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/dummy-date-provider/dummy_date_provider.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/dummy-date-provider/dummy_date_provider.c new file mode 100644 index 000000000..9c9e71638 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/dummy-date-provider/dummy_date_provider.c @@ -0,0 +1,27 @@ +/* + * Dummy Date provider + * + * There are two minimally required macros which you must provide in + * duk_config.h: + * + * extern duk_double_t dummy_get_now(void); + * + * #define DUK_USE_DATE_GET_NOW(ctx) dummy_get_now() + * #define DUK_USE_DATE_GET_LOCAL_TZOFFSET(d) 0 + * + * Note that since the providers are macros, you don't need to use + * all arguments. Similarly, you can "return" fixed values as + * constants. Above, local timezone offset is always zero i.e. + * we're always in UTC. + * + * You can also provide optional macros to parse and format timestamps + * in a platform specific format. If not provided, Duktape will use + * ISO 8601 only (which is often good enough). + */ + +#include "duktape.h" + +duk_double_t dummy_get_now(void) { + /* Return a fixed time here as a dummy example. */ + return -11504520000.0; +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/eval/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/eval/README.rst new file mode 100644 index 000000000..eed180648 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/eval/README.rst @@ -0,0 +1,5 @@ +============ +Eval example +============ + +Evaluate expressions from command line. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/eval/eval.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/eval/eval.c new file mode 100644 index 000000000..44099260d --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/eval/eval.c @@ -0,0 +1,48 @@ +/* + * Very simple example program for evaluating expressions from + * command line + */ + +#include "duktape.h" +#include + +static int eval_raw(duk_context *ctx) { + duk_eval(ctx); + return 1; +} + +static int tostring_raw(duk_context *ctx) { + duk_to_string(ctx, -1); + return 1; +} + +static void usage_exit(void) { + fprintf(stderr, "Usage: eval [] ...\n"); + fflush(stderr); + exit(1); +} + +int main(int argc, char *argv[]) { + duk_context *ctx; + int i; + const char *res; + + if (argc < 2) { + usage_exit(); + } + + ctx = duk_create_heap_default(); + for (i = 1; i < argc; i++) { + printf("=== eval: '%s' ===\n", argv[i]); + duk_push_string(ctx, argv[i]); + duk_safe_call(ctx, eval_raw, 1 /*nargs*/, 1 /*nrets*/); + duk_safe_call(ctx, tostring_raw, 1 /*nargs*/, 1 /*nrets*/); + res = duk_get_string(ctx, -1); + printf("%s\n", res ? res : "null"); + duk_pop(ctx); + } + + duk_destroy_heap(ctx); + + return 0; +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/README.rst new file mode 100644 index 000000000..5b1b14776 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/README.rst @@ -0,0 +1,76 @@ +================== +Eventloop examples +================== + +Overview and usage +================== + +A few examples on how an event loop can be implemented with Duktape, mainly +illlustrating how the Duktape interface works (not how event loops should be +built otherwise). + +To test (Linux only, perhaps other Unix):: + + $ make + $ ./evloop curses-timers.js # run with Ecmascript eventloop + $ ./evloop -c curses-timers.js # run with C eventloop + +Implementation approaches +========================= + +There are several approaches to implementation timers. Here we demonstrate +two main approaches: + +1. Using a C eventloop which calls into Javascript. All the event loop state + like timers, sockets, etc, is held in C structures. + (See ``c_eventloop.c`` and ``c_eventloop.js``.) + +2. Using an Ecmascript eventloop which never returns. All the event loop state + can be managed with Ecmascript code instead of C structures. The Ecmascript + eventloop calls a Duktape/C helper to do the lowest level poll() call. + (See ``ecma_eventloop.js``.) + +Services provided +================= + +The event loop API provided by both examples is the same, and includes: + +* Timers: setTimeout, clearTimeout, setInterval, clearInterval + +* Sockets: simple network sockets + +In addition there are a few synchronous API bindings which are not event loop +related: + +* File I/O + +* Curses, for doing beautiful character graphics + +Limitations +=========== + +This is **not** a production quality event loop. This is on purpose, to +keep the example somewhat simple. Some shortcomings include: + +* A production quality event loop would track its internal state (active + timers and sockets) much more efficiently. In general memory usage and + code footprint can be reduced. + +* Buffer churn caused by allocating a new buffer for every socket read + should be eliminated by reusing buffers where appropriate. Although + churn doesn't increase memory footprint with reference counting, it + is slower than reusing buffers and might increase memory fragmentation. + +* There is no way to suspend reading or writing in the example. Adding + them is straightforward: the poll set needs to be managed dynamically. + +* The example uses poll() while one should use epoll() on Linux, kqueue() + on BSD systems, etc. + +* Timers are not very accurate, e.g. setInterval() does not try to guarantee + a steady schedule. Instead, the next interval is scheduled after the + current callback has finished. This is not the best behavior for some + environments, but avoids bunching callbacks. + +* Error handling is mostly missing. Debug prints don't interact well + with curses. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/basic-test.js b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/basic-test.js new file mode 100644 index 000000000..04b339279 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/basic-test.js @@ -0,0 +1,17 @@ +/* + * A few basic tests + */ + +var count = 0; +var intervalId; + +setTimeout(function (x) { print('timer 1', x); }, 1234, 'foo'); +setTimeout('print("timer 2");', 4321); +setTimeout(function () { print('timer 3'); }, 2345); +intervalId = setInterval(function (x, y) { + print('interval', ++count, x, y); + if (count >= 10) { + clearInterval(intervalId); + } +}, 400, 'foo', 'bar'); + diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/c_eventloop.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/c_eventloop.c new file mode 100644 index 000000000..75d768b3a --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/c_eventloop.c @@ -0,0 +1,618 @@ +/* + * C eventloop example. + * + * Timer management is similar to eventloop.js but implemented in C. + * In particular, timer insertion is an O(n) operation; in a real world + * eventloop based on a heap insertion would be O(log N). + */ + +#include +#include +#include +#include +#include +#include + +#include "duktape.h" + +#define MAX_TIMERS 4096 /* this is quite excessive for embedded use, but good for testing */ +#define MIN_DELAY 1.0 +#define MIN_WAIT 1.0 +#define MAX_WAIT 60000.0 +#define MAX_EXPIRYS 10 + +#define MAX_FDS 256 + +typedef struct { + int64_t id; /* numeric ID (returned from e.g. setTimeout); zero if unused */ + double target; /* next target time */ + double delay; /* delay/interval */ + int oneshot; /* oneshot=1 (setTimeout), repeated=0 (setInterval) */ + int removed; /* timer has been requested for removal */ + + /* The callback associated with the timer is held in the "global stash", + * in .eventTimers[String(id)]. The references must be deleted + * when a timer struct is deleted. + */ +} ev_timer; + +/* Active timers. Dense list, terminates to end of list or first unused timer. + * The list is sorted by 'target', with lowest 'target' (earliest expiry) last + * in the list. When a timer's callback is being called, the timer is moved + * to 'timer_expiring' as it needs special handling should the user callback + * delete that particular timer. + */ +static ev_timer timer_list[MAX_TIMERS]; +static ev_timer timer_expiring; +static int timer_count; /* last timer at timer_count - 1 */ +static int64_t timer_next_id = 1; + +/* Socket poll state. */ +static struct pollfd poll_list[MAX_FDS]; +static int poll_count = 0; + +/* Misc */ +static int exit_requested = 0; + +/* Get Javascript compatible 'now' timestamp (millisecs since 1970). */ +static double get_now(void) { + struct timeval tv; + int rc; + + rc = gettimeofday(&tv, NULL); + if (rc != 0) { + /* Should never happen, so return whatever. */ + return 0.0; + } + return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0; +} + +static ev_timer *find_nearest_timer(void) { + /* Last timer expires first (list is always kept sorted). */ + if (timer_count <= 0) { + return NULL; + } + return timer_list + timer_count - 1; +} + +/* Bubble last timer on timer list backwards until it has been moved to + * its proper sorted position (based on 'target' time). + */ +static void bubble_last_timer(void) { + int i; + int n = timer_count; + ev_timer *t; + ev_timer tmp; + + for (i = n - 1; i > 0; i--) { + /* Timer to bubble is at index i, timer to compare to is + * at i-1 (both guaranteed to exist). + */ + t = timer_list + i; + if (t->target <= (t-1)->target) { + /* 't' expires earlier than (or same time as) 't-1', so we're done. */ + break; + } else { + /* 't' expires later than 't-1', so swap them and repeat. */ + memcpy((void *) &tmp, (void *) (t - 1), sizeof(ev_timer)); + memcpy((void *) (t - 1), (void *) t, sizeof(ev_timer)); + memcpy((void *) t, (void *) &tmp, sizeof(ev_timer)); + } + } +} + +static void expire_timers(duk_context *ctx) { + ev_timer *t; + int sanity = MAX_EXPIRYS; + double now; + int rc; + + /* Because a user callback can mutate the timer list (by adding or deleting + * a timer), we expire one timer and then rescan from the end again. There + * is a sanity limit on how many times we do this per expiry round. + */ + + duk_push_global_stash(ctx); + duk_get_prop_string(ctx, -1, "eventTimers"); + + /* [ ... stash eventTimers ] */ + + now = get_now(); + while (sanity-- > 0) { + /* + * If exit has been requested, exit without running further + * callbacks. + */ + + if (exit_requested) { +#if 0 + fprintf(stderr, "exit requested, exiting timer expiry loop\n"); + fflush(stderr); +#endif + break; + } + + /* + * Expired timer(s) still exist? + */ + + if (timer_count <= 0) { + break; + } + t = timer_list + timer_count - 1; + if (t->target > now) { + break; + } + + /* + * Move the timer to 'expiring' for the duration of the callback. + * Mark a one-shot timer deleted, compute a new target for an interval. + */ + + memcpy((void *) &timer_expiring, (void *) t, sizeof(ev_timer)); + memset((void *) t, 0, sizeof(ev_timer)); + timer_count--; + t = &timer_expiring; + + if (t->oneshot) { + t->removed = 1; + } else { + t->target = now + t->delay; /* XXX: or t->target + t->delay? */ + } + + /* + * Call timer callback. The callback can operate on the timer list: + * add new timers, remove timers. The callback can even remove the + * expired timer whose callback we're calling. However, because the + * timer being expired has been moved to 'timer_expiring', we don't + * need to worry about the timer's offset changing on the timer list. + */ + +#if 0 + fprintf(stderr, "calling user callback for timer id %d\n", (int) t->id); + fflush(stderr); +#endif + + duk_push_number(ctx, (double) t->id); + duk_get_prop(ctx, -2); /* -> [ ... stash eventTimers func ] */ + rc = duk_pcall(ctx, 0 /*nargs*/); /* -> [ ... stash eventTimers retval ] */ + if (rc != 0) { +#if 0 + fprintf(stderr, "timer callback failed for timer %d: %s\n", (int) t->id, duk_to_string(ctx, -1)); + fflush(stderr); +#endif + } + duk_pop(ctx); /* ignore errors for now -> [ ... stash eventTimers ] */ + + if (t->removed) { + /* One-shot timer (always removed) or removed by user callback. */ +#if 0 + fprintf(stderr, "deleting callback state for timer %d\n", (int) t->id); + fflush(stderr); +#endif + duk_push_number(ctx, (double) t->id); + duk_del_prop(ctx, -2); + } else { + /* Interval timer, not removed by user callback. Queue back to + * timer list and bubble to its final sorted position. + */ +#if 0 + fprintf(stderr, "queueing timer %d back into active list\n", (int) t->id); + fflush(stderr); +#endif + if (timer_count >= MAX_TIMERS) { + duk_error(ctx, DUK_ERR_RANGE_ERROR, "out of timer slots"); + } + memcpy((void *) (timer_list + timer_count), (void *) t, sizeof(ev_timer)); + timer_count++; + bubble_last_timer(); + } + } + + memset((void *) &timer_expiring, 0, sizeof(ev_timer)); + + duk_pop_2(ctx); /* -> [ ... ] */ +} + +static void compact_poll_list(void) { + int i, j, n; + + /* i = input index + * j = output index (initially same as i) + */ + + n = poll_count; + for (i = 0, j = 0; i < n; i++) { + struct pollfd *pfd = poll_list + i; + if (pfd->fd == 0) { + /* keep output index the same */ +#if 0 + fprintf(stderr, "remove pollfd (index %d): fd=%d, events=%d, revents=%d\n", + i, pfd->fd, pfd->events, pfd->revents), + fflush(stderr); +#endif + + continue; + } +#if 0 + fprintf(stderr, "keep pollfd (index %d -> %d): fd=%d, events=%d, revents=%d\n", + i, j, pfd->fd, pfd->events, pfd->revents), + fflush(stderr); +#endif + if (i != j) { + /* copy only if indices have diverged */ + memcpy((void *) (poll_list + j), (void *) (poll_list + i), sizeof(struct pollfd)); + } + j++; + } + + if (j < poll_count) { + /* zeroize unused entries for sanity */ + memset((void *) (poll_list + j), 0, (poll_count - j) * sizeof(struct pollfd)); + } + + poll_count = j; +} + +int eventloop_run(duk_context *ctx) { + ev_timer *t; + double now; + double diff; + int timeout; + int rc; + int i, n; + int idx_eventloop; + int idx_fd_handler; + + /* The Ecmascript poll handler is passed through EventLoop.fdPollHandler + * which c_eventloop.js sets before we come here. + */ + duk_push_global_object(ctx); + duk_get_prop_string(ctx, -1, "EventLoop"); + duk_get_prop_string(ctx, -1, "fdPollHandler"); /* -> [ global EventLoop fdPollHandler ] */ + idx_fd_handler = duk_get_top_index(ctx); + idx_eventloop = idx_fd_handler - 1; + + for (;;) { + /* + * Expire timers. + */ + + expire_timers(ctx); + + /* + * If exit requested, bail out as fast as possible. + */ + + if (exit_requested) { +#if 0 + fprintf(stderr, "exit requested, exiting event loop\n"); + fflush(stderr); +#endif + break; + } + + /* + * Compact poll list by removing pollfds with fd == 0. + */ + + compact_poll_list(); + + /* + * Determine poll() timeout (as close to poll() as possible as + * the wait is relative). + */ + + now = get_now(); + t = find_nearest_timer(); + if (t) { + diff = t->target - now; + if (diff < MIN_WAIT) { + diff = MIN_WAIT; + } else if (diff > MAX_WAIT) { + diff = MAX_WAIT; + } + timeout = (int) diff; /* clamping ensures that fits */ + } else { + if (poll_count == 0) { +#if 0 + fprintf(stderr, "no timers and no sockets to poll, exiting\n"); + fflush(stderr); +#endif + break; + } + timeout = (int) MAX_WAIT; + } + + /* + * Poll for activity or timeout. + */ + +#if 0 + fprintf(stderr, "going to poll, timeout %d ms, pollfd count %d\n", timeout, poll_count); + fflush(stderr); +#endif + + rc = poll(poll_list, poll_count, timeout); +#if 0 + fprintf(stderr, "poll rc: %d\n", rc); + fflush(stderr); +#endif + if (rc < 0) { + /* error */ + } else if (rc == 0) { + /* timeout */ + } else { + /* 'rc' fds active */ + } + + /* + * Check socket activity, handle all sockets. Handling is offloaded to + * Ecmascript code (fd + revents). + * + * If FDs are removed from the poll list while we're processing callbacks, + * the entries are simply marked unused (fd set to 0) without actually + * removing them from the poll list. This ensures indices are not + * disturbed. The poll list is compacted before next poll(). + */ + + n = (rc == 0 ? 0 : poll_count); /* if timeout, no need to check pollfd */ + for (i = 0; i < n; i++) { + struct pollfd *pfd = poll_list + i; + + if (pfd->fd == 0) { + /* deleted, perhaps by previous callback */ + continue; + } + + if (pfd->revents) { +#if 0 + fprintf(stderr, "fd %d has revents: %d\n", (int) pfd->fd, (int) pfd->revents); + fflush(stderr); +#endif + duk_dup(ctx, idx_fd_handler); + duk_dup(ctx, idx_eventloop); + duk_push_int(ctx, pfd->fd); + duk_push_int(ctx, pfd->revents); + rc = duk_pcall_method(ctx, 2 /*nargs*/); + if (rc) { +#if 0 + fprintf(stderr, "fd callback failed for fd %d: %s\n", (int) pfd->fd, duk_to_string(ctx, -1)); + fflush(stderr); +#endif + } + duk_pop(ctx); + + pfd->revents = 0; + } + + } + } + + duk_pop_n(ctx, 3); + + return 0; +} + +static int create_timer(duk_context *ctx) { + double delay; + int oneshot; + int idx; + int64_t timer_id; + double now; + ev_timer *t; + + now = get_now(); + + /* indexes: + * 0 = function (callback) + * 1 = delay + * 2 = boolean: oneshot + */ + + delay = duk_require_number(ctx, 1); + if (delay < MIN_DELAY) { + delay = MIN_DELAY; + } + oneshot = duk_require_boolean(ctx, 2); + + if (timer_count >= MAX_TIMERS) { + duk_error(ctx, DUK_ERR_RANGE_ERROR, "out of timer slots"); + } + idx = timer_count++; + timer_id = timer_next_id++; + t = timer_list + idx; + + memset((void *) t, 0, sizeof(ev_timer)); + t->id = timer_id; + t->target = now + delay; + t->delay = delay; + t->oneshot = oneshot; + t->removed = 0; + + /* Timer is now at the last position; use swaps to "bubble" it to its + * correct sorted position. + */ + + bubble_last_timer(); + + /* Finally, register the callback to the global stash 'eventTimers' object. */ + + duk_push_global_stash(ctx); + duk_get_prop_string(ctx, -1, "eventTimers"); /* -> [ func delay oneshot stash eventTimers ] */ + duk_push_number(ctx, (double) timer_id); + duk_dup(ctx, 0); + duk_put_prop(ctx, -3); /* eventTimers[timer_id] = callback */ + + /* Return timer id. */ + + duk_push_number(ctx, (double) timer_id); +#if 0 + fprintf(stderr, "created timer id: %d\n", (int) timer_id); + fflush(stderr); +#endif + return 1; +} + +static int delete_timer(duk_context *ctx) { + int i, n; + int64_t timer_id; + ev_timer *t; + int found = 0; + + /* indexes: + * 0 = timer id + */ + + timer_id = (int64_t) duk_require_number(ctx, 0); + + /* + * Unlike insertion, deletion needs a full scan of the timer list + * and an expensive remove. If no match is found, nothing is deleted. + * Caller gets a boolean return code indicating match. + * + * When a timer is being expired and its user callback is running, + * the timer has been moved to 'timer_expiring' and its deletion + * needs special handling: just mark it to-be-deleted and let the + * expiry code remove it. + */ + + t = &timer_expiring; + if (t->id == timer_id) { + t->removed = 1; + duk_push_true(ctx); +#if 0 + fprintf(stderr, "deleted expiring timer id: %d\n", (int) timer_id); + fflush(stderr); +#endif + return 1; + } + + n = timer_count; + for (i = 0; i < n; i++) { + t = timer_list + i; + if (t->id == timer_id) { + found = 1; + + /* Shift elements downwards to keep the timer list dense + * (no need if last element). + */ + if (i < timer_count - 1) { + memmove((void *) t, (void *) (t + 1), (timer_count - i - 1) * sizeof(ev_timer)); + } + + /* Zero last element for clarity. */ + memset((void *) (timer_list + n - 1), 0, sizeof(ev_timer)); + + /* Update timer_count. */ + timer_count--; + + /* The C state is now up-to-date, but we still need to delete + * the timer callback state from the global 'stash'. + */ + + duk_push_global_stash(ctx); + duk_get_prop_string(ctx, -1, "eventTimers"); /* -> [ timer_id stash eventTimers ] */ + duk_push_number(ctx, (double) timer_id); + duk_del_prop(ctx, -2); /* delete eventTimers[timer_id] */ + +#if 0 + fprintf(stderr, "deleted timer id: %d\n", (int) timer_id); + fflush(stderr); +#endif + break; + } + } + +#if 0 + if (!found) { + fprintf(stderr, "trying to delete timer id %d, but not found; ignoring\n", (int) timer_id); + fflush(stderr); + } +#endif + + duk_push_boolean(ctx, found); + return 1; +} + +static int listen_fd(duk_context *ctx) { + int fd = duk_require_int(ctx, 0); + int events = duk_require_int(ctx, 1); + int i, n; + struct pollfd *pfd; + +#if 0 + fprintf(stderr, "listen_fd: fd=%d, events=%d\n", fd, events); + fflush(stderr); +#endif + /* events == 0 means stop listening to the FD */ + + n = poll_count; + for (i = 0; i < n; i++) { + pfd = poll_list + i; + if (pfd->fd == fd) { +#if 0 + fprintf(stderr, "listen_fd: fd found at index %d\n", i); + fflush(stderr); +#endif + if (events == 0) { + /* mark to-be-deleted, cleaned up by next poll */ + pfd->fd = 0; + } else { + pfd->events = events; + } + return 0; + } + } + + /* not found, append to list */ +#if 0 + fprintf(stderr, "listen_fd: fd not found on list, add new entry\n"); + fflush(stderr); +#endif + + if (poll_count >= MAX_FDS) { + duk_error(ctx, DUK_ERR_ERROR, "out of fd slots"); + } + + pfd = poll_list + poll_count; + pfd->fd = fd; + pfd->events = events; + pfd->revents = 0; + poll_count++; + + return 0; +} + +static int request_exit(duk_context *ctx) { + (void) ctx; + exit_requested = 1; + return 0; +} + +static duk_function_list_entry eventloop_funcs[] = { + { "createTimer", create_timer, 3 }, + { "deleteTimer", delete_timer, 1 }, + { "listenFd", listen_fd, 2 }, + { "requestExit", request_exit, 0 }, + { NULL, NULL, 0 } +}; + +void eventloop_register(duk_context *ctx) { + memset((void *) timer_list, 0, MAX_TIMERS * sizeof(ev_timer)); + memset((void *) &timer_expiring, 0, sizeof(ev_timer)); + memset((void *) poll_list, 0, MAX_FDS * sizeof(struct pollfd)); + + /* Set global 'EventLoop'. */ + duk_push_global_object(ctx); + duk_push_object(ctx); + duk_put_function_list(ctx, -1, eventloop_funcs); + duk_put_prop_string(ctx, -2, "EventLoop"); + duk_pop(ctx); + + /* Initialize global stash 'eventTimers'. */ + duk_push_global_stash(ctx); + duk_push_object(ctx); + duk_put_prop_string(ctx, -2, "eventTimers"); + duk_pop(ctx); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/c_eventloop.js b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/c_eventloop.js new file mode 100644 index 000000000..b9e2d6339 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/c_eventloop.js @@ -0,0 +1,179 @@ +/* + * C eventloop example (c_eventloop.c). + * + * Ecmascript code to initialize the exposed API (setTimeout() etc) when + * using the C eventloop. + * + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Timers + */ + +/* + * Timer API + */ + +function setTimeout(func, delay) { + var cb_func; + var bind_args; + var timer_id; + + if (typeof delay !== 'number') { + throw new TypeError('delay is not a number'); + } + + if (typeof func === 'string') { + // Legacy case: callback is a string. + cb_func = eval.bind(this, func); + } else if (typeof func !== 'function') { + throw new TypeError('callback is not a function/string'); + } else if (arguments.length > 2) { + // Special case: callback arguments are provided. + bind_args = Array.prototype.slice.call(arguments, 2); // [ arg1, arg2, ... ] + bind_args.unshift(this); // [ global(this), arg1, arg2, ... ] + cb_func = func.bind.apply(func, bind_args); + } else { + // Normal case: callback given as a function without arguments. + cb_func = func; + } + + timer_id = EventLoop.createTimer(cb_func, delay, true /*oneshot*/); + + return timer_id; +} + +function clearTimeout(timer_id) { + if (typeof timer_id !== 'number') { + throw new TypeError('timer ID is not a number'); + } + var success = EventLoop.deleteTimer(timer_id); /* retval ignored */ +} + +function setInterval(func, delay) { + var cb_func; + var bind_args; + var timer_id; + + if (typeof delay !== 'number') { + throw new TypeError('delay is not a number'); + } + + if (typeof func === 'string') { + // Legacy case: callback is a string. + cb_func = eval.bind(this, func); + } else if (typeof func !== 'function') { + throw new TypeError('callback is not a function/string'); + } else if (arguments.length > 2) { + // Special case: callback arguments are provided. + bind_args = Array.prototype.slice.call(arguments, 2); // [ arg1, arg2, ... ] + bind_args.unshift(this); // [ global(this), arg1, arg2, ... ] + cb_func = func.bind.apply(func, bind_args); + } else { + // Normal case: callback given as a function without arguments. + cb_func = func; + } + + timer_id = EventLoop.createTimer(cb_func, delay, false /*oneshot*/); + + return timer_id; +} + +function clearInterval(timer_id) { + if (typeof timer_id !== 'number') { + throw new TypeError('timer ID is not a number'); + } + EventLoop.deleteTimer(timer_id); +} + +function requestEventLoopExit() { + EventLoop.requestExit(); +} + +/* + * Socket handling + * + * Ideally this would be implemented more in C than here for more speed + * and smaller footprint: C code would directly maintain the callback state + * and such. + * + * Also for more optimal I/O, the buffer churn caused by allocating and + * freeing a lot of buffer values could be eliminated by reusing buffers. + * Socket reads would then go into a pre-allocated buffer, for instance. + */ + +EventLoop.socketListening = {}; +EventLoop.socketReading = {}; +EventLoop.socketConnecting = {}; + +EventLoop.fdPollHandler = function(fd, revents) { + var data; + var cb; + var rc; + var acc_res; + + //print('activity on fd', fd, 'revents', revents); + + if (revents & Poll.POLLIN) { + cb = this.socketReading[fd]; + if (cb) { + data = Socket.read(fd); // no size control now + //print('READ', Duktape.enc('jx', data)); + if (data.length === 0) { + this.close(fd); + return; + } + cb(fd, data); + } else { + cb = this.socketListening[fd]; + if (cb) { + acc_res = Socket.accept(fd); + //print('ACCEPT:', Duktape.enc('jx', acc_res)); + cb(acc_res.fd, acc_res.addr, acc_res.port); + } else { + //print('UNKNOWN'); + } + } + } + + if (revents & Poll.POLLOUT) { + // Connected + cb = this.socketConnecting[fd]; + if (cb) { + delete this.socketConnecting[fd]; + cb(fd); + } + } + + if ((revents & ~(Poll.POLLIN | Poll.POLLOUT)) !== 0) { + //print('unexpected revents, close fd'); + this.close(fd); + } +} + +EventLoop.server = function(address, port, cb_accepted) { + var fd = Socket.createServerSocket(address, port); + this.socketListening[fd] = cb_accepted; + this.listenFd(fd, Poll.POLLIN); +} + +EventLoop.connect = function(address, port, cb_connected) { + var fd = Socket.connect(address, port); + this.socketConnecting[fd] = cb_connected; + this.listenFd(fd, Poll.POLLOUT); +} + +EventLoop.close = function(fd) { + EventLoop.listenFd(fd, 0); + delete this.socketListening[fd]; + delete this.socketReading[fd]; + delete this.socketConnecting[fd]; + Socket.close(fd); +} + +EventLoop.setReader = function(fd, cb_read) { + this.socketReading[fd] = cb_read; + this.listenFd(fd, Poll.POLLIN); +} + +EventLoop.write = function(fd, data) { + // This simple example doesn't have support for write blocking / draining + var rc = Socket.write(fd, Duktape.Buffer(data)); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/client-socket-test.js b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/client-socket-test.js new file mode 100644 index 000000000..ff8778486 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/client-socket-test.js @@ -0,0 +1,24 @@ + +var HOST = 'localhost'; +var PORT = 80; +var EXIT_TIMEOUT = 300e3; + +print('automatic exit after ' + (EXIT_TIMEOUT / 1e3) + ' seconds'); +setTimeout(function () { + print('exit timer'); + EventLoop.requestExit(); +}, EXIT_TIMEOUT); + +EventLoop.connect(HOST, PORT, function (fd) { + print('connected to ' + HOST + ':' + PORT + ', fd', fd); + EventLoop.setReader(fd, function (fd, data) { + print('read from fd', fd); + print(data); + EventLoop.close(fd); + }); + EventLoop.write(fd, "GET / HTTP/1.1\r\n" + + "Host: " + HOST + "\r\n" + + "User-Agent: client-socket-test.js\r\n" + + "\r\n"); +}); + diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/curses-timers.js b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/curses-timers.js new file mode 100644 index 000000000..450866539 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/curses-timers.js @@ -0,0 +1,79 @@ +/* + * Test using timers and intervals with curses. + */ + +if (typeof Ncurses !== 'object') { + throw new Error('Ncurses required'); +} + +function fillScreen(ch) { + var size, w, h; + var i, j; + + size = Ncurses.getmaxyx(); + h = size[0]; + w = size[1]; + + for (i = 0; i < h; i++) { + for (j = 0; j < w; j++) { + Ncurses.mvprintw(i, j, ch); + } + } + Ncurses.refresh(); +} + +function main() { + var i, j; + var counters = []; + var size, w, h; + + Ncurses.initscr(); + size = Ncurses.getmaxyx(); + h = size[0]; + w = size[1]; + + fillScreen('.'); + + setInterval(function () { + Ncurses.mvprintw(1, 4, new Date().toISOString()); + Ncurses.refresh(); + }, 1000); + + function addCounter(row, index, interval) { + counters[index] = 0; + setInterval(function () { + counters[index]++; + Ncurses.mvprintw(row, 4, '' + Date.now() + ' ' + counters[index]); + Ncurses.refresh(); + }, interval); + } + + function addRandomChar(row, col, interval) { + setTimeout(function () { + Ncurses.mvprintw(row, col, String.fromCharCode(Math.random() * 64 + 0x20)); + Ncurses.refresh(); + }, interval); + } + + for (i = 0; i < h - 5; i++) { + addCounter(3 + i, i, 363 * i + 400); + } + + /* Here the inserts take a lot of time because the underlying timer manager + * data structure has O(n) insertion performance. + */ + for (i = 0; i < h - 5; i++) { + for (j = 0; j < w - 50; j++) { + // Math.exp(0)...Math.exp(8) is an uneven distribution between 1...~2980. + addRandomChar(3 + i, 28 + j, 58000 - Math.exp(Math.random() * 8) * 20); + } + } + + setTimeout(function () { + Ncurses.endwin(); + Ncurses.delscreen(); + requestEventLoopExit(); + }, 120e3); +} + +main(); diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/ecma_eventloop.js b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/ecma_eventloop.js new file mode 100644 index 000000000..bad4e4d90 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/ecma_eventloop.js @@ -0,0 +1,466 @@ +/* + * Pure Ecmascript eventloop example. + * + * Timer state handling is inefficient in this trivial example. Timers are + * kept in an array sorted by their expiry time which works well for expiring + * timers, but has O(n) insertion performance. A better implementation would + * use a heap or some other efficient structure for managing timers so that + * all operations (insert, remove, get nearest timer) have good performance. + * + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Timers + */ + +/* + * Event loop + * + * Timers are sorted by 'target' property which indicates expiry time of + * the timer. The timer expiring next is last in the array, so that + * removals happen at the end, and inserts for timers expiring in the + * near future displace as few elements in the array as possible. + */ + +EventLoop = { + // timers + timers: [], // active timers, sorted (nearest expiry last) + expiring: null, // set to timer being expired (needs special handling in clearTimeout/clearInterval) + nextTimerId: 1, + minimumDelay: 1, + minimumWait: 1, + maximumWait: 60000, + maxExpirys: 10, + + // sockets + socketListening: {}, // fd -> callback + socketReading: {}, // fd -> callback + socketConnecting: {}, // fd -> callback + + // misc + exitRequested: false +}; + +EventLoop.dumpState = function() { + print('TIMER STATE:'); + this.timers.forEach(function(t) { + print(' ' + Duktape.enc('jx', t)); + }); + if (this.expiring) { + print(' EXPIRING: ' + Duktape.enc('jx', this.expiring)); + } +} + +// Get timer with lowest expiry time. Since the active timers list is +// sorted, it's always the last timer. +EventLoop.getEarliestTimer = function() { + var timers = this.timers; + n = timers.length; + return (n > 0 ? timers[n - 1] : null); +} + +EventLoop.getEarliestWait = function() { + var t = this.getEarliestTimer(); + return (t ? t.target - Date.now() : null); +} + +EventLoop.insertTimer = function(timer) { + var timers = this.timers; + var i, n, t; + + /* + * Find 'i' such that we want to insert *after* timers[i] at index i+1. + * If no such timer, for-loop terminates with i-1, and we insert at -1+1=0. + */ + + n = timers.length; + for (i = n - 1; i >= 0; i--) { + t = timers[i]; + if (timer.target <= t.target) { + // insert after 't', to index i+1 + break; + } + } + + timers.splice(i + 1 /*start*/, 0 /*deleteCount*/, timer); +} + +// Remove timer/interval with a timer ID. The timer/interval can reside +// either on the active list or it may be an expired timer (this.expiring) +// whose user callback we're running when this function gets called. +EventLoop.removeTimerById = function(timer_id) { + var timers = this.timers; + var i, n, t; + + t = this.expiring; + if (t) { + if (t.id === timer_id) { + // Timer has expired and we're processing its callback. User + // callback has requested timer deletion. Mark removed, so + // that the timer is not reinserted back into the active list. + // This is actually a common case because an interval may very + // well cancel itself. + t.removed = true; + return; + } + } + + n = timers.length; + for (i = 0; i < n; i++) { + t = timers[i]; + if (t.id === timer_id) { + // Timer on active list: mark removed (not really necessary, but + // nice for dumping), and remove from active list. + t.removed = true; + this.timers.splice(i /*start*/, 1 /*deleteCount*/); + return; + } + } + + // no such ID, ignore +} + +EventLoop.processTimers = function() { + var now = Date.now(); + var timers = this.timers; + var sanity = this.maxExpirys; + var n, t; + + /* + * Here we must be careful with mutations: user callback may add and + * delete an arbitrary number of timers. + * + * Current solution is simple: check whether the timer at the end of + * the list has expired. If not, we're done. If it has expired, + * remove it from the active list, record it in this.expiring, and call + * the user callback. If user code deletes the this.expiring timer, + * there is special handling which just marks the timer deleted so + * it won't get inserted back into the active list. + * + * This process is repeated at most maxExpirys times to ensure we don't + * get stuck forever; user code could in principle add more and more + * already expired timers. + */ + + while (sanity-- > 0) { + // If exit requested, don't call any more callbacks. This allows + // a callback to do cleanups and request exit, and can be sure that + // no more callbacks are processed. + + if (this.exitRequested) { + //print('exit requested, exit'); + break; + } + + // Timers to expire? + + n = timers.length; + if (n <= 0) { + break; + } + t = timers[n - 1]; + if (now <= t.target) { + // Timer has not expired, and no other timer could have expired + // either because the list is sorted. + break; + } + timers.pop(); + + // Remove the timer from the active list and process it. The user + // callback may add new timers which is not a problem. The callback + // may also delete timers which is not a problem unless the timer + // being deleted is the timer whose callback we're running; this is + // why the timer is recorded in this.expiring so that clearTimeout() + // and clearInterval() can detect this situation. + + if (t.oneshot) { + t.removed = true; // flag for removal + } else { + t.target = now + t.delay; + } + this.expiring = t; + try { + t.cb(); + } catch (e) { + print('timer callback failed, ignored: ' + e); + } + this.expiring = null; + + // If the timer was one-shot, it's marked 'removed'. If the user callback + // requested deletion for the timer, it's also marked 'removed'. If the + // timer is an interval (and is not marked removed), insert it back into + // the timer list. + + if (!t.removed) { + // Reinsert interval timer to correct sorted position. The timer + // must be an interval timer because one-shot timers are marked + // 'removed' above. + this.insertTimer(t); + } + } +} + +EventLoop.run = function() { + var wait; + var POLLIN = Poll.POLLIN; + var POLLOUT = Poll.POLLOUT; + var poll_set; + var poll_count; + var fd; + var t, rev; + var rc; + var acc_res; + + for (;;) { + /* + * Process expired timers. + */ + + this.processTimers(); + //this.dumpState(); + + /* + * Exit check (may be requested by a user callback) + */ + + if (this.exitRequested) { + //print('exit requested, exit'); + break; + } + + /* + * Create poll socket list. This is a very naive approach. + * On Linux, one could use e.g. epoll() and manage socket lists + * incrementally. + */ + + poll_set = {}; + poll_count = 0; + for (fd in this.socketListening) { + poll_set[fd] = { events: POLLIN, revents: 0 }; + poll_count++; + } + for (fd in this.socketReading) { + poll_set[fd] = { events: POLLIN, revents: 0 }; + poll_count++; + } + for (fd in this.socketConnecting) { + poll_set[fd] = { events: POLLOUT, revents: 0 }; + poll_count++; + } + //print(new Date(), 'poll_set IN:', Duktape.enc('jx', poll_set)); + + /* + * Wait timeout for timer closest to expiry. Since the poll + * timeout is relative, get this as close to poll() as possible. + */ + + wait = this.getEarliestWait(); + if (wait === null) { + if (poll_count === 0) { + print('no active timers and no sockets to poll, exit'); + break; + } else { + wait = this.maximumWait; + } + } else { + wait = Math.min(this.maximumWait, Math.max(this.minimumWait, wait)); + } + + /* + * Do the actual poll. + */ + + try { + Poll.poll(poll_set, wait); + } catch (e) { + // Eat errors silently. When resizing curses window an EINTR + // happens now. + } + + /* + * Process all sockets so that nothing is left unhandled for the + * next round. + */ + + //print(new Date(), 'poll_set OUT:', Duktape.enc('jx', poll_set)); + for (fd in poll_set) { + t = poll_set[fd]; + rev = t.revents; + + if (rev & POLLIN) { + cb = this.socketReading[fd]; + if (cb) { + data = Socket.read(fd); // no size control now + //print('READ', Duktape.enc('jx', data)); + if (data.length === 0) { + //print('zero read for fd ' + fd + ', closing forcibly'); + rc = Socket.close(fd); // ignore result + delete this.socketListening[fd]; + delete this.socketReading[fd]; + } else { + cb(fd, data); + } + } else { + cb = this.socketListening[fd]; + if (cb) { + acc_res = Socket.accept(fd); + //print('ACCEPT:', Duktape.enc('jx', acc_res)); + cb(acc_res.fd, acc_res.addr, acc_res.port); + } else { + //print('UNKNOWN'); + } + } + } + + if (rev & POLLOUT) { + cb = this.socketConnecting[fd]; + if (cb) { + delete this.socketConnecting[fd]; + cb(fd); + } else { + //print('UNKNOWN POLLOUT'); + } + } + + if ((rev & ~(POLLIN | POLLOUT)) !== 0) { + //print('revents ' + t.revents + ' for fd ' + fd + ', closing forcibly'); + rc = Socket.close(fd); // ignore result + delete this.socketListening[fd]; + delete this.socketReading[fd]; + } + } + } +} + +EventLoop.requestExit = function() { + this.exitRequested = true; +} + +EventLoop.server = function(address, port, cb_accepted) { + var fd = Socket.createServerSocket(address, port); + this.socketListening[fd] = cb_accepted; +} + +EventLoop.connect = function(address, port, cb_connected) { + var fd = Socket.connect(address, port); + this.socketConnecting[fd] = cb_connected; +} + +EventLoop.close = function(fd) { + delete this.socketReading[fd]; + delete this.socketListening[fd]; +} + +EventLoop.setReader = function(fd, cb_read) { + this.socketReading[fd] = cb_read; +} + +EventLoop.write = function(fd, data) { + // This simple example doesn't have support for write blocking / draining + var rc = Socket.write(fd, Duktape.Buffer(data)); +} + +/* + * Timer API + * + * These interface with the singleton EventLoop. + */ + +function setTimeout(func, delay) { + var cb_func; + var bind_args; + var timer_id; + var evloop = EventLoop; + + if (typeof delay !== 'number') { + throw new TypeError('delay is not a number'); + } + delay = Math.max(evloop.minimumDelay, delay); + + if (typeof func === 'string') { + // Legacy case: callback is a string. + cb_func = eval.bind(this, func); + } else if (typeof func !== 'function') { + throw new TypeError('callback is not a function/string'); + } else if (arguments.length > 2) { + // Special case: callback arguments are provided. + bind_args = Array.prototype.slice.call(arguments, 2); // [ arg1, arg2, ... ] + bind_args.unshift(this); // [ global(this), arg1, arg2, ... ] + cb_func = func.bind.apply(func, bind_args); + } else { + // Normal case: callback given as a function without arguments. + cb_func = func; + } + + timer_id = evloop.nextTimerId++; + + evloop.insertTimer({ + id: timer_id, + oneshot: true, + cb: cb_func, + delay: delay, + target: Date.now() + delay + }); + + return timer_id; +} + +function clearTimeout(timer_id) { + var evloop = EventLoop; + + if (typeof timer_id !== 'number') { + throw new TypeError('timer ID is not a number'); + } + evloop.removeTimerById(timer_id); +} + +function setInterval(func, delay) { + var cb_func; + var bind_args; + var timer_id; + var evloop = EventLoop; + + if (typeof delay !== 'number') { + throw new TypeError('delay is not a number'); + } + delay = Math.max(evloop.minimumDelay, delay); + + if (typeof func === 'string') { + // Legacy case: callback is a string. + cb_func = eval.bind(this, func); + } else if (typeof func !== 'function') { + throw new TypeError('callback is not a function/string'); + } else if (arguments.length > 2) { + // Special case: callback arguments are provided. + bind_args = Array.prototype.slice.call(arguments, 2); // [ arg1, arg2, ... ] + bind_args.unshift(this); // [ global(this), arg1, arg2, ... ] + cb_func = func.bind.apply(func, bind_args); + } else { + // Normal case: callback given as a function without arguments. + cb_func = func; + } + + timer_id = evloop.nextTimerId++; + + evloop.insertTimer({ + id: timer_id, + oneshot: false, + cb: cb_func, + delay: delay, + target: Date.now() + delay + }); + + return timer_id; +} + +function clearInterval(timer_id) { + var evloop = EventLoop; + + if (typeof timer_id !== 'number') { + throw new TypeError('timer ID is not a number'); + } + evloop.removeTimerById(timer_id); +} + +/* custom call */ +function requestEventLoopExit() { + EventLoop.requestExit(); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/fileio.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/fileio.c new file mode 100644 index 000000000..df94cd43f --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/fileio.c @@ -0,0 +1,69 @@ +/* + * File I/O binding example. + */ + +#include +#include +#include + +#include "duktape.h" + +static int fileio_readfile(duk_context *ctx) { + const char *filename = duk_to_string(ctx, 0); + FILE *f = NULL; + long len; + void *buf; + size_t got; + + if (!filename) { + goto error; + } + + f = fopen(filename, "rb"); + if (!f) { + goto error; + } + + if (fseek(f, 0, SEEK_END) != 0) { + goto error; + } + + len = ftell(f); + + if (fseek(f, 0, SEEK_SET) != 0) { + goto error; + } + + buf = duk_push_fixed_buffer(ctx, (size_t) len); + + got = fread(buf, 1, len, f); + if (got != (size_t) len) { + goto error; + } + + fclose(f); + f = NULL; + + return 1; + + error: + if (f) { + fclose(f); + } + + return DUK_RET_ERROR; +} + +static duk_function_list_entry fileio_funcs[] = { + { "readfile", fileio_readfile, 1 }, + { NULL, NULL, 0 } +}; + +void fileio_register(duk_context *ctx) { + /* Set global 'FileIo'. */ + duk_push_global_object(ctx); + duk_push_object(ctx); + duk_put_function_list(ctx, -1, fileio_funcs); + duk_put_prop_string(ctx, -2, "FileIo"); + duk_pop(ctx); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/main.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/main.c new file mode 100644 index 000000000..762792184 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/main.c @@ -0,0 +1,256 @@ +/* + * Main for evloop command line tool. + * + * Runs a given script from file or stdin inside an eventloop. The + * script can then access setTimeout() etc. + */ + +#include +#include +#include +#ifndef NO_SIGNAL +#include +#endif + +#include "duktape.h" + +extern void poll_register(duk_context *ctx); +extern void ncurses_register(duk_context *ctx); +extern void socket_register(duk_context *ctx); +extern void fileio_register(duk_context *ctx); +extern void eventloop_register(duk_context *ctx); +extern int eventloop_run(duk_context *ctx); /* Duktape/C function, safe called */ + +static int c_evloop = 0; + +#ifndef NO_SIGNAL +static void my_sighandler(int x) { + fprintf(stderr, "Got signal %d\n", x); + fflush(stderr); +} +static void set_sigint_handler(void) { + (void) signal(SIGINT, my_sighandler); +} +#endif /* NO_SIGNAL */ + +/* Print error to stderr and pop error. */ +static void print_error(duk_context *ctx, FILE *f) { + if (duk_is_object(ctx, -1) && duk_has_prop_string(ctx, -1, "stack")) { + /* XXX: print error objects specially */ + /* XXX: pcall the string coercion */ + duk_get_prop_string(ctx, -1, "stack"); + if (duk_is_string(ctx, -1)) { + fprintf(f, "%s\n", duk_get_string(ctx, -1)); + fflush(f); + duk_pop_2(ctx); + return; + } else { + duk_pop(ctx); + } + } + duk_to_string(ctx, -1); + fprintf(f, "%s\n", duk_get_string(ctx, -1)); + fflush(f); + duk_pop(ctx); +} + +int wrapped_compile_execute(duk_context *ctx) { + int comp_flags = 0; + int rc; + + /* Compile input and place it into global _USERCODE */ + duk_compile(ctx, comp_flags); + duk_push_global_object(ctx); + duk_insert(ctx, -2); /* [ ... global func ] */ + duk_put_prop_string(ctx, -2, "_USERCODE"); + duk_pop(ctx); +#if 0 + printf("compiled usercode\n"); +#endif + + /* Start a zero timer which will call _USERCODE from within + * the event loop. + */ + fprintf(stderr, "set _USERCODE timer\n"); + fflush(stderr); + duk_eval_string(ctx, "setTimeout(function() { _USERCODE(); }, 0);"); + duk_pop(ctx); + + /* Finally, launch eventloop. This call only returns after the + * eventloop terminates. + */ + if (c_evloop) { + fprintf(stderr, "calling eventloop_run()\n"); + fflush(stderr); + rc = duk_safe_call(ctx, eventloop_run, 0 /*nargs*/, 1 /*nrets*/); + if (rc != 0) { + fprintf(stderr, "eventloop_run() failed: %s\n", duk_to_string(ctx, -1)); + fflush(stderr); + } + duk_pop(ctx); + } else { + fprintf(stderr, "calling EventLoop.run()\n"); + fflush(stderr); + duk_eval_string(ctx, "EventLoop.run();"); + duk_pop(ctx); + } + + return 0; +} + +int handle_fh(duk_context *ctx, FILE *f, const char *filename) { + char *buf = NULL; + int len; + int got; + int rc; + int retval = -1; + + if (fseek(f, 0, SEEK_END) < 0) { + goto error; + } + len = (int) ftell(f); + if (fseek(f, 0, SEEK_SET) < 0) { + goto error; + } + buf = (char *) malloc(len); + if (!buf) { + goto error; + } + + got = fread((void *) buf, (size_t) 1, (size_t) len, f); + + duk_push_lstring(ctx, buf, got); + duk_push_string(ctx, filename); + + free(buf); + buf = NULL; + + rc = duk_safe_call(ctx, wrapped_compile_execute, 2 /*nargs*/, 1 /*nret*/); + if (rc != DUK_EXEC_SUCCESS) { + print_error(ctx, stderr); + goto error; + } else { + duk_pop(ctx); + retval = 0; + } + /* fall thru */ + + error: + if (buf) { + free(buf); + } + return retval; +} + +int handle_file(duk_context *ctx, const char *filename) { + FILE *f = NULL; + int retval; + + f = fopen(filename, "rb"); + if (!f) { + fprintf(stderr, "failed to open source file: %s\n", filename); + fflush(stderr); + goto error; + } + + retval = handle_fh(ctx, f, filename); + + fclose(f); + return retval; + + error: + return -1; +} + +int handle_stdin(duk_context *ctx) { + int retval; + + retval = handle_fh(ctx, stdin, "stdin"); + + return retval; +} + +int main(int argc, char *argv[]) { + duk_context *ctx = NULL; + int retval = 0; + const char *filename = NULL; + int i; + +#ifndef NO_SIGNAL + set_sigint_handler(); + + /* This is useful at the global level; libraries should avoid SIGPIPE though */ + /*signal(SIGPIPE, SIG_IGN);*/ +#endif + + for (i = 1; i < argc; i++) { + char *arg = argv[i]; + if (!arg) { + goto usage; + } + if (strcmp(arg, "-c") == 0) { + c_evloop = 1; + } else if (strlen(arg) > 1 && arg[0] == '-') { + goto usage; + } else { + if (filename) { + goto usage; + } + filename = arg; + } + } + if (!filename) { + goto usage; + } + + ctx = duk_create_heap_default(); + + poll_register(ctx); + ncurses_register(ctx); + socket_register(ctx); + fileio_register(ctx); + + if (c_evloop) { + fprintf(stderr, "Using C based eventloop (omit -c to use Ecmascript based eventloop)\n"); + fflush(stderr); + + eventloop_register(ctx); + duk_eval_file(ctx, "c_eventloop.js"); + } else { + fprintf(stderr, "Using Ecmascript based eventloop (give -c to use C based eventloop)\n"); + fflush(stderr); + + duk_eval_file(ctx, "ecma_eventloop.js"); + } + + fprintf(stderr, "Executing code from: '%s'\n", filename); + fflush(stderr); + + if (strcmp(filename, "-") == 0) { + if (handle_stdin(ctx) != 0) { + retval = 1; + goto cleanup; + } + } else { + if (handle_file(ctx, filename) != 0) { + retval = 1; + goto cleanup; + } + } + + cleanup: + if (ctx) { + duk_destroy_heap(ctx); + } + + return retval; + + usage: + fprintf(stderr, "Usage: evloop [-c] \n"); + fprintf(stderr, "\n"); + fprintf(stderr, "Uses an Ecmascript based eventloop (ecma_eventloop.js) by default.\n"); + fprintf(stderr, "If -c option given, uses a C based eventloop (c_eventloop.{c,js}).\n"); + fprintf(stderr, "If is '-', the entire STDIN executed.\n"); + fflush(stderr); + exit(1); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/ncurses.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/ncurses.c new file mode 100644 index 000000000..7734fcc98 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/ncurses.c @@ -0,0 +1,105 @@ +/* + * Ncurses bindings example. + * + * VALGRIND NOTE: when you use ncurses, there seems to be no way to get a + * clean valgrind run. Even if ncurses state is properly shut down, there + * will still be some residual leaks. + * + * Debian: install libncurses5-dev + */ + +#include +#include "duktape.h" + +static int ncurses_initscr(duk_context *ctx) { + WINDOW *win; + + win = initscr(); + duk_push_pointer(ctx, (void *) win); + return 1; +} + +static int ncurses_endwin(duk_context *ctx) { + int rc; + + rc = endwin(); + duk_push_int(ctx, rc); + return 1; +} + +static int ncurses_delscreen(duk_context *ctx) { + /* XXX: no screen management now */ + (void) ctx; + return 0; +} + +static int ncurses_getmaxyx(duk_context *ctx) { + int row, col; + + getmaxyx(stdscr, row, col); + + duk_push_array(ctx); + duk_push_int(ctx, row); + duk_put_prop_index(ctx, -2, 0); + duk_push_int(ctx, col); + duk_put_prop_index(ctx, -2, 1); + return 1; +} + +static int ncurses_printw(duk_context *ctx) { + int rc; + const char *str; + + str = duk_to_string(ctx, 0); + rc = printw("%s", str); + duk_push_int(ctx, rc); + return 1; +} + +static int ncurses_mvprintw(duk_context *ctx) { + int y = duk_to_int(ctx, 0); + int x = duk_to_int(ctx, 1); + const char *str = duk_to_string(ctx, 2); + int rc; + + rc = mvprintw(y, x, "%s", str); + duk_push_int(ctx, rc); + return 1; +} + +static int ncurses_refresh(duk_context *ctx) { + int rc; + + rc = refresh(); + duk_push_int(ctx, rc); + return 1; +} + +static int ncurses_getch(duk_context *ctx) { + int rc; + + rc = getch(); + duk_push_int(ctx, rc); + return 1; +} + +static duk_function_list_entry ncurses_funcs[] = { + { "initscr", ncurses_initscr, 0 }, + { "endwin", ncurses_endwin, 0 }, + { "delscreen", ncurses_delscreen, 0 }, + { "getmaxyx", ncurses_getmaxyx, 0 }, + { "printw", ncurses_printw, 1 }, + { "mvprintw", ncurses_mvprintw, 3 }, + { "refresh", ncurses_refresh, 0 }, + { "getch", ncurses_getch, 0 }, + { NULL, NULL, 0 } +}; + +void ncurses_register(duk_context *ctx) { + /* Set global 'Ncurses'. */ + duk_push_global_object(ctx); + duk_push_object(ctx); + duk_put_function_list(ctx, -1, ncurses_funcs); + duk_put_prop_string(ctx, -2, "Ncurses"); + duk_pop(ctx); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/poll.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/poll.c new file mode 100644 index 000000000..c78745d26 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/poll.c @@ -0,0 +1,111 @@ +/* + * C wrapper for poll(). + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +#include "duktape.h" + +static int poll_poll(duk_context *ctx) { + int timeout = duk_to_int(ctx, 1); + int i, n, nchanged; + int fd, rc; + struct pollfd fds[20]; + struct timespec ts; + + memset(fds, 0, sizeof(fds)); + + n = 0; + duk_enum(ctx, 0, 0 /*enum_flags*/); + while (duk_next(ctx, -1, 0)) { + if ((size_t) n >= sizeof(fds) / sizeof(struct pollfd)) { + return -1; + } + + /* [... enum key] */ + duk_dup_top(ctx); /* -> [... enum key key] */ + duk_get_prop(ctx, 0); /* -> [... enum key val] */ + fd = duk_to_int(ctx, -2); + + duk_push_string(ctx, "events"); + duk_get_prop(ctx, -2); /* -> [... enum key val events] */ + + fds[n].fd = fd; + fds[n].events = duk_to_int(ctx, -1); + fds[n].revents = 0; + + duk_pop_n(ctx, 3); /* -> [... enum] */ + + n++; + } + /* leave enum on stack */ + + memset(&ts, 0, sizeof(ts)); + ts.tv_nsec = (timeout % 1000) * 1000000; + ts.tv_sec = timeout / 1000; + + /*rc = ppoll(fds, n, &ts, NULL);*/ + rc = poll(fds, n, timeout); + if (rc < 0) { + duk_error(ctx, DUK_ERR_ERROR, "%s (errno=%d)", strerror(errno), errno); + } + + duk_push_array(ctx); + nchanged = 0; + for (i = 0; i < n; i++) { + /* update revents */ + + if (fds[i].revents) { + duk_push_int(ctx, fds[i].fd); /* -> [... retarr fd] */ + duk_put_prop_index(ctx, -2, nchanged); + nchanged++; + } + + duk_push_int(ctx, fds[i].fd); /* -> [... retarr key] */ + duk_get_prop(ctx, 0); /* -> [... retarr val] */ + duk_push_string(ctx, "revents"); + duk_push_int(ctx, fds[i].revents); /* -> [... retarr val "revents" fds[i].revents] */ + duk_put_prop(ctx, -3); /* -> [... retarr val] */ + duk_pop(ctx); + } + + /* [retarr] */ + + return 1; +} + +static duk_function_list_entry poll_funcs[] = { + { "poll", poll_poll, 2 }, + { NULL, NULL, 0 } +}; + +static duk_number_list_entry poll_consts[] = { + { "POLLIN", (double) POLLIN }, + { "POLLPRI", (double) POLLPRI }, + { "POLLOUT", (double) POLLOUT }, +#if 0 + /* Linux 2.6.17 and upwards, requires _GNU_SOURCE etc, not added + * now because we don't use it. + */ + { "POLLRDHUP", (double) POLLRDHUP }, +#endif + { "POLLERR", (double) POLLERR }, + { "POLLHUP", (double) POLLHUP }, + { "POLLNVAL", (double) POLLNVAL }, + { NULL, 0.0 } +}; + +void poll_register(duk_context *ctx) { + /* Set global 'Poll' with functions and constants. */ + duk_push_global_object(ctx); + duk_push_object(ctx); + duk_put_function_list(ctx, -1, poll_funcs); + duk_put_number_list(ctx, -1, poll_consts); + duk_put_prop_string(ctx, -2, "Poll"); + duk_pop(ctx); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/server-socket-test.js b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/server-socket-test.js new file mode 100644 index 000000000..68510af07 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/server-socket-test.js @@ -0,0 +1,34 @@ + +var HOST = 'localhost' +var PORT = 12345; +var EXIT_TIMEOUT = 300e3; + +print('automatic exit after ' + (EXIT_TIMEOUT / 1e3) + ' seconds'); +setTimeout(function () { + print('exit timer'); + EventLoop.requestExit(); +}, EXIT_TIMEOUT); + +print('listen on ' + HOST + ':' + PORT); +EventLoop.server(HOST, PORT, function (fd, addr, port) { + print('new connection on fd ' + fd + ' from ' + addr + ':' + port); + EventLoop.setReader(fd, function (fd, data) { + var b, i, n, x; + + // Handle socket data carefully: if you convert it to a string, + // it may not be valid UTF-8 etc. Here we operate on the data + // directly in the buffer. + + b = data.valueOf(); // ensure we get a plain buffer + n = b.length; + for (i = 0; i < n; i++) { + x = b[i]; + if (x >= 0x61 && x <= 0x7a) { + b[i] = x - 0x20; // uppercase + } + } + + print('read data on fd ' + fd + ', length ' + data.length); + EventLoop.write(fd, data); + }); +}); diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/socket.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/socket.c new file mode 100644 index 000000000..a1587fa31 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/eventloop/socket.c @@ -0,0 +1,286 @@ +/* + * TCP sockets binding example. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "duktape.h" + +#define ERROR_FROM_ERRNO(ctx) do { \ + duk_error(ctx, DUK_ERR_ERROR, "%s (errno=%d)", strerror(errno), errno); \ + } while (0) + +static void set_nonblocking(duk_context *ctx, int fd) { + int rc; + int flags; + + rc = fcntl(fd, F_GETFL); + if (rc < 0) { + ERROR_FROM_ERRNO(ctx); + } + flags = rc; + + flags |= O_NONBLOCK; + + rc = fcntl(fd, F_SETFL, flags); + if (rc < 0) { + ERROR_FROM_ERRNO(ctx); + } +} + +static void set_reuseaddr(duk_context *ctx, int fd) { + int val; + int rc; + + val = 1; + rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *) &val, sizeof(val)); + if (rc != 0) { + ERROR_FROM_ERRNO(ctx); + } +} + +#ifdef __APPLE__ +static void set_nosigpipe(duk_context *ctx, int fd) { + int val; + int rc; + + val = 1; + rc = setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (const void *) &val, sizeof(val)); + if (rc != 0) { + ERROR_FROM_ERRNO(ctx); + } +} +#endif + +static int socket_create_server_socket(duk_context *ctx) { + const char *addr = duk_to_string(ctx, 0); + int port = duk_to_int(ctx, 1); + int sock; + struct sockaddr_in sockaddr; + struct hostent *ent; + struct in_addr **addr_list; + struct in_addr *addr_inet; + int i; + int rc; + + sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + ERROR_FROM_ERRNO(ctx); + } + + set_nonblocking(ctx, sock); + set_reuseaddr(ctx, sock); +#ifdef __APPLE__ + set_nosigpipe(ctx, sock); +#endif + + ent = gethostbyname(addr); + if (!ent) { + ERROR_FROM_ERRNO(ctx); + } + + addr_list = (struct in_addr **) ent->h_addr_list; + addr_inet = NULL; + for (i = 0; addr_list[i]; i++) { + addr_inet = addr_list[i]; + break; + } + if (!addr_inet) { + duk_error(ctx, DUK_ERR_ERROR, "cannot resolve %s", addr); + } + + memset(&sockaddr, 0, sizeof(sockaddr)); + sockaddr.sin_family = AF_INET; + sockaddr.sin_port = htons(port); + sockaddr.sin_addr = *addr_inet; + + rc = bind(sock, (const struct sockaddr *) &sockaddr, sizeof(sockaddr)); + if (rc < 0) { + ERROR_FROM_ERRNO(ctx); + } + + rc = listen(sock, 10 /*backlog*/); + if (rc < 0) { + (void) close(sock); + ERROR_FROM_ERRNO(ctx); + } + + duk_push_int(ctx, sock); + return 1; +} + +static int socket_close(duk_context *ctx) { + int sock = duk_to_int(ctx, 0); + int rc; + + rc = close(sock); + if (rc < 0) { + ERROR_FROM_ERRNO(ctx); + } + return 0; +} + +static int socket_accept(duk_context *ctx) { + int sock = duk_to_int(ctx, 0); + int rc; + struct sockaddr_in addr; + socklen_t addrlen; + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addrlen = sizeof(addr); + + rc = accept(sock, (struct sockaddr *) &addr, &addrlen); + if (rc < 0) { + ERROR_FROM_ERRNO(ctx); + } + + set_nonblocking(ctx, sock); +#ifdef __APPLE__ + set_nosigpipe(ctx, sock); +#endif + + if (addrlen == sizeof(addr)) { + uint32_t tmp = ntohl(addr.sin_addr.s_addr); + + duk_push_object(ctx); + + duk_push_string(ctx, "fd"); + duk_push_int(ctx, rc); + duk_put_prop(ctx, -3); + duk_push_string(ctx, "addr"); + duk_push_sprintf(ctx, "%d.%d.%d.%d", ((tmp >> 24) & 0xff), ((tmp >> 16) & 0xff), ((tmp >> 8) & 0xff), (tmp & 0xff)); + duk_put_prop(ctx, -3); + duk_push_string(ctx, "port"); + duk_push_int(ctx, ntohs(addr.sin_port)); + duk_put_prop(ctx, -3); + + return 1; + } + + return 0; +} + +static int socket_connect(duk_context *ctx) { + const char *addr = duk_to_string(ctx, 0); + int port = duk_to_int(ctx, 1); + int sock; + struct sockaddr_in sockaddr; + struct hostent *ent; + struct in_addr **addr_list; + struct in_addr *addr_inet; + int i; + int rc; + + sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + ERROR_FROM_ERRNO(ctx); + } + + set_nonblocking(ctx, sock); +#ifdef __APPLE__ + set_nosigpipe(ctx, sock); +#endif + + ent = gethostbyname(addr); + if (!ent) { + ERROR_FROM_ERRNO(ctx); + } + + addr_list = (struct in_addr **) ent->h_addr_list; + addr_inet = NULL; + for (i = 0; addr_list[i]; i++) { + addr_inet = addr_list[i]; + break; + } + if (!addr_inet) { + duk_error(ctx, DUK_ERR_ERROR, "cannot resolve %s", addr); + } + + memset(&sockaddr, 0, sizeof(sockaddr)); + sockaddr.sin_family = AF_INET; + sockaddr.sin_port = htons(port); + sockaddr.sin_addr = *addr_inet; + + rc = connect(sock, (const struct sockaddr *) &sockaddr, (socklen_t) sizeof(sockaddr)); + if (rc < 0) { + if (errno == EINPROGRESS) { +#if 0 + fprintf(stderr, "connect() returned EINPROGRESS as expected, need to poll writability\n"); + fflush(stderr); +#endif + } else { + ERROR_FROM_ERRNO(ctx); + } + } + + duk_push_int(ctx, sock); + return 1; +} + +static int socket_read(duk_context *ctx) { + int sock = duk_to_int(ctx, 0); + char readbuf[1024]; + int rc; + void *data; + + rc = recvfrom(sock, (void *) readbuf, sizeof(readbuf), 0, NULL, NULL); + if (rc < 0) { + ERROR_FROM_ERRNO(ctx); + } + + data = duk_push_fixed_buffer(ctx, rc); + memcpy(data, readbuf, rc); + return 1; +} + +static int socket_write(duk_context *ctx) { + int sock = duk_to_int(ctx, 0); + const char *data; + size_t len; + ssize_t rc; + + data = duk_to_buffer(ctx, 1, &len); + + /* MSG_NOSIGNAL: avoid SIGPIPE */ +#ifdef __APPLE__ + rc = sendto(sock, (void *) data, len, 0, NULL, 0); +#else + rc = sendto(sock, (void *) data, len, MSG_NOSIGNAL, NULL, 0); +#endif + if (rc < 0) { + ERROR_FROM_ERRNO(ctx); + } + + duk_push_int(ctx, rc); + return 1; +} + +static duk_function_list_entry socket_funcs[] = { + { "createServerSocket", socket_create_server_socket, 2 }, + { "close", socket_close, 1 }, + { "accept", socket_accept, 1 }, + { "connect", socket_connect, 2 }, + { "read", socket_read, 1 }, + { "write", socket_write, 2 }, + { NULL, NULL, 0 } +}; + +void socket_register(duk_context *ctx) { + /* Set global 'Socket'. */ + duk_push_global_object(ctx); + duk_push_object(ctx); + duk_put_function_list(ctx, -1, socket_funcs); + duk_put_prop_string(ctx, -2, "Socket"); + duk_pop(ctx); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/README.rst new file mode 100644 index 000000000..1933094fd --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/README.rst @@ -0,0 +1,5 @@ +=========================== +Duktape guide example files +=========================== + +Examples used in the Duktape guide. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/fib.js b/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/fib.js new file mode 100644 index 000000000..2b2982f75 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/fib.js @@ -0,0 +1,16 @@ +// fib.js +function fib(n) { + if (n == 0) { return 0; } + if (n == 1) { return 1; } + return fib(n-1) + fib(n-2); +} + +function test() { + var res = []; + for (i = 0; i < 20; i++) { + res.push(fib(i)); + } + print(res.join(' ')); +} + +test(); diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/prime.js b/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/prime.js new file mode 100644 index 000000000..8959754f0 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/prime.js @@ -0,0 +1,32 @@ +// prime.js + +// Pure Ecmascript version of low level helper +function primeCheckEcmascript(val, limit) { + for (var i = 2; i <= limit; i++) { + if ((val % i) == 0) { return false; } + } + return true; +} + +// Select available helper at load time +var primeCheckHelper = (this.primeCheckNative || primeCheckEcmascript); + +// Check 'val' for primality +function primeCheck(val) { + if (val == 1 || val == 2) { return true; } + var limit = Math.ceil(Math.sqrt(val)); + while (limit * limit < val) { limit += 1; } + return primeCheckHelper(val, limit); +} + +// Find primes below one million ending in '9999'. +function primeTest() { + var res = []; + + print('Have native helper: ' + (primeCheckHelper !== primeCheckEcmascript)); + for (var i = 1; i < 1000000; i++) { + if (primeCheck(i) && (i % 10000) == 9999) { res.push(i); } + } + print(res.join(' ')); +} + diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/primecheck.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/primecheck.c new file mode 100644 index 000000000..36fa5d65e --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/primecheck.c @@ -0,0 +1,52 @@ +/* primecheck.c */ +#include +#include +#include +#include "duktape.h" + +static duk_ret_t native_prime_check(duk_context *ctx) { + int val = duk_require_int(ctx, 0); + int lim = duk_require_int(ctx, 1); + int i; + + for (i = 2; i <= lim; i++) { + if (val % i == 0) { + duk_push_false(ctx); + return 1; + } + } + + duk_push_true(ctx); + return 1; +} + +int main(int argc, const char *argv[]) { + duk_context *ctx = NULL; + + ctx = duk_create_heap_default(); + if (!ctx) { + printf("Failed to create a Duktape heap.\n"); + exit(1); + } + + duk_push_global_object(ctx); + duk_push_c_function(ctx, native_prime_check, 2 /*nargs*/); + duk_put_prop_string(ctx, -2, "primeCheckNative"); + + if (duk_peval_file(ctx, "prime.js") != 0) { + printf("Error: %s\n", duk_safe_to_string(ctx, -1)); + goto finished; + } + duk_pop(ctx); /* ignore result */ + + duk_get_prop_string(ctx, -1, "primeTest"); + if (duk_pcall(ctx, 0) != 0) { + printf("Error: %s\n", duk_safe_to_string(ctx, -1)); + } + duk_pop(ctx); /* ignore result */ + + finished: + duk_destroy_heap(ctx); + + exit(0); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/process.js b/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/process.js new file mode 100644 index 000000000..62b501175 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/process.js @@ -0,0 +1,12 @@ +// process.js +function processLine(line) { + return line.trim() + .replace(/[<>&"'\u0000-\u001F\u007E-\uFFFF]/g, function(x) { + // escape HTML characters + return '&#' + x.charCodeAt(0) + ';' + }) + .replace(/\*(.*?)\*/g, function(x, m) { + // automatically bold text between stars + return '' + m + ''; + }); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/processlines.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/processlines.c new file mode 100644 index 000000000..f91bdffc7 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/processlines.c @@ -0,0 +1,59 @@ +/* processlines.c */ +#include +#include +#include +#include "duktape.h" + +int main(int argc, const char *argv[]) { + duk_context *ctx = NULL; + char line[4096]; + char idx; + int ch; + + ctx = duk_create_heap_default(); + if (!ctx) { + printf("Failed to create a Duktape heap.\n"); + exit(1); + } + + if (duk_peval_file(ctx, "process.js") != 0) { + printf("Error: %s\n", duk_safe_to_string(ctx, -1)); + goto finished; + } + duk_pop(ctx); /* ignore result */ + + memset(line, 0, sizeof(line)); + idx = 0; + for (;;) { + if (idx >= sizeof(line)) { + printf("Line too long\n"); + exit(1); + } + + ch = fgetc(stdin); + if (ch == 0x0a) { + line[idx++] = '\0'; + + duk_push_global_object(ctx); + duk_get_prop_string(ctx, -1 /*index*/, "processLine"); + duk_push_string(ctx, line); + if (duk_pcall(ctx, 1 /*nargs*/) != 0) { + printf("Error: %s\n", duk_safe_to_string(ctx, -1)); + } else { + printf("%s\n", duk_safe_to_string(ctx, -1)); + } + duk_pop(ctx); /* pop result/error */ + + idx = 0; + } else if (ch == EOF) { + break; + } else { + line[idx++] = (char) ch; + } + } + + finished: + duk_destroy_heap(ctx); + + exit(0); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/uppercase.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/uppercase.c new file mode 100644 index 000000000..03d08698a --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/guide/uppercase.c @@ -0,0 +1,42 @@ +/* uppercase.c */ +#include +#include +#include "duktape.h" + +static int dummy_upper_case(duk_context *ctx) { + size_t sz; + const char *val = duk_require_lstring(ctx, 0, &sz); + size_t i; + + /* We're going to need 'sz' additional entries on the stack. */ + duk_require_stack(ctx, sz); + + for (i = 0; i < sz; i++) { + char ch = val[i]; + if (ch >= 'a' && ch <= 'z') { + ch = ch - 'a' + 'A'; + } + duk_push_lstring(ctx, (const char *) &ch, 1); + } + + duk_concat(ctx, sz); + return 1; +} + +int main(int argc, char *argv[]) { + duk_context *ctx; + + if (argc < 2) { exit(1); } + + ctx = duk_create_heap_default(); + if (!ctx) { exit(1); } + + duk_push_c_function(ctx, dummy_upper_case, 1); + duk_push_string(ctx, argv[1]); + duk_call(ctx, 1); + printf("%s -> %s\n", argv[1], duk_to_string(ctx, -1)); + duk_pop(ctx); + + duk_destroy_heap(ctx); + return 0; +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/hello/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/hello/README.rst new file mode 100644 index 000000000..7afef53e9 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/hello/README.rst @@ -0,0 +1,5 @@ +=================== +Hello world example +=================== + +Very simple example, most useful for compilation tests. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/hello/hello.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/hello/hello.c new file mode 100644 index 000000000..9113d2fb9 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/hello/hello.c @@ -0,0 +1,38 @@ +/* + * Very simple example program + */ + +#include "duktape.h" + +int adder(duk_context *ctx) { + int i; + int n = duk_get_top(ctx); /* #args */ + double res = 0.0; + + for (i = 0; i < n; i++) { + res += duk_to_number(ctx, i); + } + + duk_push_number(ctx, res); + return 1; /* one return value */ +} + +int main(int argc, char *argv[]) { + duk_context *ctx = duk_create_heap_default(); + + (void) argc; (void) argv; /* suppress warning */ + + duk_eval_string(ctx, "print('Hello world!');"); + + duk_push_global_object(ctx); + duk_push_c_function(ctx, adder, DUK_VARARGS); + duk_put_prop_string(ctx, -2, "adder"); + duk_pop(ctx); /* pop global */ + + duk_eval_string(ctx, "print('2+3=' + adder(2, 3));"); + duk_pop(ctx); /* pop eval result */ + + duk_destroy_heap(ctx); + + return 0; +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/jxpretty/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/jxpretty/README.rst new file mode 100644 index 000000000..5ab43a841 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/jxpretty/README.rst @@ -0,0 +1,5 @@ +================ +Jxpretty example +================ + +Simple command line utility to pretty print JSON in the JX format. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/jxpretty/jxpretty.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/jxpretty/jxpretty.c new file mode 100644 index 000000000..1e483efbe --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/jxpretty/jxpretty.c @@ -0,0 +1,63 @@ +/* + * Pretty print JSON from stdin into indented JX. + */ + +#include +#include +#include "duktape.h" + +static duk_ret_t do_jxpretty(duk_context *ctx) { + FILE *f = stdin; + char buf[4096]; + size_t ret; + + for (;;) { + if (ferror(f)) { + duk_error(ctx, DUK_ERR_ERROR, "ferror() on stdin"); + } + if (feof(f)) { + break; + } + + ret = fread(buf, 1, sizeof(buf), f); +#if 0 + fprintf(stderr, "Read: %ld\n", (long) ret); + fflush(stderr); +#endif + if (ret == 0) { + break; + } + + duk_require_stack(ctx, 1); + duk_push_lstring(ctx, (const char *) buf, ret); + } + + duk_concat(ctx, duk_get_top(ctx)); + + duk_eval_string(ctx, "(function (v) { print(Duktape.enc('jx', JSON.parse(v), null, 4)); })"); + duk_insert(ctx, -2); + duk_call(ctx, 1); + + return 0; +} + +int main(int argc, char *argv[]) { + duk_context *ctx; + duk_int_t rc; + + /* suppress warnings */ + (void) argc; + (void) argv; + + ctx = duk_create_heap_default(); + + rc = duk_safe_call(ctx, do_jxpretty, 0 /*nargs*/, 1 /*nrets*/); + if (rc) { + fprintf(stderr, "ERROR: %s\n", duk_safe_to_string(ctx, -1)); + fflush(stderr); + } + + duk_destroy_heap(ctx); + + return 0; +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/sandbox/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/examples/sandbox/README.rst new file mode 100644 index 000000000..24df0a25f --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/sandbox/README.rst @@ -0,0 +1,5 @@ +=============== +Sandbox example +=============== + +Very simple, minimal sandboxing example. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/examples/sandbox/sandbox.c b/src/civetweb/src/third_party/duktape-1.5.2/examples/sandbox/sandbox.c new file mode 100644 index 000000000..915faa9a4 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/examples/sandbox/sandbox.c @@ -0,0 +1,252 @@ +/* + * Sandboxing example + * + * Uses custom memory allocation functions which keep track of total amount + * of memory allocated, imposing a maximum total allocation size. + */ + +#include +#include +#include "duktape.h" + +/* + * Memory allocator which backs to standard library memory functions but + * keeps a small header to track current allocation size. + * + * Many other sandbox allocation models are useful, e.g. preallocated pools. + */ + +typedef struct { + /* The double value in the union is there to ensure alignment is + * good for IEEE doubles too. In many 32-bit environments 4 bytes + * would be sufficiently aligned and the double value is unnecessary. + */ + union { + size_t sz; + double d; + } u; +} alloc_hdr; + +static size_t total_allocated = 0; +static size_t max_allocated = 256 * 1024; /* 256kB sandbox */ + +static void sandbox_dump_memstate(void) { +#if 0 + fprintf(stderr, "Total allocated: %ld\n", (long) total_allocated); + fflush(stderr); +#endif +} + +static void *sandbox_alloc(void *udata, duk_size_t size) { + alloc_hdr *hdr; + + (void) udata; /* Suppress warning. */ + + if (size == 0) { + return NULL; + } + + if (total_allocated + size > max_allocated) { + fprintf(stderr, "Sandbox maximum allocation size reached, %ld requested in sandbox_alloc\n", + (long) size); + fflush(stderr); + return NULL; + } + + hdr = (alloc_hdr *) malloc(size + sizeof(alloc_hdr)); + if (!hdr) { + return NULL; + } + hdr->u.sz = size; + total_allocated += size; + sandbox_dump_memstate(); + return (void *) (hdr + 1); +} + +static void *sandbox_realloc(void *udata, void *ptr, duk_size_t size) { + alloc_hdr *hdr; + size_t old_size; + void *t; + + (void) udata; /* Suppress warning. */ + + /* Handle the ptr-NULL vs. size-zero cases explicitly to minimize + * platform assumptions. You can get away with much less in specific + * well-behaving environments. + */ + + if (ptr) { + hdr = (alloc_hdr *) (((char *) ptr) - sizeof(alloc_hdr)); + old_size = hdr->u.sz; + + if (size == 0) { + total_allocated -= old_size; + free((void *) hdr); + sandbox_dump_memstate(); + return NULL; + } else { + if (total_allocated - old_size + size > max_allocated) { + fprintf(stderr, "Sandbox maximum allocation size reached, %ld requested in sandbox_realloc\n", + (long) size); + fflush(stderr); + return NULL; + } + + t = realloc((void *) hdr, size + sizeof(alloc_hdr)); + if (!t) { + return NULL; + } + hdr = (alloc_hdr *) t; + total_allocated -= old_size; + total_allocated += size; + hdr->u.sz = size; + sandbox_dump_memstate(); + return (void *) (hdr + 1); + } + } else { + if (size == 0) { + return NULL; + } else { + if (total_allocated + size > max_allocated) { + fprintf(stderr, "Sandbox maximum allocation size reached, %ld requested in sandbox_realloc\n", + (long) size); + fflush(stderr); + return NULL; + } + + hdr = (alloc_hdr *) malloc(size + sizeof(alloc_hdr)); + if (!hdr) { + return NULL; + } + hdr->u.sz = size; + total_allocated += size; + sandbox_dump_memstate(); + return (void *) (hdr + 1); + } + } +} + +static void sandbox_free(void *udata, void *ptr) { + alloc_hdr *hdr; + + (void) udata; /* Suppress warning. */ + + if (!ptr) { + return; + } + hdr = (alloc_hdr *) (((char *) ptr) - sizeof(alloc_hdr)); + total_allocated -= hdr->u.sz; + free((void *) hdr); + sandbox_dump_memstate(); +} + +/* + * Sandbox setup and test + */ + +static duk_ret_t do_sandbox_test(duk_context *ctx) { + FILE *f; + char buf[4096]; + size_t ret; + const char *globobj; + + /* + * Setup sandbox + */ + + globobj = + "({\n" + " print: print,\n" + " Math: {\n" + " max: Math.max\n" + " }\n" + "})\n"; +#if 1 + fprintf(stderr, "Sandbox global object:\n----------------\n%s----------------\n", globobj); + fflush(stderr); +#endif + duk_eval_string(ctx, globobj); + duk_set_global_object(ctx); + + /* + * Execute code from specified file + */ + + f = fopen(duk_require_string(ctx, -1), "rb"); + if (!f) { + duk_error(ctx, DUK_ERR_ERROR, "failed to open file"); + } + + for (;;) { + if (ferror(f)) { + fclose(f); + duk_error(ctx, DUK_ERR_ERROR, "ferror when reading file"); + } + if (feof(f)) { + break; + } + + ret = fread(buf, 1, sizeof(buf), f); + if (ret == 0) { + break; + } + + duk_push_lstring(ctx, (const char *) buf, ret); + } + + duk_concat(ctx, duk_get_top(ctx) - 1); /* -1 for filename */ + + /* -> [ ... filename source ] */ + + duk_insert(ctx, -2); + + /* -> [ ... source filename ] */ + + duk_compile(ctx, 0 /*flags*/); /* Compile as program */ + duk_call(ctx, 0 /*nargs*/); + + return 0; +} + +/* + * Main + */ + +static void sandbox_fatal(duk_context *ctx, duk_errcode_t code, const char *msg) { + (void) ctx; /* Suppress warning. */ + fprintf(stderr, "FATAL %ld: %s\n", (long) code, (msg ? msg : "no message")); + fflush(stderr); + exit(1); /* must not return */ +} + +int main(int argc, char *argv[]) { + duk_context *ctx; + duk_int_t rc; + + if (argc < 2) { + fprintf(stderr, "Usage: sandbox \n"); + fflush(stderr); + exit(1); + } + + ctx = duk_create_heap(sandbox_alloc, + sandbox_realloc, + sandbox_free, + NULL, + sandbox_fatal); + + duk_push_string(ctx, argv[1]); + rc = duk_safe_call(ctx, do_sandbox_test, 1 /*nargs*/, 1 /*nrets*/); + if (rc) { + fprintf(stderr, "ERROR: %s\n", duk_safe_to_string(ctx, -1)); + fflush(stderr); + } + + duk_destroy_heap(ctx); + + /* Should be zero. */ + fprintf(stderr, "Final allocation: %ld\n", (long) total_allocated); + fflush(stderr); + + return 1; +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/extras/README.rst b/src/civetweb/src/third_party/duktape-1.5.2/extras/README.rst new file mode 100644 index 000000000..1b7016016 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/extras/README.rst @@ -0,0 +1,13 @@ +============== +Duktape extras +============== + +Extra modules and utilities. Extras provide functionality that doesn't +comfortably fit into the main Duktape library, perhaps for footprint or +portability reasons, but are still useful for most users. + +Extras are maintained and will be bug fixed. However, they don't have the +same semantic versioning guarantees like the main Duktape library. Extras +may be dropped without warning as Duktape is versioned. For instance, if +an extra breaks due to Duktape changes and there is no time to fix it, the +missing extra won't block a release and will be dropped. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/license.spdx b/src/civetweb/src/third_party/duktape-1.5.2/license.spdx new file mode 100644 index 000000000..29a6b8de1 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/license.spdx @@ -0,0 +1,3933 @@ + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>6f9134b7d1c939593043b9e0ecb308fbc745c9af + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP102"/> + + <_3:fileName>./examples/eventloop/main.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>bd2f85dccd23b76b9cf9900b8b2b86f0bf97421b + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP416"/> + + <_3:fileName>./src-separate/duk_initjs_min.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP322"/> + + <_3:fileName>./src-separate/duk_internal.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>b7ee0e88e6365390db594e5e1a90c383847cfd0f + + + + <_3:created rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2016-12-08T20:32:53Z + + <_3:licenseListVersion>1.20 + + <_3:creator>Organization: duktape.org + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP80"/> + + <_3:fileName>./examples/coffee/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP276"/> + + <_3:fileName>./src-separate/duk_heap_stringtable.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>d70b4083ccd3b5dac32ad07e58d3132dc3390b79 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>f051b42ff42dc8655b9e17739f7cb7cb76bdf78d + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>61db5ef1604240b33fa9e1e8825ad588076a5aef + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP366"/> + + <_3:fileName>./src-separate/duk_debug.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>52554f56c3e3420d3643aa22f563d32a2b18be1f + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP324"/> + + <_3:fileName>./src-separate/duk_hbufferobject_misc.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP442"/> + + <_3:fileName>./src-separate/duk_debug_fixedbuffer.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>fb4f806e7282dd3cb2df04e228672d8b0b1b8eb4 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP290"/> + + <_3:fileName>./src-separate/duk_heap_misc.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP384"/> + + <_3:fileName>./src-separate/duk_api_compile.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>422984e8fc919e9f8c64d5cc4b20d7f6d15e341b + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP278"/> + + <_3:fileName>./src-separate/duk_error_longjmp.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>d0421b2e96d2365467764d4aecc442fe543d69ad + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>1db75c875cb94e1317f095e3d78f11275c17d154 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP138"/> + + <_3:fileName>./examples/cpp-exceptions/cpp_exceptions.cpp + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>23575db7cd88e42c0c3fc1630f44bf608fcec76d + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP272"/> + + <_3:fileName>./src-separate/duk_builtins.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP418"/> + + <_3:fileName>./src-separate/duk_api_string.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP332"/> + + <_3:fileName>./src-separate/duk_lexer.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP328"/> + + <_3:fileName>./src-separate/duk_bi_buffer.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>97bf0ab32255874def0bd3157bf4bb2fc731d37f + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP148"/> + + <_3:fileName>./examples/guide/process.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>0811e545a133c3a62672e239624a0c1f11478b8d + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>742d035afe3ae16b3498926494419b74c53dfaf4 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>34b0dc439c1aeb920a1db0ac0ed20ee189069ea1 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP440"/> + + <_3:fileName>./src-separate/duk_debug_heap.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP210"/> + + <_3:fileName>./src-separate/duk_util_bufwriter.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP32"/> + + <_3:fileName>./Makefile.eval + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>221600498f2801d0bfc71e36009f084f363e159e + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP444"/> + + <_3:fileName>./src-separate/duk_alloc_default.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>bad7c737fdd2d5f21ac00c6936921184cd26908d + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>21e33f2c348ab084c0869d3bba530b1885464f38 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP70"/> + + <_3:fileName>./debugger/static/webui.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP350"/> + + <_3:fileName>./src-separate/duk_api_object.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP280"/> + + <_3:fileName>./src-separate/duk_hstring.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP218"/> + + <_3:fileName>./src-separate/duk_hobject_enum.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>525e80da2b6d3640fd42af0f662d7b68865d6529 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>84b33d737bba2dadfdd950adad7a96d3d88eb01d + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP54"/> + + <_3:fileName>./debugger/Makefile + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP106"/> + + <_3:fileName>./examples/eventloop/ncurses.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP342"/> + + <_3:fileName>./src-separate/duk_error_misc.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>012bd42371c53e6c32e357201ee91c258640e546 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP254"/> + + <_3:fileName>./src-separate/duk_js_bytecode.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP134"/> + + <_3:fileName>./examples/eval/eval.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP306"/> + + <_3:fileName>./src-separate/duk_util_tinyrandom.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP170"/> + + <_3:fileName>./examples/debug-trans-socket/duk_trans_socket_windows.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP264"/> + + <_3:fileName>./src-separate/duk_forwdecl.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP352"/> + + <_3:fileName>./src-separate/duk_hbuffer.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>a5985c22d5e36eaced8ebc72338a38ae0b73c9ad + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP104"/> + + <_3:fileName>./examples/eventloop/c_eventloop.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP484"/> + + <_3:fileName>./licenses/commonjs.txt + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP488"/> + + <_3:fileName>./src/duk_config.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>4edee4bb513bcff807e343902af260ed8ff72850 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP424"/> + + <_3:fileName>./src-separate/duk_tval.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>5e427d4685c63b3e7c638c6eaac5e7df8216507f + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP108"/> + + <_3:fileName>./examples/eventloop/socket.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP166"/> + + <_3:fileName>./examples/cmdline/duk_cmdline.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP356"/> + + <_3:fileName>./src-separate/duk_selftest.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP256"/> + + <_3:fileName>./src-separate/duk_heap.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP190"/> + + <_3:fileName>./examples/sandbox/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP374"/> + + <_3:fileName>./src-separate/duk_hbuffer_ops.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>11a7a5b6b1557adeca8a9def51cb44282f24c853 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP340"/> + + <_3:fileName>./src-separate/duk_js_var.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP248"/> + + <_3:fileName>./src-separate/duk_hobject_props.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP376"/> + + <_3:fileName>./src-separate/duk_heap_hashstring.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>2e1b55e44a5845fb785dde5b8c2e24bb7bd2e1bf + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>a98ed9095834cad5b50d63cf8e659cca90e01062 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP228"/> + + <_3:fileName>./src-separate/duk_bi_date_unix.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>467388c812411a0d2b80760983171c346d35953d + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP372"/> + + <_3:fileName>./src-separate/duk_regexp_compiler.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>4d3ab8c2d41261b0cae627db9d536d26f72b5f8e + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP460"/> + + <_3:fileName>./polyfills/object-prototype-definesetter.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>79dac8ea1aa08daa3c8e9b6137b8a01c8d501893 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>8aa63b553757fe1229638e978b540d7116b70274 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP326"/> + + <_3:fileName>./src-separate/duk_js_compiler.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>32ba21f45afd4b027113eda5aba53314c3f00da9 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP46"/> + + <_3:fileName>./debugger/duk_debug.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>c00100a35037a372ebcde740082d5bb7c7735057 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP136"/> + + <_3:fileName>./examples/cpp-exceptions/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP486"/> + + <_3:fileName>./src/metadata.json + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>5bd828887b0f46810358539868398e64b622d047 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>82314b509ee3b15b393fefe747c8e0169ab39aaa + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP408"/> + + <_3:fileName>./src-separate/duk_numconv.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP270"/> + + <_3:fileName>./src-separate/duk_bi_date.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>23b6b54a8a35fbec735ffb332f4100a7f00557be + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP346"/> + + <_3:fileName>./src-separate/duk_hthread.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP58"/> + + <_3:fileName>./debugger/duk_debug_meta.json + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>fe251e819feb6f84c1c8169eb6921174be2b1373 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>6806426efc13e73e41899bef0aecca7eec097c18 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP434"/> + + <_3:fileName>./src-separate/duktape.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP40"/> + + <_3:fileName>./src-noline/duk_config.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP450"/> + + <_3:fileName>./src-separate/duk_debug_macros.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>012eed9a677efd2062f7ed6965d5a39e9e569c25 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>8015d14ffbc7a4a4b93d654f093aa48ec17fce4e + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP118"/> + + <_3:fileName>./examples/eventloop/curses-timers.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>fa2824f02d8a2d21f729a4e9d44557f3b39a9355 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP404"/> + + <_3:fileName>./src-separate/duk_strings.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP184"/> + + <_3:fileName>./examples/debug-trans-dvalue/duk_trans_dvalue.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>a8c0b54002a3a7d8c69d385e7fbcf4b70d1efc70 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>c81b68018e3d6f1b93683f7ead8761c01f2ca760 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP446"/> + + <_3:fileName>./src-separate/duk_heap_stringcache.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>df02f8688766b929639cca4881b73cb8f745c97c + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP336"/> + + <_3:fileName>./src-separate/duk_heap_refcount.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>59df770184e4c26b190a76f4cd035c7e661d573f + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP470"/> + + <_3:fileName>./config/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>fd24ca07cfd8fd37f3e70d10ad078ee5f149c9ce + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP30"/> + + <_3:fileName>./Makefile.dukdebug + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP436"/> + + <_3:fileName>./src-separate/duk_replacements.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>22046b9dfc552ed692193d1bf5e3d0e182111ae5 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP284"/> + + <_3:fileName>./src-separate/duk_api_internal.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP400"/> + + <_3:fileName>./src-separate/duk_js_executor.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP482"/> + + <_3:fileName>./licenses/lua.txt + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>e023af5c2fb4b1efab74a230c552f1eb1b37fc10 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP128"/> + + <_3:fileName>./examples/dummy-date-provider/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>54a05944602f4a24864001a40d770591c6800daf + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>4e41e7165a8d6e3bd370bc4536b16835a7204cf6 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>a23d89e3ff8fe247e09338af7b38bdab41da5c3e + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP358"/> + + <_3:fileName>./src-separate/duk_tval.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>821b268a1d4339ea2b309bdb6f527aa61bdb6b23 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>194e0bf80a1ddcc4d2d68ea6d2539656f3044f78 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>063133679b6d072efc1c27f3a510101744ffde0c + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP354"/> + + <_3:fileName>./src-separate/duk_api_bytecode.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>5f3b6cd81be97aa687fe8b47751e68b5561d7c1b + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>ad583b87e72c08b4b44918dd1d7fce97e4950aa4 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>7e2674dd72227576375a80f6ddbb11320f5b987a + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>83b27494e6b257ab09828b0a4a1cf907686778a5 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP368"/> + + <_3:fileName>./src-separate/duk_bi_boolean.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>829c4239ff43a1e15aa08bbbab41e7fba9207859 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP126"/> + + <_3:fileName>./examples/eventloop/server-socket-test.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>a913f60f44a9e19cd5c31e84242db88a0868eba8 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP144"/> + + <_3:fileName>./examples/guide/uppercase.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>786369a33cccbadc3d2efba613a43fdd638971fc + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP220"/> + + <_3:fileName>./src-separate/duk_api_debug.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>881f1b114c9fbba139044ddcbbc5398243185d10 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP64"/> + + <_3:fileName>./debugger/duk_opcodes.yaml + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>3fa5d72893eb3e1d42d07c8effc8bb1443f87dd6 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>105dd2e79d90b96b6b1ae419bff31ed6a0273af6 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>48eafa3f368fc879bfb79c2b2a30846a0b5d9b42 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>3f2b3b6b16cc969631b9980bc67aa6cc2c872048 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP314"/> + + <_3:fileName>./src-separate/duk_error_augment.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP364"/> + + <_3:fileName>./src-separate/duk_bi_thrower.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>6b98824e13a3d9536542b8da51d012a3d66ceec1 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>23f0401f57af7b8e4df6636637caffce6956445b + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP162"/> + + <_3:fileName>./examples/hello/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>457df68e290e23da40cac39a7cc35d6d6b4257c6 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP62"/> + + <_3:fileName>./debugger/duk_classnames.yaml + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>a4e2dfaee71b2316f2c3d149b4a507250b54dd96 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP214"/> + + <_3:fileName>./src-separate/duk_jmpbuf.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>089c395fca3cedceec09fd38d32d0c77dc8658f4 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>3f640daaa5e9d226a04fe419cb5eb1c07d06d346 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP48"/> + + <_3:fileName>./debugger/duk_debugerrors.yaml + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP370"/> + + <_3:fileName>./src-separate/duk_error_macros.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>4239a907720cf3620bd0b03eb43981e7182e305a + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP294"/> + + <_3:fileName>./src-separate/duk_hthread_builtins.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP76"/> + + <_3:fileName>./examples/coffee/mandel.coffee + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>6988aac432dcc185c2967732e21947ea0696377f + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>7b167c1ea560de7ac67e217c1fdff8912f8b677d + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>521f29841ddeb9be08ff9f7e2db06277da31bd47 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP156"/> + + <_3:fileName>./examples/alloc-torture/duk_alloc_torture.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP158"/> + + <_3:fileName>./examples/alloc-torture/duk_alloc_torture.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP72"/> + + <_3:fileName>./debugger/static/index.html + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>93e24d8abbf79fb6a2fcd54679c476866ad6ab2a + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:sourceInfo>Official duktape.org release built from GitHub repo https://github.com/svaarala/duktape. + <_3:downloadLocation rdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI">http://duktape.org/duktape-1.5.2.tar.xz + <_3:licenseDeclared rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:supplier>Organization: duktape.org + <_3:packageVerificationCode rdf:nodeID="svBMzJkP5"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + + <_3:description>Duktape is an embeddable Javascript engine, with a focus on portability and compact footprint + <_3:licenseComments>Duktape is copyrighted by its authors and licensed under the MIT license. MurmurHash2 is used internally, it is also under the MIT license. Duktape module loader is based on the CommonJS module loading specification (without sharing any code), CommonJS is under the MIT license. + <_3:originator>Organization: duktape.org + <_3:hasFile rdf:nodeID="svBMzJkP127"/> + <_3:hasFile rdf:nodeID="svBMzJkP101"/> + <_3:hasFile rdf:nodeID="svBMzJkP21"/> + <_3:hasFile rdf:nodeID="svBMzJkP337"/> + <_3:hasFile rdf:nodeID="svBMzJkP55"/> + <_3:hasFile rdf:nodeID="svBMzJkP91"/> + <_3:hasFile rdf:nodeID="svBMzJkP413"/> + <_3:hasFile rdf:nodeID="svBMzJkP321"/> + <_3:hasFile rdf:nodeID="svBMzJkP129"/> + <_3:hasFile rdf:nodeID="svBMzJkP139"/> + <_3:hasFile rdf:nodeID="svBMzJkP473"/> + <_3:hasFile rdf:nodeID="svBMzJkP357"/> + <_3:hasFile rdf:nodeID="svBMzJkP419"/> + <_3:hasFile rdf:nodeID="svBMzJkP451"/> + <_3:hasFile rdf:nodeID="svBMzJkP95"/> + <_3:hasFile rdf:nodeID="svBMzJkP115"/> + <_3:hasFile rdf:nodeID="svBMzJkP79"/> + <_3:hasFile rdf:nodeID="svBMzJkP275"/> + <_3:hasFile rdf:nodeID="svBMzJkP191"/> + <_3:hasFile rdf:nodeID="svBMzJkP261"/> + <_3:hasFile rdf:nodeID="svBMzJkP23"/> + <_3:hasFile rdf:nodeID="svBMzJkP353"/> + <_3:hasFile rdf:nodeID="svBMzJkP267"/> + <_3:hasFile rdf:nodeID="svBMzJkP427"/> + <_3:hasFile rdf:nodeID="svBMzJkP119"/> + <_3:hasFile rdf:nodeID="svBMzJkP205"/> + <_3:hasFile rdf:nodeID="svBMzJkP335"/> + <_3:hasFile rdf:nodeID="svBMzJkP211"/> + <_3:hasFile rdf:nodeID="svBMzJkP19"/> + <_3:hasFile rdf:nodeID="svBMzJkP149"/> + <_3:hasFile rdf:nodeID="svBMzJkP319"/> + <_3:hasFile rdf:nodeID="svBMzJkP367"/> + <_3:hasFile rdf:nodeID="svBMzJkP265"/> + <_3:hasFile rdf:nodeID="svBMzJkP441"/> + <_3:hasFile rdf:nodeID="svBMzJkP385"/> + <_3:hasFile rdf:nodeID="svBMzJkP289"/> + <_3:hasFile rdf:nodeID="svBMzJkP175"/> + <_3:hasFile rdf:nodeID="svBMzJkP277"/> + <_3:hasFile rdf:nodeID="svBMzJkP125"/> + <_3:hasFile rdf:nodeID="svBMzJkP471"/> + <_3:hasFile rdf:nodeID="svBMzJkP229"/> + <_3:hasFile rdf:nodeID="svBMzJkP143"/> + <_3:hasFile rdf:nodeID="svBMzJkP415"/> + <_3:hasFile rdf:nodeID="svBMzJkP137"/> + <_3:hasFile rdf:nodeID="svBMzJkP219"/> + <_3:hasFile rdf:nodeID="svBMzJkP37"/> + <_3:hasFile rdf:nodeID="svBMzJkP429"/> + <_3:hasFile rdf:nodeID="svBMzJkP485"/> + <_3:hasFile rdf:nodeID="svBMzJkP63"/> + <_3:hasFile rdf:nodeID="svBMzJkP295"/> + <_3:hasFile rdf:nodeID="svBMzJkP301"/> + <_3:hasFile rdf:nodeID="svBMzJkP231"/> + <_3:hasFile rdf:nodeID="svBMzJkP89"/> + <_3:hasFile rdf:nodeID="svBMzJkP35"/> + <_3:hasFile rdf:nodeID="svBMzJkP349"/> + <_3:hasFile rdf:nodeID="svBMzJkP297"/> + <_3:hasFile rdf:nodeID="svBMzJkP271"/> + <_3:hasFile rdf:nodeID="svBMzJkP171"/> + <_3:hasFile rdf:nodeID="svBMzJkP467"/> + <_3:hasFile rdf:nodeID="svBMzJkP393"/> + <_3:hasFile rdf:nodeID="svBMzJkP417"/> + <_3:hasFile rdf:nodeID="svBMzJkP331"/> + <_3:hasFile rdf:nodeID="svBMzJkP291"/> + <_3:hasFile rdf:nodeID="svBMzJkP185"/> + <_3:hasFile rdf:nodeID="svBMzJkP327"/> + <_3:hasFile rdf:nodeID="svBMzJkP313"/> + <_3:hasFile rdf:nodeID="svBMzJkP147"/> + <_3:hasFile rdf:nodeID="svBMzJkP363"/> + <_3:hasFile rdf:nodeID="svBMzJkP97"/> + <_3:hasFile rdf:nodeID="svBMzJkP49"/> + <_3:hasFile rdf:nodeID="svBMzJkP409"/> + <_3:hasFile rdf:nodeID="svBMzJkP41"/> + <_3:hasFile rdf:nodeID="svBMzJkP243"/> + <_3:hasFile rdf:nodeID="svBMzJkP151"/> + <_3:hasFile rdf:nodeID="svBMzJkP439"/> + <_3:hasFile rdf:nodeID="svBMzJkP213"/> + <_3:hasFile rdf:nodeID="svBMzJkP379"/> + <_3:hasFile rdf:nodeID="svBMzJkP131"/> + <_3:hasFile rdf:nodeID="svBMzJkP333"/> + <_3:hasFile rdf:nodeID="svBMzJkP47"/> + <_3:hasFile rdf:nodeID="svBMzJkP369"/> + <_3:hasFile rdf:nodeID="svBMzJkP209"/> + <_3:hasFile rdf:nodeID="svBMzJkP273"/> + <_3:hasFile rdf:nodeID="svBMzJkP31"/> + <_3:hasFile rdf:nodeID="svBMzJkP73"/> + <_3:hasFile rdf:nodeID="svBMzJkP293"/> + <_3:hasFile rdf:nodeID="svBMzJkP389"/> + <_3:hasFile rdf:nodeID="svBMzJkP27"/> + <_3:hasFile rdf:nodeID="svBMzJkP281"/> + <_3:hasFile rdf:nodeID="svBMzJkP259"/> + <_3:hasFile rdf:nodeID="svBMzJkP75"/> + <_3:hasFile rdf:nodeID="svBMzJkP443"/> + <_3:hasFile rdf:nodeID="svBMzJkP287"/> + <_3:hasFile rdf:nodeID="svBMzJkP383"/> + <_3:hasFile rdf:nodeID="svBMzJkP317"/> + <_3:hasFile rdf:nodeID="svBMzJkP69"/> + <_3:hasFile rdf:nodeID="svBMzJkP155"/> + <_3:hasFile rdf:nodeID="svBMzJkP157"/> + <_3:hasFile rdf:nodeID="svBMzJkP279"/> + <_3:hasFile rdf:nodeID="svBMzJkP215"/> + <_3:hasFile rdf:nodeID="svBMzJkP217"/> + <_3:hasFile rdf:nodeID="svBMzJkP71"/> + <_3:hasFile rdf:nodeID="svBMzJkP83"/> + <_3:hasFile rdf:nodeID="svBMzJkP199"/> + <_3:hasFile rdf:nodeID="svBMzJkP53"/> + <_3:hasFile rdf:nodeID="svBMzJkP105"/> + <_3:hasFile rdf:nodeID="svBMzJkP341"/> + <_3:hasFile rdf:nodeID="svBMzJkP195"/> + <_3:hasFile rdf:nodeID="svBMzJkP347"/> + <_3:hasFile rdf:nodeID="svBMzJkP253"/> + <_3:hasFile rdf:nodeID="svBMzJkP133"/> + <_3:hasFile rdf:nodeID="svBMzJkP305"/> + <_3:hasFile rdf:nodeID="svBMzJkP169"/> + <_3:hasFile rdf:nodeID="svBMzJkP465"/> + <_3:hasFile rdf:nodeID="svBMzJkP303"/> + <_3:hasFile rdf:nodeID="svBMzJkP187"/> + <_3:hasFile rdf:nodeID="svBMzJkP489"/> + <_3:hasFile rdf:nodeID="svBMzJkP351"/> + <_3:hasFile rdf:nodeID="svBMzJkP109"/> + <_3:hasFile rdf:nodeID="svBMzJkP103"/> + <_3:hasFile rdf:nodeID="svBMzJkP9"/> + <_3:hasFile rdf:nodeID="svBMzJkP17"/> + <_3:hasFile rdf:nodeID="svBMzJkP395"/> + <_3:hasFile rdf:nodeID="svBMzJkP381"/> + <_3:hasFile rdf:nodeID="svBMzJkP431"/> + <_3:hasFile rdf:nodeID="svBMzJkP343"/> + <_3:hasFile rdf:nodeID="svBMzJkP481"/> + <_3:hasFile rdf:nodeID="svBMzJkP251"/> + <_3:hasFile rdf:nodeID="svBMzJkP421"/> + <_3:hasFile rdf:nodeID="svBMzJkP189"/> + <_3:hasFile rdf:nodeID="svBMzJkP405"/> + <_3:hasFile rdf:nodeID="svBMzJkP299"/> + <_3:hasFile rdf:nodeID="svBMzJkP487"/> + <_3:hasFile rdf:nodeID="svBMzJkP93"/> + <_3:hasFile rdf:nodeID="svBMzJkP463"/> + <_3:hasFile rdf:nodeID="svBMzJkP67"/> + <_3:hasFile rdf:nodeID="svBMzJkP423"/> + <_3:hasFile rdf:nodeID="svBMzJkP329"/> + <_3:hasFile rdf:nodeID="svBMzJkP107"/> + <_3:hasFile rdf:nodeID="svBMzJkP165"/> + <_3:hasFile rdf:nodeID="svBMzJkP111"/> + <_3:hasFile rdf:nodeID="svBMzJkP355"/> + <_3:hasFile rdf:nodeID="svBMzJkP223"/> + <_3:hasFile rdf:nodeID="svBMzJkP483"/> + <_3:hasFile rdf:nodeID="svBMzJkP387"/> + <_3:hasFile rdf:nodeID="svBMzJkP255"/> + <_3:hasFile rdf:nodeID="svBMzJkP453"/> + <_3:hasFile rdf:nodeID="svBMzJkP391"/> + <_3:hasFile rdf:nodeID="svBMzJkP7"/> + <_3:hasFile rdf:nodeID="svBMzJkP249"/> + <_3:hasFile rdf:nodeID="svBMzJkP241"/> + <_3:hasFile rdf:nodeID="svBMzJkP197"/> + <_3:hasFile rdf:nodeID="svBMzJkP263"/> + <_3:hasFile rdf:nodeID="svBMzJkP177"/> + <_3:hasFile rdf:nodeID="svBMzJkP397"/> + <_3:hasFile rdf:nodeID="svBMzJkP245"/> + <_3:hasFile rdf:nodeID="svBMzJkP113"/> + <_3:hasFile rdf:nodeID="svBMzJkP475"/> + <_3:hasFile rdf:nodeID="svBMzJkP59"/> + <_3:hasFile rdf:nodeID="svBMzJkP247"/> + <_3:hasFile rdf:nodeID="svBMzJkP375"/> + <_3:hasFile rdf:nodeID="svBMzJkP437"/> + <_3:hasFile rdf:nodeID="svBMzJkP173"/> + <_3:hasFile rdf:nodeID="svBMzJkP163"/> + <_3:hasFile rdf:nodeID="svBMzJkP411"/> + <_3:hasFile rdf:nodeID="svBMzJkP373"/> + <_3:hasFile rdf:nodeID="svBMzJkP371"/> + <_3:hasFile rdf:nodeID="svBMzJkP65"/> + <_3:hasFile rdf:nodeID="svBMzJkP459"/> + <_3:hasFile rdf:nodeID="svBMzJkP461"/> + <_3:hasFile rdf:nodeID="svBMzJkP257"/> + <_3:hasFile rdf:nodeID="svBMzJkP325"/> + <_3:hasFile rdf:nodeID="svBMzJkP43"/> + <_3:hasFile rdf:nodeID="svBMzJkP45"/> + <_3:hasFile rdf:nodeID="svBMzJkP51"/> + <_3:hasFile rdf:nodeID="svBMzJkP457"/> + <_3:hasFile rdf:nodeID="svBMzJkP365"/> + <_3:hasFile rdf:nodeID="svBMzJkP167"/> + <_3:hasFile rdf:nodeID="svBMzJkP479"/> + <_3:hasFile rdf:nodeID="svBMzJkP233"/> + <_3:hasFile rdf:nodeID="svBMzJkP135"/> + <_3:hasFile rdf:nodeID="svBMzJkP377"/> + <_3:hasFile rdf:nodeID="svBMzJkP309"/> + <_3:hasFile rdf:nodeID="svBMzJkP235"/> + <_3:hasFile rdf:nodeID="svBMzJkP207"/> + <_3:hasFile rdf:nodeID="svBMzJkP339"/> + <_3:hasFile rdf:nodeID="svBMzJkP237"/> + <_3:hasFile rdf:nodeID="svBMzJkP407"/> + <_3:hasFile rdf:nodeID="svBMzJkP269"/> + <_3:hasFile rdf:nodeID="svBMzJkP13"/> + <_3:hasFile rdf:nodeID="svBMzJkP307"/> + <_3:hasFile rdf:nodeID="svBMzJkP123"/> + <_3:hasFile rdf:nodeID="svBMzJkP221"/> + <_3:hasFile rdf:nodeID="svBMzJkP345"/> + <_3:hasFile rdf:nodeID="svBMzJkP323"/> + <_3:hasFile rdf:nodeID="svBMzJkP57"/> + <_3:hasFile rdf:nodeID="svBMzJkP193"/> + <_3:hasFile rdf:nodeID="svBMzJkP77"/> + <_3:hasFile rdf:nodeID="svBMzJkP433"/> + <_3:hasFile rdf:nodeID="svBMzJkP39"/> + <_3:hasFile rdf:nodeID="svBMzJkP401"/> + <_3:hasFile rdf:nodeID="svBMzJkP161"/> + <_3:hasFile rdf:nodeID="svBMzJkP283"/> + <_3:hasFile rdf:nodeID="svBMzJkP227"/> + <_3:hasFile rdf:nodeID="svBMzJkP445"/> + <_3:hasFile rdf:nodeID="svBMzJkP425"/> + <_3:hasFile rdf:nodeID="svBMzJkP117"/> + <_3:hasFile rdf:nodeID="svBMzJkP203"/> + <_3:hasFile rdf:nodeID="svBMzJkP403"/> + <_3:hasFile rdf:nodeID="svBMzJkP447"/> + <_3:hasFile rdf:nodeID="svBMzJkP183"/> + <_3:hasFile rdf:nodeID="svBMzJkP87"/> + <_3:hasFile rdf:nodeID="svBMzJkP159"/> + <_3:hasFile rdf:nodeID="svBMzJkP361"/> + <_3:hasFile rdf:nodeID="svBMzJkP145"/> + <_3:hasFile rdf:nodeID="svBMzJkP491"/> + <_3:hasFile rdf:nodeID="svBMzJkP225"/> + <_3:hasFile rdf:nodeID="svBMzJkP61"/> + <_3:hasFile rdf:nodeID="svBMzJkP33"/> + <_3:hasFile rdf:nodeID="svBMzJkP311"/> + <_3:hasFile rdf:nodeID="svBMzJkP239"/> + <_3:hasFile rdf:nodeID="svBMzJkP85"/> + <_3:hasFile rdf:nodeID="svBMzJkP15"/> + <_3:hasFile rdf:nodeID="svBMzJkP81"/> + <_3:hasFile rdf:nodeID="svBMzJkP121"/> + <_3:hasFile rdf:nodeID="svBMzJkP455"/> + <_3:hasFile rdf:nodeID="svBMzJkP359"/> + <_3:hasFile rdf:nodeID="svBMzJkP25"/> + <_3:hasFile rdf:nodeID="svBMzJkP469"/> + <_3:hasFile rdf:nodeID="svBMzJkP477"/> + <_3:hasFile rdf:nodeID="svBMzJkP29"/> + <_3:hasFile rdf:nodeID="svBMzJkP141"/> + <_3:hasFile rdf:nodeID="svBMzJkP285"/> + <_3:hasFile rdf:nodeID="svBMzJkP11"/> + <_3:hasFile rdf:nodeID="svBMzJkP449"/> + <_3:hasFile rdf:nodeID="svBMzJkP435"/> + <_3:hasFile rdf:nodeID="svBMzJkP99"/> + <_3:hasFile rdf:nodeID="svBMzJkP201"/> + <_3:hasFile rdf:nodeID="svBMzJkP181"/> + <_3:hasFile rdf:nodeID="svBMzJkP315"/> + <_3:hasFile rdf:nodeID="svBMzJkP179"/> + <_3:hasFile rdf:nodeID="svBMzJkP399"/> + <_3:hasFile rdf:nodeID="svBMzJkP153"/> + <_3:packageFileName>duktape-1.5.2.tar.xz + <_3:name>Duktape + <_3:homePage rdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI">http://duktape.org/ + <_3:versionInfo>1.5.2 + <_3:summary>Duktape Ecmascript interpreter + <_3:licenseInfoFromFiles rdf:resource="http://spdx.org/licenses/MIT"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>af6b0cd61c02efa575b9106682606297d26f013b + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>ddcaaa8f260ff00f22e09d7fb4b4b7ca94858f30 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>bdb9243b436ef5c70967bddd16d0cc9e74971b1c + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>cb09ad82ed76b6fcaf0ac32db698c1ce50e0a508 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>b3919a740c97b1d3ceb4e03c9ae53456916222a4 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>a20231719c1f9b97ea38fbc42dd8af1e21d74879 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>62b2d820f9a27df439cd24ac8766e680d27a95da + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>95a06fb24eca405019eb7b7d535179245eda6099 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>ea413ed7d47cf27c6f3ffe25956b1370b653c200 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>0bba41f542f2279b6bfa297ba4e7ee824e3ebd78 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP422"/> + + <_3:fileName>./src-separate/duk_js_ops.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP8"/> + + <_3:fileName>./LICENSE.txt + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>50a3349a079123b6bddf3db507df0dd9e877e246 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP300"/> + + <_3:fileName>./src-separate/duk_bi_logger.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>64566ae0bdc1c3e1017604b6b45d515c30d976eb + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP464"/> + + <_3:fileName>./polyfills/console-minimal.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP224"/> + + <_3:fileName>./src-separate/duk_bi_global.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>2c86a9ed51255ba3b0b225e48c093851a78cae71 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>742635fda10ef9775864077e647b59442768a69c + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP250"/> + + <_3:fileName>./src-separate/duk_api_call.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP398"/> + + <_3:fileName>./src-separate/duk_bi_regexp.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP164"/> + + <_3:fileName>./examples/cmdline/duk_cmdline_ajduk.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP412"/> + + <_3:fileName>./src-separate/duk_heaphdr.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>5ab84f0ff2ed8c4b4a2daa5957d2a20447887103 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>8483a2211d6323480efa605ee646959695b742c6 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>fba891f21dc0b80d282f16d39c37007965adcd04 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>47e1001460004f53dfc350355e02bf56f18b0398 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP114"/> + + <_3:fileName>./examples/eventloop/fileio.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>d529a6faac560def1e5b2cdb991713a6d70b0e26 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>b7be4a3a943099fdf318480615d1c130be87a841 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>a9493898a19810a5450d2442d1ff1dfb4ff76d1c + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>ee1f62688128fef0954cc9b93e41c199212e5a82 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP66"/> + + <_3:fileName>./debugger/merge_debug_meta.py + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>bc9d119b5e66085e7e4219685fd351fb3f68d800 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP196"/> + + <_3:fileName>./examples/alloc-logging/duk_alloc_logging.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>aa1f72e7a2b4891ec1f445eb031dfba69bdc85de + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>1f430f45a3e52e4327eb17b2213a6f473dc1f6d2 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>9b7de9f9f25d095dfcffbf2d7629cd772fe632f0 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP402"/> + + <_3:fileName>./src-separate/duk_bi_date_windows.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP480"/> + + <_3:fileName>./licenses/murmurhash2.txt + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>88ab1d060886393d94b6fb7c6054a0189117340b + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>d32ab97622eaf870648dfdf8e5b9cb034740affa + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>2e9dca0919a92c68ed30593292b3fe7140480062 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>d5f1184bc60b9049b0dbf1e51ae6a5fe7e5e3192 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>f3ed5c785b60b0f39a74cc3f5b73b0c6e24287c3 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP426"/> + + <_3:fileName>./src-separate/duk_bi_object.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP204"/> + + <_3:fileName>./src-separate/duk_unicode_support.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP160"/> + + <_3:fileName>./examples/hello/hello.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP492"/> + + <_3:fileName>./src/duktape.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP392"/> + + <_3:fileName>./src-separate/duk_bi_duktape.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP34"/> + + <_3:fileName>./Makefile.eventloop + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP312"/> + + <_3:fileName>./src-separate/duk_selftest.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP116"/> + + <_3:fileName>./examples/eventloop/ecma_eventloop.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>01138dd2786346a159c06e43f094d07f002df881 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP406"/> + + <_3:fileName>./src-separate/duk_api_buffer.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP142"/> + + <_3:fileName>./examples/guide/processlines.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>c763ffca7e15df01f44cf89518eb870380b03ee8 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP100"/> + + <_3:fileName>./examples/jxpretty/jxpretty.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>29b1960bc40607c2cf496465068611b2ffc4de87 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>55aa8c67f1fa418f8d625a32a9d6268e9d94429c + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP334"/> + + <_3:fileName>./src-separate/duk_debug_vsnprintf.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP22"/> + + <_3:fileName>./Makefile.coffee + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP212"/> + + <_3:fileName>./src-separate/duk_numconv.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP414"/> + + <_3:fileName>./src-separate/duk_bi_proxy.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>0783cf4f48c9baf2b3820a36ab276b01e2102600 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP130"/> + + <_3:fileName>./examples/dummy-date-provider/dummy_date_provider.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP140"/> + + <_3:fileName>./examples/guide/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>7edf7600e1a6f09d1e4a0e2f9be418b59fff7c52 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP96"/> + + <_3:fileName>./examples/codepage-conv/duk_codepage_conv.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>61043b4a6f3cf86375ea6e34ebf83b550a1935b2 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>13e6591c152934ace923d9e49cc52872c8a515b9 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP262"/> + + <_3:fileName>./src-separate/duk_hobject_alloc.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>ca9e1a8f73c5dcbe2e8c45d25ce9865cd93866e6 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>e17ef51b2a16cee731cf341ce08d04690da095ce + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP268"/> + + <_3:fileName>./src-separate/duk_js_call.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>07a167411a356aebfbe987353846a9c711998f20 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP20"/> + + <_3:fileName>./Makefile.cmdline + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP320"/> + + <_3:fileName>./src-separate/duk_hnativefunction.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>4fa87de84388dcd955ce27acbe2c2026babf32bc + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>9a31edd2028d39d24aa9bfdbd2b70ce2a64b8493 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP360"/> + + <_3:fileName>./src-separate/duk_hthread_alloc.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP176"/> + + <_3:fileName>./examples/debug-trans-socket/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP230"/> + + <_3:fileName>./src-separate/duk_util.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP24"/> + + <_3:fileName>./Makefile.codepage + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>9bcb9de5ed729ecc7093ab48e4f5c04e9704bb30 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP38"/> + + <_3:fileName>./src-noline/metadata.json + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>494667f18c30e4fd36245e6c4e27f93fcf9df87d + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP394"/> + + <_3:fileName>./src-separate/duk_heap_memory.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP296"/> + + <_3:fileName>./src-separate/duk_hobject_class.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>f88d68880f451267e7e2eca4b6c4191e8c3fe011 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP36"/> + + <_3:fileName>./Makefile.jxpretty + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>714e6fe76e2aed0bbb54f6c5e3674472813e4382 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP298"/> + + <_3:fileName>./src-separate/duk_bi_protos.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP172"/> + + <_3:fileName>./examples/debug-trans-socket/duk_trans_socket_unix.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP468"/> + + <_3:fileName>./config/genconfig.py + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>0ec3478b1e5713ffdbe7eb45cd5bb96c1a8a774b + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>7c438de1ba7ca2ef226340d28ec0071ece1c9e43 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>4db4ca2d7ce3f076fd195372d53e8bc5714d2597 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>a8f8cf615ef79337a83eb791477dc9ed2c22822c + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>d869c0f8797a659124b3ba31c75f494b15bea5e6 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP274"/> + + <_3:fileName>./src-separate/duk_hobject_pc2line.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP390"/> + + <_3:fileName>./src-separate/duk_debugger.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP282"/> + + <_3:fileName>./src-separate/duk_util_bitdecoder.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>5bf6196d028d10a8f2f8e3da67b7d59bb8fd9eff + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP302"/> + + <_3:fileName>./src-separate/duk_unicode_tables.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>29be453689679c9bab977c0b333a4a224d9f02e6 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>d535c4e27319a5db6026553a801e8a5ef0e6fe88 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP216"/> + + <_3:fileName>./src-separate/duk_hobject_misc.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>fcdae530da6f6dfafd21219de294c547b91dd866 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>8521b45dcbc72d66c2b4e3f0db9e1dbfb75b45f5 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP84"/> + + <_3:fileName>./examples/alloc-hybrid/duk_alloc_hybrid.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>3b8236810650f4e28bd84984b3c7e55029f241b0 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP454"/> + + <_3:fileName>./polyfills/duktape-error-setter-writable.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>cd7bd1908b56fe13dc63ac64b53ca793221f9359 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP338"/> + + <_3:fileName>./src-separate/duk_heap_markandsweep.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP466"/> + + <_3:fileName>./polyfills/performance-now.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP304"/> + + <_3:fileName>./src-separate/duk_exception.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP188"/> + + <_3:fileName>./examples/sandbox/sandbox.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP110"/> + + <_3:fileName>./examples/eventloop/c_eventloop.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP18"/> + + <_3:fileName>./mandel.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP396"/> + + <_3:fileName>./src-separate/duk_hbufferobject.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>6d5f56a1b76f9dcc9bb68d62ad0df958bd4d38ec + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>10c24a17c2f5f82d8c1a8fab8673c0c9075c1817 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>14ec83aeef66a38c6a4c48a175a544f66d319413 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>2c508fbc7eb127f6793cfe62bf947879668983a4 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>01d4eb01e522172282a4a0ea3aa06f33ce7bec3c + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP94"/> + + <_3:fileName>./examples/codepage-conv/test.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>4baffeb2aa785bdc1065eb9ddb8c64c8a028655d + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>ed3bd07dbd103d285277ab38c4b4a7dd4a4b654b + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP380"/> + + <_3:fileName>./src-separate/duk_bi_array.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP112"/> + + <_3:fileName>./examples/eventloop/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>9bb6ca1fa3ad4c593b000b2de5aca013b95adda0 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>9406efff4ddfa3fe00ae52c28963df486fc48dfa + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>ebbb5b2489ccab8f4a6ef741aac3eafac4c49ebd + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>c2d899e7f2343579f29ad3079fde44eec5d6a544 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP198"/> + + <_3:fileName>./examples/alloc-logging/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>0053a77d6c3562af6d2238f096f3a9bd4b5a834f + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>82ae634976f8641d5582058879d42d843713927b + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>11f16b15a8e52598a078b88a8d0440ff61dd5ba2 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP14"/> + + <_3:fileName>./README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>d650b46e62e2579b3f4c16ee9b5a8abd1c8241b5 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>11ecfff4142b35382d0aaf80ee6c04ae3dfd0b4c + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>0610275779180357a50777f77d1de0e0478f23a7 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>0221b13981af62e7382a806c24f92450597b6106 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>4e5a54de08355669dc98b5d4ad379384b33da4e5 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP458"/> + + <_3:fileName>./polyfills/object-assign.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + SPDX license for Duktape 1.5.2 + <_3:referencesFile rdf:nodeID="svBMzJkP127"/> + <_3:referencesFile rdf:nodeID="svBMzJkP101"/> + <_3:referencesFile rdf:nodeID="svBMzJkP21"/> + <_3:referencesFile rdf:nodeID="svBMzJkP337"/> + <_3:referencesFile rdf:nodeID="svBMzJkP55"/> + <_3:referencesFile rdf:nodeID="svBMzJkP91"/> + <_3:referencesFile rdf:nodeID="svBMzJkP413"/> + <_3:referencesFile rdf:nodeID="svBMzJkP321"/> + <_3:referencesFile rdf:nodeID="svBMzJkP129"/> + <_3:referencesFile rdf:nodeID="svBMzJkP139"/> + <_3:referencesFile rdf:nodeID="svBMzJkP473"/> + <_3:referencesFile rdf:nodeID="svBMzJkP357"/> + <_3:referencesFile rdf:nodeID="svBMzJkP419"/> + <_3:referencesFile rdf:nodeID="svBMzJkP451"/> + <_3:referencesFile rdf:nodeID="svBMzJkP95"/> + <_3:referencesFile rdf:nodeID="svBMzJkP115"/> + <_3:referencesFile rdf:nodeID="svBMzJkP79"/> + <_3:referencesFile rdf:nodeID="svBMzJkP275"/> + <_3:referencesFile rdf:nodeID="svBMzJkP191"/> + <_3:referencesFile rdf:nodeID="svBMzJkP261"/> + <_3:referencesFile rdf:nodeID="svBMzJkP23"/> + <_3:referencesFile rdf:nodeID="svBMzJkP353"/> + <_3:referencesFile rdf:nodeID="svBMzJkP267"/> + <_3:referencesFile rdf:nodeID="svBMzJkP427"/> + <_3:referencesFile rdf:nodeID="svBMzJkP119"/> + <_3:referencesFile rdf:nodeID="svBMzJkP205"/> + <_3:referencesFile rdf:nodeID="svBMzJkP335"/> + <_3:referencesFile rdf:nodeID="svBMzJkP211"/> + <_3:referencesFile rdf:nodeID="svBMzJkP19"/> + <_3:referencesFile rdf:nodeID="svBMzJkP149"/> + <_3:referencesFile rdf:nodeID="svBMzJkP319"/> + <_3:referencesFile rdf:nodeID="svBMzJkP367"/> + <_3:referencesFile rdf:nodeID="svBMzJkP265"/> + <_3:referencesFile rdf:nodeID="svBMzJkP441"/> + <_3:referencesFile rdf:nodeID="svBMzJkP385"/> + <_3:referencesFile rdf:nodeID="svBMzJkP289"/> + <_3:referencesFile rdf:nodeID="svBMzJkP175"/> + <_3:referencesFile rdf:nodeID="svBMzJkP277"/> + <_3:referencesFile rdf:nodeID="svBMzJkP125"/> + <_3:referencesFile rdf:nodeID="svBMzJkP471"/> + <_3:referencesFile rdf:nodeID="svBMzJkP229"/> + <_3:referencesFile rdf:nodeID="svBMzJkP143"/> + <_3:referencesFile rdf:nodeID="svBMzJkP415"/> + <_3:referencesFile rdf:nodeID="svBMzJkP137"/> + <_3:referencesFile rdf:nodeID="svBMzJkP219"/> + <_3:referencesFile rdf:nodeID="svBMzJkP37"/> + <_3:referencesFile rdf:nodeID="svBMzJkP429"/> + <_3:referencesFile rdf:nodeID="svBMzJkP485"/> + <_3:referencesFile rdf:nodeID="svBMzJkP63"/> + <_3:referencesFile rdf:nodeID="svBMzJkP295"/> + <_3:referencesFile rdf:nodeID="svBMzJkP301"/> + <_3:referencesFile rdf:nodeID="svBMzJkP231"/> + <_3:referencesFile rdf:nodeID="svBMzJkP89"/> + <_3:referencesFile rdf:nodeID="svBMzJkP35"/> + <_3:referencesFile rdf:nodeID="svBMzJkP349"/> + <_3:referencesFile rdf:nodeID="svBMzJkP297"/> + <_3:referencesFile rdf:nodeID="svBMzJkP271"/> + <_3:referencesFile rdf:nodeID="svBMzJkP171"/> + <_3:referencesFile rdf:nodeID="svBMzJkP467"/> + <_3:referencesFile rdf:nodeID="svBMzJkP393"/> + <_3:referencesFile rdf:nodeID="svBMzJkP417"/> + <_3:referencesFile rdf:nodeID="svBMzJkP331"/> + <_3:referencesFile rdf:nodeID="svBMzJkP291"/> + <_3:referencesFile rdf:nodeID="svBMzJkP185"/> + <_3:referencesFile rdf:nodeID="svBMzJkP327"/> + <_3:referencesFile rdf:nodeID="svBMzJkP313"/> + <_3:referencesFile rdf:nodeID="svBMzJkP147"/> + <_3:referencesFile rdf:nodeID="svBMzJkP363"/> + <_3:referencesFile rdf:nodeID="svBMzJkP97"/> + <_3:referencesFile rdf:nodeID="svBMzJkP49"/> + <_3:referencesFile rdf:nodeID="svBMzJkP409"/> + <_3:referencesFile rdf:nodeID="svBMzJkP41"/> + <_3:referencesFile rdf:nodeID="svBMzJkP243"/> + <_3:referencesFile rdf:nodeID="svBMzJkP151"/> + <_3:referencesFile rdf:nodeID="svBMzJkP439"/> + <_3:referencesFile rdf:nodeID="svBMzJkP213"/> + <_3:referencesFile rdf:nodeID="svBMzJkP379"/> + <_3:referencesFile rdf:nodeID="svBMzJkP131"/> + <_3:referencesFile rdf:nodeID="svBMzJkP333"/> + <_3:referencesFile rdf:nodeID="svBMzJkP47"/> + <_3:referencesFile rdf:nodeID="svBMzJkP369"/> + <_3:referencesFile rdf:nodeID="svBMzJkP209"/> + <_3:referencesFile rdf:nodeID="svBMzJkP273"/> + <_3:referencesFile rdf:nodeID="svBMzJkP31"/> + <_3:referencesFile rdf:nodeID="svBMzJkP73"/> + <_3:referencesFile rdf:nodeID="svBMzJkP293"/> + <_3:referencesFile rdf:nodeID="svBMzJkP389"/> + <_3:referencesFile rdf:nodeID="svBMzJkP27"/> + <_3:referencesFile rdf:nodeID="svBMzJkP281"/> + <_3:referencesFile rdf:nodeID="svBMzJkP259"/> + <_3:referencesFile rdf:nodeID="svBMzJkP75"/> + <_3:referencesFile rdf:nodeID="svBMzJkP443"/> + <_3:referencesFile rdf:nodeID="svBMzJkP287"/> + <_3:referencesFile rdf:nodeID="svBMzJkP383"/> + <_3:referencesFile rdf:nodeID="svBMzJkP317"/> + <_3:referencesFile rdf:nodeID="svBMzJkP69"/> + <_3:referencesFile rdf:nodeID="svBMzJkP155"/> + <_3:referencesFile rdf:nodeID="svBMzJkP157"/> + <_3:referencesFile rdf:nodeID="svBMzJkP279"/> + <_3:referencesFile rdf:nodeID="svBMzJkP215"/> + <_3:referencesFile rdf:nodeID="svBMzJkP217"/> + <_3:referencesFile rdf:nodeID="svBMzJkP71"/> + <_3:referencesFile rdf:nodeID="svBMzJkP83"/> + <_3:referencesFile rdf:nodeID="svBMzJkP199"/> + <_3:referencesFile rdf:nodeID="svBMzJkP53"/> + <_3:referencesFile rdf:nodeID="svBMzJkP105"/> + <_3:referencesFile rdf:nodeID="svBMzJkP341"/> + <_3:referencesFile rdf:nodeID="svBMzJkP195"/> + <_3:referencesFile rdf:nodeID="svBMzJkP347"/> + <_3:referencesFile rdf:nodeID="svBMzJkP253"/> + <_3:referencesFile rdf:nodeID="svBMzJkP133"/> + <_3:referencesFile rdf:nodeID="svBMzJkP305"/> + <_3:referencesFile rdf:nodeID="svBMzJkP169"/> + <_3:referencesFile rdf:nodeID="svBMzJkP465"/> + <_3:referencesFile rdf:nodeID="svBMzJkP303"/> + <_3:referencesFile rdf:nodeID="svBMzJkP187"/> + <_3:referencesFile rdf:nodeID="svBMzJkP489"/> + <_3:referencesFile rdf:nodeID="svBMzJkP351"/> + <_3:referencesFile rdf:nodeID="svBMzJkP109"/> + <_3:referencesFile rdf:nodeID="svBMzJkP103"/> + <_3:referencesFile rdf:nodeID="svBMzJkP9"/> + <_3:referencesFile rdf:nodeID="svBMzJkP17"/> + <_3:referencesFile rdf:nodeID="svBMzJkP395"/> + <_3:referencesFile rdf:nodeID="svBMzJkP381"/> + <_3:referencesFile rdf:nodeID="svBMzJkP431"/> + <_3:referencesFile rdf:nodeID="svBMzJkP343"/> + <_3:referencesFile rdf:nodeID="svBMzJkP481"/> + <_3:referencesFile rdf:nodeID="svBMzJkP251"/> + <_3:referencesFile rdf:nodeID="svBMzJkP421"/> + <_3:referencesFile rdf:nodeID="svBMzJkP189"/> + <_3:referencesFile rdf:nodeID="svBMzJkP405"/> + <_3:referencesFile rdf:nodeID="svBMzJkP299"/> + <_3:referencesFile rdf:nodeID="svBMzJkP487"/> + <_3:referencesFile rdf:nodeID="svBMzJkP93"/> + <_3:referencesFile rdf:nodeID="svBMzJkP463"/> + <_3:referencesFile rdf:nodeID="svBMzJkP67"/> + <_3:referencesFile rdf:nodeID="svBMzJkP423"/> + <_3:referencesFile rdf:nodeID="svBMzJkP329"/> + <_3:referencesFile rdf:nodeID="svBMzJkP107"/> + <_3:referencesFile rdf:nodeID="svBMzJkP165"/> + <_3:referencesFile rdf:nodeID="svBMzJkP111"/> + <_3:referencesFile rdf:nodeID="svBMzJkP355"/> + <_3:referencesFile rdf:nodeID="svBMzJkP223"/> + <_3:referencesFile rdf:nodeID="svBMzJkP483"/> + <_3:referencesFile rdf:nodeID="svBMzJkP387"/> + <_3:referencesFile rdf:nodeID="svBMzJkP255"/> + <_3:referencesFile rdf:nodeID="svBMzJkP453"/> + <_3:referencesFile rdf:nodeID="svBMzJkP391"/> + <_3:referencesFile rdf:nodeID="svBMzJkP7"/> + <_3:referencesFile rdf:nodeID="svBMzJkP249"/> + <_3:referencesFile rdf:nodeID="svBMzJkP241"/> + <_3:referencesFile rdf:nodeID="svBMzJkP197"/> + <_3:referencesFile rdf:nodeID="svBMzJkP263"/> + <_3:referencesFile rdf:nodeID="svBMzJkP177"/> + <_3:referencesFile rdf:nodeID="svBMzJkP397"/> + <_3:referencesFile rdf:nodeID="svBMzJkP245"/> + <_3:referencesFile rdf:nodeID="svBMzJkP113"/> + <_3:referencesFile rdf:nodeID="svBMzJkP475"/> + <_3:referencesFile rdf:nodeID="svBMzJkP59"/> + <_3:referencesFile rdf:nodeID="svBMzJkP247"/> + <_3:referencesFile rdf:nodeID="svBMzJkP375"/> + <_3:referencesFile rdf:nodeID="svBMzJkP437"/> + <_3:referencesFile rdf:nodeID="svBMzJkP173"/> + <_3:referencesFile rdf:nodeID="svBMzJkP163"/> + <_3:referencesFile rdf:nodeID="svBMzJkP411"/> + <_3:referencesFile rdf:nodeID="svBMzJkP373"/> + <_3:referencesFile rdf:nodeID="svBMzJkP371"/> + <_3:referencesFile rdf:nodeID="svBMzJkP65"/> + <_3:referencesFile rdf:nodeID="svBMzJkP459"/> + <_3:referencesFile rdf:nodeID="svBMzJkP461"/> + <_3:referencesFile rdf:nodeID="svBMzJkP257"/> + <_3:referencesFile rdf:nodeID="svBMzJkP325"/> + <_3:referencesFile rdf:nodeID="svBMzJkP43"/> + <_3:referencesFile rdf:nodeID="svBMzJkP45"/> + <_3:referencesFile rdf:nodeID="svBMzJkP51"/> + <_3:referencesFile rdf:nodeID="svBMzJkP457"/> + <_3:referencesFile rdf:nodeID="svBMzJkP365"/> + <_3:referencesFile rdf:nodeID="svBMzJkP167"/> + <_3:referencesFile rdf:nodeID="svBMzJkP479"/> + <_3:referencesFile rdf:nodeID="svBMzJkP233"/> + <_3:referencesFile rdf:nodeID="svBMzJkP135"/> + <_3:referencesFile rdf:nodeID="svBMzJkP377"/> + <_3:referencesFile rdf:nodeID="svBMzJkP309"/> + <_3:referencesFile rdf:nodeID="svBMzJkP235"/> + <_3:referencesFile rdf:nodeID="svBMzJkP207"/> + <_3:referencesFile rdf:nodeID="svBMzJkP339"/> + <_3:referencesFile rdf:nodeID="svBMzJkP237"/> + <_3:referencesFile rdf:nodeID="svBMzJkP407"/> + <_3:referencesFile rdf:nodeID="svBMzJkP269"/> + <_3:referencesFile rdf:nodeID="svBMzJkP13"/> + <_3:referencesFile rdf:nodeID="svBMzJkP307"/> + <_3:referencesFile rdf:nodeID="svBMzJkP123"/> + <_3:referencesFile rdf:nodeID="svBMzJkP221"/> + <_3:referencesFile rdf:nodeID="svBMzJkP345"/> + <_3:referencesFile rdf:nodeID="svBMzJkP323"/> + <_3:referencesFile rdf:nodeID="svBMzJkP57"/> + <_3:referencesFile rdf:nodeID="svBMzJkP193"/> + <_3:referencesFile rdf:nodeID="svBMzJkP77"/> + <_3:referencesFile rdf:nodeID="svBMzJkP433"/> + <_3:referencesFile rdf:nodeID="svBMzJkP39"/> + <_3:referencesFile rdf:nodeID="svBMzJkP401"/> + <_3:referencesFile rdf:nodeID="svBMzJkP161"/> + <_3:referencesFile rdf:nodeID="svBMzJkP283"/> + <_3:referencesFile rdf:nodeID="svBMzJkP227"/> + <_3:referencesFile rdf:nodeID="svBMzJkP445"/> + <_3:referencesFile rdf:nodeID="svBMzJkP425"/> + <_3:referencesFile rdf:nodeID="svBMzJkP117"/> + <_3:referencesFile rdf:nodeID="svBMzJkP203"/> + <_3:referencesFile rdf:nodeID="svBMzJkP403"/> + <_3:referencesFile rdf:nodeID="svBMzJkP447"/> + <_3:referencesFile rdf:nodeID="svBMzJkP183"/> + <_3:referencesFile rdf:nodeID="svBMzJkP87"/> + <_3:referencesFile rdf:nodeID="svBMzJkP159"/> + <_3:referencesFile rdf:nodeID="svBMzJkP361"/> + <_3:referencesFile rdf:nodeID="svBMzJkP145"/> + <_3:referencesFile rdf:nodeID="svBMzJkP491"/> + <_3:referencesFile rdf:nodeID="svBMzJkP225"/> + <_3:referencesFile rdf:nodeID="svBMzJkP61"/> + <_3:referencesFile rdf:nodeID="svBMzJkP33"/> + <_3:referencesFile rdf:nodeID="svBMzJkP311"/> + <_3:referencesFile rdf:nodeID="svBMzJkP239"/> + <_3:referencesFile rdf:nodeID="svBMzJkP85"/> + <_3:referencesFile rdf:nodeID="svBMzJkP15"/> + <_3:referencesFile rdf:nodeID="svBMzJkP81"/> + <_3:referencesFile rdf:nodeID="svBMzJkP121"/> + <_3:referencesFile rdf:nodeID="svBMzJkP455"/> + <_3:referencesFile rdf:nodeID="svBMzJkP359"/> + <_3:referencesFile rdf:nodeID="svBMzJkP25"/> + <_3:referencesFile rdf:nodeID="svBMzJkP469"/> + <_3:referencesFile rdf:nodeID="svBMzJkP477"/> + <_3:referencesFile rdf:nodeID="svBMzJkP29"/> + <_3:referencesFile rdf:nodeID="svBMzJkP141"/> + <_3:referencesFile rdf:nodeID="svBMzJkP285"/> + <_3:referencesFile rdf:nodeID="svBMzJkP11"/> + <_3:referencesFile rdf:nodeID="svBMzJkP449"/> + <_3:referencesFile rdf:nodeID="svBMzJkP435"/> + <_3:referencesFile rdf:nodeID="svBMzJkP99"/> + <_3:referencesFile rdf:nodeID="svBMzJkP201"/> + <_3:referencesFile rdf:nodeID="svBMzJkP181"/> + <_3:referencesFile rdf:nodeID="svBMzJkP315"/> + <_3:referencesFile rdf:nodeID="svBMzJkP179"/> + <_3:referencesFile rdf:nodeID="svBMzJkP399"/> + <_3:referencesFile rdf:nodeID="svBMzJkP153"/> + + <_3:dataLicense rdf:resource="http://spdx.org/licenses/CC0-1.0"/> + <_3:creationInfo rdf:nodeID="svBMzJkP3"/> + <_3:describesPackage rdf:nodeID="svBMzJkP4"/> + <_3:specVersion>SPDX-1.2 + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP382"/> + + <_3:fileName>./src-separate/duk_api_codec.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP234"/> + + <_3:fileName>./src-separate/duk_hthread_misc.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP236"/> + + <_3:fileName>./src-separate/duk_js.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP208"/> + + <_3:fileName>./src-separate/duk_regexp_executor.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>d49c1cdb51b3a4fdd823c13831617a1f0a93edd1 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>ef83823d07ee05f2373502187af9700d5c2aa952 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP238"/> + + <_3:fileName>./src-separate/duk_js_compiler.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>982e40839854be361e3b57b5c07ef4f710ab749b + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP308"/> + + <_3:fileName>./src-separate/duk_replacements.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP124"/> + + <_3:fileName>./examples/eventloop/client-socket-test.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP310"/> + + <_3:fileName>./src-separate/duk_util_hashprime.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP348"/> + + <_3:fileName>./src-separate/duk_json.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP150"/> + + <_3:fileName>./examples/guide/primecheck.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP186"/> + + <_3:fileName>./examples/debug-trans-dvalue/test.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>f788091973ac91ad9562aaadac9c2de87af2ba76 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>e70f9a590f4288c0feee56aa2c421cbc23036a1e + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>75c61dd080cd643b7449f673105b7b7e4bdda0f5 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>215f6fec820889330048f7f2c1b5a220eb963657 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP448"/> + + <_3:fileName>./src-separate/duk_bi_pointer.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP88"/> + + <_3:fileName>./examples/alloc-hybrid/duk_alloc_hybrid.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>2321e0a68bf940f8346d00adda9e4a58fa51f608 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP226"/> + + <_3:fileName>./src-separate/duk_bi_function.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP16"/> + + <_3:fileName>./Makefile.hello + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP82"/> + + <_3:fileName>./examples/coffee/globals.coffee + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>949e147e9b5e284590449d0e8f74427c8c12f00f + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP456"/> + + <_3:fileName>./polyfills/object-prototype-definegetter.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP26"/> + + <_3:fileName>./Makefile.sandbox + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP478"/> + + <_3:fileName>./extras/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP462"/> + + <_3:fileName>./polyfills/duktape-isfastint.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP286"/> + + <_3:fileName>./src-separate/duk_api_logging.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP12"/> + + <_3:fileName>./Makefile.sharedlibrary + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>0f87aa4dae2a89f7ed3163197f96790ec6b28b19 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>e44b8b092afcfa97e693eb3e0be97d41f8aff301 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP316"/> + + <_3:fileName>./src-separate/duk_api_heap.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP180"/> + + <_3:fileName>./examples/debug-trans-dvalue/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>8c5ee5b508a23ac5089a7e6c76593b6678965869 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>7f45dbd1e665b257c7186982b8b3362753856b18 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP56"/> + + <_3:fileName>./debugger/duk_debugcommands.yaml + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP92"/> + + <_3:fileName>./examples/codepage-conv/duk_codepage_conv.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>62ecbef7a21dd5c49b38fbe344e2511c07f36261 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>030ac3c728cca19fe93d652a4b6b8758d739d9b9 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>09ee87b3ae8afe8b7c2295127835b05b00603b25 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP474"/> + + <_3:fileName>./config/duk_config.h-modular-static + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>4fae13370046b51fe87c60fa86eb14f2948fd143 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>c06e1c0dcc0c61e40f7337b40917df034f167e54 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>dfd71ae51277aae2fe511f4df732a20bc72ab75a + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP420"/> + + <_3:fileName>./src-separate/duk_bi_thread.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>641b29e08addaa543f37561f5a35689f5fba876e + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>fb9c2ff459fd84b4587f846fbd36219cf8f06931 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>b1c899faac8f0a8b1f7844d8c773ce5e6d9262d1 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP192"/> + + <_3:fileName>./examples/alloc-logging/duk_alloc_logging.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>d45e3fc443887261ce432a807aaebd2a1812264e + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP428"/> + + <_3:fileName>./src-separate/duk_api_var.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP120"/> + + <_3:fileName>./examples/eventloop/basic-test.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP206"/> + + <_3:fileName>./src-separate/duk_builtins.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>c8ae046bda2c920b5d807dfa68c8159b655b0873 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>f20291ecae9e775ebac2a6ed24f5399c2b3a7883 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP386"/> + + <_3:fileName>./src-separate/duk_heap_alloc.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>239011a773fbafa409c2f02b5e889bd5d28d16e1 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>6a1500fea27ea278c4f0c64d908e16cc60df684b + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP472"/> + + <_3:fileName>./config/duk_config.h-modular-dll + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>c33541d338ff3d8461dd1a8573a6372e19cb12e4 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP232"/> + + <_3:fileName>./src-separate/duk_hstring_misc.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP90"/> + + <_3:fileName>./examples/codepage-conv/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>f396c6aec456b183ef77e33da1dfffc89c417056 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>a9db75839022e7dcf7e691ada7480a9b7c2715d0 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>a2c79a55a60f73e1a1567b96d584399571180ef5 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>92ce3decf72b10b0023116ac727a752e8749aa90 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>339f26c72e051cf531426a63fe6c35c476bad645 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>a9206a7b46f8d8319b798475650217f494fa76ab + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>fa85ee3394206d18c611b837e700257ba8336250 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP292"/> + + <_3:fileName>./src-separate/duk_bi_json.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>149fd21526c90d52b22e77b297f76e6da3e656ff + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>03cad085177f1e776be90afceef1f2160e6c8bf4 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP50"/> + + <_3:fileName>./debugger/duk_debug_proxy.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP410"/> + + <_3:fileName>./src-separate/duk_debugger.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP42"/> + + <_3:fileName>./src-noline/duktape.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP244"/> + + <_3:fileName>./src-separate/duk_strings.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP152"/> + + <_3:fileName>./examples/guide/prime.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP132"/> + + <_3:fileName>./examples/eval/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>62a4483b185b620431e13ddb2b18555baa344937 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>a476b63d336a8d171891330c965fbb8b72f5863a + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP28"/> + + <_3:fileName>./AUTHORS.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP260"/> + + <_3:fileName>./src-separate/duk_regexp.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP288"/> + + <_3:fileName>./src-separate/duk_bi_math.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>a32730ebb239feb5599bcac3561b7a0acb3a1b94 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP318"/> + + <_3:fileName>./src-separate/duk_hthread_stacks.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>87018db6c5898305a7e5196d9590c5b42424a7ea + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>2df8e3d302a5c74ac385b0e1e110d0845a4aef35 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>5ab84f0ff2ed8c4b4a2daa5957d2a20447887103 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>484fb41d516abcbe8c97af5aa80b76c67dbfb0cb + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP490"/> + + <_3:fileName>./src/duktape.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP200"/> + + <_3:fileName>./src-separate/duk_bi_error.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP60"/> + + <_3:fileName>./debugger/package.json + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP10"/> + + <_3:fileName>./duk_build_meta.json + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP432"/> + + <_3:fileName>./src-separate/duk_util_hashbytes.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>206ffd13c06208e85baba2b257fe3ddebb56a6c2 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>6b438afc07b9ac67fb6ea8bc9d4f01f3b362c9e1 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>73ff75213f392b6125c68ab6b430176dfad71269 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP452"/> + + <_3:fileName>./polyfills/duktape-error-setter-nonwritable.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>24a2346fa389c2294cc0e96427e0bd206dac10b9 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>ff17cbc9cb84b86f83bb104a247e5c265a9fe5b5 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>5ab84f0ff2ed8c4b4a2daa5957d2a20447887103 + + + + <_3:packageVerificationCodeExcludedFile>./license.spdx + <_3:packageVerificationCodeValue>a60d2f27825f0294858de60a5ef8f35433967a27 + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP68"/> + + <_3:fileName>./debugger/static/style.css + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP330"/> + + <_3:fileName>./src-separate/duk_hcompiledfunction.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP242"/> + + <_3:fileName>./src-separate/duk_util_misc.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>036a814b2c1187af4c5c882095bd6b3d710cb79a + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>6fdfe8f7b5fc3226d83f0246079f76b33d4bcb9b + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>b381ea15009eb8140c0789203345cdcc2c6c1cd6 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>6c4bfae55df6a1fef08fd758e32bf465316cab9a + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>734560a1ac6155f124000b5a93569aae99149b6c + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP266"/> + + <_3:fileName>./src-separate/duk_bi_string.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>64edd8d85b88c69b5c2c686b85f19b350bef7880 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>1c40a52fd54baf7ac334fce4829892d5d951401a + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP252"/> + + <_3:fileName>./src-separate/duk_lexer.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP430"/> + + <_3:fileName>./src-separate/duk_api_stack.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>fbe432d33d4ef75f30fa2d87f4f1a83f78b4811b + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP178"/> + + <_3:fileName>./examples/debug-trans-dvalue/duk_trans_dvalue.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>bc9d119b5e66085e7e4219685fd351fb3f68d800 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP246"/> + + <_3:fileName>./src-separate/duk_unicode.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>3da07a99a4ed68844140352133867b2857fab685 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP476"/> + + <_3:fileName>./config/genconfig_metadata.tar.gz + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP344"/> + + <_3:fileName>./src-separate/duk_api_memory.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP378"/> + + <_3:fileName>./src-separate/duk_hobject.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>4392fa63e6e9174ec09ef2e3a4e4e3bd03e94126 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>9b95f60e95f7da3773bdac1d2a436b3c5a662ab1 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>6985052d270ff2a00543b641115b946fc91ebf3a + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>f9072f5c361c86887b57ce662b3cd7211b4ce346 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>5d593ae6316ed7a3ea6e7ed0a47267745d79d12d + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>ddbc206357dd4b97f3630b87bfdac148a5e8b190 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP258"/> + + <_3:fileName>./src-separate/duk_hbuffer_alloc.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP44"/> + + <_3:fileName>./src-noline/duktape.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP52"/> + + <_3:fileName>./debugger/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP74"/> + + <_3:fileName>./examples/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>89a7d62e8ae337fca35cacbb4b4cc6f294daedc9 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>5f89525f75f61dd8c89e010cb62bc071d6d13170 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>4396fb38f8a42bfc90ef1bc7c11c402074367343 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>ab29fe562681a0e6785515ef284fc51a20f0f7b5 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>ebaa68746ec59e01ef742f68aab11e053f882f63 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP388"/> + + <_3:fileName>./src-separate/duk_util_bitencoder.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>8ddbeb97a1a5acc3bed35f4df60e3aa1f4267380 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP98"/> + + <_3:fileName>./examples/jxpretty/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP168"/> + + <_3:fileName>./examples/cmdline/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP222"/> + + <_3:fileName>./src-separate/duk_error.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP194"/> + + <_3:fileName>./examples/alloc-logging/log2gnuplot.py + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP78"/> + + <_3:fileName>./examples/coffee/hello.coffee + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>0125ebcabe62cef4923e75527d3128e8c48cdf1f + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP438"/> + + <_3:fileName>./src-separate/duk_bi_number.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>c1685717742f3e9af9c901796a8bb5a08950461f + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>e172079513ed0c12550285bcbc4d6bfc83985676 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP174"/> + + <_3:fileName>./examples/debug-trans-socket/duk_trans_socket.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>52b4480b68f7f8bf93543d3bb795cdc5ade66fba + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>93573df8348a41adb790f59e39bf99d9a7bf3a3d + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP146"/> + + <_3:fileName>./examples/guide/fib.js + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>127e52c8b193d777811c25ceaab2c97c26a2bdab + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>601a6783177c1c6140c12b42c0bedb0ee0079d16 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>02741b89fd8d8df210223a3da3a200d0f609d803 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP240"/> + + <_3:fileName>./src-separate/duk_hobject_finalizer.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>44c9219558aba26dc69982a9f432028290707c36 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP86"/> + + <_3:fileName>./examples/alloc-hybrid/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>1289ac12dfcacbe472caea1333de3fbe70a5662c + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>3d700df1f765c39fc63bfd4e31a9d7da8d15c543 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP122"/> + + <_3:fileName>./examples/eventloop/poll.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>e5a5f0db067ec56c6d2f7356f412222a8884dc92 + + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>46f70bf98738ea7c9e5b353490d3e8815c610aa6 + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP202"/> + + <_3:fileName>./src-separate/duk_error_throw.c + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP182"/> + + <_3:fileName>./examples/debug-trans-dvalue/Makefile + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP362"/> + + <_3:fileName>./src-separate/duk_config.h + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_source"/> + + + <_3:algorithm rdf:resource="http://spdx.org/rdf/terms#checksumAlgorithm_sha1"/> + <_3:checksumValue>58c701ade28ee5a475dede84a6e771893e3fc40f + + + + <_3:licenseConcluded rdf:resource="http://spdx.org/licenses/MIT"/> + <_3:licenseInfoInFile rdf:resource="http://spdx.org/rdf/terms#none"/> + <_3:copyrightText>Copyright 2013-2016 Duktape authors (see AUTHORS.rst in the Duktape distributable) + <_3:checksum rdf:nodeID="svBMzJkP154"/> + + <_3:fileName>./examples/alloc-torture/README.rst + <_3:fileType rdf:resource="http://spdx.org/rdf/terms#fileType_other"/> + + diff --git a/src/civetweb/src/third_party/duktape-1.5.2/licenses/commonjs.txt b/src/civetweb/src/third_party/duktape-1.5.2/licenses/commonjs.txt new file mode 100644 index 000000000..54a1cd7b5 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/licenses/commonjs.txt @@ -0,0 +1,2 @@ +CommonJS specification snapshots are included in the references/ +directory. CommonJS is under the MIT license: http://www.commonjs.org/. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/licenses/lua.txt b/src/civetweb/src/third_party/duktape-1.5.2/licenses/lua.txt new file mode 100644 index 000000000..24e65f1cf --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/licenses/lua.txt @@ -0,0 +1 @@ +Lua is under the MIT license: http://www.lua.org/license.html. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/licenses/murmurhash2.txt b/src/civetweb/src/third_party/duktape-1.5.2/licenses/murmurhash2.txt new file mode 100644 index 000000000..70299256f --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/licenses/murmurhash2.txt @@ -0,0 +1,21 @@ +The MIT License + +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/src/civetweb/src/third_party/duktape-1.5.2/mandel.js b/src/civetweb/src/third_party/duktape-1.5.2/mandel.js new file mode 100644 index 000000000..79b5195b0 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/mandel.js @@ -0,0 +1,53 @@ +/* + * Mandelbrot example: + * + * $ ./duk mandel.js + * [...] + */ + +function mandel() { + var w = 76, h = 28, iter = 100; + var i, j, k, c; + var x0, y0, xx, yy, xx2, yy2; + var line; + + for (i = 0; i < h; i++) { + y0 = (i / h) * 2.5 - 1.25; + + for (j = 0, line = []; j < w; j++) { + x0 = (j / w) * 3.0 - 2.0; + + for (k = 0, xx = 0, yy = 0, c = '#'; k < iter; k++) { + /* z -> z^2 + c + * -> (xx+i*yy)^2 + (x0+i*y0) + * -> xx*xx+i*2*xx*yy-yy*yy + x0 + i*y0 + * -> (xx*xx - yy*yy + x0) + i*(2*xx*yy + y0) + */ + + xx2 = xx*xx; yy2 = yy*yy; + + if (xx2 + yy2 < 4.0) { + yy = 2*xx*yy + y0; + xx = xx2 - yy2 + x0; + } else { + /* xx^2 + yy^2 >= 4.0 */ + if (k < 3) { c = '.'; } + else if (k < 5) { c = ','; } + else if (k < 10) { c = '-'; } + else { c = '='; } + break; + } + } + + line.push(c); + } + + print(line.join('')); + } +} + +try { + mandel(); +} catch (e) { + print(e.stack || e); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/polyfills/console-minimal.js b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/console-minimal.js new file mode 100644 index 000000000..1876c5fda --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/console-minimal.js @@ -0,0 +1,20 @@ +/* + * Minimal console.log() polyfill + */ + +if (typeof console === 'undefined') { + Object.defineProperty(this, 'console', { + value: {}, writable: true, enumerable: false, configurable: true + }); +} +if (typeof console.log === 'undefined') { + (function () { + var origPrint = print; // capture in closure in case changed later + Object.defineProperty(this.console, 'log', { + value: function () { + var strArgs = Array.prototype.map.call(arguments, function (v) { return String(v); }); + origPrint(Array.prototype.join.call(strArgs, ' ')); + }, writable: true, enumerable: false, configurable: true + }); + })(); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/polyfills/duktape-error-setter-nonwritable.js b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/duktape-error-setter-nonwritable.js new file mode 100644 index 000000000..236c706b2 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/duktape-error-setter-nonwritable.js @@ -0,0 +1,20 @@ +/* + * Ensure Error .fileName, .lineNumber, and .stack are not directly writable, + * but can be written using Object.defineProperty(). This matches Duktape + * 1.3.0 and prior. + * + * See: https://github.com/svaarala/duktape/pull/390. + */ + +(function () { + var err = new Error('test'); + err.fileName = 999; + if (err.fileName !== 999) { return; } // already non-writable + + var fn = new Function(''); // nop + Object.defineProperties(Error.prototype, { + fileName: { set: fn }, + lineNumber: { set: fn }, + stack: { set: fn } + }); +})(); diff --git a/src/civetweb/src/third_party/duktape-1.5.2/polyfills/duktape-error-setter-writable.js b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/duktape-error-setter-writable.js new file mode 100644 index 000000000..5d487cdfe --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/duktape-error-setter-writable.js @@ -0,0 +1,19 @@ +/* + * Ensure Error .fileName, .lineNumber, and .stack are directly writable + * without having to use Object.defineProperty(). This matches Duktape + * 1.4.0 behavior. + * + * See: https://github.com/svaarala/duktape/pull/390. + */ + +(function () { + var err = new Error('test'); + err.fileName = 999; + if (err.fileName === 999) { return; } // already writable + + Object.defineProperties(Error.prototype, { + fileName: { set: new Function('v', 'Object.defineProperty(this, "fileName", { value: v, writable: true, enumerable: false, configurable: true });') }, + lineNumber: { set: new Function('v', 'Object.defineProperty(this, "lineNumber", { value: v, writable: true, enumerable: false, configurable: true });') }, + stack: { set: new Function('v', 'Object.defineProperty(this, "stack", { value: v, writable: true, enumerable: false, configurable: true });') }, + }); +})(); diff --git a/src/civetweb/src/third_party/duktape-1.5.2/polyfills/duktape-isfastint.js b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/duktape-isfastint.js new file mode 100644 index 000000000..68219e42b --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/duktape-isfastint.js @@ -0,0 +1,38 @@ +/* + * Helper to check if a number is internally represented as a fastint: + * + * if (Duktape.isFastint(x)) { + * print('fastint'); + * } else { + * print('not a fastint (or not a number)'); + * } + * + * NOTE: This helper depends on the internal tag numbering (defined in + * duk_tval.h) which is both version specific and depends on whether + * duk_tval is packed or not. + */ + +if (typeof Duktape === 'object') { + if (typeof Duktape.fastintTag === 'undefined') { + Object.defineProperty(Duktape, 'fastintTag', { + /* Tag number depends on duk_tval packing. */ + value: (Duktape.info(true)[1] >= 0xfff0) ? + 0xfff1 /* tag for packed duk_tval */ : + 1 /* tag for unpacked duk_tval */, + writable: false, + enumerable: false, + configurable: true + }); + } + if (typeof Duktape.isFastint === 'undefined') { + Object.defineProperty(Duktape, 'isFastint', { + value: function (v) { + return Duktape.info(v)[0] === 4 && /* public type is DUK_TYPE_NUMBER */ + Duktape.info(v)[1] === Duktape.fastintTag; /* internal tag is fastint */ + }, + writable: false, + enumerable: false, + configurable: true + }); + } +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/polyfills/object-assign.js b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/object-assign.js new file mode 100644 index 000000000..dfa5a44d8 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/object-assign.js @@ -0,0 +1,45 @@ +/* + * Object.assign(), described in E6 Section 19.1.2.1 + * + * http://www.ecma-international.org/ecma-262/6.0/index.html#sec-object.assign + */ + +if (typeof Object.assign === 'undefined') { + Object.defineProperty(Object, 'assign', { + value: function (target) { + var i, n, j, m, k; + var source, keys; + var gotError; + var pendingError; + + if (target == null) { + throw new Exception('target null or undefined'); + } + + for (i = 1, n = arguments.length; i < n; i++) { + source = arguments[i]; + if (source == null) { + continue; // null or undefined + } + source = Object(source); + keys = Object.keys(source); // enumerable own keys + + for (j = 0, m = keys.length; j < m; j++) { + k = keys[j]; + try { + target[k] = source[k]; + } catch (e) { + if (!gotError) { + gotError = true; + pendingError = e; + } + } + } + } + + if (gotError) { + throw pendingError; + } + }, writable: true, enumerable: false, configurable: true + }); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/polyfills/object-prototype-definegetter.js b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/object-prototype-definegetter.js new file mode 100644 index 000000000..8d8cabb3d --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/object-prototype-definegetter.js @@ -0,0 +1,11 @@ +/* + * Object.prototype.__defineGetter__ polyfill + */ + +if (typeof Object.prototype.__defineGetter__ === 'undefined') { + Object.defineProperty(Object.prototype, '__defineGetter__', { + value: function (n, f) { + Object.defineProperty(this, n, { enumerable: true, configurable: true, get: f }); + }, writable: true, enumerable: false, configurable: true + }); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/polyfills/object-prototype-definesetter.js b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/object-prototype-definesetter.js new file mode 100644 index 000000000..6bd1722f9 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/object-prototype-definesetter.js @@ -0,0 +1,11 @@ +/* + * Object.prototype.__defineSetter__ polyfill + */ + +if (typeof Object.prototype.__defineSetter__ === 'undefined') { + Object.defineProperty(Object.prototype, '__defineSetter__', { + value: function (n, f) { + Object.defineProperty(this, n, { enumerable: true, configurable: true, set: f }); + }, writable: true, enumerable: false, configurable: true + }); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/polyfills/performance-now.js b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/performance-now.js new file mode 100644 index 000000000..dfb0a1d6e --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/polyfills/performance-now.js @@ -0,0 +1,25 @@ +/* + * Performance.now() polyfill + * + * http://www.w3.org/TR/hr-time/#sec-high-resolution-time + * + * Dummy implementation which uses the Date built-in and has no higher + * resolution. If/when Duktape has a built-in high resolution timer + * interface, reimplement this. + */ + +var _perfNowZeroTime = Date.now(); + +if (typeof Performance === 'undefined') { + Object.defineProperty(this, 'Performance', { + value: {}, + writable: true, enumerable: false, configurable: true + }); +} +if (typeof Performance.now === 'undefined') { + Object.defineProperty(Performance, 'now', { + value: function () { + return Date.now() - _perfNowZeroTime; + }, writable: true, enumerable: false, configurable: true + }); +} diff --git a/src/civetweb/src/third_party/duktape-1.5.2/src-noline/duk_config.h b/src/civetweb/src/third_party/duktape-1.5.2/src-noline/duk_config.h new file mode 100644 index 000000000..ef622353f --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/src-noline/duk_config.h @@ -0,0 +1,3804 @@ +/* + * duk_config.h configuration header generated by genconfig.py. + * + * Git commit: cad34ae155acb0846545ca6bf2d29f9463b22bbb + * Git describe: v1.5.2 + * Git branch: HEAD + * + * Supported platforms: + * - Mac OSX, iPhone, Darwin + * - OpenBSD + * - Generic BSD + * - Atari ST TOS + * - AmigaOS + * - Windows + * - Flashplayer (Crossbridge) + * - QNX + * - TI-Nspire + * - Emscripten + * - Linux + * - Solaris + * - Generic POSIX + * - Cygwin + * - Generic UNIX + * - Generic fallback + * + * Supported architectures: + * - x86 + * - x64 + * - x32 + * - ARM 32-bit + * - ARM 64-bit + * - MIPS 32-bit + * - MIPS 64-bit + * - PowerPC 32-bit + * - PowerPC 64-bit + * - SPARC 32-bit + * - SPARC 64-bit + * - SuperH + * - Motorola 68k + * - Emscripten + * - Generic + * + * Supported compilers: + * - Clang + * - GCC + * - MSVC + * - Emscripten + * - TinyC + * - VBCC + * - Bruce's C compiler + * - Generic + * + */ + +#if !defined(DUK_CONFIG_H_INCLUDED) +#define DUK_CONFIG_H_INCLUDED + +/* + * Intermediate helper defines + */ + +/* DLL build detection */ +#if defined(DUK_OPT_DLL_BUILD) +#define DUK_F_DLL_BUILD +#elif defined(DUK_OPT_NO_DLL_BUILD) +#undef DUK_F_DLL_BUILD +#else +/* not configured for DLL build */ +#undef DUK_F_DLL_BUILD +#endif + +/* Apple OSX, iOS */ +#if defined(__APPLE__) +#define DUK_F_APPLE +#endif + +/* OpenBSD */ +#if defined(__OpenBSD__) || defined(__OpenBSD) +#define DUK_F_OPENBSD +#endif + +/* NetBSD */ +#if defined(__NetBSD__) || defined(__NetBSD) +#define DUK_F_NETBSD +#endif + +/* FreeBSD */ +#if defined(__FreeBSD__) || defined(__FreeBSD) +#define DUK_F_FREEBSD +#endif + +/* BSD variant */ +#if defined(DUK_F_FREEBSD) || defined(DUK_F_NETBSD) || defined(DUK_F_OPENBSD) || \ + defined(__bsdi__) || defined(__DragonFly__) +#define DUK_F_BSD +#endif + +/* Atari ST TOS. __TOS__ defined by PureC. No platform define in VBCC + * apparently, so to use with VBCC user must define __TOS__ manually. + */ +#if defined(__TOS__) +#define DUK_F_TOS +#endif + +/* Motorola 68K. Not defined by VBCC, so user must define one of these + * manually when using VBCC. + */ +#if defined(__m68k__) || defined(M68000) || defined(__MC68K__) +#define DUK_F_M68K +#endif + +/* AmigaOS. Neither AMIGA nor __amigaos__ is defined on VBCC, so user must + * define 'AMIGA' manually when using VBCC. + */ +#if defined(AMIGA) || defined(__amigaos__) +#define DUK_F_AMIGAOS +#endif + +/* PowerPC */ +#if defined(__powerpc) || defined(__powerpc__) || defined(__PPC__) +#define DUK_F_PPC +#if defined(__PPC64__) || defined(__LP64__) || defined(_LP64) +#define DUK_F_PPC64 +#else +#define DUK_F_PPC32 +#endif +#endif + +/* Windows, both 32-bit and 64-bit */ +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || \ + defined(__WIN32__) || defined(__TOS_WIN__) || defined(__WINDOWS__) +#define DUK_F_WINDOWS +#if defined(_WIN64) || defined(WIN64) +#define DUK_F_WIN64 +#else +#define DUK_F_WIN32 +#endif +#endif + +/* Flash player (e.g. Crossbridge) */ +#if defined(__FLASHPLAYER__) +#define DUK_F_FLASHPLAYER +#endif + +/* QNX */ +#if defined(__QNX__) +#define DUK_F_QNX +#endif + +/* TI-Nspire (using Ndless) */ +#if defined(_TINSPIRE) +#define DUK_F_TINSPIRE +#endif + +/* Emscripten (provided explicitly by user), improve if possible */ +#if defined(EMSCRIPTEN) +#define DUK_F_EMSCRIPTEN +#endif + +/* BCC (Bruce's C compiler): this is a "torture target" for compilation */ +#if defined(__BCC__) || defined(__BCC_VERSION__) +#define DUK_F_BCC +#endif + +/* Linux */ +#if defined(__linux) || defined(__linux__) || defined(linux) +#define DUK_F_LINUX +#endif + +/* illumos / Solaris */ +#if defined(__sun) && defined(__SVR4) +#define DUK_F_SUN +#endif + +/* POSIX */ +#if defined(__posix) +#define DUK_F_POSIX +#endif + +/* Cygwin */ +#if defined(__CYGWIN__) +#define DUK_F_CYGWIN +#endif + +/* Generic Unix (includes Cygwin) */ +#if defined(__unix) || defined(__unix__) || defined(unix) || \ + defined(DUK_F_LINUX) || defined(DUK_F_BSD) +#define DUK_F_UNIX +#endif + +/* stdint.h not available */ +#if defined(DUK_F_WINDOWS) && defined(_MSC_VER) +#if (_MSC_VER < 1700) +/* VS2012+ has stdint.h, < VS2012 does not (but it's available for download). */ +#define DUK_F_NO_STDINT_H +#endif +#endif +#if !defined(DUK_F_NO_STDINT_H) && (defined(DUK_F_TOS) || defined(DUK_F_BCC)) +#define DUK_F_NO_STDINT_H +#endif + +/* C++ */ +#undef DUK_F_CPP +#if defined(__cplusplus) +#define DUK_F_CPP +#endif + +/* Intel x86 (32-bit), x64 (64-bit) or x32 (64-bit but 32-bit pointers), + * define only one of DUK_F_X86, DUK_F_X64, DUK_F_X32. + * https://sites.google.com/site/x32abi/ + */ +#if defined(__amd64__) || defined(__amd64) || \ + defined(__x86_64__) || defined(__x86_64) || \ + defined(_M_X64) || defined(_M_AMD64) +#if defined(__ILP32__) || defined(_ILP32) +#define DUK_F_X32 +#else +#define DUK_F_X64 +#endif +#elif defined(i386) || defined(__i386) || defined(__i386__) || \ + defined(__i486__) || defined(__i586__) || defined(__i686__) || \ + defined(__IA32__) || defined(_M_IX86) || defined(__X86__) || \ + defined(_X86_) || defined(__THW_INTEL__) || defined(__I86__) +#if defined(__LP64__) || defined(_LP64) +/* This should not really happen, but would indicate x64. */ +#define DUK_F_X64 +#else +#define DUK_F_X86 +#endif +#endif + +/* ARM */ +#if defined(__arm__) || defined(__thumb__) || defined(_ARM) || defined(_M_ARM) +#define DUK_F_ARM +#if defined(__LP64__) || defined(_LP64) || defined(__arm64) || defined(__arm64__) +#define DUK_F_ARM64 +#else +#define DUK_F_ARM32 +#endif +#endif + +/* MIPS. Related defines: __MIPSEB__, __MIPSEL__, __mips_isa_rev, __LP64__ */ +#if defined(__mips__) || defined(mips) || defined(_MIPS_ISA) || \ + defined(_R3000) || defined(_R4000) || defined(_R5900) || \ + defined(_MIPS_ISA_MIPS1) || defined(_MIPS_ISA_MIPS2) || \ + defined(_MIPS_ISA_MIPS3) || defined(_MIPS_ISA_MIPS4) || \ + defined(__mips) || defined(__MIPS__) +#define DUK_F_MIPS +#if defined(__LP64__) || defined(_LP64) || defined(__mips64) || \ + defined(__mips64__) || defined(__mips_n64) +#define DUK_F_MIPS64 +#else +#define DUK_F_MIPS32 +#endif +#endif + +/* SPARC */ +#if defined(sparc) || defined(__sparc) || defined(__sparc__) +#define DUK_F_SPARC +#if defined(__LP64__) || defined(_LP64) +#define DUK_F_SPARC64 +#else +#define DUK_F_SPARC32 +#endif +#endif + +/* SuperH */ +#if defined(__sh__) || \ + defined(__sh1__) || defined(__SH1__) || \ + defined(__sh2__) || defined(__SH2__) || \ + defined(__sh3__) || defined(__SH3__) || \ + defined(__sh4__) || defined(__SH4__) || \ + defined(__sh5__) || defined(__SH5__) +#define DUK_F_SUPERH +#endif + +/* Clang */ +#if defined(__clang__) +#define DUK_F_CLANG +#endif + +/* C99 or above */ +#undef DUK_F_C99 +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) +#define DUK_F_C99 +#endif + +/* C++11 or above */ +#undef DUK_F_CPP11 +#if defined(__cplusplus) && (__cplusplus >= 201103L) +#define DUK_F_CPP11 +#endif + +/* GCC. Clang also defines __GNUC__ so don't detect GCC if using Clang. */ +#if defined(__GNUC__) && !defined(__clang__) && !defined(DUK_F_CLANG) +#define DUK_F_GCC +#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) +/* Convenience, e.g. gcc 4.5.1 == 40501; http://stackoverflow.com/questions/6031819/emulating-gccs-builtin-unreachable */ +#define DUK_F_GCC_VERSION (__GNUC__ * 10000L + __GNUC_MINOR__ * 100L + __GNUC_PATCHLEVEL__) +#else +#error cannot figure out gcc version +#endif +#endif + +/* MinGW. Also GCC flags (DUK_F_GCC) are enabled now. */ +#if defined(__MINGW32__) || defined(__MINGW64__) +#define DUK_F_MINGW +#endif + +/* MSVC */ +#if defined(_MSC_VER) +/* MSVC preprocessor defines: http://msdn.microsoft.com/en-us/library/b0084kay.aspx + * _MSC_FULL_VER includes the build number, but it has at least two formats, see e.g. + * BOOST_MSVC_FULL_VER in http://www.boost.org/doc/libs/1_52_0/boost/config/compiler/visualc.hpp + */ +#define DUK_F_MSVC +#if defined(_MSC_FULL_VER) +#if (_MSC_FULL_VER > 100000000) +#define DUK_F_MSVC_FULL_VER _MSC_FULL_VER +#else +#define DUK_F_MSCV_FULL_VER (_MSC_FULL_VER * 10) +#endif +#endif +#endif /* _MSC_VER */ + +/* TinyC */ +#if defined(__TINYC__) +/* http://bellard.org/tcc/tcc-doc.html#SEC9 */ +#define DUK_F_TINYC +#endif + +/* VBCC */ +#if defined(__VBCC__) +#define DUK_F_VBCC +#endif + +/* Atari Mint */ +#if defined(__MINT__) +#define DUK_F_MINT +#endif + +/* + * Platform autodetection + */ + +/* Workaround for older C++ compilers before including , + * see e.g.: https://sourceware.org/bugzilla/show_bug.cgi?id=15366 + */ +#if defined(__cplusplus) && !defined(__STDC_LIMIT_MACROS) +#define __STDC_LIMIT_MACROS +#endif +#if defined(__cplusplus) && !defined(__STDC_CONSTANT_MACROS) +#define __STDC_CONSTANT_MACROS +#endif + +#if defined(DUK_F_APPLE) +/* --- Mac OSX, iPhone, Darwin --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +/* http://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor */ +#if TARGET_IPHONE_SIMULATOR +#define DUK_USE_OS_STRING "iphone-sim" +#elif TARGET_OS_IPHONE +#define DUK_USE_OS_STRING "iphone" +#elif TARGET_OS_MAC +#define DUK_USE_OS_STRING "osx" +#else +#define DUK_USE_OS_STRING "osx-unknown" +#endif + +/* Use _setjmp() on Apple by default, see GH-55. */ +#define DUK_JMPBUF_TYPE jmp_buf +#define DUK_SETJMP(jb) _setjmp((jb)) +#define DUK_LONGJMP(jb) _longjmp((jb), 1) +#elif defined(DUK_F_OPENBSD) +/* --- OpenBSD --- */ +/* http://www.monkey.org/openbsd/archive/ports/0401/msg00089.html */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "openbsd" +#elif defined(DUK_F_BSD) +/* --- Generic BSD --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "bsd" +#elif defined(DUK_F_TOS) +/* --- Atari ST TOS --- */ +#define DUK_USE_DATE_NOW_TIME +#define DUK_USE_DATE_TZO_GMTIME +/* no parsing (not an error) */ +#define DUK_USE_DATE_FMT_STRFTIME +#include + +#define DUK_USE_OS_STRING "tos" + +/* TOS on M68K is always big endian. */ +#if !defined(DUK_USE_BYTEORDER) && defined(DUK_F_M68K) +#define DUK_USE_BYTEORDER 3 +#endif +#elif defined(DUK_F_AMIGAOS) +/* --- AmigaOS --- */ +#if defined(DUK_F_M68K) +/* AmigaOS on M68k */ +#define DUK_USE_DATE_NOW_TIME +#define DUK_USE_DATE_TZO_GMTIME +/* no parsing (not an error) */ +#define DUK_USE_DATE_FMT_STRFTIME +#include +#elif defined(DUK_F_PPC) +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#ifndef UINTPTR_MAX +#define UINTPTR_MAX UINT_MAX +#endif +#else +#error AmigaOS but not M68K/PPC, not supported now +#endif + +#define DUK_USE_OS_STRING "amigaos" + +/* AmigaOS on M68K or PPC is always big endian. */ +#if !defined(DUK_USE_BYTEORDER) && (defined(DUK_F_M68K) || defined(DUK_F_PPC)) +#define DUK_USE_BYTEORDER 3 +#endif +#elif defined(DUK_F_WINDOWS) +/* --- Windows --- */ +/* Initial fix: disable secure CRT related warnings when compiling Duktape + * itself (must be defined before including Windows headers). Don't define + * for user code including duktape.h. + */ +#if defined(DUK_COMPILING_DUKTAPE) && !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +/* Windows 32-bit and 64-bit are currently the same. */ +/* MSVC does not have sys/param.h */ +#define DUK_USE_DATE_NOW_WINDOWS +#define DUK_USE_DATE_TZO_WINDOWS +/* Note: PRS and FMT are intentionally left undefined for now. This means + * there is no platform specific date parsing/formatting but there is still + * the ISO 8601 standard format. + */ +#if defined(DUK_COMPILING_DUKTAPE) +/* Only include when compiling Duktape to avoid polluting application build + * with a lot of unnecessary defines. + */ +#include +#endif + +#define DUK_USE_OS_STRING "windows" + +/* On Windows, assume we're little endian. Even Itanium which has a + * configurable endianness runs little endian in Windows. + */ +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +#elif defined(DUK_F_FLASHPLAYER) +/* --- Flashplayer (Crossbridge) --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "flashplayer" + +#if !defined(DUK_USE_BYTEORDER) && defined(DUK_F_FLASHPLAYER) +#define DUK_USE_BYTEORDER 1 +#endif +#elif defined(DUK_F_QNX) +/* --- QNX --- */ +#if defined(DUK_F_QNX) && defined(DUK_COMPILING_DUKTAPE) +/* See: /opt/qnx650/target/qnx6/usr/include/sys/platform.h */ +#define _XOPEN_SOURCE 600 +#define _POSIX_C_SOURCE 200112L +#endif + +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "qnx" +#elif defined(DUK_F_TINSPIRE) +/* --- TI-Nspire --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "tinspire" +#elif defined(DUK_F_EMSCRIPTEN) +/* --- Emscripten --- */ +#if defined(DUK_COMPILING_DUKTAPE) +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#ifndef _GNU_SOURCE +#define _GNU_SOURCE /* e.g. getdate_r */ +#endif +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE /* e.g. strptime */ +#endif +#endif /* DUK_COMPILING_DUKTAPE */ + +#include +#if defined(DUK_F_BCC) +/* no endian.h */ +#else +#include +#endif /* DUK_F_BCC */ +#include +#include +#include +#include + +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME + +#define DUK_USE_OS_STRING "emscripten" +#elif defined(DUK_F_LINUX) +/* --- Linux --- */ +#if defined(DUK_COMPILING_DUKTAPE) +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#ifndef _GNU_SOURCE +#define _GNU_SOURCE /* e.g. getdate_r */ +#endif +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE /* e.g. strptime */ +#endif +#endif /* DUK_COMPILING_DUKTAPE */ + +#include +#if defined(DUK_F_BCC) +/* no endian.h or stdint.h */ +#else +#include +#include +#endif /* DUK_F_BCC */ +#include +#include +#include + +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME + +#define DUK_USE_OS_STRING "linux" +#elif defined(DUK_F_SUN) +/* --- Solaris --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME + +#include +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "solaris" +#elif defined(DUK_F_POSIX) +/* --- Generic POSIX --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "posix" +#elif defined(DUK_F_CYGWIN) +/* --- Cygwin --- */ +/* don't use strptime() for now */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +#define DUK_JMPBUF_TYPE jmp_buf +#define DUK_SETJMP(jb) _setjmp((jb)) +#define DUK_LONGJMP(jb) _longjmp((jb), 1) + +#define DUK_USE_OS_STRING "windows" +#elif defined(DUK_F_UNIX) +/* --- Generic UNIX --- */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_R +#define DUK_USE_DATE_PRS_STRPTIME +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#define DUK_USE_OS_STRING "unknown" +#else +/* --- Generic fallback --- */ +/* The most portable current time provider is time(), but it only has a + * one second resolution. + */ +#define DUK_USE_DATE_NOW_TIME + +/* The most portable way to figure out local time offset is gmtime(), + * but it's not thread safe so use with caution. + */ +#define DUK_USE_DATE_TZO_GMTIME + +/* Avoid custom date parsing and formatting for portability. */ +#undef DUK_USE_DATE_PRS_STRPTIME +#undef DUK_USE_DATE_FMT_STRFTIME + +/* Rely on C89 headers only; time.h must be here. */ +#include + +#define DUK_USE_OS_STRING "unknown" +#endif /* autodetect platform */ + +/* Shared includes: C89 */ +#include +#include +#include +#include /* varargs */ +#include +#include /* e.g. ptrdiff_t */ +#include +#include + +/* date.h is omitted, and included per platform */ + +/* Shared includes: stdint.h is C99 */ +#if defined(DUK_F_NO_STDINT_H) +/* stdint.h not available */ +#else +/* Technically C99 (C++11) but found in many systems. On some systems + * __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS must be defined before + * including stdint.h (see above). + */ +#include +#endif + +#if defined(DUK_F_CPP) +#include /* std::exception */ +#endif + +/* + * Architecture autodetection + */ + +#if defined(DUK_F_X86) +/* --- x86 --- */ +#define DUK_USE_ARCH_STRING "x86" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +/* XXX: This is technically not guaranteed because it's possible to configure + * an x86 to require aligned accesses with Alignment Check (AC) flag. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 1 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_X64) +/* --- x64 --- */ +#define DUK_USE_ARCH_STRING "x64" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +/* XXX: This is technically not guaranteed because it's possible to configure + * an x86 to require aligned accesses with Alignment Check (AC) flag. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 1 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_X32) +/* --- x32 --- */ +#define DUK_USE_ARCH_STRING "x32" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +/* XXX: This is technically not guaranteed because it's possible to configure + * an x86 to require aligned accesses with Alignment Check (AC) flag. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 1 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_ARM32) +/* --- ARM 32-bit --- */ +#define DUK_USE_ARCH_STRING "arm32" +/* Byte order varies, so rely on autodetect. */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 4 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_ARM64) +/* --- ARM 64-bit --- */ +#define DUK_USE_ARCH_STRING "arm64" +/* Byte order varies, so rely on autodetect. */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_MIPS32) +/* --- MIPS 32-bit --- */ +#define DUK_USE_ARCH_STRING "mips32" +/* MIPS byte order varies so rely on autodetection. */ +/* Based on 'make checkalign' there are no alignment requirements on + * Linux MIPS except for doubles, which need align by 4. Alignment + * requirements vary based on target though. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 4 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_MIPS64) +/* --- MIPS 64-bit --- */ +#define DUK_USE_ARCH_STRING "mips64" +/* MIPS byte order varies so rely on autodetection. */ +/* Good default is a bit arbitrary because alignment requirements + * depend on target. See https://github.com/svaarala/duktape/issues/102. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_PPC32) +/* --- PowerPC 32-bit --- */ +#define DUK_USE_ARCH_STRING "ppc32" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 3 +#endif +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_PPC64) +/* --- PowerPC 64-bit --- */ +#define DUK_USE_ARCH_STRING "ppc64" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 3 +#endif +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_SPARC32) +/* --- SPARC 32-bit --- */ +#define DUK_USE_ARCH_STRING "sparc32" +/* SPARC byte order varies so rely on autodetection. */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_SPARC64) +/* --- SPARC 64-bit --- */ +#define DUK_USE_ARCH_STRING "sparc64" +/* SPARC byte order varies so rely on autodetection. */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_SUPERH) +/* --- SuperH --- */ +#define DUK_USE_ARCH_STRING "sh" +/* Byte order varies, rely on autodetection. */ +/* Based on 'make checkalign' there are no alignment requirements on + * Linux SH4, but align by 4 is probably a good basic default. + */ +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 4 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_M68K) +/* --- Motorola 68k --- */ +#define DUK_USE_ARCH_STRING "m68k" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 3 +#endif +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#define DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#elif defined(DUK_F_EMSCRIPTEN) +/* --- Emscripten --- */ +#define DUK_USE_ARCH_STRING "emscripten" +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +#if !defined(DUK_USE_ALIGN_BY) +#define DUK_USE_ALIGN_BY 8 +#endif +#undef DUK_USE_PACKED_TVAL +#define DUK_F_PACKED_TVAL_PROVIDED +#else +/* --- Generic --- */ +/* These are necessary wild guesses. */ +#define DUK_USE_ARCH_STRING "generic" +/* Rely on autodetection for byte order, alignment, and packed tval. */ +#endif /* autodetect architecture */ + +/* + * Compiler autodetection + */ + +#if defined(DUK_F_CLANG) +/* --- Clang --- */ +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +/* C99 / C++11 and above: rely on va_copy() which is required. */ +#define DUK_VA_COPY(dest,src) va_copy(dest,src) +#else +/* Clang: assume we have __va_copy() in non-C99 mode. */ +#define DUK_VA_COPY(dest,src) __va_copy(dest,src) +#endif + +#define DUK_NORETURN(decl) decl __attribute__((noreturn)) + +#if defined(__clang__) && defined(__has_builtin) +#if __has_builtin(__builtin_unreachable) +#define DUK_UNREACHABLE() do { __builtin_unreachable(); } while (0) +#endif +#endif + +#define DUK_USE_BRANCH_HINTS +#define DUK_LIKELY(x) __builtin_expect((x), 1) +#define DUK_UNLIKELY(x) __builtin_expect((x), 0) + +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_NOINLINE __attribute__((noinline)) +#define DUK_INLINE inline +#define DUK_ALWAYS_INLINE inline __attribute__((always_inline)) +#endif + +#if defined(DUK_F_DLL_BUILD) && defined(DUK_F_WINDOWS) +/* MSVC dllexport/dllimport: appropriate __declspec depends on whether we're + * compiling Duktape or the application. + */ +#if defined(DUK_COMPILING_DUKTAPE) +#define DUK_EXTERNAL_DECL extern __declspec(dllexport) +#define DUK_EXTERNAL __declspec(dllexport) +#else +#define DUK_EXTERNAL_DECL extern __declspec(dllimport) +#define DUK_EXTERNAL should_not_happen +#endif +#if defined(DUK_SINGLE_FILE) +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#else +#define DUK_INTERNAL_DECL extern +#define DUK_INTERNAL /*empty*/ +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static +#else +#define DUK_EXTERNAL_DECL __attribute__ ((visibility("default"))) extern +#define DUK_EXTERNAL __attribute__ ((visibility("default"))) +#if defined(DUK_SINGLE_FILE) +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +/* Minimize warnings for unused internal functions with GCC >= 3.1.1 and + * Clang. Based on documentation it should suffice to have the attribute + * in the declaration only, but in practice some warnings are generated unless + * the attribute is also applied to the definition. + */ +#define DUK_INTERNAL_DECL static __attribute__ ((unused)) +#define DUK_INTERNAL static __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#endif +#else +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) +#endif +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static +#endif + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "clang" +#else +#define DUK_USE_COMPILER_STRING "clang" +#endif + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_USE_VARIADIC_MACROS +#endif + +#define DUK_USE_UNION_INITIALIZERS + +#undef DUK_USE_FLEX_C99 +#undef DUK_USE_FLEX_ZEROSIZE +#undef DUK_USE_FLEX_ONESIZE +#if defined(DUK_F_C99) +#define DUK_USE_FLEX_C99 +#else +#define DUK_USE_FLEX_ZEROSIZE +#endif + +#undef DUK_USE_GCC_PRAGMAS +#define DUK_USE_PACK_CLANG_ATTR +#elif defined(DUK_F_GCC) +/* --- GCC --- */ +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +/* C99 / C++11 and above: rely on va_copy() which is required. */ +#define DUK_VA_COPY(dest,src) va_copy(dest,src) +#else +/* GCC: assume we have __va_copy() in non-C99 mode. */ +#define DUK_VA_COPY(dest,src) __va_copy(dest,src) +#endif + +#if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 20500L) +/* since gcc-2.5 */ +#define DUK_NORETURN(decl) decl __attribute__((noreturn)) +#endif + +#if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 40500L) +/* since gcc-4.5 */ +#define DUK_UNREACHABLE() do { __builtin_unreachable(); } while (0) +#endif + +#define DUK_USE_BRANCH_HINTS +#if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 40500L) +/* GCC: test not very accurate; enable only in relatively recent builds + * because of bugs in gcc-4.4 (http://lists.debian.org/debian-gcc/2010/04/msg00000.html) + */ +#define DUK_LIKELY(x) __builtin_expect((x), 1) +#define DUK_UNLIKELY(x) __builtin_expect((x), 0) +#endif + +#if (defined(DUK_F_C99) || defined(DUK_F_CPP11)) && \ + defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 30101) +#define DUK_NOINLINE __attribute__((noinline)) +#define DUK_INLINE inline +#define DUK_ALWAYS_INLINE inline __attribute__((always_inline)) +#endif + +#if defined(DUK_F_DLL_BUILD) && defined(DUK_F_WINDOWS) +/* MSVC dllexport/dllimport: appropriate __declspec depends on whether we're + * compiling Duktape or the application. + */ +#if defined(DUK_COMPILING_DUKTAPE) +#define DUK_EXTERNAL_DECL extern __declspec(dllexport) +#define DUK_EXTERNAL __declspec(dllexport) +#else +#define DUK_EXTERNAL_DECL extern __declspec(dllimport) +#define DUK_EXTERNAL should_not_happen +#endif +#if defined(DUK_SINGLE_FILE) +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#else +#define DUK_INTERNAL_DECL extern +#define DUK_INTERNAL /*empty*/ +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static +#elif defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 40000) +#define DUK_EXTERNAL_DECL __attribute__ ((visibility("default"))) extern +#define DUK_EXTERNAL __attribute__ ((visibility("default"))) +#if defined(DUK_SINGLE_FILE) +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +/* Minimize warnings for unused internal functions with GCC >= 3.1.1 and + * Clang. Based on documentation it should suffice to have the attribute + * in the declaration only, but in practice some warnings are generated unless + * the attribute is also applied to the definition. + */ +#define DUK_INTERNAL_DECL static __attribute__ ((unused)) +#define DUK_INTERNAL static __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#endif +#else +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) +#endif +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static +#endif + +#if defined(DUK_F_MINGW) +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "mingw++" +#else +#define DUK_USE_COMPILER_STRING "mingw" +#endif +#else +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "g++" +#else +#define DUK_USE_COMPILER_STRING "gcc" +#endif +#endif + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) || (defined(DUK_F_CPP11) && defined(__GNUC__)) +#define DUK_USE_VARIADIC_MACROS +#endif + +#define DUK_USE_UNION_INITIALIZERS + +#undef DUK_USE_FLEX_C99 +#undef DUK_USE_FLEX_ZEROSIZE +#undef DUK_USE_FLEX_ONESIZE +#if defined(DUK_F_C99) +#define DUK_USE_FLEX_C99 +#else +#define DUK_USE_FLEX_ZEROSIZE +#endif + +#if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 40600) +#define DUK_USE_GCC_PRAGMAS +#else +#undef DUK_USE_GCC_PRAGMAS +#endif + +#define DUK_USE_PACK_GCC_ATTR +#elif defined(DUK_F_MSVC) +/* --- MSVC --- */ +/* http://msdn.microsoft.com/en-us/library/aa235362(VS.60).aspx */ +#define DUK_NORETURN(decl) __declspec(noreturn) decl + +/* XXX: DUK_UNREACHABLE for msvc? */ + +#undef DUK_USE_BRANCH_HINTS + +/* XXX: DUK_LIKELY, DUK_UNLIKELY for msvc? */ +/* XXX: DUK_NOINLINE, DUK_INLINE, DUK_ALWAYS_INLINE for msvc? */ + +#if defined(DUK_F_DLL_BUILD) && defined(DUK_F_WINDOWS) +/* MSVC dllexport/dllimport: appropriate __declspec depends on whether we're + * compiling Duktape or the application. + */ +#if defined(DUK_COMPILING_DUKTAPE) +#define DUK_EXTERNAL_DECL extern __declspec(dllexport) +#define DUK_EXTERNAL __declspec(dllexport) +#else +#define DUK_EXTERNAL_DECL extern __declspec(dllimport) +#define DUK_EXTERNAL should_not_happen +#endif +#if defined(DUK_SINGLE_FILE) +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#else +#define DUK_INTERNAL_DECL extern +#define DUK_INTERNAL /*empty*/ +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static +#endif + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "msvc++" +#else +#define DUK_USE_COMPILER_STRING "msvc" +#endif + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) +#define DUK_USE_VARIADIC_MACROS +#elif defined(_MSC_VER) && (_MSC_VER >= 1400) +/* VS2005+ should have variadic macros even when they're not C99. */ +#define DUK_USE_VARIADIC_MACROS +#endif + +#undef DUK_USE_UNION_INITIALIZERS +#if defined(_MSC_VER) && (_MSC_VER >= 1800) +/* VS2013+ supports union initializers but there's a bug involving union-inside-struct: + * https://connect.microsoft.com/VisualStudio/feedback/details/805981 + * The bug was fixed (at least) in VS2015 so check for VS2015 for now: + * https://blogs.msdn.microsoft.com/vcblog/2015/07/01/c-compiler-front-end-fixes-in-vs2015/ + * Manually tested using VS2013, CL reports 18.00.31101, so enable for VS2013 too. + */ +#define DUK_USE_UNION_INITIALIZERS +#endif + +#undef DUK_USE_FLEX_C99 +#undef DUK_USE_FLEX_ZEROSIZE +#undef DUK_USE_FLEX_ONESIZE +#if defined(DUK_F_C99) +#define DUK_USE_FLEX_C99 +#else +#define DUK_USE_FLEX_ZEROSIZE +#endif + +#undef DUK_USE_GCC_PRAGMAS + +#define DUK_USE_PACK_MSVC_PRAGMA + +/* These have been tested from VS2008 onwards; may work in older VS versions + * too but not enabled by default. + */ +#if defined(_MSC_VER) && (_MSC_VER >= 1500) +#define DUK_NOINLINE __declspec(noinline) +#define DUK_INLINE __inline +#define DUK_ALWAYS_INLINE __forceinline +#endif + +#if defined(_MSC_VER) && (_MSC_VER >= 1900) +#define DUK_SNPRINTF snprintf +#define DUK_VSNPRINTF vsnprintf +#else +/* (v)snprintf() is missing before MSVC 2015. Note that _(v)snprintf() does + * NOT NUL terminate on truncation, but Duktape code never assumes that. + * http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010 + */ +#define DUK_SNPRINTF _snprintf +#define DUK_VSNPRINTF _vsnprintf +#endif +#elif defined(DUK_F_EMSCRIPTEN) +/* --- Emscripten --- */ +#define DUK_NORETURN(decl) decl __attribute__((noreturn)) + +#if defined(__clang__) && defined(__has_builtin) +#if __has_builtin(__builtin_unreachable) +#define DUK_UNREACHABLE() do { __builtin_unreachable(); } while (0) +#endif +#endif + +#define DUK_USE_BRANCH_HINTS +#define DUK_LIKELY(x) __builtin_expect((x), 1) +#define DUK_UNLIKELY(x) __builtin_expect((x), 0) + +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_NOINLINE __attribute__((noinline)) +#define DUK_INLINE inline +#define DUK_ALWAYS_INLINE inline __attribute__((always_inline)) +#endif + +#define DUK_EXTERNAL_DECL __attribute__ ((visibility("default"))) extern +#define DUK_EXTERNAL __attribute__ ((visibility("default"))) +#if defined(DUK_SINGLE_FILE) +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +/* Minimize warnings for unused internal functions with GCC >= 3.1.1 and + * Clang. Based on documentation it should suffice to have the attribute + * in the declaration only, but in practice some warnings are generated unless + * the attribute is also applied to the definition. + */ +#define DUK_INTERNAL_DECL static __attribute__ ((unused)) +#define DUK_INTERNAL static __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL static +#define DUK_INTERNAL static +#endif +#else +#if (defined(DUK_F_GCC_VERSION) && DUK_F_GCC_VERSION >= 30101) || defined(DUK_F_CLANG) +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) __attribute__ ((unused)) +#else +#define DUK_INTERNAL_DECL __attribute__ ((visibility("hidden"))) extern +#define DUK_INTERNAL __attribute__ ((visibility("hidden"))) +#endif +#endif +#define DUK_LOCAL_DECL static +#define DUK_LOCAL static + +#define DUK_USE_COMPILER_STRING "emscripten" + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_USE_VARIADIC_MACROS +#endif + +#define DUK_USE_UNION_INITIALIZERS + +#undef DUK_USE_FLEX_C99 +#undef DUK_USE_FLEX_ZEROSIZE +#undef DUK_USE_FLEX_ONESIZE +#if defined(DUK_F_C99) +#define DUK_USE_FLEX_C99 +#else +#define DUK_USE_FLEX_ZEROSIZE +#endif + +#undef DUK_USE_GCC_PRAGMAS +#define DUK_USE_PACK_CLANG_ATTR +#elif defined(DUK_F_TINYC) +/* --- TinyC --- */ +#undef DUK_USE_BRANCH_HINTS + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "tinyc++" +#else +#define DUK_USE_COMPILER_STRING "tinyc" +#endif + +/* http://bellard.org/tcc/tcc-doc.html#SEC7 */ +#define DUK_USE_VARIADIC_MACROS + +#define DUK_USE_UNION_INITIALIZERS + +/* Most portable, wastes space */ +#define DUK_USE_FLEX_ONESIZE + +/* Most portable, potentially wastes space */ +#define DUK_USE_PACK_DUMMY_MEMBER +#elif defined(DUK_F_VBCC) +/* --- VBCC --- */ +#undef DUK_USE_BRANCH_HINTS + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "vbcc-c++" +#else +#define DUK_USE_COMPILER_STRING "vbcc" +#endif + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_USE_VARIADIC_MACROS +#endif + +/* VBCC supports C99 so check only for C99 for union initializer support. + * Designated union initializers would possibly work even without a C99 check. + */ +#undef DUK_USE_UNION_INITIALIZERS +#if defined(DUK_F_C99) +#define DUK_USE_UNION_INITIALIZERS +#endif + +#define DUK_USE_FLEX_ZEROSIZE +#define DUK_USE_PACK_DUMMY_MEMBER +#elif defined(DUK_F_BCC) +/* --- Bruce's C compiler --- */ +#undef DUK_USE_BRANCH_HINTS + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "bcc++" +#else +#define DUK_USE_COMPILER_STRING "bcc" +#endif + +/* Most portable */ +#undef DUK_USE_VARIADIC_MACROS + +/* Most portable, wastes space */ +#undef DUK_USE_UNION_INITIALIZERS + +/* Most portable, wastes space */ +#define DUK_USE_FLEX_ONESIZE + +/* Most portable, potentially wastes space */ +#define DUK_USE_PACK_DUMMY_MEMBER + +/* BCC, assume we're on x86. */ +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +#else +/* --- Generic --- */ +#undef DUK_USE_BRANCH_HINTS + +#if defined(DUK_F_CPP) +#define DUK_USE_COMPILER_STRING "generic-c++" +#else +#define DUK_USE_COMPILER_STRING "generic" +#endif + +#undef DUK_USE_VARIADIC_MACROS +#if defined(DUK_F_C99) || defined(DUK_F_CPP11) +#define DUK_USE_VARIADIC_MACROS +#endif + +/* C++ doesn't have standard designated union initializers ({ .foo = 1 }). */ +#undef DUK_USE_UNION_INITIALIZERS +#if defined(DUK_F_C99) +#define DUK_USE_UNION_INITIALIZERS +#endif + +/* Most portable, wastes space */ +#define DUK_USE_FLEX_ONESIZE + +/* Most portable, potentially wastes space */ +#define DUK_USE_PACK_DUMMY_MEMBER +#endif /* autodetect compiler */ + +/* uclibc */ +#if defined(__UCLIBC__) +#define DUK_F_UCLIBC +#endif + +/* + * Wrapper typedefs and constants for integer types, also sanity check types. + * + * C99 typedefs are quite good but not always available, and we want to avoid + * forcibly redefining the C99 typedefs. So, there are Duktape wrappers for + * all C99 typedefs and Duktape code should only use these typedefs. Type + * detection when C99 is not supported is best effort and may end up detecting + * some types incorrectly. + * + * Pointer sizes are a portability problem: pointers to different types may + * have a different size and function pointers are very difficult to manage + * portably. + * + * http://en.wikipedia.org/wiki/C_data_types#Fixed-width_integer_types + * + * Note: there's an interesting corner case when trying to define minimum + * signed integer value constants which leads to the current workaround of + * defining e.g. -0x80000000 as (-0x7fffffffL - 1L). See doc/code-issues.txt + * for a longer discussion. + * + * Note: avoid typecasts and computations in macro integer constants as they + * can then no longer be used in macro relational expressions (such as + * #if DUK_SIZE_MAX < 0xffffffffUL). There is internal code which relies on + * being able to compare DUK_SIZE_MAX against a limit. + */ + +/* XXX: add feature options to force basic types from outside? */ + +#if !defined(INT_MAX) +#error INT_MAX not defined +#endif + +/* Check that architecture is two's complement, standard C allows e.g. + * INT_MIN to be -2**31+1 (instead of -2**31). + */ +#if defined(INT_MAX) && defined(INT_MIN) +#if INT_MAX != -(INT_MIN + 1) +#error platform does not seem complement of two +#endif +#else +#error cannot check complement of two +#endif + +/* Pointer size determination based on __WORDSIZE or architecture when + * that's not available. + */ +#if defined(DUK_F_X86) || defined(DUK_F_X32) || \ + defined(DUK_F_M68K) || defined(DUK_F_PPC32) || \ + defined(DUK_F_BCC) || \ + (defined(__WORDSIZE) && (__WORDSIZE == 32)) +#define DUK_F_32BIT_PTRS +#elif defined(DUK_F_X64) || \ + (defined(__WORDSIZE) && (__WORDSIZE == 64)) +#define DUK_F_64BIT_PTRS +#else +/* not sure, not needed with C99 anyway */ +#endif + +/* Intermediate define for 'have inttypes.h' */ +#undef DUK_F_HAVE_INTTYPES +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \ + !(defined(DUK_F_AMIGAOS) && defined(DUK_F_VBCC)) +/* vbcc + AmigaOS has C99 but no inttypes.h */ +#define DUK_F_HAVE_INTTYPES +#elif defined(__cplusplus) && (__cplusplus >= 201103L) +/* C++11 apparently ratified stdint.h */ +#define DUK_F_HAVE_INTTYPES +#endif + +/* Basic integer typedefs and limits, preferably from inttypes.h, otherwise + * through automatic detection. + */ +#if defined(DUK_F_HAVE_INTTYPES) +/* C99 or compatible */ + +#define DUK_F_HAVE_64BIT +#include + +typedef uint8_t duk_uint8_t; +typedef int8_t duk_int8_t; +typedef uint16_t duk_uint16_t; +typedef int16_t duk_int16_t; +typedef uint32_t duk_uint32_t; +typedef int32_t duk_int32_t; +typedef uint64_t duk_uint64_t; +typedef int64_t duk_int64_t; +typedef uint_least8_t duk_uint_least8_t; +typedef int_least8_t duk_int_least8_t; +typedef uint_least16_t duk_uint_least16_t; +typedef int_least16_t duk_int_least16_t; +typedef uint_least32_t duk_uint_least32_t; +typedef int_least32_t duk_int_least32_t; +typedef uint_least64_t duk_uint_least64_t; +typedef int_least64_t duk_int_least64_t; +typedef uint_fast8_t duk_uint_fast8_t; +typedef int_fast8_t duk_int_fast8_t; +typedef uint_fast16_t duk_uint_fast16_t; +typedef int_fast16_t duk_int_fast16_t; +typedef uint_fast32_t duk_uint_fast32_t; +typedef int_fast32_t duk_int_fast32_t; +typedef uint_fast64_t duk_uint_fast64_t; +typedef int_fast64_t duk_int_fast64_t; +typedef uintptr_t duk_uintptr_t; +typedef intptr_t duk_intptr_t; +typedef uintmax_t duk_uintmax_t; +typedef intmax_t duk_intmax_t; + +#define DUK_UINT8_MIN 0 +#define DUK_UINT8_MAX UINT8_MAX +#define DUK_INT8_MIN INT8_MIN +#define DUK_INT8_MAX INT8_MAX +#define DUK_UINT_LEAST8_MIN 0 +#define DUK_UINT_LEAST8_MAX UINT_LEAST8_MAX +#define DUK_INT_LEAST8_MIN INT_LEAST8_MIN +#define DUK_INT_LEAST8_MAX INT_LEAST8_MAX +#define DUK_UINT_FAST8_MIN 0 +#define DUK_UINT_FAST8_MAX UINT_FAST8_MAX +#define DUK_INT_FAST8_MIN INT_FAST8_MIN +#define DUK_INT_FAST8_MAX INT_FAST8_MAX +#define DUK_UINT16_MIN 0 +#define DUK_UINT16_MAX UINT16_MAX +#define DUK_INT16_MIN INT16_MIN +#define DUK_INT16_MAX INT16_MAX +#define DUK_UINT_LEAST16_MIN 0 +#define DUK_UINT_LEAST16_MAX UINT_LEAST16_MAX +#define DUK_INT_LEAST16_MIN INT_LEAST16_MIN +#define DUK_INT_LEAST16_MAX INT_LEAST16_MAX +#define DUK_UINT_FAST16_MIN 0 +#define DUK_UINT_FAST16_MAX UINT_FAST16_MAX +#define DUK_INT_FAST16_MIN INT_FAST16_MIN +#define DUK_INT_FAST16_MAX INT_FAST16_MAX +#define DUK_UINT32_MIN 0 +#define DUK_UINT32_MAX UINT32_MAX +#define DUK_INT32_MIN INT32_MIN +#define DUK_INT32_MAX INT32_MAX +#define DUK_UINT_LEAST32_MIN 0 +#define DUK_UINT_LEAST32_MAX UINT_LEAST32_MAX +#define DUK_INT_LEAST32_MIN INT_LEAST32_MIN +#define DUK_INT_LEAST32_MAX INT_LEAST32_MAX +#define DUK_UINT_FAST32_MIN 0 +#define DUK_UINT_FAST32_MAX UINT_FAST32_MAX +#define DUK_INT_FAST32_MIN INT_FAST32_MIN +#define DUK_INT_FAST32_MAX INT_FAST32_MAX +#define DUK_UINT64_MIN 0 +#define DUK_UINT64_MAX UINT64_MAX +#define DUK_INT64_MIN INT64_MIN +#define DUK_INT64_MAX INT64_MAX +#define DUK_UINT_LEAST64_MIN 0 +#define DUK_UINT_LEAST64_MAX UINT_LEAST64_MAX +#define DUK_INT_LEAST64_MIN INT_LEAST64_MIN +#define DUK_INT_LEAST64_MAX INT_LEAST64_MAX +#define DUK_UINT_FAST64_MIN 0 +#define DUK_UINT_FAST64_MAX UINT_FAST64_MAX +#define DUK_INT_FAST64_MIN INT_FAST64_MIN +#define DUK_INT_FAST64_MAX INT_FAST64_MAX + +#define DUK_UINTPTR_MIN 0 +#define DUK_UINTPTR_MAX UINTPTR_MAX +#define DUK_INTPTR_MIN INTPTR_MIN +#define DUK_INTPTR_MAX INTPTR_MAX + +#define DUK_UINTMAX_MIN 0 +#define DUK_UINTMAX_MAX UINTMAX_MAX +#define DUK_INTMAX_MIN INTMAX_MIN +#define DUK_INTMAX_MAX INTMAX_MAX + +#define DUK_SIZE_MIN 0 +#define DUK_SIZE_MAX SIZE_MAX +#undef DUK_SIZE_MAX_COMPUTED + +#else /* C99 types */ + +/* When C99 types are not available, we use heuristic detection to get + * the basic 8, 16, 32, and (possibly) 64 bit types. The fast/least + * types are then assumed to be exactly the same for now: these could + * be improved per platform but C99 types are very often now available. + * 64-bit types are not available on all platforms; this is OK at least + * on 32-bit platforms. + * + * This detection code is necessarily a bit hacky and can provide typedefs + * and defines that won't work correctly on some exotic platform. + */ + +#if (defined(CHAR_BIT) && (CHAR_BIT == 8)) || \ + (defined(UCHAR_MAX) && (UCHAR_MAX == 255)) +typedef unsigned char duk_uint8_t; +typedef signed char duk_int8_t; +#else +#error cannot detect 8-bit type +#endif + +#if defined(USHRT_MAX) && (USHRT_MAX == 65535UL) +typedef unsigned short duk_uint16_t; +typedef signed short duk_int16_t; +#elif defined(UINT_MAX) && (UINT_MAX == 65535UL) +/* On some platforms int is 16-bit but long is 32-bit (e.g. PureC) */ +typedef unsigned int duk_uint16_t; +typedef signed int duk_int16_t; +#else +#error cannot detect 16-bit type +#endif + +#if defined(UINT_MAX) && (UINT_MAX == 4294967295UL) +typedef unsigned int duk_uint32_t; +typedef signed int duk_int32_t; +#elif defined(ULONG_MAX) && (ULONG_MAX == 4294967295UL) +/* On some platforms int is 16-bit but long is 32-bit (e.g. PureC) */ +typedef unsigned long duk_uint32_t; +typedef signed long duk_int32_t; +#else +#error cannot detect 32-bit type +#endif + +/* 64-bit type detection is a bit tricky. + * + * ULLONG_MAX is a standard define. __LONG_LONG_MAX__ and __ULONG_LONG_MAX__ + * are used by at least GCC (even if system headers don't provide ULLONG_MAX). + * Some GCC variants may provide __LONG_LONG_MAX__ but not __ULONG_LONG_MAX__. + * + * ULL / LL constants are rejected / warned about by some compilers, even if + * the compiler has a 64-bit type and the compiler/system headers provide an + * unsupported constant (ULL/LL)! Try to avoid using ULL / LL constants. + * As a side effect we can only check that e.g. ULONG_MAX is larger than 32 + * bits but can't be sure it is exactly 64 bits. Self tests will catch such + * cases. + */ +#undef DUK_F_HAVE_64BIT +#if !defined(DUK_F_HAVE_64BIT) && defined(ULONG_MAX) +#if (ULONG_MAX > 4294967295UL) +#define DUK_F_HAVE_64BIT +typedef unsigned long duk_uint64_t; +typedef signed long duk_int64_t; +#endif +#endif +#if !defined(DUK_F_HAVE_64BIT) && defined(ULLONG_MAX) +#if (ULLONG_MAX > 4294967295UL) +#define DUK_F_HAVE_64BIT +typedef unsigned long long duk_uint64_t; +typedef signed long long duk_int64_t; +#endif +#endif +#if !defined(DUK_F_HAVE_64BIT) && defined(__ULONG_LONG_MAX__) +#if (__ULONG_LONG_MAX__ > 4294967295UL) +#define DUK_F_HAVE_64BIT +typedef unsigned long long duk_uint64_t; +typedef signed long long duk_int64_t; +#endif +#endif +#if !defined(DUK_F_HAVE_64BIT) && defined(__LONG_LONG_MAX__) +#if (__LONG_LONG_MAX__ > 2147483647L) +#define DUK_F_HAVE_64BIT +typedef unsigned long long duk_uint64_t; +typedef signed long long duk_int64_t; +#endif +#endif +#if !defined(DUK_F_HAVE_64BIT) && \ + (defined(DUK_F_MINGW) || defined(DUK_F_MSVC)) +/* Both MinGW and MSVC have a 64-bit type. */ +#define DUK_F_HAVE_64BIT +typedef unsigned long duk_uint64_t; +typedef signed long duk_int64_t; +#endif +#if !defined(DUK_F_HAVE_64BIT) +/* cannot detect 64-bit type, not always needed so don't error */ +#endif + +typedef duk_uint8_t duk_uint_least8_t; +typedef duk_int8_t duk_int_least8_t; +typedef duk_uint16_t duk_uint_least16_t; +typedef duk_int16_t duk_int_least16_t; +typedef duk_uint32_t duk_uint_least32_t; +typedef duk_int32_t duk_int_least32_t; +typedef duk_uint8_t duk_uint_fast8_t; +typedef duk_int8_t duk_int_fast8_t; +typedef duk_uint16_t duk_uint_fast16_t; +typedef duk_int16_t duk_int_fast16_t; +typedef duk_uint32_t duk_uint_fast32_t; +typedef duk_int32_t duk_int_fast32_t; +#if defined(DUK_F_HAVE_64BIT) +typedef duk_uint64_t duk_uint_least64_t; +typedef duk_int64_t duk_int_least64_t; +typedef duk_uint64_t duk_uint_fast64_t; +typedef duk_int64_t duk_int_fast64_t; +#endif +#if defined(DUK_F_HAVE_64BIT) +typedef duk_uint64_t duk_uintmax_t; +typedef duk_int64_t duk_intmax_t; +#else +typedef duk_uint32_t duk_uintmax_t; +typedef duk_int32_t duk_intmax_t; +#endif + +/* Note: the funny looking computations for signed minimum 16-bit, 32-bit, and + * 64-bit values are intentional as the obvious forms (e.g. -0x80000000L) are + * -not- portable. See code-issues.txt for a detailed discussion. + */ +#define DUK_UINT8_MIN 0UL +#define DUK_UINT8_MAX 0xffUL +#define DUK_INT8_MIN (-0x80L) +#define DUK_INT8_MAX 0x7fL +#define DUK_UINT_LEAST8_MIN 0UL +#define DUK_UINT_LEAST8_MAX 0xffUL +#define DUK_INT_LEAST8_MIN (-0x80L) +#define DUK_INT_LEAST8_MAX 0x7fL +#define DUK_UINT_FAST8_MIN 0UL +#define DUK_UINT_FAST8_MAX 0xffUL +#define DUK_INT_FAST8_MIN (-0x80L) +#define DUK_INT_FAST8_MAX 0x7fL +#define DUK_UINT16_MIN 0UL +#define DUK_UINT16_MAX 0xffffUL +#define DUK_INT16_MIN (-0x7fffL - 1L) +#define DUK_INT16_MAX 0x7fffL +#define DUK_UINT_LEAST16_MIN 0UL +#define DUK_UINT_LEAST16_MAX 0xffffUL +#define DUK_INT_LEAST16_MIN (-0x7fffL - 1L) +#define DUK_INT_LEAST16_MAX 0x7fffL +#define DUK_UINT_FAST16_MIN 0UL +#define DUK_UINT_FAST16_MAX 0xffffUL +#define DUK_INT_FAST16_MIN (-0x7fffL - 1L) +#define DUK_INT_FAST16_MAX 0x7fffL +#define DUK_UINT32_MIN 0UL +#define DUK_UINT32_MAX 0xffffffffUL +#define DUK_INT32_MIN (-0x7fffffffL - 1L) +#define DUK_INT32_MAX 0x7fffffffL +#define DUK_UINT_LEAST32_MIN 0UL +#define DUK_UINT_LEAST32_MAX 0xffffffffUL +#define DUK_INT_LEAST32_MIN (-0x7fffffffL - 1L) +#define DUK_INT_LEAST32_MAX 0x7fffffffL +#define DUK_UINT_FAST32_MIN 0UL +#define DUK_UINT_FAST32_MAX 0xffffffffUL +#define DUK_INT_FAST32_MIN (-0x7fffffffL - 1L) +#define DUK_INT_FAST32_MAX 0x7fffffffL + +/* 64-bit constants. Since LL / ULL constants are not always available, + * use computed values. These values can't be used in preprocessor + * comparisons; flag them as such. + */ +#if defined(DUK_F_HAVE_64BIT) +#define DUK_UINT64_MIN ((duk_uint64_t) 0) +#define DUK_UINT64_MAX ((duk_uint64_t) -1) +#define DUK_INT64_MIN ((duk_int64_t) (~(DUK_UINT64_MAX >> 1))) +#define DUK_INT64_MAX ((duk_int64_t) (DUK_UINT64_MAX >> 1)) +#define DUK_UINT_LEAST64_MIN DUK_UINT64_MIN +#define DUK_UINT_LEAST64_MAX DUK_UINT64_MAX +#define DUK_INT_LEAST64_MIN DUK_INT64_MIN +#define DUK_INT_LEAST64_MAX DUK_INT64_MAX +#define DUK_UINT_FAST64_MIN DUK_UINT64_MIN +#define DUK_UINT_FAST64_MAX DUK_UINT64_MAX +#define DUK_INT_FAST64_MIN DUK_INT64_MIN +#define DUK_INT_FAST64_MAX DUK_INT64_MAX +#define DUK_UINT64_MIN_COMPUTED +#define DUK_UINT64_MAX_COMPUTED +#define DUK_INT64_MIN_COMPUTED +#define DUK_INT64_MAX_COMPUTED +#define DUK_UINT_LEAST64_MIN_COMPUTED +#define DUK_UINT_LEAST64_MAX_COMPUTED +#define DUK_INT_LEAST64_MIN_COMPUTED +#define DUK_INT_LEAST64_MAX_COMPUTED +#define DUK_UINT_FAST64_MIN_COMPUTED +#define DUK_UINT_FAST64_MAX_COMPUTED +#define DUK_INT_FAST64_MIN_COMPUTED +#define DUK_INT_FAST64_MAX_COMPUTED +#endif + +#if defined(DUK_F_HAVE_64BIT) +#define DUK_UINTMAX_MIN DUK_UINT64_MIN +#define DUK_UINTMAX_MAX DUK_UINT64_MAX +#define DUK_INTMAX_MIN DUK_INT64_MIN +#define DUK_INTMAX_MAX DUK_INT64_MAX +#define DUK_UINTMAX_MIN_COMPUTED +#define DUK_UINTMAX_MAX_COMPUTED +#define DUK_INTMAX_MIN_COMPUTED +#define DUK_INTMAX_MAX_COMPUTED +#else +#define DUK_UINTMAX_MIN 0UL +#define DUK_UINTMAX_MAX 0xffffffffUL +#define DUK_INTMAX_MIN (-0x7fffffffL - 1L) +#define DUK_INTMAX_MAX 0x7fffffffL +#endif + +/* This detection is not very reliable. */ +#if defined(DUK_F_32BIT_PTRS) +typedef duk_int32_t duk_intptr_t; +typedef duk_uint32_t duk_uintptr_t; +#define DUK_UINTPTR_MIN DUK_UINT32_MIN +#define DUK_UINTPTR_MAX DUK_UINT32_MAX +#define DUK_INTPTR_MIN DUK_INT32_MIN +#define DUK_INTPTR_MAX DUK_INT32_MAX +#elif defined(DUK_F_64BIT_PTRS) && defined(DUK_F_HAVE_64BIT) +typedef duk_int64_t duk_intptr_t; +typedef duk_uint64_t duk_uintptr_t; +#define DUK_UINTPTR_MIN DUK_UINT64_MIN +#define DUK_UINTPTR_MAX DUK_UINT64_MAX +#define DUK_INTPTR_MIN DUK_INT64_MIN +#define DUK_INTPTR_MAX DUK_INT64_MAX +#define DUK_UINTPTR_MIN_COMPUTED +#define DUK_UINTPTR_MAX_COMPUTED +#define DUK_INTPTR_MIN_COMPUTED +#define DUK_INTPTR_MAX_COMPUTED +#else +#error cannot determine intptr type +#endif + +/* SIZE_MAX may be missing so use an approximate value for it. */ +#undef DUK_SIZE_MAX_COMPUTED +#if !defined(SIZE_MAX) +#define DUK_SIZE_MAX_COMPUTED +#define SIZE_MAX ((size_t) (-1)) +#endif +#define DUK_SIZE_MIN 0 +#define DUK_SIZE_MAX SIZE_MAX + +#endif /* C99 types */ + +/* A few types are assumed to always exist. */ +typedef size_t duk_size_t; +typedef ptrdiff_t duk_ptrdiff_t; + +/* The best type for an "all around int" in Duktape internals is "at least + * 32 bit signed integer" which is most convenient. Same for unsigned type. + * Prefer 'int' when large enough, as it is almost always a convenient type. + */ +#if defined(UINT_MAX) && (UINT_MAX >= 0xffffffffUL) +typedef int duk_int_t; +typedef unsigned int duk_uint_t; +#define DUK_INT_MIN INT_MIN +#define DUK_INT_MAX INT_MAX +#define DUK_UINT_MIN 0 +#define DUK_UINT_MAX UINT_MAX +#else +typedef duk_int_fast32_t duk_int_t; +typedef duk_uint_fast32_t duk_uint_t; +#define DUK_INT_MIN DUK_INT_FAST32_MIN +#define DUK_INT_MAX DUK_INT_FAST32_MAX +#define DUK_UINT_MIN DUK_UINT_FAST32_MIN +#define DUK_UINT_MAX DUK_UINT_FAST32_MAX +#endif + +/* Same as 'duk_int_t' but guaranteed to be a 'fast' variant if this + * distinction matters for the CPU. These types are used mainly in the + * executor where it might really matter. + */ +typedef duk_int_fast32_t duk_int_fast_t; +typedef duk_uint_fast32_t duk_uint_fast_t; +#define DUK_INT_FAST_MIN DUK_INT_FAST32_MIN +#define DUK_INT_FAST_MAX DUK_INT_FAST32_MAX +#define DUK_UINT_FAST_MIN DUK_UINT_FAST32_MIN +#define DUK_UINT_FAST_MAX DUK_UINT_FAST32_MAX + +/* Small integers (16 bits or more) can fall back to the 'int' type, but + * have a typedef so they are marked "small" explicitly. + */ +typedef int duk_small_int_t; +typedef unsigned int duk_small_uint_t; +#define DUK_SMALL_INT_MIN INT_MIN +#define DUK_SMALL_INT_MAX INT_MAX +#define DUK_SMALL_UINT_MIN 0 +#define DUK_SMALL_UINT_MAX UINT_MAX + +/* Fast variants of small integers, again for really fast paths like the + * executor. + */ +typedef duk_int_fast16_t duk_small_int_fast_t; +typedef duk_uint_fast16_t duk_small_uint_fast_t; +#define DUK_SMALL_INT_FAST_MIN DUK_INT_FAST16_MIN +#define DUK_SMALL_INT_FAST_MAX DUK_INT_FAST16_MAX +#define DUK_SMALL_UINT_FAST_MIN DUK_UINT_FAST16_MIN +#define DUK_SMALL_UINT_FAST_MAX DUK_UINT_FAST16_MAX + +/* Boolean values are represented with the platform 'int'. */ +typedef duk_small_int_t duk_bool_t; +#define DUK_BOOL_MIN DUK_SMALL_INT_MIN +#define DUK_BOOL_MAX DUK_SMALL_INT_MAX + +/* Index values must have at least 32-bit signed range. */ +typedef duk_int_t duk_idx_t; +#define DUK_IDX_MIN DUK_INT_MIN +#define DUK_IDX_MAX DUK_INT_MAX + +/* Unsigned index variant. */ +typedef duk_uint_t duk_uidx_t; +#define DUK_UIDX_MIN DUK_UINT_MIN +#define DUK_UIDX_MAX DUK_UINT_MAX + +/* Array index values, could be exact 32 bits. + * Currently no need for signed duk_arridx_t. + */ +typedef duk_uint_t duk_uarridx_t; +#define DUK_UARRIDX_MIN DUK_UINT_MIN +#define DUK_UARRIDX_MAX DUK_UINT_MAX + +/* Duktape/C function return value, platform int is enough for now to + * represent 0, 1, or negative error code. Must be compatible with + * assigning truth values (e.g. duk_ret_t rc = (foo == bar);). + */ +typedef duk_small_int_t duk_ret_t; +#define DUK_RET_MIN DUK_SMALL_INT_MIN +#define DUK_RET_MAX DUK_SMALL_INT_MAX + +/* Error codes are represented with platform int. High bits are used + * for flags and such, so 32 bits are needed. + */ +typedef duk_int_t duk_errcode_t; +#define DUK_ERRCODE_MIN DUK_INT_MIN +#define DUK_ERRCODE_MAX DUK_INT_MAX + +/* Codepoint type. Must be 32 bits or more because it is used also for + * internal codepoints. The type is signed because negative codepoints + * are used as internal markers (e.g. to mark EOF or missing argument). + * (X)UTF-8/CESU-8 encode/decode take and return an unsigned variant to + * ensure duk_uint32_t casts back and forth nicely. Almost everything + * else uses the signed one. + */ +typedef duk_int_t duk_codepoint_t; +typedef duk_uint_t duk_ucodepoint_t; +#define DUK_CODEPOINT_MIN DUK_INT_MIN +#define DUK_CODEPOINT_MAX DUK_INT_MAX +#define DUK_UCODEPOINT_MIN DUK_UINT_MIN +#define DUK_UCODEPOINT_MAX DUK_UINT_MAX + +/* IEEE float/double typedef. */ +typedef float duk_float_t; +typedef double duk_double_t; + +/* We're generally assuming that we're working on a platform with a 32-bit + * address space. If DUK_SIZE_MAX is a typecast value (which is necessary + * if SIZE_MAX is missing), the check must be avoided because the + * preprocessor can't do a comparison. + */ +#if !defined(DUK_SIZE_MAX) +#error DUK_SIZE_MAX is undefined, probably missing SIZE_MAX +#elif !defined(DUK_SIZE_MAX_COMPUTED) +#if DUK_SIZE_MAX < 0xffffffffUL +/* On some systems SIZE_MAX can be smaller than max unsigned 32-bit value + * which seems incorrect if size_t is (at least) an unsigned 32-bit type. + * However, it doesn't seem useful to error out compilation if this is the + * case. + */ +#endif +#endif + +/* Type for public API calls. */ +typedef struct duk_hthread duk_context; + +/* Check whether we should use 64-bit integers or not. + * + * Quite incomplete now. Use 64-bit types if detected (C99 or other detection) + * unless they are known to be unreliable. For instance, 64-bit types are + * available on VBCC but seem to misbehave. + */ +#if defined(DUK_F_HAVE_64BIT) && !defined(DUK_F_VBCC) +#define DUK_USE_64BIT_OPS +#else +#undef DUK_USE_64BIT_OPS +#endif + +/* + * Fill-ins for platform, architecture, and compiler + */ + +#if !defined(DUK_SETJMP) +#define DUK_JMPBUF_TYPE jmp_buf +#define DUK_SETJMP(jb) setjmp((jb)) +#define DUK_LONGJMP(jb) longjmp((jb), 1) +#endif + +#if 0 +/* sigsetjmp() alternative */ +#define DUK_JMPBUF_TYPE sigjmp_buf +#define DUK_SETJMP(jb) sigsetjmp((jb)) +#define DUK_LONGJMP(jb) siglongjmp((jb), 1) +#endif + +typedef FILE duk_file; +#if !defined(DUK_STDIN) +#define DUK_STDIN stdin +#endif +#if !defined(DUK_STDOUT) +#define DUK_STDOUT stdout +#endif +#if !defined(DUK_STDERR) +#define DUK_STDERR stderr +#endif + +/* Special naming to avoid conflict with e.g. DUK_FREE() in duk_heap.h + * (which is unfortunately named). May sometimes need replacement, e.g. + * some compilers don't handle zero length or NULL correctly in realloc(). + */ +#if !defined(DUK_ANSI_MALLOC) +#define DUK_ANSI_MALLOC malloc +#endif +#if !defined(DUK_ANSI_REALLOC) +#define DUK_ANSI_REALLOC realloc +#endif +#if !defined(DUK_ANSI_CALLOC) +#define DUK_ANSI_CALLOC calloc +#endif +#if !defined(DUK_ANSI_FREE) +#define DUK_ANSI_FREE free +#endif + +/* ANSI C (various versions) and some implementations require that the + * pointer arguments to memset(), memcpy(), and memmove() be valid values + * even when byte size is 0 (even a NULL pointer is considered invalid in + * this context). Zero-size operations as such are allowed, as long as their + * pointer arguments point to a valid memory area. The DUK_MEMSET(), + * DUK_MEMCPY(), and DUK_MEMMOVE() macros require this same behavior, i.e.: + * (1) pointers must be valid and non-NULL, (2) zero size must otherwise be + * allowed. If these are not fulfilled, a macro wrapper is needed. + * + * http://stackoverflow.com/questions/5243012/is-it-guaranteed-to-be-safe-to-perform-memcpy0-0-0 + * http://lists.cs.uiuc.edu/pipermail/llvmdev/2007-October/011065.html + * + * Not sure what's the required behavior when a pointer points just past the + * end of a buffer, which often happens in practice (e.g. zero size memmoves). + * For example, if allocation size is 3, the following pointer would not + * technically point to a valid memory byte: + * + * <-- alloc --> + * | 0 | 1 | 2 | ..... + * ^-- p=3, points after last valid byte (2) + */ +#if !defined(DUK_MEMCPY) +#if defined(DUK_F_UCLIBC) +/* Old uclibcs have a broken memcpy so use memmove instead (this is overly wide + * now on purpose): http://lists.uclibc.org/pipermail/uclibc-cvs/2008-October/025511.html + */ +#define DUK_MEMCPY memmove +#else +#define DUK_MEMCPY memcpy +#endif +#endif +#if !defined(DUK_MEMMOVE) +#define DUK_MEMMOVE memmove +#endif +#if !defined(DUK_MEMCMP) +#define DUK_MEMCMP memcmp +#endif +#if !defined(DUK_MEMSET) +#define DUK_MEMSET memset +#endif +#if !defined(DUK_STRLEN) +#define DUK_STRLEN strlen +#endif +#if !defined(DUK_STRCMP) +#define DUK_STRCMP strcmp +#endif +#if !defined(DUK_STRNCMP) +#define DUK_STRNCMP strncmp +#endif +#if !defined(DUK_PRINTF) +#define DUK_PRINTF printf +#endif +#if !defined(DUK_FPRINTF) +#define DUK_FPRINTF fprintf +#endif +#if !defined(DUK_SPRINTF) +#define DUK_SPRINTF sprintf +#endif +#if !defined(DUK_SNPRINTF) +/* snprintf() is technically not part of C89 but usually available. */ +#define DUK_SNPRINTF snprintf +#endif +#if !defined(DUK_VSPRINTF) +#define DUK_VSPRINTF vsprintf +#endif +#if !defined(DUK_VSNPRINTF) +/* vsnprintf() is technically not part of C89 but usually available. */ +#define DUK_VSNPRINTF vsnprintf +#endif +#if !defined(DUK_SSCANF) +#define DUK_SSCANF sscanf +#endif +#if !defined(DUK_VSSCANF) +#define DUK_VSSCANF vsscanf +#endif +#if !defined(DUK_FOPEN) +#define DUK_FOPEN fopen +#endif +#if !defined(DUK_FCLOSE) +#define DUK_FCLOSE fclose +#endif +#if !defined(DUK_FREAD) +#define DUK_FREAD fread +#endif +#if !defined(DUK_FWRITE) +#define DUK_FWRITE fwrite +#endif +#if !defined(DUK_FSEEK) +#define DUK_FSEEK fseek +#endif +#if !defined(DUK_FTELL) +#define DUK_FTELL ftell +#endif +#if !defined(DUK_FFLUSH) +#define DUK_FFLUSH fflush +#endif +#if !defined(DUK_FPUTC) +#define DUK_FPUTC fputc +#endif +#if !defined(DUK_MEMZERO) +#define DUK_MEMZERO(p,n) DUK_MEMSET((p), 0, (n)) +#endif +#if !defined(DUK_ABORT) +#define DUK_ABORT abort +#endif +#if !defined(DUK_EXIT) +#define DUK_EXIT exit +#endif + +#if !defined(DUK_DOUBLE_2TO32) +#define DUK_DOUBLE_2TO32 4294967296.0 +#endif +#if !defined(DUK_DOUBLE_2TO31) +#define DUK_DOUBLE_2TO31 2147483648.0 +#endif + +#if !defined(DUK_DOUBLE_INFINITY) +#undef DUK_USE_COMPUTED_INFINITY +#if defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION < 40600) +/* GCC older than 4.6: avoid overflow warnings related to using INFINITY */ +#define DUK_DOUBLE_INFINITY (__builtin_inf()) +#elif defined(INFINITY) +#define DUK_DOUBLE_INFINITY ((double) INFINITY) +#elif !defined(DUK_F_VBCC) && !defined(DUK_F_MSVC) && !defined(DUK_F_BCC) +#define DUK_DOUBLE_INFINITY (1.0 / 0.0) +#else +/* In VBCC (1.0 / 0.0) results in a warning and 0.0 instead of infinity. + * Use a computed infinity (initialized when a heap is created at the + * latest). + */ +#define DUK_USE_COMPUTED_INFINITY +#define DUK_DOUBLE_INFINITY duk_computed_infinity +#endif +#endif + +#if !defined(DUK_DOUBLE_NAN) +#undef DUK_USE_COMPUTED_NAN +#if defined(NAN) +#define DUK_DOUBLE_NAN NAN +#elif !defined(DUK_F_VBCC) && !defined(DUK_F_MSVC) && !defined(DUK_F_BCC) +#define DUK_DOUBLE_NAN (0.0 / 0.0) +#else +/* In VBCC (0.0 / 0.0) results in a warning and 0.0 instead of NaN. + * In MSVC (VS2010 Express) (0.0 / 0.0) results in a compile error. + * Use a computed NaN (initialized when a heap is created at the + * latest). + */ +#define DUK_USE_COMPUTED_NAN +#define DUK_DOUBLE_NAN duk_computed_nan +#endif +#endif + +/* Many platforms are missing fpclassify() and friends, so use replacements + * if necessary. The replacement constants (FP_NAN etc) can be anything but + * match Linux constants now. + */ +#undef DUK_USE_REPL_FPCLASSIFY +#undef DUK_USE_REPL_SIGNBIT +#undef DUK_USE_REPL_ISFINITE +#undef DUK_USE_REPL_ISNAN +#undef DUK_USE_REPL_ISINF + +/* Complex condition broken into separate parts. */ +#undef DUK_F_USE_REPL_ALL +#if !(defined(FP_NAN) && defined(FP_INFINITE) && defined(FP_ZERO) && \ + defined(FP_SUBNORMAL) && defined(FP_NORMAL)) +/* Missing some obvious constants. */ +#define DUK_F_USE_REPL_ALL +#elif defined(DUK_F_AMIGAOS) && defined(DUK_F_VBCC) +/* VBCC is missing the built-ins even in C99 mode (perhaps a header issue). */ +#define DUK_F_USE_REPL_ALL +#elif defined(DUK_F_AMIGAOS) && defined(DUK_F_M68K) +/* AmigaOS + M68K seems to have math issues even when using GCC cross + * compilation. Use replacements for all AmigaOS versions on M68K + * regardless of compiler. + */ +#define DUK_F_USE_REPL_ALL +#elif defined(DUK_F_FREEBSD) && defined(DUK_F_CLANG) +/* Placeholder fix for (detection is wider than necessary): + * http://llvm.org/bugs/show_bug.cgi?id=17788 + */ +#define DUK_F_USE_REPL_ALL +#elif defined(DUK_F_UCLIBC) +/* At least some uclibc versions have broken floating point math. For + * example, fpclassify() can incorrectly classify certain NaN formats. + * To be safe, use replacements. + */ +#define DUK_F_USE_REPL_ALL +#endif + +#if defined(DUK_F_USE_REPL_ALL) +#define DUK_USE_REPL_FPCLASSIFY +#define DUK_USE_REPL_SIGNBIT +#define DUK_USE_REPL_ISFINITE +#define DUK_USE_REPL_ISNAN +#define DUK_USE_REPL_ISINF +#define DUK_FPCLASSIFY duk_repl_fpclassify +#define DUK_SIGNBIT duk_repl_signbit +#define DUK_ISFINITE duk_repl_isfinite +#define DUK_ISNAN duk_repl_isnan +#define DUK_ISINF duk_repl_isinf +#define DUK_FP_NAN 0 +#define DUK_FP_INFINITE 1 +#define DUK_FP_ZERO 2 +#define DUK_FP_SUBNORMAL 3 +#define DUK_FP_NORMAL 4 +#else +#define DUK_FPCLASSIFY fpclassify +#define DUK_SIGNBIT signbit +#define DUK_ISFINITE isfinite +#define DUK_ISNAN isnan +#define DUK_ISINF isinf +#define DUK_FP_NAN FP_NAN +#define DUK_FP_INFINITE FP_INFINITE +#define DUK_FP_ZERO FP_ZERO +#define DUK_FP_SUBNORMAL FP_SUBNORMAL +#define DUK_FP_NORMAL FP_NORMAL +#endif + +#if defined(DUK_F_USE_REPL_ALL) +#undef DUK_F_USE_REPL_ALL +#endif + +/* Some math functions are C99 only. This is also an issue with some + * embedded environments using uclibc where uclibc has been configured + * not to provide some functions. For now, use replacements whenever + * using uclibc. + */ +#undef DUK_USE_MATH_FMIN +#undef DUK_USE_MATH_FMAX +#undef DUK_USE_MATH_ROUND +#if defined(DUK_F_UCLIBC) +/* uclibc may be missing these */ +#elif defined(DUK_F_AMIGAOS) && defined(DUK_F_VBCC) +/* vbcc + AmigaOS may be missing these */ +#elif defined(DUK_F_MINT) +/* mint clib is missing these */ +#elif !defined(DUK_F_C99) && !defined(DUK_F_CPP11) +/* build is not C99 or C++11, play it safe */ +#else +/* C99 or C++11, no known issues */ +#define DUK_USE_MATH_FMIN +#define DUK_USE_MATH_FMAX +#define DUK_USE_MATH_ROUND +#endif + +/* These functions don't currently need replacement but are wrapped for + * completeness. Because these are used as function pointers, they need + * to be defined as concrete C functions (not macros). + */ +#if !defined(DUK_FABS) +#define DUK_FABS fabs +#endif +#if !defined(DUK_FMIN) +#define DUK_FMIN fmin +#endif +#if !defined(DUK_FMAX) +#define DUK_FMAX fmax +#endif +#if !defined(DUK_FLOOR) +#define DUK_FLOOR floor +#endif +#if !defined(DUK_CEIL) +#define DUK_CEIL ceil +#endif +#if !defined(DUK_FMOD) +#define DUK_FMOD fmod +#endif +#if !defined(DUK_POW) +#define DUK_POW pow +#endif +#if !defined(DUK_ACOS) +#define DUK_ACOS acos +#endif +#if !defined(DUK_ASIN) +#define DUK_ASIN asin +#endif +#if !defined(DUK_ATAN) +#define DUK_ATAN atan +#endif +#if !defined(DUK_ATAN2) +#define DUK_ATAN2 atan2 +#endif +#if !defined(DUK_SIN) +#define DUK_SIN sin +#endif +#if !defined(DUK_COS) +#define DUK_COS cos +#endif +#if !defined(DUK_TAN) +#define DUK_TAN tan +#endif +#if !defined(DUK_EXP) +#define DUK_EXP exp +#endif +#if !defined(DUK_LOG) +#define DUK_LOG log +#endif +#if !defined(DUK_SQRT) +#define DUK_SQRT sqrt +#endif + +/* NetBSD 6.0 x86 (at least) has a few problems with pow() semantics, + * see test-bug-netbsd-math-pow.js. Use NetBSD specific workaround. + * (This might be a wider problem; if so, generalize the define name.) + */ +#undef DUK_USE_POW_NETBSD_WORKAROUND +#if defined(DUK_F_NETBSD) +#define DUK_USE_POW_NETBSD_WORKAROUND +#endif + +/* Rely as little as possible on compiler behavior for NaN comparison, + * signed zero handling, etc. Currently never activated but may be needed + * for broken compilers. + */ +#undef DUK_USE_PARANOID_MATH + +/* There was a curious bug where test-bi-date-canceling.js would fail e.g. + * on 64-bit Ubuntu, gcc-4.8.1, -m32, and no -std=c99. Some date computations + * using doubles would be optimized which then broke some corner case tests. + * The problem goes away by adding 'volatile' to the datetime computations. + * Not sure what the actual triggering conditions are, but using this on + * non-C99 systems solves the known issues and has relatively little cost + * on other platforms. + */ +#undef DUK_USE_PARANOID_DATE_COMPUTATION +#if !defined(DUK_F_C99) +#define DUK_USE_PARANOID_DATE_COMPUTATION +#endif + +/* + * Byte order and double memory layout detection + * + * Endianness detection is a major portability hassle because the macros + * and headers are not standardized. There's even variance across UNIX + * platforms. Even with "standard" headers, details like underscore count + * varies between platforms, e.g. both __BYTE_ORDER and _BYTE_ORDER are used + * (Crossbridge has a single underscore, for instance). + * + * The checks below are structured with this in mind: several approaches are + * used, and at the end we check if any of them worked. This allows generic + * approaches to be tried first, and platform/compiler specific hacks tried + * last. As a last resort, the user can force a specific endianness, as it's + * not likely that automatic detection will work on the most exotic platforms. + * + * Duktape supports little and big endian machines. There's also support + * for a hybrid used by some ARM machines where integers are little endian + * but IEEE double values use a mixed order (12345678 -> 43218765). This + * byte order for doubles is referred to as "mixed endian". + */ + +/* For custom platforms allow user to define byteorder explicitly. + * Since endianness headers are not standardized, this is a useful + * workaround for custom platforms for which endianness detection + * is not directly supported. Perhaps custom hardware is used and + * user cannot submit upstream patches. + */ +#if defined(DUK_OPT_FORCE_BYTEORDER) +#undef DUK_USE_BYTEORDER +#if (DUK_OPT_FORCE_BYTEORDER == 1) +#define DUK_USE_BYTEORDER 1 +#elif (DUK_OPT_FORCE_BYTEORDER == 2) +#define DUK_USE_BYTEORDER 2 +#elif (DUK_OPT_FORCE_BYTEORDER == 3) +#define DUK_USE_BYTEORDER 3 +#else +#error invalid DUK_OPT_FORCE_BYTEORDER value +#endif +#endif /* DUK_OPT_FORCE_BYTEORDER */ + +/* GCC and Clang provide endianness defines as built-in predefines, with + * leading and trailing double underscores (e.g. __BYTE_ORDER__). See + * output of "make gccpredefs" and "make clangpredefs". Clang doesn't + * seem to provide __FLOAT_WORD_ORDER__; assume not mixed endian for clang. + * http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html + */ +#if !defined(DUK_USE_BYTEORDER) && defined(__BYTE_ORDER__) +#if defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) +#if defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__) +#define DUK_USE_BYTEORDER 1 +#elif defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__) +#define DUK_USE_BYTEORDER 2 +#elif !defined(__FLOAT_WORD_ORDER__) +/* Float word order not known, assume not a hybrid. */ +#define DUK_USE_BYTEORDER 1 +#else +/* Byte order is little endian but cannot determine IEEE double word order. */ +#endif /* float word order */ +#elif defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#if defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__) +#define DUK_USE_BYTEORDER 3 +#elif !defined(__FLOAT_WORD_ORDER__) +/* Float word order not known, assume not a hybrid. */ +#define DUK_USE_BYTEORDER 3 +#else +/* Byte order is big endian but cannot determine IEEE double word order. */ +#endif /* float word order */ +#else +/* Cannot determine byte order; __ORDER_PDP_ENDIAN__ is related to 32-bit + * integer ordering and is not relevant. + */ +#endif /* integer byte order */ +#endif /* !defined(DUK_USE_BYTEORDER) && defined(__BYTE_ORDER__) */ + +/* More or less standard endianness predefines provided by header files. + * The ARM hybrid case is detected by assuming that __FLOAT_WORD_ORDER + * will be big endian, see: http://lists.mysql.com/internals/443. + * On some platforms some defines may be present with an empty value which + * causes comparisons to fail: https://github.com/svaarala/duktape/issues/453. + */ +#if !defined(DUK_USE_BYTEORDER) +#if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN) || \ + defined(_BYTE_ORDER) && defined(_LITTLE_ENDIAN) && (_BYTE_ORDER == _LITTLE_ENDIAN) || \ + defined(__LITTLE_ENDIAN__) +#if defined(__FLOAT_WORD_ORDER) && defined(__LITTLE_ENDIAN) && (__FLOAT_WORD_ORDER == __LITTLE_ENDIAN) || \ + defined(_FLOAT_WORD_ORDER) && defined(_LITTLE_ENDIAN) && (_FLOAT_WORD_ORDER == _LITTLE_ENDIAN) +#define DUK_USE_BYTEORDER 1 +#elif defined(__FLOAT_WORD_ORDER) && defined(__BIG_ENDIAN) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN) || \ + defined(_FLOAT_WORD_ORDER) && defined(_BIG_ENDIAN) && (_FLOAT_WORD_ORDER == _BIG_ENDIAN) +#define DUK_USE_BYTEORDER 2 +#elif !defined(__FLOAT_WORD_ORDER) && !defined(_FLOAT_WORD_ORDER) +/* Float word order not known, assume not a hybrid. */ +#define DUK_USE_BYTEORDER 1 +#else +/* Byte order is little endian but cannot determine IEEE double word order. */ +#endif /* float word order */ +#elif defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN) || \ + defined(_BYTE_ORDER) && defined(_BIG_ENDIAN) && (_BYTE_ORDER == _BIG_ENDIAN) || \ + defined(__BIG_ENDIAN__) +#if defined(__FLOAT_WORD_ORDER) && defined(__BIG_ENDIAN) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN) || \ + defined(_FLOAT_WORD_ORDER) && defined(_BIG_ENDIAN) && (_FLOAT_WORD_ORDER == _BIG_ENDIAN) +#define DUK_USE_BYTEORDER 3 +#elif !defined(__FLOAT_WORD_ORDER) && !defined(_FLOAT_WORD_ORDER) +/* Float word order not known, assume not a hybrid. */ +#define DUK_USE_BYTEORDER 3 +#else +/* Byte order is big endian but cannot determine IEEE double word order. */ +#endif /* float word order */ +#else +/* Cannot determine byte order. */ +#endif /* integer byte order */ +#endif /* !defined(DUK_USE_BYTEORDER) */ + +/* QNX gcc cross compiler seems to define e.g. __LITTLEENDIAN__ or __BIGENDIAN__: + * $ /opt/qnx650/host/linux/x86/usr/bin/i486-pc-nto-qnx6.5.0-gcc -dM -E - > 24) | \ + ((((duk_uint32_t) (x)) >> 8) & 0xff00UL) | \ + ((((duk_uint32_t) (x)) << 8) & 0xff0000UL) | \ + (((duk_uint32_t) (x)) << 24)) +#endif +#if !defined(DUK_BSWAP16) +#define DUK_BSWAP16(x) \ + ((duk_uint16_t) (x) >> 8) | \ + ((duk_uint16_t) (x) << 8) +#endif + +/* DUK_USE_VARIADIC_MACROS: required from compilers, so no fill-in. */ +/* DUK_USE_UNION_INITIALIZERS: required from compilers, so no fill-in. */ + +#if !(defined(DUK_USE_FLEX_C99) || defined(DUK_USE_FLEX_ZEROSIZE) || defined(DUK_USE_FLEX_ONESIZE)) +#if defined(DUK_F_C99) +#define DUK_USE_FLEX_C99 +#else +#define DUK_USE_FLEX_ZEROSIZE /* Not standard but common enough */ +#endif +#endif + +#if !(defined(DUK_USE_PACK_GCC_ATTR) || defined(DUK_USE_PACK_CLANG_ATTR) || \ + defined(DUK_USE_PACK_MSVC_PRAGMA) || defined(DUK_USE_PACK_DUMMY_MEMBER)) +#define DUK_USE_PACK_DUMMY_MEMBER +#endif + +#if 0 /* not defined by default */ +#undef DUK_USE_GCC_PRAGMAS +#endif + +/* Workaround for GH-323: avoid inlining control when compiling from + * multiple sources, as it causes compiler portability trouble. + */ +#if !defined(DUK_SINGLE_FILE) +#undef DUK_NOINLINE +#undef DUK_INLINE +#undef DUK_ALWAYS_INLINE +#define DUK_NOINLINE /*nop*/ +#define DUK_INLINE /*nop*/ +#define DUK_ALWAYS_INLINE /*nop*/ +#endif + +/* + * Check whether or not a packed duk_tval representation is possible. + * What's basically required is that pointers are 32-bit values + * (sizeof(void *) == 4). Best effort check, not always accurate. + * If guess goes wrong, crashes may result; self tests also verify + * the guess. + */ + +/* Explicit marker needed; may be 'defined', 'undefined, 'or 'not provided'. */ +#if !defined(DUK_F_PACKED_TVAL_PROVIDED) +#undef DUK_F_PACKED_TVAL_POSSIBLE + +/* Strict C99 case: DUK_UINTPTR_MAX (= UINTPTR_MAX) should be very reliable */ +#if !defined(DUK_F_PACKED_TVAL_POSSIBLE) && defined(DUK_UINTPTR_MAX) +#if (DUK_UINTPTR_MAX <= 0xffffffffUL) +#define DUK_F_PACKED_TVAL_POSSIBLE +#endif +#endif + +/* Non-C99 case, still relying on DUK_UINTPTR_MAX, as long as it is not a computed value */ +#if !defined(DUK_F_PACKED_TVAL_POSSIBLE) && defined(DUK_UINTPTR_MAX) && !defined(DUK_UINTPTR_MAX_COMPUTED) +#if (DUK_UINTPTR_MAX <= 0xffffffffUL) +#define DUK_F_PACKED_TVAL_POSSIBLE +#endif +#endif + +/* DUK_SIZE_MAX (= SIZE_MAX) is often reliable */ +#if !defined(DUK_F_PACKED_TVAL_POSSIBLE) && defined(DUK_SIZE_MAX) && !defined(DUK_SIZE_MAX_COMPUTED) +#if (DUK_SIZE_MAX <= 0xffffffffUL) +#define DUK_F_PACKED_TVAL_POSSIBLE +#endif +#endif + +#undef DUK_USE_PACKED_TVAL +#if defined(DUK_F_PACKED_TVAL_POSSIBLE) +#define DUK_USE_PACKED_TVAL +#endif + +#undef DUK_F_PACKED_TVAL_POSSIBLE +#endif /* DUK_F_PACKED_TVAL_PROVIDED */ + +/* Feature option forcing. */ +#if defined(DUK_OPT_NO_PACKED_TVAL) +#undef DUK_USE_PACKED_TVAL +#elif defined(DUK_OPT_PACKED_TVAL) +#undef DUK_USE_PACKED_TVAL +#define DUK_USE_PACKED_TVAL +#endif +/* Object property allocation layout has implications for memory and code + * footprint and generated code size/speed. The best layout also depends + * on whether the platform has alignment requirements or benefits from + * having mostly aligned accesses. + */ +#undef DUK_USE_HOBJECT_LAYOUT_1 +#undef DUK_USE_HOBJECT_LAYOUT_2 +#undef DUK_USE_HOBJECT_LAYOUT_3 +#if (DUK_USE_ALIGN_BY == 1) +/* On platforms without any alignment issues, layout 1 is preferable + * because it compiles to slightly less code and provides direct access + * to property keys. + */ +#define DUK_USE_HOBJECT_LAYOUT_1 +#else +/* On other platforms use layout 2, which requires some padding but + * is a bit more natural than layout 3 in ordering the entries. Layout + * 3 is currently not used. + */ +#define DUK_USE_HOBJECT_LAYOUT_2 +#endif + +/* GCC/clang inaccurate math would break compliance and probably duk_tval, + * so refuse to compile. Relax this if -ffast-math is tested to work. + */ +#if defined(__FAST_MATH__) +#error __FAST_MATH__ defined, refusing to compile +#endif + +/* + * Feature option handling + */ + +#if !defined(DUK_USE_ALIGN_BY) +#if defined(DUK_OPT_FORCE_ALIGN) +#define DUK_USE_ALIGN_BY DUK_OPT_FORCE_ALIGN +#else +#define DUK_USE_ALIGN_BY 8 +#endif +#endif + +#if defined(DUK_OPT_ASSERTIONS) +#define DUK_USE_ASSERTIONS +#elif defined(DUK_OPT_NO_ASSERTIONS) +#undef DUK_USE_ASSERTIONS +#else +#undef DUK_USE_ASSERTIONS +#endif + +#if defined(DUK_OPT_AUGMENT_ERRORS) +#define DUK_USE_AUGMENT_ERROR_CREATE +#elif defined(DUK_OPT_NO_AUGMENT_ERRORS) +#undef DUK_USE_AUGMENT_ERROR_CREATE +#else +#define DUK_USE_AUGMENT_ERROR_CREATE +#endif + +#if defined(DUK_OPT_AUGMENT_ERRORS) +#define DUK_USE_AUGMENT_ERROR_THROW +#elif defined(DUK_OPT_NO_AUGMENT_ERRORS) +#undef DUK_USE_AUGMENT_ERROR_THROW +#else +#define DUK_USE_AUGMENT_ERROR_THROW +#endif + +#if defined(DUK_OPT_BROWSER_LIKE) +#define DUK_USE_BROWSER_LIKE +#elif defined(DUK_OPT_NO_BROWSER_LIKE) +#undef DUK_USE_BROWSER_LIKE +#else +#define DUK_USE_BROWSER_LIKE +#endif + +#if defined(DUK_OPT_BUFFEROBJECT_SUPPORT) +#define DUK_USE_BUFFEROBJECT_SUPPORT +#elif defined(DUK_OPT_NO_BUFFEROBJECT_SUPPORT) +#undef DUK_USE_BUFFEROBJECT_SUPPORT +#else +#define DUK_USE_BUFFEROBJECT_SUPPORT +#endif + +#if defined(DUK_OPT_BUFLEN16) +#define DUK_USE_BUFLEN16 +#elif defined(DUK_OPT_NO_BUFLEN16) +#undef DUK_USE_BUFLEN16 +#else +#undef DUK_USE_BUFLEN16 +#endif + +#if defined(DUK_OPT_BYTECODE_DUMP_SUPPORT) +#define DUK_USE_BYTECODE_DUMP_SUPPORT +#elif defined(DUK_OPT_NO_BYTECODE_DUMP_SUPPORT) +#undef DUK_USE_BYTECODE_DUMP_SUPPORT +#else +#define DUK_USE_BYTECODE_DUMP_SUPPORT +#endif + +#if defined(DUK_OPT_COMMONJS_MODULES) +#define DUK_USE_COMMONJS_MODULES +#elif defined(DUK_OPT_NO_COMMONJS_MODULES) +#undef DUK_USE_COMMONJS_MODULES +#else +#define DUK_USE_COMMONJS_MODULES +#endif + +#if defined(DUK_OPT_CPP_EXCEPTIONS) +#define DUK_USE_CPP_EXCEPTIONS +#elif defined(DUK_OPT_NO_CPP_EXCEPTIONS) +#undef DUK_USE_CPP_EXCEPTIONS +#else +#undef DUK_USE_CPP_EXCEPTIONS +#endif + +#if defined(DUK_OPT_DATAPTR16) +#define DUK_USE_DATAPTR16 +#elif defined(DUK_OPT_NO_DATAPTR16) +#undef DUK_USE_DATAPTR16 +#else +#undef DUK_USE_DATAPTR16 +#endif + +#if defined(DUK_OPT_DATAPTR_DEC16) +#define DUK_USE_DATAPTR_DEC16(udata,ptr) DUK_OPT_DATAPTR_DEC16((udata),(ptr)) +#else +#undef DUK_USE_DATAPTR_DEC16 +#endif + +#if defined(DUK_OPT_DATAPTR_ENC16) +#define DUK_USE_DATAPTR_ENC16(udata,ptr) DUK_OPT_DATAPTR_ENC16((udata),(ptr)) +#else +#undef DUK_USE_DATAPTR_ENC16 +#endif + +#if defined(DUK_OPT_DDDPRINT) +#define DUK_USE_DDDPRINT +#elif defined(DUK_OPT_NO_DDDPRINT) +#undef DUK_USE_DDDPRINT +#else +#undef DUK_USE_DDDPRINT +#endif + +#if defined(DUK_OPT_DDPRINT) +#define DUK_USE_DDPRINT +#elif defined(DUK_OPT_NO_DDPRINT) +#undef DUK_USE_DDPRINT +#else +#undef DUK_USE_DDPRINT +#endif + +#if defined(DUK_OPT_DEBUG) +#define DUK_USE_DEBUG +#elif defined(DUK_OPT_NO_DEBUG) +#undef DUK_USE_DEBUG +#else +#undef DUK_USE_DEBUG +#endif + +#if defined(DUK_OPT_DEBUGGER_DUMPHEAP) +#define DUK_USE_DEBUGGER_DUMPHEAP +#elif defined(DUK_OPT_NO_DEBUGGER_DUMPHEAP) +#undef DUK_USE_DEBUGGER_DUMPHEAP +#else +#undef DUK_USE_DEBUGGER_DUMPHEAP +#endif + +#if defined(DUK_OPT_DEBUGGER_FWD_LOGGING) +#define DUK_USE_DEBUGGER_FWD_LOGGING +#elif defined(DUK_OPT_NO_DEBUGGER_FWD_LOGGING) +#undef DUK_USE_DEBUGGER_FWD_LOGGING +#else +#undef DUK_USE_DEBUGGER_FWD_LOGGING +#endif + +#if defined(DUK_OPT_DEBUGGER_FWD_PRINTALERT) +#define DUK_USE_DEBUGGER_FWD_PRINTALERT +#elif defined(DUK_OPT_NO_DEBUGGER_FWD_PRINTALERT) +#undef DUK_USE_DEBUGGER_FWD_PRINTALERT +#else +#undef DUK_USE_DEBUGGER_FWD_PRINTALERT +#endif + +#if defined(DUK_OPT_DEBUGGER_INSPECT) +#define DUK_USE_DEBUGGER_INSPECT +#elif defined(DUK_OPT_NO_DEBUGGER_INSPECT) +#undef DUK_USE_DEBUGGER_INSPECT +#else +#undef DUK_USE_DEBUGGER_INSPECT +#endif + +#if defined(DUK_OPT_DEBUGGER_PAUSE_UNCAUGHT) +#define DUK_USE_DEBUGGER_PAUSE_UNCAUGHT +#elif defined(DUK_OPT_NO_DEBUGGER_PAUSE_UNCAUGHT) +#undef DUK_USE_DEBUGGER_PAUSE_UNCAUGHT +#else +#undef DUK_USE_DEBUGGER_PAUSE_UNCAUGHT +#endif + +#if defined(DUK_OPT_DEBUGGER_SUPPORT) +#define DUK_USE_DEBUGGER_SUPPORT +#elif defined(DUK_OPT_NO_DEBUGGER_SUPPORT) +#undef DUK_USE_DEBUGGER_SUPPORT +#else +#undef DUK_USE_DEBUGGER_SUPPORT +#endif + +#if defined(DUK_OPT_DEBUGGER_THROW_NOTIFY) +#define DUK_USE_DEBUGGER_THROW_NOTIFY +#elif defined(DUK_OPT_NO_DEBUGGER_THROW_NOTIFY) +#undef DUK_USE_DEBUGGER_THROW_NOTIFY +#else +#define DUK_USE_DEBUGGER_THROW_NOTIFY +#endif + +#if defined(DUK_OPT_DEBUGGER_TRANSPORT_TORTURE) +#define DUK_USE_DEBUGGER_TRANSPORT_TORTURE +#elif defined(DUK_OPT_NO_DEBUGGER_TRANSPORT_TORTURE) +#undef DUK_USE_DEBUGGER_TRANSPORT_TORTURE +#else +#undef DUK_USE_DEBUGGER_TRANSPORT_TORTURE +#endif + +#if defined(DUK_OPT_DEBUG_BUFSIZE) +#define DUK_USE_DEBUG_BUFSIZE DUK_OPT_DEBUG_BUFSIZE +#else +#define DUK_USE_DEBUG_BUFSIZE 65536L +#endif + +#if defined(DUK_OPT_REFERENCE_COUNTING) +#define DUK_USE_DOUBLE_LINKED_HEAP +#elif defined(DUK_OPT_NO_REFERENCE_COUNTING) +#undef DUK_USE_DOUBLE_LINKED_HEAP +#else +#define DUK_USE_DOUBLE_LINKED_HEAP +#endif + +#if defined(DUK_OPT_DPRINT) +#define DUK_USE_DPRINT +#elif defined(DUK_OPT_NO_DPRINT) +#undef DUK_USE_DPRINT +#else +#undef DUK_USE_DPRINT +#endif + +#if defined(DUK_OPT_DPRINT_COLORS) +#define DUK_USE_DPRINT_COLORS +#elif defined(DUK_OPT_NO_DPRINT_COLORS) +#undef DUK_USE_DPRINT_COLORS +#else +#undef DUK_USE_DPRINT_COLORS +#endif + +#if defined(DUK_OPT_DPRINT_RDTSC) +#define DUK_USE_DPRINT_RDTSC +#elif defined(DUK_OPT_NO_DPRINT_RDTSC) +#undef DUK_USE_DPRINT_RDTSC +#else +#undef DUK_USE_DPRINT_RDTSC +#endif + +#if defined(DUK_OPT_AUGMENT_ERRORS) +#define DUK_USE_ERRCREATE +#elif defined(DUK_OPT_NO_AUGMENT_ERRORS) +#undef DUK_USE_ERRCREATE +#else +#define DUK_USE_ERRCREATE +#endif + +#if defined(DUK_OPT_AUGMENT_ERRORS) +#define DUK_USE_ERRTHROW +#elif defined(DUK_OPT_NO_AUGMENT_ERRORS) +#undef DUK_USE_ERRTHROW +#else +#define DUK_USE_ERRTHROW +#endif + +#if defined(DUK_OPT_ES6_OBJECT_PROTO_PROPERTY) +#define DUK_USE_ES6_OBJECT_PROTO_PROPERTY +#elif defined(DUK_OPT_NO_ES6_OBJECT_PROTO_PROPERTY) +#undef DUK_USE_ES6_OBJECT_PROTO_PROPERTY +#else +#define DUK_USE_ES6_OBJECT_PROTO_PROPERTY +#endif + +#if defined(DUK_OPT_ES6_OBJECT_SETPROTOTYPEOF) +#define DUK_USE_ES6_OBJECT_SETPROTOTYPEOF +#elif defined(DUK_OPT_NO_ES6_OBJECT_SETPROTOTYPEOF) +#undef DUK_USE_ES6_OBJECT_SETPROTOTYPEOF +#else +#define DUK_USE_ES6_OBJECT_SETPROTOTYPEOF +#endif + +#if defined(DUK_OPT_ES6_PROXY) +#define DUK_USE_ES6_PROXY +#elif defined(DUK_OPT_NO_ES6_PROXY) +#undef DUK_USE_ES6_PROXY +#else +#define DUK_USE_ES6_PROXY +#endif + +#if defined(DUK_OPT_ES6_REGEXP_BRACES) +#define DUK_USE_ES6_REGEXP_BRACES +#elif defined(DUK_OPT_NO_ES6_REGEXP_BRACES) +#undef DUK_USE_ES6_REGEXP_BRACES +#else +#define DUK_USE_ES6_REGEXP_BRACES +#endif + +#undef DUK_USE_EXEC_INDIRECT_BOUND_CHECK +#if defined(DUK_OPT_DEBUG) || defined(DUK_OPT_ASSERTIONS) +/* Enabled with debug/assertions just so that any issues can be caught. */ +#define DUK_USE_EXEC_INDIRECT_BOUND_CHECK +#endif + +#undef DUK_USE_EXEC_TIMEOUT_CHECK +#if defined(DUK_OPT_EXEC_TIMEOUT_CHECK) +#define DUK_USE_EXEC_TIMEOUT_CHECK(udata) DUK_OPT_EXEC_TIMEOUT_CHECK((udata)) +#endif + +#undef DUK_USE_EXTSTR_FREE +#if defined(DUK_OPT_EXTERNAL_STRINGS) && defined(DUK_OPT_EXTSTR_FREE) +#define DUK_USE_EXTSTR_FREE(udata,ptr) DUK_OPT_EXTSTR_FREE((udata), (ptr)) +#endif + +#undef DUK_USE_EXTSTR_INTERN_CHECK +#if defined(DUK_OPT_EXTERNAL_STRINGS) && defined(DUK_OPT_EXTSTR_INTERN_CHECK) +#define DUK_USE_EXTSTR_INTERN_CHECK(udata,ptr,len) DUK_OPT_EXTSTR_INTERN_CHECK((udata), (ptr), (len)) +#endif + +/* Support for 48-bit signed integer duk_tval with transparent semantics. */ +#undef DUK_USE_FASTINT +#if defined(DUK_OPT_FASTINT) +#if !defined(DUK_F_HAVE_64BIT) +#error DUK_OPT_FASTINT requires 64-bit integer type support at the moment +#endif +#define DUK_USE_FASTINT +#endif + +#if defined(DUK_OPT_FILE_IO) +#define DUK_USE_FILE_IO +#elif defined(DUK_OPT_NO_FILE_IO) +#undef DUK_USE_FILE_IO +#else +#define DUK_USE_FILE_IO +#endif + +#if defined(DUK_OPT_FUNCPTR16) +#define DUK_USE_FUNCPTR16 +#elif defined(DUK_OPT_NO_FUNCPTR16) +#undef DUK_USE_FUNCPTR16 +#else +#undef DUK_USE_FUNCPTR16 +#endif + +#if defined(DUK_OPT_FUNCPTR_DEC16) +#define DUK_USE_FUNCPTR_DEC16(udata,ptr) DUK_OPT_FUNCPTR_DEC16((udata),(ptr)) +#else +#undef DUK_USE_FUNCPTR_DEC16 +#endif + +#if defined(DUK_OPT_FUNCPTR_ENC16) +#define DUK_USE_FUNCPTR_ENC16(udata,ptr) DUK_OPT_FUNCPTR_ENC16((udata),(ptr)) +#else +#undef DUK_USE_FUNCPTR_ENC16 +#endif + +#if defined(DUK_OPT_GC_TORTURE) +#define DUK_USE_GC_TORTURE +#elif defined(DUK_OPT_NO_GC_TORTURE) +#undef DUK_USE_GC_TORTURE +#else +#undef DUK_USE_GC_TORTURE +#endif + +#if defined(DUK_OPT_HEAPPTR16) +#define DUK_USE_HEAPPTR16 +#elif defined(DUK_OPT_NO_HEAPPTR16) +#undef DUK_USE_HEAPPTR16 +#else +#undef DUK_USE_HEAPPTR16 +#endif + +#if defined(DUK_OPT_HEAPPTR_DEC16) +#define DUK_USE_HEAPPTR_DEC16(udata,ptr) DUK_OPT_HEAPPTR_DEC16((udata),(ptr)) +#else +#undef DUK_USE_HEAPPTR_DEC16 +#endif + +#if defined(DUK_OPT_HEAPPTR_ENC16) +#define DUK_USE_HEAPPTR_ENC16(udata,ptr) DUK_OPT_HEAPPTR_ENC16((udata),(ptr)) +#else +#undef DUK_USE_HEAPPTR_ENC16 +#endif + +/* For now, hash part is dropped if and only if 16-bit object fields are used. */ +#define DUK_USE_HOBJECT_HASH_PART +#if defined(DUK_OPT_OBJSIZES16) +#undef DUK_USE_HOBJECT_HASH_PART +#endif + +#if defined(DUK_OPT_HSTRING_CLEN) +#define DUK_USE_HSTRING_CLEN +#elif defined(DUK_OPT_NO_HSTRING_CLEN) +#undef DUK_USE_HSTRING_CLEN +#else +#define DUK_USE_HSTRING_CLEN +#endif + +#if defined(DUK_OPT_EXTERNAL_STRINGS) +#define DUK_USE_HSTRING_EXTDATA +#elif defined(DUK_OPT_NO_EXTERNAL_STRINGS) +#undef DUK_USE_HSTRING_EXTDATA +#else +#undef DUK_USE_HSTRING_EXTDATA +#endif + +#if defined(DUK_OPT_INTERRUPT_COUNTER) +#define DUK_USE_INTERRUPT_COUNTER +#elif defined(DUK_OPT_NO_INTERRUPT_COUNTER) +#undef DUK_USE_INTERRUPT_COUNTER +#else +#undef DUK_USE_INTERRUPT_COUNTER +#endif + +#if defined(DUK_OPT_JC) +#define DUK_USE_JC +#elif defined(DUK_OPT_NO_JC) +#undef DUK_USE_JC +#else +#define DUK_USE_JC +#endif + +#if defined(DUK_OPT_JSON_STRINGIFY_FASTPATH) +#define DUK_USE_JSON_STRINGIFY_FASTPATH +#elif defined(DUK_OPT_NO_JSON_STRINGIFY_FASTPATH) +#undef DUK_USE_JSON_STRINGIFY_FASTPATH +#else +#undef DUK_USE_JSON_STRINGIFY_FASTPATH +#endif + +#if defined(DUK_OPT_JX) +#define DUK_USE_JX +#elif defined(DUK_OPT_NO_JX) +#undef DUK_USE_JX +#else +#define DUK_USE_JX +#endif + +#if defined(DUK_OPT_LIGHTFUNC_BUILTINS) +#define DUK_USE_LIGHTFUNC_BUILTINS +#elif defined(DUK_OPT_NO_LIGHTFUNC_BUILTINS) +#undef DUK_USE_LIGHTFUNC_BUILTINS +#else +#undef DUK_USE_LIGHTFUNC_BUILTINS +#endif + +#if defined(DUK_OPT_MARK_AND_SWEEP) +#define DUK_USE_MARK_AND_SWEEP +#elif defined(DUK_OPT_NO_MARK_AND_SWEEP) +#undef DUK_USE_MARK_AND_SWEEP +#else +#define DUK_USE_MARK_AND_SWEEP +#endif + +#if defined(DUK_OPT_MS_STRINGTABLE_RESIZE) +#define DUK_USE_MS_STRINGTABLE_RESIZE +#elif defined(DUK_OPT_NO_MS_STRINGTABLE_RESIZE) +#undef DUK_USE_MS_STRINGTABLE_RESIZE +#else +#define DUK_USE_MS_STRINGTABLE_RESIZE +#endif + +#if defined(DUK_OPT_NONSTD_ARRAY_CONCAT_TRAILER) +#define DUK_USE_NONSTD_ARRAY_CONCAT_TRAILER +#elif defined(DUK_OPT_NO_NONSTD_ARRAY_CONCAT_TRAILER) +#undef DUK_USE_NONSTD_ARRAY_CONCAT_TRAILER +#else +#define DUK_USE_NONSTD_ARRAY_CONCAT_TRAILER +#endif + +#if defined(DUK_OPT_NONSTD_ARRAY_MAP_TRAILER) +#define DUK_USE_NONSTD_ARRAY_MAP_TRAILER +#elif defined(DUK_OPT_NO_NONSTD_ARRAY_MAP_TRAILER) +#undef DUK_USE_NONSTD_ARRAY_MAP_TRAILER +#else +#define DUK_USE_NONSTD_ARRAY_MAP_TRAILER +#endif + +#if defined(DUK_OPT_NONSTD_ARRAY_SPLICE_DELCOUNT) +#define DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT +#elif defined(DUK_OPT_NO_NONSTD_ARRAY_SPLICE_DELCOUNT) +#undef DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT +#else +#define DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT +#endif + +#if defined(DUK_OPT_NONSTD_FUNC_CALLER_PROPERTY) +#define DUK_USE_NONSTD_FUNC_CALLER_PROPERTY +#elif defined(DUK_OPT_NO_NONSTD_FUNC_CALLER_PROPERTY) +#undef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY +#else +#undef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY +#endif + +#if defined(DUK_OPT_NONSTD_FUNC_SOURCE_PROPERTY) +#define DUK_USE_NONSTD_FUNC_SOURCE_PROPERTY +#elif defined(DUK_OPT_NO_NONSTD_FUNC_SOURCE_PROPERTY) +#undef DUK_USE_NONSTD_FUNC_SOURCE_PROPERTY +#else +#undef DUK_USE_NONSTD_FUNC_SOURCE_PROPERTY +#endif + +#if defined(DUK_OPT_NONSTD_FUNC_STMT) +#define DUK_USE_NONSTD_FUNC_STMT +#elif defined(DUK_OPT_NO_NONSTD_FUNC_STMT) +#undef DUK_USE_NONSTD_FUNC_STMT +#else +#define DUK_USE_NONSTD_FUNC_STMT +#endif + +#if defined(DUK_OPT_NONSTD_ACCESSOR_KEY_ARGUMENT) +#define DUK_USE_NONSTD_GETTER_KEY_ARGUMENT +#elif defined(DUK_OPT_NO_NONSTD_ACCESSOR_KEY_ARGUMENT) +#undef DUK_USE_NONSTD_GETTER_KEY_ARGUMENT +#else +#define DUK_USE_NONSTD_GETTER_KEY_ARGUMENT +#endif + +#if defined(DUK_OPT_NONSTD_JSON_ESC_U2028_U2029) +#define DUK_USE_NONSTD_JSON_ESC_U2028_U2029 +#elif defined(DUK_OPT_NO_NONSTD_JSON_ESC_U2028_U2029) +#undef DUK_USE_NONSTD_JSON_ESC_U2028_U2029 +#else +#define DUK_USE_NONSTD_JSON_ESC_U2028_U2029 +#endif + +#if defined(DUK_OPT_NONSTD_REGEXP_DOLLAR_ESCAPE) +#define DUK_USE_NONSTD_REGEXP_DOLLAR_ESCAPE +#elif defined(DUK_OPT_NO_NONSTD_REGEXP_DOLLAR_ESCAPE) +#undef DUK_USE_NONSTD_REGEXP_DOLLAR_ESCAPE +#else +#define DUK_USE_NONSTD_REGEXP_DOLLAR_ESCAPE +#endif + +#if defined(DUK_OPT_NONSTD_ACCESSOR_KEY_ARGUMENT) +#define DUK_USE_NONSTD_SETTER_KEY_ARGUMENT +#elif defined(DUK_OPT_NO_NONSTD_ACCESSOR_KEY_ARGUMENT) +#undef DUK_USE_NONSTD_SETTER_KEY_ARGUMENT +#else +#define DUK_USE_NONSTD_SETTER_KEY_ARGUMENT +#endif + +#if defined(DUK_OPT_NONSTD_STRING_FROMCHARCODE_32BIT) +#define DUK_USE_NONSTD_STRING_FROMCHARCODE_32BIT +#elif defined(DUK_OPT_NO_NONSTD_STRING_FROMCHARCODE_32BIT) +#undef DUK_USE_NONSTD_STRING_FROMCHARCODE_32BIT +#else +#define DUK_USE_NONSTD_STRING_FROMCHARCODE_32BIT +#endif + +#if defined(DUK_OPT_OBJSIZES16) +#define DUK_USE_OBJSIZES16 +#elif defined(DUK_OPT_NO_OBJSIZES16) +#undef DUK_USE_OBJSIZES16 +#else +#undef DUK_USE_OBJSIZES16 +#endif + +#if defined(DUK_OPT_OCTAL_SUPPORT) +#define DUK_USE_OCTAL_SUPPORT +#elif defined(DUK_OPT_NO_OCTAL_SUPPORT) +#undef DUK_USE_OCTAL_SUPPORT +#else +#define DUK_USE_OCTAL_SUPPORT +#endif + +#if defined(DUK_OPT_PACKED_TVAL) +#define DUK_USE_PACKED_TVAL +#elif defined(DUK_OPT_NO_PACKED_TVAL) +#undef DUK_USE_PACKED_TVAL +#else +/* Already provided above */ +#endif + +#undef DUK_USE_PANIC_ABORT +#if !defined(DUK_OPT_SEGFAULT_ON_PANIC) +#define DUK_USE_PANIC_ABORT +#endif + +#undef DUK_USE_PANIC_HANDLER +#if defined(DUK_OPT_PANIC_HANDLER) +#define DUK_USE_PANIC_HANDLER(code,msg) DUK_OPT_PANIC_HANDLER((code),(msg)) +#endif + +#undef DUK_USE_PANIC_SEGFAULT +#if defined(DUK_OPT_SEGFAULT_ON_PANIC) +#define DUK_USE_PANIC_SEGFAULT +#endif + +#if defined(DUK_OPT_PARANOID_ERRORS) +#define DUK_USE_PARANOID_ERRORS +#elif defined(DUK_OPT_NO_PARANOID_ERRORS) +#undef DUK_USE_PARANOID_ERRORS +#else +#undef DUK_USE_PARANOID_ERRORS +#endif + +#if defined(DUK_OPT_PC2LINE) +#define DUK_USE_PC2LINE +#elif defined(DUK_OPT_NO_PC2LINE) +#undef DUK_USE_PC2LINE +#else +#define DUK_USE_PC2LINE +#endif + +#if defined(DUK_OPT_REFCOUNT16) +#define DUK_USE_REFCOUNT16 +#elif defined(DUK_OPT_NO_REFCOUNT16) +#undef DUK_USE_REFCOUNT16 +#else +#undef DUK_USE_REFCOUNT16 +#endif + +#if defined(DUK_OPT_REFERENCE_COUNTING) +#define DUK_USE_REFERENCE_COUNTING +#elif defined(DUK_OPT_NO_REFERENCE_COUNTING) +#undef DUK_USE_REFERENCE_COUNTING +#else +#define DUK_USE_REFERENCE_COUNTING +#endif + +#if defined(DUK_OPT_REGEXP_CANON_WORKAROUND) +#define DUK_USE_REGEXP_CANON_WORKAROUND +#elif defined(DUK_OPT_NO_REGEXP_CANON_WORKAROUND) +#undef DUK_USE_REGEXP_CANON_WORKAROUND +#else +#undef DUK_USE_REGEXP_CANON_WORKAROUND +#endif + +#if defined(DUK_OPT_REGEXP_SUPPORT) +#define DUK_USE_REGEXP_SUPPORT +#elif defined(DUK_OPT_NO_REGEXP_SUPPORT) +#undef DUK_USE_REGEXP_SUPPORT +#else +#define DUK_USE_REGEXP_SUPPORT +#endif + +#if defined(DUK_OPT_ROM_GLOBAL_CLONE) +#define DUK_USE_ROM_GLOBAL_CLONE +#elif defined(DUK_OPT_NO_ROM_GLOBAL_CLONE) +#undef DUK_USE_ROM_GLOBAL_CLONE +#else +#undef DUK_USE_ROM_GLOBAL_CLONE +#endif + +#if defined(DUK_OPT_ROM_GLOBAL_INHERIT) +#define DUK_USE_ROM_GLOBAL_INHERIT +#elif defined(DUK_OPT_NO_ROM_GLOBAL_INHERIT) +#undef DUK_USE_ROM_GLOBAL_INHERIT +#else +#undef DUK_USE_ROM_GLOBAL_INHERIT +#endif + +#if defined(DUK_OPT_ROM_OBJECTS) +#define DUK_USE_ROM_OBJECTS +#elif defined(DUK_OPT_NO_ROM_OBJECTS) +#undef DUK_USE_ROM_OBJECTS +#else +#undef DUK_USE_ROM_OBJECTS +#endif + +#if defined(DUK_OPT_ROM_STRINGS) +#define DUK_USE_ROM_STRINGS +#elif defined(DUK_OPT_NO_ROM_STRINGS) +#undef DUK_USE_ROM_STRINGS +#else +#undef DUK_USE_ROM_STRINGS +#endif + +#if defined(DUK_OPT_SECTION_B) +#define DUK_USE_SECTION_B +#elif defined(DUK_OPT_NO_SECTION_B) +#undef DUK_USE_SECTION_B +#else +#define DUK_USE_SECTION_B +#endif + +#if defined(DUK_OPT_SELF_TESTS) +#define DUK_USE_SELF_TESTS +#elif defined(DUK_OPT_NO_SELF_TESTS) +#undef DUK_USE_SELF_TESTS +#else +#undef DUK_USE_SELF_TESTS +#endif + +#if defined(DUK_OPT_SHUFFLE_TORTURE) +#define DUK_USE_SHUFFLE_TORTURE +#elif defined(DUK_OPT_NO_SHUFFLE_TORTURE) +#undef DUK_USE_SHUFFLE_TORTURE +#else +#undef DUK_USE_SHUFFLE_TORTURE +#endif + +#if defined(DUK_OPT_SOURCE_NONBMP) +#define DUK_USE_SOURCE_NONBMP +#elif defined(DUK_OPT_NO_SOURCE_NONBMP) +#undef DUK_USE_SOURCE_NONBMP +#else +#define DUK_USE_SOURCE_NONBMP +#endif + +#if defined(DUK_OPT_STRHASH16) +#define DUK_USE_STRHASH16 +#elif defined(DUK_OPT_NO_STRHASH16) +#undef DUK_USE_STRHASH16 +#else +#undef DUK_USE_STRHASH16 +#endif + +#if defined(DUK_OPT_STRICT_DECL) +#define DUK_USE_STRICT_DECL +#elif defined(DUK_OPT_NO_STRICT_DECL) +#undef DUK_USE_STRICT_DECL +#else +#define DUK_USE_STRICT_DECL +#endif + +#if defined(DUK_OPT_STRICT_UTF8_SOURCE) +#define DUK_USE_STRICT_UTF8_SOURCE +#elif defined(DUK_OPT_NO_STRICT_UTF8_SOURCE) +#undef DUK_USE_STRICT_UTF8_SOURCE +#else +#undef DUK_USE_STRICT_UTF8_SOURCE +#endif + +#if defined(DUK_OPT_STRLEN16) +#define DUK_USE_STRLEN16 +#elif defined(DUK_OPT_NO_STRLEN16) +#undef DUK_USE_STRLEN16 +#else +#undef DUK_USE_STRLEN16 +#endif + +#undef DUK_USE_STRTAB_CHAIN +#if defined(DUK_OPT_STRTAB_CHAIN) && defined(DUK_OPT_STRTAB_CHAIN_SIZE) +#define DUK_USE_STRTAB_CHAIN +#endif + +#undef DUK_USE_STRTAB_CHAIN_SIZE +#if defined(DUK_OPT_STRTAB_CHAIN) && defined(DUK_OPT_STRTAB_CHAIN_SIZE) +/* Low memory algorithm: separate chaining using arrays, fixed size hash */ +#define DUK_USE_STRTAB_CHAIN_SIZE DUK_OPT_STRTAB_CHAIN_SIZE +#endif + +#undef DUK_USE_STRTAB_PROBE +#if !(defined(DUK_OPT_STRTAB_CHAIN) && defined(DUK_OPT_STRTAB_CHAIN_SIZE)) +#define DUK_USE_STRTAB_PROBE +#endif + +#if defined(DUK_OPT_NONSTD_FUNC_CALLER_PROPERTY) +#undef DUK_USE_TAILCALL +#else +#define DUK_USE_TAILCALL +#endif + +#if defined(DUK_OPT_TARGET_INFO) +#define DUK_USE_TARGET_INFO DUK_OPT_TARGET_INFO +#else +#define DUK_USE_TARGET_INFO "unknown" +#endif + +#if defined(DUK_OPT_NO_AUGMENT_ERRORS) +#undef DUK_USE_TRACEBACKS +#elif defined(DUK_OPT_NO_TRACEBACKS) +#undef DUK_USE_TRACEBACKS +#else +#define DUK_USE_TRACEBACKS +#endif + +#if defined(DUK_OPT_TRACEBACK_DEPTH) +#define DUK_USE_TRACEBACK_DEPTH DUK_OPT_TRACEBACK_DEPTH +#else +#define DUK_USE_TRACEBACK_DEPTH 10 +#endif + +#if defined(DUK_OPT_DECLARE) +#define DUK_USE_USER_DECLARE() DUK_OPT_DECLARE +#else +#define DUK_USE_USER_DECLARE() /* no user declarations */ +#endif + +/* User provided InitJS. */ +#undef DUK_USE_USER_INITJS +#if defined(DUK_OPT_USER_INITJS) +#define DUK_USE_USER_INITJS (DUK_OPT_USER_INITJS) +#endif + +#if defined(DUK_OPT_VERBOSE_ERRORS) +#define DUK_USE_VERBOSE_ERRORS +#elif defined(DUK_OPT_NO_VERBOSE_ERRORS) +#undef DUK_USE_VERBOSE_ERRORS +#else +#define DUK_USE_VERBOSE_ERRORS +#endif + +#if defined(DUK_OPT_VOLUNTARY_GC) +#define DUK_USE_VOLUNTARY_GC +#elif defined(DUK_OPT_NO_VOLUNTARY_GC) +#undef DUK_USE_VOLUNTARY_GC +#else +#define DUK_USE_VOLUNTARY_GC +#endif + +#if defined(DUK_OPT_ZERO_BUFFER_DATA) +#define DUK_USE_ZERO_BUFFER_DATA +#elif defined(DUK_OPT_NO_ZERO_BUFFER_DATA) +#undef DUK_USE_ZERO_BUFFER_DATA +#else +#define DUK_USE_ZERO_BUFFER_DATA +#endif + +/* + * Autogenerated defaults + */ + +#define DUK_USE_AVOID_PLATFORM_FUNCPTRS +#define DUK_USE_BASE64_FASTPATH +#define DUK_USE_BUILTIN_INITJS +#define DUK_USE_COMPILER_RECLIMIT 2500 +#undef DUK_USE_DATE_FORMAT_STRING +#undef DUK_USE_DATE_GET_LOCAL_TZOFFSET +#undef DUK_USE_DATE_GET_NOW +#undef DUK_USE_DATE_PARSE_STRING +#undef DUK_USE_DATE_PRS_GETDATE +#define DUK_USE_ESBC_LIMITS +#define DUK_USE_ESBC_MAX_BYTES 2147418112L +#define DUK_USE_ESBC_MAX_LINENUMBER 2147418112L +#undef DUK_USE_EXEC_FUN_LOCAL +#undef DUK_USE_EXPLICIT_NULL_INIT +#define DUK_USE_FAST_REFCOUNT_DEFAULT +#define DUK_USE_HEX_FASTPATH +#define DUK_USE_IDCHAR_FASTPATH +#undef DUK_USE_INTERRUPT_DEBUG_FIXUP +#define DUK_USE_JSON_DECNUMBER_FASTPATH +#define DUK_USE_JSON_DECSTRING_FASTPATH +#define DUK_USE_JSON_DEC_RECLIMIT 1000 +#define DUK_USE_JSON_EATWHITE_FASTPATH +#define DUK_USE_JSON_ENC_RECLIMIT 1000 +#define DUK_USE_JSON_QUOTESTRING_FASTPATH +#define DUK_USE_LEXER_SLIDING_WINDOW +#undef DUK_USE_MARKANDSWEEP_FINALIZER_TORTURE +#define DUK_USE_MARK_AND_SWEEP_RECLIMIT 256 +#define DUK_USE_MATH_BUILTIN +#define DUK_USE_NATIVE_CALL_RECLIMIT 1000 +#undef DUK_USE_PANIC_EXIT +#undef DUK_USE_PREFER_SIZE +#define DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS +#undef DUK_USE_REFZERO_FINALIZER_TORTURE +#define DUK_USE_REGEXP_COMPILER_RECLIMIT 10000 +#define DUK_USE_REGEXP_EXECUTOR_RECLIMIT 10000 +#define DUK_USE_ROM_PTRCOMP_FIRST 63488L +#undef DUK_USE_STRHASH_DENSE +#define DUK_USE_STRHASH_SKIP_SHIFT 5 +#undef DUK_USE_VALSTACK_UNSAFE +#define DUK_USE_VERBOSE_EXECUTOR_ERRORS + +/* + * Alternative customization header + * + * If you want to modify the final DUK_USE_xxx flags directly (without + * using the available DUK_OPT_xxx flags), define DUK_OPT_HAVE_CUSTOM_H + * and tweak the final flags there. + */ + +#if defined(DUK_OPT_HAVE_CUSTOM_H) +#include "duk_custom.h" +#endif + +/* + * You may add overriding #define/#undef directives below for + * customization. You of course cannot un-#include or un-typedef + * anything; these require direct changes above. + */ + +/* __OVERRIDE_DEFINES__ */ + +/* + * Date provider selection + * + * User may define DUK_USE_DATE_GET_NOW() etc directly, in which case we'll + * rely on an external provider. If this is not done, revert to previous + * behavior and use Unix/Windows built-in provider. + */ + +#if defined(DUK_COMPILING_DUKTAPE) + +#if defined(DUK_USE_DATE_GET_NOW) +/* External provider already defined. */ +#elif defined(DUK_USE_DATE_NOW_GETTIMEOFDAY) +#define DUK_USE_DATE_GET_NOW(ctx) duk_bi_date_get_now_gettimeofday((ctx)) +#elif defined(DUK_USE_DATE_NOW_TIME) +#define DUK_USE_DATE_GET_NOW(ctx) duk_bi_date_get_now_time((ctx)) +#elif defined(DUK_USE_DATE_NOW_WINDOWS) +#define DUK_USE_DATE_GET_NOW(ctx) duk_bi_date_get_now_windows((ctx)) +#else +#error no provider for DUK_USE_DATE_GET_NOW() +#endif + +#if defined(DUK_USE_DATE_GET_LOCAL_TZOFFSET) +/* External provider already defined. */ +#elif defined(DUK_USE_DATE_TZO_GMTIME_R) || defined(DUK_USE_DATE_TZO_GMTIME) +#define DUK_USE_DATE_GET_LOCAL_TZOFFSET(d) duk_bi_date_get_local_tzoffset_gmtime((d)) +#elif defined(DUK_USE_DATE_TZO_WINDOWS) +#define DUK_USE_DATE_GET_LOCAL_TZOFFSET(d) duk_bi_date_get_local_tzoffset_windows((d)) +#else +#error no provider for DUK_USE_DATE_GET_LOCAL_TZOFFSET() +#endif + +#if defined(DUK_USE_DATE_PARSE_STRING) +/* External provider already defined. */ +#elif defined(DUK_USE_DATE_PRS_STRPTIME) +#define DUK_USE_DATE_PARSE_STRING(ctx,str) duk_bi_date_parse_string_strptime((ctx), (str)) +#elif defined(DUK_USE_DATE_PRS_GETDATE) +#define DUK_USE_DATE_PARSE_STRING(ctx,str) duk_bi_date_parse_string_getdate((ctx), (str)) +#else +/* No provider for DUK_USE_DATE_PARSE_STRING(), fall back to ISO 8601 only. */ +#endif + +#if defined(DUK_USE_DATE_FORMAT_STRING) +/* External provider already defined. */ +#elif defined(DUK_USE_DATE_FMT_STRFTIME) +#define DUK_USE_DATE_FORMAT_STRING(ctx,parts,tzoffset,flags) \ + duk_bi_date_format_parts_strftime((ctx), (parts), (tzoffset), (flags)) +#else +/* No provider for DUK_USE_DATE_FORMAT_STRING(), fall back to ISO 8601 only. */ +#endif + +#endif /* DUK_COMPILING_DUKTAPE */ + +/* + * Checks for config option consistency (DUK_USE_xxx) + */ + +#if defined(DUK_USE_32BIT_PTRS) +#error unsupported config option used (option has been removed): DUK_USE_32BIT_PTRS +#endif +#if defined(DUK_USE_ALIGN_4) +#error unsupported config option used (option has been removed): DUK_USE_ALIGN_4 +#endif +#if defined(DUK_USE_ALIGN_8) +#error unsupported config option used (option has been removed): DUK_USE_ALIGN_8 +#endif +#if defined(DUK_USE_BYTEORDER_FORCED) +#error unsupported config option used (option has been removed): DUK_USE_BYTEORDER_FORCED +#endif +#if defined(DUK_USE_DATAPTR_DEC16) && !defined(DUK_USE_DATAPTR16) +#error config option DUK_USE_DATAPTR_DEC16 requires option DUK_USE_DATAPTR16 (which is missing) +#endif +#if defined(DUK_USE_DATAPTR_ENC16) && !defined(DUK_USE_DATAPTR16) +#error config option DUK_USE_DATAPTR_ENC16 requires option DUK_USE_DATAPTR16 (which is missing) +#endif +#if defined(DUK_USE_DEBUGGER_SUPPORT) && !defined(DUK_USE_INTERRUPT_COUNTER) +#error config option DUK_USE_DEBUGGER_SUPPORT requires option DUK_USE_INTERRUPT_COUNTER (which is missing) +#endif +#if defined(DUK_USE_DEEP_C_STACK) +#error unsupported config option used (option has been removed): DUK_USE_DEEP_C_STACK +#endif +#if defined(DUK_USE_DOUBLE_BE) +#error unsupported config option used (option has been removed): DUK_USE_DOUBLE_BE +#endif +#if defined(DUK_USE_DOUBLE_BE) && defined(DUK_USE_DOUBLE_LE) +#error config option DUK_USE_DOUBLE_BE conflicts with option DUK_USE_DOUBLE_LE (which is also defined) +#endif +#if defined(DUK_USE_DOUBLE_BE) && defined(DUK_USE_DOUBLE_ME) +#error config option DUK_USE_DOUBLE_BE conflicts with option DUK_USE_DOUBLE_ME (which is also defined) +#endif +#if defined(DUK_USE_DOUBLE_LE) +#error unsupported config option used (option has been removed): DUK_USE_DOUBLE_LE +#endif +#if defined(DUK_USE_DOUBLE_LE) && defined(DUK_USE_DOUBLE_BE) +#error config option DUK_USE_DOUBLE_LE conflicts with option DUK_USE_DOUBLE_BE (which is also defined) +#endif +#if defined(DUK_USE_DOUBLE_LE) && defined(DUK_USE_DOUBLE_ME) +#error config option DUK_USE_DOUBLE_LE conflicts with option DUK_USE_DOUBLE_ME (which is also defined) +#endif +#if defined(DUK_USE_DOUBLE_ME) +#error unsupported config option used (option has been removed): DUK_USE_DOUBLE_ME +#endif +#if defined(DUK_USE_DOUBLE_ME) && defined(DUK_USE_DOUBLE_LE) +#error config option DUK_USE_DOUBLE_ME conflicts with option DUK_USE_DOUBLE_LE (which is also defined) +#endif +#if defined(DUK_USE_DOUBLE_ME) && defined(DUK_USE_DOUBLE_BE) +#error config option DUK_USE_DOUBLE_ME conflicts with option DUK_USE_DOUBLE_BE (which is also defined) +#endif +#if defined(DUK_USE_DPRINT) && !defined(DUK_USE_DEBUG) +#error config option DUK_USE_DPRINT requires option DUK_USE_DEBUG (which is missing) +#endif +#if defined(DUK_USE_ESBC_MAX_BYTES) && !defined(DUK_USE_ESBC_LIMITS) +#error config option DUK_USE_ESBC_MAX_BYTES requires option DUK_USE_ESBC_LIMITS (which is missing) +#endif +#if defined(DUK_USE_ESBC_MAX_LINENUMBER) && !defined(DUK_USE_ESBC_LIMITS) +#error config option DUK_USE_ESBC_MAX_LINENUMBER requires option DUK_USE_ESBC_LIMITS (which is missing) +#endif +#if defined(DUK_USE_EXEC_TIMEOUT_CHECK) && !defined(DUK_USE_INTERRUPT_COUNTER) +#error config option DUK_USE_EXEC_TIMEOUT_CHECK requires option DUK_USE_INTERRUPT_COUNTER (which is missing) +#endif +#if defined(DUK_USE_EXTSTR_FREE) && !defined(DUK_USE_HSTRING_EXTDATA) +#error config option DUK_USE_EXTSTR_FREE requires option DUK_USE_HSTRING_EXTDATA (which is missing) +#endif +#if defined(DUK_USE_EXTSTR_INTERN_CHECK) && !defined(DUK_USE_HSTRING_EXTDATA) +#error config option DUK_USE_EXTSTR_INTERN_CHECK requires option DUK_USE_HSTRING_EXTDATA (which is missing) +#endif +#if defined(DUK_USE_FULL_TVAL) +#error unsupported config option used (option has been removed): DUK_USE_FULL_TVAL +#endif +#if defined(DUK_USE_FUNCPTR_DEC16) && !defined(DUK_USE_FUNCPTR16) +#error config option DUK_USE_FUNCPTR_DEC16 requires option DUK_USE_FUNCPTR16 (which is missing) +#endif +#if defined(DUK_USE_FUNCPTR_ENC16) && !defined(DUK_USE_FUNCPTR16) +#error config option DUK_USE_FUNCPTR_ENC16 requires option DUK_USE_FUNCPTR16 (which is missing) +#endif +#if defined(DUK_USE_HASHBYTES_UNALIGNED_U32_ACCESS) +#error unsupported config option used (option has been removed): DUK_USE_HASHBYTES_UNALIGNED_U32_ACCESS +#endif +#if defined(DUK_USE_HEAPPTR16) && defined(DUK_USE_DEBUG) +#error config option DUK_USE_HEAPPTR16 conflicts with option DUK_USE_DEBUG (which is also defined) +#endif +#if defined(DUK_USE_HEAPPTR_DEC16) && !defined(DUK_USE_HEAPPTR16) +#error config option DUK_USE_HEAPPTR_DEC16 requires option DUK_USE_HEAPPTR16 (which is missing) +#endif +#if defined(DUK_USE_HEAPPTR_ENC16) && !defined(DUK_USE_HEAPPTR16) +#error config option DUK_USE_HEAPPTR_ENC16 requires option DUK_USE_HEAPPTR16 (which is missing) +#endif +#if defined(DUK_USE_INTEGER_BE) +#error unsupported config option used (option has been removed): DUK_USE_INTEGER_BE +#endif +#if defined(DUK_USE_INTEGER_BE) && defined(DUK_USE_INTEGER_LE) +#error config option DUK_USE_INTEGER_BE conflicts with option DUK_USE_INTEGER_LE (which is also defined) +#endif +#if defined(DUK_USE_INTEGER_BE) && defined(DUK_USE_INTEGER_ME) +#error config option DUK_USE_INTEGER_BE conflicts with option DUK_USE_INTEGER_ME (which is also defined) +#endif +#if defined(DUK_USE_INTEGER_LE) +#error unsupported config option used (option has been removed): DUK_USE_INTEGER_LE +#endif +#if defined(DUK_USE_INTEGER_LE) && defined(DUK_USE_INTEGER_BE) +#error config option DUK_USE_INTEGER_LE conflicts with option DUK_USE_INTEGER_BE (which is also defined) +#endif +#if defined(DUK_USE_INTEGER_LE) && defined(DUK_USE_INTEGER_ME) +#error config option DUK_USE_INTEGER_LE conflicts with option DUK_USE_INTEGER_ME (which is also defined) +#endif +#if defined(DUK_USE_INTEGER_ME) +#error unsupported config option used (option has been removed): DUK_USE_INTEGER_ME +#endif +#if defined(DUK_USE_INTEGER_ME) && defined(DUK_USE_INTEGER_LE) +#error config option DUK_USE_INTEGER_ME conflicts with option DUK_USE_INTEGER_LE (which is also defined) +#endif +#if defined(DUK_USE_INTEGER_ME) && defined(DUK_USE_INTEGER_BE) +#error config option DUK_USE_INTEGER_ME conflicts with option DUK_USE_INTEGER_BE (which is also defined) +#endif +#if defined(DUK_USE_NO_DOUBLE_ALIASING_SELFTEST) +#error unsupported config option used (option has been removed): DUK_USE_NO_DOUBLE_ALIASING_SELFTEST +#endif +#if defined(DUK_USE_PACKED_TVAL_POSSIBLE) +#error unsupported config option used (option has been removed): DUK_USE_PACKED_TVAL_POSSIBLE +#endif +#if defined(DUK_USE_RDTSC) +#error unsupported config option used (option has been removed): DUK_USE_RDTSC +#endif +#if defined(DUK_USE_ROM_GLOBAL_CLONE) && !defined(DUK_USE_ROM_STRINGS) +#error config option DUK_USE_ROM_GLOBAL_CLONE requires option DUK_USE_ROM_STRINGS (which is missing) +#endif +#if defined(DUK_USE_ROM_GLOBAL_CLONE) && !defined(DUK_USE_ROM_OBJECTS) +#error config option DUK_USE_ROM_GLOBAL_CLONE requires option DUK_USE_ROM_OBJECTS (which is missing) +#endif +#if defined(DUK_USE_ROM_GLOBAL_CLONE) && defined(DUK_USE_ROM_GLOBAL_INHERIT) +#error config option DUK_USE_ROM_GLOBAL_CLONE conflicts with option DUK_USE_ROM_GLOBAL_INHERIT (which is also defined) +#endif +#if defined(DUK_USE_ROM_GLOBAL_INHERIT) && !defined(DUK_USE_ROM_STRINGS) +#error config option DUK_USE_ROM_GLOBAL_INHERIT requires option DUK_USE_ROM_STRINGS (which is missing) +#endif +#if defined(DUK_USE_ROM_GLOBAL_INHERIT) && !defined(DUK_USE_ROM_OBJECTS) +#error config option DUK_USE_ROM_GLOBAL_INHERIT requires option DUK_USE_ROM_OBJECTS (which is missing) +#endif +#if defined(DUK_USE_ROM_GLOBAL_INHERIT) && defined(DUK_USE_ROM_GLOBAL_CLONE) +#error config option DUK_USE_ROM_GLOBAL_INHERIT conflicts with option DUK_USE_ROM_GLOBAL_CLONE (which is also defined) +#endif +#if defined(DUK_USE_ROM_OBJECTS) && !defined(DUK_USE_ROM_STRINGS) +#error config option DUK_USE_ROM_OBJECTS requires option DUK_USE_ROM_STRINGS (which is missing) +#endif +#if defined(DUK_USE_ROM_STRINGS) && !defined(DUK_USE_ROM_OBJECTS) +#error config option DUK_USE_ROM_STRINGS requires option DUK_USE_ROM_OBJECTS (which is missing) +#endif +#if defined(DUK_USE_SETJMP) +#error unsupported config option used (option has been removed): DUK_USE_SETJMP +#endif +#if defined(DUK_USE_SIGSETJMP) +#error unsupported config option used (option has been removed): DUK_USE_SIGSETJMP +#endif +#if defined(DUK_USE_STRTAB_CHAIN_SIZE) && !defined(DUK_USE_STRTAB_CHAIN) +#error config option DUK_USE_STRTAB_CHAIN_SIZE requires option DUK_USE_STRTAB_CHAIN (which is missing) +#endif +#if defined(DUK_USE_TAILCALL) && defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY) +#error config option DUK_USE_TAILCALL conflicts with option DUK_USE_NONSTD_FUNC_CALLER_PROPERTY (which is also defined) +#endif +#if defined(DUK_USE_UNALIGNED_ACCESSES_POSSIBLE) +#error unsupported config option used (option has been removed): DUK_USE_UNALIGNED_ACCESSES_POSSIBLE +#endif +#if defined(DUK_USE_UNDERSCORE_SETJMP) +#error unsupported config option used (option has been removed): DUK_USE_UNDERSCORE_SETJMP +#endif + +#if defined(DUK_USE_CPP_EXCEPTIONS) && !defined(__cplusplus) +#error DUK_USE_CPP_EXCEPTIONS enabled but not compiling with a C++ compiler +#endif + +/* + * Convert DUK_USE_BYTEORDER, from whatever source, into currently used + * internal defines. If detection failed, #error out. + */ + +#if defined(DUK_USE_BYTEORDER) +#if (DUK_USE_BYTEORDER == 1) +#define DUK_USE_INTEGER_LE +#define DUK_USE_DOUBLE_LE +#elif (DUK_USE_BYTEORDER == 2) +#define DUK_USE_INTEGER_LE /* integer endianness is little on purpose */ +#define DUK_USE_DOUBLE_ME +#elif (DUK_USE_BYTEORDER == 3) +#define DUK_USE_INTEGER_BE +#define DUK_USE_DOUBLE_BE +#else +#error unsupported: byte order invalid +#endif /* byte order */ +#else +#error unsupported: byte order detection failed +#endif /* defined(DUK_USE_BYTEORDER) */ + +#endif /* DUK_CONFIG_H_INCLUDED */ diff --git a/src/civetweb/src/third_party/duktape-1.5.2/src-noline/duktape.c b/src/civetweb/src/third_party/duktape-1.5.2/src-noline/duktape.c new file mode 100644 index 000000000..9a65f55a2 --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.5.2/src-noline/duktape.c @@ -0,0 +1,86570 @@ +/* + * Single source autogenerated distributable for Duktape 1.5.2. + * + * Git commit cad34ae155acb0846545ca6bf2d29f9463b22bbb (v1.5.2). + * Git branch HEAD. + * + * See Duktape AUTHORS.rst and LICENSE.txt for copyright and + * licensing information. + */ + +/* LICENSE.txt */ +/* +* =============== +* Duktape license +* =============== +* +* (http://opensource.org/licenses/MIT) +* +* Copyright (c) 2013-2016 by Duktape authors (see AUTHORS.rst) +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ +/* AUTHORS.rst */ +/* +* =============== +* Duktape authors +* =============== +* +* Copyright +* ========= +* +* Duktape copyrights are held by its authors. Each author has a copyright +* to their contribution, and agrees to irrevocably license the contribution +* under the Duktape ``LICENSE.txt``. +* +* Authors +* ======= +* +* Please include an e-mail address, a link to your GitHub profile, or something +* similar to allow your contribution to be identified accurately. +* +* The following people have contributed code, website contents, or Wiki contents, +* and agreed to irrevocably license their contributions under the Duktape +* ``LICENSE.txt`` (in order of appearance): +* +* * Sami Vaarala +* * Niki Dobrev +* * Andreas \u00d6man +* * L\u00e1szl\u00f3 Lang\u00f3 +* * Legimet +* * Karl Skomski +* * Bruce Pascoe +* * Ren\u00e9 Hollander +* * Julien Hamaide (https://github.com/crazyjul) +* * Sebastian G\u00f6tte (https://github.com/jaseg) +* +* Other contributions +* =================== +* +* The following people have contributed something other than code (e.g. reported +* bugs, provided ideas, etc; roughly in order of appearance): +* +* * Greg Burns +* * Anthony Rabine +* * Carlos Costa +* * Aur\u00e9lien Bouilland +* * Preet Desai (Pris Matic) +* * judofyr (http://www.reddit.com/user/judofyr) +* * Jason Woofenden +* * Micha\u0142 Przyby\u015b +* * Anthony Howe +* * Conrad Pankoff +* * Jim Schimpf +* * Rajaran Gaunker (https://github.com/zimbabao) +* * Andreas \u00d6man +* * Doug Sanden +* * Josh Engebretson (https://github.com/JoshEngebretson) +* * Remo Eichenberger (https://github.com/remoe) +* * Mamod Mehyar (https://github.com/mamod) +* * David Demelier (https://github.com/markand) +* * Tim Caswell (https://github.com/creationix) +* * Mitchell Blank Jr (https://github.com/mitchblank) +* * https://github.com/yushli +* * Seo Sanghyeon (https://github.com/sanxiyn) +* * Han ChoongWoo (https://github.com/tunz) +* * Joshua Peek (https://github.com/josh) +* * Bruce E. Pascoe (https://github.com/fatcerberus) +* * https://github.com/Kelledin +* * https://github.com/sstruchtrup +* * Michael Drake (https://github.com/tlsa) +* * https://github.com/chris-y +* * Laurent Zubiaur (https://github.com/lzubiaur) +* * Ole Andr\u00e9 Vadla Ravn\u00e5s (https://github.com/oleavr) +* +* If you are accidentally missing from this list, send me an e-mail +* (``sami.vaarala@iki.fi``) and I'll fix the omission. +*/ +/* + * Top-level include file to be used for all (internal) source files. + * + * Source files should not include individual header files, as they + * have not been designed to be individually included. + */ + +#ifndef DUK_INTERNAL_H_INCLUDED +#define DUK_INTERNAL_H_INCLUDED + +/* + * The 'duktape.h' header provides the public API, but also handles all + * compiler and platform specific feature detection, Duktape feature + * resolution, inclusion of system headers, etc. These have been merged + * because the public API is also dependent on e.g. detecting appropriate + * C types which is quite platform/compiler specific especially for a non-C99 + * build. The public API is also dependent on the resolved feature set. + * + * Some actions taken by the merged header (such as including system headers) + * are not appropriate for building a user application. The define + * DUK_COMPILING_DUKTAPE allows the merged header to skip/include some + * sections depending on what is being built. + */ + +#define DUK_COMPILING_DUKTAPE +#include "duktape.h" + +/* + * User declarations, e.g. prototypes for user functions used by Duktape + * macros. Concretely, if DUK_USE_PANIC_HANDLER is used and the macro + * value calls a user function, it needs to be declared for Duktape + * compilation to avoid warnings. + */ + +DUK_USE_USER_DECLARE() + +/* + * Duktape includes (other than duk_features.h) + * + * The header files expect to be included in an order which satisfies header + * dependencies correctly (the headers themselves don't include any other + * includes). Forward declarations are used to break circular struct/typedef + * dependencies. + */ + +#ifndef DUK_REPLACEMENTS_H_INCLUDED +#define DUK_REPLACEMENTS_H_INCLUDED + +#if !defined(DUK_SINGLE_FILE) +#if defined(DUK_USE_COMPUTED_INFINITY) +DUK_INTERNAL_DECL double duk_computed_infinity; +#endif +#if defined(DUK_USE_COMPUTED_NAN) +DUK_INTERNAL_DECL double duk_computed_nan; +#endif +#if defined(DUK_USE_REPL_FPCLASSIFY) +DUK_INTERNAL_DECL int duk_repl_fpclassify(double x); +#endif +#if defined(DUK_USE_REPL_SIGNBIT) +DUK_INTERNAL_DECL int duk_repl_signbit(double x); +#endif +#if defined(DUK_USE_REPL_ISFINITE) +DUK_INTERNAL_DECL int duk_repl_isfinite(double x); +#endif +#if defined(DUK_USE_REPL_ISNAN) +DUK_INTERNAL_DECL int duk_repl_isnan(double x); +#endif +#if defined(DUK_USE_REPL_ISINF) +DUK_INTERNAL_DECL int duk_repl_isinf(double x); +#endif +#endif /* !DUK_SINGLE_FILE */ + +#endif /* DUK_REPLACEMENTS_H_INCLUDED */ +/* + * Wrapper for jmp_buf. + * + * This is used because jmp_buf is an array type for backward compatibility. + * Wrapping jmp_buf in a struct makes pointer references, sizeof, etc, + * behave more intuitively. + * + * http://en.wikipedia.org/wiki/Setjmp.h#Member_types + */ + +#ifndef DUK_JMPBUF_H_INCLUDED +#define DUK_JMPBUF_H_INCLUDED + +#if defined(DUK_USE_CPP_EXCEPTIONS) +struct duk_jmpbuf { + duk_small_int_t dummy; /* unused */ +}; +#else +struct duk_jmpbuf { + DUK_JMPBUF_TYPE jb; +}; +#endif + +#endif /* DUK_JMPBUF_H_INCLUDED */ +/* + * Exception for Duktape internal throws when C++ exceptions are used + * for long control transfers. + * + * Doesn't inherit from any exception base class to minimize the chance + * that user code would accidentally catch this exception. + */ + +#ifndef DUK_EXCEPTION_H_INCLUDED +#define DUK_EXCEPTION_H_INCLUDED + +#if defined(DUK_USE_CPP_EXCEPTIONS) +class duk_internal_exception { + /* intentionally empty */ +}; +#endif + +#endif /* DUK_EXCEPTION_H_INCLUDED */ +/* + * Forward declarations for all Duktape structures. + */ + +#ifndef DUK_FORWDECL_H_INCLUDED +#define DUK_FORWDECL_H_INCLUDED + +/* + * Forward declarations + */ + +#if defined(DUK_USE_CPP_EXCEPTIONS) +class duk_internal_exception; +#else +struct duk_jmpbuf; +#endif + +/* duk_tval intentionally skipped */ +struct duk_heaphdr; +struct duk_heaphdr_string; +struct duk_hstring; +struct duk_hstring_external; +struct duk_hobject; +struct duk_hcompiledfunction; +struct duk_hnativefunction; +struct duk_hthread; +struct duk_hbufferobject; +struct duk_hbuffer; +struct duk_hbuffer_fixed; +struct duk_hbuffer_dynamic; +struct duk_hbuffer_external; + +struct duk_propaccessor; +union duk_propvalue; +struct duk_propdesc; + +struct duk_heap; +struct duk_breakpoint; + +struct duk_activation; +struct duk_catcher; +struct duk_strcache; +struct duk_ljstate; +struct duk_strtab_entry; + +#ifdef DUK_USE_DEBUG +struct duk_fixedbuffer; +#endif + +struct duk_bitdecoder_ctx; +struct duk_bitencoder_ctx; +struct duk_bufwriter_ctx; + +struct duk_token; +struct duk_re_token; +struct duk_lexer_point; +struct duk_lexer_ctx; +struct duk_lexer_codepoint; + +struct duk_compiler_instr; +struct duk_compiler_func; +struct duk_compiler_ctx; + +struct duk_re_matcher_ctx; +struct duk_re_compiler_ctx; + +#if defined(DUK_USE_CPP_EXCEPTIONS) +/* no typedef */ +#else +typedef struct duk_jmpbuf duk_jmpbuf; +#endif + +/* duk_tval intentionally skipped */ +typedef struct duk_heaphdr duk_heaphdr; +typedef struct duk_heaphdr_string duk_heaphdr_string; +typedef struct duk_hstring duk_hstring; +typedef struct duk_hstring_external duk_hstring_external; +typedef struct duk_hobject duk_hobject; +typedef struct duk_hcompiledfunction duk_hcompiledfunction; +typedef struct duk_hnativefunction duk_hnativefunction; +typedef struct duk_hbufferobject duk_hbufferobject; +typedef struct duk_hthread duk_hthread; +typedef struct duk_hbuffer duk_hbuffer; +typedef struct duk_hbuffer_fixed duk_hbuffer_fixed; +typedef struct duk_hbuffer_dynamic duk_hbuffer_dynamic; +typedef struct duk_hbuffer_external duk_hbuffer_external; + +typedef struct duk_propaccessor duk_propaccessor; +typedef union duk_propvalue duk_propvalue; +typedef struct duk_propdesc duk_propdesc; + +typedef struct duk_heap duk_heap; +typedef struct duk_breakpoint duk_breakpoint; + +typedef struct duk_activation duk_activation; +typedef struct duk_catcher duk_catcher; +typedef struct duk_strcache duk_strcache; +typedef struct duk_ljstate duk_ljstate; +typedef struct duk_strtab_entry duk_strtab_entry; + +#ifdef DUK_USE_DEBUG +typedef struct duk_fixedbuffer duk_fixedbuffer; +#endif + +typedef struct duk_bitdecoder_ctx duk_bitdecoder_ctx; +typedef struct duk_bitencoder_ctx duk_bitencoder_ctx; +typedef struct duk_bufwriter_ctx duk_bufwriter_ctx; + +typedef struct duk_token duk_token; +typedef struct duk_re_token duk_re_token; +typedef struct duk_lexer_point duk_lexer_point; +typedef struct duk_lexer_ctx duk_lexer_ctx; +typedef struct duk_lexer_codepoint duk_lexer_codepoint; + +typedef struct duk_compiler_instr duk_compiler_instr; +typedef struct duk_compiler_func duk_compiler_func; +typedef struct duk_compiler_ctx duk_compiler_ctx; + +typedef struct duk_re_matcher_ctx duk_re_matcher_ctx; +typedef struct duk_re_compiler_ctx duk_re_compiler_ctx; + +#endif /* DUK_FORWDECL_H_INCLUDED */ +/* + * Tagged type definition (duk_tval) and accessor macros. + * + * Access all fields through the accessor macros, as the representation + * is quite tricky. + * + * There are two packed type alternatives: an 8-byte representation + * based on an IEEE double (preferred for compactness), and a 12-byte + * representation (portability). The latter is needed also in e.g. + * 64-bit environments (it usually pads to 16 bytes per value). + * + * Selecting the tagged type format involves many trade-offs (memory + * use, size and performance of generated code, portability, etc), + * see doc/types.rst for a detailed discussion (especially of how the + * IEEE double format is used to pack tagged values). + * + * NB: because macro arguments are often expressions, macros should + * avoid evaluating their argument more than once. + */ + +#ifndef DUK_TVAL_H_INCLUDED +#define DUK_TVAL_H_INCLUDED + +/* sanity */ +#if !defined(DUK_USE_DOUBLE_LE) && !defined(DUK_USE_DOUBLE_ME) && !defined(DUK_USE_DOUBLE_BE) +#error unsupported: cannot determine byte order variant +#endif + +#if defined(DUK_USE_PACKED_TVAL) +/* ======================================================================== */ + +/* + * Packed 8-byte representation + */ + +/* use duk_double_union as duk_tval directly */ +typedef union duk_double_union duk_tval; + +/* tags */ +#define DUK_TAG_NORMALIZED_NAN 0x7ff8UL /* the NaN variant we use */ +/* avoid tag 0xfff0, no risk of confusion with negative infinity */ +#if defined(DUK_USE_FASTINT) +#define DUK_TAG_FASTINT 0xfff1UL /* embed: integer value */ +#endif +#define DUK_TAG_UNUSED 0xfff2UL /* marker; not actual tagged value */ +#define DUK_TAG_UNDEFINED 0xfff3UL /* embed: nothing */ +#define DUK_TAG_NULL 0xfff4UL /* embed: nothing */ +#define DUK_TAG_BOOLEAN 0xfff5UL /* embed: 0 or 1 (false or true) */ +/* DUK_TAG_NUMBER would logically go here, but it has multiple 'tags' */ +#define DUK_TAG_POINTER 0xfff6UL /* embed: void ptr */ +#define DUK_TAG_LIGHTFUNC 0xfff7UL /* embed: func ptr */ +#define DUK_TAG_STRING 0xfff8UL /* embed: duk_hstring ptr */ +#define DUK_TAG_OBJECT 0xfff9UL /* embed: duk_hobject ptr */ +#define DUK_TAG_BUFFER 0xfffaUL /* embed: duk_hbuffer ptr */ + +/* for convenience */ +#define DUK_XTAG_BOOLEAN_FALSE 0xfff50000UL +#define DUK_XTAG_BOOLEAN_TRUE 0xfff50001UL + +/* two casts to avoid gcc warning: "warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]" */ +#if defined(DUK_USE_64BIT_OPS) +#if defined(DUK_USE_DOUBLE_ME) +#define DUK__TVAL_SET_TAGGEDPOINTER(v,h,tag) do { \ + (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) (tag)) << 16) | (((duk_uint64_t) (duk_uint32_t) (h)) << 32); \ + } while (0) +#else +#define DUK__TVAL_SET_TAGGEDPOINTER(v,h,tag) do { \ + (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) (tag)) << 48) | ((duk_uint64_t) (duk_uint32_t) (h)); \ + } while (0) +#endif +#else /* DUK_USE_64BIT_OPS */ +#define DUK__TVAL_SET_TAGGEDPOINTER(v,h,tag) do { \ + (v)->ui[DUK_DBL_IDX_UI0] = ((duk_uint32_t) (tag)) << 16; \ + (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (h); \ + } while (0) +#endif /* DUK_USE_64BIT_OPS */ + +#if defined(DUK_USE_64BIT_OPS) +/* Double casting for pointer to avoid gcc warning (cast from pointer to integer of different size) */ +#if defined(DUK_USE_DOUBLE_ME) +#define DUK__TVAL_SET_LIGHTFUNC(v,fp,flags) do { \ + (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_LIGHTFUNC) << 16) | \ + ((duk_uint64_t) (flags)) | \ + (((duk_uint64_t) (duk_uint32_t) (fp)) << 32); \ + } while (0) +#else +#define DUK__TVAL_SET_LIGHTFUNC(v,fp,flags) do { \ + (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_LIGHTFUNC) << 48) | \ + (((duk_uint64_t) (flags)) << 32) | \ + ((duk_uint64_t) (duk_uint32_t) (fp)); \ + } while (0) +#endif +#else /* DUK_USE_64BIT_OPS */ +#define DUK__TVAL_SET_LIGHTFUNC(v,fp,flags) do { \ + (v)->ui[DUK_DBL_IDX_UI0] = (((duk_uint32_t) DUK_TAG_LIGHTFUNC) << 16) | ((duk_uint32_t) (flags)); \ + (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (fp); \ + } while (0) +#endif /* DUK_USE_64BIT_OPS */ + +#if defined(DUK_USE_FASTINT) +/* Note: masking is done for 'i' to deal with negative numbers correctly */ +#if defined(DUK_USE_DOUBLE_ME) +#define DUK__TVAL_SET_FASTINT(v,i) do { \ + (v)->ui[DUK_DBL_IDX_UI0] = ((duk_uint32_t) DUK_TAG_FASTINT) << 16 | (((duk_uint32_t) ((i) >> 32)) & 0x0000ffffUL); \ + (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (i); \ + } while (0) +#define DUK__TVAL_SET_FASTINT_U32(v,i) do { \ + (v)->ui[DUK_DBL_IDX_UI0] = ((duk_uint32_t) DUK_TAG_FASTINT) << 16; \ + (v)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (i); \ + } while (0) +#else +#define DUK__TVAL_SET_FASTINT(v,i) do { \ + (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_FASTINT) << 48) | (((duk_uint64_t) (i)) & 0x0000ffffffffffffULL); \ + } while (0) +#define DUK__TVAL_SET_FASTINT_U32(v,i) do { \ + (v)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_FASTINT) << 48) | (duk_uint64_t) (i); \ + } while (0) +#endif + +#define DUK__TVAL_SET_FASTINT_I32(v,i) do { \ + duk_int64_t duk__tmp = (duk_int64_t) (i); \ + DUK_TVAL_SET_FASTINT((v), duk__tmp); \ + } while (0) + +/* XXX: clumsy sign extend and masking of 16 topmost bits */ +#if defined(DUK_USE_DOUBLE_ME) +#define DUK__TVAL_GET_FASTINT(v) (((duk_int64_t) ((((duk_uint64_t) (v)->ui[DUK_DBL_IDX_UI0]) << 32) | ((duk_uint64_t) (v)->ui[DUK_DBL_IDX_UI1]))) << 16 >> 16) +#else +#define DUK__TVAL_GET_FASTINT(v) ((((duk_int64_t) (v)->ull[DUK_DBL_IDX_ULL0]) << 16) >> 16) +#endif +#define DUK__TVAL_GET_FASTINT_U32(v) ((v)->ui[DUK_DBL_IDX_UI1]) +#define DUK__TVAL_GET_FASTINT_I32(v) ((duk_int32_t) (v)->ui[DUK_DBL_IDX_UI1]) +#endif /* DUK_USE_FASTINT */ + +#define DUK_TVAL_SET_UNDEFINED(v) do { \ + (v)->us[DUK_DBL_IDX_US0] = (duk_uint16_t) DUK_TAG_UNDEFINED; \ + } while (0) +#define DUK_TVAL_SET_UNUSED(v) do { \ + (v)->us[DUK_DBL_IDX_US0] = (duk_uint16_t) DUK_TAG_UNUSED; \ + } while (0) +#define DUK_TVAL_SET_NULL(v) do { \ + (v)->us[DUK_DBL_IDX_US0] = (duk_uint16_t) DUK_TAG_NULL; \ + } while (0) + +#define DUK_TVAL_SET_BOOLEAN(v,val) DUK_DBLUNION_SET_HIGH32((v), (((duk_uint32_t) DUK_TAG_BOOLEAN) << 16) | ((duk_uint32_t) (val))) + +#define DUK_TVAL_SET_NAN(v) DUK_DBLUNION_SET_NAN_FULL((v)) + +/* Assumes that caller has normalized NaNs, otherwise trouble ahead. */ +#if defined(DUK_USE_FASTINT) +#define DUK_TVAL_SET_DOUBLE(v,d) do { \ + duk_double_t duk__dblval; \ + duk__dblval = (d); \ + DUK_ASSERT_DOUBLE_IS_NORMALIZED(duk__dblval); \ + DUK_DBLUNION_SET_DOUBLE((v), duk__dblval); \ + } while (0) +#define DUK_TVAL_SET_FASTINT(v,i) DUK__TVAL_SET_FASTINT((v), (i)) +#define DUK_TVAL_SET_FASTINT_I32(v,i) DUK__TVAL_SET_FASTINT_I32((v), (i)) +#define DUK_TVAL_SET_FASTINT_U32(v,i) DUK__TVAL_SET_FASTINT_U32((v), (i)) +#define DUK_TVAL_SET_NUMBER_CHKFAST(v,d) duk_tval_set_number_chkfast((v), (d)) +#define DUK_TVAL_SET_NUMBER(v,d) DUK_TVAL_SET_DOUBLE((v), (d)) +#define DUK_TVAL_CHKFAST_INPLACE(v) do { \ + duk_tval *duk__tv; \ + duk_double_t duk__d; \ + duk__tv = (v); \ + if (DUK_TVAL_IS_DOUBLE(duk__tv)) { \ + duk__d = DUK_TVAL_GET_DOUBLE(duk__tv); \ + DUK_TVAL_SET_NUMBER_CHKFAST(duk__tv, duk__d); \ + } \ + } while (0) +#else +#define DUK_TVAL_SET_DOUBLE(v,d) do { \ + duk_double_t duk__dblval; \ + duk__dblval = (d); \ + DUK_ASSERT_DOUBLE_IS_NORMALIZED(duk__dblval); \ + DUK_DBLUNION_SET_DOUBLE((v), duk__dblval); \ + } while (0) +#define DUK_TVAL_SET_FASTINT(v,i) DUK_TVAL_SET_DOUBLE((v), (duk_double_t) (i)) /* XXX: fast int-to-double */ +#define DUK_TVAL_SET_FASTINT_I32(v,i) DUK_TVAL_SET_DOUBLE((v), (duk_double_t) (i)) +#define DUK_TVAL_SET_FASTINT_U32(v,i) DUK_TVAL_SET_DOUBLE((v), (duk_double_t) (i)) +#define DUK_TVAL_SET_NUMBER_CHKFAST(v,d) DUK_TVAL_SET_DOUBLE((v), (d)) +#define DUK_TVAL_SET_NUMBER(v,d) DUK_TVAL_SET_DOUBLE((v), (d)) +#define DUK_TVAL_CHKFAST_INPLACE(v) do { } while (0) +#endif + +#define DUK_TVAL_SET_LIGHTFUNC(v,fp,flags) DUK__TVAL_SET_LIGHTFUNC((v), (fp), (flags)) +#define DUK_TVAL_SET_STRING(v,h) DUK__TVAL_SET_TAGGEDPOINTER((v), (h), DUK_TAG_STRING) +#define DUK_TVAL_SET_OBJECT(v,h) DUK__TVAL_SET_TAGGEDPOINTER((v), (h), DUK_TAG_OBJECT) +#define DUK_TVAL_SET_BUFFER(v,h) DUK__TVAL_SET_TAGGEDPOINTER((v), (h), DUK_TAG_BUFFER) +#define DUK_TVAL_SET_POINTER(v,p) DUK__TVAL_SET_TAGGEDPOINTER((v), (p), DUK_TAG_POINTER) + +#define DUK_TVAL_SET_TVAL(v,x) do { *(v) = *(x); } while (0) + +/* getters */ +#define DUK_TVAL_GET_BOOLEAN(v) ((int) (v)->us[DUK_DBL_IDX_US1]) +#if defined(DUK_USE_FASTINT) +#define DUK_TVAL_GET_DOUBLE(v) ((v)->d) +#define DUK_TVAL_GET_FASTINT(v) DUK__TVAL_GET_FASTINT((v)) +#define DUK_TVAL_GET_FASTINT_U32(v) DUK__TVAL_GET_FASTINT_U32((v)) +#define DUK_TVAL_GET_FASTINT_I32(v) DUK__TVAL_GET_FASTINT_I32((v)) +#define DUK_TVAL_GET_NUMBER(v) duk_tval_get_number_packed((v)) +#else +#define DUK_TVAL_GET_NUMBER(v) ((v)->d) +#define DUK_TVAL_GET_DOUBLE(v) ((v)->d) +#endif +#define DUK_TVAL_GET_LIGHTFUNC(v,out_fp,out_flags) do { \ + (out_flags) = (v)->ui[DUK_DBL_IDX_UI0] & 0xffffUL; \ + (out_fp) = (duk_c_function) (v)->ui[DUK_DBL_IDX_UI1]; \ + } while (0) +#define DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(v) ((duk_c_function) ((v)->ui[DUK_DBL_IDX_UI1])) +#define DUK_TVAL_GET_LIGHTFUNC_FLAGS(v) (((int) (v)->ui[DUK_DBL_IDX_UI0]) & 0xffffUL) +#define DUK_TVAL_GET_STRING(v) ((duk_hstring *) (v)->vp[DUK_DBL_IDX_VP1]) +#define DUK_TVAL_GET_OBJECT(v) ((duk_hobject *) (v)->vp[DUK_DBL_IDX_VP1]) +#define DUK_TVAL_GET_BUFFER(v) ((duk_hbuffer *) (v)->vp[DUK_DBL_IDX_VP1]) +#define DUK_TVAL_GET_POINTER(v) ((void *) (v)->vp[DUK_DBL_IDX_VP1]) +#define DUK_TVAL_GET_HEAPHDR(v) ((duk_heaphdr *) (v)->vp[DUK_DBL_IDX_VP1]) + +/* decoding */ +#define DUK_TVAL_GET_TAG(v) ((duk_small_uint_t) (v)->us[DUK_DBL_IDX_US0]) + +#define DUK_TVAL_IS_UNDEFINED(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_UNDEFINED) +#define DUK_TVAL_IS_UNUSED(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_UNUSED) +#define DUK_TVAL_IS_NULL(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_NULL) +#define DUK_TVAL_IS_BOOLEAN(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_BOOLEAN) +#define DUK_TVAL_IS_BOOLEAN_TRUE(v) ((v)->ui[DUK_DBL_IDX_UI0] == DUK_XTAG_BOOLEAN_TRUE) +#define DUK_TVAL_IS_BOOLEAN_FALSE(v) ((v)->ui[DUK_DBL_IDX_UI0] == DUK_XTAG_BOOLEAN_FALSE) +#define DUK_TVAL_IS_LIGHTFUNC(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_LIGHTFUNC) +#define DUK_TVAL_IS_STRING(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_STRING) +#define DUK_TVAL_IS_OBJECT(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_OBJECT) +#define DUK_TVAL_IS_BUFFER(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_BUFFER) +#define DUK_TVAL_IS_POINTER(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_POINTER) +#if defined(DUK_USE_FASTINT) +/* 0xfff0 is -Infinity */ +#define DUK_TVAL_IS_DOUBLE(v) (DUK_TVAL_GET_TAG((v)) <= 0xfff0UL) +#define DUK_TVAL_IS_FASTINT(v) (DUK_TVAL_GET_TAG((v)) == DUK_TAG_FASTINT) +#define DUK_TVAL_IS_NUMBER(v) (DUK_TVAL_GET_TAG((v)) <= 0xfff1UL) +#else +#define DUK_TVAL_IS_NUMBER(v) (DUK_TVAL_GET_TAG((v)) <= 0xfff0UL) +#define DUK_TVAL_IS_DOUBLE(v) DUK_TVAL_IS_NUMBER((v)) +#endif + +/* This is performance critical because it appears in every DECREF. */ +#define DUK_TVAL_IS_HEAP_ALLOCATED(v) (DUK_TVAL_GET_TAG((v)) >= DUK_TAG_STRING) + +#if defined(DUK_USE_FASTINT) +DUK_INTERNAL_DECL duk_double_t duk_tval_get_number_packed(duk_tval *tv); +#endif + +#else /* DUK_USE_PACKED_TVAL */ +/* ======================================================================== */ + +/* + * Portable 12-byte representation + */ + +/* Note: not initializing all bytes is normally not an issue: Duktape won't + * read or use the uninitialized bytes so valgrind won't issue warnings. + * In some special cases a harmless valgrind warning may be issued though. + * For example, the DumpHeap debugger command writes out a compiled function's + * 'data' area as is, including any uninitialized bytes, which causes a + * valgrind warning. + */ + +typedef struct duk_tval_struct duk_tval; + +struct duk_tval_struct { + duk_small_uint_t t; + duk_small_uint_t v_extra; + union { + duk_double_t d; + duk_small_int_t i; +#if defined(DUK_USE_FASTINT) + duk_int64_t fi; /* if present, forces 16-byte duk_tval */ +#endif + void *voidptr; + duk_hstring *hstring; + duk_hobject *hobject; + duk_hcompiledfunction *hcompiledfunction; + duk_hnativefunction *hnativefunction; + duk_hthread *hthread; + duk_hbuffer *hbuffer; + duk_heaphdr *heaphdr; + duk_c_function lightfunc; + } v; +}; + +#define DUK__TAG_NUMBER 0 /* not exposed */ +#if defined(DUK_USE_FASTINT) +#define DUK_TAG_FASTINT 1 +#endif +#define DUK_TAG_UNDEFINED 2 +#define DUK_TAG_NULL 3 +#define DUK_TAG_BOOLEAN 4 +#define DUK_TAG_POINTER 5 +#define DUK_TAG_LIGHTFUNC 6 +#define DUK_TAG_UNUSED 7 /* marker; not actual tagged type */ +#define DUK_TAG_STRING 8 /* first heap allocated, match bit boundary */ +#define DUK_TAG_OBJECT 9 +#define DUK_TAG_BUFFER 10 + +/* DUK__TAG_NUMBER is intentionally first, as it is the default clause in code + * to support the 8-byte representation. Further, it is a non-heap-allocated + * type so it should come before DUK_TAG_STRING. Finally, it should not break + * the tag value ranges covered by case-clauses in a switch-case. + */ + +/* setters */ +#define DUK_TVAL_SET_UNDEFINED(tv) do { \ + (tv)->t = DUK_TAG_UNDEFINED; \ + } while (0) + +#define DUK_TVAL_SET_UNUSED(tv) do { \ + (tv)->t = DUK_TAG_UNUSED; \ + } while (0) + +#define DUK_TVAL_SET_NULL(tv) do { \ + (tv)->t = DUK_TAG_NULL; \ + } while (0) + +#define DUK_TVAL_SET_BOOLEAN(tv,val) do { \ + (tv)->t = DUK_TAG_BOOLEAN; \ + (tv)->v.i = (val); \ + } while (0) + +#if defined(DUK_USE_FASTINT) +#define DUK_TVAL_SET_DOUBLE(tv,val) do { \ + (tv)->t = DUK__TAG_NUMBER; \ + (tv)->v.d = (val); \ + } while (0) +#define DUK_TVAL_SET_FASTINT(tv,val) do { \ + (tv)->t = DUK_TAG_FASTINT; \ + (tv)->v.fi = (val); \ + } while (0) +#define DUK_TVAL_SET_FASTINT_U32(tv,val) do { \ + (tv)->t = DUK_TAG_FASTINT; \ + (tv)->v.fi = (duk_int64_t) (val); \ + } while (0) +#define DUK_TVAL_SET_FASTINT_I32(tv,val) do { \ + (tv)->t = DUK_TAG_FASTINT; \ + (tv)->v.fi = (duk_int64_t) (val); \ + } while (0) +#define DUK_TVAL_SET_NUMBER_CHKFAST(tv,d) \ + duk_tval_set_number_chkfast((tv), (d)) +#define DUK_TVAL_SET_NUMBER(tv,val) \ + DUK_TVAL_SET_DOUBLE((tv), (val)) +#define DUK_TVAL_CHKFAST_INPLACE(v) do { \ + duk_tval *duk__tv; \ + duk_double_t duk__d; \ + duk__tv = (v); \ + if (DUK_TVAL_IS_DOUBLE(duk__tv)) { \ + duk__d = DUK_TVAL_GET_DOUBLE(duk__tv); \ + DUK_TVAL_SET_NUMBER_CHKFAST(duk__tv, duk__d); \ + } \ + } while (0) +#else +#define DUK_TVAL_SET_DOUBLE(tv,d) \ + DUK_TVAL_SET_NUMBER((tv), (d)) +#define DUK_TVAL_SET_FASTINT(tv,val) \ + DUK_TVAL_SET_NUMBER((tv), (duk_double_t) (val)) /* XXX: fast int-to-double */ +#define DUK_TVAL_SET_FASTINT_U32(tv,val) \ + DUK_TVAL_SET_NUMBER((tv), (duk_double_t) (val)) +#define DUK_TVAL_SET_FASTINT_I32(tv,val) \ + DUK_TVAL_SET_NUMBER((tv), (duk_double_t) (val)) +#define DUK_TVAL_SET_NUMBER(tv,val) do { \ + (tv)->t = DUK__TAG_NUMBER; \ + (tv)->v.d = (val); \ + } while (0) +#define DUK_TVAL_SET_NUMBER_CHKFAST(tv,d) \ + DUK_TVAL_SET_NUMBER((tv), (d)) +#define DUK_TVAL_CHKFAST_INPLACE(v) do { } while (0) +#endif /* DUK_USE_FASTINT */ + +#define DUK_TVAL_SET_POINTER(tv,hptr) do { \ + (tv)->t = DUK_TAG_POINTER; \ + (tv)->v.voidptr = (hptr); \ + } while (0) + +#define DUK_TVAL_SET_LIGHTFUNC(tv,fp,flags) do { \ + (tv)->t = DUK_TAG_LIGHTFUNC; \ + (tv)->v_extra = (flags); \ + (tv)->v.lightfunc = (duk_c_function) (fp); \ + } while (0) + +#define DUK_TVAL_SET_STRING(tv,hptr) do { \ + (tv)->t = DUK_TAG_STRING; \ + (tv)->v.hstring = (hptr); \ + } while (0) + +#define DUK_TVAL_SET_OBJECT(tv,hptr) do { \ + (tv)->t = DUK_TAG_OBJECT; \ + (tv)->v.hobject = (hptr); \ + } while (0) + +#define DUK_TVAL_SET_BUFFER(tv,hptr) do { \ + (tv)->t = DUK_TAG_BUFFER; \ + (tv)->v.hbuffer = (hptr); \ + } while (0) + +#define DUK_TVAL_SET_NAN(tv) do { \ + /* in non-packed representation we don't care about which NaN is used */ \ + (tv)->t = DUK__TAG_NUMBER; \ + (tv)->v.d = DUK_DOUBLE_NAN; \ + } while (0) + +#define DUK_TVAL_SET_TVAL(v,x) do { *(v) = *(x); } while (0) + +/* getters */ +#define DUK_TVAL_GET_BOOLEAN(tv) ((tv)->v.i) +#if defined(DUK_USE_FASTINT) +#define DUK_TVAL_GET_DOUBLE(tv) ((tv)->v.d) +#define DUK_TVAL_GET_FASTINT(tv) ((tv)->v.fi) +#define DUK_TVAL_GET_FASTINT_U32(tv) ((duk_uint32_t) ((tv)->v.fi)) +#define DUK_TVAL_GET_FASTINT_I32(tv) ((duk_int32_t) ((tv)->v.fi)) +#if 0 +#define DUK_TVAL_GET_NUMBER(tv) (DUK_TVAL_IS_FASTINT((tv)) ? \ + (duk_double_t) DUK_TVAL_GET_FASTINT((tv)) : \ + DUK_TVAL_GET_DOUBLE((tv))) +#define DUK_TVAL_GET_NUMBER(tv) duk_tval_get_number_unpacked((tv)) +#else +/* This seems reasonable overall. */ +#define DUK_TVAL_GET_NUMBER(tv) (DUK_TVAL_IS_FASTINT((tv)) ? \ + duk_tval_get_number_unpacked_fastint((tv)) : \ + DUK_TVAL_GET_DOUBLE((tv))) +#endif +#else +#define DUK_TVAL_GET_NUMBER(tv) ((tv)->v.d) +#define DUK_TVAL_GET_DOUBLE(tv) ((tv)->v.d) +#endif /* DUK_USE_FASTINT */ +#define DUK_TVAL_GET_POINTER(tv) ((tv)->v.voidptr) +#define DUK_TVAL_GET_LIGHTFUNC(tv,out_fp,out_flags) do { \ + (out_flags) = (duk_uint32_t) (tv)->v_extra; \ + (out_fp) = (tv)->v.lightfunc; \ + } while (0) +#define DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv) ((tv)->v.lightfunc) +#define DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv) ((duk_uint32_t) ((tv)->v_extra)) +#define DUK_TVAL_GET_STRING(tv) ((tv)->v.hstring) +#define DUK_TVAL_GET_OBJECT(tv) ((tv)->v.hobject) +#define DUK_TVAL_GET_BUFFER(tv) ((tv)->v.hbuffer) +#define DUK_TVAL_GET_HEAPHDR(tv) ((tv)->v.heaphdr) + +/* decoding */ +#define DUK_TVAL_GET_TAG(tv) ((tv)->t) +#define DUK_TVAL_IS_UNDEFINED(tv) ((tv)->t == DUK_TAG_UNDEFINED) +#define DUK_TVAL_IS_UNUSED(tv) ((tv)->t == DUK_TAG_UNUSED) +#define DUK_TVAL_IS_NULL(tv) ((tv)->t == DUK_TAG_NULL) +#define DUK_TVAL_IS_BOOLEAN(tv) ((tv)->t == DUK_TAG_BOOLEAN) +#define DUK_TVAL_IS_BOOLEAN_TRUE(tv) (((tv)->t == DUK_TAG_BOOLEAN) && ((tv)->v.i != 0)) +#define DUK_TVAL_IS_BOOLEAN_FALSE(tv) (((tv)->t == DUK_TAG_BOOLEAN) && ((tv)->v.i == 0)) +#if defined(DUK_USE_FASTINT) +#define DUK_TVAL_IS_DOUBLE(tv) ((tv)->t == DUK__TAG_NUMBER) +#define DUK_TVAL_IS_FASTINT(tv) ((tv)->t == DUK_TAG_FASTINT) +#define DUK_TVAL_IS_NUMBER(tv) ((tv)->t == DUK__TAG_NUMBER || \ + (tv)->t == DUK_TAG_FASTINT) +#else +#define DUK_TVAL_IS_NUMBER(tv) ((tv)->t == DUK__TAG_NUMBER) +#define DUK_TVAL_IS_DOUBLE(v) DUK_TVAL_IS_NUMBER((v)) +#endif /* DUK_USE_FASTINT */ +#define DUK_TVAL_IS_POINTER(tv) ((tv)->t == DUK_TAG_POINTER) +#define DUK_TVAL_IS_LIGHTFUNC(tv) ((tv)->t == DUK_TAG_LIGHTFUNC) +#define DUK_TVAL_IS_STRING(tv) ((tv)->t == DUK_TAG_STRING) +#define DUK_TVAL_IS_OBJECT(tv) ((tv)->t == DUK_TAG_OBJECT) +#define DUK_TVAL_IS_BUFFER(tv) ((tv)->t == DUK_TAG_BUFFER) + +/* This is performance critical because it's needed for every DECREF. + * Take advantage of the fact that the first heap allocated tag is 8, + * so that bit 3 is set for all heap allocated tags (and never set for + * non-heap-allocated tags). + */ +#if 0 +#define DUK_TVAL_IS_HEAP_ALLOCATED(tv) ((tv)->t >= DUK_TAG_STRING) +#endif +#define DUK_TVAL_IS_HEAP_ALLOCATED(tv) ((tv)->t & 0x08) + +#if defined(DUK_USE_FASTINT) +#if 0 +DUK_INTERNAL_DECL duk_double_t duk_tval_get_number_unpacked(duk_tval *tv); +#endif +DUK_INTERNAL_DECL duk_double_t duk_tval_get_number_unpacked_fastint(duk_tval *tv); +#endif + +#endif /* DUK_USE_PACKED_TVAL */ + +/* + * Convenience (independent of representation) + */ + +#define DUK_TVAL_SET_BOOLEAN_TRUE(v) DUK_TVAL_SET_BOOLEAN(v, 1) +#define DUK_TVAL_SET_BOOLEAN_FALSE(v) DUK_TVAL_SET_BOOLEAN(v, 0) + +/* Lightfunc flags packing and unpacking. */ +/* Sign extend: 0x0000##00 -> 0x##000000 -> sign extend to 0xssssss## */ +#define DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags) \ + ((((duk_int32_t) (lf_flags)) << 16) >> 24) +#define DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags) \ + (((lf_flags) >> 4) & 0x0f) +#define DUK_LFUNC_FLAGS_GET_NARGS(lf_flags) \ + ((lf_flags) & 0x0f) +#define DUK_LFUNC_FLAGS_PACK(magic,length,nargs) \ + (((magic) & 0xff) << 8) | ((length) << 4) | (nargs) + +#define DUK_LFUNC_NARGS_VARARGS 0x0f /* varargs marker */ +#define DUK_LFUNC_NARGS_MIN 0x00 +#define DUK_LFUNC_NARGS_MAX 0x0e /* max, excl. varargs marker */ +#define DUK_LFUNC_LENGTH_MIN 0x00 +#define DUK_LFUNC_LENGTH_MAX 0x0f +#define DUK_LFUNC_MAGIC_MIN (-0x80) +#define DUK_LFUNC_MAGIC_MAX 0x7f + +/* fastint constants etc */ +#if defined(DUK_USE_FASTINT) +#define DUK_FASTINT_MIN (-0x800000000000LL) +#define DUK_FASTINT_MAX 0x7fffffffffffLL +#define DUK_FASTINT_BITS 48 + +DUK_INTERNAL_DECL void duk_tval_set_number_chkfast(duk_tval *tv, duk_double_t x); +#endif + +#endif /* DUK_TVAL_H_INCLUDED */ +/* + * Automatically generated by genbuiltins.py, do not edit! + */ + +#ifndef DUK_BUILTINS_H_INCLUDED +#define DUK_BUILTINS_H_INCLUDED + +#if defined(DUK_USE_ROM_STRINGS) +#error ROM support not enabled, rerun make_dist.py with --rom-support +#else /* DUK_USE_ROM_STRINGS */ +#define DUK_STRIDX_UC_UNDEFINED 0 /* 'Undefined' */ +#define DUK_HEAP_STRING_UC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_UNDEFINED) +#define DUK_HTHREAD_STRING_UC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_UNDEFINED) +#define DUK_STRIDX_UC_NULL 1 /* 'Null' */ +#define DUK_HEAP_STRING_UC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NULL) +#define DUK_HTHREAD_STRING_UC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NULL) +#define DUK_STRIDX_UC_ARGUMENTS 2 /* 'Arguments' */ +#define DUK_HEAP_STRING_UC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ARGUMENTS) +#define DUK_HTHREAD_STRING_UC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ARGUMENTS) +#define DUK_STRIDX_UC_OBJECT 3 /* 'Object' */ +#define DUK_HEAP_STRING_UC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_OBJECT) +#define DUK_HTHREAD_STRING_UC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_OBJECT) +#define DUK_STRIDX_UC_FUNCTION 4 /* 'Function' */ +#define DUK_HEAP_STRING_UC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_FUNCTION) +#define DUK_HTHREAD_STRING_UC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_FUNCTION) +#define DUK_STRIDX_ARRAY 5 /* 'Array' */ +#define DUK_HEAP_STRING_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY) +#define DUK_HTHREAD_STRING_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY) +#define DUK_STRIDX_UC_STRING 6 /* 'String' */ +#define DUK_HEAP_STRING_UC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_STRING) +#define DUK_HTHREAD_STRING_UC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_STRING) +#define DUK_STRIDX_UC_BOOLEAN 7 /* 'Boolean' */ +#define DUK_HEAP_STRING_UC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BOOLEAN) +#define DUK_HTHREAD_STRING_UC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BOOLEAN) +#define DUK_STRIDX_UC_NUMBER 8 /* 'Number' */ +#define DUK_HEAP_STRING_UC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NUMBER) +#define DUK_HTHREAD_STRING_UC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NUMBER) +#define DUK_STRIDX_DATE 9 /* 'Date' */ +#define DUK_HEAP_STRING_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATE) +#define DUK_HTHREAD_STRING_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATE) +#define DUK_STRIDX_REG_EXP 10 /* 'RegExp' */ +#define DUK_HEAP_STRING_REG_EXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REG_EXP) +#define DUK_HTHREAD_STRING_REG_EXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REG_EXP) +#define DUK_STRIDX_UC_ERROR 11 /* 'Error' */ +#define DUK_HEAP_STRING_UC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ERROR) +#define DUK_HTHREAD_STRING_UC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ERROR) +#define DUK_STRIDX_MATH 12 /* 'Math' */ +#define DUK_HEAP_STRING_MATH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MATH) +#define DUK_HTHREAD_STRING_MATH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MATH) +#define DUK_STRIDX_JSON 13 /* 'JSON' */ +#define DUK_HEAP_STRING_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON) +#define DUK_HTHREAD_STRING_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON) +#define DUK_STRIDX_EMPTY_STRING 14 /* '' */ +#define DUK_HEAP_STRING_EMPTY_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EMPTY_STRING) +#define DUK_HTHREAD_STRING_EMPTY_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EMPTY_STRING) +#define DUK_STRIDX_ARRAY_BUFFER 15 /* 'ArrayBuffer' */ +#define DUK_HEAP_STRING_ARRAY_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY_BUFFER) +#define DUK_HTHREAD_STRING_ARRAY_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY_BUFFER) +#define DUK_STRIDX_DATA_VIEW 16 /* 'DataView' */ +#define DUK_HEAP_STRING_DATA_VIEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA_VIEW) +#define DUK_HTHREAD_STRING_DATA_VIEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA_VIEW) +#define DUK_STRIDX_INT8_ARRAY 17 /* 'Int8Array' */ +#define DUK_HEAP_STRING_INT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT8_ARRAY) +#define DUK_HTHREAD_STRING_INT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT8_ARRAY) +#define DUK_STRIDX_UINT8_ARRAY 18 /* 'Uint8Array' */ +#define DUK_HEAP_STRING_UINT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_ARRAY) +#define DUK_HTHREAD_STRING_UINT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_ARRAY) +#define DUK_STRIDX_UINT8_CLAMPED_ARRAY 19 /* 'Uint8ClampedArray' */ +#define DUK_HEAP_STRING_UINT8_CLAMPED_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_CLAMPED_ARRAY) +#define DUK_HTHREAD_STRING_UINT8_CLAMPED_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_CLAMPED_ARRAY) +#define DUK_STRIDX_INT16_ARRAY 20 /* 'Int16Array' */ +#define DUK_HEAP_STRING_INT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT16_ARRAY) +#define DUK_HTHREAD_STRING_INT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT16_ARRAY) +#define DUK_STRIDX_UINT16_ARRAY 21 /* 'Uint16Array' */ +#define DUK_HEAP_STRING_UINT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT16_ARRAY) +#define DUK_HTHREAD_STRING_UINT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT16_ARRAY) +#define DUK_STRIDX_INT32_ARRAY 22 /* 'Int32Array' */ +#define DUK_HEAP_STRING_INT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT32_ARRAY) +#define DUK_HTHREAD_STRING_INT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT32_ARRAY) +#define DUK_STRIDX_UINT32_ARRAY 23 /* 'Uint32Array' */ +#define DUK_HEAP_STRING_UINT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT32_ARRAY) +#define DUK_HTHREAD_STRING_UINT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT32_ARRAY) +#define DUK_STRIDX_FLOAT32_ARRAY 24 /* 'Float32Array' */ +#define DUK_HEAP_STRING_FLOAT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT32_ARRAY) +#define DUK_HTHREAD_STRING_FLOAT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT32_ARRAY) +#define DUK_STRIDX_FLOAT64_ARRAY 25 /* 'Float64Array' */ +#define DUK_HEAP_STRING_FLOAT64_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT64_ARRAY) +#define DUK_HTHREAD_STRING_FLOAT64_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT64_ARRAY) +#define DUK_STRIDX_GLOBAL 26 /* 'global' */ +#define DUK_HEAP_STRING_GLOBAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GLOBAL) +#define DUK_HTHREAD_STRING_GLOBAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GLOBAL) +#define DUK_STRIDX_OBJ_ENV 27 /* 'ObjEnv' */ +#define DUK_HEAP_STRING_OBJ_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OBJ_ENV) +#define DUK_HTHREAD_STRING_OBJ_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OBJ_ENV) +#define DUK_STRIDX_DEC_ENV 28 /* 'DecEnv' */ +#define DUK_HEAP_STRING_DEC_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEC_ENV) +#define DUK_HTHREAD_STRING_DEC_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEC_ENV) +#define DUK_STRIDX_UC_BUFFER 29 /* 'Buffer' */ +#define DUK_HEAP_STRING_UC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BUFFER) +#define DUK_HTHREAD_STRING_UC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BUFFER) +#define DUK_STRIDX_UC_POINTER 30 /* 'Pointer' */ +#define DUK_HEAP_STRING_UC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_POINTER) +#define DUK_HTHREAD_STRING_UC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_POINTER) +#define DUK_STRIDX_UC_THREAD 31 /* 'Thread' */ +#define DUK_HEAP_STRING_UC_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_THREAD) +#define DUK_HTHREAD_STRING_UC_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_THREAD) +#define DUK_STRIDX_EVAL 32 /* 'eval' */ +#define DUK_HEAP_STRING_EVAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVAL) +#define DUK_HTHREAD_STRING_EVAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVAL) +#define DUK_STRIDX_DEFINE_PROPERTY 33 /* 'defineProperty' */ +#define DUK_HEAP_STRING_DEFINE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFINE_PROPERTY) +#define DUK_HTHREAD_STRING_DEFINE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFINE_PROPERTY) +#define DUK_STRIDX_VALUE 34 /* 'value' */ +#define DUK_HEAP_STRING_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE) +#define DUK_HTHREAD_STRING_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE) +#define DUK_STRIDX_WRITABLE 35 /* 'writable' */ +#define DUK_HEAP_STRING_WRITABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITABLE) +#define DUK_HTHREAD_STRING_WRITABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITABLE) +#define DUK_STRIDX_CONFIGURABLE 36 /* 'configurable' */ +#define DUK_HEAP_STRING_CONFIGURABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONFIGURABLE) +#define DUK_HTHREAD_STRING_CONFIGURABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONFIGURABLE) +#define DUK_STRIDX_ENUMERABLE 37 /* 'enumerable' */ +#define DUK_HEAP_STRING_ENUMERABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERABLE) +#define DUK_HTHREAD_STRING_ENUMERABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERABLE) +#define DUK_STRIDX_JOIN 38 /* 'join' */ +#define DUK_HEAP_STRING_JOIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JOIN) +#define DUK_HTHREAD_STRING_JOIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JOIN) +#define DUK_STRIDX_TO_LOCALE_STRING 39 /* 'toLocaleString' */ +#define DUK_HEAP_STRING_TO_LOCALE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_STRING) +#define DUK_HTHREAD_STRING_TO_LOCALE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_STRING) +#define DUK_STRIDX_VALUE_OF 40 /* 'valueOf' */ +#define DUK_HEAP_STRING_VALUE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE_OF) +#define DUK_HTHREAD_STRING_VALUE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE_OF) +#define DUK_STRIDX_TO_UTC_STRING 41 /* 'toUTCString' */ +#define DUK_HEAP_STRING_TO_UTC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_UTC_STRING) +#define DUK_HTHREAD_STRING_TO_UTC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_UTC_STRING) +#define DUK_STRIDX_TO_ISO_STRING 42 /* 'toISOString' */ +#define DUK_HEAP_STRING_TO_ISO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_ISO_STRING) +#define DUK_HTHREAD_STRING_TO_ISO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_ISO_STRING) +#define DUK_STRIDX_TO_GMT_STRING 43 /* 'toGMTString' */ +#define DUK_HEAP_STRING_TO_GMT_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_GMT_STRING) +#define DUK_HTHREAD_STRING_TO_GMT_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_GMT_STRING) +#define DUK_STRIDX_SOURCE 44 /* 'source' */ +#define DUK_HEAP_STRING_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SOURCE) +#define DUK_HTHREAD_STRING_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SOURCE) +#define DUK_STRIDX_IGNORE_CASE 45 /* 'ignoreCase' */ +#define DUK_HEAP_STRING_IGNORE_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IGNORE_CASE) +#define DUK_HTHREAD_STRING_IGNORE_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IGNORE_CASE) +#define DUK_STRIDX_MULTILINE 46 /* 'multiline' */ +#define DUK_HEAP_STRING_MULTILINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MULTILINE) +#define DUK_HTHREAD_STRING_MULTILINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MULTILINE) +#define DUK_STRIDX_LAST_INDEX 47 /* 'lastIndex' */ +#define DUK_HEAP_STRING_LAST_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LAST_INDEX) +#define DUK_HTHREAD_STRING_LAST_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LAST_INDEX) +#define DUK_STRIDX_ESCAPED_EMPTY_REGEXP 48 /* '(?:)' */ +#define DUK_HEAP_STRING_ESCAPED_EMPTY_REGEXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ESCAPED_EMPTY_REGEXP) +#define DUK_HTHREAD_STRING_ESCAPED_EMPTY_REGEXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ESCAPED_EMPTY_REGEXP) +#define DUK_STRIDX_INDEX 49 /* 'index' */ +#define DUK_HEAP_STRING_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INDEX) +#define DUK_HTHREAD_STRING_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INDEX) +#define DUK_STRIDX_PROTOTYPE 50 /* 'prototype' */ +#define DUK_HEAP_STRING_PROTOTYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTOTYPE) +#define DUK_HTHREAD_STRING_PROTOTYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTOTYPE) +#define DUK_STRIDX_CONSTRUCTOR 51 /* 'constructor' */ +#define DUK_HEAP_STRING_CONSTRUCTOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONSTRUCTOR) +#define DUK_HTHREAD_STRING_CONSTRUCTOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONSTRUCTOR) +#define DUK_STRIDX_MESSAGE 52 /* 'message' */ +#define DUK_HEAP_STRING_MESSAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MESSAGE) +#define DUK_HTHREAD_STRING_MESSAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MESSAGE) +#define DUK_STRIDX_LC_BOOLEAN 53 /* 'boolean' */ +#define DUK_HEAP_STRING_LC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BOOLEAN) +#define DUK_HTHREAD_STRING_LC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BOOLEAN) +#define DUK_STRIDX_LC_NUMBER 54 /* 'number' */ +#define DUK_HEAP_STRING_LC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NUMBER) +#define DUK_HTHREAD_STRING_LC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NUMBER) +#define DUK_STRIDX_LC_STRING 55 /* 'string' */ +#define DUK_HEAP_STRING_LC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_STRING) +#define DUK_HTHREAD_STRING_LC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_STRING) +#define DUK_STRIDX_LC_OBJECT 56 /* 'object' */ +#define DUK_HEAP_STRING_LC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_OBJECT) +#define DUK_HTHREAD_STRING_LC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_OBJECT) +#define DUK_STRIDX_LC_UNDEFINED 57 /* 'undefined' */ +#define DUK_HEAP_STRING_LC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_UNDEFINED) +#define DUK_HTHREAD_STRING_LC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_UNDEFINED) +#define DUK_STRIDX_NAN 58 /* 'NaN' */ +#define DUK_HEAP_STRING_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAN) +#define DUK_HTHREAD_STRING_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAN) +#define DUK_STRIDX_INFINITY 59 /* 'Infinity' */ +#define DUK_HEAP_STRING_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INFINITY) +#define DUK_HTHREAD_STRING_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INFINITY) +#define DUK_STRIDX_MINUS_INFINITY 60 /* '-Infinity' */ +#define DUK_HEAP_STRING_MINUS_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_INFINITY) +#define DUK_HTHREAD_STRING_MINUS_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_INFINITY) +#define DUK_STRIDX_MINUS_ZERO 61 /* '-0' */ +#define DUK_HEAP_STRING_MINUS_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_ZERO) +#define DUK_HTHREAD_STRING_MINUS_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_ZERO) +#define DUK_STRIDX_COMMA 62 /* ',' */ +#define DUK_HEAP_STRING_COMMA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMMA) +#define DUK_HTHREAD_STRING_COMMA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMMA) +#define DUK_STRIDX_SPACE 63 /* ' ' */ +#define DUK_HEAP_STRING_SPACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SPACE) +#define DUK_HTHREAD_STRING_SPACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SPACE) +#define DUK_STRIDX_NEWLINE_4SPACE 64 /* '\n ' */ +#define DUK_HEAP_STRING_NEWLINE_4SPACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEWLINE_4SPACE) +#define DUK_HTHREAD_STRING_NEWLINE_4SPACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEWLINE_4SPACE) +#define DUK_STRIDX_BRACKETED_ELLIPSIS 65 /* '[...]' */ +#define DUK_HEAP_STRING_BRACKETED_ELLIPSIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BRACKETED_ELLIPSIS) +#define DUK_HTHREAD_STRING_BRACKETED_ELLIPSIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BRACKETED_ELLIPSIS) +#define DUK_STRIDX_INVALID_DATE 66 /* 'Invalid Date' */ +#define DUK_HEAP_STRING_INVALID_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INVALID_DATE) +#define DUK_HTHREAD_STRING_INVALID_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INVALID_DATE) +#define DUK_STRIDX_LC_ARGUMENTS 67 /* 'arguments' */ +#define DUK_HEAP_STRING_LC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ARGUMENTS) +#define DUK_HTHREAD_STRING_LC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ARGUMENTS) +#define DUK_STRIDX_CALLEE 68 /* 'callee' */ +#define DUK_HEAP_STRING_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLEE) +#define DUK_HTHREAD_STRING_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLEE) +#define DUK_STRIDX_CALLER 69 /* 'caller' */ +#define DUK_HEAP_STRING_CALLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLER) +#define DUK_HTHREAD_STRING_CALLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLER) +#define DUK_STRIDX_HAS 70 /* 'has' */ +#define DUK_HEAP_STRING_HAS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HAS) +#define DUK_HTHREAD_STRING_HAS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HAS) +#define DUK_STRIDX_GET 71 /* 'get' */ +#define DUK_HEAP_STRING_GET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET) +#define DUK_HTHREAD_STRING_GET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET) +#define DUK_STRIDX_DELETE_PROPERTY 72 /* 'deleteProperty' */ +#define DUK_HEAP_STRING_DELETE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE_PROPERTY) +#define DUK_HTHREAD_STRING_DELETE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE_PROPERTY) +#define DUK_STRIDX_ENUMERATE 73 /* 'enumerate' */ +#define DUK_HEAP_STRING_ENUMERATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERATE) +#define DUK_HTHREAD_STRING_ENUMERATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERATE) +#define DUK_STRIDX_OWN_KEYS 74 /* 'ownKeys' */ +#define DUK_HEAP_STRING_OWN_KEYS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OWN_KEYS) +#define DUK_HTHREAD_STRING_OWN_KEYS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OWN_KEYS) +#define DUK_STRIDX_SET_PROTOTYPE_OF 75 /* 'setPrototypeOf' */ +#define DUK_HEAP_STRING_SET_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_PROTOTYPE_OF) +#define DUK_HTHREAD_STRING_SET_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_PROTOTYPE_OF) +#define DUK_STRIDX___PROTO__ 76 /* '__proto__' */ +#define DUK_HEAP_STRING___PROTO__(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX___PROTO__) +#define DUK_HTHREAD_STRING___PROTO__(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX___PROTO__) +#define DUK_STRIDX_REQUIRE 77 /* 'require' */ +#define DUK_HEAP_STRING_REQUIRE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REQUIRE) +#define DUK_HTHREAD_STRING_REQUIRE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REQUIRE) +#define DUK_STRIDX_ID 78 /* 'id' */ +#define DUK_HEAP_STRING_ID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ID) +#define DUK_HTHREAD_STRING_ID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ID) +#define DUK_STRIDX_EXPORTS 79 /* 'exports' */ +#define DUK_HEAP_STRING_EXPORTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORTS) +#define DUK_HTHREAD_STRING_EXPORTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORTS) +#define DUK_STRIDX_FILENAME 80 /* 'filename' */ +#define DUK_HEAP_STRING_FILENAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILENAME) +#define DUK_HTHREAD_STRING_FILENAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILENAME) +#define DUK_STRIDX_TO_STRING 81 /* 'toString' */ +#define DUK_HEAP_STRING_TO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_STRING) +#define DUK_HTHREAD_STRING_TO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_STRING) +#define DUK_STRIDX_TO_JSON 82 /* 'toJSON' */ +#define DUK_HEAP_STRING_TO_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_JSON) +#define DUK_HTHREAD_STRING_TO_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_JSON) +#define DUK_STRIDX_TYPE 83 /* 'type' */ +#define DUK_HEAP_STRING_TYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPE) +#define DUK_HTHREAD_STRING_TYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPE) +#define DUK_STRIDX_DATA 84 /* 'data' */ +#define DUK_HEAP_STRING_DATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA) +#define DUK_HTHREAD_STRING_DATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA) +#define DUK_STRIDX_LENGTH 85 /* 'length' */ +#define DUK_HEAP_STRING_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LENGTH) +#define DUK_HTHREAD_STRING_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LENGTH) +#define DUK_STRIDX_BYTE_LENGTH 86 /* 'byteLength' */ +#define DUK_HEAP_STRING_BYTE_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_LENGTH) +#define DUK_HTHREAD_STRING_BYTE_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_LENGTH) +#define DUK_STRIDX_BYTE_OFFSET 87 /* 'byteOffset' */ +#define DUK_HEAP_STRING_BYTE_OFFSET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTE_OFFSET) +#define DUK_HTHREAD_STRING_BYTE_OFFSET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTE_OFFSET) +#define DUK_STRIDX_BYTES_PER_ELEMENT 88 /* 'BYTES_PER_ELEMENT' */ +#define DUK_HEAP_STRING_BYTES_PER_ELEMENT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BYTES_PER_ELEMENT) +#define DUK_HTHREAD_STRING_BYTES_PER_ELEMENT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BYTES_PER_ELEMENT) +#define DUK_STRIDX_SET 89 /* 'set' */ +#define DUK_HEAP_STRING_SET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET) +#define DUK_HTHREAD_STRING_SET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET) +#define DUK_STRIDX_STACK 90 /* 'stack' */ +#define DUK_HEAP_STRING_STACK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STACK) +#define DUK_HTHREAD_STRING_STACK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STACK) +#define DUK_STRIDX_PC 91 /* 'pc' */ +#define DUK_HEAP_STRING_PC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PC) +#define DUK_HTHREAD_STRING_PC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PC) +#define DUK_STRIDX_LINE_NUMBER 92 /* 'lineNumber' */ +#define DUK_HEAP_STRING_LINE_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LINE_NUMBER) +#define DUK_HTHREAD_STRING_LINE_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LINE_NUMBER) +#define DUK_STRIDX_INT_TRACEDATA 93 /* '\xffTracedata' */ +#define DUK_HEAP_STRING_INT_TRACEDATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TRACEDATA) +#define DUK_HTHREAD_STRING_INT_TRACEDATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TRACEDATA) +#define DUK_STRIDX_NAME 94 /* 'name' */ +#define DUK_HEAP_STRING_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAME) +#define DUK_HTHREAD_STRING_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAME) +#define DUK_STRIDX_FILE_NAME 95 /* 'fileName' */ +#define DUK_HEAP_STRING_FILE_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILE_NAME) +#define DUK_HTHREAD_STRING_FILE_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILE_NAME) +#define DUK_STRIDX_LC_BUFFER 96 /* 'buffer' */ +#define DUK_HEAP_STRING_LC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BUFFER) +#define DUK_HTHREAD_STRING_LC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BUFFER) +#define DUK_STRIDX_LC_POINTER 97 /* 'pointer' */ +#define DUK_HEAP_STRING_LC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_POINTER) +#define DUK_HTHREAD_STRING_LC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_POINTER) +#define DUK_STRIDX_INT_VALUE 98 /* '\xffValue' */ +#define DUK_HEAP_STRING_INT_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VALUE) +#define DUK_HTHREAD_STRING_INT_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VALUE) +#define DUK_STRIDX_INT_NEXT 99 /* '\xffNext' */ +#define DUK_HEAP_STRING_INT_NEXT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_NEXT) +#define DUK_HTHREAD_STRING_INT_NEXT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_NEXT) +#define DUK_STRIDX_INT_BYTECODE 100 /* '\xffBytecode' */ +#define DUK_HEAP_STRING_INT_BYTECODE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_BYTECODE) +#define DUK_HTHREAD_STRING_INT_BYTECODE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_BYTECODE) +#define DUK_STRIDX_INT_FORMALS 101 /* '\xffFormals' */ +#define DUK_HEAP_STRING_INT_FORMALS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FORMALS) +#define DUK_HTHREAD_STRING_INT_FORMALS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FORMALS) +#define DUK_STRIDX_INT_VARMAP 102 /* '\xffVarmap' */ +#define DUK_HEAP_STRING_INT_VARMAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARMAP) +#define DUK_HTHREAD_STRING_INT_VARMAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARMAP) +#define DUK_STRIDX_INT_LEXENV 103 /* '\xffLexenv' */ +#define DUK_HEAP_STRING_INT_LEXENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_LEXENV) +#define DUK_HTHREAD_STRING_INT_LEXENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_LEXENV) +#define DUK_STRIDX_INT_VARENV 104 /* '\xffVarenv' */ +#define DUK_HEAP_STRING_INT_VARENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARENV) +#define DUK_HTHREAD_STRING_INT_VARENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARENV) +#define DUK_STRIDX_INT_SOURCE 105 /* '\xffSource' */ +#define DUK_HEAP_STRING_INT_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_SOURCE) +#define DUK_HTHREAD_STRING_INT_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_SOURCE) +#define DUK_STRIDX_INT_PC2LINE 106 /* '\xffPc2line' */ +#define DUK_HEAP_STRING_INT_PC2LINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_PC2LINE) +#define DUK_HTHREAD_STRING_INT_PC2LINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_PC2LINE) +#define DUK_STRIDX_INT_ARGS 107 /* '\xffArgs' */ +#define DUK_HEAP_STRING_INT_ARGS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_ARGS) +#define DUK_HTHREAD_STRING_INT_ARGS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_ARGS) +#define DUK_STRIDX_INT_MAP 108 /* '\xffMap' */ +#define DUK_HEAP_STRING_INT_MAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_MAP) +#define DUK_HTHREAD_STRING_INT_MAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_MAP) +#define DUK_STRIDX_INT_FINALIZER 109 /* '\xffFinalizer' */ +#define DUK_HEAP_STRING_INT_FINALIZER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FINALIZER) +#define DUK_HTHREAD_STRING_INT_FINALIZER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FINALIZER) +#define DUK_STRIDX_INT_HANDLER 110 /* '\xffHandler' */ +#define DUK_HEAP_STRING_INT_HANDLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_HANDLER) +#define DUK_HTHREAD_STRING_INT_HANDLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_HANDLER) +#define DUK_STRIDX_INT_CALLEE 111 /* '\xffCallee' */ +#define DUK_HEAP_STRING_INT_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_CALLEE) +#define DUK_HTHREAD_STRING_INT_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_CALLEE) +#define DUK_STRIDX_INT_THREAD 112 /* '\xffThread' */ +#define DUK_HEAP_STRING_INT_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THREAD) +#define DUK_HTHREAD_STRING_INT_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THREAD) +#define DUK_STRIDX_INT_REGBASE 113 /* '\xffRegbase' */ +#define DUK_HEAP_STRING_INT_REGBASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_REGBASE) +#define DUK_HTHREAD_STRING_INT_REGBASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_REGBASE) +#define DUK_STRIDX_INT_TARGET 114 /* '\xffTarget' */ +#define DUK_HEAP_STRING_INT_TARGET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TARGET) +#define DUK_HTHREAD_STRING_INT_TARGET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TARGET) +#define DUK_STRIDX_INT_THIS 115 /* '\xffThis' */ +#define DUK_HEAP_STRING_INT_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_THIS) +#define DUK_HTHREAD_STRING_INT_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_THIS) +#define DUK_STRIDX_COMPILE 116 /* 'compile' */ +#define DUK_HEAP_STRING_COMPILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPILE) +#define DUK_HTHREAD_STRING_COMPILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPILE) +#define DUK_STRIDX_INPUT 117 /* 'input' */ +#define DUK_HEAP_STRING_INPUT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INPUT) +#define DUK_HTHREAD_STRING_INPUT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INPUT) +#define DUK_STRIDX_ERR_CREATE 118 /* 'errCreate' */ +#define DUK_HEAP_STRING_ERR_CREATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_CREATE) +#define DUK_HTHREAD_STRING_ERR_CREATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_CREATE) +#define DUK_STRIDX_ERR_THROW 119 /* 'errThrow' */ +#define DUK_HEAP_STRING_ERR_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_THROW) +#define DUK_HTHREAD_STRING_ERR_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_THROW) +#define DUK_STRIDX_MOD_SEARCH 120 /* 'modSearch' */ +#define DUK_HEAP_STRING_MOD_SEARCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_SEARCH) +#define DUK_HTHREAD_STRING_MOD_SEARCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_SEARCH) +#define DUK_STRIDX_MOD_LOADED 121 /* 'modLoaded' */ +#define DUK_HEAP_STRING_MOD_LOADED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MOD_LOADED) +#define DUK_HTHREAD_STRING_MOD_LOADED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MOD_LOADED) +#define DUK_STRIDX_ENV 122 /* 'env' */ +#define DUK_HEAP_STRING_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENV) +#define DUK_HTHREAD_STRING_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENV) +#define DUK_STRIDX_HEX 123 /* 'hex' */ +#define DUK_HEAP_STRING_HEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HEX) +#define DUK_HTHREAD_STRING_HEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HEX) +#define DUK_STRIDX_BASE64 124 /* 'base64' */ +#define DUK_HEAP_STRING_BASE64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BASE64) +#define DUK_HTHREAD_STRING_BASE64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BASE64) +#define DUK_STRIDX_JX 125 /* 'jx' */ +#define DUK_HEAP_STRING_JX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JX) +#define DUK_HTHREAD_STRING_JX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JX) +#define DUK_STRIDX_JC 126 /* 'jc' */ +#define DUK_HEAP_STRING_JC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JC) +#define DUK_HTHREAD_STRING_JC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JC) +#define DUK_STRIDX_RESUME 127 /* 'resume' */ +#define DUK_HEAP_STRING_RESUME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RESUME) +#define DUK_HTHREAD_STRING_RESUME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RESUME) +#define DUK_STRIDX_FMT 128 /* 'fmt' */ +#define DUK_HEAP_STRING_FMT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FMT) +#define DUK_HTHREAD_STRING_FMT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FMT) +#define DUK_STRIDX_RAW 129 /* 'raw' */ +#define DUK_HEAP_STRING_RAW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RAW) +#define DUK_HTHREAD_STRING_RAW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RAW) +#define DUK_STRIDX_LC_TRACE 130 /* 'trace' */ +#define DUK_HEAP_STRING_LC_TRACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_TRACE) +#define DUK_HTHREAD_STRING_LC_TRACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_TRACE) +#define DUK_STRIDX_LC_DEBUG 131 /* 'debug' */ +#define DUK_HEAP_STRING_LC_DEBUG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_DEBUG) +#define DUK_HTHREAD_STRING_LC_DEBUG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_DEBUG) +#define DUK_STRIDX_LC_INFO 132 /* 'info' */ +#define DUK_HEAP_STRING_LC_INFO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_INFO) +#define DUK_HTHREAD_STRING_LC_INFO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_INFO) +#define DUK_STRIDX_LC_WARN 133 /* 'warn' */ +#define DUK_HEAP_STRING_LC_WARN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_WARN) +#define DUK_HTHREAD_STRING_LC_WARN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_WARN) +#define DUK_STRIDX_LC_ERROR 134 /* 'error' */ +#define DUK_HEAP_STRING_LC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ERROR) +#define DUK_HTHREAD_STRING_LC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ERROR) +#define DUK_STRIDX_LC_FATAL 135 /* 'fatal' */ +#define DUK_HEAP_STRING_LC_FATAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FATAL) +#define DUK_HTHREAD_STRING_LC_FATAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FATAL) +#define DUK_STRIDX_LC_N 136 /* 'n' */ +#define DUK_HEAP_STRING_LC_N(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_N) +#define DUK_HTHREAD_STRING_LC_N(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_N) +#define DUK_STRIDX_LC_L 137 /* 'l' */ +#define DUK_HEAP_STRING_LC_L(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_L) +#define DUK_HTHREAD_STRING_LC_L(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_L) +#define DUK_STRIDX_CLOG 138 /* 'clog' */ +#define DUK_HEAP_STRING_CLOG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLOG) +#define DUK_HTHREAD_STRING_CLOG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLOG) +#define DUK_STRIDX_TO_LOG_STRING 139 /* 'toLogString' */ +#define DUK_HEAP_STRING_TO_LOG_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOG_STRING) +#define DUK_HTHREAD_STRING_TO_LOG_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOG_STRING) +#define DUK_STRIDX_JSON_EXT_UNDEFINED 140 /* '{"_undef":true}' */ +#define DUK_HEAP_STRING_JSON_EXT_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_UNDEFINED) +#define DUK_HTHREAD_STRING_JSON_EXT_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_UNDEFINED) +#define DUK_STRIDX_JSON_EXT_NAN 141 /* '{"_nan":true}' */ +#define DUK_HEAP_STRING_JSON_EXT_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NAN) +#define DUK_HTHREAD_STRING_JSON_EXT_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NAN) +#define DUK_STRIDX_JSON_EXT_POSINF 142 /* '{"_inf":true}' */ +#define DUK_HEAP_STRING_JSON_EXT_POSINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_POSINF) +#define DUK_HTHREAD_STRING_JSON_EXT_POSINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_POSINF) +#define DUK_STRIDX_JSON_EXT_NEGINF 143 /* '{"_ninf":true}' */ +#define DUK_HEAP_STRING_JSON_EXT_NEGINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NEGINF) +#define DUK_HTHREAD_STRING_JSON_EXT_NEGINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NEGINF) +#define DUK_STRIDX_JSON_EXT_FUNCTION1 144 /* '{"_func":true}' */ +#define DUK_HEAP_STRING_JSON_EXT_FUNCTION1(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION1) +#define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION1(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION1) +#define DUK_STRIDX_JSON_EXT_FUNCTION2 145 /* '{_func:true}' */ +#define DUK_HEAP_STRING_JSON_EXT_FUNCTION2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION2) +#define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION2) +#define DUK_STRIDX_BREAK 146 /* 'break' */ +#define DUK_HEAP_STRING_BREAK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BREAK) +#define DUK_HTHREAD_STRING_BREAK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BREAK) +#define DUK_STRIDX_CASE 147 /* 'case' */ +#define DUK_HEAP_STRING_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CASE) +#define DUK_HTHREAD_STRING_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CASE) +#define DUK_STRIDX_CATCH 148 /* 'catch' */ +#define DUK_HEAP_STRING_CATCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CATCH) +#define DUK_HTHREAD_STRING_CATCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CATCH) +#define DUK_STRIDX_CONTINUE 149 /* 'continue' */ +#define DUK_HEAP_STRING_CONTINUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONTINUE) +#define DUK_HTHREAD_STRING_CONTINUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONTINUE) +#define DUK_STRIDX_DEBUGGER 150 /* 'debugger' */ +#define DUK_HEAP_STRING_DEBUGGER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEBUGGER) +#define DUK_HTHREAD_STRING_DEBUGGER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEBUGGER) +#define DUK_STRIDX_DEFAULT 151 /* 'default' */ +#define DUK_HEAP_STRING_DEFAULT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFAULT) +#define DUK_HTHREAD_STRING_DEFAULT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFAULT) +#define DUK_STRIDX_DELETE 152 /* 'delete' */ +#define DUK_HEAP_STRING_DELETE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE) +#define DUK_HTHREAD_STRING_DELETE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE) +#define DUK_STRIDX_DO 153 /* 'do' */ +#define DUK_HEAP_STRING_DO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DO) +#define DUK_HTHREAD_STRING_DO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DO) +#define DUK_STRIDX_ELSE 154 /* 'else' */ +#define DUK_HEAP_STRING_ELSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ELSE) +#define DUK_HTHREAD_STRING_ELSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ELSE) +#define DUK_STRIDX_FINALLY 155 /* 'finally' */ +#define DUK_HEAP_STRING_FINALLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FINALLY) +#define DUK_HTHREAD_STRING_FINALLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FINALLY) +#define DUK_STRIDX_FOR 156 /* 'for' */ +#define DUK_HEAP_STRING_FOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FOR) +#define DUK_HTHREAD_STRING_FOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FOR) +#define DUK_STRIDX_LC_FUNCTION 157 /* 'function' */ +#define DUK_HEAP_STRING_LC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FUNCTION) +#define DUK_HTHREAD_STRING_LC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FUNCTION) +#define DUK_STRIDX_IF 158 /* 'if' */ +#define DUK_HEAP_STRING_IF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IF) +#define DUK_HTHREAD_STRING_IF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IF) +#define DUK_STRIDX_IN 159 /* 'in' */ +#define DUK_HEAP_STRING_IN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IN) +#define DUK_HTHREAD_STRING_IN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IN) +#define DUK_STRIDX_INSTANCEOF 160 /* 'instanceof' */ +#define DUK_HEAP_STRING_INSTANCEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INSTANCEOF) +#define DUK_HTHREAD_STRING_INSTANCEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INSTANCEOF) +#define DUK_STRIDX_NEW 161 /* 'new' */ +#define DUK_HEAP_STRING_NEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEW) +#define DUK_HTHREAD_STRING_NEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEW) +#define DUK_STRIDX_RETURN 162 /* 'return' */ +#define DUK_HEAP_STRING_RETURN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RETURN) +#define DUK_HTHREAD_STRING_RETURN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RETURN) +#define DUK_STRIDX_SWITCH 163 /* 'switch' */ +#define DUK_HEAP_STRING_SWITCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SWITCH) +#define DUK_HTHREAD_STRING_SWITCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SWITCH) +#define DUK_STRIDX_THIS 164 /* 'this' */ +#define DUK_HEAP_STRING_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THIS) +#define DUK_HTHREAD_STRING_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THIS) +#define DUK_STRIDX_THROW 165 /* 'throw' */ +#define DUK_HEAP_STRING_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THROW) +#define DUK_HTHREAD_STRING_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THROW) +#define DUK_STRIDX_TRY 166 /* 'try' */ +#define DUK_HEAP_STRING_TRY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRY) +#define DUK_HTHREAD_STRING_TRY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRY) +#define DUK_STRIDX_TYPEOF 167 /* 'typeof' */ +#define DUK_HEAP_STRING_TYPEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPEOF) +#define DUK_HTHREAD_STRING_TYPEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPEOF) +#define DUK_STRIDX_VAR 168 /* 'var' */ +#define DUK_HEAP_STRING_VAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VAR) +#define DUK_HTHREAD_STRING_VAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VAR) +#define DUK_STRIDX_CONST 169 /* 'const' */ +#define DUK_HEAP_STRING_CONST(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONST) +#define DUK_HTHREAD_STRING_CONST(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONST) +#define DUK_STRIDX_VOID 170 /* 'void' */ +#define DUK_HEAP_STRING_VOID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VOID) +#define DUK_HTHREAD_STRING_VOID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VOID) +#define DUK_STRIDX_WHILE 171 /* 'while' */ +#define DUK_HEAP_STRING_WHILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WHILE) +#define DUK_HTHREAD_STRING_WHILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WHILE) +#define DUK_STRIDX_WITH 172 /* 'with' */ +#define DUK_HEAP_STRING_WITH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WITH) +#define DUK_HTHREAD_STRING_WITH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WITH) +#define DUK_STRIDX_CLASS 173 /* 'class' */ +#define DUK_HEAP_STRING_CLASS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLASS) +#define DUK_HTHREAD_STRING_CLASS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLASS) +#define DUK_STRIDX_ENUM 174 /* 'enum' */ +#define DUK_HEAP_STRING_ENUM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUM) +#define DUK_HTHREAD_STRING_ENUM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUM) +#define DUK_STRIDX_EXPORT 175 /* 'export' */ +#define DUK_HEAP_STRING_EXPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORT) +#define DUK_HTHREAD_STRING_EXPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORT) +#define DUK_STRIDX_EXTENDS 176 /* 'extends' */ +#define DUK_HEAP_STRING_EXTENDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXTENDS) +#define DUK_HTHREAD_STRING_EXTENDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXTENDS) +#define DUK_STRIDX_IMPORT 177 /* 'import' */ +#define DUK_HEAP_STRING_IMPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPORT) +#define DUK_HTHREAD_STRING_IMPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPORT) +#define DUK_STRIDX_SUPER 178 /* 'super' */ +#define DUK_HEAP_STRING_SUPER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUPER) +#define DUK_HTHREAD_STRING_SUPER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUPER) +#define DUK_STRIDX_LC_NULL 179 /* 'null' */ +#define DUK_HEAP_STRING_LC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NULL) +#define DUK_HTHREAD_STRING_LC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NULL) +#define DUK_STRIDX_TRUE 180 /* 'true' */ +#define DUK_HEAP_STRING_TRUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRUE) +#define DUK_HTHREAD_STRING_TRUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRUE) +#define DUK_STRIDX_FALSE 181 /* 'false' */ +#define DUK_HEAP_STRING_FALSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FALSE) +#define DUK_HTHREAD_STRING_FALSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FALSE) +#define DUK_STRIDX_IMPLEMENTS 182 /* 'implements' */ +#define DUK_HEAP_STRING_IMPLEMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPLEMENTS) +#define DUK_HTHREAD_STRING_IMPLEMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPLEMENTS) +#define DUK_STRIDX_INTERFACE 183 /* 'interface' */ +#define DUK_HEAP_STRING_INTERFACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INTERFACE) +#define DUK_HTHREAD_STRING_INTERFACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INTERFACE) +#define DUK_STRIDX_LET 184 /* 'let' */ +#define DUK_HEAP_STRING_LET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LET) +#define DUK_HTHREAD_STRING_LET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LET) +#define DUK_STRIDX_PACKAGE 185 /* 'package' */ +#define DUK_HEAP_STRING_PACKAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PACKAGE) +#define DUK_HTHREAD_STRING_PACKAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PACKAGE) +#define DUK_STRIDX_PRIVATE 186 /* 'private' */ +#define DUK_HEAP_STRING_PRIVATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PRIVATE) +#define DUK_HTHREAD_STRING_PRIVATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PRIVATE) +#define DUK_STRIDX_PROTECTED 187 /* 'protected' */ +#define DUK_HEAP_STRING_PROTECTED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTECTED) +#define DUK_HTHREAD_STRING_PROTECTED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTECTED) +#define DUK_STRIDX_PUBLIC 188 /* 'public' */ +#define DUK_HEAP_STRING_PUBLIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PUBLIC) +#define DUK_HTHREAD_STRING_PUBLIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PUBLIC) +#define DUK_STRIDX_STATIC 189 /* 'static' */ +#define DUK_HEAP_STRING_STATIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STATIC) +#define DUK_HTHREAD_STRING_STATIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STATIC) +#define DUK_STRIDX_YIELD 190 /* 'yield' */ +#define DUK_HEAP_STRING_YIELD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_YIELD) +#define DUK_HTHREAD_STRING_YIELD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_YIELD) + +#define DUK_HEAP_NUM_STRINGS 191 +#define DUK_STRIDX_START_RESERVED 146 +#define DUK_STRIDX_START_STRICT_RESERVED 182 +#define DUK_STRIDX_END_RESERVED 191 /* exclusive endpoint */ + +/* To convert a heap stridx to a token number, subtract + * DUK_STRIDX_START_RESERVED and add DUK_TOK_START_RESERVED. + */ +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const duk_uint8_t duk_strings_data[1049]; +#endif /* !DUK_SINGLE_FILE */ +#define DUK_STRDATA_MAX_STRLEN 17 +#define DUK_STRDATA_DATA_LENGTH 1049 +#endif /* DUK_USE_ROM_STRINGS */ + +#if defined(DUK_USE_ROM_OBJECTS) +#error ROM support not enabled, rerun make_dist.py with --rom-support +#else +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_function_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_boolean_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_number_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_error_constructor_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_type_error_thrower(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_proxy_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_pointer_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_arraybuffer_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_dataview_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_constructor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_eval(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_parse_int(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_parse_float(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_is_nan(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_is_finite(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_decode_uri(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_decode_uri_component(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_encode_uri(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_encode_uri_component(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_escape(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_unescape(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_require(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_getprototype_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_setprototype_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_get_own_property_descriptor(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_keys_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_create(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_define_property(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_define_properties(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_seal_freeze_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_prevent_extensions(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_is_sealed_frozen_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_is_extensible(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_to_string(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_to_locale_string(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_value_of(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_has_own_property(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_is_prototype_of(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_property_is_enumerable(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_to_string(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_apply(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_call(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_bind(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_constructor_is_array(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_to_string(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_join_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_concat(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_pop(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_push(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_reverse(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_shift(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_slice(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_sort(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_splice(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_unshift(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_indexof_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_iter_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_reduce_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_constructor_from_char_code(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_to_string(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_char_at(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_char_code_at(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_concat(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_indexof_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_locale_compare(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_match(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_replace(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_search(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_slice(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_split(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_substring(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_caseconv_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_trim(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_substr(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_boolean_prototype_tostring_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_string(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_locale_string(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_value_of(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_fixed(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_exponential(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_precision(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor_parse(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor_utc(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor_now(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_tostring_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_to_json(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_value_of(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_get_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_get_timezone_offset(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_set_time(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_set_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_exec(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_test(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_to_string(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_stack_getter(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_stack_setter(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_filename_getter(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_filename_setter(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_linenumber_getter(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_linenumber_setter(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_to_string(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_onearg_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_twoarg_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_max(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_min(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_random(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_json_object_parse(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_json_object_stringify(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_info(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_act(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_gc(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_fin(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_enc(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_dec(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_compact(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_yield(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_resume(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_current(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_prototype_tostring_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_pointer_prototype_tostring_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_prototype_fmt(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_prototype_raw(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_logger_prototype_log_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_arraybuffer_isview(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_slice_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_readfield(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_writefield(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_typedarray_set(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_concat(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_is_encoding(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_is_buffer(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_byte_length(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_compare_shared(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_tojson(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_fill(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_copy(duk_context *ctx); +DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_write(duk_context *ctx); +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const duk_c_function duk_bi_native_functions[149]; +#endif /* !DUK_SINGLE_FILE */ +#if defined(DUK_USE_BUILTIN_INITJS) +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const duk_uint8_t duk_initjs_data[204]; +#endif /* !DUK_SINGLE_FILE */ +#define DUK_BUILTIN_INITJS_DATA_LENGTH 204 +#endif /* DUK_USE_BUILTIN_INITJS */ +#define DUK_BIDX_GLOBAL 0 +#define DUK_BIDX_GLOBAL_ENV 1 +#define DUK_BIDX_OBJECT_CONSTRUCTOR 2 +#define DUK_BIDX_OBJECT_PROTOTYPE 3 +#define DUK_BIDX_FUNCTION_CONSTRUCTOR 4 +#define DUK_BIDX_FUNCTION_PROTOTYPE 5 +#define DUK_BIDX_ARRAY_CONSTRUCTOR 6 +#define DUK_BIDX_ARRAY_PROTOTYPE 7 +#define DUK_BIDX_STRING_CONSTRUCTOR 8 +#define DUK_BIDX_STRING_PROTOTYPE 9 +#define DUK_BIDX_BOOLEAN_CONSTRUCTOR 10 +#define DUK_BIDX_BOOLEAN_PROTOTYPE 11 +#define DUK_BIDX_NUMBER_CONSTRUCTOR 12 +#define DUK_BIDX_NUMBER_PROTOTYPE 13 +#define DUK_BIDX_DATE_CONSTRUCTOR 14 +#define DUK_BIDX_DATE_PROTOTYPE 15 +#define DUK_BIDX_REGEXP_CONSTRUCTOR 16 +#define DUK_BIDX_REGEXP_PROTOTYPE 17 +#define DUK_BIDX_ERROR_CONSTRUCTOR 18 +#define DUK_BIDX_ERROR_PROTOTYPE 19 +#define DUK_BIDX_EVAL_ERROR_CONSTRUCTOR 20 +#define DUK_BIDX_EVAL_ERROR_PROTOTYPE 21 +#define DUK_BIDX_RANGE_ERROR_CONSTRUCTOR 22 +#define DUK_BIDX_RANGE_ERROR_PROTOTYPE 23 +#define DUK_BIDX_REFERENCE_ERROR_CONSTRUCTOR 24 +#define DUK_BIDX_REFERENCE_ERROR_PROTOTYPE 25 +#define DUK_BIDX_SYNTAX_ERROR_CONSTRUCTOR 26 +#define DUK_BIDX_SYNTAX_ERROR_PROTOTYPE 27 +#define DUK_BIDX_TYPE_ERROR_CONSTRUCTOR 28 +#define DUK_BIDX_TYPE_ERROR_PROTOTYPE 29 +#define DUK_BIDX_URI_ERROR_CONSTRUCTOR 30 +#define DUK_BIDX_URI_ERROR_PROTOTYPE 31 +#define DUK_BIDX_MATH 32 +#define DUK_BIDX_JSON 33 +#define DUK_BIDX_TYPE_ERROR_THROWER 34 +#define DUK_BIDX_PROXY_CONSTRUCTOR 35 +#define DUK_BIDX_DUKTAPE 36 +#define DUK_BIDX_THREAD_CONSTRUCTOR 37 +#define DUK_BIDX_THREAD_PROTOTYPE 38 +#define DUK_BIDX_BUFFER_CONSTRUCTOR 39 +#define DUK_BIDX_BUFFER_PROTOTYPE 40 +#define DUK_BIDX_POINTER_CONSTRUCTOR 41 +#define DUK_BIDX_POINTER_PROTOTYPE 42 +#define DUK_BIDX_LOGGER_CONSTRUCTOR 43 +#define DUK_BIDX_LOGGER_PROTOTYPE 44 +#define DUK_BIDX_DOUBLE_ERROR 45 +#define DUK_BIDX_ARRAYBUFFER_CONSTRUCTOR 46 +#define DUK_BIDX_ARRAYBUFFER_PROTOTYPE 47 +#define DUK_BIDX_DATAVIEW_CONSTRUCTOR 48 +#define DUK_BIDX_DATAVIEW_PROTOTYPE 49 +#define DUK_BIDX_TYPEDARRAY_PROTOTYPE 50 +#define DUK_BIDX_INT8ARRAY_CONSTRUCTOR 51 +#define DUK_BIDX_INT8ARRAY_PROTOTYPE 52 +#define DUK_BIDX_UINT8ARRAY_CONSTRUCTOR 53 +#define DUK_BIDX_UINT8ARRAY_PROTOTYPE 54 +#define DUK_BIDX_UINT8CLAMPEDARRAY_CONSTRUCTOR 55 +#define DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE 56 +#define DUK_BIDX_INT16ARRAY_CONSTRUCTOR 57 +#define DUK_BIDX_INT16ARRAY_PROTOTYPE 58 +#define DUK_BIDX_UINT16ARRAY_CONSTRUCTOR 59 +#define DUK_BIDX_UINT16ARRAY_PROTOTYPE 60 +#define DUK_BIDX_INT32ARRAY_CONSTRUCTOR 61 +#define DUK_BIDX_INT32ARRAY_PROTOTYPE 62 +#define DUK_BIDX_UINT32ARRAY_CONSTRUCTOR 63 +#define DUK_BIDX_UINT32ARRAY_PROTOTYPE 64 +#define DUK_BIDX_FLOAT32ARRAY_CONSTRUCTOR 65 +#define DUK_BIDX_FLOAT32ARRAY_PROTOTYPE 66 +#define DUK_BIDX_FLOAT64ARRAY_CONSTRUCTOR 67 +#define DUK_BIDX_FLOAT64ARRAY_PROTOTYPE 68 +#define DUK_BIDX_NODEJS_BUFFER_CONSTRUCTOR 69 +#define DUK_BIDX_NODEJS_BUFFER_PROTOTYPE 70 +#define DUK_NUM_BUILTINS 71 +#define DUK_NUM_BIDX_BUILTINS 71 +#define DUK_NUM_ALL_BUILTINS 71 +#if defined(DUK_USE_DOUBLE_LE) +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const duk_uint8_t duk_builtins_data[3833]; +#endif /* !DUK_SINGLE_FILE */ +#define DUK_BUILTINS_DATA_LENGTH 3833 +#elif defined(DUK_USE_DOUBLE_BE) +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const duk_uint8_t duk_builtins_data[3833]; +#endif /* !DUK_SINGLE_FILE */ +#define DUK_BUILTINS_DATA_LENGTH 3833 +#elif defined(DUK_USE_DOUBLE_ME) +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const duk_uint8_t duk_builtins_data[3833]; +#endif /* !DUK_SINGLE_FILE */ +#define DUK_BUILTINS_DATA_LENGTH 3833 +#else +#error invalid endianness defines +#endif +#endif /* DUK_USE_ROM_OBJECTS */ +#endif /* DUK_BUILTINS_H_INCLUDED */ + +/* + * Utilities + */ + +#ifndef DUK_UTIL_H_INCLUDED +#define DUK_UTIL_H_INCLUDED + +#define DUK_UTIL_MIN_HASH_PRIME 17 /* must match genhashsizes.py */ + +#define DUK_UTIL_GET_HASH_PROBE_STEP(hash) (duk_util_probe_steps[(hash) & 0x1f]) + +/* + * Endian conversion + */ + +#if defined(DUK_USE_INTEGER_LE) +#define DUK_HTON32(x) DUK_BSWAP32((x)) +#define DUK_NTOH32(x) DUK_BSWAP32((x)) +#define DUK_HTON16(x) DUK_BSWAP16((x)) +#define DUK_NTOH16(x) DUK_BSWAP16((x)) +#elif defined(DUK_USE_INTEGER_BE) +#define DUK_HTON32(x) (x) +#define DUK_NTOH32(x) (x) +#define DUK_HTON16(x) (x) +#define DUK_NTOH16(x) (x) +#else +#error internal error, endianness defines broken +#endif + +/* + * Bitstream decoder + */ + +struct duk_bitdecoder_ctx { + const duk_uint8_t *data; + duk_size_t offset; + duk_size_t length; + duk_uint32_t currval; + duk_small_int_t currbits; +}; + +/* + * Bitstream encoder + */ + +struct duk_bitencoder_ctx { + duk_uint8_t *data; + duk_size_t offset; + duk_size_t length; + duk_uint32_t currval; + duk_small_int_t currbits; + duk_small_int_t truncated; +}; + +/* + * Raw write/read macros for big endian, unaligned basic values. + * Caller ensures there's enough space. The macros update the pointer + * argument automatically on resizes. The idiom seems a bit odd, but + * leads to compact code. + */ + +#define DUK_RAW_WRITE_U8(ptr,val) do { \ + *(ptr)++ = (duk_uint8_t) (val); \ + } while (0) +#define DUK_RAW_WRITE_U16_BE(ptr,val) duk_raw_write_u16_be(&(ptr), (duk_uint16_t) (val)) +#define DUK_RAW_WRITE_U32_BE(ptr,val) duk_raw_write_u32_be(&(ptr), (duk_uint32_t) (val)) +#define DUK_RAW_WRITE_DOUBLE_BE(ptr,val) duk_raw_write_double_be(&(ptr), (duk_double_t) (val)) +#define DUK_RAW_WRITE_XUTF8(ptr,val) do { \ + /* 'ptr' is evaluated both as LHS and RHS. */ \ + duk_uint8_t *duk__ptr; \ + duk_small_int_t duk__len; \ + duk__ptr = (duk_uint8_t *) (ptr); \ + duk__len = duk_unicode_encode_xutf8((duk_ucodepoint_t) (val), duk__ptr); \ + duk__ptr += duk__len; \ + (ptr) = duk__ptr; \ + } while (0) +#define DUK_RAW_WRITE_CESU8(ptr,val) do { \ + /* 'ptr' is evaluated both as LHS and RHS. */ \ + duk_uint8_t *duk__ptr; \ + duk_small_int_t duk__len; \ + duk__ptr = (duk_uint8_t *) (ptr); \ + duk__len = duk_unicode_encode_cesu8((duk_ucodepoint_t) (val), duk__ptr); \ + duk__ptr += duk__len; \ + (ptr) = duk__ptr; \ + } while (0) + +#define DUK_RAW_READ_U8(ptr) ((duk_uint8_t) (*(ptr)++)) +#define DUK_RAW_READ_U16_BE(ptr) duk_raw_read_u16_be(&(ptr)); +#define DUK_RAW_READ_U32_BE(ptr) duk_raw_read_u32_be(&(ptr)); +#define DUK_RAW_READ_DOUBLE_BE(ptr) duk_raw_read_double_be(&(ptr)); + +/* + * Buffer writer (dynamic buffer only) + * + * Helper for writing to a dynamic buffer with a concept of a "spare" area + * to reduce resizes. You can ensure there is enough space beforehand and + * then write for a while without further checks, relying on a stable data + * pointer. Spare handling is automatic so call sites only indicate how + * much data they need right now. + * + * There are several ways to write using bufwriter. The best approach + * depends mainly on how much performance matters over code footprint. + * The key issues are (1) ensuring there is space and (2) keeping the + * pointers consistent. Fast code should ensure space for multiple writes + * with one ensure call. Fastest inner loop code can temporarily borrow + * the 'p' pointer but must write it back eventually. + * + * Be careful to ensure all macro arguments (other than static pointers like + * 'thr' and 'bw_ctx') are evaluated exactly once, using temporaries if + * necessary (if that's not possible, there should be a note near the macro). + * Buffer write arguments often contain arithmetic etc so this is + * particularly important here. + */ + +/* XXX: Migrate bufwriter and other read/write helpers to its own header? */ + +struct duk_bufwriter_ctx { + duk_uint8_t *p; + duk_uint8_t *p_base; + duk_uint8_t *p_limit; + duk_hbuffer_dynamic *buf; +}; + +#define DUK_BW_SPARE_ADD 64 +#define DUK_BW_SPARE_SHIFT 4 /* 2^4 -> 1/16 = 6.25% spare */ + +/* Initialization and finalization (compaction), converting to other types. */ + +#define DUK_BW_INIT_PUSHBUF(thr,bw_ctx,sz) do { \ + duk_bw_init_pushbuf((thr), (bw_ctx), (sz)); \ + } while (0) +#define DUK_BW_INIT_WITHBUF(thr,bw_ctx,buf) do { \ + duk_bw_init((thr), (bw_ctx), (buf)); \ + } while (0) +#define DUK_BW_COMPACT(thr,bw_ctx) do { \ + /* Make underlying buffer compact to match DUK_BW_GET_SIZE(). */ \ + duk_bw_compact((thr), (bw_ctx)); \ + } while (0) +#define DUK_BW_PUSH_AS_STRING(thr,bw_ctx) do { \ + duk_push_lstring((duk_context *) (thr), \ + (const char *) (bw_ctx)->p_base, \ + (duk_size_t) ((bw_ctx)->p - (bw_ctx)->p_base)); \ + } while (0) +/* Pointers may be NULL for a while when 'buf' size is zero and before any + * ENSURE calls have been made. Once an ENSURE has been made, the pointers + * are required to be non-NULL so that it's always valid to use memcpy() and + * memmove(), even for zero size. + */ +#define DUK_BW_ASSERT_VALID_EXPR(thr,bw_ctx) \ + DUK_ASSERT_EXPR((bw_ctx) != NULL && \ + (bw_ctx)->buf != NULL && \ + ((DUK_HBUFFER_DYNAMIC_GET_SIZE((bw_ctx)->buf) == 0) || \ + ((bw_ctx)->p != NULL && \ + (bw_ctx)->p_base != NULL && \ + (bw_ctx)->p_limit != NULL && \ + (bw_ctx)->p_limit >= (bw_ctx)->p_base && \ + (bw_ctx)->p >= (bw_ctx)->p_base && \ + (bw_ctx)->p <= (bw_ctx)->p_limit))) +#define DUK_BW_ASSERT_VALID(thr,bw_ctx) do { \ + DUK_BW_ASSERT_VALID_EXPR((thr), (bw_ctx)); \ + } while (0) + +/* Working with the pointer and current size. */ + +#define DUK_BW_GET_PTR(thr,bw_ctx) \ + ((bw_ctx)->p) +#define DUK_BW_SET_PTR(thr,bw_ctx,ptr) do { \ + (bw_ctx)->p = (ptr); \ + } while (0) +#define DUK_BW_ADD_PTR(thr,bw_ctx,delta) do { \ + (bw_ctx)->p += (delta); \ + } while (0) +#define DUK_BW_GET_BASEPTR(thr,bw_ctx) \ + ((bw_ctx)->p_base) +#define DUK_BW_GET_LIMITPTR(thr,bw_ctx) \ + ((bw_ctx)->p_limit) +#define DUK_BW_GET_SIZE(thr,bw_ctx) \ + ((duk_size_t) ((bw_ctx)->p - (bw_ctx)->p_base)) +#define DUK_BW_SET_SIZE(thr,bw_ctx,sz) do { \ + DUK_ASSERT((duk_size_t) (sz) <= (duk_size_t) ((bw_ctx)->p - (bw_ctx)->p_base)); \ + (bw_ctx)->p = (bw_ctx)->p_base + (sz); \ + } while (0) +#define DUK_BW_RESET_SIZE(thr,bw_ctx) do { \ + /* Reset to zero size, keep current limit. */ \ + (bw_ctx)->p = (bw_ctx)->p_base; \ + } while (0) +#define DUK_BW_GET_BUFFER(thr,bw_ctx) \ + ((bw_ctx)->buf) + +/* Ensuring (reserving) space. */ + +#define DUK_BW_ENSURE(thr,bw_ctx,sz) do { \ + duk_size_t duk__sz, duk__space; \ + DUK_BW_ASSERT_VALID((thr), (bw_ctx)); \ + duk__sz = (sz); \ + duk__space = (duk_size_t) ((bw_ctx)->p_limit - (bw_ctx)->p); \ + if (duk__space < duk__sz) { \ + (void) duk_bw_resize((thr), (bw_ctx), duk__sz); \ + } \ + } while (0) +/* NOTE: Multiple evaluation of 'ptr' in this macro. */ +/* XXX: Rework to use an always-inline function? */ +#define DUK_BW_ENSURE_RAW(thr,bw_ctx,sz,ptr) \ + (((duk_size_t) ((bw_ctx)->p_limit - (ptr)) >= (sz)) ? \ + (ptr) : \ + ((bw_ctx)->p = (ptr), duk_bw_resize((thr),(bw_ctx),(sz)))) +#define DUK_BW_ENSURE_GETPTR(thr,bw_ctx,sz) \ + DUK_BW_ENSURE_RAW((thr), (bw_ctx), (sz), (bw_ctx)->p) +#define DUK_BW_ASSERT_SPACE_EXPR(thr,bw_ctx,sz) \ + (DUK_BW_ASSERT_VALID_EXPR((thr), (bw_ctx)), \ + DUK_ASSERT_EXPR((duk_size_t) ((bw_ctx)->p_limit - (bw_ctx)->p) >= (duk_size_t) (sz))) +#define DUK_BW_ASSERT_SPACE(thr,bw_ctx,sz) do { \ + DUK_BW_ASSERT_SPACE_EXPR((thr), (bw_ctx), (sz)); \ + } while (0) + +/* Miscellaneous. */ + +#define DUK_BW_SETPTR_AND_COMPACT(thr,bw_ctx,ptr) do { \ + (bw_ctx)->p = (ptr); \ + duk_bw_compact((thr), (bw_ctx)); \ + } while (0) + +/* Fast write calls which assume you control the spare beforehand. + * Multibyte write variants exist and use a temporary write pointer + * because byte writes alias with anything: with a stored pointer + * explicit pointer load/stores get generated (e.g. gcc -Os). + */ + +#define DUK_BW_WRITE_RAW_U8(thr,bw_ctx,val) do { \ + DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 1); \ + *(bw_ctx)->p++ = (duk_uint8_t) (val); \ + } while (0) +#define DUK_BW_WRITE_RAW_U8_2(thr,bw_ctx,val1,val2) do { \ + duk_uint8_t *duk__p; \ + DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 2); \ + duk__p = (bw_ctx)->p; \ + *duk__p++ = (duk_uint8_t) (val1); \ + *duk__p++ = (duk_uint8_t) (val2); \ + (bw_ctx)->p = duk__p; \ + } while (0) +#define DUK_BW_WRITE_RAW_U8_3(thr,bw_ctx,val1,val2,val3) do { \ + duk_uint8_t *duk__p; \ + DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 3); \ + duk__p = (bw_ctx)->p; \ + *duk__p++ = (duk_uint8_t) (val1); \ + *duk__p++ = (duk_uint8_t) (val2); \ + *duk__p++ = (duk_uint8_t) (val3); \ + (bw_ctx)->p = duk__p; \ + } while (0) +#define DUK_BW_WRITE_RAW_U8_4(thr,bw_ctx,val1,val2,val3,val4) do { \ + duk_uint8_t *duk__p; \ + DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 4); \ + duk__p = (bw_ctx)->p; \ + *duk__p++ = (duk_uint8_t) (val1); \ + *duk__p++ = (duk_uint8_t) (val2); \ + *duk__p++ = (duk_uint8_t) (val3); \ + *duk__p++ = (duk_uint8_t) (val4); \ + (bw_ctx)->p = duk__p; \ + } while (0) +#define DUK_BW_WRITE_RAW_U8_5(thr,bw_ctx,val1,val2,val3,val4,val5) do { \ + duk_uint8_t *duk__p; \ + DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 5); \ + duk__p = (bw_ctx)->p; \ + *duk__p++ = (duk_uint8_t) (val1); \ + *duk__p++ = (duk_uint8_t) (val2); \ + *duk__p++ = (duk_uint8_t) (val3); \ + *duk__p++ = (duk_uint8_t) (val4); \ + *duk__p++ = (duk_uint8_t) (val5); \ + (bw_ctx)->p = duk__p; \ + } while (0) +#define DUK_BW_WRITE_RAW_U8_6(thr,bw_ctx,val1,val2,val3,val4,val5,val6) do { \ + duk_uint8_t *duk__p; \ + DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 6); \ + duk__p = (bw_ctx)->p; \ + *duk__p++ = (duk_uint8_t) (val1); \ + *duk__p++ = (duk_uint8_t) (val2); \ + *duk__p++ = (duk_uint8_t) (val3); \ + *duk__p++ = (duk_uint8_t) (val4); \ + *duk__p++ = (duk_uint8_t) (val5); \ + *duk__p++ = (duk_uint8_t) (val6); \ + (bw_ctx)->p = duk__p; \ + } while (0) +#define DUK_BW_WRITE_RAW_XUTF8(thr,bw_ctx,cp) do { \ + duk_ucodepoint_t duk__cp; \ + duk_small_int_t duk__enc_len; \ + duk__cp = (cp); \ + DUK_BW_ASSERT_SPACE((thr), (bw_ctx), duk_unicode_get_xutf8_length(duk__cp)); \ + duk__enc_len = duk_unicode_encode_xutf8(duk__cp, (bw_ctx)->p); \ + (bw_ctx)->p += duk__enc_len; \ + } while (0) +#define DUK_BW_WRITE_RAW_CESU8(thr,bw_ctx,cp) do { \ + duk_ucodepoint_t duk__cp; \ + duk_small_int_t duk__enc_len; \ + duk__cp = (duk_ucodepoint_t) (cp); \ + DUK_BW_ASSERT_SPACE((thr), (bw_ctx), duk_unicode_get_cesu8_length(duk__cp)); \ + duk__enc_len = duk_unicode_encode_cesu8(duk__cp, (bw_ctx)->p); \ + (bw_ctx)->p += duk__enc_len; \ + } while (0) +/* XXX: add temporary duk__p pointer here too; sharing */ +#define DUK_BW_WRITE_RAW_BYTES(thr,bw_ctx,valptr,valsz) do { \ + const void *duk__valptr; \ + duk_size_t duk__valsz; \ + duk__valptr = (const void *) (valptr); \ + duk__valsz = (duk_size_t) (valsz); \ + DUK_MEMCPY((void *) ((bw_ctx)->p), duk__valptr, duk__valsz); \ + (bw_ctx)->p += duk__valsz; \ + } while (0) +#define DUK_BW_WRITE_RAW_CSTRING(thr,bw_ctx,val) do { \ + const duk_uint8_t *duk__val; \ + duk_size_t duk__val_len; \ + duk__val = (const duk_uint8_t *) (val); \ + duk__val_len = DUK_STRLEN((const char *) duk__val); \ + DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) duk__val, duk__val_len); \ + (bw_ctx)->p += duk__val_len; \ + } while (0) +#define DUK_BW_WRITE_RAW_HSTRING(thr,bw_ctx,val) do { \ + duk_size_t duk__val_len; \ + duk__val_len = DUK_HSTRING_GET_BYTELEN((val)); \ + DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HSTRING_GET_DATA((val)), duk__val_len); \ + (bw_ctx)->p += duk__val_len; \ + } while (0) +#define DUK_BW_WRITE_RAW_HBUFFER(thr,bw_ctx,val) do { \ + duk_size_t duk__val_len; \ + duk__val_len = DUK_HBUFFER_GET_SIZE((val)); \ + DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \ + (bw_ctx)->p += duk__val_len; \ + } while (0) +#define DUK_BW_WRITE_RAW_HBUFFER_FIXED(thr,bw_ctx,val) do { \ + duk_size_t duk__val_len; \ + duk__val_len = DUK_HBUFFER_FIXED_GET_SIZE((val)); \ + DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_FIXED_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \ + (bw_ctx)->p += duk__val_len; \ + } while (0) +#define DUK_BW_WRITE_RAW_HBUFFER_DYNAMIC(thr,bw_ctx,val) do { \ + duk_size_t duk__val_len; \ + duk__val_len = DUK_HBUFFER_DYNAMIC_GET_SIZE((val)); \ + DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \ + (bw_ctx)->p += duk__val_len; \ + } while (0) + +/* Append bytes from a slice already in the buffer. */ +#define DUK_BW_WRITE_RAW_SLICE(thr,bw,dst_off,dst_len) \ + duk_bw_write_raw_slice((thr), (bw), (dst_off), (dst_len)) + +/* Insert bytes in the middle of the buffer from an external buffer. */ +#define DUK_BW_INSERT_RAW_BYTES(thr,bw,dst_off,buf,len) \ + duk_bw_insert_raw_bytes((thr), (bw), (dst_off), (buf), (len)) + +/* Insert bytes in the middle of the buffer from a slice already + * in the buffer. Source offset is interpreted "before" the operation. + */ +#define DUK_BW_INSERT_RAW_SLICE(thr,bw,dst_off,src_off,len) \ + duk_bw_insert_raw_slice((thr), (bw), (dst_off), (src_off), (len)) + +/* Insert a reserved area somewhere in the buffer; caller fills it. + * Evaluates to a (duk_uint_t *) pointing to the start of the reserved + * area for convenience. + */ +#define DUK_BW_INSERT_RAW_AREA(thr,bw,off,len) \ + duk_bw_insert_raw_area((thr), (bw), (off), (len)) + +/* Remove a slice from inside buffer. */ +#define DUK_BW_REMOVE_RAW_SLICE(thr,bw,off,len) \ + duk_bw_remove_raw_slice((thr), (bw), (off), (len)) + +/* Safe write calls which will ensure space first. */ + +#define DUK_BW_WRITE_ENSURE_U8(thr,bw_ctx,val) do { \ + DUK_BW_ENSURE((thr), (bw_ctx), 1); \ + DUK_BW_WRITE_RAW_U8((thr), (bw_ctx), (val)); \ + } while (0) +#define DUK_BW_WRITE_ENSURE_U8_2(thr,bw_ctx,val1,val2) do { \ + DUK_BW_ENSURE((thr), (bw_ctx), 2); \ + DUK_BW_WRITE_RAW_U8_2((thr), (bw_ctx), (val1), (val2)); \ + } while (0) +#define DUK_BW_WRITE_ENSURE_U8_3(thr,bw_ctx,val1,val2,val3) do { \ + DUK_BW_ENSURE((thr), (bw_ctx), 3); \ + DUK_BW_WRITE_RAW_U8_3((thr), (bw_ctx), (val1), (val2), (val3)); \ + } while (0) +#define DUK_BW_WRITE_ENSURE_U8_4(thr,bw_ctx,val1,val2,val3,val4) do { \ + DUK_BW_ENSURE((thr), (bw_ctx), 4); \ + DUK_BW_WRITE_RAW_U8_4((thr), (bw_ctx), (val1), (val2), (val3), (val4)); \ + } while (0) +#define DUK_BW_WRITE_ENSURE_U8_5(thr,bw_ctx,val1,val2,val3,val4,val5) do { \ + DUK_BW_ENSURE((thr), (bw_ctx), 5); \ + DUK_BW_WRITE_RAW_U8_5((thr), (bw_ctx), (val1), (val2), (val3), (val4), (val5)); \ + } while (0) +#define DUK_BW_WRITE_ENSURE_U8_6(thr,bw_ctx,val1,val2,val3,val4,val5,val6) do { \ + DUK_BW_ENSURE((thr), (bw_ctx), 6); \ + DUK_BW_WRITE_RAW_U8_6((thr), (bw_ctx), (val1), (val2), (val3), (val4), (val5), (val6)); \ + } while (0) +#define DUK_BW_WRITE_ENSURE_XUTF8(thr,bw_ctx,cp) do { \ + DUK_BW_ENSURE((thr), (bw_ctx), DUK_UNICODE_MAX_XUTF8_LENGTH); \ + DUK_BW_WRITE_RAW_XUTF8((thr), (bw_ctx), (cp)); \ + } while (0) +#define DUK_BW_WRITE_ENSURE_CESU8(thr,bw_ctx,cp) do { \ + DUK_BW_ENSURE((thr), (bw_ctx), DUK_UNICODE_MAX_CESU8_LENGTH); \ + DUK_BW_WRITE_RAW_CESU8((thr), (bw_ctx), (cp)); \ + } while (0) +/* XXX: add temporary duk__p pointer here too; sharing */ +#define DUK_BW_WRITE_ENSURE_BYTES(thr,bw_ctx,valptr,valsz) do { \ + const void *duk__valptr; \ + duk_size_t duk__valsz; \ + duk__valptr = (const void *) (valptr); \ + duk__valsz = (duk_size_t) (valsz); \ + DUK_BW_ENSURE((thr), (bw_ctx), duk__valsz); \ + DUK_MEMCPY((void *) ((bw_ctx)->p), duk__valptr, duk__valsz); \ + (bw_ctx)->p += duk__valsz; \ + } while (0) +#define DUK_BW_WRITE_ENSURE_CSTRING(thr,bw_ctx,val) do { \ + const duk_uint8_t *duk__val; \ + duk_size_t duk__val_len; \ + duk__val = (const duk_uint8_t *) (val); \ + duk__val_len = DUK_STRLEN((const char *) duk__val); \ + DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \ + DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) duk__val, duk__val_len); \ + (bw_ctx)->p += duk__val_len; \ + } while (0) +#define DUK_BW_WRITE_ENSURE_HSTRING(thr,bw_ctx,val) do { \ + duk_size_t duk__val_len; \ + duk__val_len = DUK_HSTRING_GET_BYTELEN((val)); \ + DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \ + DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HSTRING_GET_DATA((val)), duk__val_len); \ + (bw_ctx)->p += duk__val_len; \ + } while (0) +#define DUK_BW_WRITE_ENSURE_HBUFFER(thr,bw_ctx,val) do { \ + duk_size_t duk__val_len; \ + duk__val_len = DUK_HBUFFER_GET_SIZE((val)); \ + DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \ + DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \ + (bw_ctx)->p += duk__val_len; \ + } while (0) +#define DUK_BW_WRITE_ENSURE_HBUFFER_FIXED(thr,bw_ctx,val) do { \ + duk_size_t duk__val_len; \ + duk__val_len = DUK_HBUFFER_FIXED_GET_SIZE((val)); \ + DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \ + DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_FIXED_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \ + (bw_ctx)->p += duk__val_len; \ + } while (0) +#define DUK_BW_WRITE_ENSURE_HBUFFER_DYNAMIC(thr,bw_ctx,val) do { \ + duk_size_t duk__val_len; \ + duk__val_len = DUK_HBUFFER_DYNAMIC_GET_SIZE((val)); \ + DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \ + DUK_MEMCPY((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \ + (bw_ctx)->p += duk__val_len; \ + } while (0) + +#define DUK_BW_WRITE_ENSURE_SLICE(thr,bw,dst_off,dst_len) \ + duk_bw_write_ensure_slice((thr), (bw), (dst_off), (dst_len)) +#define DUK_BW_INSERT_ENSURE_BYTES(thr,bw,dst_off,buf,len) \ + duk_bw_insert_ensure_bytes((thr), (bw), (dst_off), (buf), (len)) +#define DUK_BW_INSERT_ENSURE_SLICE(thr,bw,dst_off,src_off,len) \ + duk_bw_insert_ensure_slice((thr), (bw), (dst_off), (src_off), (len)) +#define DUK_BW_INSERT_ENSURE_AREA(thr,bw,off,len) \ + /* Evaluates to (duk_uint8_t *) pointing to start of area. */ \ + duk_bw_insert_ensure_area((thr), (bw), (off), (len)) +#define DUK_BW_REMOVE_ENSURE_SLICE(thr,bw,off,len) \ + /* No difference between raw/ensure because the buffer shrinks. */ \ + DUK_BW_REMOVE_RAW_SLICE((thr), (bw), (off), (len)) + +/* + * Externs and prototypes + */ + +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const duk_uint8_t duk_lc_digits[36]; +DUK_INTERNAL_DECL const duk_uint8_t duk_uc_nybbles[16]; +DUK_INTERNAL_DECL const duk_int8_t duk_hex_dectab[256]; +#if defined(DUK_USE_HEX_FASTPATH) +DUK_INTERNAL_DECL const duk_int16_t duk_hex_dectab_shift4[256]; +DUK_INTERNAL_DECL const duk_uint16_t duk_hex_enctab[256]; +#endif +#if defined(DUK_USE_BASE64_FASTPATH) +DUK_INTERNAL_DECL const duk_uint8_t duk_base64_enctab[64]; +DUK_INTERNAL_DECL const duk_int8_t duk_base64_dectab[256]; +#endif +#endif /* !DUK_SINGLE_FILE */ + +/* Note: assumes that duk_util_probe_steps size is 32 */ +#if defined(DUK_USE_HOBJECT_HASH_PART) || defined(DUK_USE_STRTAB_PROBE) +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL duk_uint8_t duk_util_probe_steps[32]; +#endif /* !DUK_SINGLE_FILE */ +#endif + +#if defined(DUK_USE_STRHASH_DENSE) +DUK_INTERNAL_DECL duk_uint32_t duk_util_hashbytes(const duk_uint8_t *data, duk_size_t len, duk_uint32_t seed); +#endif + +#if defined(DUK_USE_HOBJECT_HASH_PART) || defined(DUK_USE_STRTAB_PROBE) +DUK_INTERNAL_DECL duk_uint32_t duk_util_get_hash_prime(duk_uint32_t size); +#endif + +DUK_INTERNAL_DECL duk_int32_t duk_bd_decode(duk_bitdecoder_ctx *ctx, duk_small_int_t bits); +DUK_INTERNAL_DECL duk_small_int_t duk_bd_decode_flag(duk_bitdecoder_ctx *ctx); +DUK_INTERNAL_DECL duk_int32_t duk_bd_decode_flagged(duk_bitdecoder_ctx *ctx, duk_small_int_t bits, duk_int32_t def_value); + +DUK_INTERNAL_DECL void duk_be_encode(duk_bitencoder_ctx *ctx, duk_uint32_t data, duk_small_int_t bits); +DUK_INTERNAL_DECL void duk_be_finish(duk_bitencoder_ctx *ctx); + +DUK_INTERNAL_DECL duk_uint32_t duk_util_tinyrandom_get_bits(duk_hthread *thr, duk_small_int_t n); +DUK_INTERNAL_DECL duk_double_t duk_util_tinyrandom_get_double(duk_hthread *thr); + +DUK_INTERNAL_DECL void duk_bw_init(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_hbuffer_dynamic *h_buf); +DUK_INTERNAL_DECL void duk_bw_init_pushbuf(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_size_t buf_size); +DUK_INTERNAL_DECL duk_uint8_t *duk_bw_resize(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_size_t sz); +DUK_INTERNAL_DECL void duk_bw_compact(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx); +DUK_INTERNAL_DECL void duk_bw_write_raw_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t src_off, duk_size_t len); +DUK_INTERNAL_DECL void duk_bw_write_ensure_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t src_off, duk_size_t len); +DUK_INTERNAL_DECL void duk_bw_insert_raw_bytes(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, const duk_uint8_t *buf, duk_size_t len); +DUK_INTERNAL_DECL void duk_bw_insert_ensure_bytes(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, const duk_uint8_t *buf, duk_size_t len); +DUK_INTERNAL_DECL void duk_bw_insert_raw_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, duk_size_t src_off, duk_size_t len); +DUK_INTERNAL_DECL void duk_bw_insert_ensure_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, duk_size_t src_off, duk_size_t len); +DUK_INTERNAL_DECL duk_uint8_t *duk_bw_insert_raw_area(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t off, duk_size_t len); +DUK_INTERNAL_DECL duk_uint8_t *duk_bw_insert_ensure_area(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t off, duk_size_t len); +DUK_INTERNAL_DECL void duk_bw_remove_raw_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t off, duk_size_t len); +/* No duk_bw_remove_ensure_slice(), functionality would be identical. */ + +DUK_INTERNAL_DECL duk_uint16_t duk_raw_read_u16_be(duk_uint8_t **p); +DUK_INTERNAL_DECL duk_uint32_t duk_raw_read_u32_be(duk_uint8_t **p); +DUK_INTERNAL_DECL duk_double_t duk_raw_read_double_be(duk_uint8_t **p); +DUK_INTERNAL_DECL void duk_raw_write_u16_be(duk_uint8_t **p, duk_uint16_t val); +DUK_INTERNAL_DECL void duk_raw_write_u32_be(duk_uint8_t **p, duk_uint32_t val); +DUK_INTERNAL_DECL void duk_raw_write_double_be(duk_uint8_t **p, duk_double_t val); + +#if defined(DUK_USE_DEBUGGER_SUPPORT) /* For now only needed by the debugger. */ +DUK_INTERNAL void duk_byteswap_bytes(duk_uint8_t *p, duk_small_uint_t len); +#endif + +#endif /* DUK_UTIL_H_INCLUDED */ +/* + * Shared error messages: declarations and macros + * + * Error messages are accessed through macros with fine-grained, explicit + * error message distinctions. Concrete error messages are selected by the + * macros and multiple macros can map to the same concrete string to save + * on code footprint. This allows flexible footprint/verbosity tuning with + * minimal code impact. There are a few limitations to this approach: + * (1) switching between plain messages and format strings doesn't work + * conveniently, and (2) conditional strings are a bit awkward to handle. + * + * Because format strings behave differently in the call site (they need to + * be followed by format arguments), they have a special prefix (DUK_STR_FMT_ + * and duk_str_fmt_). + * + * On some compilers using explicit shared strings is preferable; on others + * it may be better to use straight literals because the compiler will combine + * them anyway, and such strings won't end up unnecessarily in a symbol table. + */ + +#ifndef DUK_ERRMSG_H_INCLUDED +#define DUK_ERRMSG_H_INCLUDED + +#define DUK_STR_INTERNAL_ERROR duk_str_internal_error +#define DUK_STR_INVALID_COUNT duk_str_invalid_count +#define DUK_STR_INVALID_CALL_ARGS duk_str_invalid_call_args +#define DUK_STR_NOT_CONSTRUCTABLE duk_str_not_constructable +#define DUK_STR_NOT_CALLABLE duk_str_not_callable +#define DUK_STR_NOT_EXTENSIBLE duk_str_not_extensible +#define DUK_STR_NOT_WRITABLE duk_str_not_writable +#define DUK_STR_NOT_CONFIGURABLE duk_str_not_configurable + +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const char *duk_str_internal_error; +DUK_INTERNAL_DECL const char *duk_str_invalid_count; +DUK_INTERNAL_DECL const char *duk_str_invalid_call_args; +DUK_INTERNAL_DECL const char *duk_str_not_constructable; +DUK_INTERNAL_DECL const char *duk_str_not_callable; +DUK_INTERNAL_DECL const char *duk_str_not_extensible; +DUK_INTERNAL_DECL const char *duk_str_not_writable; +DUK_INTERNAL_DECL const char *duk_str_not_configurable; +#endif /* !DUK_SINGLE_FILE */ + +#define DUK_STR_INVALID_CONTEXT duk_str_invalid_context +#define DUK_STR_INVALID_INDEX duk_str_invalid_call_args +#define DUK_STR_PUSH_BEYOND_ALLOC_STACK duk_str_push_beyond_alloc_stack +#define DUK_STR_NOT_UNDEFINED duk_str_unexpected_type +#define DUK_STR_NOT_NULL duk_str_unexpected_type +#define DUK_STR_NOT_BOOLEAN duk_str_unexpected_type +#define DUK_STR_NOT_NUMBER duk_str_unexpected_type +#define DUK_STR_NOT_STRING duk_str_unexpected_type +#define DUK_STR_NOT_OBJECT duk_str_unexpected_type +#define DUK_STR_NOT_POINTER duk_str_unexpected_type +#define DUK_STR_NOT_BUFFER duk_str_not_buffer /* still in use with verbose messages */ +#define DUK_STR_UNEXPECTED_TYPE duk_str_unexpected_type +#define DUK_STR_NOT_THREAD duk_str_unexpected_type +#define DUK_STR_NOT_COMPILEDFUNCTION duk_str_unexpected_type +#define DUK_STR_NOT_NATIVEFUNCTION duk_str_unexpected_type +#define DUK_STR_NOT_C_FUNCTION duk_str_unexpected_type +#define DUK_STR_NOT_FUNCTION duk_str_unexpected_type +#define DUK_STR_NOT_REGEXP duk_str_unexpected_type +#define DUK_STR_DEFAULTVALUE_COERCE_FAILED duk_str_defaultvalue_coerce_failed +#define DUK_STR_NUMBER_OUTSIDE_RANGE duk_str_number_outside_range +#define DUK_STR_NOT_OBJECT_COERCIBLE duk_str_not_object_coercible +#define DUK_STR_STRING_TOO_LONG duk_str_string_too_long +#define DUK_STR_BUFFER_TOO_LONG duk_str_buffer_too_long +#define DUK_STR_SPRINTF_TOO_LONG duk_str_sprintf_too_long +#define DUK_STR_ALLOC_FAILED duk_str_alloc_failed +#define DUK_STR_POP_TOO_MANY duk_str_pop_too_many +#define DUK_STR_WRONG_BUFFER_TYPE duk_str_wrong_buffer_type +#define DUK_STR_ENCODE_FAILED duk_str_encode_failed +#define DUK_STR_DECODE_FAILED duk_str_decode_failed +#define DUK_STR_NO_SOURCECODE duk_str_no_sourcecode +#define DUK_STR_CONCAT_RESULT_TOO_LONG duk_str_concat_result_too_long +#define DUK_STR_UNIMPLEMENTED duk_str_unimplemented +#define DUK_STR_UNSUPPORTED duk_str_unsupported +#define DUK_STR_ARRAY_LENGTH_OVER_2G duk_str_array_length_over_2g + +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const char *duk_str_invalid_context; +DUK_INTERNAL_DECL const char *duk_str_push_beyond_alloc_stack; +DUK_INTERNAL_DECL const char *duk_str_not_buffer; +DUK_INTERNAL_DECL const char *duk_str_unexpected_type; +DUK_INTERNAL_DECL const char *duk_str_defaultvalue_coerce_failed; +DUK_INTERNAL_DECL const char *duk_str_number_outside_range; +DUK_INTERNAL_DECL const char *duk_str_not_object_coercible; +DUK_INTERNAL_DECL const char *duk_str_string_too_long; +DUK_INTERNAL_DECL const char *duk_str_buffer_too_long; +DUK_INTERNAL_DECL const char *duk_str_sprintf_too_long; +DUK_INTERNAL_DECL const char *duk_str_alloc_failed; +DUK_INTERNAL_DECL const char *duk_str_pop_too_many; +DUK_INTERNAL_DECL const char *duk_str_wrong_buffer_type; +DUK_INTERNAL_DECL const char *duk_str_encode_failed; +DUK_INTERNAL_DECL const char *duk_str_decode_failed; +DUK_INTERNAL_DECL const char *duk_str_no_sourcecode; +DUK_INTERNAL_DECL const char *duk_str_concat_result_too_long; +DUK_INTERNAL_DECL const char *duk_str_unimplemented; +DUK_INTERNAL_DECL const char *duk_str_unsupported; +DUK_INTERNAL_DECL const char *duk_str_array_length_over_2g; +#endif /* !DUK_SINGLE_FILE */ + +#define DUK_STR_FMT_PTR duk_str_fmt_ptr +#define DUK_STR_FMT_INVALID_JSON duk_str_fmt_invalid_json +#define DUK_STR_JSONDEC_RECLIMIT duk_str_jsondec_reclimit +#define DUK_STR_JSONENC_RECLIMIT duk_str_jsonenc_reclimit +#define DUK_STR_CYCLIC_INPUT duk_str_cyclic_input + +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const char *duk_str_fmt_ptr; +DUK_INTERNAL_DECL const char *duk_str_fmt_invalid_json; +DUK_INTERNAL_DECL const char *duk_str_jsondec_reclimit; +DUK_INTERNAL_DECL const char *duk_str_jsonenc_reclimit; +DUK_INTERNAL_DECL const char *duk_str_cyclic_input; +#endif /* !DUK_SINGLE_FILE */ + +#define DUK_STR_PROXY_REVOKED duk_str_proxy_revoked +#define DUK_STR_INVALID_BASE duk_str_invalid_base +#define DUK_STR_STRICT_CALLER_READ duk_str_strict_caller_read +#define DUK_STR_PROXY_REJECTED duk_str_proxy_rejected +#define DUK_STR_INVALID_ARRAY_LENGTH duk_str_invalid_array_length +#define DUK_STR_ARRAY_LENGTH_WRITE_FAILED duk_str_array_length_write_failed +#define DUK_STR_ARRAY_LENGTH_NOT_WRITABLE duk_str_array_length_not_writable +#define DUK_STR_SETTER_UNDEFINED duk_str_setter_undefined +#define DUK_STR_REDEFINE_VIRT_PROP duk_str_redefine_virt_prop +#define DUK_STR_INVALID_DESCRIPTOR duk_str_invalid_descriptor +#define DUK_STR_PROPERTY_IS_VIRTUAL duk_str_property_is_virtual + +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const char *duk_str_proxy_revoked; +DUK_INTERNAL_DECL const char *duk_str_invalid_base; +DUK_INTERNAL_DECL const char *duk_str_strict_caller_read; +DUK_INTERNAL_DECL const char *duk_str_proxy_rejected; +DUK_INTERNAL_DECL const char *duk_str_invalid_array_length; +DUK_INTERNAL_DECL const char *duk_str_array_length_write_failed; +DUK_INTERNAL_DECL const char *duk_str_array_length_not_writable; +DUK_INTERNAL_DECL const char *duk_str_setter_undefined; +DUK_INTERNAL_DECL const char *duk_str_redefine_virt_prop; +DUK_INTERNAL_DECL const char *duk_str_invalid_descriptor; +DUK_INTERNAL_DECL const char *duk_str_property_is_virtual; +#endif /* !DUK_SINGLE_FILE */ + +#define DUK_STR_PARSE_ERROR duk_str_parse_error +#define DUK_STR_DUPLICATE_LABEL duk_str_duplicate_label +#define DUK_STR_INVALID_LABEL duk_str_invalid_label +#define DUK_STR_INVALID_ARRAY_LITERAL duk_str_invalid_array_literal +#define DUK_STR_INVALID_OBJECT_LITERAL duk_str_invalid_object_literal +#define DUK_STR_INVALID_VAR_DECLARATION duk_str_invalid_var_declaration +#define DUK_STR_CANNOT_DELETE_IDENTIFIER duk_str_cannot_delete_identifier +#define DUK_STR_INVALID_EXPRESSION duk_str_invalid_expression +#define DUK_STR_INVALID_LVALUE duk_str_invalid_lvalue +#define DUK_STR_EXPECTED_IDENTIFIER duk_str_expected_identifier +#define DUK_STR_EMPTY_EXPR_NOT_ALLOWED duk_str_empty_expr_not_allowed +#define DUK_STR_INVALID_FOR duk_str_invalid_for +#define DUK_STR_INVALID_SWITCH duk_str_invalid_switch +#define DUK_STR_INVALID_BREAK_CONT_LABEL duk_str_invalid_break_cont_label +#define DUK_STR_INVALID_RETURN duk_str_invalid_return +#define DUK_STR_INVALID_TRY duk_str_invalid_try +#define DUK_STR_INVALID_THROW duk_str_invalid_throw +#define DUK_STR_WITH_IN_STRICT_MODE duk_str_with_in_strict_mode +#define DUK_STR_FUNC_STMT_NOT_ALLOWED duk_str_func_stmt_not_allowed +#define DUK_STR_UNTERMINATED_STMT duk_str_unterminated_stmt +#define DUK_STR_INVALID_ARG_NAME duk_str_invalid_arg_name +#define DUK_STR_INVALID_FUNC_NAME duk_str_invalid_func_name +#define DUK_STR_INVALID_GETSET_NAME duk_str_invalid_getset_name +#define DUK_STR_FUNC_NAME_REQUIRED duk_str_func_name_required + +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const char *duk_str_parse_error; +DUK_INTERNAL_DECL const char *duk_str_duplicate_label; +DUK_INTERNAL_DECL const char *duk_str_invalid_label; +DUK_INTERNAL_DECL const char *duk_str_invalid_array_literal; +DUK_INTERNAL_DECL const char *duk_str_invalid_object_literal; +DUK_INTERNAL_DECL const char *duk_str_invalid_var_declaration; +DUK_INTERNAL_DECL const char *duk_str_cannot_delete_identifier; +DUK_INTERNAL_DECL const char *duk_str_invalid_expression; +DUK_INTERNAL_DECL const char *duk_str_invalid_lvalue; +DUK_INTERNAL_DECL const char *duk_str_expected_identifier; +DUK_INTERNAL_DECL const char *duk_str_empty_expr_not_allowed; +DUK_INTERNAL_DECL const char *duk_str_invalid_for; +DUK_INTERNAL_DECL const char *duk_str_invalid_switch; +DUK_INTERNAL_DECL const char *duk_str_invalid_break_cont_label; +DUK_INTERNAL_DECL const char *duk_str_invalid_return; +DUK_INTERNAL_DECL const char *duk_str_invalid_try; +DUK_INTERNAL_DECL const char *duk_str_invalid_throw; +DUK_INTERNAL_DECL const char *duk_str_with_in_strict_mode; +DUK_INTERNAL_DECL const char *duk_str_func_stmt_not_allowed; +DUK_INTERNAL_DECL const char *duk_str_unterminated_stmt; +DUK_INTERNAL_DECL const char *duk_str_invalid_arg_name; +DUK_INTERNAL_DECL const char *duk_str_invalid_func_name; +DUK_INTERNAL_DECL const char *duk_str_invalid_getset_name; +DUK_INTERNAL_DECL const char *duk_str_func_name_required; +#endif /* !DUK_SINGLE_FILE */ + +#define DUK_STR_INVALID_QUANTIFIER_NO_ATOM duk_str_invalid_quantifier_no_atom +#define DUK_STR_INVALID_QUANTIFIER_VALUES duk_str_invalid_quantifier_values +#define DUK_STR_QUANTIFIER_TOO_MANY_COPIES duk_str_quantifier_too_many_copies +#define DUK_STR_UNEXPECTED_CLOSING_PAREN duk_str_unexpected_closing_paren +#define DUK_STR_UNEXPECTED_END_OF_PATTERN duk_str_unexpected_end_of_pattern +#define DUK_STR_UNEXPECTED_REGEXP_TOKEN duk_str_unexpected_regexp_token +#define DUK_STR_INVALID_REGEXP_FLAGS duk_str_invalid_regexp_flags +#define DUK_STR_INVALID_BACKREFS duk_str_invalid_backrefs + +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const char *duk_str_invalid_quantifier_no_atom; +DUK_INTERNAL_DECL const char *duk_str_invalid_quantifier_values; +DUK_INTERNAL_DECL const char *duk_str_quantifier_too_many_copies; +DUK_INTERNAL_DECL const char *duk_str_unexpected_closing_paren; +DUK_INTERNAL_DECL const char *duk_str_unexpected_end_of_pattern; +DUK_INTERNAL_DECL const char *duk_str_unexpected_regexp_token; +DUK_INTERNAL_DECL const char *duk_str_invalid_regexp_flags; +DUK_INTERNAL_DECL const char *duk_str_invalid_backrefs; +#endif /* !DUK_SINGLE_FILE */ + +#define DUK_STR_VALSTACK_LIMIT duk_str_valstack_limit +#define DUK_STR_CALLSTACK_LIMIT duk_str_callstack_limit +#define DUK_STR_CATCHSTACK_LIMIT duk_str_catchstack_limit +#define DUK_STR_PROTOTYPE_CHAIN_LIMIT duk_str_prototype_chain_limit +#define DUK_STR_BOUND_CHAIN_LIMIT duk_str_bound_chain_limit +#define DUK_STR_C_CALLSTACK_LIMIT duk_str_c_callstack_limit +#define DUK_STR_COMPILER_RECURSION_LIMIT duk_str_compiler_recursion_limit +#define DUK_STR_BYTECODE_LIMIT duk_str_bytecode_limit +#define DUK_STR_REG_LIMIT duk_str_reg_limit +#define DUK_STR_TEMP_LIMIT duk_str_temp_limit +#define DUK_STR_CONST_LIMIT duk_str_const_limit +#define DUK_STR_FUNC_LIMIT duk_str_func_limit +#define DUK_STR_REGEXP_COMPILER_RECURSION_LIMIT duk_str_regexp_compiler_recursion_limit +#define DUK_STR_REGEXP_EXECUTOR_RECURSION_LIMIT duk_str_regexp_executor_recursion_limit +#define DUK_STR_REGEXP_EXECUTOR_STEP_LIMIT duk_str_regexp_executor_step_limit + +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const char *duk_str_valstack_limit; +DUK_INTERNAL_DECL const char *duk_str_callstack_limit; +DUK_INTERNAL_DECL const char *duk_str_catchstack_limit; +DUK_INTERNAL_DECL const char *duk_str_prototype_chain_limit; +DUK_INTERNAL_DECL const char *duk_str_bound_chain_limit; +DUK_INTERNAL_DECL const char *duk_str_c_callstack_limit; +DUK_INTERNAL_DECL const char *duk_str_compiler_recursion_limit; +DUK_INTERNAL_DECL const char *duk_str_bytecode_limit; +DUK_INTERNAL_DECL const char *duk_str_reg_limit; +DUK_INTERNAL_DECL const char *duk_str_temp_limit; +DUK_INTERNAL_DECL const char *duk_str_const_limit; +DUK_INTERNAL_DECL const char *duk_str_func_limit; +DUK_INTERNAL_DECL const char *duk_str_regexp_compiler_recursion_limit; +DUK_INTERNAL_DECL const char *duk_str_regexp_executor_recursion_limit; +DUK_INTERNAL_DECL const char *duk_str_regexp_executor_step_limit; +#endif /* !DUK_SINGLE_FILE */ + +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const char *duk_str_anon; +#endif /* !DUK_SINGLE_FILE */ + +#endif /* DUK_ERRMSG_H_INCLUDED */ +/* + * Ecmascript bytecode + */ + +#ifndef DUK_JS_BYTECODE_H_INCLUDED +#define DUK_JS_BYTECODE_H_INCLUDED + +/* + * Logical instruction layout + * ========================== + * + * !3!3!2!2!2!2!2!2!2!2!2!2!1!1!1!1!1!1!1!1!1!1! ! ! ! ! ! ! ! ! ! ! + * !1!0!9!8!7!6!5!4!3!2!1!0!9!8!7!6!5!4!3!2!1!0!9!8!7!6!5!4!3!2!1!0! + * +---------------------------------------------------+-----------+ + * ! C ! B ! A ! OP ! + * +---------------------------------------------------+-----------+ + * + * OP (6 bits): opcode (DUK_OP_*), access should be fastest + * A (8 bits): typically a target register number + * B (9 bits): typically first source register/constant number + * C (9 bits): typically second source register/constant number + * + * Some instructions combine BC or ABC together for larger parameter values. + * Signed integers (e.g. jump offsets) are encoded as unsigned, with an opcode + * specific bias. B and C may denote a register or a constant, see + * DUK_BC_ISREG() and DUK_BC_ISCONST(). + * + * Note: macro naming is a bit misleading, e.g. "ABC" in macro name but + * the field layout is logically "CBA". + */ + +typedef duk_uint32_t duk_instr_t; + +#define DUK_DEC_OP(x) ((x) & 0x3fUL) +#define DUK_DEC_A(x) (((x) >> 6) & 0xffUL) +#define DUK_DEC_B(x) (((x) >> 14) & 0x1ffUL) +#define DUK_DEC_C(x) (((x) >> 23) & 0x1ffUL) +#define DUK_DEC_BC(x) (((x) >> 14) & 0x3ffffUL) +#define DUK_DEC_ABC(x) (((x) >> 6) & 0x3ffffffUL) + +#define DUK_ENC_OP(op) ((duk_instr_t) (op)) +#define DUK_ENC_OP_ABC(op,abc) ((duk_instr_t) ( \ + (((duk_instr_t) (abc)) << 6) | \ + ((duk_instr_t) (op)) \ + )) +#define DUK_ENC_OP_A_BC(op,a,bc) ((duk_instr_t) ( \ + (((duk_instr_t) (bc)) << 14) | \ + (((duk_instr_t) (a)) << 6) | \ + ((duk_instr_t) (op)) \ + )) +#define DUK_ENC_OP_A_B_C(op,a,b,c) ((duk_instr_t) ( \ + (((duk_instr_t) (c)) << 23) | \ + (((duk_instr_t) (b)) << 14) | \ + (((duk_instr_t) (a)) << 6) | \ + ((duk_instr_t) (op)) \ + )) +#define DUK_ENC_OP_A_B(op,a,b) DUK_ENC_OP_A_B_C(op,a,b,0) +#define DUK_ENC_OP_A(op,a) DUK_ENC_OP_A_B_C(op,a,0,0) + +/* Constants should be signed so that signed arithmetic involving them + * won't cause values to be coerced accidentally to unsigned. + */ +#define DUK_BC_OP_MIN 0 +#define DUK_BC_OP_MAX 0x3fL +#define DUK_BC_A_MIN 0 +#define DUK_BC_A_MAX 0xffL +#define DUK_BC_B_MIN 0 +#define DUK_BC_B_MAX 0x1ffL +#define DUK_BC_C_MIN 0 +#define DUK_BC_C_MAX 0x1ffL +#define DUK_BC_BC_MIN 0 +#define DUK_BC_BC_MAX 0x3ffffL +#define DUK_BC_ABC_MIN 0 +#define DUK_BC_ABC_MAX 0x3ffffffL +#define DUK_BC_EXTRAOP_MIN DUK_BC_A_MIN +#define DUK_BC_EXTRAOP_MAX DUK_BC_A_MAX + +#define DUK_OP_LDREG 0 +#define DUK_OP_STREG 1 +#define DUK_OP_LDCONST 2 +#define DUK_OP_LDINT 3 +#define DUK_OP_LDINTX 4 +#define DUK_OP_MPUTOBJ 5 +#define DUK_OP_MPUTOBJI 6 +#define DUK_OP_MPUTARR 7 +#define DUK_OP_MPUTARRI 8 +#define DUK_OP_NEW 9 +#define DUK_OP_NEWI 10 +#define DUK_OP_REGEXP 11 +#define DUK_OP_CSREG 12 +#define DUK_OP_CSREGI 13 +#define DUK_OP_GETVAR 14 +#define DUK_OP_PUTVAR 15 +#define DUK_OP_DECLVAR 16 +#define DUK_OP_DELVAR 17 +#define DUK_OP_CSVAR 18 +#define DUK_OP_CSVARI 19 +#define DUK_OP_CLOSURE 20 +#define DUK_OP_GETPROP 21 +#define DUK_OP_PUTPROP 22 +#define DUK_OP_DELPROP 23 +#define DUK_OP_CSPROP 24 +#define DUK_OP_CSPROPI 25 +#define DUK_OP_ADD 26 +#define DUK_OP_SUB 27 +#define DUK_OP_MUL 28 +#define DUK_OP_DIV 29 +#define DUK_OP_MOD 30 +#define DUK_OP_BAND 31 +#define DUK_OP_BOR 32 +#define DUK_OP_BXOR 33 +#define DUK_OP_BASL 34 +#define DUK_OP_BLSR 35 +#define DUK_OP_BASR 36 +#define DUK_OP_EQ 37 +#define DUK_OP_NEQ 38 +#define DUK_OP_SEQ 39 +#define DUK_OP_SNEQ 40 +#define DUK_OP_GT 41 +#define DUK_OP_GE 42 +#define DUK_OP_LT 43 +#define DUK_OP_LE 44 +#define DUK_OP_IF 45 +#define DUK_OP_JUMP 46 +#define DUK_OP_RETURN 47 +#define DUK_OP_CALL 48 +#define DUK_OP_CALLI 49 +#define DUK_OP_TRYCATCH 50 +#define DUK_OP_EXTRA 51 +#define DUK_OP_PREINCR 52 /* pre/post opcode values have constraints, */ +#define DUK_OP_PREDECR 53 /* see duk_js_executor.c */ +#define DUK_OP_POSTINCR 54 +#define DUK_OP_POSTDECR 55 +#define DUK_OP_PREINCV 56 +#define DUK_OP_PREDECV 57 +#define DUK_OP_POSTINCV 58 +#define DUK_OP_POSTDECV 59 +#define DUK_OP_PREINCP 60 +#define DUK_OP_PREDECP 61 +#define DUK_OP_POSTINCP 62 +#define DUK_OP_POSTDECP 63 +#define DUK_OP_NONE 64 /* dummy value used as marker */ + +/* DUK_OP_EXTRA, sub-operation in A */ +#define DUK_EXTRAOP_NOP 0 +#define DUK_EXTRAOP_INVALID 1 +#define DUK_EXTRAOP_LDTHIS 2 +#define DUK_EXTRAOP_LDUNDEF 3 +#define DUK_EXTRAOP_LDNULL 4 +#define DUK_EXTRAOP_LDTRUE 5 +#define DUK_EXTRAOP_LDFALSE 6 +#define DUK_EXTRAOP_NEWOBJ 7 +#define DUK_EXTRAOP_NEWARR 8 +#define DUK_EXTRAOP_SETALEN 9 +#define DUK_EXTRAOP_TYPEOF 10 +#define DUK_EXTRAOP_TYPEOFID 11 +#define DUK_EXTRAOP_INITENUM 12 +#define DUK_EXTRAOP_NEXTENUM 13 +#define DUK_EXTRAOP_INITSET 14 +#define DUK_EXTRAOP_INITSETI 15 +#define DUK_EXTRAOP_INITGET 16 +#define DUK_EXTRAOP_INITGETI 17 +#define DUK_EXTRAOP_ENDTRY 18 +#define DUK_EXTRAOP_ENDCATCH 19 +#define DUK_EXTRAOP_ENDFIN 20 +#define DUK_EXTRAOP_THROW 21 +#define DUK_EXTRAOP_INVLHS 22 +#define DUK_EXTRAOP_UNM 23 +#define DUK_EXTRAOP_UNP 24 +#define DUK_EXTRAOP_DEBUGGER 25 +#define DUK_EXTRAOP_BREAK 26 +#define DUK_EXTRAOP_CONTINUE 27 +#define DUK_EXTRAOP_BNOT 28 +#define DUK_EXTRAOP_LNOT 29 +#define DUK_EXTRAOP_INSTOF 30 +#define DUK_EXTRAOP_IN 31 +#define DUK_EXTRAOP_LABEL 32 +#define DUK_EXTRAOP_ENDLABEL 33 + +/* DUK_OP_CALL flags in A */ +#define DUK_BC_CALL_FLAG_TAILCALL (1 << 0) +#define DUK_BC_CALL_FLAG_EVALCALL (1 << 1) + +/* DUK_OP_TRYCATCH flags in A */ +#define DUK_BC_TRYCATCH_FLAG_HAVE_CATCH (1 << 0) +#define DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY (1 << 1) +#define DUK_BC_TRYCATCH_FLAG_CATCH_BINDING (1 << 2) +#define DUK_BC_TRYCATCH_FLAG_WITH_BINDING (1 << 3) + +/* DUK_OP_RETURN flags in A */ +#define DUK_BC_RETURN_FLAG_HAVE_RETVAL (1 << 0) + +/* DUK_OP_DECLVAR flags in A; bottom bits are reserved for propdesc flags (DUK_PROPDESC_FLAG_XXX) */ +#define DUK_BC_DECLVAR_FLAG_UNDEF_VALUE (1 << 4) /* use 'undefined' for value automatically */ +#define DUK_BC_DECLVAR_FLAG_FUNC_DECL (1 << 5) /* function declaration */ + +/* misc constants and helper macros */ +#define DUK_BC_REGLIMIT 256 /* if B/C is >= this value, refers to a const */ +#define DUK_BC_ISREG(x) ((x) < DUK_BC_REGLIMIT) +#define DUK_BC_ISCONST(x) ((x) >= DUK_BC_REGLIMIT) +#define DUK_BC_LDINT_BIAS (1L << 17) +#define DUK_BC_LDINTX_SHIFT 18 +#define DUK_BC_JUMP_BIAS (1L << 25) + +#endif /* DUK_JS_BYTECODE_H_INCLUDED */ +/* + * Lexer defines. + */ + +#ifndef DUK_LEXER_H_INCLUDED +#define DUK_LEXER_H_INCLUDED + +typedef void (*duk_re_range_callback)(void *user, duk_codepoint_t r1, duk_codepoint_t r2, duk_bool_t direct); + +/* + * A token is interpreted as any possible production of InputElementDiv + * and InputElementRegExp, see E5 Section 7 in its entirety. Note that + * the E5 "Token" production does not cover all actual tokens of the + * language (which is explicitly stated in the specification, Section 7.5). + * Null and boolean literals are defined as part of both ReservedWord + * (E5 Section 7.6.1) and Literal (E5 Section 7.8) productions. Here, + * null and boolean values have literal tokens, and are not reserved + * words. + * + * Decimal literal negative/positive sign is -not- part of DUK_TOK_NUMBER. + * The number tokens always have a non-negative value. The unary minus + * operator in "-1.0" is optimized during compilation to yield a single + * negative constant. + * + * Token numbering is free except that reserved words are required to be + * in a continuous range and in a particular order. See genstrings.py. + */ + +#define DUK_LEXER_INITCTX(ctx) duk_lexer_initctx((ctx)) + +#define DUK_LEXER_SETPOINT(ctx,pt) duk_lexer_setpoint((ctx), (pt)) + +#define DUK_LEXER_GETPOINT(ctx,pt) do { (pt)->offset = (ctx)->window[0].offset; \ + (pt)->line = (ctx)->window[0].line; } while (0) + +/* currently 6 characters of lookup are actually needed (duk_lexer.c) */ +#define DUK_LEXER_WINDOW_SIZE 6 +#if defined(DUK_USE_LEXER_SLIDING_WINDOW) +#define DUK_LEXER_BUFFER_SIZE 64 +#endif + +#define DUK_TOK_MINVAL 0 + +/* returned after EOF (infinite amount) */ +#define DUK_TOK_EOF 0 + +/* identifier names (E5 Section 7.6) */ +#define DUK_TOK_IDENTIFIER 1 + +/* reserved words: keywords */ +#define DUK_TOK_START_RESERVED 2 +#define DUK_TOK_BREAK 2 +#define DUK_TOK_CASE 3 +#define DUK_TOK_CATCH 4 +#define DUK_TOK_CONTINUE 5 +#define DUK_TOK_DEBUGGER 6 +#define DUK_TOK_DEFAULT 7 +#define DUK_TOK_DELETE 8 +#define DUK_TOK_DO 9 +#define DUK_TOK_ELSE 10 +#define DUK_TOK_FINALLY 11 +#define DUK_TOK_FOR 12 +#define DUK_TOK_FUNCTION 13 +#define DUK_TOK_IF 14 +#define DUK_TOK_IN 15 +#define DUK_TOK_INSTANCEOF 16 +#define DUK_TOK_NEW 17 +#define DUK_TOK_RETURN 18 +#define DUK_TOK_SWITCH 19 +#define DUK_TOK_THIS 20 +#define DUK_TOK_THROW 21 +#define DUK_TOK_TRY 22 +#define DUK_TOK_TYPEOF 23 +#define DUK_TOK_VAR 24 +#define DUK_TOK_CONST 25 +#define DUK_TOK_VOID 26 +#define DUK_TOK_WHILE 27 +#define DUK_TOK_WITH 28 + +/* reserved words: future reserved words */ +#define DUK_TOK_CLASS 29 +#define DUK_TOK_ENUM 30 +#define DUK_TOK_EXPORT 31 +#define DUK_TOK_EXTENDS 32 +#define DUK_TOK_IMPORT 33 +#define DUK_TOK_SUPER 34 + +/* "null", "true", and "false" are always reserved words. + * Note that "get" and "set" are not! + */ +#define DUK_TOK_NULL 35 +#define DUK_TOK_TRUE 36 +#define DUK_TOK_FALSE 37 + +/* reserved words: additional future reserved words in strict mode */ +#define DUK_TOK_START_STRICT_RESERVED 38 /* inclusive */ +#define DUK_TOK_IMPLEMENTS 38 +#define DUK_TOK_INTERFACE 39 +#define DUK_TOK_LET 40 +#define DUK_TOK_PACKAGE 41 +#define DUK_TOK_PRIVATE 42 +#define DUK_TOK_PROTECTED 43 +#define DUK_TOK_PUBLIC 44 +#define DUK_TOK_STATIC 45 +#define DUK_TOK_YIELD 46 + +#define DUK_TOK_END_RESERVED 47 /* exclusive */ + +/* "get" and "set" are tokens but NOT ReservedWords. They are currently + * parsed and identifiers and these defines are actually now unused. + */ +#define DUK_TOK_GET 47 +#define DUK_TOK_SET 48 + +/* punctuators (unlike the spec, also includes "/" and "/=") */ +#define DUK_TOK_LCURLY 49 +#define DUK_TOK_RCURLY 50 +#define DUK_TOK_LBRACKET 51 +#define DUK_TOK_RBRACKET 52 +#define DUK_TOK_LPAREN 53 +#define DUK_TOK_RPAREN 54 +#define DUK_TOK_PERIOD 55 +#define DUK_TOK_SEMICOLON 56 +#define DUK_TOK_COMMA 57 +#define DUK_TOK_LT 58 +#define DUK_TOK_GT 59 +#define DUK_TOK_LE 60 +#define DUK_TOK_GE 61 +#define DUK_TOK_EQ 62 +#define DUK_TOK_NEQ 63 +#define DUK_TOK_SEQ 64 +#define DUK_TOK_SNEQ 65 +#define DUK_TOK_ADD 66 +#define DUK_TOK_SUB 67 +#define DUK_TOK_MUL 68 +#define DUK_TOK_DIV 69 +#define DUK_TOK_MOD 70 +#define DUK_TOK_INCREMENT 71 +#define DUK_TOK_DECREMENT 72 +#define DUK_TOK_ALSHIFT 73 /* named "arithmetic" because result is signed */ +#define DUK_TOK_ARSHIFT 74 +#define DUK_TOK_RSHIFT 75 +#define DUK_TOK_BAND 76 +#define DUK_TOK_BOR 77 +#define DUK_TOK_BXOR 78 +#define DUK_TOK_LNOT 79 +#define DUK_TOK_BNOT 80 +#define DUK_TOK_LAND 81 +#define DUK_TOK_LOR 82 +#define DUK_TOK_QUESTION 83 +#define DUK_TOK_COLON 84 +#define DUK_TOK_EQUALSIGN 85 +#define DUK_TOK_ADD_EQ 86 +#define DUK_TOK_SUB_EQ 87 +#define DUK_TOK_MUL_EQ 88 +#define DUK_TOK_DIV_EQ 89 +#define DUK_TOK_MOD_EQ 90 +#define DUK_TOK_ALSHIFT_EQ 91 +#define DUK_TOK_ARSHIFT_EQ 92 +#define DUK_TOK_RSHIFT_EQ 93 +#define DUK_TOK_BAND_EQ 94 +#define DUK_TOK_BOR_EQ 95 +#define DUK_TOK_BXOR_EQ 96 + +/* literals (E5 Section 7.8), except null, true, false, which are treated + * like reserved words (above). + */ +#define DUK_TOK_NUMBER 97 +#define DUK_TOK_STRING 98 +#define DUK_TOK_REGEXP 99 + +#define DUK_TOK_MAXVAL 99 /* inclusive */ + +/* Convert heap string index to a token (reserved words) */ +#define DUK_STRIDX_TO_TOK(x) ((x) - DUK_STRIDX_START_RESERVED + DUK_TOK_START_RESERVED) + +/* Sanity check */ +#if (DUK_TOK_MAXVAL > 255) +#error DUK_TOK_MAXVAL too large, code assumes it fits into 8 bits +#endif + +/* Sanity checks for string and token defines */ +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_BREAK) != DUK_TOK_BREAK) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CASE) != DUK_TOK_CASE) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CATCH) != DUK_TOK_CATCH) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CONTINUE) != DUK_TOK_CONTINUE) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DEBUGGER) != DUK_TOK_DEBUGGER) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DEFAULT) != DUK_TOK_DEFAULT) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DELETE) != DUK_TOK_DELETE) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DO) != DUK_TOK_DO) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_ELSE) != DUK_TOK_ELSE) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_FINALLY) != DUK_TOK_FINALLY) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_FOR) != DUK_TOK_FOR) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_LC_FUNCTION) != DUK_TOK_FUNCTION) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IF) != DUK_TOK_IF) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IN) != DUK_TOK_IN) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_INSTANCEOF) != DUK_TOK_INSTANCEOF) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_NEW) != DUK_TOK_NEW) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_RETURN) != DUK_TOK_RETURN) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_SWITCH) != DUK_TOK_SWITCH) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_THIS) != DUK_TOK_THIS) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_THROW) != DUK_TOK_THROW) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_TRY) != DUK_TOK_TRY) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_TYPEOF) != DUK_TOK_TYPEOF) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_VAR) != DUK_TOK_VAR) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_VOID) != DUK_TOK_VOID) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_WHILE) != DUK_TOK_WHILE) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_WITH) != DUK_TOK_WITH) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CLASS) != DUK_TOK_CLASS) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CONST) != DUK_TOK_CONST) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_ENUM) != DUK_TOK_ENUM) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_EXPORT) != DUK_TOK_EXPORT) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_EXTENDS) != DUK_TOK_EXTENDS) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IMPORT) != DUK_TOK_IMPORT) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_SUPER) != DUK_TOK_SUPER) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_LC_NULL) != DUK_TOK_NULL) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_TRUE) != DUK_TOK_TRUE) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_FALSE) != DUK_TOK_FALSE) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IMPLEMENTS) != DUK_TOK_IMPLEMENTS) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_INTERFACE) != DUK_TOK_INTERFACE) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_LET) != DUK_TOK_LET) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PACKAGE) != DUK_TOK_PACKAGE) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PRIVATE) != DUK_TOK_PRIVATE) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PROTECTED) != DUK_TOK_PROTECTED) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PUBLIC) != DUK_TOK_PUBLIC) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_STATIC) != DUK_TOK_STATIC) +#error mismatch in token defines +#endif +#if (DUK_STRIDX_TO_TOK(DUK_STRIDX_YIELD) != DUK_TOK_YIELD) +#error mismatch in token defines +#endif + +/* Regexp tokens */ +#define DUK_RETOK_EOF 0 +#define DUK_RETOK_DISJUNCTION 1 +#define DUK_RETOK_QUANTIFIER 2 +#define DUK_RETOK_ASSERT_START 3 +#define DUK_RETOK_ASSERT_END 4 +#define DUK_RETOK_ASSERT_WORD_BOUNDARY 5 +#define DUK_RETOK_ASSERT_NOT_WORD_BOUNDARY 6 +#define DUK_RETOK_ASSERT_START_POS_LOOKAHEAD 7 +#define DUK_RETOK_ASSERT_START_NEG_LOOKAHEAD 8 +#define DUK_RETOK_ATOM_PERIOD 9 +#define DUK_RETOK_ATOM_CHAR 10 +#define DUK_RETOK_ATOM_DIGIT 11 +#define DUK_RETOK_ATOM_NOT_DIGIT 12 +#define DUK_RETOK_ATOM_WHITE 13 +#define DUK_RETOK_ATOM_NOT_WHITE 14 +#define DUK_RETOK_ATOM_WORD_CHAR 15 +#define DUK_RETOK_ATOM_NOT_WORD_CHAR 16 +#define DUK_RETOK_ATOM_BACKREFERENCE 17 +#define DUK_RETOK_ATOM_START_CAPTURE_GROUP 18 +#define DUK_RETOK_ATOM_START_NONCAPTURE_GROUP 19 +#define DUK_RETOK_ATOM_START_CHARCLASS 20 +#define DUK_RETOK_ATOM_START_CHARCLASS_INVERTED 21 +#define DUK_RETOK_ATOM_END_GROUP 22 + +/* Constants for duk_lexer_ctx.buf. */ +#define DUK_LEXER_TEMP_BUF_LIMIT 256 + +/* A token value. Can be memcpy()'d, but note that slot1/slot2 values are on the valstack. + * Some fields (like num, str1, str2) are only valid for specific token types and may have + * stale values otherwise. + */ +struct duk_token { + duk_small_int_t t; /* token type (with reserved word identification) */ + duk_small_int_t t_nores; /* token type (with reserved words as DUK_TOK_IDENTIFER) */ + duk_double_t num; /* numeric value of token */ + duk_hstring *str1; /* string 1 of token (borrowed, stored to ctx->slot1_idx) */ + duk_hstring *str2; /* string 2 of token (borrowed, stored to ctx->slot2_idx) */ + duk_size_t start_offset; /* start byte offset of token in lexer input */ + duk_int_t start_line; /* start line of token (first char) */ + duk_int_t num_escapes; /* number of escapes and line continuations (for directive prologue) */ + duk_bool_t lineterm; /* token was preceded by a lineterm */ + duk_bool_t allow_auto_semi; /* token allows automatic semicolon insertion (eof or preceded by newline) */ +}; + +#define DUK_RE_QUANTIFIER_INFINITE ((duk_uint32_t) 0xffffffffUL) + +/* A regexp token value. */ +struct duk_re_token { + duk_small_int_t t; /* token type */ + duk_small_int_t greedy; + duk_uint_fast32_t num; /* numeric value (character, count) */ + duk_uint_fast32_t qmin; + duk_uint_fast32_t qmax; +}; + +/* A structure for 'snapshotting' a point for rewinding */ +struct duk_lexer_point { + duk_size_t offset; + duk_int_t line; +}; + +/* Lexer codepoint with additional info like offset/line number */ +struct duk_lexer_codepoint { + duk_codepoint_t codepoint; + duk_size_t offset; + duk_int_t line; +}; + +/* Lexer context. Same context is used for Ecmascript and Regexp parsing. */ +struct duk_lexer_ctx { +#if defined(DUK_USE_LEXER_SLIDING_WINDOW) + duk_lexer_codepoint *window; /* unicode code points, window[0] is always next, points to 'buffer' */ + duk_lexer_codepoint buffer[DUK_LEXER_BUFFER_SIZE]; +#else + duk_lexer_codepoint window[DUK_LEXER_WINDOW_SIZE]; /* unicode code points, window[0] is always next */ +#endif + + duk_hthread *thr; /* thread; minimizes argument passing */ + + const duk_uint8_t *input; /* input string (may be a user pointer) */ + duk_size_t input_length; /* input byte length */ + duk_size_t input_offset; /* input offset for window leading edge (not window[0]) */ + duk_int_t input_line; /* input linenumber at input_offset (not window[0]), init to 1 */ + + duk_idx_t slot1_idx; /* valstack slot for 1st token value */ + duk_idx_t slot2_idx; /* valstack slot for 2nd token value */ + duk_idx_t buf_idx; /* valstack slot for temp buffer */ + duk_hbuffer_dynamic *buf; /* temp accumulation buffer */ + duk_bufwriter_ctx bw; /* bufwriter for temp accumulation */ + + duk_int_t token_count; /* number of tokens parsed */ + duk_int_t token_limit; /* maximum token count before error (sanity backstop) */ +}; + +/* + * Prototypes + */ + +DUK_INTERNAL_DECL void duk_lexer_initctx(duk_lexer_ctx *lex_ctx); + +DUK_INTERNAL_DECL void duk_lexer_setpoint(duk_lexer_ctx *lex_ctx, duk_lexer_point *pt); + +DUK_INTERNAL_DECL +void duk_lexer_parse_js_input_element(duk_lexer_ctx *lex_ctx, + duk_token *out_token, + duk_bool_t strict_mode, + duk_bool_t regexp_mode); +#ifdef DUK_USE_REGEXP_SUPPORT +DUK_INTERNAL_DECL void duk_lexer_parse_re_token(duk_lexer_ctx *lex_ctx, duk_re_token *out_token); +DUK_INTERNAL_DECL void duk_lexer_parse_re_ranges(duk_lexer_ctx *lex_ctx, duk_re_range_callback gen_range, void *userdata); +#endif /* DUK_USE_REGEXP_SUPPORT */ + +#endif /* DUK_LEXER_H_INCLUDED */ +/* + * Ecmascript compiler. + */ + +#ifndef DUK_JS_COMPILER_H_INCLUDED +#define DUK_JS_COMPILER_H_INCLUDED + +/* ecmascript compiler limits */ +#define DUK_COMPILER_TOKEN_LIMIT 100000000L /* 1e8: protects against deeply nested inner functions */ + +/* maximum loopcount for peephole optimization */ +#define DUK_COMPILER_PEEPHOLE_MAXITER 3 + +/* maximum bytecode length in instructions */ +#define DUK_COMPILER_MAX_BYTECODE_LENGTH (256L * 1024L * 1024L) /* 1 GB */ + +/* + * Compiler intermediate values + * + * Intermediate values describe either plain values (e.g. strings or + * numbers) or binary operations which have not yet been coerced into + * either a left-hand-side or right-hand-side role (e.g. object property). + */ + +#define DUK_IVAL_NONE 0 /* no value */ +#define DUK_IVAL_PLAIN 1 /* register, constant, or value */ +#define DUK_IVAL_ARITH 2 /* binary arithmetic; DUK_OP_ADD, DUK_OP_EQ, other binary ops */ +#define DUK_IVAL_ARITH_EXTRAOP 3 /* binary arithmetic using extraops; DUK_EXTRAOP_INSTOF etc */ +#define DUK_IVAL_PROP 4 /* property access */ +#define DUK_IVAL_VAR 5 /* variable access */ + +#define DUK_ISPEC_NONE 0 /* no value */ +#define DUK_ISPEC_VALUE 1 /* value resides in 'valstack_idx' */ +#define DUK_ISPEC_REGCONST 2 /* value resides in a register or constant */ + +/* bit mask which indicates that a regconst is a constant instead of a register */ +#define DUK_JS_CONST_MARKER 0x80000000UL + +/* type to represent a reg/const reference during compilation */ +typedef duk_uint32_t duk_regconst_t; + +/* type to represent a straight register reference, with <0 indicating none */ +typedef duk_int32_t duk_reg_t; + +typedef struct { + duk_small_uint_t t; /* DUK_ISPEC_XXX */ + duk_regconst_t regconst; + duk_idx_t valstack_idx; /* always set; points to a reserved valstack slot */ +} duk_ispec; + +typedef struct { + /* + * PLAIN: x1 + * ARITH: x1 x2 + * PROP: x1.x2 + * VAR: x1 (name) + */ + + /* XXX: can be optimized for smaller footprint esp. on 32-bit environments */ + duk_small_uint_t t; /* DUK_IVAL_XXX */ + duk_small_uint_t op; /* bytecode opcode (or extraop) for binary ops */ + duk_ispec x1; + duk_ispec x2; +} duk_ivalue; + +/* + * Bytecode instruction representation during compilation + * + * Contains the actual instruction and (optionally) debug info. + */ + +struct duk_compiler_instr { + duk_instr_t ins; +#if defined(DUK_USE_PC2LINE) + duk_uint32_t line; +#endif +}; + +/* + * Compiler state + */ + +#define DUK_LABEL_FLAG_ALLOW_BREAK (1 << 0) +#define DUK_LABEL_FLAG_ALLOW_CONTINUE (1 << 1) + +#define DUK_DECL_TYPE_VAR 0 +#define DUK_DECL_TYPE_FUNC 1 + +/* XXX: optimize to 16 bytes */ +typedef struct { + duk_small_uint_t flags; + duk_int_t label_id; /* numeric label_id (-1 reserved as marker) */ + duk_hstring *h_label; /* borrowed label name */ + duk_int_t catch_depth; /* catch depth at point of definition */ + duk_int_t pc_label; /* pc of label statement: + * pc+1: break jump site + * pc+2: continue jump site + */ + + /* Fast jumps (which avoid longjmp) jump directly to the jump sites + * which are always known even while the iteration/switch statement + * is still being parsed. A final peephole pass "straightens out" + * the jumps. + */ +} duk_labelinfo; + +/* Compiling state of one function, eventually converted to duk_hcompiledfunction */ +struct duk_compiler_func { + /* These pointers are at the start of the struct so that they pack + * nicely. Mixing pointers and integer values is bad on some + * platforms (e.g. if int is 32 bits and pointers are 64 bits). + */ + + duk_bufwriter_ctx bw_code; /* bufwriter for code */ + + duk_hstring *h_name; /* function name (borrowed reference), ends up in _name */ + /* h_code: held in bw_code */ + duk_hobject *h_consts; /* array */ + duk_hobject *h_funcs; /* array of function templates: [func1, offset1, line1, func2, offset2, line2] + * offset/line points to closing brace to allow skipping on pass 2 + */ + duk_hobject *h_decls; /* array of declarations: [ name1, val1, name2, val2, ... ] + * valN = (typeN) | (fnum << 8), where fnum is inner func number (0 for vars) + * record function and variable declarations in pass 1 + */ + duk_hobject *h_labelnames; /* array of active label names */ + duk_hbuffer_dynamic *h_labelinfos; /* C array of duk_labelinfo */ + duk_hobject *h_argnames; /* array of formal argument names (-> _Formals) */ + duk_hobject *h_varmap; /* variable map for pass 2 (identifier -> register number or null (unmapped)) */ + + /* value stack indices for tracking objects */ + /* code_idx: not needed */ + duk_idx_t consts_idx; + duk_idx_t funcs_idx; + duk_idx_t decls_idx; + duk_idx_t labelnames_idx; + duk_idx_t labelinfos_idx; + duk_idx_t argnames_idx; + duk_idx_t varmap_idx; + + /* temp reg handling */ + duk_reg_t temp_first; /* first register that is a temporary (below: variables) */ + duk_reg_t temp_next; /* next temporary register to allocate */ + duk_reg_t temp_max; /* highest value of temp_reg (temp_max - 1 is highest used reg) */ + + /* shuffle registers if large number of regs/consts */ + duk_reg_t shuffle1; + duk_reg_t shuffle2; + duk_reg_t shuffle3; + + /* stats for current expression being parsed */ + duk_int_t nud_count; + duk_int_t led_count; + duk_int_t paren_level; /* parenthesis count, 0 = top level */ + duk_bool_t expr_lhs; /* expression is left-hand-side compatible */ + duk_bool_t allow_in; /* current paren level allows 'in' token */ + + /* misc */ + duk_int_t stmt_next; /* statement id allocation (running counter) */ + duk_int_t label_next; /* label id allocation (running counter) */ + duk_int_t catch_depth; /* catch stack depth */ + duk_int_t with_depth; /* with stack depth (affects identifier lookups) */ + duk_int_t fnum_next; /* inner function numbering */ + duk_int_t num_formals; /* number of formal arguments */ + duk_reg_t reg_stmt_value; /* register for writing value of 'non-empty' statements (global or eval code), -1 is marker */ +#if defined(DUK_USE_DEBUGGER_SUPPORT) + duk_int_t min_line; /* XXX: typing (duk_hcompiledfunction has duk_uint32_t) */ + duk_int_t max_line; +#endif + + /* status booleans */ + duk_bool_t is_function; /* is an actual function (not global/eval code) */ + duk_bool_t is_eval; /* is eval code */ + duk_bool_t is_global; /* is global code */ + duk_bool_t is_setget; /* is a setter/getter */ + duk_bool_t is_decl; /* is a function declaration (as opposed to function expression) */ + duk_bool_t is_strict; /* function is strict */ + duk_bool_t is_notail; /* function must not be tail called */ + duk_bool_t in_directive_prologue; /* parsing in "directive prologue", recognize directives */ + duk_bool_t in_scanning; /* parsing in "scanning" phase (first pass) */ + duk_bool_t may_direct_eval; /* function may call direct eval */ + duk_bool_t id_access_arguments; /* function refers to 'arguments' identifier */ + duk_bool_t id_access_slow; /* function makes one or more slow path accesses */ + duk_bool_t is_arguments_shadowed; /* argument/function declaration shadows 'arguments' */ + duk_bool_t needs_shuffle; /* function needs shuffle registers */ + duk_bool_t reject_regexp_in_adv; /* reject RegExp literal on next advance() call; needed for handling IdentifierName productions */ +}; + +struct duk_compiler_ctx { + duk_hthread *thr; + + /* filename being compiled (ends up in functions' '_filename' property) */ + duk_hstring *h_filename; /* borrowed reference */ + + /* lexing (tokenization) state (contains two valstack slot indices) */ + duk_lexer_ctx lex; + + /* current and previous token for parsing */ + duk_token prev_token; + duk_token curr_token; + duk_idx_t tok11_idx; /* curr_token slot1 (matches 'lex' slot1_idx) */ + duk_idx_t tok12_idx; /* curr_token slot2 (matches 'lex' slot2_idx) */ + duk_idx_t tok21_idx; /* prev_token slot1 */ + duk_idx_t tok22_idx; /* prev_token slot2 */ + + /* recursion limit */ + duk_int_t recursion_depth; + duk_int_t recursion_limit; + + /* code emission temporary */ + duk_int_t emit_jumpslot_pc; + + /* current function being compiled (embedded instead of pointer for more compact access) */ + duk_compiler_func curr_func; +}; + +/* + * Prototypes + */ + +#define DUK_JS_COMPILE_FLAG_EVAL (1 << 0) /* source is eval code (not global) */ +#define DUK_JS_COMPILE_FLAG_STRICT (1 << 1) /* strict outer context */ +#define DUK_JS_COMPILE_FLAG_FUNCEXPR (1 << 2) /* source is a function expression (used for Function constructor) */ + +DUK_INTERNAL_DECL void duk_js_compile(duk_hthread *thr, const duk_uint8_t *src_buffer, duk_size_t src_length, duk_small_uint_t flags); + +#endif /* DUK_JS_COMPILER_H_INCLUDED */ +/* + * Regular expression structs, constants, and bytecode defines. + */ + +#ifndef DUK_REGEXP_H_INCLUDED +#define DUK_REGEXP_H_INCLUDED + +/* maximum bytecode copies for {n,m} quantifiers */ +#define DUK_RE_MAX_ATOM_COPIES 1000 + +/* regexp compilation limits */ +#define DUK_RE_COMPILE_TOKEN_LIMIT 100000000L /* 1e8 */ + +/* regexp execution limits */ +#define DUK_RE_EXECUTE_STEPS_LIMIT 1000000000L /* 1e9 */ + +/* regexp opcodes */ +#define DUK_REOP_MATCH 1 +#define DUK_REOP_CHAR 2 +#define DUK_REOP_PERIOD 3 +#define DUK_REOP_RANGES 4 +#define DUK_REOP_INVRANGES 5 +#define DUK_REOP_JUMP 6 +#define DUK_REOP_SPLIT1 7 +#define DUK_REOP_SPLIT2 8 +#define DUK_REOP_SQMINIMAL 9 +#define DUK_REOP_SQGREEDY 10 +#define DUK_REOP_SAVE 11 +#define DUK_REOP_WIPERANGE 12 +#define DUK_REOP_LOOKPOS 13 +#define DUK_REOP_LOOKNEG 14 +#define DUK_REOP_BACKREFERENCE 15 +#define DUK_REOP_ASSERT_START 16 +#define DUK_REOP_ASSERT_END 17 +#define DUK_REOP_ASSERT_WORD_BOUNDARY 18 +#define DUK_REOP_ASSERT_NOT_WORD_BOUNDARY 19 + +/* flags */ +#define DUK_RE_FLAG_GLOBAL (1 << 0) +#define DUK_RE_FLAG_IGNORE_CASE (1 << 1) +#define DUK_RE_FLAG_MULTILINE (1 << 2) + +struct duk_re_matcher_ctx { + duk_hthread *thr; + + duk_uint32_t re_flags; + const duk_uint8_t *input; + const duk_uint8_t *input_end; + const duk_uint8_t *bytecode; + const duk_uint8_t *bytecode_end; + const duk_uint8_t **saved; /* allocated from valstack (fixed buffer) */ + duk_uint32_t nsaved; + duk_uint32_t recursion_depth; + duk_uint32_t recursion_limit; + duk_uint32_t steps_count; + duk_uint32_t steps_limit; +}; + +struct duk_re_compiler_ctx { + duk_hthread *thr; + + duk_uint32_t re_flags; + duk_lexer_ctx lex; + duk_re_token curr_token; + duk_bufwriter_ctx bw; + duk_uint32_t captures; /* highest capture number emitted so far (used as: ++captures) */ + duk_uint32_t highest_backref; + duk_uint32_t recursion_depth; + duk_uint32_t recursion_limit; + duk_uint32_t nranges; /* internal temporary value, used for char classes */ +}; + +/* + * Prototypes + */ + +DUK_INTERNAL_DECL void duk_regexp_compile(duk_hthread *thr); +DUK_INTERNAL_DECL void duk_regexp_create_instance(duk_hthread *thr); +DUK_INTERNAL_DECL void duk_regexp_match(duk_hthread *thr); +DUK_INTERNAL_DECL void duk_regexp_match_force_global(duk_hthread *thr); /* hacky helper for String.prototype.split() */ + +#endif /* DUK_REGEXP_H_INCLUDED */ +/* + * Heap header definition and assorted macros, including ref counting. + * Access all fields through the accessor macros. + */ + +#ifndef DUK_HEAPHDR_H_INCLUDED +#define DUK_HEAPHDR_H_INCLUDED + +/* + * Common heap header + * + * All heap objects share the same flags and refcount fields. Objects other + * than strings also need to have a single or double linked list pointers + * for insertion into the "heap allocated" list. Strings are held in the + * heap-wide string table so they don't need link pointers. + * + * Technically, 'h_refcount' must be wide enough to guarantee that it cannot + * wrap (otherwise objects might be freed incorrectly after wrapping). This + * means essentially that the refcount field must be as wide as data pointers. + * On 64-bit platforms this means that the refcount needs to be 64 bits even + * if an 'int' is 32 bits. This is a bit unfortunate, and compromising on + * this might be reasonable in the future. + * + * Heap header size on 32-bit platforms: 8 bytes without reference counting, + * 16 bytes with reference counting. + */ + +struct duk_heaphdr { + duk_uint32_t h_flags; + +#if defined(DUK_USE_REFERENCE_COUNTING) +#if defined(DUK_USE_REFCOUNT16) + duk_uint16_t h_refcount16; +#else + duk_size_t h_refcount; +#endif +#endif + +#if defined(DUK_USE_HEAPPTR16) + duk_uint16_t h_next16; +#else + duk_heaphdr *h_next; +#endif + +#if defined(DUK_USE_DOUBLE_LINKED_HEAP) + /* refcounting requires direct heap frees, which in turn requires a dual linked heap */ +#if defined(DUK_USE_HEAPPTR16) + duk_uint16_t h_prev16; +#else + duk_heaphdr *h_prev; +#endif +#endif + + /* When DUK_USE_HEAPPTR16 (and DUK_USE_REFCOUNT16) is in use, the + * struct won't align nicely to 4 bytes. This 16-bit extra field + * is added to make the alignment clean; the field can be used by + * heap objects when 16-bit packing is used. This field is now + * conditional to DUK_USE_HEAPPTR16 only, but it is intended to be + * used with DUK_USE_REFCOUNT16 and DUK_USE_DOUBLE_LINKED_HEAP; + * this only matter to low memory environments anyway. + */ +#if defined(DUK_USE_HEAPPTR16) + duk_uint16_t h_extra16; +#endif +}; + +struct duk_heaphdr_string { + /* 16 bits would be enough for shared heaphdr flags and duk_hstring + * flags. The initial parts of duk_heaphdr_string and duk_heaphdr + * must match so changing the flags field size here would be quite + * awkward. However, to minimize struct size, we can pack at least + * 16 bits of duk_hstring data into the flags field. + */ + duk_uint32_t h_flags; + +#if defined(DUK_USE_REFERENCE_COUNTING) +#if defined(DUK_USE_REFCOUNT16) + duk_uint16_t h_refcount16; + duk_uint16_t h_strextra16; /* round out to 8 bytes */ +#else + duk_size_t h_refcount; +#endif +#endif +}; + +#define DUK_HEAPHDR_FLAGS_TYPE_MASK 0x00000003UL +#define DUK_HEAPHDR_FLAGS_FLAG_MASK (~DUK_HEAPHDR_FLAGS_TYPE_MASK) + + /* 2 bits for heap type */ +#define DUK_HEAPHDR_FLAGS_HEAP_START 2 /* 5 heap flags */ +#define DUK_HEAPHDR_FLAGS_USER_START 7 /* 25 user flags */ + +#define DUK_HEAPHDR_HEAP_FLAG_NUMBER(n) (DUK_HEAPHDR_FLAGS_HEAP_START + (n)) +#define DUK_HEAPHDR_USER_FLAG_NUMBER(n) (DUK_HEAPHDR_FLAGS_USER_START + (n)) +#define DUK_HEAPHDR_HEAP_FLAG(n) (1UL << (DUK_HEAPHDR_FLAGS_HEAP_START + (n))) +#define DUK_HEAPHDR_USER_FLAG(n) (1UL << (DUK_HEAPHDR_FLAGS_USER_START + (n))) + +#define DUK_HEAPHDR_FLAG_REACHABLE DUK_HEAPHDR_HEAP_FLAG(0) /* mark-and-sweep: reachable */ +#define DUK_HEAPHDR_FLAG_TEMPROOT DUK_HEAPHDR_HEAP_FLAG(1) /* mark-and-sweep: children not processed */ +#define DUK_HEAPHDR_FLAG_FINALIZABLE DUK_HEAPHDR_HEAP_FLAG(2) /* mark-and-sweep: finalizable (on current pass) */ +#define DUK_HEAPHDR_FLAG_FINALIZED DUK_HEAPHDR_HEAP_FLAG(3) /* mark-and-sweep: finalized (on previous pass) */ +#define DUK_HEAPHDR_FLAG_READONLY DUK_HEAPHDR_HEAP_FLAG(4) /* read-only object, in code section */ + +#define DUK_HTYPE_MIN 1 +#define DUK_HTYPE_STRING 1 +#define DUK_HTYPE_OBJECT 2 +#define DUK_HTYPE_BUFFER 3 +#define DUK_HTYPE_MAX 3 + +#if defined(DUK_USE_HEAPPTR16) +#define DUK_HEAPHDR_GET_NEXT(heap,h) \ + ((duk_heaphdr *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->h_next16)) +#define DUK_HEAPHDR_SET_NEXT(heap,h,val) do { \ + (h)->h_next16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) val); \ + } while (0) +#else +#define DUK_HEAPHDR_GET_NEXT(heap,h) ((h)->h_next) +#define DUK_HEAPHDR_SET_NEXT(heap,h,val) do { \ + (h)->h_next = (val); \ + } while (0) +#endif + +#if defined(DUK_USE_DOUBLE_LINKED_HEAP) +#if defined(DUK_USE_HEAPPTR16) +#define DUK_HEAPHDR_GET_PREV(heap,h) \ + ((duk_heaphdr *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->h_prev16)) +#define DUK_HEAPHDR_SET_PREV(heap,h,val) do { \ + (h)->h_prev16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (val)); \ + } while (0) +#else +#define DUK_HEAPHDR_GET_PREV(heap,h) ((h)->h_prev) +#define DUK_HEAPHDR_SET_PREV(heap,h,val) do { \ + (h)->h_prev = (val); \ + } while (0) +#endif +#endif + +#if defined(DUK_USE_REFERENCE_COUNTING) +#if defined(DUK_USE_REFCOUNT16) +#define DUK_HEAPHDR_GET_REFCOUNT(h) ((h)->h_refcount16) +#define DUK_HEAPHDR_SET_REFCOUNT(h,val) do { \ + (h)->h_refcount16 = (val); \ + } while (0) +#define DUK_HEAPHDR_PREINC_REFCOUNT(h) (++(h)->h_refcount16) /* result: updated refcount */ +#define DUK_HEAPHDR_PREDEC_REFCOUNT(h) (--(h)->h_refcount16) /* result: updated refcount */ +#else +#define DUK_HEAPHDR_GET_REFCOUNT(h) ((h)->h_refcount) +#define DUK_HEAPHDR_SET_REFCOUNT(h,val) do { \ + (h)->h_refcount = (val); \ + } while (0) +#define DUK_HEAPHDR_PREINC_REFCOUNT(h) (++(h)->h_refcount) /* result: updated refcount */ +#define DUK_HEAPHDR_PREDEC_REFCOUNT(h) (--(h)->h_refcount) /* result: updated refcount */ +#endif +#else +/* refcount macros not defined without refcounting, caller must #ifdef now */ +#endif /* DUK_USE_REFERENCE_COUNTING */ + +/* + * Note: type is treated as a field separate from flags, so some masking is + * involved in the macros below. + */ + +#define DUK_HEAPHDR_GET_FLAGS_RAW(h) ((h)->h_flags) + +#define DUK_HEAPHDR_GET_FLAGS(h) ((h)->h_flags & DUK_HEAPHDR_FLAGS_FLAG_MASK) +#define DUK_HEAPHDR_SET_FLAGS(h,val) do { \ + (h)->h_flags = ((h)->h_flags & ~(DUK_HEAPHDR_FLAGS_FLAG_MASK)) | (val); \ + } while (0) + +#define DUK_HEAPHDR_GET_TYPE(h) ((h)->h_flags & DUK_HEAPHDR_FLAGS_TYPE_MASK) +#define DUK_HEAPHDR_SET_TYPE(h,val) do { \ + (h)->h_flags = ((h)->h_flags & ~(DUK_HEAPHDR_FLAGS_TYPE_MASK)) | (val); \ + } while (0) + +#define DUK_HEAPHDR_HTYPE_VALID(h) ( \ + DUK_HEAPHDR_GET_TYPE((h)) >= DUK_HTYPE_MIN && \ + DUK_HEAPHDR_GET_TYPE((h)) <= DUK_HTYPE_MAX \ + ) + +#define DUK_HEAPHDR_SET_TYPE_AND_FLAGS(h,tval,fval) do { \ + (h)->h_flags = ((tval) & DUK_HEAPHDR_FLAGS_TYPE_MASK) | \ + ((fval) & DUK_HEAPHDR_FLAGS_FLAG_MASK); \ + } while (0) + +#define DUK_HEAPHDR_SET_FLAG_BITS(h,bits) do { \ + DUK_ASSERT(((bits) & ~(DUK_HEAPHDR_FLAGS_FLAG_MASK)) == 0); \ + (h)->h_flags |= (bits); \ + } while (0) + +#define DUK_HEAPHDR_CLEAR_FLAG_BITS(h,bits) do { \ + DUK_ASSERT(((bits) & ~(DUK_HEAPHDR_FLAGS_FLAG_MASK)) == 0); \ + (h)->h_flags &= ~((bits)); \ + } while (0) + +#define DUK_HEAPHDR_CHECK_FLAG_BITS(h,bits) (((h)->h_flags & (bits)) != 0) + +#define DUK_HEAPHDR_SET_REACHABLE(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_REACHABLE) +#define DUK_HEAPHDR_CLEAR_REACHABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_REACHABLE) +#define DUK_HEAPHDR_HAS_REACHABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_REACHABLE) + +#define DUK_HEAPHDR_SET_TEMPROOT(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_TEMPROOT) +#define DUK_HEAPHDR_CLEAR_TEMPROOT(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_TEMPROOT) +#define DUK_HEAPHDR_HAS_TEMPROOT(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_TEMPROOT) + +#define DUK_HEAPHDR_SET_FINALIZABLE(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZABLE) +#define DUK_HEAPHDR_CLEAR_FINALIZABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZABLE) +#define DUK_HEAPHDR_HAS_FINALIZABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZABLE) + +#define DUK_HEAPHDR_SET_FINALIZED(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZED) +#define DUK_HEAPHDR_CLEAR_FINALIZED(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZED) +#define DUK_HEAPHDR_HAS_FINALIZED(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZED) + +#define DUK_HEAPHDR_SET_READONLY(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_READONLY) +#define DUK_HEAPHDR_CLEAR_READONLY(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_READONLY) +#define DUK_HEAPHDR_HAS_READONLY(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_READONLY) + +/* get or set a range of flags; m=first bit number, n=number of bits */ +#define DUK_HEAPHDR_GET_FLAG_RANGE(h,m,n) (((h)->h_flags >> (m)) & ((1UL << (n)) - 1UL)) + +#define DUK_HEAPHDR_SET_FLAG_RANGE(h,m,n,v) do { \ + (h)->h_flags = \ + ((h)->h_flags & (~(((1 << (n)) - 1) << (m)))) \ + | ((v) << (m)); \ + } while (0) + +/* init pointer fields to null */ +#if defined(DUK_USE_DOUBLE_LINKED_HEAP) +#define DUK_HEAPHDR_INIT_NULLS(h) do { \ + DUK_HEAPHDR_SET_NEXT((h), (void *) NULL); \ + DUK_HEAPHDR_SET_PREV((h), (void *) NULL); \ + } while (0) +#else +#define DUK_HEAPHDR_INIT_NULLS(h) do { \ + DUK_HEAPHDR_SET_NEXT((h), (void *) NULL); \ + } while (0) +#endif + +#define DUK_HEAPHDR_STRING_INIT_NULLS(h) /* currently nop */ + +/* + * Assert helpers + */ + +/* Check that prev/next links are consistent: if e.g. h->prev is != NULL, + * h->prev->next should point back to h. + */ +#if defined(DUK_USE_DOUBLE_LINKED_HEAP) && defined(DUK_USE_ASSERTIONS) +#define DUK_ASSERT_HEAPHDR_LINKS(heap,h) do { \ + if ((h) != NULL) { \ + duk_heaphdr *h__prev, *h__next; \ + h__prev = DUK_HEAPHDR_GET_PREV((heap), (h)); \ + h__next = DUK_HEAPHDR_GET_NEXT((heap), (h)); \ + DUK_ASSERT(h__prev == NULL || (DUK_HEAPHDR_GET_NEXT((heap), h__prev) == (h))); \ + DUK_ASSERT(h__next == NULL || (DUK_HEAPHDR_GET_PREV((heap), h__next) == (h))); \ + } \ + } while (0) +#else +#define DUK_ASSERT_HEAPHDR_LINKS(heap,h) do {} while (0) +#endif + +/* + * Reference counting helper macros. The macros take a thread argument + * and must thus always be executed in a specific thread context. The + * thread argument is needed for features like finalization. Currently + * it is not required for INCREF, but it is included just in case. + * + * Note that 'raw' macros such as DUK_HEAPHDR_GET_REFCOUNT() are not + * defined without DUK_USE_REFERENCE_COUNTING, so caller must #ifdef + * around them. + */ + +#if defined(DUK_USE_REFERENCE_COUNTING) + +#if defined(DUK_USE_ROM_OBJECTS) +/* With ROM objects "needs refcount update" is true when the value is + * heap allocated and is not a ROM object. + */ +/* XXX: double evaluation for 'tv' argument. */ +#define DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv) \ + (DUK_TVAL_IS_HEAP_ALLOCATED((tv)) && !DUK_HEAPHDR_HAS_READONLY(DUK_TVAL_GET_HEAPHDR((tv)))) +#define DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE(h) (!DUK_HEAPHDR_HAS_READONLY((h))) +#else /* DUK_USE_ROM_OBJECTS */ +/* Without ROM objects "needs refcount update" == is heap allocated. */ +#define DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv) DUK_TVAL_IS_HEAP_ALLOCATED((tv)) +#define DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE(h) 1 +#endif /* DUK_USE_ROM_OBJECTS */ + +/* Fast variants, inline refcount operations except for refzero handling. + * Can be used explicitly when speed is always more important than size. + * For a good compiler and a single file build, these are basically the + * same as a forced inline. + */ +#define DUK_TVAL_INCREF_FAST(thr,tv) do { \ + duk_tval *duk__tv = (tv); \ + DUK_ASSERT(duk__tv != NULL); \ + if (DUK_TVAL_NEEDS_REFCOUNT_UPDATE(duk__tv)) { \ + duk_heaphdr *duk__h = DUK_TVAL_GET_HEAPHDR(duk__tv); \ + DUK_ASSERT(duk__h != NULL); \ + DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \ + DUK_HEAPHDR_PREINC_REFCOUNT(duk__h); \ + } \ + } while (0) +#define DUK_TVAL_DECREF_FAST(thr,tv) do { \ + duk_tval *duk__tv = (tv); \ + DUK_ASSERT(duk__tv != NULL); \ + if (DUK_TVAL_NEEDS_REFCOUNT_UPDATE(duk__tv)) { \ + duk_heaphdr *duk__h = DUK_TVAL_GET_HEAPHDR(duk__tv); \ + DUK_ASSERT(duk__h != NULL); \ + DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \ + DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(duk__h) > 0); \ + if (DUK_HEAPHDR_PREDEC_REFCOUNT(duk__h) == 0) { \ + duk_heaphdr_refzero((thr), duk__h); \ + } \ + } \ + } while (0) +#define DUK_HEAPHDR_INCREF_FAST(thr,h) do { \ + duk_heaphdr *duk__h = (duk_heaphdr *) (h); \ + DUK_ASSERT(duk__h != NULL); \ + DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \ + if (DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE(duk__h)) { \ + DUK_HEAPHDR_PREINC_REFCOUNT(duk__h); \ + } \ + } while (0) +#define DUK_HEAPHDR_DECREF_FAST(thr,h) do { \ + duk_heaphdr *duk__h = (duk_heaphdr *) (h); \ + DUK_ASSERT(duk__h != NULL); \ + DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \ + DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(duk__h) > 0); \ + if (DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE(duk__h)) { \ + if (DUK_HEAPHDR_PREDEC_REFCOUNT(duk__h) == 0) { \ + duk_heaphdr_refzero((thr), duk__h); \ + } \ + } \ + } while (0) + +/* Slow variants, call to a helper to reduce code size. + * Can be used explicitly when size is always more important than speed. + */ +#define DUK_TVAL_INCREF_SLOW(thr,tv) do { \ + duk_tval_incref((tv)); \ + } while (0) +#define DUK_TVAL_DECREF_SLOW(thr,tv) do { \ + duk_tval_decref((thr), (tv)); \ + } while (0) +#define DUK_HEAPHDR_INCREF_SLOW(thr,h) do { \ + duk_heaphdr_incref((duk_heaphdr *) (h)); \ + } while (0) +#define DUK_HEAPHDR_DECREF_SLOW(thr,h) do { \ + duk_heaphdr_decref((thr), (duk_heaphdr *) (h)); \ + } while (0) + +/* Default variants. Selection depends on speed/size preference. + * Concretely: with gcc 4.8.1 -Os x64 the difference in final binary + * is about +1kB for _FAST variants. + */ +#if defined(DUK_USE_FAST_REFCOUNT_DEFAULT) +#define DUK_TVAL_INCREF(thr,tv) DUK_TVAL_INCREF_FAST((thr),(tv)) +#define DUK_TVAL_DECREF(thr,tv) DUK_TVAL_DECREF_FAST((thr),(tv)) +#define DUK_HEAPHDR_INCREF(thr,h) DUK_HEAPHDR_INCREF_FAST((thr),(h)) +#define DUK_HEAPHDR_DECREF(thr,h) DUK_HEAPHDR_DECREF_FAST((thr),(h)) +#else +#define DUK_TVAL_INCREF(thr,tv) DUK_TVAL_INCREF_SLOW((thr),(tv)) +#define DUK_TVAL_DECREF(thr,tv) DUK_TVAL_DECREF_SLOW((thr),(tv)) +#define DUK_HEAPHDR_INCREF(thr,h) DUK_HEAPHDR_INCREF_SLOW((thr),(h)) +#define DUK_HEAPHDR_DECREF(thr,h) DUK_HEAPHDR_DECREF_SLOW((thr),(h)) +#endif + +/* Casting convenience. */ +#define DUK_HSTRING_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h)) +#define DUK_HSTRING_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) (h)) +#define DUK_HOBJECT_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h)) +#define DUK_HOBJECT_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) (h)) +#define DUK_HBUFFER_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h)) +#define DUK_HBUFFER_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) (h)) +#define DUK_HCOMPILEDFUNCTION_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj) +#define DUK_HCOMPILEDFUNCTION_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj) +#define DUK_HNATIVEFUNCTION_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj) +#define DUK_HNATIVEFUNCTION_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj) +#define DUK_HBUFFEROBJECT_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj) +#define DUK_HBUFFEROBJECT_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj) +#define DUK_HTHREAD_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj) +#define DUK_HTHREAD_DECREF(thr,h) DUK_HEAPHDR_DECREF((thr),(duk_heaphdr *) &(h)->obj) + +/* Convenience for some situations; the above macros don't allow NULLs + * for performance reasons. + */ +#define DUK_HOBJECT_INCREF_ALLOWNULL(thr,h) do { \ + if ((h) != NULL) { \ + DUK_HEAPHDR_INCREF((thr), (duk_heaphdr *) (h)); \ + } \ + } while (0) +#define DUK_HOBJECT_DECREF_ALLOWNULL(thr,h) do { \ + if ((h) != NULL) { \ + DUK_HEAPHDR_DECREF((thr), (duk_heaphdr *) (h)); \ + } \ + } while (0) + +/* + * Macros to set a duk_tval and update refcount of the target (decref the + * old value and incref the new value if necessary). This is both performance + * and footprint critical; any changes made should be measured for size/speed. + */ + +#define DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0(thr,tvptr_dst) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_UNDEFINED(tv__dst); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) + +#define DUK_TVAL_SET_UNUSED_UPDREF_ALT0(thr,tvptr_dst) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_UNUSED(tv__dst); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) + +#define DUK_TVAL_SET_NULL_UPDREF_ALT0(thr,tvptr_dst) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_NULL(tv__dst); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) + +#define DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_BOOLEAN(tv__dst, (newval)); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) + +#define DUK_TVAL_SET_NUMBER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_NUMBER(tv__dst, (newval)); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) +#define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_NUMBER_CHKFAST(tv__dst, (newval)); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) +#define DUK_TVAL_SET_DOUBLE_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_DOUBLE(tv__dst, (newval)); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) +#define DUK_TVAL_SET_NAN_UPDREF_ALT0(thr,tvptr_dst) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_NAN(tv__dst); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) +#if defined(DUK_USE_FASTINT) +#define DUK_TVAL_SET_FASTINT_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_FASTINT(tv__dst, (newval)); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) +#define DUK_TVAL_SET_FASTINT_I32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_FASTINT_I32(tv__dst, (newval)); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) +#define DUK_TVAL_SET_FASTINT_U32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_FASTINT_U32(tv__dst, (newval)); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) +#else +#define DUK_TVAL_SET_DOUBLE_CAST_UPDREF(thr,tvptr_dst,newval) \ + DUK_TVAL_SET_DOUBLE_UPDREF((thr), (tvptr_dst), (duk_double_t) (newval)) +#endif /* DUK_USE_FASTINT */ + +#define DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0(thr,tvptr_dst,lf_v,lf_fp,lf_flags) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_LIGHTFUNC(tv__dst, (lf_v), (lf_fp), (lf_flags)); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) + +#define DUK_TVAL_SET_STRING_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_STRING(tv__dst, (newval)); \ + DUK_HSTRING_INCREF((thr), (newval)); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) + +#define DUK_TVAL_SET_OBJECT_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_OBJECT(tv__dst, (newval)); \ + DUK_HOBJECT_INCREF((thr), (newval)); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) + +#define DUK_TVAL_SET_BUFFER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_BUFFER(tv__dst, (newval)); \ + DUK_HBUFFER_INCREF((thr), (newval)); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) + +#define DUK_TVAL_SET_POINTER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_POINTER(tv__dst, (newval)); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) + +/* DUK_TVAL_SET_TVAL_UPDREF() is used a lot in executor, property lookups, + * etc, so it's very important for performance. Measure when changing. + * + * NOTE: the source and destination duk_tval pointers may be the same, and + * the macros MUST deal with that correctly. + */ + +/* Original idiom used, minimal code size. */ +#define DUK_TVAL_SET_TVAL_UPDREF_ALT0(thr,tvptr_dst,tvptr_src) do { \ + duk_tval *tv__dst, *tv__src; duk_tval tv__tmp; \ + tv__dst = (tvptr_dst); tv__src = (tvptr_src); \ + DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \ + DUK_TVAL_SET_TVAL(tv__dst, tv__src); \ + DUK_TVAL_INCREF((thr), tv__src); \ + DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \ + } while (0) + +/* Faster alternative: avoid making a temporary copy of tvptr_dst and use + * fast incref/decref macros. + */ +#define DUK_TVAL_SET_TVAL_UPDREF_ALT1(thr,tvptr_dst,tvptr_src) do { \ + duk_tval *tv__dst, *tv__src; duk_heaphdr *h__obj; \ + tv__dst = (tvptr_dst); tv__src = (tvptr_src); \ + DUK_TVAL_INCREF_FAST((thr), tv__src); \ + if (DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv__dst)) { \ + h__obj = DUK_TVAL_GET_HEAPHDR(tv__dst); \ + DUK_ASSERT(h__obj != NULL); \ + DUK_TVAL_SET_TVAL(tv__dst, tv__src); \ + DUK_HEAPHDR_DECREF_FAST((thr), h__obj); /* side effects */ \ + } else { \ + DUK_TVAL_SET_TVAL(tv__dst, tv__src); \ + } \ + } while (0) + +/* XXX: no optimized variants yet */ +#define DUK_TVAL_SET_UNDEFINED_UPDREF DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0 +#define DUK_TVAL_SET_UNUSED_UPDREF DUK_TVAL_SET_UNUSED_UPDREF_ALT0 +#define DUK_TVAL_SET_NULL_UPDREF DUK_TVAL_SET_NULL_UPDREF_ALT0 +#define DUK_TVAL_SET_BOOLEAN_UPDREF DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0 +#define DUK_TVAL_SET_NUMBER_UPDREF DUK_TVAL_SET_NUMBER_UPDREF_ALT0 +#define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0 +#define DUK_TVAL_SET_DOUBLE_UPDREF DUK_TVAL_SET_DOUBLE_UPDREF_ALT0 +#define DUK_TVAL_SET_NAN_UPDREF DUK_TVAL_SET_NAN_UPDREF_ALT0 +#if defined(DUK_USE_FASTINT) +#define DUK_TVAL_SET_FASTINT_UPDREF DUK_TVAL_SET_FASTINT_UPDREF_ALT0 +#define DUK_TVAL_SET_FASTINT_I32_UPDREF DUK_TVAL_SET_FASTINT_I32_UPDREF_ALT0 +#define DUK_TVAL_SET_FASTINT_U32_UPDREF DUK_TVAL_SET_FASTINT_U32_UPDREF_ALT0 +#else +#define DUK_TVAL_SET_FASTINT_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF /* XXX: fast int-to-double */ +#define DUK_TVAL_SET_FASTINT_I32_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF +#define DUK_TVAL_SET_FASTINT_U32_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF +#endif /* DUK_USE_FASTINT */ +#define DUK_TVAL_SET_LIGHTFUNC_UPDREF DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0 +#define DUK_TVAL_SET_STRING_UPDREF DUK_TVAL_SET_STRING_UPDREF_ALT0 +#define DUK_TVAL_SET_OBJECT_UPDREF DUK_TVAL_SET_OBJECT_UPDREF_ALT0 +#define DUK_TVAL_SET_BUFFER_UPDREF DUK_TVAL_SET_BUFFER_UPDREF_ALT0 +#define DUK_TVAL_SET_POINTER_UPDREF DUK_TVAL_SET_POINTER_UPDREF_ALT0 + +#if defined(DUK_USE_FAST_REFCOUNT_DEFAULT) +/* Optimized for speed. */ +#define DUK_TVAL_SET_TVAL_UPDREF DUK_TVAL_SET_TVAL_UPDREF_ALT1 +#define DUK_TVAL_SET_TVAL_UPDREF_FAST DUK_TVAL_SET_TVAL_UPDREF_ALT1 +#define DUK_TVAL_SET_TVAL_UPDREF_SLOW DUK_TVAL_SET_TVAL_UPDREF_ALT0 +#else +/* Optimized for size. */ +#define DUK_TVAL_SET_TVAL_UPDREF DUK_TVAL_SET_TVAL_UPDREF_ALT0 +#define DUK_TVAL_SET_TVAL_UPDREF_FAST DUK_TVAL_SET_TVAL_UPDREF_ALT0 +#define DUK_TVAL_SET_TVAL_UPDREF_SLOW DUK_TVAL_SET_TVAL_UPDREF_ALT0 +#endif + +#else /* DUK_USE_REFERENCE_COUNTING */ + +#define DUK_TVAL_INCREF_FAST(thr,v) do {} while (0) /* nop */ +#define DUK_TVAL_DECREF_FAST(thr,v) do {} while (0) /* nop */ +#define DUK_TVAL_INCREF_SLOW(thr,v) do {} while (0) /* nop */ +#define DUK_TVAL_DECREF_SLOW(thr,v) do {} while (0) /* nop */ +#define DUK_TVAL_INCREF(thr,v) do {} while (0) /* nop */ +#define DUK_TVAL_DECREF(thr,v) do {} while (0) /* nop */ +#define DUK_HEAPHDR_INCREF_FAST(thr,h) do {} while (0) /* nop */ +#define DUK_HEAPHDR_DECREF_FAST(thr,h) do {} while (0) /* nop */ +#define DUK_HEAPHDR_INCREF_SLOW(thr,h) do {} while (0) /* nop */ +#define DUK_HEAPHDR_DECREF_SLOW(thr,h) do {} while (0) /* nop */ +#define DUK_HEAPHDR_INCREF(thr,h) do {} while (0) /* nop */ +#define DUK_HEAPHDR_DECREF(thr,h) do {} while (0) /* nop */ +#define DUK_HSTRING_INCREF(thr,h) do {} while (0) /* nop */ +#define DUK_HSTRING_DECREF(thr,h) do {} while (0) /* nop */ +#define DUK_HOBJECT_INCREF(thr,h) do {} while (0) /* nop */ +#define DUK_HOBJECT_DECREF(thr,h) do {} while (0) /* nop */ +#define DUK_HBUFFER_INCREF(thr,h) do {} while (0) /* nop */ +#define DUK_HBUFFER_DECREF(thr,h) do {} while (0) /* nop */ +#define DUK_HCOMPILEDFUNCTION_INCREF(thr,h) do {} while (0) /* nop */ +#define DUK_HCOMPILEDFUNCTION_DECREF(thr,h) do {} while (0) /* nop */ +#define DUK_HNATIVEFUNCTION_INCREF(thr,h) do {} while (0) /* nop */ +#define DUK_HNATIVEFUNCTION_DECREF(thr,h) do {} while (0) /* nop */ +#define DUK_HBUFFEROBJECT_INCREF(thr,h) do {} while (0) /* nop */ +#define DUK_HBUFFEROBJECT_DECREF(thr,h) do {} while (0) /* nop */ +#define DUK_HTHREAD_INCREF(thr,h) do {} while (0) /* nop */ +#define DUK_HTHREAD_DECREF(thr,h) do {} while (0) /* nop */ +#define DUK_HOBJECT_INCREF_ALLOWNULL(thr,h) do {} while (0) /* nop */ +#define DUK_HOBJECT_DECREF_ALLOWNULL(thr,h) do {} while (0) /* nop */ + +#define DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0(thr,tvptr_dst) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_UNDEFINED(tv__dst); \ + DUK_UNREF((thr)); \ + } while (0) + +#define DUK_TVAL_SET_UNUSED_UPDREF_ALT0(thr,tvptr_dst) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_UNUSED(tv__dst); \ + DUK_UNREF((thr)); \ + } while (0) + +#define DUK_TVAL_SET_NULL_UPDREF_ALT0(thr,tvptr_dst) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_NULL(tv__dst); \ + DUK_UNREF((thr)); \ + } while (0) + +#define DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_BOOLEAN(tv__dst, (newval)); \ + DUK_UNREF((thr)); \ + } while (0) + +#define DUK_TVAL_SET_NUMBER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_NUMBER(tv__dst, (newval)); \ + DUK_UNREF((thr)); \ + } while (0) +#define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_NUMBER_CHKFAST(tv__dst, (newval)); \ + DUK_UNREF((thr)); \ + } while (0) +#define DUK_TVAL_SET_DOUBLE_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_DOUBLE(tv__dst, (newval)); \ + DUK_UNREF((thr)); \ + } while (0) +#define DUK_TVAL_SET_NAN_UPDREF_ALT0(thr,tvptr_dst) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_NAN(tv__dst); \ + DUK_UNREF((thr)); \ + } while (0) +#if defined(DUK_USE_FASTINT) +#define DUK_TVAL_SET_FASTINT_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_FASTINT(tv__dst, (newval)); \ + DUK_UNREF((thr)); \ + } while (0) +#define DUK_TVAL_SET_FASTINT_I32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_FASTINT_I32(tv__dst, (newval)); \ + DUK_UNREF((thr)); \ + } while (0) +#define DUK_TVAL_SET_FASTINT_U32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_FASTINT_U32(tv__dst, (newval)); \ + DUK_UNREF((thr)); \ + } while (0) +#else +#define DUK_TVAL_SET_DOUBLE_CAST_UPDREF(thr,tvptr_dst,newval) \ + DUK_TVAL_SET_DOUBLE_UPDREF((thr), (tvptr_dst), (duk_double_t) (newval)) +#endif /* DUK_USE_FASTINT */ + +#define DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0(thr,tvptr_dst,lf_v,lf_fp,lf_flags) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_LIGHTFUNC(tv__dst, (lf_v), (lf_fp), (lf_flags)); \ + DUK_UNREF((thr)); \ + } while (0) + +#define DUK_TVAL_SET_STRING_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_STRING(tv__dst, (newval)); \ + DUK_UNREF((thr)); \ + } while (0) + +#define DUK_TVAL_SET_OBJECT_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_OBJECT(tv__dst, (newval)); \ + DUK_UNREF((thr)); \ + } while (0) + +#define DUK_TVAL_SET_BUFFER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_BUFFER(tv__dst, (newval)); \ + DUK_UNREF((thr)); \ + } while (0) + +#define DUK_TVAL_SET_POINTER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \ + duk_tval *tv__dst; tv__dst = (tvptr_dst); \ + DUK_TVAL_SET_POINTER(tv__dst, (newval)); \ + DUK_UNREF((thr)); \ + } while (0) + +#define DUK_TVAL_SET_TVAL_UPDREF_ALT0(thr,tvptr_dst,tvptr_src) do { \ + duk_tval *tv__dst, *tv__src; \ + tv__dst = (tvptr_dst); tv__src = (tvptr_src); \ + DUK_TVAL_SET_TVAL(tv__dst, tv__src); \ + DUK_UNREF((thr)); \ + } while (0) + +#define DUK_TVAL_SET_UNDEFINED_UPDREF DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0 +#define DUK_TVAL_SET_UNUSED_UPDREF DUK_TVAL_SET_UNUSED_UPDREF_ALT0 +#define DUK_TVAL_SET_NULL_UPDREF DUK_TVAL_SET_NULL_UPDREF_ALT0 +#define DUK_TVAL_SET_BOOLEAN_UPDREF DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0 +#define DUK_TVAL_SET_NUMBER_UPDREF DUK_TVAL_SET_NUMBER_UPDREF_ALT0 +#define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0 +#define DUK_TVAL_SET_DOUBLE_UPDREF DUK_TVAL_SET_DOUBLE_UPDREF_ALT0 +#define DUK_TVAL_SET_NAN_UPDREF DUK_TVAL_SET_NAN_UPDREF_ALT0 +#if defined(DUK_USE_FASTINT) +#define DUK_TVAL_SET_FASTINT_UPDREF DUK_TVAL_SET_FASTINT_UPDREF_ALT0 +#define DUK_TVAL_SET_FASTINT_I32_UPDREF DUK_TVAL_SET_FASTINT_I32_UPDREF_ALT0 +#define DUK_TVAL_SET_FASTINT_U32_UPDREF DUK_TVAL_SET_FASTINT_U32_UPDREF_ALT0 +#else +#define DUK_TVAL_SET_FASTINT_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF /* XXX: fast-int-to-double */ +#define DUK_TVAL_SET_FASTINT_I32_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF +#define DUK_TVAL_SET_FASTINT_U32_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF +#endif /* DUK_USE_FASTINT */ +#define DUK_TVAL_SET_LIGHTFUNC_UPDREF DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0 +#define DUK_TVAL_SET_STRING_UPDREF DUK_TVAL_SET_STRING_UPDREF_ALT0 +#define DUK_TVAL_SET_OBJECT_UPDREF DUK_TVAL_SET_OBJECT_UPDREF_ALT0 +#define DUK_TVAL_SET_BUFFER_UPDREF DUK_TVAL_SET_BUFFER_UPDREF_ALT0 +#define DUK_TVAL_SET_POINTER_UPDREF DUK_TVAL_SET_POINTER_UPDREF_ALT0 + +#define DUK_TVAL_SET_TVAL_UPDREF DUK_TVAL_SET_TVAL_UPDREF_ALT0 +#define DUK_TVAL_SET_TVAL_UPDREF_FAST DUK_TVAL_SET_TVAL_UPDREF_ALT0 +#define DUK_TVAL_SET_TVAL_UPDREF_SLOW DUK_TVAL_SET_TVAL_UPDREF_ALT0 + +#endif /* DUK_USE_REFERENCE_COUNTING */ + +#endif /* DUK_HEAPHDR_H_INCLUDED */ +/* + * Internal API calls which have (stack and other) semantics similar + * to the public API. + */ + +#ifndef DUK_API_INTERNAL_H_INCLUDED +#define DUK_API_INTERNAL_H_INCLUDED + +/* duk_push_sprintf constants */ +#define DUK_PUSH_SPRINTF_INITIAL_SIZE 256L +#define DUK_PUSH_SPRINTF_SANITY_LIMIT (1L * 1024L * 1024L * 1024L) + +/* Flag ORed to err_code to indicate __FILE__ / __LINE__ is not + * blamed as source of error for error fileName / lineNumber. + */ +#define DUK_ERRCODE_FLAG_NOBLAME_FILELINE (1L << 24) + +/* Valstack resize flags */ +#define DUK_VSRESIZE_FLAG_SHRINK (1 << 0) +#define DUK_VSRESIZE_FLAG_COMPACT (1 << 1) +#define DUK_VSRESIZE_FLAG_THROW (1 << 2) + +/* Current convention is to use duk_size_t for value stack sizes and global indices, + * and duk_idx_t for local frame indices. + */ +DUK_INTERNAL_DECL +duk_bool_t duk_valstack_resize_raw(duk_context *ctx, + duk_size_t min_new_size, + duk_small_uint_t flags); + +#if defined(DUK_USE_VERBOSE_ERRORS) && defined(DUK_USE_PARANOID_ERRORS) +DUK_INTERNAL_DECL const char *duk_get_type_name(duk_context *ctx, duk_idx_t index); +#endif + +DUK_INTERNAL_DECL duk_tval *duk_get_tval(duk_context *ctx, duk_idx_t index); +DUK_INTERNAL_DECL duk_tval *duk_require_tval(duk_context *ctx, duk_idx_t index); +DUK_INTERNAL_DECL void duk_push_tval(duk_context *ctx, duk_tval *tv); + +/* Push the current 'this' binding; throw TypeError if binding is not object + * coercible (CheckObjectCoercible). + */ +DUK_INTERNAL_DECL void duk_push_this_check_object_coercible(duk_context *ctx); + +/* duk_push_this() + CheckObjectCoercible() + duk_to_object() */ +DUK_INTERNAL_DECL duk_hobject *duk_push_this_coercible_to_object(duk_context *ctx); + +/* duk_push_this() + CheckObjectCoercible() + duk_to_string() */ +DUK_INTERNAL_DECL duk_hstring *duk_push_this_coercible_to_string(duk_context *ctx); + +/* Get a borrowed duk_tval pointer to the current 'this' binding. Caller must + * make sure there's an active callstack entry. Note that the returned pointer + * is unstable with regards to side effects. + */ +DUK_INTERNAL_DECL duk_tval *duk_get_borrowed_this_tval(duk_context *ctx); + +/* XXX: add fastint support? */ +#define duk_push_u64(ctx,val) \ + duk_push_number((ctx), (duk_double_t) (val)) +#define duk_push_i64(ctx,val) \ + duk_push_number((ctx), (duk_double_t) (val)) + +/* duk_push_(u)int() is guaranteed to support at least (un)signed 32-bit range */ +#define duk_push_u32(ctx,val) \ + duk_push_uint((ctx), (duk_uint_t) (val)) +#define duk_push_i32(ctx,val) \ + duk_push_int((ctx), (duk_int_t) (val)) + +/* sometimes stack and array indices need to go on the stack */ +#define duk_push_idx(ctx,val) \ + duk_push_int((ctx), (duk_int_t) (val)) +#define duk_push_uarridx(ctx,val) \ + duk_push_uint((ctx), (duk_uint_t) (val)) +#define duk_push_size_t(ctx,val) \ + duk_push_uint((ctx), (duk_uint_t) (val)) /* XXX: assumed to fit for now */ + +DUK_INTERNAL_DECL duk_hstring *duk_get_hstring(duk_context *ctx, duk_idx_t index); +DUK_INTERNAL_DECL duk_hobject *duk_get_hobject(duk_context *ctx, duk_idx_t index); +DUK_INTERNAL_DECL duk_hbuffer *duk_get_hbuffer(duk_context *ctx, duk_idx_t index); +DUK_INTERNAL_DECL duk_hthread *duk_get_hthread(duk_context *ctx, duk_idx_t index); +DUK_INTERNAL_DECL duk_hcompiledfunction *duk_get_hcompiledfunction(duk_context *ctx, duk_idx_t index); +DUK_INTERNAL_DECL duk_hnativefunction *duk_get_hnativefunction(duk_context *ctx, duk_idx_t index); + +DUK_INTERNAL_DECL duk_hobject *duk_get_hobject_with_class(duk_context *ctx, duk_idx_t index, duk_small_uint_t classnum); + +#if 0 /* This would be pointless: unexpected type and lightfunc would both return NULL */ +DUK_INTERNAL_DECL duk_hobject *duk_get_hobject_or_lfunc(duk_context *ctx, duk_idx_t index); +#endif +DUK_INTERNAL_DECL duk_hobject *duk_get_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index); + +#if 0 /*unused*/ +DUK_INTERNAL_DECL void *duk_get_voidptr(duk_context *ctx, duk_idx_t index); +#endif + +DUK_INTERNAL_DECL duk_hstring *duk_to_hstring(duk_context *ctx, duk_idx_t index); +#if defined(DUK_USE_DEBUGGER_SUPPORT) /* only needed by debugger for now */ +DUK_INTERNAL_DECL duk_hstring *duk_safe_to_hstring(duk_context *ctx, duk_idx_t index); +#endif +DUK_INTERNAL_DECL void duk_to_object_class_string_top(duk_context *ctx); +#if !defined(DUK_USE_PARANOID_ERRORS) +DUK_INTERNAL_DECL void duk_push_hobject_class_string(duk_context *ctx, duk_hobject *h); +#endif + +DUK_INTERNAL_DECL duk_int_t duk_to_int_clamped_raw(duk_context *ctx, duk_idx_t index, duk_int_t minval, duk_int_t maxval, duk_bool_t *out_clamped); /* out_clamped=NULL, RangeError if outside range */ +DUK_INTERNAL_DECL duk_int_t duk_to_int_clamped(duk_context *ctx, duk_idx_t index, duk_int_t minval, duk_int_t maxval); +DUK_INTERNAL_DECL duk_int_t duk_to_int_check_range(duk_context *ctx, duk_idx_t index, duk_int_t minval, duk_int_t maxval); +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL_DECL duk_uint8_t duk_to_uint8clamped(duk_context *ctx, duk_idx_t index); +#endif + +DUK_INTERNAL_DECL duk_hstring *duk_require_hstring(duk_context *ctx, duk_idx_t index); +DUK_INTERNAL_DECL duk_hobject *duk_require_hobject(duk_context *ctx, duk_idx_t index); +DUK_INTERNAL_DECL duk_hbuffer *duk_require_hbuffer(duk_context *ctx, duk_idx_t index); +DUK_INTERNAL_DECL duk_hthread *duk_require_hthread(duk_context *ctx, duk_idx_t index); +DUK_INTERNAL_DECL duk_hcompiledfunction *duk_require_hcompiledfunction(duk_context *ctx, duk_idx_t index); +DUK_INTERNAL_DECL duk_hnativefunction *duk_require_hnativefunction(duk_context *ctx, duk_idx_t index); + +DUK_INTERNAL_DECL duk_hobject *duk_require_hobject_with_class(duk_context *ctx, duk_idx_t index, duk_small_uint_t classnum); + +DUK_INTERNAL_DECL duk_hobject *duk_require_hobject_or_lfunc(duk_context *ctx, duk_idx_t index); +DUK_INTERNAL_DECL duk_hobject *duk_require_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index); + +DUK_INTERNAL_DECL void duk_push_hstring(duk_context *ctx, duk_hstring *h); +DUK_INTERNAL_DECL void duk_push_hstring_stridx(duk_context *ctx, duk_small_int_t stridx); +DUK_INTERNAL_DECL void duk_push_hobject(duk_context *ctx, duk_hobject *h); +DUK_INTERNAL_DECL void duk_push_hbuffer(duk_context *ctx, duk_hbuffer *h); +#define duk_push_hthread(ctx,h) \ + duk_push_hobject((ctx), (duk_hobject *) (h)) +#define duk_push_hcompiledfunction(ctx,h) \ + duk_push_hobject((ctx), (duk_hobject *) (h)) +#define duk_push_hnativefunction(ctx,h) \ + duk_push_hobject((ctx), (duk_hobject *) (h)) +DUK_INTERNAL_DECL void duk_push_hobject_bidx(duk_context *ctx, duk_small_int_t builtin_idx); +DUK_INTERNAL_DECL duk_idx_t duk_push_object_helper(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx); +DUK_INTERNAL_DECL duk_idx_t duk_push_object_helper_proto(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_hobject *proto); +DUK_INTERNAL_DECL duk_idx_t duk_push_object_internal(duk_context *ctx); +DUK_INTERNAL_DECL duk_idx_t duk_push_compiledfunction(duk_context *ctx); +DUK_INTERNAL_DECL void duk_push_c_function_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs); +DUK_INTERNAL_DECL void duk_push_c_function_noconstruct_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs); + +DUK_INTERNAL_DECL void duk_push_string_funcptr(duk_context *ctx, duk_uint8_t *ptr, duk_size_t sz); +DUK_INTERNAL_DECL void duk_push_lightfunc_name(duk_context *ctx, duk_tval *tv); +DUK_INTERNAL_DECL void duk_push_lightfunc_tostring(duk_context *ctx, duk_tval *tv); +DUK_INTERNAL_DECL duk_hbufferobject *duk_push_bufferobject_raw(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx); + +#if !defined(DUK_USE_PARANOID_ERRORS) +DUK_INTERNAL_DECL const char *duk_push_string_readable(duk_context *ctx, duk_idx_t index); +DUK_INTERNAL_DECL const char *duk_push_string_tval_readable(duk_context *ctx, duk_tval *tv); +#endif + +DUK_INTERNAL_DECL duk_bool_t duk_get_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [] -> [val] */ +DUK_INTERNAL_DECL duk_bool_t duk_put_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [val] -> [] */ +DUK_INTERNAL_DECL duk_bool_t duk_del_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [] -> [] */ +DUK_INTERNAL_DECL duk_bool_t duk_has_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx); /* [] -> [] */ + +DUK_INTERNAL_DECL duk_bool_t duk_get_prop_stridx_boolean(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_bool_t *out_has_prop); /* [] -> [] */ + +DUK_INTERNAL_DECL void duk_xdef_prop(duk_context *ctx, duk_idx_t obj_index, duk_small_uint_t desc_flags); /* [key val] -> [] */ +DUK_INTERNAL_DECL void duk_xdef_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index, duk_small_uint_t desc_flags); /* [val] -> [] */ +DUK_INTERNAL_DECL void duk_xdef_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_uint_t desc_flags); /* [val] -> [] */ +DUK_INTERNAL_DECL void duk_xdef_prop_stridx_builtin(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_int_t builtin_idx, duk_small_uint_t desc_flags); /* [] -> [] */ +DUK_INTERNAL_DECL void duk_xdef_prop_stridx_thrower(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_uint_t desc_flags); /* [] -> [] */ + +/* These are macros for now, but could be separate functions to reduce code + * footprint (check call site count before refactoring). + */ +#define duk_xdef_prop_wec(ctx,obj_index) \ + duk_xdef_prop((ctx), (obj_index), DUK_PROPDESC_FLAGS_WEC) +#define duk_xdef_prop_index_wec(ctx,obj_index,arr_index) \ + duk_xdef_prop_index((ctx), (obj_index), (arr_index), DUK_PROPDESC_FLAGS_WEC) +#define duk_xdef_prop_stridx_wec(ctx,obj_index,stridx) \ + duk_xdef_prop_stridx((ctx), (obj_index), (stridx), DUK_PROPDESC_FLAGS_WEC) + +/* Set object 'length'. */ +DUK_INTERNAL_DECL void duk_set_length(duk_context *ctx, duk_idx_t index, duk_size_t length); + +/* Raw internal valstack access macros: access is unsafe so call site + * must have a guarantee that the index is valid. When that is the case, + * using these macro results in faster and smaller code than duk_get_tval(). + * Both 'ctx' and 'idx' are evaluted multiple times, but only for asserts. + */ +#define DUK_ASSERT_VALID_NEGIDX(ctx,idx) \ + (DUK_ASSERT_EXPR((idx) < 0), DUK_ASSERT_EXPR(duk_is_valid_index((ctx), (idx)))) +#define DUK_ASSERT_VALID_POSIDX(ctx,idx) \ + (DUK_ASSERT_EXPR((idx) >= 0), DUK_ASSERT_EXPR(duk_is_valid_index((ctx), (idx)))) +#define DUK_GET_TVAL_NEGIDX(ctx,idx) \ + (DUK_ASSERT_VALID_NEGIDX((ctx),(idx)), ((duk_hthread *) (ctx))->valstack_top + (idx)) +#define DUK_GET_TVAL_POSIDX(ctx,idx) \ + (DUK_ASSERT_VALID_POSIDX((ctx),(idx)), ((duk_hthread *) (ctx))->valstack_bottom + (idx)) +#define DUK_GET_HOBJECT_NEGIDX(ctx,idx) \ + (DUK_ASSERT_VALID_NEGIDX((ctx),(idx)), DUK_TVAL_GET_OBJECT(((duk_hthread *) (ctx))->valstack_top + (idx))) +#define DUK_GET_HOBJECT_POSIDX(ctx,idx) \ + (DUK_ASSERT_VALID_POSIDX((ctx),(idx)), DUK_TVAL_GET_OBJECT(((duk_hthread *) (ctx))->valstack_bottom + (idx))) + +#endif /* DUK_API_INTERNAL_H_INCLUDED */ +/* + * Heap string representation. + * + * Strings are byte sequences ordinarily stored in extended UTF-8 format, + * allowing values larger than the official UTF-8 range (used internally) + * and also allowing UTF-8 encoding of surrogate pairs (CESU-8 format). + * Strings may also be invalid UTF-8 altogether which is the case e.g. with + * strings used as internal property names and raw buffers converted to + * strings. In such cases the 'clen' field contains an inaccurate value. + * + * Ecmascript requires support for 32-bit long strings. However, since each + * 16-bit codepoint can take 3 bytes in CESU-8, this representation can only + * support about 1.4G codepoint long strings in extreme cases. This is not + * really a practical issue. + */ + +#ifndef DUK_HSTRING_H_INCLUDED +#define DUK_HSTRING_H_INCLUDED + +/* Impose a maximum string length for now. Restricted artificially to + * ensure adding a heap header length won't overflow size_t. The limit + * should be synchronized with DUK_HBUFFER_MAX_BYTELEN. + * + * E5.1 makes provisions to support strings longer than 4G characters. + * This limit should be eliminated on 64-bit platforms (and increased + * closer to maximum support on 32-bit platforms). + */ + +#if defined(DUK_USE_STRLEN16) +#define DUK_HSTRING_MAX_BYTELEN (0x0000ffffUL) +#else +#define DUK_HSTRING_MAX_BYTELEN (0x7fffffffUL) +#endif + +/* XXX: could add flags for "is valid CESU-8" (Ecmascript compatible strings), + * "is valid UTF-8", "is valid extended UTF-8" (internal strings are not, + * regexp bytecode is), and "contains non-BMP characters". These are not + * needed right now. + */ + +#define DUK_HSTRING_FLAG_ASCII DUK_HEAPHDR_USER_FLAG(0) /* string is ASCII, clen == blen */ +#define DUK_HSTRING_FLAG_ARRIDX DUK_HEAPHDR_USER_FLAG(1) /* string is a valid array index */ +#define DUK_HSTRING_FLAG_INTERNAL DUK_HEAPHDR_USER_FLAG(2) /* string is internal */ +#define DUK_HSTRING_FLAG_RESERVED_WORD DUK_HEAPHDR_USER_FLAG(3) /* string is a reserved word (non-strict) */ +#define DUK_HSTRING_FLAG_STRICT_RESERVED_WORD DUK_HEAPHDR_USER_FLAG(4) /* string is a reserved word (strict) */ +#define DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS DUK_HEAPHDR_USER_FLAG(5) /* string is 'eval' or 'arguments' */ +#define DUK_HSTRING_FLAG_EXTDATA DUK_HEAPHDR_USER_FLAG(6) /* string data is external (duk_hstring_external) */ + +#define DUK_HSTRING_HAS_ASCII(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ASCII) +#define DUK_HSTRING_HAS_ARRIDX(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ARRIDX) +#define DUK_HSTRING_HAS_INTERNAL(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_INTERNAL) +#define DUK_HSTRING_HAS_RESERVED_WORD(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_RESERVED_WORD) +#define DUK_HSTRING_HAS_STRICT_RESERVED_WORD(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_STRICT_RESERVED_WORD) +#define DUK_HSTRING_HAS_EVAL_OR_ARGUMENTS(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS) +#define DUK_HSTRING_HAS_EXTDATA(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EXTDATA) + +#define DUK_HSTRING_SET_ASCII(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ASCII) +#define DUK_HSTRING_SET_ARRIDX(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ARRIDX) +#define DUK_HSTRING_SET_INTERNAL(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_INTERNAL) +#define DUK_HSTRING_SET_RESERVED_WORD(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_RESERVED_WORD) +#define DUK_HSTRING_SET_STRICT_RESERVED_WORD(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_STRICT_RESERVED_WORD) +#define DUK_HSTRING_SET_EVAL_OR_ARGUMENTS(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS) +#define DUK_HSTRING_SET_EXTDATA(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EXTDATA) + +#define DUK_HSTRING_CLEAR_ASCII(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ASCII) +#define DUK_HSTRING_CLEAR_ARRIDX(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ARRIDX) +#define DUK_HSTRING_CLEAR_INTERNAL(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_INTERNAL) +#define DUK_HSTRING_CLEAR_RESERVED_WORD(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_RESERVED_WORD) +#define DUK_HSTRING_CLEAR_STRICT_RESERVED_WORD(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_STRICT_RESERVED_WORD) +#define DUK_HSTRING_CLEAR_EVAL_OR_ARGUMENTS(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS) +#define DUK_HSTRING_CLEAR_EXTDATA(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EXTDATA) + +#if 0 /* Slightly smaller code without explicit flag, but explicit flag + * is very useful when 'clen' is dropped. + */ +#define DUK_HSTRING_IS_ASCII(x) (DUK_HSTRING_GET_BYTELEN((x)) == DUK_HSTRING_GET_CHARLEN((x))) +#endif +#define DUK_HSTRING_IS_ASCII(x) DUK_HSTRING_HAS_ASCII((x)) +#define DUK_HSTRING_IS_EMPTY(x) (DUK_HSTRING_GET_BYTELEN((x)) == 0) + +#if defined(DUK_USE_STRHASH16) +#define DUK_HSTRING_GET_HASH(x) ((x)->hdr.h_flags >> 16) +#define DUK_HSTRING_SET_HASH(x,v) do { \ + (x)->hdr.h_flags = ((x)->hdr.h_flags & 0x0000ffffUL) | ((v) << 16); \ + } while (0) +#else +#define DUK_HSTRING_GET_HASH(x) ((x)->hash) +#define DUK_HSTRING_SET_HASH(x,v) do { \ + (x)->hash = (v); \ + } while (0) +#endif + +#if defined(DUK_USE_STRLEN16) +#define DUK_HSTRING_GET_BYTELEN(x) ((x)->hdr.h_strextra16) +#define DUK_HSTRING_SET_BYTELEN(x,v) do { \ + (x)->hdr.h_strextra16 = (v); \ + } while (0) +#if defined(DUK_USE_HSTRING_CLEN) +#define DUK_HSTRING_GET_CHARLEN(x) ((x)->clen16) +#define DUK_HSTRING_SET_CHARLEN(x,v) do { \ + (x)->clen16 = (v); \ + } while (0) +#else +#define DUK_HSTRING_GET_CHARLEN(x) duk_hstring_get_charlen((x)) +#define DUK_HSTRING_SET_CHARLEN(x,v) do { \ + DUK_ASSERT(0); /* should never be called */ \ + } while (0) +#endif +#else +#define DUK_HSTRING_GET_BYTELEN(x) ((x)->blen) +#define DUK_HSTRING_SET_BYTELEN(x,v) do { \ + (x)->blen = (v); \ + } while (0) +#define DUK_HSTRING_GET_CHARLEN(x) ((x)->clen) +#define DUK_HSTRING_SET_CHARLEN(x,v) do { \ + (x)->clen = (v); \ + } while (0) +#endif + +#if defined(DUK_USE_HSTRING_EXTDATA) +#define DUK_HSTRING_GET_EXTDATA(x) \ + ((x)->extdata) +#define DUK_HSTRING_GET_DATA(x) \ + (DUK_HSTRING_HAS_EXTDATA((x)) ? \ + DUK_HSTRING_GET_EXTDATA((const duk_hstring_external *) (x)) : ((const duk_uint8_t *) ((x) + 1))) +#else +#define DUK_HSTRING_GET_DATA(x) \ + ((const duk_uint8_t *) ((x) + 1)) +#endif + +#define DUK_HSTRING_GET_DATA_END(x) \ + (DUK_HSTRING_GET_DATA((x)) + (x)->blen) + +/* marker value; in E5 2^32-1 is not a valid array index (2^32-2 is highest valid) */ +#define DUK_HSTRING_NO_ARRAY_INDEX (0xffffffffUL) + +/* get array index related to string (or return DUK_HSTRING_NO_ARRAY_INDEX); + * avoids helper call if string has no array index value. + */ +#define DUK_HSTRING_GET_ARRIDX_FAST(h) \ + (DUK_HSTRING_HAS_ARRIDX((h)) ? duk_js_to_arrayindex_string_helper((h)) : DUK_HSTRING_NO_ARRAY_INDEX) + +/* slower but more compact variant */ +#define DUK_HSTRING_GET_ARRIDX_SLOW(h) \ + (duk_js_to_arrayindex_string_helper((h))) + +/* + * Misc + */ + +struct duk_hstring { + /* Smaller heaphdr than for other objects, because strings are held + * in string intern table which requires no link pointers. Much of + * the 32-bit flags field is unused by flags, so we can stuff a 16-bit + * field in there. + */ + duk_heaphdr_string hdr; + + /* Note: we could try to stuff a partial hash (e.g. 16 bits) into the + * shared heap header. Good hashing needs more hash bits though. + */ + + /* string hash */ +#if defined(DUK_USE_STRHASH16) + /* If 16-bit hash is in use, stuff it into duk_heaphdr_string flags. */ +#else + duk_uint32_t hash; +#endif + + /* length in bytes (not counting NUL term) */ +#if defined(DUK_USE_STRLEN16) + /* placed in duk_heaphdr_string */ +#else + duk_uint32_t blen; +#endif + + /* length in codepoints (must be E5 compatible) */ +#if defined(DUK_USE_STRLEN16) +#if defined(DUK_USE_HSTRING_CLEN) + duk_uint16_t clen16; +#else + /* computed live */ +#endif +#else + duk_uint32_t clen; +#endif + + /* + * String value of 'blen+1' bytes follows (+1 for NUL termination + * convenience for C API). No alignment needs to be guaranteed + * for strings, but fields above should guarantee alignment-by-4 + * (but not alignment-by-8). + */ +}; + +/* The external string struct is defined even when the feature is inactive. */ +struct duk_hstring_external { + duk_hstring str; + + /* + * For an external string, the NUL-terminated string data is stored + * externally. The user must guarantee that data behind this pointer + * doesn't change while it's used. + */ + + const duk_uint8_t *extdata; +}; + +/* + * Prototypes + */ + +DUK_INTERNAL_DECL duk_ucodepoint_t duk_hstring_char_code_at_raw(duk_hthread *thr, duk_hstring *h, duk_uint_t pos); + +#if !defined(DUK_USE_HSTRING_CLEN) +DUK_INTERNAL_DECL duk_size_t duk_hstring_get_charlen(duk_hstring *h); +#endif + +#endif /* DUK_HSTRING_H_INCLUDED */ +/* + * Heap object representation. + * + * Heap objects are used for Ecmascript objects, arrays, and functions, + * but also for internal control like declarative and object environment + * records. Compiled functions, native functions, and threads are also + * objects but with an extended C struct. + * + * Objects provide the required Ecmascript semantics and exotic behaviors + * especially for property access. + * + * Properties are stored in three conceptual parts: + * + * 1. A linear 'entry part' contains ordered key-value-attributes triples + * and is the main method of string properties. + * + * 2. An optional linear 'array part' is used for array objects to store a + * (dense) range of [0,N[ array indexed entries with default attributes + * (writable, enumerable, configurable). If the array part would become + * sparse or non-default attributes are required, the array part is + * abandoned and moved to the 'entry part'. + * + * 3. An optional 'hash part' is used to optimize lookups of the entry + * part; it is used only for objects with sufficiently many properties + * and can be abandoned without loss of information. + * + * These three conceptual parts are stored in a single memory allocated area. + * This minimizes memory allocation overhead but also means that all three + * parts are resized together, and makes property access a bit complicated. + */ + +#ifndef DUK_HOBJECT_H_INCLUDED +#define DUK_HOBJECT_H_INCLUDED + +/* Object flag. There are currently 26 flag bits available. Make sure + * this stays in sync with debugger object inspection code. + */ +#define DUK_HOBJECT_FLAG_EXTENSIBLE DUK_HEAPHDR_USER_FLAG(0) /* object is extensible */ +#define DUK_HOBJECT_FLAG_CONSTRUCTABLE DUK_HEAPHDR_USER_FLAG(1) /* object is constructable */ +#define DUK_HOBJECT_FLAG_BOUND DUK_HEAPHDR_USER_FLAG(2) /* object established using Function.prototype.bind() */ +#define DUK_HOBJECT_FLAG_COMPILEDFUNCTION DUK_HEAPHDR_USER_FLAG(4) /* object is a compiled function (duk_hcompiledfunction) */ +#define DUK_HOBJECT_FLAG_NATIVEFUNCTION DUK_HEAPHDR_USER_FLAG(5) /* object is a native function (duk_hnativefunction) */ +#define DUK_HOBJECT_FLAG_BUFFEROBJECT DUK_HEAPHDR_USER_FLAG(6) /* object is a buffer object (duk_hbufferobject) (always exotic) */ +#define DUK_HOBJECT_FLAG_THREAD DUK_HEAPHDR_USER_FLAG(7) /* object is a thread (duk_hthread) */ +#define DUK_HOBJECT_FLAG_ARRAY_PART DUK_HEAPHDR_USER_FLAG(8) /* object has an array part (a_size may still be 0) */ +#define DUK_HOBJECT_FLAG_STRICT DUK_HEAPHDR_USER_FLAG(9) /* function: function object is strict */ +#define DUK_HOBJECT_FLAG_NOTAIL DUK_HEAPHDR_USER_FLAG(10) /* function: function must not be tail called */ +#define DUK_HOBJECT_FLAG_NEWENV DUK_HEAPHDR_USER_FLAG(11) /* function: create new environment when called (see duk_hcompiledfunction) */ +#define DUK_HOBJECT_FLAG_NAMEBINDING DUK_HEAPHDR_USER_FLAG(12) /* function: create binding for func name (function templates only, used for named function expressions) */ +#define DUK_HOBJECT_FLAG_CREATEARGS DUK_HEAPHDR_USER_FLAG(13) /* function: create an arguments object on function call */ +#define DUK_HOBJECT_FLAG_ENVRECCLOSED DUK_HEAPHDR_USER_FLAG(14) /* envrec: (declarative) record is closed */ +#define DUK_HOBJECT_FLAG_EXOTIC_ARRAY DUK_HEAPHDR_USER_FLAG(15) /* 'Array' object, array length and index exotic behavior */ +#define DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ DUK_HEAPHDR_USER_FLAG(16) /* 'String' object, array index exotic behavior */ +#define DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS DUK_HEAPHDR_USER_FLAG(17) /* 'Arguments' object and has arguments exotic behavior (non-strict callee) */ +#define DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC DUK_HEAPHDR_USER_FLAG(18) /* Duktape/C (nativefunction) object, exotic 'length' */ +#define DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ DUK_HEAPHDR_USER_FLAG(19) /* 'Proxy' object */ + +#define DUK_HOBJECT_FLAG_CLASS_BASE DUK_HEAPHDR_USER_FLAG_NUMBER(20) +#define DUK_HOBJECT_FLAG_CLASS_BITS 5 + +#define DUK_HOBJECT_GET_CLASS_NUMBER(h) \ + DUK_HEAPHDR_GET_FLAG_RANGE(&(h)->hdr, DUK_HOBJECT_FLAG_CLASS_BASE, DUK_HOBJECT_FLAG_CLASS_BITS) +#define DUK_HOBJECT_SET_CLASS_NUMBER(h,v) \ + DUK_HEAPHDR_SET_FLAG_RANGE(&(h)->hdr, DUK_HOBJECT_FLAG_CLASS_BASE, DUK_HOBJECT_FLAG_CLASS_BITS, (v)) + +#define DUK_HOBJECT_GET_CLASS_MASK(h) \ + (1UL << DUK_HEAPHDR_GET_FLAG_RANGE(&(h)->hdr, DUK_HOBJECT_FLAG_CLASS_BASE, DUK_HOBJECT_FLAG_CLASS_BITS)) + +/* Macro for creating flag initializer from a class number. + * Unsigned type cast is needed to avoid warnings about coercing + * a signed integer to an unsigned one; the largest class values + * have the highest bit (bit 31) set which causes this. + */ +#define DUK_HOBJECT_CLASS_AS_FLAGS(v) (((duk_uint_t) (v)) << DUK_HOBJECT_FLAG_CLASS_BASE) + +/* E5 Section 8.6.2 + custom classes */ +#define DUK_HOBJECT_CLASS_UNUSED 0 +#define DUK_HOBJECT_CLASS_ARGUMENTS 1 +#define DUK_HOBJECT_CLASS_ARRAY 2 +#define DUK_HOBJECT_CLASS_BOOLEAN 3 +#define DUK_HOBJECT_CLASS_DATE 4 +#define DUK_HOBJECT_CLASS_ERROR 5 +#define DUK_HOBJECT_CLASS_FUNCTION 6 +#define DUK_HOBJECT_CLASS_JSON 7 +#define DUK_HOBJECT_CLASS_MATH 8 +#define DUK_HOBJECT_CLASS_NUMBER 9 +#define DUK_HOBJECT_CLASS_OBJECT 10 +#define DUK_HOBJECT_CLASS_REGEXP 11 +#define DUK_HOBJECT_CLASS_STRING 12 +#define DUK_HOBJECT_CLASS_GLOBAL 13 +#define DUK_HOBJECT_CLASS_OBJENV 14 /* custom */ +#define DUK_HOBJECT_CLASS_DECENV 15 /* custom */ +#define DUK_HOBJECT_CLASS_BUFFER 16 /* custom; implies DUK_HOBJECT_IS_BUFFEROBJECT */ +#define DUK_HOBJECT_CLASS_POINTER 17 /* custom */ +#define DUK_HOBJECT_CLASS_THREAD 18 /* custom; implies DUK_HOBJECT_IS_THREAD */ +#define DUK_HOBJECT_CLASS_ARRAYBUFFER 19 /* implies DUK_HOBJECT_IS_BUFFEROBJECT */ +#define DUK_HOBJECT_CLASS_DATAVIEW 20 +#define DUK_HOBJECT_CLASS_INT8ARRAY 21 +#define DUK_HOBJECT_CLASS_UINT8ARRAY 22 +#define DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY 23 +#define DUK_HOBJECT_CLASS_INT16ARRAY 24 +#define DUK_HOBJECT_CLASS_UINT16ARRAY 25 +#define DUK_HOBJECT_CLASS_INT32ARRAY 26 +#define DUK_HOBJECT_CLASS_UINT32ARRAY 27 +#define DUK_HOBJECT_CLASS_FLOAT32ARRAY 28 +#define DUK_HOBJECT_CLASS_FLOAT64ARRAY 29 +#define DUK_HOBJECT_CLASS_MAX 29 + +/* class masks */ +#define DUK_HOBJECT_CMASK_ALL ((1UL << (DUK_HOBJECT_CLASS_MAX + 1)) - 1UL) +#define DUK_HOBJECT_CMASK_UNUSED (1UL << DUK_HOBJECT_CLASS_UNUSED) +#define DUK_HOBJECT_CMASK_ARGUMENTS (1UL << DUK_HOBJECT_CLASS_ARGUMENTS) +#define DUK_HOBJECT_CMASK_ARRAY (1UL << DUK_HOBJECT_CLASS_ARRAY) +#define DUK_HOBJECT_CMASK_BOOLEAN (1UL << DUK_HOBJECT_CLASS_BOOLEAN) +#define DUK_HOBJECT_CMASK_DATE (1UL << DUK_HOBJECT_CLASS_DATE) +#define DUK_HOBJECT_CMASK_ERROR (1UL << DUK_HOBJECT_CLASS_ERROR) +#define DUK_HOBJECT_CMASK_FUNCTION (1UL << DUK_HOBJECT_CLASS_FUNCTION) +#define DUK_HOBJECT_CMASK_JSON (1UL << DUK_HOBJECT_CLASS_JSON) +#define DUK_HOBJECT_CMASK_MATH (1UL << DUK_HOBJECT_CLASS_MATH) +#define DUK_HOBJECT_CMASK_NUMBER (1UL << DUK_HOBJECT_CLASS_NUMBER) +#define DUK_HOBJECT_CMASK_OBJECT (1UL << DUK_HOBJECT_CLASS_OBJECT) +#define DUK_HOBJECT_CMASK_REGEXP (1UL << DUK_HOBJECT_CLASS_REGEXP) +#define DUK_HOBJECT_CMASK_STRING (1UL << DUK_HOBJECT_CLASS_STRING) +#define DUK_HOBJECT_CMASK_GLOBAL (1UL << DUK_HOBJECT_CLASS_GLOBAL) +#define DUK_HOBJECT_CMASK_OBJENV (1UL << DUK_HOBJECT_CLASS_OBJENV) +#define DUK_HOBJECT_CMASK_DECENV (1UL << DUK_HOBJECT_CLASS_DECENV) +#define DUK_HOBJECT_CMASK_BUFFER (1UL << DUK_HOBJECT_CLASS_BUFFER) +#define DUK_HOBJECT_CMASK_POINTER (1UL << DUK_HOBJECT_CLASS_POINTER) +#define DUK_HOBJECT_CMASK_THREAD (1UL << DUK_HOBJECT_CLASS_THREAD) +#define DUK_HOBJECT_CMASK_ARRAYBUFFER (1UL << DUK_HOBJECT_CLASS_ARRAYBUFFER) +#define DUK_HOBJECT_CMASK_DATAVIEW (1UL << DUK_HOBJECT_CLASS_DATAVIEW) +#define DUK_HOBJECT_CMASK_INT8ARRAY (1UL << DUK_HOBJECT_CLASS_INT8ARRAY) +#define DUK_HOBJECT_CMASK_UINT8ARRAY (1UL << DUK_HOBJECT_CLASS_UINT8ARRAY) +#define DUK_HOBJECT_CMASK_UINT8CLAMPEDARRAY (1UL << DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY) +#define DUK_HOBJECT_CMASK_INT16ARRAY (1UL << DUK_HOBJECT_CLASS_INT16ARRAY) +#define DUK_HOBJECT_CMASK_UINT16ARRAY (1UL << DUK_HOBJECT_CLASS_UINT16ARRAY) +#define DUK_HOBJECT_CMASK_INT32ARRAY (1UL << DUK_HOBJECT_CLASS_INT32ARRAY) +#define DUK_HOBJECT_CMASK_UINT32ARRAY (1UL << DUK_HOBJECT_CLASS_UINT32ARRAY) +#define DUK_HOBJECT_CMASK_FLOAT32ARRAY (1UL << DUK_HOBJECT_CLASS_FLOAT32ARRAY) +#define DUK_HOBJECT_CMASK_FLOAT64ARRAY (1UL << DUK_HOBJECT_CLASS_FLOAT64ARRAY) + +#define DUK_HOBJECT_CMASK_ALL_BUFFEROBJECTS \ + (DUK_HOBJECT_CMASK_BUFFER | \ + DUK_HOBJECT_CMASK_ARRAYBUFFER | \ + DUK_HOBJECT_CMASK_DATAVIEW | \ + DUK_HOBJECT_CMASK_INT8ARRAY | \ + DUK_HOBJECT_CMASK_UINT8ARRAY | \ + DUK_HOBJECT_CMASK_UINT8CLAMPEDARRAY | \ + DUK_HOBJECT_CMASK_INT16ARRAY | \ + DUK_HOBJECT_CMASK_UINT16ARRAY | \ + DUK_HOBJECT_CMASK_INT32ARRAY | \ + DUK_HOBJECT_CMASK_UINT32ARRAY | \ + DUK_HOBJECT_CMASK_FLOAT32ARRAY | \ + DUK_HOBJECT_CMASK_FLOAT64ARRAY) + +#define DUK_HOBJECT_IS_OBJENV(h) (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_OBJENV) +#define DUK_HOBJECT_IS_DECENV(h) (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_DECENV) +#define DUK_HOBJECT_IS_ENV(h) (DUK_HOBJECT_IS_OBJENV((h)) || DUK_HOBJECT_IS_DECENV((h))) +#define DUK_HOBJECT_IS_ARRAY(h) (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_ARRAY) +#define DUK_HOBJECT_IS_COMPILEDFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION) +#define DUK_HOBJECT_IS_NATIVEFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION) +#define DUK_HOBJECT_IS_BUFFEROBJECT(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT) +#define DUK_HOBJECT_IS_THREAD(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD) + +#define DUK_HOBJECT_IS_NONBOUND_FUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, \ + DUK_HOBJECT_FLAG_COMPILEDFUNCTION | \ + DUK_HOBJECT_FLAG_NATIVEFUNCTION) + +#define DUK_HOBJECT_IS_FUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, \ + DUK_HOBJECT_FLAG_BOUND | \ + DUK_HOBJECT_FLAG_COMPILEDFUNCTION | \ + DUK_HOBJECT_FLAG_NATIVEFUNCTION) + +#define DUK_HOBJECT_IS_CALLABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, \ + DUK_HOBJECT_FLAG_BOUND | \ + DUK_HOBJECT_FLAG_COMPILEDFUNCTION | \ + DUK_HOBJECT_FLAG_NATIVEFUNCTION) + +/* object has any exotic behavior(s) */ +#define DUK_HOBJECT_EXOTIC_BEHAVIOR_FLAGS (DUK_HOBJECT_FLAG_EXOTIC_ARRAY | \ + DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS | \ + DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ | \ + DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC | \ + DUK_HOBJECT_FLAG_BUFFEROBJECT | \ + DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ) + +#define DUK_HOBJECT_HAS_EXOTIC_BEHAVIOR(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_EXOTIC_BEHAVIOR_FLAGS) + +#define DUK_HOBJECT_HAS_EXTENSIBLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXTENSIBLE) +#define DUK_HOBJECT_HAS_CONSTRUCTABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CONSTRUCTABLE) +#define DUK_HOBJECT_HAS_BOUND(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUND) +#define DUK_HOBJECT_HAS_COMPILEDFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION) +#define DUK_HOBJECT_HAS_NATIVEFUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION) +#define DUK_HOBJECT_HAS_BUFFEROBJECT(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT) +#define DUK_HOBJECT_HAS_THREAD(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD) +#define DUK_HOBJECT_HAS_ARRAY_PART(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ARRAY_PART) +#define DUK_HOBJECT_HAS_STRICT(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_STRICT) +#define DUK_HOBJECT_HAS_NOTAIL(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NOTAIL) +#define DUK_HOBJECT_HAS_NEWENV(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NEWENV) +#define DUK_HOBJECT_HAS_NAMEBINDING(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NAMEBINDING) +#define DUK_HOBJECT_HAS_CREATEARGS(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CREATEARGS) +#define DUK_HOBJECT_HAS_ENVRECCLOSED(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ENVRECCLOSED) +#define DUK_HOBJECT_HAS_EXOTIC_ARRAY(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARRAY) +#define DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ) +#define DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS) +#define DUK_HOBJECT_HAS_EXOTIC_DUKFUNC(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC) +#define DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ) + +#define DUK_HOBJECT_SET_EXTENSIBLE(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXTENSIBLE) +#define DUK_HOBJECT_SET_CONSTRUCTABLE(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CONSTRUCTABLE) +#define DUK_HOBJECT_SET_BOUND(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUND) +#define DUK_HOBJECT_SET_COMPILEDFUNCTION(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION) +#define DUK_HOBJECT_SET_NATIVEFUNCTION(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION) +#define DUK_HOBJECT_SET_BUFFEROBJECT(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT) +#define DUK_HOBJECT_SET_THREAD(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD) +#define DUK_HOBJECT_SET_ARRAY_PART(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ARRAY_PART) +#define DUK_HOBJECT_SET_STRICT(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_STRICT) +#define DUK_HOBJECT_SET_NOTAIL(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NOTAIL) +#define DUK_HOBJECT_SET_NEWENV(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NEWENV) +#define DUK_HOBJECT_SET_NAMEBINDING(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NAMEBINDING) +#define DUK_HOBJECT_SET_CREATEARGS(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CREATEARGS) +#define DUK_HOBJECT_SET_ENVRECCLOSED(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ENVRECCLOSED) +#define DUK_HOBJECT_SET_EXOTIC_ARRAY(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARRAY) +#define DUK_HOBJECT_SET_EXOTIC_STRINGOBJ(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ) +#define DUK_HOBJECT_SET_EXOTIC_ARGUMENTS(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS) +#define DUK_HOBJECT_SET_EXOTIC_DUKFUNC(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC) +#define DUK_HOBJECT_SET_EXOTIC_PROXYOBJ(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ) + +#define DUK_HOBJECT_CLEAR_EXTENSIBLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXTENSIBLE) +#define DUK_HOBJECT_CLEAR_CONSTRUCTABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CONSTRUCTABLE) +#define DUK_HOBJECT_CLEAR_BOUND(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUND) +#define DUK_HOBJECT_CLEAR_COMPILEDFUNCTION(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPILEDFUNCTION) +#define DUK_HOBJECT_CLEAR_NATIVEFUNCTION(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATIVEFUNCTION) +#define DUK_HOBJECT_CLEAR_BUFFEROBJECT(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFFEROBJECT) +#define DUK_HOBJECT_CLEAR_THREAD(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_THREAD) +#define DUK_HOBJECT_CLEAR_ARRAY_PART(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ARRAY_PART) +#define DUK_HOBJECT_CLEAR_STRICT(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_STRICT) +#define DUK_HOBJECT_CLEAR_NOTAIL(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NOTAIL) +#define DUK_HOBJECT_CLEAR_NEWENV(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NEWENV) +#define DUK_HOBJECT_CLEAR_NAMEBINDING(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NAMEBINDING) +#define DUK_HOBJECT_CLEAR_CREATEARGS(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CREATEARGS) +#define DUK_HOBJECT_CLEAR_ENVRECCLOSED(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ENVRECCLOSED) +#define DUK_HOBJECT_CLEAR_EXOTIC_ARRAY(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARRAY) +#define DUK_HOBJECT_CLEAR_EXOTIC_STRINGOBJ(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ) +#define DUK_HOBJECT_CLEAR_EXOTIC_ARGUMENTS(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS) +#define DUK_HOBJECT_CLEAR_EXOTIC_DUKFUNC(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC) +#define DUK_HOBJECT_CLEAR_EXOTIC_PROXYOBJ(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ) + +/* flags used for property attributes in duk_propdesc and packed flags */ +#define DUK_PROPDESC_FLAG_WRITABLE (1 << 0) /* E5 Section 8.6.1 */ +#define DUK_PROPDESC_FLAG_ENUMERABLE (1 << 1) /* E5 Section 8.6.1 */ +#define DUK_PROPDESC_FLAG_CONFIGURABLE (1 << 2) /* E5 Section 8.6.1 */ +#define DUK_PROPDESC_FLAG_ACCESSOR (1 << 3) /* accessor */ +#define DUK_PROPDESC_FLAG_VIRTUAL (1 << 4) /* property is virtual: used in duk_propdesc, never stored + * (used by e.g. buffer virtual properties) + */ +#define DUK_PROPDESC_FLAGS_MASK (DUK_PROPDESC_FLAG_WRITABLE | \ + DUK_PROPDESC_FLAG_ENUMERABLE | \ + DUK_PROPDESC_FLAG_CONFIGURABLE | \ + DUK_PROPDESC_FLAG_ACCESSOR) + +/* additional flags which are passed in the same flags argument as property + * flags but are not stored in object properties. + */ +#define DUK_PROPDESC_FLAG_NO_OVERWRITE (1 << 4) /* internal define property: skip write silently if exists */ + +/* convenience */ +#define DUK_PROPDESC_FLAGS_NONE 0 +#define DUK_PROPDESC_FLAGS_W (DUK_PROPDESC_FLAG_WRITABLE) +#define DUK_PROPDESC_FLAGS_E (DUK_PROPDESC_FLAG_ENUMERABLE) +#define DUK_PROPDESC_FLAGS_C (DUK_PROPDESC_FLAG_CONFIGURABLE) +#define DUK_PROPDESC_FLAGS_WE (DUK_PROPDESC_FLAG_WRITABLE | DUK_PROPDESC_FLAG_ENUMERABLE) +#define DUK_PROPDESC_FLAGS_WC (DUK_PROPDESC_FLAG_WRITABLE | DUK_PROPDESC_FLAG_CONFIGURABLE) +#define DUK_PROPDESC_FLAGS_EC (DUK_PROPDESC_FLAG_ENUMERABLE | DUK_PROPDESC_FLAG_CONFIGURABLE) +#define DUK_PROPDESC_FLAGS_WEC (DUK_PROPDESC_FLAG_WRITABLE | \ + DUK_PROPDESC_FLAG_ENUMERABLE | \ + DUK_PROPDESC_FLAG_CONFIGURABLE) + +/* flags for duk_hobject_get_own_propdesc() and variants */ +#define DUK_GETDESC_FLAG_PUSH_VALUE (1 << 0) /* push value to stack */ +#define DUK_GETDESC_FLAG_IGNORE_PROTOLOOP (1 << 1) /* don't throw for prototype loop */ + +/* + * Macro for object validity check + * + * Assert for currently guaranteed relations between flags, for instance. + */ + +#define DUK_ASSERT_HOBJECT_VALID(h) do { \ + DUK_ASSERT((h) != NULL); \ + DUK_ASSERT(!DUK_HOBJECT_IS_CALLABLE((h)) || \ + DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_FUNCTION); \ + DUK_ASSERT(!DUK_HOBJECT_IS_BUFFEROBJECT((h)) || \ + (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_BUFFER || \ + DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_ARRAYBUFFER || \ + DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_DATAVIEW || \ + DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_INT8ARRAY || \ + DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT8ARRAY || \ + DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY || \ + DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_INT16ARRAY || \ + DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT16ARRAY || \ + DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_INT32ARRAY || \ + DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT32ARRAY || \ + DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_FLOAT32ARRAY || \ + DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_FLOAT64ARRAY)); \ + } while (0) + +/* + * Macros to access the 'props' allocation. + */ + +#if defined(DUK_USE_HEAPPTR16) +#define DUK_HOBJECT_GET_PROPS(heap,h) \ + ((duk_uint8_t *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, ((duk_heaphdr *) (h))->h_extra16)) +#define DUK_HOBJECT_SET_PROPS(heap,h,x) do { \ + ((duk_heaphdr *) (h))->h_extra16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (x)); \ + } while (0) +#else +#define DUK_HOBJECT_GET_PROPS(heap,h) \ + ((h)->props) +#define DUK_HOBJECT_SET_PROPS(heap,h,x) do { \ + (h)->props = (duk_uint8_t *) (x); \ + } while (0) +#endif + +#if defined(DUK_USE_HOBJECT_LAYOUT_1) +/* LAYOUT 1 */ +#define DUK_HOBJECT_E_GET_KEY_BASE(heap,h) \ + ((duk_hstring **) (void *) ( \ + DUK_HOBJECT_GET_PROPS((heap), (h)) \ + )) +#define DUK_HOBJECT_E_GET_VALUE_BASE(heap,h) \ + ((duk_propvalue *) (void *) ( \ + DUK_HOBJECT_GET_PROPS((heap), (h)) + \ + DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_hstring *) \ + )) +#define DUK_HOBJECT_E_GET_FLAGS_BASE(heap,h) \ + ((duk_uint8_t *) (void *) ( \ + DUK_HOBJECT_GET_PROPS((heap), (h)) + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue)) \ + )) +#define DUK_HOBJECT_A_GET_BASE(heap,h) \ + ((duk_tval *) (void *) ( \ + DUK_HOBJECT_GET_PROPS((heap), (h)) + \ + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) \ + )) +#define DUK_HOBJECT_H_GET_BASE(heap,h) \ + ((duk_uint32_t *) (void *) ( \ + DUK_HOBJECT_GET_PROPS((heap), (h)) + \ + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \ + DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \ + )) +#define DUK_HOBJECT_P_COMPUTE_SIZE(n_ent,n_arr,n_hash) \ + ( \ + (n_ent) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \ + (n_arr) * sizeof(duk_tval) + \ + (n_hash) * sizeof(duk_uint32_t) \ + ) +#define DUK_HOBJECT_P_SET_REALLOC_PTRS(p_base,set_e_k,set_e_pv,set_e_f,set_a,set_h,n_ent,n_arr,n_hash) do { \ + (set_e_k) = (duk_hstring **) (void *) (p_base); \ + (set_e_pv) = (duk_propvalue *) (void *) ((set_e_k) + (n_ent)); \ + (set_e_f) = (duk_uint8_t *) (void *) ((set_e_pv) + (n_ent)); \ + (set_a) = (duk_tval *) (void *) ((set_e_f) + (n_ent)); \ + (set_h) = (duk_uint32_t *) (void *) ((set_a) + (n_arr)); \ + } while (0) +#elif defined(DUK_USE_HOBJECT_LAYOUT_2) +/* LAYOUT 2 */ +#if (DUK_USE_ALIGN_BY == 4) +#define DUK_HOBJECT_E_FLAG_PADDING(e_sz) ((4 - (e_sz)) & 0x03) +#elif (DUK_USE_ALIGN_BY == 8) +#define DUK_HOBJECT_E_FLAG_PADDING(e_sz) ((8 - (e_sz)) & 0x07) +#elif (DUK_USE_ALIGN_BY == 1) +#define DUK_HOBJECT_E_FLAG_PADDING(e_sz) 0 +#else +#error invalid DUK_USE_ALIGN_BY +#endif +#define DUK_HOBJECT_E_GET_KEY_BASE(heap,h) \ + ((duk_hstring **) (void *) ( \ + DUK_HOBJECT_GET_PROPS((heap), (h)) + \ + DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_propvalue) \ + )) +#define DUK_HOBJECT_E_GET_VALUE_BASE(heap,h) \ + ((duk_propvalue *) (void *) ( \ + DUK_HOBJECT_GET_PROPS((heap), (h)) \ + )) +#define DUK_HOBJECT_E_GET_FLAGS_BASE(heap,h) \ + ((duk_uint8_t *) (void *) ( \ + DUK_HOBJECT_GET_PROPS((heap), (h)) + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue)) \ + )) +#define DUK_HOBJECT_A_GET_BASE(heap,h) \ + ((duk_tval *) (void *) ( \ + DUK_HOBJECT_GET_PROPS((heap), (h)) + \ + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \ + DUK_HOBJECT_E_FLAG_PADDING(DUK_HOBJECT_GET_ESIZE((h))) \ + )) +#define DUK_HOBJECT_H_GET_BASE(heap,h) \ + ((duk_uint32_t *) (void *) ( \ + DUK_HOBJECT_GET_PROPS((heap), (h)) + \ + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \ + DUK_HOBJECT_E_FLAG_PADDING(DUK_HOBJECT_GET_ESIZE((h))) + \ + DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \ + )) +#define DUK_HOBJECT_P_COMPUTE_SIZE(n_ent,n_arr,n_hash) \ + ( \ + (n_ent) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \ + DUK_HOBJECT_E_FLAG_PADDING((n_ent)) + \ + (n_arr) * sizeof(duk_tval) + \ + (n_hash) * sizeof(duk_uint32_t) \ + ) +#define DUK_HOBJECT_P_SET_REALLOC_PTRS(p_base,set_e_k,set_e_pv,set_e_f,set_a,set_h,n_ent,n_arr,n_hash) do { \ + (set_e_pv) = (duk_propvalue *) (void *) (p_base); \ + (set_e_k) = (duk_hstring **) (void *) ((set_e_pv) + (n_ent)); \ + (set_e_f) = (duk_uint8_t *) (void *) ((set_e_k) + (n_ent)); \ + (set_a) = (duk_tval *) (void *) (((duk_uint8_t *) (set_e_f)) + \ + sizeof(duk_uint8_t) * (n_ent) + \ + DUK_HOBJECT_E_FLAG_PADDING((n_ent))); \ + (set_h) = (duk_uint32_t *) (void *) ((set_a) + (n_arr)); \ + } while (0) +#elif defined(DUK_USE_HOBJECT_LAYOUT_3) +/* LAYOUT 3 */ +#define DUK_HOBJECT_E_GET_KEY_BASE(heap,h) \ + ((duk_hstring **) (void *) ( \ + DUK_HOBJECT_GET_PROPS((heap), (h)) + \ + DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_propvalue) + \ + DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \ + )) +#define DUK_HOBJECT_E_GET_VALUE_BASE(heap,h) \ + ((duk_propvalue *) (void *) ( \ + DUK_HOBJECT_GET_PROPS((heap), (h)) \ + )) +#define DUK_HOBJECT_E_GET_FLAGS_BASE(heap,h) \ + ((duk_uint8_t *) (void *) ( \ + DUK_HOBJECT_GET_PROPS((heap), (h)) + \ + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_propvalue) + sizeof(duk_hstring *)) + \ + DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) + \ + DUK_HOBJECT_GET_HSIZE((h)) * sizeof(duk_uint32_t) \ + )) +#define DUK_HOBJECT_A_GET_BASE(heap,h) \ + ((duk_tval *) (void *) ( \ + DUK_HOBJECT_GET_PROPS((heap), (h)) + \ + DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_propvalue) \ + )) +#define DUK_HOBJECT_H_GET_BASE(heap,h) \ + ((duk_uint32_t *) (void *) ( \ + DUK_HOBJECT_GET_PROPS((heap), (h)) + \ + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_propvalue) + sizeof(duk_hstring *)) + \ + DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \ + )) +#define DUK_HOBJECT_P_COMPUTE_SIZE(n_ent,n_arr,n_hash) \ + ( \ + (n_ent) * (sizeof(duk_propvalue) + sizeof(duk_hstring *) + sizeof(duk_uint8_t)) + \ + (n_arr) * sizeof(duk_tval) + \ + (n_hash) * sizeof(duk_uint32_t) \ + ) +#define DUK_HOBJECT_P_SET_REALLOC_PTRS(p_base,set_e_k,set_e_pv,set_e_f,set_a,set_h,n_ent,n_arr,n_hash) do { \ + (set_e_pv) = (duk_propvalue *) (void *) (p_base); \ + (set_a) = (duk_tval *) (void *) ((set_e_pv) + (n_ent)); \ + (set_e_k) = (duk_hstring **) (void *) ((set_a) + (n_arr)); \ + (set_h) = (duk_uint32_t *) (void *) ((set_e_k) + (n_ent)); \ + (set_e_f) = (duk_uint8_t *) (void *) ((set_h) + (n_hash)); \ + } while (0) +#else +#error invalid hobject layout defines +#endif /* hobject property layout */ + +#define DUK_HOBJECT_P_ALLOC_SIZE(h) \ + DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE((h)), DUK_HOBJECT_GET_ASIZE((h)), DUK_HOBJECT_GET_HSIZE((h))) + +#define DUK_HOBJECT_E_GET_KEY(heap,h,i) (DUK_HOBJECT_E_GET_KEY_BASE((heap), (h))[(i)]) +#define DUK_HOBJECT_E_GET_KEY_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_KEY_BASE((heap), (h))[(i)]) +#define DUK_HOBJECT_E_GET_VALUE(heap,h,i) (DUK_HOBJECT_E_GET_VALUE_BASE((heap), (h))[(i)]) +#define DUK_HOBJECT_E_GET_VALUE_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE_BASE((heap), (h))[(i)]) +#define DUK_HOBJECT_E_GET_VALUE_TVAL(heap,h,i) (DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).v) +#define DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).v) +#define DUK_HOBJECT_E_GET_VALUE_GETTER(heap,h,i) (DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.get) +#define DUK_HOBJECT_E_GET_VALUE_GETTER_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.get) +#define DUK_HOBJECT_E_GET_VALUE_SETTER(heap,h,i) (DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.set) +#define DUK_HOBJECT_E_GET_VALUE_SETTER_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.set) +#define DUK_HOBJECT_E_GET_FLAGS(heap,h,i) (DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)]) +#define DUK_HOBJECT_E_GET_FLAGS_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)]) +#define DUK_HOBJECT_A_GET_VALUE(heap,h,i) (DUK_HOBJECT_A_GET_BASE((heap), (h))[(i)]) +#define DUK_HOBJECT_A_GET_VALUE_PTR(heap,h,i) (&DUK_HOBJECT_A_GET_BASE((heap), (h))[(i)]) +#define DUK_HOBJECT_H_GET_INDEX(heap,h,i) (DUK_HOBJECT_H_GET_BASE((heap), (h))[(i)]) +#define DUK_HOBJECT_H_GET_INDEX_PTR(heap,h,i) (&DUK_HOBJECT_H_GET_BASE((heap), (h))[(i)]) + +#define DUK_HOBJECT_E_SET_KEY(heap,h,i,k) do { \ + DUK_HOBJECT_E_GET_KEY((heap), (h), (i)) = (k); \ + } while (0) +#define DUK_HOBJECT_E_SET_VALUE(heap,h,i,v) do { \ + DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)) = (v); \ + } while (0) +#define DUK_HOBJECT_E_SET_VALUE_TVAL(heap,h,i,v) do { \ + DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).v = (v); \ + } while (0) +#define DUK_HOBJECT_E_SET_VALUE_GETTER(heap,h,i,v) do { \ + DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.get = (v); \ + } while (0) +#define DUK_HOBJECT_E_SET_VALUE_SETTER(heap,h,i,v) do { \ + DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.set = (v); \ + } while (0) +#define DUK_HOBJECT_E_SET_FLAGS(heap,h,i,f) do { \ + DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) = (duk_uint8_t) (f); \ + } while (0) +#define DUK_HOBJECT_A_SET_VALUE(heap,h,i,v) do { \ + DUK_HOBJECT_A_GET_VALUE((heap), (h), (i)) = (v); \ + } while (0) +#define DUK_HOBJECT_A_SET_VALUE_TVAL(heap,h,i,v) \ + DUK_HOBJECT_A_SET_VALUE((heap), (h), (i), (v)) /* alias for above */ +#define DUK_HOBJECT_H_SET_INDEX(heap,h,i,v) do { \ + DUK_HOBJECT_H_GET_INDEX((heap), (h), (i)) = (v); \ + } while (0) + +#define DUK_HOBJECT_E_SET_FLAG_BITS(heap,h,i,mask) do { \ + DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)] |= (mask); \ + } while (0) + +#define DUK_HOBJECT_E_CLEAR_FLAG_BITS(heap,h,i,mask) do { \ + DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)] &= ~(mask); \ + } while (0) + +#define DUK_HOBJECT_E_SLOT_IS_WRITABLE(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_WRITABLE) != 0) +#define DUK_HOBJECT_E_SLOT_IS_ENUMERABLE(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_ENUMERABLE) != 0) +#define DUK_HOBJECT_E_SLOT_IS_CONFIGURABLE(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_CONFIGURABLE) != 0) +#define DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_ACCESSOR) != 0) + +#define DUK_HOBJECT_E_SLOT_SET_WRITABLE(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_WRITABLE) +#define DUK_HOBJECT_E_SLOT_SET_ENUMERABLE(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ENUMERABLE) +#define DUK_HOBJECT_E_SLOT_SET_CONFIGURABLE(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_CONFIGURABLE) +#define DUK_HOBJECT_E_SLOT_SET_ACCESSOR(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ACCESSOR) + +#define DUK_HOBJECT_E_SLOT_CLEAR_WRITABLE(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_WRITABLE) +#define DUK_HOBJECT_E_SLOT_CLEAR_ENUMERABLE(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ENUMERABLE) +#define DUK_HOBJECT_E_SLOT_CLEAR_CONFIGURABLE(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_CONFIGURABLE) +#define DUK_HOBJECT_E_SLOT_CLEAR_ACCESSOR(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ACCESSOR) + +#define DUK_PROPDESC_IS_WRITABLE(p) (((p)->flags & DUK_PROPDESC_FLAG_WRITABLE) != 0) +#define DUK_PROPDESC_IS_ENUMERABLE(p) (((p)->flags & DUK_PROPDESC_FLAG_ENUMERABLE) != 0) +#define DUK_PROPDESC_IS_CONFIGURABLE(p) (((p)->flags & DUK_PROPDESC_FLAG_CONFIGURABLE) != 0) +#define DUK_PROPDESC_IS_ACCESSOR(p) (((p)->flags & DUK_PROPDESC_FLAG_ACCESSOR) != 0) + +#define DUK_HOBJECT_HASHIDX_UNUSED 0xffffffffUL +#define DUK_HOBJECT_HASHIDX_DELETED 0xfffffffeUL + +/* + * Macros for accessing size fields + */ + +#if defined(DUK_USE_OBJSIZES16) +#define DUK_HOBJECT_GET_ESIZE(h) ((h)->e_size16) +#define DUK_HOBJECT_SET_ESIZE(h,v) do { (h)->e_size16 = (v); } while (0) +#define DUK_HOBJECT_GET_ENEXT(h) ((h)->e_next16) +#define DUK_HOBJECT_SET_ENEXT(h,v) do { (h)->e_next16 = (v); } while (0) +#define DUK_HOBJECT_POSTINC_ENEXT(h) ((h)->e_next16++) +#define DUK_HOBJECT_GET_ASIZE(h) ((h)->a_size16) +#define DUK_HOBJECT_SET_ASIZE(h,v) do { (h)->a_size16 = (v); } while (0) +#if defined(DUK_USE_HOBJECT_HASH_PART) +#define DUK_HOBJECT_GET_HSIZE(h) ((h)->h_size16) +#define DUK_HOBJECT_SET_HSIZE(h,v) do { (h)->h_size16 = (v); } while (0) +#else +#define DUK_HOBJECT_GET_HSIZE(h) 0 +#define DUK_HOBJECT_SET_HSIZE(h,v) do { DUK_ASSERT((v) == 0); } while (0) +#endif +#else +#define DUK_HOBJECT_GET_ESIZE(h) ((h)->e_size) +#define DUK_HOBJECT_SET_ESIZE(h,v) do { (h)->e_size = (v); } while (0) +#define DUK_HOBJECT_GET_ENEXT(h) ((h)->e_next) +#define DUK_HOBJECT_SET_ENEXT(h,v) do { (h)->e_next = (v); } while (0) +#define DUK_HOBJECT_POSTINC_ENEXT(h) ((h)->e_next++) +#define DUK_HOBJECT_GET_ASIZE(h) ((h)->a_size) +#define DUK_HOBJECT_SET_ASIZE(h,v) do { (h)->a_size = (v); } while (0) +#if defined(DUK_USE_HOBJECT_HASH_PART) +#define DUK_HOBJECT_GET_HSIZE(h) ((h)->h_size) +#define DUK_HOBJECT_SET_HSIZE(h,v) do { (h)->h_size = (v); } while (0) +#else +#define DUK_HOBJECT_GET_HSIZE(h) 0 +#define DUK_HOBJECT_SET_HSIZE(h,v) do { DUK_ASSERT((v) == 0); } while (0) +#endif +#endif + +/* + * Misc + */ + +/* Maximum prototype traversal depth. Sanity limit which handles e.g. + * prototype loops (even complex ones like 1->2->3->4->2->3->4->2->3->4). + */ +#define DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY 10000L + +/* Maximum traversal depth for "bound function" chains. */ +#define DUK_HOBJECT_BOUND_CHAIN_SANITY 10000L + +/* + * Ecmascript [[Class]] + */ + +/* range check not necessary because all 4-bit values are mapped */ +#define DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(n) duk_class_number_to_stridx[(n)] + +#define DUK_HOBJECT_GET_CLASS_STRING(heap,h) \ + DUK_HEAP_GET_STRING( \ + (heap), \ + DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(DUK_HOBJECT_GET_CLASS_NUMBER((h))) \ + ) + +/* + * Macros for property handling + */ + +#if defined(DUK_USE_HEAPPTR16) +#define DUK_HOBJECT_GET_PROTOTYPE(heap,h) \ + ((duk_hobject *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->prototype16)) +#define DUK_HOBJECT_SET_PROTOTYPE(heap,h,x) do { \ + (h)->prototype16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (x)); \ + } while (0) +#else +#define DUK_HOBJECT_GET_PROTOTYPE(heap,h) \ + ((h)->prototype) +#define DUK_HOBJECT_SET_PROTOTYPE(heap,h,x) do { \ + (h)->prototype = (x); \ + } while (0) +#endif + +/* note: this updates refcounts */ +#define DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr,h,p) duk_hobject_set_prototype_updref((thr), (h), (p)) + +/* + * Resizing and hash behavior + */ + +/* Sanity limit on max number of properties (allocated, not necessarily used). + * This is somewhat arbitrary, but if we're close to 2**32 properties some + * algorithms will fail (e.g. hash size selection, next prime selection). + * Also, we use negative array/entry table indices to indicate 'not found', + * so anything above 0x80000000 will cause trouble now. + */ +#if defined(DUK_USE_OBJSIZES16) +#define DUK_HOBJECT_MAX_PROPERTIES 0x0000ffffUL +#else +#define DUK_HOBJECT_MAX_PROPERTIES 0x7fffffffUL /* 2**31-1 ~= 2G properties */ +#endif + +/* higher value conserves memory; also note that linear scan is cache friendly */ +#define DUK_HOBJECT_E_USE_HASH_LIMIT 32 + +/* hash size relative to entries size: for value X, approx. hash_prime(e_size + e_size / X) */ +#define DUK_HOBJECT_H_SIZE_DIVISOR 4 /* hash size approx. 1.25 times entries size */ + +/* if new_size < L * old_size, resize without abandon check; L = 3-bit fixed point, e.g. 9 -> 9/8 = 112.5% */ +#define DUK_HOBJECT_A_FAST_RESIZE_LIMIT 9 /* 112.5%, i.e. new size less than 12.5% higher -> fast resize */ + +/* if density < L, abandon array part, L = 3-bit fixed point, e.g. 2 -> 2/8 = 25% */ +/* limit is quite low: one array entry is 8 bytes, one normal entry is 4+1+8+4 = 17 bytes (with hash entry) */ +#define DUK_HOBJECT_A_ABANDON_LIMIT 2 /* 25%, i.e. less than 25% used -> abandon */ + +/* internal align target for props allocation, must be 2*n for some n */ +#if (DUK_USE_ALIGN_BY == 4) +#define DUK_HOBJECT_ALIGN_TARGET 4 +#elif (DUK_USE_ALIGN_BY == 8) +#define DUK_HOBJECT_ALIGN_TARGET 8 +#elif (DUK_USE_ALIGN_BY == 1) +#define DUK_HOBJECT_ALIGN_TARGET 1 +#else +#error invalid DUK_USE_ALIGN_BY +#endif + +/* controls for minimum entry part growth */ +#define DUK_HOBJECT_E_MIN_GROW_ADD 16 +#define DUK_HOBJECT_E_MIN_GROW_DIVISOR 8 /* 2^3 -> 1/8 = 12.5% min growth */ + +/* controls for minimum array part growth */ +#define DUK_HOBJECT_A_MIN_GROW_ADD 16 +#define DUK_HOBJECT_A_MIN_GROW_DIVISOR 8 /* 2^3 -> 1/8 = 12.5% min growth */ + +/* probe sequence */ +#define DUK_HOBJECT_HASH_INITIAL(hash,h_size) ((hash) % (h_size)) +#define DUK_HOBJECT_HASH_PROBE_STEP(hash) DUK_UTIL_GET_HASH_PROBE_STEP((hash)) + +/* + * PC-to-line constants + */ + +#define DUK_PC2LINE_SKIP 64 + +/* maximum length for a SKIP-1 diffstream: 35 bits per entry, rounded up to bytes */ +#define DUK_PC2LINE_MAX_DIFF_LENGTH (((DUK_PC2LINE_SKIP - 1) * 35 + 7) / 8) + +/* + * Struct defs + */ + +struct duk_propaccessor { + duk_hobject *get; + duk_hobject *set; +}; + +union duk_propvalue { + /* The get/set pointers could be 16-bit pointer compressed but it + * would make no difference on 32-bit platforms because duk_tval is + * 8 bytes or more anyway. + */ + duk_tval v; + duk_propaccessor a; +}; + +struct duk_propdesc { + /* read-only values 'lifted' for ease of use */ + duk_small_int_t flags; + duk_hobject *get; + duk_hobject *set; + + /* for updating (all are set to < 0 for virtual properties) */ + duk_int_t e_idx; /* prop index in 'entry part', < 0 if not there */ + duk_int_t h_idx; /* prop index in 'hash part', < 0 if not there */ + duk_int_t a_idx; /* prop index in 'array part', < 0 if not there */ +}; + +struct duk_hobject { + duk_heaphdr hdr; + + /* + * 'props' contains {key,value,flags} entries, optional array entries, and + * an optional hash lookup table for non-array entries in a single 'sliced' + * allocation. There are several layout options, which differ slightly in + * generated code size/speed and alignment/padding; duk_features.h selects + * the layout used. + * + * Layout 1 (DUK_USE_HOBJECT_LAYOUT_1): + * + * e_size * sizeof(duk_hstring *) bytes of entry keys (e_next gc reachable) + * e_size * sizeof(duk_propvalue) bytes of entry values (e_next gc reachable) + * e_size * sizeof(duk_uint8_t) bytes of entry flags (e_next gc reachable) + * a_size * sizeof(duk_tval) bytes of (opt) array values (plain only) (all gc reachable) + * h_size * sizeof(duk_uint32_t) bytes of (opt) hash indexes to entries (e_size), + * 0xffffffffUL = unused, 0xfffffffeUL = deleted + * + * Layout 2 (DUK_USE_HOBJECT_LAYOUT_2): + * + * e_size * sizeof(duk_propvalue) bytes of entry values (e_next gc reachable) + * e_size * sizeof(duk_hstring *) bytes of entry keys (e_next gc reachable) + * e_size * sizeof(duk_uint8_t) + pad bytes of entry flags (e_next gc reachable) + * a_size * sizeof(duk_tval) bytes of (opt) array values (plain only) (all gc reachable) + * h_size * sizeof(duk_uint32_t) bytes of (opt) hash indexes to entries (e_size), + * 0xffffffffUL = unused, 0xfffffffeUL = deleted + * + * Layout 3 (DUK_USE_HOBJECT_LAYOUT_3): + * + * e_size * sizeof(duk_propvalue) bytes of entry values (e_next gc reachable) + * a_size * sizeof(duk_tval) bytes of (opt) array values (plain only) (all gc reachable) + * e_size * sizeof(duk_hstring *) bytes of entry keys (e_next gc reachable) + * h_size * sizeof(duk_uint32_t) bytes of (opt) hash indexes to entries (e_size), + * 0xffffffffUL = unused, 0xfffffffeUL = deleted + * e_size * sizeof(duk_uint8_t) bytes of entry flags (e_next gc reachable) + * + * In layout 1, the 'e_next' count is rounded to 4 or 8 on platforms + * requiring 4 or 8 byte alignment. This ensures proper alignment + * for the entries, at the cost of memory footprint. However, it's + * probably preferable to use another layout on such platforms instead. + * + * In layout 2, the key and value parts are swapped to avoid padding + * the key array on platforms requiring alignment by 8. The flags part + * is padded to get alignment for array entries. The 'e_next' count does + * not need to be rounded as in layout 1. + * + * In layout 3, entry values and array values are always aligned properly, + * and assuming pointers are at most 8 bytes, so are the entry keys. Hash + * indices will be properly aligned (assuming pointers are at least 4 bytes). + * Finally, flags don't need additional alignment. This layout provides + * compact allocations without padding (even on platforms with alignment + * requirements) at the cost of a bit slower lookups. + * + * Objects with few keys don't have a hash index; keys are looked up linearly, + * which is cache efficient because the keys are consecutive. Larger objects + * have a hash index part which contains integer indexes to the entries part. + * + * A single allocation reduces memory allocation overhead but requires more + * work when any part needs to be resized. A sliced allocation for entries + * makes linear key matching faster on most platforms (more locality) and + * skimps on flags size (which would be followed by 3 bytes of padding in + * most architectures if entries were placed in a struct). + * + * 'props' also contains internal properties distinguished with a non-BMP + * prefix. Often used properties should be placed early in 'props' whenever + * possible to make accessing them as fast a possible. + */ + +#if defined(DUK_USE_HEAPPTR16) + /* Located in duk_heaphdr h_extra16. Subclasses of duk_hobject (like + * duk_hcompiledfunction) are not free to use h_extra16 for this reason. + */ +#else + duk_uint8_t *props; +#endif + + /* prototype: the only internal property lifted outside 'e' as it is so central */ +#if defined(DUK_USE_HEAPPTR16) + duk_uint16_t prototype16; +#else + duk_hobject *prototype; +#endif + +#if defined(DUK_USE_OBJSIZES16) + duk_uint16_t e_size16; + duk_uint16_t e_next16; + duk_uint16_t a_size16; +#if defined(DUK_USE_HOBJECT_HASH_PART) + duk_uint16_t h_size16; +#endif +#else + duk_uint32_t e_size; /* entry part size */ + duk_uint32_t e_next; /* index for next new key ([0,e_next[ are gc reachable) */ + duk_uint32_t a_size; /* array part size (entirely gc reachable) */ +#if defined(DUK_USE_HOBJECT_HASH_PART) + duk_uint32_t h_size; /* hash part size or 0 if unused */ +#endif +#endif +}; + +/* + * Exposed data + */ + +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL duk_uint8_t duk_class_number_to_stridx[32]; +#endif /* !DUK_SINGLE_FILE */ + +/* + * Prototypes + */ + +/* alloc and init */ +DUK_INTERNAL_DECL duk_hobject *duk_hobject_alloc(duk_heap *heap, duk_uint_t hobject_flags); +#if 0 /* unused */ +DUK_INTERNAL_DECL duk_hobject *duk_hobject_alloc_checked(duk_hthread *thr, duk_uint_t hobject_flags); +#endif +DUK_INTERNAL_DECL duk_hcompiledfunction *duk_hcompiledfunction_alloc(duk_heap *heap, duk_uint_t hobject_flags); +DUK_INTERNAL_DECL duk_hnativefunction *duk_hnativefunction_alloc(duk_heap *heap, duk_uint_t hobject_flags); +DUK_INTERNAL duk_hbufferobject *duk_hbufferobject_alloc(duk_heap *heap, duk_uint_t hobject_flags); +DUK_INTERNAL_DECL duk_hthread *duk_hthread_alloc(duk_heap *heap, duk_uint_t hobject_flags); + +/* low-level property functions */ +DUK_INTERNAL_DECL void duk_hobject_find_existing_entry(duk_heap *heap, duk_hobject *obj, duk_hstring *key, duk_int_t *e_idx, duk_int_t *h_idx); +DUK_INTERNAL_DECL duk_tval *duk_hobject_find_existing_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_hstring *key); +DUK_INTERNAL_DECL duk_tval *duk_hobject_find_existing_entry_tval_ptr_and_attrs(duk_heap *heap, duk_hobject *obj, duk_hstring *key, duk_int_t *out_attrs); +DUK_INTERNAL_DECL duk_tval *duk_hobject_find_existing_array_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_uarridx_t i); +DUK_INTERNAL_DECL duk_bool_t duk_hobject_get_own_propdesc(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *out_desc, duk_small_uint_t flags); + +/* XXX: when optimizing for guaranteed property slots, use a guaranteed + * slot for internal value; this call can then access it directly. + */ +#define duk_hobject_get_internal_value_tval_ptr(heap,obj) \ + duk_hobject_find_existing_entry_tval_ptr((heap), (obj), DUK_HEAP_STRING_INT_VALUE((heap))) + +/* core property functions */ +DUK_INTERNAL_DECL duk_bool_t duk_hobject_getprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key); +DUK_INTERNAL_DECL duk_bool_t duk_hobject_putprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key, duk_tval *tv_val, duk_bool_t throw_flag); +DUK_INTERNAL_DECL duk_bool_t duk_hobject_delprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key, duk_bool_t throw_flag); +DUK_INTERNAL_DECL duk_bool_t duk_hobject_hasprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key); + +/* internal property functions */ +#define DUK_DELPROP_FLAG_THROW (1 << 0) +#define DUK_DELPROP_FLAG_FORCE (1 << 1) +DUK_INTERNAL_DECL duk_bool_t duk_hobject_delprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags); +DUK_INTERNAL_DECL duk_bool_t duk_hobject_hasprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key); +DUK_INTERNAL_DECL void duk_hobject_define_property_internal(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags); +DUK_INTERNAL_DECL void duk_hobject_define_property_internal_arridx(duk_hthread *thr, duk_hobject *obj, duk_uarridx_t arr_idx, duk_small_uint_t flags); +DUK_INTERNAL_DECL void duk_hobject_define_accessor_internal(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_hobject *getter, duk_hobject *setter, duk_small_uint_t propflags); +DUK_INTERNAL_DECL void duk_hobject_set_length(duk_hthread *thr, duk_hobject *obj, duk_uint32_t length); /* XXX: duk_uarridx_t? */ +DUK_INTERNAL_DECL void duk_hobject_set_length_zero(duk_hthread *thr, duk_hobject *obj); +DUK_INTERNAL_DECL duk_uint32_t duk_hobject_get_length(duk_hthread *thr, duk_hobject *obj); /* XXX: duk_uarridx_t? */ + +/* helpers for defineProperty() and defineProperties() */ +DUK_INTERNAL_DECL +void duk_hobject_prepare_property_descriptor(duk_context *ctx, + duk_idx_t idx_in, + duk_uint_t *out_defprop_flags, + duk_idx_t *out_idx_value, + duk_hobject **out_getter, + duk_hobject **out_setter); +DUK_INTERNAL_DECL +void duk_hobject_define_property_helper(duk_context *ctx, + duk_uint_t defprop_flags, + duk_hobject *obj, + duk_hstring *key, + duk_idx_t idx_value, + duk_hobject *get, + duk_hobject *set); + +/* Object built-in methods */ +DUK_INTERNAL_DECL duk_ret_t duk_hobject_object_get_own_property_descriptor(duk_context *ctx); +DUK_INTERNAL_DECL void duk_hobject_object_seal_freeze_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_freeze); +DUK_INTERNAL_DECL duk_bool_t duk_hobject_object_is_sealed_frozen_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_frozen); +DUK_INTERNAL_DECL duk_bool_t duk_hobject_object_ownprop_helper(duk_context *ctx, duk_small_uint_t required_desc_flags); + +/* internal properties */ +DUK_INTERNAL_DECL duk_bool_t duk_hobject_get_internal_value(duk_heap *heap, duk_hobject *obj, duk_tval *tv); +DUK_INTERNAL_DECL duk_hstring *duk_hobject_get_internal_value_string(duk_heap *heap, duk_hobject *obj); + +/* hobject management functions */ +DUK_INTERNAL_DECL void duk_hobject_compact_props(duk_hthread *thr, duk_hobject *obj); + +/* ES6 proxy */ +#if defined(DUK_USE_ES6_PROXY) +DUK_INTERNAL_DECL duk_bool_t duk_hobject_proxy_check(duk_hthread *thr, duk_hobject *obj, duk_hobject **out_target, duk_hobject **out_handler); +DUK_INTERNAL_DECL duk_hobject *duk_hobject_resolve_proxy_target(duk_hthread *thr, duk_hobject *obj); +#endif + +/* enumeration */ +DUK_INTERNAL_DECL void duk_hobject_enumerator_create(duk_context *ctx, duk_small_uint_t enum_flags); +DUK_INTERNAL_DECL duk_ret_t duk_hobject_get_enumerated_keys(duk_context *ctx, duk_small_uint_t enum_flags); +DUK_INTERNAL_DECL duk_bool_t duk_hobject_enumerator_next(duk_context *ctx, duk_bool_t get_value); + +/* macros */ +DUK_INTERNAL_DECL void duk_hobject_set_prototype_updref(duk_hthread *thr, duk_hobject *h, duk_hobject *p); + +/* finalization */ +DUK_INTERNAL_DECL void duk_hobject_run_finalizer(duk_hthread *thr, duk_hobject *obj); + +/* pc2line */ +#if defined(DUK_USE_PC2LINE) +DUK_INTERNAL_DECL void duk_hobject_pc2line_pack(duk_hthread *thr, duk_compiler_instr *instrs, duk_uint_fast32_t length); +DUK_INTERNAL_DECL duk_uint_fast32_t duk_hobject_pc2line_query(duk_context *ctx, duk_idx_t idx_func, duk_uint_fast32_t pc); +#endif + +/* misc */ +DUK_INTERNAL_DECL duk_bool_t duk_hobject_prototype_chain_contains(duk_hthread *thr, duk_hobject *h, duk_hobject *p, duk_bool_t ignore_loop); + +#endif /* DUK_HOBJECT_H_INCLUDED */ +/* + * Heap compiled function (Ecmascript function) representation. + * + * There is a single data buffer containing the Ecmascript function's + * bytecode, constants, and inner functions. + */ + +#ifndef DUK_HCOMPILEDFUNCTION_H_INCLUDED +#define DUK_HCOMPILEDFUNCTION_H_INCLUDED + +/* + * Field accessor macros + */ + +/* XXX: casts could be improved, especially for GET/SET DATA */ + +#if defined(DUK_USE_HEAPPTR16) +#define DUK_HCOMPILEDFUNCTION_GET_DATA(heap,h) \ + ((duk_hbuffer_fixed *) (void *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->data16)) +#define DUK_HCOMPILEDFUNCTION_SET_DATA(heap,h,v) do { \ + (h)->data16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \ + } while (0) +#define DUK_HCOMPILEDFUNCTION_GET_FUNCS(heap,h) \ + ((duk_hobject **) (void *) (DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->funcs16))) +#define DUK_HCOMPILEDFUNCTION_SET_FUNCS(heap,h,v) do { \ + (h)->funcs16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \ + } while (0) +#define DUK_HCOMPILEDFUNCTION_GET_BYTECODE(heap,h) \ + ((duk_instr_t *) (void *) (DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->bytecode16))) +#define DUK_HCOMPILEDFUNCTION_SET_BYTECODE(heap,h,v) do { \ + (h)->bytecode16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \ + } while (0) +#else +#define DUK_HCOMPILEDFUNCTION_GET_DATA(heap,h) \ + ((duk_hbuffer_fixed *) (void *) (h)->data) +#define DUK_HCOMPILEDFUNCTION_SET_DATA(heap,h,v) do { \ + (h)->data = (duk_hbuffer *) (v); \ + } while (0) +#define DUK_HCOMPILEDFUNCTION_GET_FUNCS(heap,h) \ + ((h)->funcs) +#define DUK_HCOMPILEDFUNCTION_SET_FUNCS(heap,h,v) do { \ + (h)->funcs = (v); \ + } while (0) +#define DUK_HCOMPILEDFUNCTION_GET_BYTECODE(heap,h) \ + ((h)->bytecode) +#define DUK_HCOMPILEDFUNCTION_SET_BYTECODE(heap,h,v) do { \ + (h)->bytecode = (v); \ + } while (0) +#endif + +/* + * Accessor macros for function specific data areas + */ + +/* Note: assumes 'data' is always a fixed buffer */ +#define DUK_HCOMPILEDFUNCTION_GET_BUFFER_BASE(heap,h) \ + DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), DUK_HCOMPILEDFUNCTION_GET_DATA((heap), (h))) + +#define DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(heap,h) \ + ((duk_tval *) (void *) DUK_HCOMPILEDFUNCTION_GET_BUFFER_BASE((heap), (h))) + +#define DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(heap,h) \ + DUK_HCOMPILEDFUNCTION_GET_FUNCS((heap), (h)) + +#define DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(heap,h) \ + DUK_HCOMPILEDFUNCTION_GET_BYTECODE((heap), (h)) + +#define DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(heap,h) \ + ((duk_tval *) (void *) DUK_HCOMPILEDFUNCTION_GET_FUNCS((heap), (h))) + +#define DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(heap,h) \ + ((duk_hobject **) (void *) DUK_HCOMPILEDFUNCTION_GET_BYTECODE((heap), (h))) + +/* XXX: double evaluation of DUK_HCOMPILEDFUNCTION_GET_DATA() */ +#define DUK_HCOMPILEDFUNCTION_GET_CODE_END(heap,h) \ + ((duk_instr_t *) (void *) (DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), DUK_HCOMPILEDFUNCTION_GET_DATA((heap), (h))) + \ + DUK_HBUFFER_GET_SIZE((duk_hbuffer *) DUK_HCOMPILEDFUNCTION_GET_DATA((heap), h)))) + +#define DUK_HCOMPILEDFUNCTION_GET_CONSTS_SIZE(heap,h) \ + ( \ + (duk_size_t) \ + ( \ + ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_END((heap), (h))) - \ + ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE((heap), (h))) \ + ) \ + ) + +#define DUK_HCOMPILEDFUNCTION_GET_FUNCS_SIZE(heap,h) \ + ( \ + (duk_size_t) \ + ( \ + ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_END((heap), (h))) - \ + ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE((heap), (h))) \ + ) \ + ) + +#define DUK_HCOMPILEDFUNCTION_GET_CODE_SIZE(heap,h) \ + ( \ + (duk_size_t) \ + ( \ + ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CODE_END((heap),(h))) - \ + ((const duk_uint8_t *) DUK_HCOMPILEDFUNCTION_GET_CODE_BASE((heap),(h))) \ + ) \ + ) + +#define DUK_HCOMPILEDFUNCTION_GET_CONSTS_COUNT(heap,h) \ + ((duk_size_t) (DUK_HCOMPILEDFUNCTION_GET_CONSTS_SIZE((heap), (h)) / sizeof(duk_tval))) + +#define DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(heap,h) \ + ((duk_size_t) (DUK_HCOMPILEDFUNCTION_GET_FUNCS_SIZE((heap), (h)) / sizeof(duk_hobject *))) + +#define DUK_HCOMPILEDFUNCTION_GET_CODE_COUNT(heap,h) \ + ((duk_size_t) (DUK_HCOMPILEDFUNCTION_GET_CODE_SIZE((heap), (h)) / sizeof(duk_instr_t))) + + +/* + * Main struct + */ + +struct duk_hcompiledfunction { + /* shared object part */ + duk_hobject obj; + + /* + * Pointers to function data area for faster access. Function + * data is a buffer shared between all closures of the same + * "template" function. The data buffer is always fixed (non- + * dynamic, hence stable), with a layout as follows: + * + * constants (duk_tval) + * inner functions (duk_hobject *) + * bytecode (duk_instr_t) + * + * Note: bytecode end address can be computed from 'data' buffer + * size. It is not strictly necessary functionally, assuming + * bytecode never jumps outside its allocated area. However, + * it's a safety/robustness feature for avoiding the chance of + * executing random data as bytecode due to a compiler error. + * + * Note: values in the data buffer must be incref'd (they will + * be decref'd on release) for every compiledfunction referring + * to the 'data' element. + */ + + /* Data area, fixed allocation, stable data ptrs. */ +#if defined(DUK_USE_HEAPPTR16) + duk_uint16_t data16; +#else + duk_hbuffer *data; +#endif + + /* No need for constants pointer (= same as data). + * + * When using 16-bit packing alignment to 4 is nice. 'funcs' will be + * 4-byte aligned because 'constants' are duk_tvals. For now the + * inner function pointers are not compressed, so that 'bytecode' will + * also be 4-byte aligned. + */ +#if defined(DUK_USE_HEAPPTR16) + duk_uint16_t funcs16; + duk_uint16_t bytecode16; +#else + duk_hobject **funcs; + duk_instr_t *bytecode; +#endif + + /* + * 'nregs' registers are allocated on function entry, at most 'nargs' + * are initialized to arguments, and the rest to undefined. Arguments + * above 'nregs' are not mapped to registers. All registers in the + * active stack range must be initialized because they are GC reachable. + * 'nargs' is needed so that if the function is given more than 'nargs' + * arguments, the additional arguments do not 'clobber' registers + * beyond 'nregs' which must be consistently initialized to undefined. + * + * Usually there is no need to know which registers are mapped to + * local variables. Registers may be allocated to variable in any + * way (even including gaps). However, a register-variable mapping + * must be the same for the duration of the function execution and + * the register cannot be used for anything else. + * + * When looking up variables by name, the '_Varmap' map is used. + * When an activation closes, registers mapped to arguments are + * copied into the environment record based on the same map. The + * reverse map (from register to variable) is not currently needed + * at run time, except for debugging, so it is not maintained. + */ + + duk_uint16_t nregs; /* regs to allocate */ + duk_uint16_t nargs; /* number of arguments allocated to regs */ + + /* + * Additional control information is placed into the object itself + * as internal properties to avoid unnecessary fields for the + * majority of functions. The compiler tries to omit internal + * control fields when possible. + * + * Function templates: + * + * { + * name: "func", // declaration, named function expressions + * fileName: + * _Varmap: { "arg1": 0, "arg2": 1, "varname": 2 }, + * _Formals: [ "arg1", "arg2" ], + * _Source: "function func(arg1, arg2) { ... }", + * _Pc2line: , + * } + * + * Function instances: + * + * { + * length: 2, + * prototype: { constructor: }, + * caller: , + * arguments: , + * name: "func", // declaration, named function expressions + * fileName: + * _Varmap: { "arg1": 0, "arg2": 1, "varname": 2 }, + * _Formals: [ "arg1", "arg2" ], + * _Source: "function func(arg1, arg2) { ... }", + * _Pc2line: , + * _Varenv: , + * _Lexenv: + * } + * + * More detailed description of these properties can be found + * in the documentation. + */ + +#if defined(DUK_USE_DEBUGGER_SUPPORT) + /* Line number range for function. Needed during debugging to + * determine active breakpoints. + */ + duk_uint32_t start_line; + duk_uint32_t end_line; +#endif +}; + +#endif /* DUK_HCOMPILEDFUNCTION_H_INCLUDED */ +/* + * Heap native function representation. + */ + +#ifndef DUK_HNATIVEFUNCTION_H_INCLUDED +#define DUK_HNATIVEFUNCTION_H_INCLUDED + +#define DUK_HNATIVEFUNCTION_NARGS_VARARGS ((duk_int16_t) -1) +#define DUK_HNATIVEFUNCTION_NARGS_MAX ((duk_int16_t) 0x7fff) + +struct duk_hnativefunction { + /* shared object part */ + duk_hobject obj; + + duk_c_function func; + duk_int16_t nargs; + duk_int16_t magic; + + /* The 'magic' field allows an opaque 16-bit field to be accessed by the + * Duktape/C function. This allows, for instance, the same native function + * to be used for a set of very similar functions, with the 'magic' field + * providing the necessary non-argument flags / values to guide the behavior + * of the native function. The value is signed on purpose: it is easier to + * convert a signed value to unsigned (simply AND with 0xffff) than vice + * versa. + * + * Note: cannot place nargs/magic into the heaphdr flags, because + * duk_hobject takes almost all flags already (and needs the spare). + */ +}; + +#endif /* DUK_HNATIVEFUNCTION_H_INCLUDED */ +/* + * Heap Buffer object representation. Used for all Buffer variants. + */ + +#ifndef DUK_HBUFFEROBJECT_H_INCLUDED +#define DUK_HBUFFEROBJECT_H_INCLUDED + +/* All element accessors are host endian now (driven by TypedArray spec). */ +#define DUK_HBUFFEROBJECT_ELEM_UINT8 0 +#define DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED 1 +#define DUK_HBUFFEROBJECT_ELEM_INT8 2 +#define DUK_HBUFFEROBJECT_ELEM_UINT16 3 +#define DUK_HBUFFEROBJECT_ELEM_INT16 4 +#define DUK_HBUFFEROBJECT_ELEM_UINT32 5 +#define DUK_HBUFFEROBJECT_ELEM_INT32 6 +#define DUK_HBUFFEROBJECT_ELEM_FLOAT32 7 +#define DUK_HBUFFEROBJECT_ELEM_FLOAT64 8 +#define DUK_HBUFFEROBJECT_ELEM_MAX 8 + +#define DUK_ASSERT_HBUFFEROBJECT_VALID(h) do { \ + DUK_ASSERT((h) != NULL); \ + DUK_ASSERT((h)->shift <= 3); \ + DUK_ASSERT((h)->elem_type <= DUK_HBUFFEROBJECT_ELEM_MAX); \ + DUK_ASSERT(((h)->shift == 0 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8) || \ + ((h)->shift == 0 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED) || \ + ((h)->shift == 0 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_INT8) || \ + ((h)->shift == 1 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT16) || \ + ((h)->shift == 1 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_INT16) || \ + ((h)->shift == 2 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT32) || \ + ((h)->shift == 2 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_INT32) || \ + ((h)->shift == 2 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_FLOAT32) || \ + ((h)->shift == 3 && (h)->elem_type == DUK_HBUFFEROBJECT_ELEM_FLOAT64)); \ + DUK_ASSERT((h)->is_view == 0 || (h)->is_view == 1); \ + DUK_ASSERT(DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) (h))); \ + if ((h)->buf == NULL) { \ + DUK_ASSERT((h)->offset == 0); \ + DUK_ASSERT((h)->length == 0); \ + } else { \ + /* No assertions for offset or length; in particular, \ + * it's OK for length to be longer than underlying \ + * buffer. Just ensure they don't wrap when added. \ + */ \ + DUK_ASSERT((h)->offset + (h)->length >= (h)->offset); \ + } \ + } while (0) + +/* Get the current data pointer (caller must ensure buf != NULL) as a + * duk_uint8_t ptr. + */ +#define DUK_HBUFFEROBJECT_GET_SLICE_BASE(heap,h) \ + (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \ + (((duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR((heap), (h)->buf)) + (h)->offset)) + +/* True if slice is full, i.e. offset is zero and length covers the entire + * buffer. This status may change independently of the duk_hbufferobject if + * the underlying buffer is dynamic and changes without the hbufferobject + * being changed. + */ +#define DUK_HBUFFEROBJECT_FULL_SLICE(h) \ + (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \ + ((h)->offset == 0 && (h)->length == DUK_HBUFFER_GET_SIZE((h)->buf))) + +/* Validate that the whole slice [0,length[ is contained in the underlying + * buffer. Caller must ensure 'buf' != NULL. + */ +#define DUK_HBUFFEROBJECT_VALID_SLICE(h) \ + (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \ + ((h)->offset + (h)->length <= DUK_HBUFFER_GET_SIZE((h)->buf))) + +/* Validate byte read/write for virtual 'offset', i.e. check that the + * offset, taking into account h->offset, is within the underlying + * buffer size. This is a safety check which is needed to ensure + * that even a misconfigured duk_hbufferobject never causes memory + * unsafe behavior (e.g. if an underlying dynamic buffer changes + * after being setup). Caller must ensure 'buf' != NULL. + */ +#define DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_INCL(h,off) \ + (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \ + ((h)->offset + (off) < DUK_HBUFFER_GET_SIZE((h)->buf))) + +#define DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h,off) \ + (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \ + ((h)->offset + (off) <= DUK_HBUFFER_GET_SIZE((h)->buf))) + +/* Clamp an input byte length (already assumed to be within the nominal + * duk_hbufferobject 'length') to the current dynamic buffer limits to + * yield a byte length limit that's safe for memory accesses. This value + * can be invalidated by any side effect because it may trigger a user + * callback that resizes the underlying buffer. + */ +#define DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h,len) \ + (DUK_ASSERT_EXPR((h) != NULL), \ + duk_hbufferobject_clamp_bytelength((h), (len))) + +struct duk_hbufferobject { + /* Shared object part. */ + duk_hobject obj; + + /* Underlying buffer (refcounted), may be NULL. */ + duk_hbuffer *buf; + + /* Slice and accessor information. + * + * Because the underlying buffer may be dynamic, these may be + * invalidated by the buffer being modified so that both offset + * and length should be validated before every access. Behavior + * when the underlying buffer has changed doesn't need to be clean: + * virtual 'length' doesn't need to be affected, reads can return + * zero/NaN, and writes can be ignored. + * + * Note that a data pointer cannot be precomputed because 'buf' may + * be dynamic and its pointer unstable. + */ + + duk_uint_t offset; /* byte offset to buf */ + duk_uint_t length; /* byte index limit for element access, exclusive */ + duk_uint8_t shift; /* element size shift: + * 0 = u8/i8 + * 1 = u16/i16 + * 2 = u32/i32/float + * 3 = double + */ + duk_uint8_t elem_type; /* element type */ + duk_uint8_t is_view; +}; + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL_DECL duk_uint_t duk_hbufferobject_clamp_bytelength(duk_hbufferobject *h_bufobj, duk_uint_t len); +#endif +DUK_INTERNAL_DECL void duk_hbufferobject_push_validated_read(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size); +DUK_INTERNAL_DECL void duk_hbufferobject_validated_write(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size); + +#endif /* DUK_HBUFFEROBJECT_H_INCLUDED */ +/* + * Heap thread object representation. + * + * duk_hthread is also the 'context' (duk_context) for exposed APIs + * which mostly operate on the topmost frame of the value stack. + */ + +#ifndef DUK_HTHREAD_H_INCLUDED +#define DUK_HTHREAD_H_INCLUDED + +/* + * Stack constants + */ + +#define DUK_VALSTACK_GROW_STEP 128 /* roughly 1 kiB */ +#define DUK_VALSTACK_SHRINK_THRESHOLD 256 /* roughly 2 kiB */ +#define DUK_VALSTACK_SHRINK_SPARE 64 /* roughly 0.5 kiB */ +#define DUK_VALSTACK_INITIAL_SIZE 128 /* roughly 1.0 kiB -> but rounds up to DUK_VALSTACK_GROW_STEP in practice */ +#define DUK_VALSTACK_INTERNAL_EXTRA 64 /* internal extra elements assumed on function entry, + * always added to user-defined 'extra' for e.g. the + * duk_check_stack() call. + */ +#define DUK_VALSTACK_API_ENTRY_MINIMUM DUK_API_ENTRY_STACK + /* number of elements guaranteed to be user accessible + * (in addition to call arguments) on Duktape/C function entry. + */ + +/* Note: DUK_VALSTACK_INITIAL_SIZE must be >= DUK_VALSTACK_API_ENTRY_MINIMUM + * + DUK_VALSTACK_INTERNAL_EXTRA so that the initial stack conforms to spare + * requirements. + */ + +#define DUK_VALSTACK_DEFAULT_MAX 1000000L + +#define DUK_CALLSTACK_GROW_STEP 8 /* roughly 256 bytes */ +#define DUK_CALLSTACK_SHRINK_THRESHOLD 16 /* roughly 512 bytes */ +#define DUK_CALLSTACK_SHRINK_SPARE 8 /* roughly 256 bytes */ +#define DUK_CALLSTACK_INITIAL_SIZE 8 +#define DUK_CALLSTACK_DEFAULT_MAX 10000L + +#define DUK_CATCHSTACK_GROW_STEP 4 /* roughly 64 bytes */ +#define DUK_CATCHSTACK_SHRINK_THRESHOLD 8 /* roughly 128 bytes */ +#define DUK_CATCHSTACK_SHRINK_SPARE 4 /* roughly 64 bytes */ +#define DUK_CATCHSTACK_INITIAL_SIZE 4 +#define DUK_CATCHSTACK_DEFAULT_MAX 10000L + +/* + * Activation defines + */ + +#define DUK_ACT_FLAG_STRICT (1 << 0) /* function executes in strict mode */ +#define DUK_ACT_FLAG_TAILCALLED (1 << 1) /* activation has tail called one or more times */ +#define DUK_ACT_FLAG_CONSTRUCT (1 << 2) /* function executes as a constructor (called via "new") */ +#define DUK_ACT_FLAG_PREVENT_YIELD (1 << 3) /* activation prevents yield (native call or "new") */ +#define DUK_ACT_FLAG_DIRECT_EVAL (1 << 4) /* activation is a direct eval call */ +#define DUK_ACT_FLAG_BREAKPOINT_ACTIVE (1 << 5) /* activation has active breakpoint(s) */ + +#define DUK_ACT_GET_FUNC(act) ((act)->func) + +/* + * Flags for __FILE__ / __LINE__ registered into tracedata + */ + +#define DUK_TB_FLAG_NOBLAME_FILELINE (1 << 0) /* don't report __FILE__ / __LINE__ as fileName/lineNumber */ + +/* + * Catcher defines + */ + +/* flags field: LLLLLLFT, L = label (24 bits), F = flags (4 bits), T = type (4 bits) */ +#define DUK_CAT_TYPE_MASK 0x0000000fUL +#define DUK_CAT_TYPE_BITS 4 +#define DUK_CAT_LABEL_MASK 0xffffff00UL +#define DUK_CAT_LABEL_BITS 24 +#define DUK_CAT_LABEL_SHIFT 8 + +#define DUK_CAT_FLAG_CATCH_ENABLED (1 << 4) /* catch part will catch */ +#define DUK_CAT_FLAG_FINALLY_ENABLED (1 << 5) /* finally part will catch */ +#define DUK_CAT_FLAG_CATCH_BINDING_ENABLED (1 << 6) /* request to create catch binding */ +#define DUK_CAT_FLAG_LEXENV_ACTIVE (1 << 7) /* catch or with binding is currently active */ + +#define DUK_CAT_TYPE_UNKNOWN 0 +#define DUK_CAT_TYPE_TCF 1 +#define DUK_CAT_TYPE_LABEL 2 + +#define DUK_CAT_GET_TYPE(c) ((c)->flags & DUK_CAT_TYPE_MASK) +#define DUK_CAT_GET_LABEL(c) (((c)->flags & DUK_CAT_LABEL_MASK) >> DUK_CAT_LABEL_SHIFT) + +#define DUK_CAT_HAS_CATCH_ENABLED(c) ((c)->flags & DUK_CAT_FLAG_CATCH_ENABLED) +#define DUK_CAT_HAS_FINALLY_ENABLED(c) ((c)->flags & DUK_CAT_FLAG_FINALLY_ENABLED) +#define DUK_CAT_HAS_CATCH_BINDING_ENABLED(c) ((c)->flags & DUK_CAT_FLAG_CATCH_BINDING_ENABLED) +#define DUK_CAT_HAS_LEXENV_ACTIVE(c) ((c)->flags & DUK_CAT_FLAG_LEXENV_ACTIVE) + +#define DUK_CAT_SET_CATCH_ENABLED(c) do { \ + (c)->flags |= DUK_CAT_FLAG_CATCH_ENABLED; \ + } while (0) +#define DUK_CAT_SET_FINALLY_ENABLED(c) do { \ + (c)->flags |= DUK_CAT_FLAG_FINALLY_ENABLED; \ + } while (0) +#define DUK_CAT_SET_CATCH_BINDING_ENABLED(c) do { \ + (c)->flags |= DUK_CAT_FLAG_CATCH_BINDING_ENABLED; \ + } while (0) +#define DUK_CAT_SET_LEXENV_ACTIVE(c) do { \ + (c)->flags |= DUK_CAT_FLAG_LEXENV_ACTIVE; \ + } while (0) + +#define DUK_CAT_CLEAR_CATCH_ENABLED(c) do { \ + (c)->flags &= ~DUK_CAT_FLAG_CATCH_ENABLED; \ + } while (0) +#define DUK_CAT_CLEAR_FINALLY_ENABLED(c) do { \ + (c)->flags &= ~DUK_CAT_FLAG_FINALLY_ENABLED; \ + } while (0) +#define DUK_CAT_CLEAR_CATCH_BINDING_ENABLED(c) do { \ + (c)->flags &= ~DUK_CAT_FLAG_CATCH_BINDING_ENABLED; \ + } while (0) +#define DUK_CAT_CLEAR_LEXENV_ACTIVE(c) do { \ + (c)->flags &= ~DUK_CAT_FLAG_LEXENV_ACTIVE; \ + } while (0) + +/* + * Thread defines + */ + +#if defined(DUK_USE_ROM_STRINGS) +#define DUK_HTHREAD_GET_STRING(thr,idx) \ + ((duk_hstring *) DUK_LOSE_CONST(duk_rom_strings_stridx[(idx)])) +#else /* DUK_USE_ROM_STRINGS */ +#if defined(DUK_USE_HEAPPTR16) +#define DUK_HTHREAD_GET_STRING(thr,idx) \ + ((duk_hstring *) DUK_USE_HEAPPTR_DEC16((thr)->heap->heap_udata, (thr)->strs16[(idx)])) +#else +#define DUK_HTHREAD_GET_STRING(thr,idx) \ + ((thr)->strs[(idx)]) +#endif +#endif /* DUK_USE_ROM_STRINGS */ + +#define DUK_HTHREAD_GET_CURRENT_ACTIVATION(thr) (&(thr)->callstack[(thr)->callstack_top - 1]) + +/* values for the state field */ +#define DUK_HTHREAD_STATE_INACTIVE 1 /* thread not currently running */ +#define DUK_HTHREAD_STATE_RUNNING 2 /* thread currently running (only one at a time) */ +#define DUK_HTHREAD_STATE_RESUMED 3 /* thread resumed another thread (active but not running) */ +#define DUK_HTHREAD_STATE_YIELDED 4 /* thread has yielded */ +#define DUK_HTHREAD_STATE_TERMINATED 5 /* thread has terminated */ + +/* Executor interrupt default interval when nothing else requires a + * smaller value. The default interval must be small enough to allow + * for reasonable execution timeout checking but large enough to keep + * impact on execution performance low. + */ +#if defined(DUK_USE_INTERRUPT_COUNTER) +#define DUK_HTHREAD_INTCTR_DEFAULT (256L * 1024L) +#endif + +/* + * Assert context is valid: non-NULL pointer, fields look sane. + * + * This is used by public API call entrypoints to catch invalid 'ctx' pointers + * as early as possible; invalid 'ctx' pointers cause very odd and difficult to + * diagnose behavior so it's worth checking even when the check is not 100%. + */ + +#if defined(DUK_USE_PREFER_SIZE) +#define DUK_ASSERT_CTX_VSSIZE(ctx) /*nop*/ +#else +#define DUK_ASSERT_CTX_VSSIZE(ctx) \ + DUK_ASSERT((duk_size_t) (((duk_hthread *) (ctx))->valstack_end - ((duk_hthread *) (ctx))->valstack) == \ + ((duk_hthread *) (ctx))->valstack_size) +#endif +#define DUK_ASSERT_CTX_VALID(ctx) do { \ + DUK_ASSERT((ctx) != NULL); \ + DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) (ctx)) == DUK_HTYPE_OBJECT); \ + DUK_ASSERT(DUK_HOBJECT_IS_THREAD((duk_hobject *) (ctx))); \ + DUK_ASSERT(((duk_hthread *) (ctx))->unused1 == 0); \ + DUK_ASSERT(((duk_hthread *) (ctx))->unused2 == 0); \ + DUK_ASSERT(((duk_hthread *) (ctx))->valstack != NULL); \ + DUK_ASSERT(((duk_hthread *) (ctx))->valstack_end >= ((duk_hthread *) (ctx))->valstack); \ + DUK_ASSERT(((duk_hthread *) (ctx))->valstack_top >= ((duk_hthread *) (ctx))->valstack); \ + DUK_ASSERT(((duk_hthread *) (ctx))->valstack_top >= ((duk_hthread *) (ctx))->valstack_bottom); \ + DUK_ASSERT(((duk_hthread *) (ctx))->valstack_end >= ((duk_hthread *) (ctx))->valstack_top); \ + DUK_ASSERT_CTX_VSSIZE((ctx)); \ + } while (0) + +/* + * Struct defines + */ + +/* XXX: for a memory-code tradeoff, remove 'func' and make it's access either a function + * or a macro. This would make the activation 32 bytes long on 32-bit platforms again. + */ + +/* Note: it's nice if size is 2^N (at least for 32-bit platforms). */ +struct duk_activation { + duk_tval tv_func; /* borrowed: full duk_tval for function being executed; for lightfuncs */ + duk_hobject *func; /* borrowed: function being executed; for bound function calls, this is the final, real function, NULL for lightfuncs */ + duk_hobject *var_env; /* current variable environment (may be NULL if delayed) */ + duk_hobject *lex_env; /* current lexical environment (may be NULL if delayed) */ +#ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY + /* Previous value of 'func' caller, restored when unwound. Only in use + * when 'func' is non-strict. + */ + duk_hobject *prev_caller; +#endif + + duk_instr_t *curr_pc; /* next instruction to execute (points to 'func' bytecode, stable pointer), NULL for native calls */ +#if defined(DUK_USE_DEBUGGER_SUPPORT) + duk_uint32_t prev_line; /* needed for stepping */ +#endif + duk_small_uint_t flags; + + /* idx_bottom and idx_retval are only used for book-keeping of + * Ecmascript-initiated calls, to allow returning to an Ecmascript + * function properly. They are duk_size_t to match the convention + * that value stack sizes are duk_size_t and local frame indices + * are duk_idx_t. + */ + + /* Bottom of valstack for this activation, used to reset + * valstack_bottom on return; index is absolute. Note: + * idx_top not needed because top is set to 'nregs' always + * when returning to an Ecmascript activation. + */ + duk_size_t idx_bottom; + + /* Return value when returning to this activation (points to caller + * reg, not callee reg); index is absolute (only set if activation is + * not topmost). + * + * Note: idx_bottom is always set, while idx_retval is only applicable + * for activations below the topmost one. Currently idx_retval for + * the topmost activation is considered garbage (and it not initialized + * on entry or cleared on return; may contain previous or garbage + * values). + */ + duk_size_t idx_retval; + + /* Current 'this' binding is the value just below idx_bottom. + * Previously, 'this' binding was handled with an index to the + * (calling) valstack. This works for everything except tail + * calls, which must not "cumulate" valstack temps. + */ +}; + +/* Note: it's nice if size is 2^N (not 4x4 = 16 bytes on 32 bit) */ +struct duk_catcher { + duk_hstring *h_varname; /* borrowed reference to catch variable name (or NULL if none) */ + /* (reference is valid as long activation exists) */ + duk_instr_t *pc_base; /* resume execution from pc_base or pc_base+1 (points to 'func' bytecode, stable pointer) */ + duk_size_t callstack_index; /* callstack index of related activation */ + duk_size_t idx_base; /* idx_base and idx_base+1 get completion value and type */ + duk_uint32_t flags; /* type and control flags, label number */ +}; + +struct duk_hthread { + /* Shared object part */ + duk_hobject obj; + + /* Pointer to bytecode executor's 'curr_pc' variable. Used to copy + * the current PC back into the topmost activation when activation + * state is about to change (or "syncing" is otherwise needed). This + * is rather awkward but important for performance, see execution.rst. + */ + duk_instr_t **ptr_curr_pc; + + /* Backpointers. */ + duk_heap *heap; + + /* Current strictness flag: affects API calls. */ + duk_uint8_t strict; + + /* Thread state. */ + duk_uint8_t state; + duk_uint8_t unused1; + duk_uint8_t unused2; + + /* Sanity limits for stack sizes. */ + duk_size_t valstack_max; + duk_size_t callstack_max; + duk_size_t catchstack_max; + + /* XXX: Valstack, callstack, and catchstack are currently assumed + * to have non-NULL pointers. Relaxing this would not lead to big + * benefits (except perhaps for terminated threads). + */ + + /* Value stack: these are expressed as pointers for faster stack manipulation. + * [valstack,valstack_top[ is GC-reachable, [valstack_top,valstack_end[ is + * not GC-reachable but kept initialized as 'undefined'. + */ + duk_tval *valstack; /* start of valstack allocation */ + duk_tval *valstack_end; /* end of valstack allocation (exclusive) */ + duk_tval *valstack_bottom; /* bottom of current frame */ + duk_tval *valstack_top; /* top of current frame (exclusive) */ +#if !defined(DUK_USE_PREFER_SIZE) + duk_size_t valstack_size; /* cached: valstack_end - valstack (in entries, not bytes) */ +#endif + + /* Call stack. [0,callstack_top[ is GC reachable. */ + duk_activation *callstack; + duk_size_t callstack_size; /* allocation size */ + duk_size_t callstack_top; /* next to use, highest used is top - 1 */ + duk_size_t callstack_preventcount; /* number of activation records in callstack preventing a yield */ + + /* Catch stack. [0,catchstack_top[ is GC reachable. */ + duk_catcher *catchstack; + duk_size_t catchstack_size; /* allocation size */ + duk_size_t catchstack_top; /* next to use, highest used is top - 1 */ + + /* Yield/resume book-keeping. */ + duk_hthread *resumer; /* who resumed us (if any) */ + + /* Current compiler state (if any), used for augmenting SyntaxErrors. */ + duk_compiler_ctx *compile_ctx; + +#if defined(DUK_USE_INTERRUPT_COUNTER) + /* Interrupt counter for triggering a slow path check for execution + * timeout, debugger interaction such as breakpoints, etc. The value + * is valid for the current running thread, and both the init and + * counter values are copied whenever a thread switch occurs. It's + * important for the counter to be conveniently accessible for the + * bytecode executor inner loop for performance reasons. + */ + duk_int_t interrupt_counter; /* countdown state */ + duk_int_t interrupt_init; /* start value for current countdown */ +#endif + + /* Builtin-objects; may or may not be shared with other threads, + * threads existing in different "compartments" will have different + * built-ins. Must be stored on a per-thread basis because there + * is no intermediate structure for a thread group / compartment. + * This takes quite a lot of space, currently 43x4 = 172 bytes on + * 32-bit platforms. + * + * In some cases the builtins array could be ROM based, but it's + * sometimes edited (e.g. for sandboxing) so it's better to keep + * this array in RAM. + */ + duk_hobject *builtins[DUK_NUM_BUILTINS]; + + /* Convenience copies from heap/vm for faster access. */ +#if defined(DUK_USE_ROM_STRINGS) + /* No field needed when strings are in ROM. */ +#else +#if defined(DUK_USE_HEAPPTR16) + duk_uint16_t *strs16; +#else + duk_hstring **strs; +#endif +#endif +}; + +/* + * Prototypes + */ + +DUK_INTERNAL_DECL void duk_hthread_copy_builtin_objects(duk_hthread *thr_from, duk_hthread *thr_to); +DUK_INTERNAL_DECL void duk_hthread_create_builtin_objects(duk_hthread *thr); +DUK_INTERNAL_DECL duk_bool_t duk_hthread_init_stacks(duk_heap *heap, duk_hthread *thr); +DUK_INTERNAL_DECL void duk_hthread_terminate(duk_hthread *thr); + +DUK_INTERNAL_DECL void duk_hthread_callstack_grow(duk_hthread *thr); +DUK_INTERNAL_DECL void duk_hthread_callstack_shrink_check(duk_hthread *thr); +DUK_INTERNAL_DECL void duk_hthread_callstack_unwind(duk_hthread *thr, duk_size_t new_top); +DUK_INTERNAL_DECL void duk_hthread_catchstack_grow(duk_hthread *thr); +DUK_INTERNAL_DECL void duk_hthread_catchstack_shrink_check(duk_hthread *thr); +DUK_INTERNAL_DECL void duk_hthread_catchstack_unwind(duk_hthread *thr, duk_size_t new_top); + +DUK_INTERNAL_DECL duk_activation *duk_hthread_get_current_activation(duk_hthread *thr); +DUK_INTERNAL_DECL void *duk_hthread_get_valstack_ptr(duk_heap *heap, void *ud); /* indirect allocs */ +DUK_INTERNAL_DECL void *duk_hthread_get_callstack_ptr(duk_heap *heap, void *ud); /* indirect allocs */ +DUK_INTERNAL_DECL void *duk_hthread_get_catchstack_ptr(duk_heap *heap, void *ud); /* indirect allocs */ + +#if defined(DUK_USE_DEBUGGER_SUPPORT) +DUK_INTERNAL_DECL duk_uint_fast32_t duk_hthread_get_act_curr_pc(duk_hthread *thr, duk_activation *act); +#endif +DUK_INTERNAL_DECL duk_uint_fast32_t duk_hthread_get_act_prev_pc(duk_hthread *thr, duk_activation *act); +DUK_INTERNAL_DECL void duk_hthread_sync_currpc(duk_hthread *thr); +DUK_INTERNAL_DECL void duk_hthread_sync_and_null_currpc(duk_hthread *thr); + +#endif /* DUK_HTHREAD_H_INCLUDED */ +/* + * Heap buffer representation. + * + * Heap allocated user data buffer which is either: + * + * 1. A fixed size buffer (data follows header statically) + * 2. A dynamic size buffer (data pointer follows header) + * + * The data pointer for a variable size buffer of zero size may be NULL. + */ + +#ifndef DUK_HBUFFER_H_INCLUDED +#define DUK_HBUFFER_H_INCLUDED + +/* + * Flags + * + * Fixed buffer: 0 + * Dynamic buffer: DUK_HBUFFER_FLAG_DYNAMIC + * External buffer: DUK_HBUFFER_FLAG_DYNAMIC | DUK_HBUFFER_FLAG_EXTERNAL + */ + +#define DUK_HBUFFER_FLAG_DYNAMIC DUK_HEAPHDR_USER_FLAG(0) /* buffer is behind a pointer, dynamic or external */ +#define DUK_HBUFFER_FLAG_EXTERNAL DUK_HEAPHDR_USER_FLAG(1) /* buffer pointer is to an externally allocated buffer */ + +#define DUK_HBUFFER_HAS_DYNAMIC(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_DYNAMIC) +#define DUK_HBUFFER_HAS_EXTERNAL(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_EXTERNAL) + +#define DUK_HBUFFER_SET_DYNAMIC(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_DYNAMIC) +#define DUK_HBUFFER_SET_EXTERNAL(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_EXTERNAL) + +#define DUK_HBUFFER_CLEAR_DYNAMIC(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_DYNAMIC) +#define DUK_HBUFFER_CLEAR_EXTERNAL(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_EXTERNAL) + +/* + * Misc defines + */ + +/* Impose a maximum buffer length for now. Restricted artificially to + * ensure resize computations or adding a heap header length won't + * overflow size_t and that a signed duk_int_t can hold a buffer + * length. The limit should be synchronized with DUK_HSTRING_MAX_BYTELEN. + */ + +#if defined(DUK_USE_BUFLEN16) +#define DUK_HBUFFER_MAX_BYTELEN (0x0000ffffUL) +#else +/* Intentionally not 0x7fffffffUL; at least JSON code expects that + * 2*len + 2 fits in 32 bits. + */ +#define DUK_HBUFFER_MAX_BYTELEN (0x7ffffffeUL) +#endif + +/* + * Field access + */ + +/* Get/set the current user visible size, without accounting for a dynamic + * buffer's "spare" (= usable size). + */ +#if defined(DUK_USE_BUFLEN16) +/* size stored in duk_heaphdr unused flag bits */ +#define DUK_HBUFFER_GET_SIZE(x) ((x)->hdr.h_flags >> 16) +#define DUK_HBUFFER_SET_SIZE(x,v) do { \ + duk_size_t duk__v; \ + duk__v = (v); \ + DUK_ASSERT(duk__v <= 0xffffUL); \ + (x)->hdr.h_flags = ((x)->hdr.h_flags & 0x0000ffffUL) | (((duk_uint32_t) duk__v) << 16); \ + } while (0) +#define DUK_HBUFFER_ADD_SIZE(x,dv) do { \ + (x)->hdr.h_flags += ((dv) << 16); \ + } while (0) +#define DUK_HBUFFER_SUB_SIZE(x,dv) do { \ + (x)->hdr.h_flags -= ((dv) << 16); \ + } while (0) +#else +#define DUK_HBUFFER_GET_SIZE(x) (((duk_hbuffer *) (x))->size) +#define DUK_HBUFFER_SET_SIZE(x,v) do { \ + ((duk_hbuffer *) (x))->size = (v); \ + } while (0) +#define DUK_HBUFFER_ADD_SIZE(x,dv) do { \ + (x)->size += (dv); \ + } while (0) +#define DUK_HBUFFER_SUB_SIZE(x,dv) do { \ + (x)->size -= (dv); \ + } while (0) +#endif + +#define DUK_HBUFFER_FIXED_GET_SIZE(x) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) (x)) +#define DUK_HBUFFER_FIXED_SET_SIZE(x,v) DUK_HBUFFER_SET_SIZE((duk_hbuffer *) (x)) + +#define DUK_HBUFFER_DYNAMIC_GET_SIZE(x) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) (x)) +#define DUK_HBUFFER_DYNAMIC_SET_SIZE(x,v) DUK_HBUFFER_SET_SIZE((duk_hbuffer *) (x), (v)) +#define DUK_HBUFFER_DYNAMIC_ADD_SIZE(x,dv) DUK_HBUFFER_ADD_SIZE((duk_hbuffer *) (x), (dv)) +#define DUK_HBUFFER_DYNAMIC_SUB_SIZE(x,dv) DUK_HBUFFER_SUB_SIZE((duk_hbuffer *) (x), (dv)) + +#define DUK_HBUFFER_EXTERNAL_GET_SIZE(x) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) (x)) +#define DUK_HBUFFER_EXTERNAL_SET_SIZE(x,v) DUK_HBUFFER_SET_SIZE((duk_hbuffer *) (x), (v)) + +#define DUK_HBUFFER_FIXED_GET_DATA_PTR(heap,x) ((duk_uint8_t *) (((duk_hbuffer_fixed *) (x)) + 1)) + +#if defined(DUK_USE_HEAPPTR16) +#define DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap,x) \ + ((void *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, ((duk_heaphdr *) (x))->h_extra16)) +#define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(heap,x,v) do { \ + ((duk_heaphdr *) (x))->h_extra16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \ + } while (0) +#define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR_NULL(heap,x) do { \ + ((duk_heaphdr *) (x))->h_extra16 = 0; /* assume 0 <=> NULL */ \ + } while (0) +#else +#define DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap,x) ((x)->curr_alloc) +#define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(heap,x,v) do { \ + (x)->curr_alloc = (void *) (v); \ + } while (0) +#define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR_NULL(heap,x) do { \ + (x)->curr_alloc = (void *) NULL; \ + } while (0) +#endif + +/* No pointer compression because pointer is potentially outside of + * Duktape heap. + */ +#if defined(DUK_USE_HEAPPTR16) +#define DUK_HBUFFER_EXTERNAL_GET_DATA_PTR(heap,x) \ + ((void *) (x)->curr_alloc) +#define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR(heap,x,v) do { \ + (x)->curr_alloc = (void *) (v); \ + } while (0) +#define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR_NULL(heap,x) do { \ + (x)->curr_alloc = (void *) NULL; \ + } while (0) +#else +#define DUK_HBUFFER_EXTERNAL_GET_DATA_PTR(heap,x) \ + ((void *) (x)->curr_alloc) +#define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR(heap,x,v) do { \ + (x)->curr_alloc = (void *) (v); \ + } while (0) +#define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR_NULL(heap,x) do { \ + (x)->curr_alloc = (void *) NULL; \ + } while (0) +#endif + +/* Get a pointer to the current buffer contents (matching current allocation + * size). May be NULL for zero size dynamic/external buffer. + */ +#if defined(DUK_USE_HEAPPTR16) +#define DUK_HBUFFER_GET_DATA_PTR(heap,x) ( \ + DUK_HBUFFER_HAS_DYNAMIC((x)) ? \ + ( \ + DUK_HBUFFER_HAS_EXTERNAL((x)) ? \ + DUK_HBUFFER_EXTERNAL_GET_DATA_PTR((heap), (duk_hbuffer_external *) (x)) : \ + DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((heap), (duk_hbuffer_dynamic *) (x)) \ + ) : \ + DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), (duk_hbuffer_fixed *) (x)) \ + ) +#else +/* Without heap pointer compression duk_hbuffer_dynamic and duk_hbuffer_external + * have the same layout so checking for fixed vs. dynamic (or external) is enough. + */ +#define DUK_HBUFFER_GET_DATA_PTR(heap,x) ( \ + DUK_HBUFFER_HAS_DYNAMIC((x)) ? \ + DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((heap), (duk_hbuffer_dynamic *) (x)) : \ + DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), (duk_hbuffer_fixed *) (x)) \ + ) +#endif + +/* + * Structs + */ + +/* Shared prefix for all buffer types. */ +struct duk_hbuffer { + duk_heaphdr hdr; + + /* It's not strictly necessary to track the current size, but + * it is useful for writing robust native code. + */ + + /* Current size (not counting a dynamic buffer's "spare"). */ +#if defined(DUK_USE_BUFLEN16) + /* Stored in duk_heaphdr unused flags. */ +#else + duk_size_t size; +#endif + + /* + * Data following the header depends on the DUK_HBUFFER_FLAG_DYNAMIC + * flag. + * + * If the flag is clear (the buffer is a fixed size one), the buffer + * data follows the header directly, consisting of 'size' bytes. + * + * If the flag is set, the actual buffer is allocated separately, and + * a few control fields follow the header. Specifically: + * + * - a "void *" pointing to the current allocation + * - a duk_size_t indicating the full allocated size (always >= 'size') + * + * If DUK_HBUFFER_FLAG_EXTERNAL is set, the buffer has been allocated + * by user code, so that Duktape won't be able to resize it and won't + * free it. This allows buffers to point to e.g. an externally + * allocated structure such as a frame buffer. + * + * Unlike strings, no terminator byte (NUL) is guaranteed after the + * data. This would be convenient, but would pad aligned user buffers + * unnecessarily upwards in size. For instance, if user code requested + * a 64-byte dynamic buffer, 65 bytes would actually be allocated which + * would then potentially round upwards to perhaps 68 or 72 bytes. + */ +}; + +/* Fixed buffer; data follows struct, with proper alignment guaranteed by + * struct size. + */ +#if (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_MSVC_PRAGMA) +#pragma pack(push, 8) +#endif +struct duk_hbuffer_fixed { + /* A union is used here as a portable struct size / alignment trick: + * by adding a 32-bit or a 64-bit (unused) union member, the size of + * the struct is effectively forced to be a multiple of 4 or 8 bytes + * (respectively) without increasing the size of the struct unless + * necessary. + */ + union { + struct { + duk_heaphdr hdr; +#if defined(DUK_USE_BUFLEN16) + /* Stored in duk_heaphdr unused flags. */ +#else + duk_size_t size; +#endif + } s; +#if (DUK_USE_ALIGN_BY == 4) + duk_uint32_t dummy_for_align4; +#elif (DUK_USE_ALIGN_BY == 8) + duk_double_t dummy_for_align8; +#elif (DUK_USE_ALIGN_BY == 1) + /* no extra padding */ +#else +#error invalid DUK_USE_ALIGN_BY +#endif + } u; + + /* + * Data follows the struct header. The struct size is padded by the + * compiler based on the struct members. This guarantees that the + * buffer data will be aligned-by-4 but not necessarily aligned-by-8. + * + * On platforms where alignment does not matter, the struct padding + * could be removed (if there is any). On platforms where alignment + * by 8 is required, the struct size must be forced to be a multiple + * of 8 by some means. Without it, some user code may break, and also + * Duktape itself breaks (e.g. the compiler stores duk_tvals in a + * dynamic buffer). + */ +} +#if (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_GCC_ATTR) +__attribute__ ((aligned (8))) +#elif (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_CLANG_ATTR) +__attribute__ ((aligned (8))) +#endif +; +#if (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_MSVC_PRAGMA) +#pragma pack(pop) +#endif + +/* Dynamic buffer with 'curr_alloc' pointing to a dynamic area allocated using + * heap allocation primitives. Also used for external buffers when low memory + * options are not used. + */ +struct duk_hbuffer_dynamic { + duk_heaphdr hdr; + +#if defined(DUK_USE_BUFLEN16) + /* Stored in duk_heaphdr unused flags. */ +#else + duk_size_t size; +#endif + +#if defined(DUK_USE_HEAPPTR16) + /* Stored in duk_heaphdr h_extra16. */ +#else + void *curr_alloc; /* may be NULL if alloc_size == 0 */ +#endif + + /* + * Allocation size for 'curr_alloc' is alloc_size. There is no + * automatic NUL terminator for buffers (see above for rationale). + * + * 'curr_alloc' is explicitly allocated with heap allocation + * primitives and will thus always have alignment suitable for + * e.g. duk_tval and an IEEE double. + */ +}; + +/* External buffer with 'curr_alloc' managed by user code and pointing to an + * arbitrary address. When heap pointer compression is not used, this struct + * has the same layout as duk_hbuffer_dynamic. + */ +struct duk_hbuffer_external { + duk_heaphdr hdr; + +#if defined(DUK_USE_BUFLEN16) + /* Stored in duk_heaphdr unused flags. */ +#else + duk_size_t size; +#endif + + /* Cannot be compressed as a heap pointer because may point to + * an arbitrary address. + */ + void *curr_alloc; /* may be NULL if alloc_size == 0 */ +}; + +/* + * Prototypes + */ + +DUK_INTERNAL_DECL duk_hbuffer *duk_hbuffer_alloc(duk_heap *heap, duk_size_t size, duk_small_uint_t flags, void **out_bufdata); +DUK_INTERNAL_DECL void *duk_hbuffer_get_dynalloc_ptr(duk_heap *heap, void *ud); /* indirect allocs */ + +/* dynamic buffer ops */ +DUK_INTERNAL_DECL void duk_hbuffer_resize(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t new_size); +DUK_INTERNAL_DECL void duk_hbuffer_reset(duk_hthread *thr, duk_hbuffer_dynamic *buf); + +#endif /* DUK_HBUFFER_H_INCLUDED */ +/* + * Heap structure. + * + * Heap contains allocated heap objects, interned strings, and built-in + * strings for one or more threads. + */ + +#ifndef DUK_HEAP_H_INCLUDED +#define DUK_HEAP_H_INCLUDED + +/* alloc function typedefs in duktape.h */ + +/* + * Heap flags + */ + +#define DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING (1 << 0) /* mark-and-sweep is currently running */ +#define DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED (1 << 1) /* mark-and-sweep marking reached a recursion limit and must use multi-pass marking */ +#define DUK_HEAP_FLAG_REFZERO_FREE_RUNNING (1 << 2) /* refcount code is processing refzero list */ +#define DUK_HEAP_FLAG_ERRHANDLER_RUNNING (1 << 3) /* an error handler (user callback to augment/replace error) is running */ +#define DUK_HEAP_FLAG_INTERRUPT_RUNNING (1 << 4) /* executor interrupt running (used to avoid nested interrupts) */ +#define DUK_HEAP_FLAG_FINALIZER_NORESCUE (1 << 5) /* heap destruction ongoing, finalizer rescue no longer possible */ + +#define DUK__HEAP_HAS_FLAGS(heap,bits) ((heap)->flags & (bits)) +#define DUK__HEAP_SET_FLAGS(heap,bits) do { \ + (heap)->flags |= (bits); \ + } while (0) +#define DUK__HEAP_CLEAR_FLAGS(heap,bits) do { \ + (heap)->flags &= ~(bits); \ + } while (0) + +#define DUK_HEAP_HAS_MARKANDSWEEP_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING) +#define DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED) +#define DUK_HEAP_HAS_REFZERO_FREE_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING) +#define DUK_HEAP_HAS_ERRHANDLER_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING) +#define DUK_HEAP_HAS_INTERRUPT_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING) +#define DUK_HEAP_HAS_FINALIZER_NORESCUE(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_FINALIZER_NORESCUE) + +#define DUK_HEAP_SET_MARKANDSWEEP_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING) +#define DUK_HEAP_SET_MARKANDSWEEP_RECLIMIT_REACHED(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED) +#define DUK_HEAP_SET_REFZERO_FREE_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING) +#define DUK_HEAP_SET_ERRHANDLER_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING) +#define DUK_HEAP_SET_INTERRUPT_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING) +#define DUK_HEAP_SET_FINALIZER_NORESCUE(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_FINALIZER_NORESCUE) + +#define DUK_HEAP_CLEAR_MARKANDSWEEP_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RUNNING) +#define DUK_HEAP_CLEAR_MARKANDSWEEP_RECLIMIT_REACHED(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED) +#define DUK_HEAP_CLEAR_REFZERO_FREE_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_REFZERO_FREE_RUNNING) +#define DUK_HEAP_CLEAR_ERRHANDLER_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_ERRHANDLER_RUNNING) +#define DUK_HEAP_CLEAR_INTERRUPT_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING) +#define DUK_HEAP_CLEAR_FINALIZER_NORESCUE(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_FINALIZER_NORESCUE) + +/* + * Longjmp types, also double as identifying continuation type for a rethrow (in 'finally') + */ + +#define DUK_LJ_TYPE_UNKNOWN 0 /* unused */ +#define DUK_LJ_TYPE_THROW 1 /* value1 -> error object */ +#define DUK_LJ_TYPE_YIELD 2 /* value1 -> yield value, iserror -> error / normal */ +#define DUK_LJ_TYPE_RESUME 3 /* value1 -> resume value, value2 -> resumee thread, iserror -> error/normal */ +#define DUK_LJ_TYPE_BREAK 4 /* value1 -> label number, pseudo-type to indicate a break continuation (for ENDFIN) */ +#define DUK_LJ_TYPE_CONTINUE 5 /* value1 -> label number, pseudo-type to indicate a continue continuation (for ENDFIN) */ +#define DUK_LJ_TYPE_RETURN 6 /* value1 -> return value, pseudo-type to indicate a return continuation (for ENDFIN) */ +#define DUK_LJ_TYPE_NORMAL 7 /* no value, pseudo-type to indicate a normal continuation (for ENDFIN) */ + +/* + * Mark-and-sweep flags + * + * These are separate from heap level flags now but could be merged. + * The heap structure only contains a 'base mark-and-sweep flags' + * field and the GC caller can impose further flags. + */ + +#define DUK_MS_FLAG_EMERGENCY (1 << 0) /* emergency mode: try extra hard */ +#define DUK_MS_FLAG_NO_STRINGTABLE_RESIZE (1 << 1) /* don't resize stringtable (but may sweep it); needed during stringtable resize */ +#define DUK_MS_FLAG_NO_OBJECT_COMPACTION (1 << 2) /* don't compact objects; needed during object property allocation resize */ +#define DUK_MS_FLAG_NO_FINALIZERS (1 << 3) /* don't run finalizers; leave finalizable objects in finalize_list for next round */ +#define DUK_MS_FLAG_SKIP_FINALIZERS (1 << 4) /* don't run finalizers; queue finalizable objects back to heap_allocated */ + +/* + * Thread switching + * + * To switch heap->curr_thread, use the macro below so that interrupt counters + * get updated correctly. The macro allows a NULL target thread because that + * happens e.g. in call handling. + */ + +#if defined(DUK_USE_INTERRUPT_COUNTER) +#define DUK_HEAP_SWITCH_THREAD(heap,newthr) duk_heap_switch_thread((heap), (newthr)) +#else +#define DUK_HEAP_SWITCH_THREAD(heap,newthr) do { \ + (heap)->curr_thread = (newthr); \ + } while (0) +#endif + +/* + * Other heap related defines + */ + +/* Mark-and-sweep interval is relative to combined count of objects and + * strings kept in the heap during the latest mark-and-sweep pass. + * Fixed point .8 multiplier and .0 adder. Trigger count (interval) is + * decreased by each (re)allocation attempt (regardless of size), and each + * refzero processed object. + * + * 'SKIP' indicates how many (re)allocations to wait until a retry if + * GC is skipped because there is no thread do it with yet (happens + * only during init phases). + */ +#if defined(DUK_USE_MARK_AND_SWEEP) +#if defined(DUK_USE_REFERENCE_COUNTING) +#define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_MULT 12800L /* 50x heap size */ +#define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_ADD 1024L +#define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_SKIP 256L +#else +#define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_MULT 256L /* 1x heap size */ +#define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_ADD 1024L +#define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_SKIP 256L +#endif +#endif + +/* Stringcache is used for speeding up char-offset-to-byte-offset + * translations for non-ASCII strings. + */ +#define DUK_HEAP_STRCACHE_SIZE 4 +#define DUK_HEAP_STRINGCACHE_NOCACHE_LIMIT 16 /* strings up to the this length are not cached */ + +/* helper to insert a (non-string) heap object into heap allocated list */ +#define DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap,hdr) duk_heap_insert_into_heap_allocated((heap),(hdr)) + +/* + * Stringtable + */ + +/* initial stringtable size, must be prime and higher than DUK_UTIL_MIN_HASH_PRIME */ +#define DUK_STRTAB_INITIAL_SIZE 17 + +/* indicates a deleted string; any fixed non-NULL, non-hstring pointer works */ +#define DUK_STRTAB_DELETED_MARKER(heap) ((duk_hstring *) heap) + +/* resizing parameters */ +#define DUK_STRTAB_MIN_FREE_DIVISOR 4 /* load factor max 75% */ +#define DUK_STRTAB_MIN_USED_DIVISOR 4 /* load factor min 25% */ +#define DUK_STRTAB_GROW_ST_SIZE(n) ((n) + (n)) /* used entries + approx 100% -> reset load to 50% */ + +#define DUK_STRTAB_U32_MAX_STRLEN 10 /* 4'294'967'295 */ +#define DUK_STRTAB_HIGHEST_32BIT_PRIME 0xfffffffbUL + +/* probe sequence (open addressing) */ +#define DUK_STRTAB_HASH_INITIAL(hash,h_size) ((hash) % (h_size)) +#define DUK_STRTAB_HASH_PROBE_STEP(hash) DUK_UTIL_GET_HASH_PROBE_STEP((hash)) + +/* fixed top level hashtable size (separate chaining) */ +#define DUK_STRTAB_CHAIN_SIZE DUK_USE_STRTAB_CHAIN_SIZE + +/* + * Built-in strings + */ + +/* heap string indices are autogenerated in duk_strings.h */ +#if defined(DUK_USE_ROM_STRINGS) +#define DUK_HEAP_GET_STRING(heap,idx) \ + ((duk_hstring *) DUK_LOSE_CONST(duk_rom_strings_stridx[(idx)])) +#else /* DUK_USE_ROM_STRINGS */ +#if defined(DUK_USE_HEAPPTR16) +#define DUK_HEAP_GET_STRING(heap,idx) \ + ((duk_hstring *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (heap)->strs16[(idx)])) +#else +#define DUK_HEAP_GET_STRING(heap,idx) \ + ((heap)->strs[(idx)]) +#endif +#endif /* DUK_USE_ROM_STRINGS */ + +/* + * Raw memory calls: relative to heap, but no GC interaction + */ + +#define DUK_ALLOC_RAW(heap,size) \ + ((heap)->alloc_func((heap)->heap_udata, (size))) + +#define DUK_REALLOC_RAW(heap,ptr,newsize) \ + ((heap)->realloc_func((heap)->heap_udata, (void *) (ptr), (newsize))) + +#define DUK_FREE_RAW(heap,ptr) \ + ((heap)->free_func((heap)->heap_udata, (void *) (ptr))) + +/* + * Memory calls: relative to heap, GC interaction, but no error throwing. + * + * XXX: Currently a mark-and-sweep triggered by memory allocation will run + * using the heap->heap_thread. This thread is also used for running + * mark-and-sweep finalization; this is not ideal because it breaks the + * isolation between multiple global environments. + * + * Notes: + * + * - DUK_FREE() is required to ignore NULL and any other possible return + * value of a zero-sized alloc/realloc (same as ANSI C free()). + * + * - There is no DUK_REALLOC_ZEROED because we don't assume to know the + * old size. Caller must zero the reallocated memory. + * + * - DUK_REALLOC_INDIRECT() must be used when a mark-and-sweep triggered + * by an allocation failure might invalidate the original 'ptr', thus + * causing a realloc retry to use an invalid pointer. Example: we're + * reallocating the value stack and a finalizer resizes the same value + * stack during mark-and-sweep. The indirect variant requests for the + * current location of the pointer being reallocated using a callback + * right before every realloc attempt; this circuitous approach is used + * to avoid strict aliasing issues in a more straightforward indirect + * pointer (void **) approach. Note: the pointer in the storage + * location is read but is NOT updated; the caller must do that. + */ + +/* callback for indirect reallocs, request for current pointer */ +typedef void *(*duk_mem_getptr)(duk_heap *heap, void *ud); + +#define DUK_ALLOC(heap,size) duk_heap_mem_alloc((heap), (size)) +#define DUK_ALLOC_ZEROED(heap,size) duk_heap_mem_alloc_zeroed((heap), (size)) +#define DUK_REALLOC(heap,ptr,newsize) duk_heap_mem_realloc((heap), (ptr), (newsize)) +#define DUK_REALLOC_INDIRECT(heap,cb,ud,newsize) duk_heap_mem_realloc_indirect((heap), (cb), (ud), (newsize)) +#define DUK_FREE(heap,ptr) duk_heap_mem_free((heap), (ptr)) + +/* + * Memory constants + */ + +#define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT 5 /* Retry allocation after mark-and-sweep for this + * many times. A single mark-and-sweep round is + * not guaranteed to free all unreferenced memory + * because of finalization (in fact, ANY number of + * rounds is strictly not enough). + */ + +#define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT 3 /* Starting from this round, use emergency mode + * for mark-and-sweep. + */ + +/* + * Debugger support + */ + +/* Maximum number of breakpoints. Only breakpoints that are set are + * consulted so increasing this has no performance impact. + */ +#define DUK_HEAP_MAX_BREAKPOINTS 16 + +/* Opcode interval for a Date-based status/peek rate limit check. Only + * relevant when debugger is attached. Requesting a timestamp may be a + * slow operation on some platforms so this shouldn't be too low. On the + * other hand a high value makes Duktape react to a pause request slowly. + */ +#define DUK_HEAP_DBG_RATELIMIT_OPCODES 4000 + +/* Milliseconds between status notify and transport peeks. */ +#define DUK_HEAP_DBG_RATELIMIT_MILLISECS 200 + +/* Step types */ +#define DUK_STEP_TYPE_NONE 0 +#define DUK_STEP_TYPE_INTO 1 +#define DUK_STEP_TYPE_OVER 2 +#define DUK_STEP_TYPE_OUT 3 + +struct duk_breakpoint { + duk_hstring *filename; + duk_uint32_t line; +}; + +#if defined(DUK_USE_DEBUGGER_SUPPORT) +#define DUK_HEAP_IS_DEBUGGER_ATTACHED(heap) ((heap)->dbg_read_cb != NULL) +#define DUK_HEAP_CLEAR_STEP_STATE(heap) do { \ + (heap)->dbg_step_type = DUK_STEP_TYPE_NONE; \ + (heap)->dbg_step_thread = NULL; \ + (heap)->dbg_step_csindex = 0; \ + (heap)->dbg_step_startline = 0; \ + } while (0) +#define DUK_HEAP_SET_PAUSED(heap) do { \ + (heap)->dbg_paused = 1; \ + (heap)->dbg_state_dirty = 1; \ + DUK_HEAP_CLEAR_STEP_STATE((heap)); \ + } while (0) +#define DUK_HEAP_CLEAR_PAUSED(heap) do { \ + (heap)->dbg_paused = 0; \ + (heap)->dbg_state_dirty = 1; \ + DUK_HEAP_CLEAR_STEP_STATE((heap)); \ + } while (0) +#define DUK_HEAP_IS_PAUSED(heap) ((heap)->dbg_paused) +#endif /* DUK_USE_DEBUGGER_SUPPORT */ + +/* + * String cache should ideally be at duk_hthread level, but that would + * cause string finalization to slow down relative to the number of + * threads; string finalization must check the string cache for "weak" + * references to the string being finalized to avoid dead pointers. + * + * Thus, string caches are now at the heap level now. + */ + +struct duk_strcache { + duk_hstring *h; + duk_uint32_t bidx; + duk_uint32_t cidx; +}; + +/* + * Longjmp state, contains the information needed to perform a longjmp. + * Longjmp related values are written to value1, value2, and iserror. + */ + +struct duk_ljstate { + duk_jmpbuf *jmpbuf_ptr; /* current setjmp() catchpoint */ + duk_small_uint_t type; /* longjmp type */ + duk_bool_t iserror; /* isError flag for yield */ + duk_tval value1; /* 1st related value (type specific) */ + duk_tval value2; /* 2nd related value (type specific) */ +}; + +/* + * Stringtable entry for fixed size stringtable + */ + +struct duk_strtab_entry { +#if defined(DUK_USE_HEAPPTR16) + /* A 16-bit listlen makes sense with 16-bit heap pointers: there + * won't be space for 64k strings anyway. + */ + duk_uint16_t listlen; /* if 0, 'str16' used, if > 0, 'strlist16' used */ + union { + duk_uint16_t strlist16; + duk_uint16_t str16; + } u; +#else + duk_size_t listlen; /* if 0, 'str' used, if > 0, 'strlist' used */ + union { + duk_hstring **strlist; + duk_hstring *str; + } u; +#endif +}; + +/* + * Main heap structure + */ + +struct duk_heap { + duk_small_uint_t flags; + + /* Allocator functions. */ + duk_alloc_function alloc_func; + duk_realloc_function realloc_func; + duk_free_function free_func; + + /* Heap udata, used for allocator functions but also for other heap + * level callbacks like pointer compression, etc. + */ + void *heap_udata; + + /* Precomputed pointers when using 16-bit heap pointer packing. */ +#if defined(DUK_USE_HEAPPTR16) + duk_uint16_t heapptr_null16; + duk_uint16_t heapptr_deleted16; +#endif + + /* Fatal error handling, called e.g. when a longjmp() is needed but + * lj.jmpbuf_ptr is NULL. fatal_func must never return; it's not + * declared as "noreturn" because doing that for typedefs is a bit + * challenging portability-wise. + */ + duk_fatal_function fatal_func; + + /* allocated heap objects */ + duk_heaphdr *heap_allocated; + + /* work list for objects whose refcounts are zero but which have not been + * "finalized"; avoids recursive C calls when refcounts go to zero in a + * chain of objects. + */ +#if defined(DUK_USE_REFERENCE_COUNTING) + duk_heaphdr *refzero_list; + duk_heaphdr *refzero_list_tail; +#endif + +#if defined(DUK_USE_MARK_AND_SWEEP) + /* mark-and-sweep control */ +#if defined(DUK_USE_VOLUNTARY_GC) + duk_int_t mark_and_sweep_trigger_counter; +#endif + duk_int_t mark_and_sweep_recursion_depth; + + /* mark-and-sweep flags automatically active (used for critical sections) */ + duk_small_uint_t mark_and_sweep_base_flags; + + /* work list for objects to be finalized (by mark-and-sweep) */ + duk_heaphdr *finalize_list; +#endif + + /* longjmp state */ + duk_ljstate lj; + + /* marker for detecting internal "double faults", see duk_error_throw.c */ + duk_bool_t handling_error; + + /* heap thread, used internally and for finalization */ + duk_hthread *heap_thread; + + /* current thread */ + duk_hthread *curr_thread; /* currently running thread */ + + /* heap level "stash" object (e.g., various reachability roots) */ + duk_hobject *heap_object; + + /* duk_handle_call / duk_handle_safe_call recursion depth limiting */ + duk_int_t call_recursion_depth; + duk_int_t call_recursion_limit; + + /* mix-in value for computing string hashes; should be reasonably unpredictable */ + duk_uint32_t hash_seed; + + /* rnd_state for duk_util_tinyrandom.c */ + duk_uint32_t rnd_state; + + /* For manual debugging: instruction count based on executor and + * interrupt counter book-keeping. Inspect debug logs to see how + * they match up. + */ +#if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG) + duk_int_t inst_count_exec; + duk_int_t inst_count_interrupt; +#endif + + /* debugger */ + +#if defined(DUK_USE_DEBUGGER_SUPPORT) + /* callbacks and udata; dbg_read_cb != NULL is used to indicate attached state */ + duk_debug_read_function dbg_read_cb; /* required, NULL implies detached */ + duk_debug_write_function dbg_write_cb; /* required */ + duk_debug_peek_function dbg_peek_cb; + duk_debug_read_flush_function dbg_read_flush_cb; + duk_debug_write_flush_function dbg_write_flush_cb; + duk_debug_request_function dbg_request_cb; + duk_debug_detached_function dbg_detached_cb; + void *dbg_udata; + + /* debugger state, only relevant when attached */ + duk_bool_t dbg_processing; /* currently processing messages or breakpoints: don't enter message processing recursively (e.g. no breakpoints when processing debugger eval) */ + duk_bool_t dbg_paused; /* currently paused: talk with debug client until step/resume */ + duk_bool_t dbg_state_dirty; /* resend state next time executor is about to run */ + duk_bool_t dbg_force_restart; /* force executor restart to recheck breakpoints; used to handle function returns (see GH-303) */ + duk_bool_t dbg_detaching; /* debugger detaching; used to avoid calling detach handler recursively */ + duk_small_uint_t dbg_step_type; /* step type: none, step into, step over, step out */ + duk_hthread *dbg_step_thread; /* borrowed; NULL if no step state (NULLed in unwind) */ + duk_size_t dbg_step_csindex; /* callstack index */ + duk_uint32_t dbg_step_startline; /* starting line number */ + duk_breakpoint dbg_breakpoints[DUK_HEAP_MAX_BREAKPOINTS]; /* breakpoints: [0,breakpoint_count[ gc reachable */ + duk_small_uint_t dbg_breakpoint_count; + duk_breakpoint *dbg_breakpoints_active[DUK_HEAP_MAX_BREAKPOINTS + 1]; /* currently active breakpoints: NULL term, borrowed pointers */ + /* XXX: make active breakpoints actual copies instead of pointers? */ + + /* These are for rate limiting Status notifications and transport peeking. */ + duk_uint32_t dbg_exec_counter; /* cumulative opcode execution count (overflows are OK) */ + duk_uint32_t dbg_last_counter; /* value of dbg_exec_counter when we last did a Date-based check */ + duk_double_t dbg_last_time; /* time when status/peek was last done (Date-based rate limit) */ + + /* Used to support single-byte stream lookahead. */ + duk_bool_t dbg_have_next_byte; + duk_uint8_t dbg_next_byte; +#endif + + /* string intern table (weak refs) */ +#if defined(DUK_USE_STRTAB_PROBE) +#if defined(DUK_USE_HEAPPTR16) + duk_uint16_t *strtable16; +#else + duk_hstring **strtable; +#endif + duk_uint32_t st_size; /* alloc size in elements */ + duk_uint32_t st_used; /* used elements (includes DELETED) */ +#endif + + /* XXX: static alloc is OK until separate chaining stringtable + * resizing is implemented. + */ +#if defined(DUK_USE_STRTAB_CHAIN) + duk_strtab_entry strtable[DUK_STRTAB_CHAIN_SIZE]; +#endif + + /* string access cache (codepoint offset -> byte offset) for fast string + * character looping; 'weak' reference which needs special handling in GC. + */ + duk_strcache strcache[DUK_HEAP_STRCACHE_SIZE]; + + /* built-in strings */ +#if defined(DUK_USE_ROM_STRINGS) + /* No field needed when strings are in ROM. */ +#else +#if defined(DUK_USE_HEAPPTR16) + duk_uint16_t strs16[DUK_HEAP_NUM_STRINGS]; +#else + duk_hstring *strs[DUK_HEAP_NUM_STRINGS]; +#endif +#endif +}; + +/* + * Prototypes + */ + +DUK_INTERNAL_DECL +duk_heap *duk_heap_alloc(duk_alloc_function alloc_func, + duk_realloc_function realloc_func, + duk_free_function free_func, + void *heap_udata, + duk_fatal_function fatal_func); +DUK_INTERNAL_DECL void duk_heap_free(duk_heap *heap); +DUK_INTERNAL_DECL void duk_free_hobject_inner(duk_heap *heap, duk_hobject *h); +DUK_INTERNAL_DECL void duk_free_hbuffer_inner(duk_heap *heap, duk_hbuffer *h); +DUK_INTERNAL_DECL void duk_free_hstring_inner(duk_heap *heap, duk_hstring *h); +DUK_INTERNAL_DECL void duk_heap_free_heaphdr_raw(duk_heap *heap, duk_heaphdr *hdr); + +DUK_INTERNAL_DECL void duk_heap_insert_into_heap_allocated(duk_heap *heap, duk_heaphdr *hdr); +#if defined(DUK_USE_DOUBLE_LINKED_HEAP) && defined(DUK_USE_REFERENCE_COUNTING) +DUK_INTERNAL_DECL void duk_heap_remove_any_from_heap_allocated(duk_heap *heap, duk_heaphdr *hdr); +#endif +#if defined(DUK_USE_INTERRUPT_COUNTER) +DUK_INTERNAL_DECL void duk_heap_switch_thread(duk_heap *heap, duk_hthread *new_thr); +#endif + +#if 0 /*unused*/ +DUK_INTERNAL_DECL duk_hstring *duk_heap_string_lookup(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen); +#endif +DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen); +DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern_checked(duk_hthread *thr, const duk_uint8_t *str, duk_uint32_t len); +#if 0 /*unused*/ +DUK_INTERNAL_DECL duk_hstring *duk_heap_string_lookup_u32(duk_heap *heap, duk_uint32_t val); +#endif +DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern_u32(duk_heap *heap, duk_uint32_t val); +DUK_INTERNAL_DECL duk_hstring *duk_heap_string_intern_u32_checked(duk_hthread *thr, duk_uint32_t val); +#if defined(DUK_USE_REFERENCE_COUNTING) +DUK_INTERNAL_DECL void duk_heap_string_remove(duk_heap *heap, duk_hstring *h); +#endif +#if defined(DUK_USE_MARK_AND_SWEEP) && defined(DUK_USE_MS_STRINGTABLE_RESIZE) +DUK_INTERNAL_DECL void duk_heap_force_strtab_resize(duk_heap *heap); +#endif +DUK_INTERNAL void duk_heap_free_strtab(duk_heap *heap); +#if defined(DUK_USE_DEBUG) +DUK_INTERNAL void duk_heap_dump_strtab(duk_heap *heap); +#endif + + +DUK_INTERNAL_DECL void duk_heap_strcache_string_remove(duk_heap *heap, duk_hstring *h); +DUK_INTERNAL_DECL duk_uint_fast32_t duk_heap_strcache_offset_char2byte(duk_hthread *thr, duk_hstring *h, duk_uint_fast32_t char_offset); + +#if defined(DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS) +DUK_INTERNAL_DECL void *duk_default_alloc_function(void *udata, duk_size_t size); +DUK_INTERNAL_DECL void *duk_default_realloc_function(void *udata, void *ptr, duk_size_t newsize); +DUK_INTERNAL_DECL void duk_default_free_function(void *udata, void *ptr); +#endif + +DUK_INTERNAL_DECL void *duk_heap_mem_alloc(duk_heap *heap, duk_size_t size); +DUK_INTERNAL_DECL void *duk_heap_mem_alloc_zeroed(duk_heap *heap, duk_size_t size); +DUK_INTERNAL_DECL void *duk_heap_mem_realloc(duk_heap *heap, void *ptr, duk_size_t newsize); +DUK_INTERNAL_DECL void *duk_heap_mem_realloc_indirect(duk_heap *heap, duk_mem_getptr cb, void *ud, duk_size_t newsize); +DUK_INTERNAL_DECL void duk_heap_mem_free(duk_heap *heap, void *ptr); + +#ifdef DUK_USE_REFERENCE_COUNTING +#if !defined(DUK_USE_FAST_REFCOUNT_DEFAULT) +DUK_INTERNAL_DECL void duk_tval_incref(duk_tval *tv); +#endif +#if 0 /* unused */ +DUK_INTERNAL_DECL void duk_tval_incref_allownull(duk_tval *tv); +#endif +DUK_INTERNAL_DECL void duk_tval_decref(duk_hthread *thr, duk_tval *tv); +#if 0 /* unused */ +DUK_INTERNAL_DECL void duk_tval_decref_allownull(duk_hthread *thr, duk_tval *tv); +#endif +#if !defined(DUK_USE_FAST_REFCOUNT_DEFAULT) +DUK_INTERNAL_DECL void duk_heaphdr_incref(duk_heaphdr *h); +#endif +#if 0 /* unused */ +DUK_INTERNAL_DECL void duk_heaphdr_incref_allownull(duk_heaphdr *h); +#endif +DUK_INTERNAL_DECL void duk_heaphdr_decref(duk_hthread *thr, duk_heaphdr *h); +DUK_INTERNAL_DECL void duk_heaphdr_decref_allownull(duk_hthread *thr, duk_heaphdr *h); +DUK_INTERNAL_DECL void duk_heaphdr_refzero(duk_hthread *thr, duk_heaphdr *h); +DUK_INTERNAL_DECL void duk_heaphdr_refcount_finalize(duk_hthread *thr, duk_heaphdr *hdr); +#else +/* no refcounting */ +#endif + +#if defined(DUK_USE_MARK_AND_SWEEP) +DUK_INTERNAL_DECL duk_bool_t duk_heap_mark_and_sweep(duk_heap *heap, duk_small_uint_t flags); +#endif + +DUK_INTERNAL_DECL duk_uint32_t duk_heap_hashstring(duk_heap *heap, const duk_uint8_t *str, duk_size_t len); + +#endif /* DUK_HEAP_H_INCLUDED */ +#ifndef DUK_DEBUGGER_H_INCLUDED +#define DUK_DEBUGGER_H_INCLUDED + +/* Debugger protocol version is defined in the public API header. */ + +/* Initial bytes for markers. */ +#define DUK_DBG_IB_EOM 0x00 +#define DUK_DBG_IB_REQUEST 0x01 +#define DUK_DBG_IB_REPLY 0x02 +#define DUK_DBG_IB_ERROR 0x03 +#define DUK_DBG_IB_NOTIFY 0x04 + +/* Other initial bytes. */ +#define DUK_DBG_IB_INT4 0x10 +#define DUK_DBG_IB_STR4 0x11 +#define DUK_DBG_IB_STR2 0x12 +#define DUK_DBG_IB_BUF4 0x13 +#define DUK_DBG_IB_BUF2 0x14 +#define DUK_DBG_IB_UNUSED 0x15 +#define DUK_DBG_IB_UNDEFINED 0x16 +#define DUK_DBG_IB_NULL 0x17 +#define DUK_DBG_IB_TRUE 0x18 +#define DUK_DBG_IB_FALSE 0x19 +#define DUK_DBG_IB_NUMBER 0x1a +#define DUK_DBG_IB_OBJECT 0x1b +#define DUK_DBG_IB_POINTER 0x1c +#define DUK_DBG_IB_LIGHTFUNC 0x1d +#define DUK_DBG_IB_HEAPPTR 0x1e +/* The short string/integer initial bytes starting from 0x60 don't have + * defines now. + */ + +/* Error codes. */ +#define DUK_DBG_ERR_UNKNOWN 0x00 +#define DUK_DBG_ERR_UNSUPPORTED 0x01 +#define DUK_DBG_ERR_TOOMANY 0x02 +#define DUK_DBG_ERR_NOTFOUND 0x03 +#define DUK_DBG_ERR_APPLICATION 0x04 + +/* Commands and notifys initiated by Duktape. */ +#define DUK_DBG_CMD_STATUS 0x01 +#define DUK_DBG_CMD_PRINT 0x02 +#define DUK_DBG_CMD_ALERT 0x03 +#define DUK_DBG_CMD_LOG 0x04 +#define DUK_DBG_CMD_THROW 0x05 +#define DUK_DBG_CMD_DETACHING 0x06 +#define DUK_DBG_CMD_APPNOTIFY 0x07 + +/* Commands initiated by debug client. */ +#define DUK_DBG_CMD_BASICINFO 0x10 +#define DUK_DBG_CMD_TRIGGERSTATUS 0x11 +#define DUK_DBG_CMD_PAUSE 0x12 +#define DUK_DBG_CMD_RESUME 0x13 +#define DUK_DBG_CMD_STEPINTO 0x14 +#define DUK_DBG_CMD_STEPOVER 0x15 +#define DUK_DBG_CMD_STEPOUT 0x16 +#define DUK_DBG_CMD_LISTBREAK 0x17 +#define DUK_DBG_CMD_ADDBREAK 0x18 +#define DUK_DBG_CMD_DELBREAK 0x19 +#define DUK_DBG_CMD_GETVAR 0x1a +#define DUK_DBG_CMD_PUTVAR 0x1b +#define DUK_DBG_CMD_GETCALLSTACK 0x1c +#define DUK_DBG_CMD_GETLOCALS 0x1d +#define DUK_DBG_CMD_EVAL 0x1e +#define DUK_DBG_CMD_DETACH 0x1f +#define DUK_DBG_CMD_DUMPHEAP 0x20 +#define DUK_DBG_CMD_GETBYTECODE 0x21 +#define DUK_DBG_CMD_APPREQUEST 0x22 +#define DUK_DBG_CMD_GETHEAPOBJINFO 0x23 +#define DUK_DBG_CMD_GETOBJPROPDESC 0x24 +#define DUK_DBG_CMD_GETOBJPROPDESCRANGE 0x25 + +/* The low 8 bits map directly to duk_hobject.h DUK_PROPDESC_FLAG_xxx. + * The remaining flags are specific to the debugger. + */ +#define DUK_DBG_PROPFLAG_INTERNAL (1 << 8) + +#if defined(DUK_USE_DEBUGGER_SUPPORT) +DUK_INTERNAL_DECL void duk_debug_do_detach(duk_heap *heap); + +DUK_INTERNAL_DECL duk_bool_t duk_debug_read_peek(duk_hthread *thr); +DUK_INTERNAL_DECL void duk_debug_write_flush(duk_hthread *thr); + +DUK_INTERNAL_DECL void duk_debug_skip_bytes(duk_hthread *thr, duk_size_t length); +DUK_INTERNAL_DECL void duk_debug_skip_byte(duk_hthread *thr); + +DUK_INTERNAL_DECL void duk_debug_read_bytes(duk_hthread *thr, duk_uint8_t *data, duk_size_t length); +DUK_INTERNAL_DECL duk_uint8_t duk_debug_read_byte(duk_hthread *thr); +DUK_INTERNAL_DECL duk_int32_t duk_debug_read_int(duk_hthread *thr); +DUK_INTERNAL_DECL duk_hstring *duk_debug_read_hstring(duk_hthread *thr); +/* XXX: exposed duk_debug_read_pointer */ +/* XXX: exposed duk_debug_read_buffer */ +/* XXX: exposed duk_debug_read_hbuffer */ +#if 0 +DUK_INTERNAL_DECL duk_heaphdr *duk_debug_read_heapptr(duk_hthread *thr); +#endif +#if defined(DUK_USE_DEBUGGER_INSPECT) +DUK_INTERNAL_DECL duk_heaphdr *duk_debug_read_any_ptr(duk_hthread *thr); +#endif +DUK_INTERNAL_DECL duk_tval *duk_debug_read_tval(duk_hthread *thr); + +DUK_INTERNAL_DECL void duk_debug_write_bytes(duk_hthread *thr, const duk_uint8_t *data, duk_size_t length); +DUK_INTERNAL_DECL void duk_debug_write_byte(duk_hthread *thr, duk_uint8_t x); +DUK_INTERNAL_DECL void duk_debug_write_unused(duk_hthread *thr); +DUK_INTERNAL_DECL void duk_debug_write_undefined(duk_hthread *thr); +#if defined(DUK_USE_DEBUGGER_INSPECT) +DUK_INTERNAL_DECL void duk_debug_write_null(duk_hthread *thr); +#endif +DUK_INTERNAL_DECL void duk_debug_write_boolean(duk_hthread *thr, duk_uint_t val); +DUK_INTERNAL_DECL void duk_debug_write_int(duk_hthread *thr, duk_int32_t x); +DUK_INTERNAL_DECL void duk_debug_write_uint(duk_hthread *thr, duk_uint32_t x); +DUK_INTERNAL_DECL void duk_debug_write_string(duk_hthread *thr, const char *data, duk_size_t length); +DUK_INTERNAL_DECL void duk_debug_write_cstring(duk_hthread *thr, const char *data); +DUK_INTERNAL_DECL void duk_debug_write_hstring(duk_hthread *thr, duk_hstring *h); +DUK_INTERNAL_DECL void duk_debug_write_buffer(duk_hthread *thr, const char *data, duk_size_t length); +DUK_INTERNAL_DECL void duk_debug_write_hbuffer(duk_hthread *thr, duk_hbuffer *h); +DUK_INTERNAL_DECL void duk_debug_write_pointer(duk_hthread *thr, void *ptr); +#if defined(DUK_USE_DEBUGGER_DUMPHEAP) || defined(DUK_USE_DEBUGGER_INSPECT) +DUK_INTERNAL_DECL void duk_debug_write_heapptr(duk_hthread *thr, duk_heaphdr *h); +#endif +DUK_INTERNAL_DECL void duk_debug_write_hobject(duk_hthread *thr, duk_hobject *obj); +DUK_INTERNAL_DECL void duk_debug_write_tval(duk_hthread *thr, duk_tval *tv); +#if 0 /* unused */ +DUK_INTERNAL_DECL void duk_debug_write_request(duk_hthread *thr, duk_small_uint_t command); +#endif +DUK_INTERNAL_DECL void duk_debug_write_reply(duk_hthread *thr); +DUK_INTERNAL_DECL void duk_debug_write_error_eom(duk_hthread *thr, duk_small_uint_t err_code, const char *msg); +DUK_INTERNAL_DECL void duk_debug_write_notify(duk_hthread *thr, duk_small_uint_t command); +DUK_INTERNAL_DECL void duk_debug_write_eom(duk_hthread *thr); + +DUK_INTERNAL_DECL duk_uint_fast32_t duk_debug_curr_line(duk_hthread *thr); +DUK_INTERNAL_DECL void duk_debug_send_status(duk_hthread *thr); +#if defined(DUK_USE_DEBUGGER_THROW_NOTIFY) +DUK_INTERNAL_DECL void duk_debug_send_throw(duk_hthread *thr, duk_bool_t fatal); +#endif + +DUK_INTERNAL_DECL void duk_debug_halt_execution(duk_hthread *thr, duk_bool_t use_prev_pc); +DUK_INTERNAL_DECL duk_bool_t duk_debug_process_messages(duk_hthread *thr, duk_bool_t no_block); + +DUK_INTERNAL_DECL duk_small_int_t duk_debug_add_breakpoint(duk_hthread *thr, duk_hstring *filename, duk_uint32_t line); +DUK_INTERNAL_DECL duk_bool_t duk_debug_remove_breakpoint(duk_hthread *thr, duk_small_uint_t breakpoint_index); +#endif + +#endif /* DUK_DEBUGGER_H_INCLUDED */ +/* + * Debugging macros, DUK_DPRINT() and its variants in particular. + * + * DUK_DPRINT() allows formatted debug prints, and supports standard + * and Duktape specific formatters. See duk_debug_vsnprintf.c for details. + * + * DUK_D(x), DUK_DD(x), and DUK_DDD(x) are used together with log macros + * for technical reasons. They are concretely used to hide 'x' from the + * compiler when the corresponding log level is disabled. This allows + * clean builds on non-C99 compilers, at the cost of more verbose code. + * Examples: + * + * DUK_D(DUK_DPRINT("foo")); + * DUK_DD(DUK_DDPRINT("foo")); + * DUK_DDD(DUK_DDDPRINT("foo")); + * + * This approach is preferable to the old "double parentheses" hack because + * double parentheses make the C99 solution worse: __FILE__ and __LINE__ can + * no longer be added transparently without going through globals, which + * works poorly with threading. + */ + +#ifndef DUK_DEBUG_H_INCLUDED +#define DUK_DEBUG_H_INCLUDED + +#ifdef DUK_USE_DEBUG + +#if defined(DUK_USE_DPRINT) +#define DUK_D(x) x +#else +#define DUK_D(x) do { } while (0) /* omit */ +#endif + +#if defined(DUK_USE_DDPRINT) +#define DUK_DD(x) x +#else +#define DUK_DD(x) do { } while (0) /* omit */ +#endif + +#if defined(DUK_USE_DDDPRINT) +#define DUK_DDD(x) x +#else +#define DUK_DDD(x) do { } while (0) /* omit */ +#endif + +/* + * Exposed debug macros: debugging enabled + */ + +#define DUK_LEVEL_DEBUG 1 +#define DUK_LEVEL_DDEBUG 2 +#define DUK_LEVEL_DDDEBUG 3 + +#ifdef DUK_USE_VARIADIC_MACROS + +/* Note: combining __FILE__, __LINE__, and __func__ into fmt would be + * possible compile time, but waste some space with shared function names. + */ +#define DUK__DEBUG_LOG(lev,...) duk_debug_log((duk_small_int_t) (lev), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, DUK_FUNC_MACRO, __VA_ARGS__); + +#define DUK_DPRINT(...) DUK__DEBUG_LOG(DUK_LEVEL_DEBUG, __VA_ARGS__) + +#ifdef DUK_USE_DDPRINT +#define DUK_DDPRINT(...) DUK__DEBUG_LOG(DUK_LEVEL_DDEBUG, __VA_ARGS__) +#else +#define DUK_DDPRINT(...) +#endif + +#ifdef DUK_USE_DDDPRINT +#define DUK_DDDPRINT(...) DUK__DEBUG_LOG(DUK_LEVEL_DDDEBUG, __VA_ARGS__) +#else +#define DUK_DDDPRINT(...) +#endif + +#else /* DUK_USE_VARIADIC_MACROS */ + +#define DUK__DEBUG_STASH(lev) \ + (void) DUK_SNPRINTF(duk_debug_file_stash, DUK_DEBUG_STASH_SIZE, "%s", (const char *) DUK_FILE_MACRO), \ + duk_debug_file_stash[DUK_DEBUG_STASH_SIZE - 1] = (char) 0; \ + (void) DUK_SNPRINTF(duk_debug_line_stash, DUK_DEBUG_STASH_SIZE, "%ld", (long) DUK_LINE_MACRO), \ + duk_debug_line_stash[DUK_DEBUG_STASH_SIZE - 1] = (char) 0; \ + (void) DUK_SNPRINTF(duk_debug_func_stash, DUK_DEBUG_STASH_SIZE, "%s", (const char *) DUK_FUNC_MACRO), \ + duk_debug_func_stash[DUK_DEBUG_STASH_SIZE - 1] = (char) 0; \ + (void) (duk_debug_level_stash = (lev)) + +/* Without variadic macros resort to comma expression trickery to handle debug + * prints. This generates a lot of harmless warnings. These hacks are not + * needed normally because DUK_D() and friends will hide the entire debug log + * statement from the compiler. + */ + +#ifdef DUK_USE_DPRINT +#define DUK_DPRINT DUK__DEBUG_STASH(DUK_LEVEL_DEBUG), (void) duk_debug_log /* args go here in parens */ +#else +#define DUK_DPRINT 0 && /* args go here as a comma expression in parens */ +#endif + +#ifdef DUK_USE_DDPRINT +#define DUK_DDPRINT DUK__DEBUG_STASH(DUK_LEVEL_DDEBUG), (void) duk_debug_log /* args go here in parens */ +#else +#define DUK_DDPRINT 0 && /* args */ +#endif + +#ifdef DUK_USE_DDDPRINT +#define DUK_DDDPRINT DUK__DEBUG_STASH(DUK_LEVEL_DDDEBUG), (void) duk_debug_log /* args go here in parens */ +#else +#define DUK_DDDPRINT 0 && /* args */ +#endif + +#endif /* DUK_USE_VARIADIC_MACROS */ + +#else /* DUK_USE_DEBUG */ + +/* + * Exposed debug macros: debugging disabled + */ + +#define DUK_D(x) do { } while (0) /* omit */ +#define DUK_DD(x) do { } while (0) /* omit */ +#define DUK_DDD(x) do { } while (0) /* omit */ + +#ifdef DUK_USE_VARIADIC_MACROS + +#define DUK_DPRINT(...) +#define DUK_DDPRINT(...) +#define DUK_DDDPRINT(...) + +#else /* DUK_USE_VARIADIC_MACROS */ + +#define DUK_DPRINT 0 && /* args go here as a comma expression in parens */ +#define DUK_DDPRINT 0 && /* args */ +#define DUK_DDDPRINT 0 && /* args */ + +#endif /* DUK_USE_VARIADIC_MACROS */ + +#endif /* DUK_USE_DEBUG */ + +/* + * Structs + */ + +#ifdef DUK_USE_DEBUG +struct duk_fixedbuffer { + duk_uint8_t *buffer; + duk_size_t length; + duk_size_t offset; + duk_bool_t truncated; +}; +#endif + +/* + * Prototypes + */ + +#ifdef DUK_USE_DEBUG +DUK_INTERNAL_DECL duk_int_t duk_debug_vsnprintf(char *str, duk_size_t size, const char *format, va_list ap); +#if 0 /*unused*/ +DUK_INTERNAL_DECL duk_int_t duk_debug_snprintf(char *str, duk_size_t size, const char *format, ...); +#endif +DUK_INTERNAL_DECL void duk_debug_format_funcptr(char *buf, duk_size_t buf_size, duk_uint8_t *fptr, duk_size_t fptr_size); + +#ifdef DUK_USE_VARIADIC_MACROS +DUK_INTERNAL_DECL void duk_debug_log(duk_small_int_t level, const char *file, duk_int_t line, const char *func, const char *fmt, ...); +#else /* DUK_USE_VARIADIC_MACROS */ +/* parameter passing, not thread safe */ +#define DUK_DEBUG_STASH_SIZE 128 +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL char duk_debug_file_stash[DUK_DEBUG_STASH_SIZE]; +DUK_INTERNAL_DECL char duk_debug_line_stash[DUK_DEBUG_STASH_SIZE]; +DUK_INTERNAL_DECL char duk_debug_func_stash[DUK_DEBUG_STASH_SIZE]; +DUK_INTERNAL_DECL duk_small_int_t duk_debug_level_stash; +#endif +DUK_INTERNAL_DECL void duk_debug_log(const char *fmt, ...); +#endif /* DUK_USE_VARIADIC_MACROS */ + +DUK_INTERNAL_DECL void duk_fb_put_bytes(duk_fixedbuffer *fb, const duk_uint8_t *buffer, duk_size_t length); +DUK_INTERNAL_DECL void duk_fb_put_byte(duk_fixedbuffer *fb, duk_uint8_t x); +DUK_INTERNAL_DECL void duk_fb_put_cstring(duk_fixedbuffer *fb, const char *x); +DUK_INTERNAL_DECL void duk_fb_sprintf(duk_fixedbuffer *fb, const char *fmt, ...); +DUK_INTERNAL_DECL void duk_fb_put_funcptr(duk_fixedbuffer *fb, duk_uint8_t *fptr, duk_size_t fptr_size); +DUK_INTERNAL_DECL duk_bool_t duk_fb_is_full(duk_fixedbuffer *fb); + +#endif /* DUK_USE_DEBUG */ + +#endif /* DUK_DEBUG_H_INCLUDED */ +/* + * Error handling macros, assertion macro, error codes. + * + * There are three level of 'errors': + * + * 1. Ordinary errors, relative to a thread, cause a longjmp, catchable. + * 2. Fatal errors, relative to a heap, cause fatal handler to be called. + * 3. Panic errors, unrelated to a heap and cause a process exit. + * + * Panics are used by the default fatal error handler and by debug code + * such as assertions. By providing a proper fatal error handler, user + * code can avoid panics in non-debug builds. + */ + +#ifndef DUK_ERROR_H_INCLUDED +#define DUK_ERROR_H_INCLUDED + +/* + * Error codes: defined in duktape.h + * + * Error codes are used as a shorthand to throw exceptions from inside + * the implementation. The appropriate Ecmascript object is constructed + * based on the code. Ecmascript code throws objects directly. The error + * codes are defined in the public API header because they are also used + * by calling code. + */ + +/* + * Normal error + * + * Normal error is thrown with a longjmp() through the current setjmp() + * catchpoint record in the duk_heap. The 'curr_thread' of the duk_heap + * identifies the throwing thread. + * + * Error formatting is usually unnecessary. The error macros provide a + * zero argument version (no formatting) and separate macros for small + * argument counts. Variadic macros are not used to avoid portability + * issues and avoid the need for stash-based workarounds when they're not + * available. Vararg calls are avoided for non-formatted error calls + * because vararg call sites are larger than normal, and there are a lot + * of call sites with no formatting. + * + * Note that special formatting provided by debug macros is NOT available. + * + * The _RAW variants allow the caller to specify file and line. This makes + * it easier to write checked calls which want to use the call site of the + * checked function, not the error macro call inside the checked function. + */ + +#if defined(DUK_USE_VERBOSE_ERRORS) + +/* Because there are quite many call sites, pack error code (require at most + * 8-bit) into a single argument. + */ +#define DUK_ERROR(thr,err,msg) do { \ + duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) DUK_LINE_MACRO; \ + DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ + duk_err_handle_error((thr), DUK_FILE_MACRO, (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (msg)); \ + } while (0) +#define DUK_ERROR_RAW(thr,file,line,err,msg) do { \ + duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) (line); \ + DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ + duk_err_handle_error((thr), (file), (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (msg)); \ + } while (0) + +#define DUK_ERROR_FMT1(thr,err,fmt,arg1) do { \ + duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) DUK_LINE_MACRO; \ + DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ + duk_err_handle_error_fmt((thr), DUK_FILE_MACRO, (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1)); \ + } while (0) +#define DUK_ERROR_RAW_FMT1(thr,file,line,err,fmt,arg1) do { \ + duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) (line); \ + DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ + duk_err_handle_error_fmt((thr), (file), (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1)); \ + } while (0) + +#define DUK_ERROR_FMT2(thr,err,fmt,arg1,arg2) do { \ + duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) DUK_LINE_MACRO; \ + DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ + duk_err_handle_error_fmt((thr), DUK_FILE_MACRO, (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2)); \ + } while (0) +#define DUK_ERROR_RAW_FMT2(thr,file,line,err,fmt,arg1,arg2) do { \ + duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) (line); \ + DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ + duk_err_handle_error_fmt((thr), (file), (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2)); \ + } while (0) + +#define DUK_ERROR_FMT3(thr,err,fmt,arg1,arg2,arg3) do { \ + duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) DUK_LINE_MACRO; \ + DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ + duk_err_handle_error_fmt((thr), DUK_FILE_MACRO, (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2), (arg3)); \ + } while (0) +#define DUK_ERROR_RAW_FMT3(thr,file,line,err,fmt,arg1,arg2,arg3) do { \ + duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) (line); \ + DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ + duk_err_handle_error_fmt((thr), (file), (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2), (arg3)); \ + } while (0) + +#define DUK_ERROR_FMT4(thr,err,fmt,arg1,arg2,arg3,arg4) do { \ + duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) DUK_LINE_MACRO; \ + DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ + duk_err_handle_error_fmt((thr), DUK_FILE_MACRO, (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2), (arg3), (arg4)); \ + } while (0) +#define DUK_ERROR_RAW_FMT4(thr,file,line,err,fmt,arg1,arg2,arg3,arg4) do { \ + duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) (line); \ + DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \ + duk_err_handle_error_fmt((thr), (file), (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2), (arg3), (arg4)); \ + } while (0) + +#else /* DUK_USE_VERBOSE_ERRORS */ + +#define DUK_ERROR(thr,err,msg) duk_err_handle_error((thr), (err)) +#define DUK_ERROR_RAW(thr,file,line,err,msg) duk_err_handle_error((thr), (err)) + +#define DUK_ERROR_FMT1(thr,err,fmt,arg1) DUK_ERROR((thr),(err),(fmt)) +#define DUK_ERROR_RAW_FMT1(thr,file,line,err,fmt,arg1) DUK_ERROR_RAW((thr),(file),(line),(err),(fmt)) + +#define DUK_ERROR_FMT2(thr,err,fmt,arg1,arg2) DUK_ERROR((thr),(err),(fmt)) +#define DUK_ERROR_RAW_FMT2(thr,file,line,err,fmt,arg1,arg2) DUK_ERROR_RAW((thr),(file),(line),(err),(fmt)) + +#define DUK_ERROR_FMT3(thr,err,fmt,arg1,arg2,arg3) DUK_ERROR((thr),(err),(fmt)) +#define DUK_ERROR_RAW_FMT3(thr,file,line,err,fmt,arg1,arg2,arg3) DUK_ERROR_RAW((thr),(file),(line),(err),(fmt)) + +#define DUK_ERROR_FMT4(thr,err,fmt,arg1,arg2,arg3,arg4) DUK_ERROR((thr),(err),(fmt)) +#define DUK_ERROR_RAW_FMT4(thr,file,line,err,fmt,arg1,arg2,arg3,arg4) DUK_ERROR_RAW((thr),(file),(line),(err),(fmt)) + +#endif /* DUK_USE_VERBOSE_ERRORS */ + +/* + * Fatal error + * + * There are no fatal error macros at the moment. There are so few call + * sites that the fatal error handler is called directly. + */ + +/* + * Panic error + * + * Panic errors are not relative to either a heap or a thread, and cause + * DUK_PANIC() macro to be invoked. Unless a user provides DUK_USE_PANIC_HANDLER, + * DUK_PANIC() calls a helper which prints out the error and causes a process + * exit. + * + * The user can override the macro to provide custom handling. A macro is + * used to allow the user to have inline panic handling if desired (without + * causing a potentially risky function call). + * + * Panics are only used in debug code such as assertions, and by the default + * fatal error handler. + */ + +#if defined(DUK_USE_PANIC_HANDLER) +/* already defined, good */ +#define DUK_PANIC(code,msg) DUK_USE_PANIC_HANDLER((code),(msg)) +#else +#define DUK_PANIC(code,msg) duk_default_panic_handler((code),(msg)) +#endif /* DUK_USE_PANIC_HANDLER */ + +/* + * Assert macro: failure causes panic. + */ + +#if defined(DUK_USE_ASSERTIONS) + +/* the message should be a compile time constant without formatting (less risk); + * we don't care about assertion text size because they're not used in production + * builds. + */ +#define DUK_ASSERT(x) do { \ + if (!(x)) { \ + DUK_PANIC(DUK_ERR_ASSERTION_ERROR, \ + "assertion failed: " #x \ + " (" DUK_FILE_MACRO ":" DUK_MACRO_STRINGIFY(DUK_LINE_MACRO) ")"); \ + } \ + } while (0) + +/* Assertion compatible inside a comma expression, evaluates to void. + * Currently not compatible with DUK_USE_PANIC_HANDLER() which may have + * a statement block. + */ +#if defined(DUK_USE_PANIC_HANDLER) +/* XXX: resolve macro definition issue or call through a helper function? */ +#define DUK_ASSERT_EXPR(x) ((void) 0) +#else +#define DUK_ASSERT_EXPR(x) \ + ((void) ((x) ? 0 : (DUK_PANIC(DUK_ERR_ASSERTION_ERROR, \ + "assertion failed: " #x \ + " (" DUK_FILE_MACRO ":" DUK_MACRO_STRINGIFY(DUK_LINE_MACRO) ")"), 0))) +#endif + +#else /* DUK_USE_ASSERTIONS */ + +#define DUK_ASSERT(x) do { /* assertion omitted */ } while (0) + +#define DUK_ASSERT_EXPR(x) ((void) 0) + +#endif /* DUK_USE_ASSERTIONS */ + +/* this variant is used when an assert would generate a compile warning by + * being always true (e.g. >= 0 comparison for an unsigned value + */ +#define DUK_ASSERT_DISABLE(x) do { /* assertion disabled */ } while (0) + +/* + * Assertion helpers + */ + +#if defined(DUK_USE_ASSERTIONS) && defined(DUK_USE_REFERENCE_COUNTING) +#define DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(h) do { \ + DUK_ASSERT((h) == NULL || DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) (h)) > 0); \ + } while (0) +#define DUK_ASSERT_REFCOUNT_NONZERO_TVAL(tv) do { \ + if ((tv) != NULL && DUK_TVAL_IS_HEAP_ALLOCATED((tv))) { \ + DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(DUK_TVAL_GET_HEAPHDR((tv))) > 0); \ + } \ + } while (0) +#else +#define DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(h) /* no refcount check */ +#define DUK_ASSERT_REFCOUNT_NONZERO_TVAL(tv) /* no refcount check */ +#endif + +#define DUK_ASSERT_TOP(ctx,n) DUK_ASSERT((duk_idx_t) duk_get_top((ctx)) == (duk_idx_t) (n)) + +#if defined(DUK_USE_ASSERTIONS) && defined(DUK_USE_PACKED_TVAL) +#define DUK_ASSERT_DOUBLE_IS_NORMALIZED(dval) do { \ + duk_double_union duk__assert_tmp_du; \ + duk__assert_tmp_du.d = (dval); \ + DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&duk__assert_tmp_du)); \ + } while (0) +#else +#define DUK_ASSERT_DOUBLE_IS_NORMALIZED(dval) /* nop */ +#endif + +/* + * Helper for valstack space + * + * Caller of DUK_ASSERT_VALSTACK_SPACE() estimates the number of free stack entries + * required for its own use, and any child calls which are not (a) Duktape API calls + * or (b) Duktape calls which involve extending the valstack (e.g. getter call). + */ + +#define DUK_VALSTACK_ASSERT_EXTRA 5 /* this is added to checks to allow for Duktape + * API calls in addition to function's own use + */ +#if defined(DUK_USE_ASSERTIONS) +#define DUK_ASSERT_VALSTACK_SPACE(thr,n) do { \ + DUK_ASSERT((thr) != NULL); \ + DUK_ASSERT((thr)->valstack_end - (thr)->valstack_top >= (n) + DUK_VALSTACK_ASSERT_EXTRA); \ + } while (0) +#else +#define DUK_ASSERT_VALSTACK_SPACE(thr,n) /* no valstack space check */ +#endif + +/* + * Error throwing helpers + * + * The goal is to provide verbose and configurable error messages. Call + * sites should be clean in source code and compile to a small footprint. + * Small footprint is also useful for performance because small cold paths + * reduce code cache pressure. Adding macros here only makes sense if there + * are enough call sites to get concrete benefits. + */ + +#if defined(DUK_USE_VERBOSE_ERRORS) +/* Verbose errors with key/value summaries (non-paranoid) or without key/value + * summaries (paranoid, for some security sensitive environments), the paranoid + * vs. non-paranoid distinction affects only a few specific errors. + */ +#if defined(DUK_USE_PARANOID_ERRORS) +#define DUK_ERROR_REQUIRE_TYPE_INDEX(thr,index,expectname,lowmemstr) do { \ + duk_err_require_type_index((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (index), (expectname)); \ + } while (0) +#else /* DUK_USE_PARANOID_ERRORS */ +#define DUK_ERROR_REQUIRE_TYPE_INDEX(thr,index,expectname,lowmemstr) do { \ + duk_err_require_type_index((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (index), (expectname)); \ + } while (0) +#endif /* DUK_USE_PARANOID_ERRORS */ + +#define DUK_ERROR_UNIMPLEMENTED(thr,msg) do { \ + DUK_ERROR((thr), DUK_ERR_UNIMPLEMENTED_ERROR, (msg)); \ + } while (0) +#define DUK_ERROR_UNIMPLEMENTED_DEFMSG(thr) do { \ + duk_err_unimplemented_defmsg((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO); \ + } while (0) +#define DUK_ERROR_UNSUPPORTED(thr,msg) do { \ + DUK_ERROR((thr), DUK_ERR_UNSUPPORTED_ERROR, (msg)); \ + } while (0) +#if !defined(DUK_USE_BYTECODE_DUMP_SUPPORT) +#define DUK_ERROR_UNSUPPORTED_DEFMSG(thr) do { \ + duk_err_unsupported_defmsg((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO); \ + } while (0) +#endif +#define DUK_ERROR_INTERNAL(thr,msg) do { \ + duk_err_internal((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (msg)); \ + } while (0) +#define DUK_ERROR_INTERNAL_DEFMSG(thr) do { \ + duk_err_internal_defmsg((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO); \ + } while (0) +#define DUK_ERROR_ALLOC(thr,msg) do { \ + duk_err_alloc((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (msg)); \ + } while (0) +#define DUK_ERROR_ALLOC_DEFMSG(thr) do { \ + DUK_ERROR_ALLOC((thr), DUK_STR_ALLOC_FAILED); \ + } while (0) +/* DUK_ERR_ASSERTION_ERROR: no macros needed */ +#define DUK_ERROR_API_INDEX(thr,index) do { \ + duk_err_api_index((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (index)); \ + } while (0) +#define DUK_ERROR_API(thr,msg) do { \ + duk_err_api((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (msg)); \ + } while (0) +/* DUK_ERR_UNCAUGHT_ERROR: no macros needed */ +/* DUK_ERR_ERROR: no macros needed */ +/* DUK_ERR_EVAL: no macros needed */ +#define DUK_ERROR_RANGE(thr,msg) do { \ + duk_err_range((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (msg)); \ + } while (0) +/* DUK_ERR_REFERENCE_ERROR: no macros needed */ +#define DUK_ERROR_SYNTAX(thr,msg) do { \ + DUK_ERROR((thr), DUK_ERR_SYNTAX_ERROR, (msg)); \ + } while (0) +#define DUK_ERROR_TYPE(thr,msg) do { \ + DUK_ERROR((thr), DUK_ERR_TYPE_ERROR, (msg)); \ + } while (0) +/* DUK_ERR_URI_ERROR: no macros needed */ +#else /* DUK_USE_VERBOSE_ERRORS */ +/* Non-verbose errors for low memory targets: no file, line, or message. */ + +#define DUK_ERROR_REQUIRE_TYPE_INDEX(thr,index,expectname,lowmemstr) do { \ + duk_err_type((thr)); \ + } while (0) + +#define DUK_ERROR_UNIMPLEMENTED(thr,msg) do { \ + duk_err_unimplemented((thr)); \ + } while (0) +#define DUK_ERROR_UNIMPLEMENTED_DEFMSG(thr) do { \ + duk_err_unimplemented((thr)); \ + } while (0) +#define DUK_ERROR_UNSUPPORTED(thr,msg) do { \ + duk_err_unsupported((thr)); \ + } while (0) +#define DUK_ERROR_UNSUPPORTED_DEFMSG(thr) do { \ + duk_err_unsupported((thr)); \ + } while (0) +#define DUK_ERROR_INTERNAL(thr,msg) do { \ + duk_err_internal((thr)); \ + } while (0) +#define DUK_ERROR_INTERNAL_DEFMSG(thr) do { \ + duk_err_internal((thr)); \ + } while (0) +#define DUK_ERROR_ALLOC(thr,msg) do { \ + duk_err_alloc((thr)); \ + } while (0) +#define DUK_ERROR_ALLOC_DEFMSG(thr) do { \ + duk_err_alloc((thr)); \ + } while (0) +#define DUK_ERROR_API_INDEX(thr,index) do { \ + duk_err_api((thr)); \ + } while (0) +#define DUK_ERROR_API(thr,msg) do { \ + duk_err_api((thr)); \ + } while (0) +#define DUK_ERROR_RANGE(thr,msg) do { \ + duk_err_range((thr)); \ + } while (0) +#define DUK_ERROR_SYNTAX(thr,msg) do { \ + duk_err_syntax((thr)); \ + } while (0) +#define DUK_ERROR_TYPE(thr,msg) do { \ + duk_err_type((thr)); \ + } while (0) +#endif /* DUK_USE_VERBOSE_ERRORS */ + +/* + * Prototypes + */ + +#if defined(DUK_USE_VERBOSE_ERRORS) +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error(duk_hthread *thr, const char *filename, duk_uint_t line_and_code, const char *msg)); +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error_fmt(duk_hthread *thr, const char *filename, duk_uint_t line_and_code, const char *fmt, ...)); +#else /* DUK_USE_VERBOSE_ERRORS */ +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error(duk_hthread *thr, duk_errcode_t code)); +#endif /* DUK_USE_VERBOSE_ERRORS */ + +#if defined(DUK_USE_VERBOSE_ERRORS) +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code, const char *msg, const char *filename, duk_int_t line)); +#else +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code)); +#endif + +DUK_NORETURN(DUK_INTERNAL_DECL void duk_error_throw_from_negative_rc(duk_hthread *thr, duk_ret_t rc)); + +#if defined(DUK_USE_AUGMENT_ERROR_CREATE) +DUK_INTERNAL_DECL void duk_err_augment_error_create(duk_hthread *thr, duk_hthread *thr_callstack, const char *filename, duk_int_t line, duk_bool_t noblame_fileline); +#endif +#if defined(DUK_USE_AUGMENT_ERROR_THROW) +DUK_INTERNAL_DECL void duk_err_augment_error_throw(duk_hthread *thr); +#endif + +#if defined(DUK_USE_VERBOSE_ERRORS) +#if defined(DUK_USE_PARANOID_ERRORS) +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_require_type_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t index, const char *expect_name)); +#else +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_require_type_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t index, const char *expect_name)); +#endif +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_api_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t index)); +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_api(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message)); +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_range(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message)); +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_unimplemented_defmsg(duk_hthread *thr, const char *filename, duk_int_t linenumber)); +#if !defined(DUK_USE_BYTECODE_DUMP_SUPPORT) +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_unsupported_defmsg(duk_hthread *thr, const char *filename, duk_int_t linenumber)); +#endif +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_internal_defmsg(duk_hthread *thr, const char *filename, duk_int_t linenumber)); +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_internal(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message)); +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_alloc(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message)); +#else /* DUK_VERBOSE_ERRORS */ +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_range(duk_hthread *thr)); +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_syntax(duk_hthread *thr)); +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_type(duk_hthread *thr)); +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_api(duk_hthread *thr)); +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_unimplemented(duk_hthread *thr)); +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_unsupported(duk_hthread *thr)); +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_internal(duk_hthread *thr)); +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_alloc(duk_hthread *thr)); +#endif /* DUK_VERBOSE_ERRORS */ + +DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_longjmp(duk_hthread *thr)); + +DUK_NORETURN(DUK_INTERNAL_DECL void duk_default_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg)); + +#if !defined(DUK_USE_PANIC_HANDLER) +DUK_NORETURN(DUK_INTERNAL_DECL void duk_default_panic_handler(duk_errcode_t code, const char *msg)); +#endif + +DUK_INTERNAL_DECL void duk_err_setup_heap_ljstate(duk_hthread *thr, duk_small_int_t lj_type); + +DUK_INTERNAL_DECL duk_hobject *duk_error_prototype_from_code(duk_hthread *thr, duk_errcode_t err_code); + +#endif /* DUK_ERROR_H_INCLUDED */ +/* + * Unicode helpers + */ + +#ifndef DUK_UNICODE_H_INCLUDED +#define DUK_UNICODE_H_INCLUDED + +/* + * UTF-8 / XUTF-8 / CESU-8 constants + */ + +#define DUK_UNICODE_MAX_XUTF8_LENGTH 7 /* up to 36 bit codepoints */ +#define DUK_UNICODE_MAX_XUTF8_BMP_LENGTH 3 /* all codepoints up to U+FFFF */ +#define DUK_UNICODE_MAX_CESU8_LENGTH 6 /* all codepoints up to U+10FFFF */ +#define DUK_UNICODE_MAX_CESU8_BMP_LENGTH 3 /* all codepoints up to U+FFFF */ + +/* + * Useful Unicode codepoints + * + * Integer constants must be signed to avoid unexpected coercions + * in comparisons. + */ + +#define DUK_UNICODE_CP_ZWNJ 0x200cL /* zero-width non-joiner */ +#define DUK_UNICODE_CP_ZWJ 0x200dL /* zero-width joiner */ +#define DUK_UNICODE_CP_REPLACEMENT_CHARACTER 0xfffdL /* http://en.wikipedia.org/wiki/Replacement_character#Replacement_character */ + +/* + * ASCII character constants + * + * C character literals like 'x' have a platform specific value and do + * not match ASCII (UTF-8) values on e.g. EBCDIC platforms. So, use + * these (admittedly awkward) constants instead. These constants must + * also have signed values to avoid unexpected coercions in comparisons. + * + * http://en.wikipedia.org/wiki/ASCII + */ + +#define DUK_ASC_NUL 0x00 +#define DUK_ASC_SOH 0x01 +#define DUK_ASC_STX 0x02 +#define DUK_ASC_ETX 0x03 +#define DUK_ASC_EOT 0x04 +#define DUK_ASC_ENQ 0x05 +#define DUK_ASC_ACK 0x06 +#define DUK_ASC_BEL 0x07 +#define DUK_ASC_BS 0x08 +#define DUK_ASC_HT 0x09 +#define DUK_ASC_LF 0x0a +#define DUK_ASC_VT 0x0b +#define DUK_ASC_FF 0x0c +#define DUK_ASC_CR 0x0d +#define DUK_ASC_SO 0x0e +#define DUK_ASC_SI 0x0f +#define DUK_ASC_DLE 0x10 +#define DUK_ASC_DC1 0x11 +#define DUK_ASC_DC2 0x12 +#define DUK_ASC_DC3 0x13 +#define DUK_ASC_DC4 0x14 +#define DUK_ASC_NAK 0x15 +#define DUK_ASC_SYN 0x16 +#define DUK_ASC_ETB 0x17 +#define DUK_ASC_CAN 0x18 +#define DUK_ASC_EM 0x19 +#define DUK_ASC_SUB 0x1a +#define DUK_ASC_ESC 0x1b +#define DUK_ASC_FS 0x1c +#define DUK_ASC_GS 0x1d +#define DUK_ASC_RS 0x1e +#define DUK_ASC_US 0x1f +#define DUK_ASC_SPACE 0x20 +#define DUK_ASC_EXCLAMATION 0x21 +#define DUK_ASC_DOUBLEQUOTE 0x22 +#define DUK_ASC_HASH 0x23 +#define DUK_ASC_DOLLAR 0x24 +#define DUK_ASC_PERCENT 0x25 +#define DUK_ASC_AMP 0x26 +#define DUK_ASC_SINGLEQUOTE 0x27 +#define DUK_ASC_LPAREN 0x28 +#define DUK_ASC_RPAREN 0x29 +#define DUK_ASC_STAR 0x2a +#define DUK_ASC_PLUS 0x2b +#define DUK_ASC_COMMA 0x2c +#define DUK_ASC_MINUS 0x2d +#define DUK_ASC_PERIOD 0x2e +#define DUK_ASC_SLASH 0x2f +#define DUK_ASC_0 0x30 +#define DUK_ASC_1 0x31 +#define DUK_ASC_2 0x32 +#define DUK_ASC_3 0x33 +#define DUK_ASC_4 0x34 +#define DUK_ASC_5 0x35 +#define DUK_ASC_6 0x36 +#define DUK_ASC_7 0x37 +#define DUK_ASC_8 0x38 +#define DUK_ASC_9 0x39 +#define DUK_ASC_COLON 0x3a +#define DUK_ASC_SEMICOLON 0x3b +#define DUK_ASC_LANGLE 0x3c +#define DUK_ASC_EQUALS 0x3d +#define DUK_ASC_RANGLE 0x3e +#define DUK_ASC_QUESTION 0x3f +#define DUK_ASC_ATSIGN 0x40 +#define DUK_ASC_UC_A 0x41 +#define DUK_ASC_UC_B 0x42 +#define DUK_ASC_UC_C 0x43 +#define DUK_ASC_UC_D 0x44 +#define DUK_ASC_UC_E 0x45 +#define DUK_ASC_UC_F 0x46 +#define DUK_ASC_UC_G 0x47 +#define DUK_ASC_UC_H 0x48 +#define DUK_ASC_UC_I 0x49 +#define DUK_ASC_UC_J 0x4a +#define DUK_ASC_UC_K 0x4b +#define DUK_ASC_UC_L 0x4c +#define DUK_ASC_UC_M 0x4d +#define DUK_ASC_UC_N 0x4e +#define DUK_ASC_UC_O 0x4f +#define DUK_ASC_UC_P 0x50 +#define DUK_ASC_UC_Q 0x51 +#define DUK_ASC_UC_R 0x52 +#define DUK_ASC_UC_S 0x53 +#define DUK_ASC_UC_T 0x54 +#define DUK_ASC_UC_U 0x55 +#define DUK_ASC_UC_V 0x56 +#define DUK_ASC_UC_W 0x57 +#define DUK_ASC_UC_X 0x58 +#define DUK_ASC_UC_Y 0x59 +#define DUK_ASC_UC_Z 0x5a +#define DUK_ASC_LBRACKET 0x5b +#define DUK_ASC_BACKSLASH 0x5c +#define DUK_ASC_RBRACKET 0x5d +#define DUK_ASC_CARET 0x5e +#define DUK_ASC_UNDERSCORE 0x5f +#define DUK_ASC_GRAVE 0x60 +#define DUK_ASC_LC_A 0x61 +#define DUK_ASC_LC_B 0x62 +#define DUK_ASC_LC_C 0x63 +#define DUK_ASC_LC_D 0x64 +#define DUK_ASC_LC_E 0x65 +#define DUK_ASC_LC_F 0x66 +#define DUK_ASC_LC_G 0x67 +#define DUK_ASC_LC_H 0x68 +#define DUK_ASC_LC_I 0x69 +#define DUK_ASC_LC_J 0x6a +#define DUK_ASC_LC_K 0x6b +#define DUK_ASC_LC_L 0x6c +#define DUK_ASC_LC_M 0x6d +#define DUK_ASC_LC_N 0x6e +#define DUK_ASC_LC_O 0x6f +#define DUK_ASC_LC_P 0x70 +#define DUK_ASC_LC_Q 0x71 +#define DUK_ASC_LC_R 0x72 +#define DUK_ASC_LC_S 0x73 +#define DUK_ASC_LC_T 0x74 +#define DUK_ASC_LC_U 0x75 +#define DUK_ASC_LC_V 0x76 +#define DUK_ASC_LC_W 0x77 +#define DUK_ASC_LC_X 0x78 +#define DUK_ASC_LC_Y 0x79 +#define DUK_ASC_LC_Z 0x7a +#define DUK_ASC_LCURLY 0x7b +#define DUK_ASC_PIPE 0x7c +#define DUK_ASC_RCURLY 0x7d +#define DUK_ASC_TILDE 0x7e +#define DUK_ASC_DEL 0x7f + +/* + * Unicode tables + */ + +#ifdef DUK_USE_SOURCE_NONBMP +/* + * Automatically generated by extract_chars.py, do not edit! + */ + +extern const duk_uint8_t duk_unicode_ids_noa[791]; +#else +/* + * Automatically generated by extract_chars.py, do not edit! + */ + +extern const duk_uint8_t duk_unicode_ids_noabmp[611]; +#endif + +#ifdef DUK_USE_SOURCE_NONBMP +/* + * Automatically generated by extract_chars.py, do not edit! + */ + +extern const duk_uint8_t duk_unicode_ids_m_let_noa[42]; +#else +/* + * Automatically generated by extract_chars.py, do not edit! + */ + +extern const duk_uint8_t duk_unicode_ids_m_let_noabmp[24]; +#endif + +#ifdef DUK_USE_SOURCE_NONBMP +/* + * Automatically generated by extract_chars.py, do not edit! + */ + +extern const duk_uint8_t duk_unicode_idp_m_ids_noa[397]; +#else +/* + * Automatically generated by extract_chars.py, do not edit! + */ + +extern const duk_uint8_t duk_unicode_idp_m_ids_noabmp[348]; +#endif + +/* + * Automatically generated by extract_caseconv.py, do not edit! + */ + +extern const duk_uint8_t duk_unicode_caseconv_uc[1288]; +extern const duk_uint8_t duk_unicode_caseconv_lc[616]; + +#if defined(DUK_USE_REGEXP_CANON_WORKAROUND) +/* + * Automatically generated by extract_caseconv.py, do not edit! + */ + +extern const duk_uint16_t duk_unicode_re_canon_lookup[65536]; +#endif + +/* + * Extern + */ + +/* duk_unicode_support.c */ +#if !defined(DUK_SINGLE_FILE) +DUK_INTERNAL_DECL const duk_uint8_t duk_unicode_xutf8_markers[7]; +DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_digit[2]; +DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_white[22]; +DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_wordchar[8]; +DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_not_digit[4]; +DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_not_white[24]; +DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_not_wordchar[10]; +DUK_INTERNAL_DECL const duk_int8_t duk_is_idchar_tab[128]; +#endif /* !DUK_SINGLE_FILE */ + +/* + * Prototypes + */ + +DUK_INTERNAL_DECL duk_small_int_t duk_unicode_get_xutf8_length(duk_ucodepoint_t cp); +#if defined(DUK_USE_ASSERTIONS) +DUK_INTERNAL_DECL duk_small_int_t duk_unicode_get_cesu8_length(duk_ucodepoint_t cp); +#endif +DUK_INTERNAL_DECL duk_small_int_t duk_unicode_encode_xutf8(duk_ucodepoint_t cp, duk_uint8_t *out); +DUK_INTERNAL_DECL duk_small_int_t duk_unicode_encode_cesu8(duk_ucodepoint_t cp, duk_uint8_t *out); +DUK_INTERNAL_DECL duk_small_int_t duk_unicode_decode_xutf8(duk_hthread *thr, const duk_uint8_t **ptr, const duk_uint8_t *ptr_start, const duk_uint8_t *ptr_end, duk_ucodepoint_t *out_cp); +DUK_INTERNAL_DECL duk_ucodepoint_t duk_unicode_decode_xutf8_checked(duk_hthread *thr, const duk_uint8_t **ptr, const duk_uint8_t *ptr_start, const duk_uint8_t *ptr_end); +DUK_INTERNAL_DECL duk_size_t duk_unicode_unvalidated_utf8_length(const duk_uint8_t *data, duk_size_t blen); +DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_whitespace(duk_codepoint_t cp); +DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_line_terminator(duk_codepoint_t cp); +DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_identifier_start(duk_codepoint_t cp); +DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_identifier_part(duk_codepoint_t cp); +DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_letter(duk_codepoint_t cp); +DUK_INTERNAL_DECL void duk_unicode_case_convert_string(duk_hthread *thr, duk_bool_t uppercase); +DUK_INTERNAL_DECL duk_codepoint_t duk_unicode_re_canonicalize_char(duk_hthread *thr, duk_codepoint_t cp); +DUK_INTERNAL_DECL duk_small_int_t duk_unicode_re_is_wordchar(duk_codepoint_t cp); + +#endif /* DUK_UNICODE_H_INCLUDED */ +/* + * Defines for JSON, especially duk_bi_json.c. + */ + +#ifndef DUK_JSON_H_INCLUDED +#define DUK_JSON_H_INCLUDED + +/* Encoding/decoding flags */ +#define DUK_JSON_FLAG_ASCII_ONLY (1 << 0) /* escape any non-ASCII characters */ +#define DUK_JSON_FLAG_AVOID_KEY_QUOTES (1 << 1) /* avoid key quotes when key is an ASCII Identifier */ +#define DUK_JSON_FLAG_EXT_CUSTOM (1 << 2) /* extended types: custom encoding */ +#define DUK_JSON_FLAG_EXT_COMPATIBLE (1 << 3) /* extended types: compatible encoding */ + +/* How much stack to require on entry to object/array encode */ +#define DUK_JSON_ENC_REQSTACK 32 + +/* How much stack to require on entry to object/array decode */ +#define DUK_JSON_DEC_REQSTACK 32 + +/* How large a loop detection stack to use */ +#define DUK_JSON_ENC_LOOPARRAY 64 + +/* Encoding state. Heap object references are all borrowed. */ +typedef struct { + duk_hthread *thr; + duk_bufwriter_ctx bw; /* output bufwriter */ + duk_hobject *h_replacer; /* replacer function */ + duk_hstring *h_gap; /* gap (if empty string, NULL) */ + duk_idx_t idx_proplist; /* explicit PropertyList */ + duk_idx_t idx_loop; /* valstack index of loop detection object */ + duk_small_uint_t flags; + duk_small_uint_t flag_ascii_only; + duk_small_uint_t flag_avoid_key_quotes; +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + duk_small_uint_t flag_ext_custom; + duk_small_uint_t flag_ext_compatible; + duk_small_uint_t flag_ext_custom_or_compatible; +#endif + duk_int_t recursion_depth; + duk_int_t recursion_limit; + duk_uint_t mask_for_undefined; /* type bit mask: types which certainly produce 'undefined' */ +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + duk_small_uint_t stridx_custom_undefined; + duk_small_uint_t stridx_custom_nan; + duk_small_uint_t stridx_custom_neginf; + duk_small_uint_t stridx_custom_posinf; + duk_small_uint_t stridx_custom_function; +#endif + duk_hobject *visiting[DUK_JSON_ENC_LOOPARRAY]; /* indexed by recursion_depth */ +} duk_json_enc_ctx; + +typedef struct { + duk_hthread *thr; + const duk_uint8_t *p; + const duk_uint8_t *p_start; + const duk_uint8_t *p_end; + duk_idx_t idx_reviver; + duk_small_uint_t flags; +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + duk_small_uint_t flag_ext_custom; + duk_small_uint_t flag_ext_compatible; + duk_small_uint_t flag_ext_custom_or_compatible; +#endif + duk_int_t recursion_depth; + duk_int_t recursion_limit; +} duk_json_dec_ctx; + +#endif /* DUK_JSON_H_INCLUDED */ +/* + * Ecmascript execution, support primitives. + */ + +#ifndef DUK_JS_H_INCLUDED +#define DUK_JS_H_INCLUDED + +/* Flags for call handling. */ +#define DUK_CALL_FLAG_IGNORE_RECLIMIT (1 << 0) /* duk_handle_call_xxx: call ignores C recursion limit (for errhandler calls) */ +#define DUK_CALL_FLAG_CONSTRUCTOR_CALL (1 << 1) /* duk_handle_call_xxx: constructor call (i.e. called as 'new Foo()') */ +#define DUK_CALL_FLAG_IS_RESUME (1 << 2) /* duk_handle_ecma_call_setup: setup for a resume() */ +#define DUK_CALL_FLAG_IS_TAILCALL (1 << 3) /* duk_handle_ecma_call_setup: setup for a tail call */ +#define DUK_CALL_FLAG_DIRECT_EVAL (1 << 4) /* call is a direct eval call */ + +/* Flags for duk_js_equals_helper(). */ +#define DUK_EQUALS_FLAG_SAMEVALUE (1 << 0) /* use SameValue instead of non-strict equality */ +#define DUK_EQUALS_FLAG_STRICT (1 << 1) /* use strict equality instead of non-strict equality */ + +/* Flags for duk_js_compare_helper(). */ +#define DUK_COMPARE_FLAG_EVAL_LEFT_FIRST (1 << 0) /* eval left argument first */ +#define DUK_COMPARE_FLAG_NEGATE (1 << 1) /* negate result */ + +/* conversions, coercions, comparison, etc */ +DUK_INTERNAL_DECL duk_bool_t duk_js_toboolean(duk_tval *tv); +DUK_INTERNAL_DECL duk_double_t duk_js_tonumber(duk_hthread *thr, duk_tval *tv); +DUK_INTERNAL_DECL duk_double_t duk_js_tointeger_number(duk_double_t x); +DUK_INTERNAL_DECL duk_double_t duk_js_tointeger(duk_hthread *thr, duk_tval *tv); +DUK_INTERNAL_DECL duk_uint32_t duk_js_touint32(duk_hthread *thr, duk_tval *tv); +DUK_INTERNAL_DECL duk_int32_t duk_js_toint32(duk_hthread *thr, duk_tval *tv); +DUK_INTERNAL_DECL duk_uint16_t duk_js_touint16(duk_hthread *thr, duk_tval *tv); +DUK_INTERNAL_DECL duk_small_int_t duk_js_to_arrayindex_raw_string(const duk_uint8_t *str, duk_uint32_t blen, duk_uarridx_t *out_idx); +DUK_INTERNAL_DECL duk_uarridx_t duk_js_to_arrayindex_string_helper(duk_hstring *h); +DUK_INTERNAL_DECL duk_bool_t duk_js_equals_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_int_t flags); +DUK_INTERNAL_DECL duk_small_int_t duk_js_data_compare(const duk_uint8_t *buf1, const duk_uint8_t *buf2, duk_size_t len1, duk_size_t len2); +DUK_INTERNAL_DECL duk_small_int_t duk_js_string_compare(duk_hstring *h1, duk_hstring *h2); +#if 0 /* unused */ +DUK_INTERNAL_DECL duk_small_int_t duk_js_buffer_compare(duk_heap *heap, duk_hbuffer *h1, duk_hbuffer *h2); +#endif +DUK_INTERNAL_DECL duk_bool_t duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_int_t flags); +DUK_INTERNAL_DECL duk_bool_t duk_js_instanceof(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y); +DUK_INTERNAL_DECL duk_bool_t duk_js_in(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y); +DUK_INTERNAL_DECL duk_hstring *duk_js_typeof(duk_hthread *thr, duk_tval *tv_x); + +#define duk_js_equals(thr,tv_x,tv_y) \ + duk_js_equals_helper((thr), (tv_x), (tv_y), 0) +#define duk_js_strict_equals(tv_x,tv_y) \ + duk_js_equals_helper(NULL, (tv_x), (tv_y), DUK_EQUALS_FLAG_STRICT) +#define duk_js_samevalue(tv_x,tv_y) \ + duk_js_equals_helper(NULL, (tv_x), (tv_y), DUK_EQUALS_FLAG_SAMEVALUE) + +/* E5 Sections 11.8.1, 11.8.5; x < y */ +#define duk_js_lessthan(thr,tv_x,tv_y) \ + duk_js_compare_helper((thr), (tv_x), (tv_Y), DUK_COMPARE_FLAG_EVAL_LEFT_FIRST) + +/* E5 Sections 11.8.2, 11.8.5; x > y --> y < x */ +#define duk_js_greaterthan(thr,tv_x,tv_y) \ + duk_js_compare_helper((thr), (tv_y), (tv_x), 0) + +/* E5 Sections 11.8.3, 11.8.5; x <= y --> not (x > y) --> not (y < x) */ +#define duk_js_lessthanorequal(thr,tv_x,tv_y) \ + duk_js_compare_helper((thr), (tv_y), (tv_x), DUK_COMPARE_FLAG_NEGATE) + +/* E5 Sections 11.8.4, 11.8.5; x >= y --> not (x < y) */ +#define duk_js_greaterthanorequal(thr,tv_x,tv_y) \ + duk_js_compare_helper((thr), (tv_x), (tv_y), DUK_COMPARE_FLAG_EVAL_LEFT_FIRST | DUK_COMPARE_FLAG_NEGATE) + +/* identifiers and environment handling */ +#if 0 /*unused*/ +DUK_INTERNAL duk_bool_t duk_js_hasvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name); +#endif +DUK_INTERNAL_DECL duk_bool_t duk_js_getvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name, duk_bool_t throw_flag); +DUK_INTERNAL_DECL duk_bool_t duk_js_getvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name, duk_bool_t throw_flag); +DUK_INTERNAL_DECL void duk_js_putvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name, duk_tval *val, duk_bool_t strict); +DUK_INTERNAL_DECL void duk_js_putvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name, duk_tval *val, duk_bool_t strict); +#if 0 /*unused*/ +DUK_INTERNAL_DECL duk_bool_t duk_js_delvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name); +#endif +DUK_INTERNAL_DECL duk_bool_t duk_js_delvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name); +DUK_INTERNAL_DECL duk_bool_t duk_js_declvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name, duk_tval *val, duk_small_int_t prop_flags, duk_bool_t is_func_decl); +DUK_INTERNAL_DECL void duk_js_init_activation_environment_records_delayed(duk_hthread *thr, duk_activation *act); +DUK_INTERNAL_DECL void duk_js_close_environment_record(duk_hthread *thr, duk_hobject *env, duk_hobject *func, duk_size_t regbase); +DUK_INTERNAL_DECL duk_hobject *duk_create_activation_environment_record(duk_hthread *thr, duk_hobject *func, duk_size_t idx_bottom); +DUK_INTERNAL_DECL +void duk_js_push_closure(duk_hthread *thr, + duk_hcompiledfunction *fun_temp, + duk_hobject *outer_var_env, + duk_hobject *outer_lex_env, + duk_bool_t add_auto_proto); + +/* call handling */ +DUK_INTERNAL_DECL duk_int_t duk_handle_call_protected(duk_hthread *thr, duk_idx_t num_stack_args, duk_small_uint_t call_flags); +DUK_INTERNAL_DECL void duk_handle_call_unprotected(duk_hthread *thr, duk_idx_t num_stack_args, duk_small_uint_t call_flags); +DUK_INTERNAL_DECL duk_int_t duk_handle_safe_call(duk_hthread *thr, duk_safe_call_function func, duk_idx_t num_stack_args, duk_idx_t num_stack_res); +DUK_INTERNAL_DECL duk_bool_t duk_handle_ecma_call_setup(duk_hthread *thr, duk_idx_t num_stack_args, duk_small_uint_t call_flags); + +/* bytecode execution */ +DUK_INTERNAL_DECL void duk_js_execute_bytecode(duk_hthread *exec_thr); + +#endif /* DUK_JS_H_INCLUDED */ +#ifndef DUK_NUMCONV_H_INCLUDED +#define DUK_NUMCONV_H_INCLUDED + +/* + * Number-to-string conversion. The semantics of these is very tightly + * bound with the Ecmascript semantics required for call sites. + */ + +/* Output a specified number of digits instead of using the shortest + * form. Used for toPrecision() and toFixed(). + */ +#define DUK_N2S_FLAG_FIXED_FORMAT (1 << 0) + +/* Force exponential format. Used for toExponential(). */ +#define DUK_N2S_FLAG_FORCE_EXP (1 << 1) + +/* If number would need zero padding (for whole number part), use + * exponential format instead. E.g. if input number is 12300, 3 + * digits are generated ("123"), output "1.23e+4" instead of "12300". + * Used for toPrecision(). + */ +#define DUK_N2S_FLAG_NO_ZERO_PAD (1 << 2) + +/* Digit count indicates number of fractions (i.e. an absolute + * digit index instead of a relative one). Used together with + * DUK_N2S_FLAG_FIXED_FORMAT for toFixed(). + */ +#define DUK_N2S_FLAG_FRACTION_DIGITS (1 << 3) + +/* + * String-to-number conversion + */ + +/* Maximum exponent value when parsing numbers. This is not strictly + * compliant as there should be no upper limit, but as we parse the + * exponent without a bigint, impose some limit. + */ +#define DUK_S2N_MAX_EXPONENT 1000000000 + +/* Trim white space (= allow leading and trailing whitespace) */ +#define DUK_S2N_FLAG_TRIM_WHITE (1 << 0) + +/* Allow exponent */ +#define DUK_S2N_FLAG_ALLOW_EXP (1 << 1) + +/* Allow trailing garbage (e.g. treat "123foo" as "123) */ +#define DUK_S2N_FLAG_ALLOW_GARBAGE (1 << 2) + +/* Allow leading plus sign */ +#define DUK_S2N_FLAG_ALLOW_PLUS (1 << 3) + +/* Allow leading minus sign */ +#define DUK_S2N_FLAG_ALLOW_MINUS (1 << 4) + +/* Allow 'Infinity' */ +#define DUK_S2N_FLAG_ALLOW_INF (1 << 5) + +/* Allow fraction part */ +#define DUK_S2N_FLAG_ALLOW_FRAC (1 << 6) + +/* Allow naked fraction (e.g. ".123") */ +#define DUK_S2N_FLAG_ALLOW_NAKED_FRAC (1 << 7) + +/* Allow empty fraction (e.g. "123.") */ +#define DUK_S2N_FLAG_ALLOW_EMPTY_FRAC (1 << 8) + +/* Allow empty string to be interpreted as 0 */ +#define DUK_S2N_FLAG_ALLOW_EMPTY_AS_ZERO (1 << 9) + +/* Allow leading zeroes (e.g. "0123" -> "123") */ +#define DUK_S2N_FLAG_ALLOW_LEADING_ZERO (1 << 10) + +/* Allow automatic detection of hex base ("0x" or "0X" prefix), + * overrides radix argument and forces integer mode. + */ +#define DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT (1 << 11) + +/* Allow automatic detection of octal base, overrides radix + * argument and forces integer mode. + */ +#define DUK_S2N_FLAG_ALLOW_AUTO_OCT_INT (1 << 12) + +/* + * Prototypes + */ + +DUK_INTERNAL_DECL void duk_numconv_stringify(duk_context *ctx, duk_small_int_t radix, duk_small_int_t digits, duk_small_uint_t flags); +DUK_INTERNAL_DECL void duk_numconv_parse(duk_context *ctx, duk_small_int_t radix, duk_small_uint_t flags); + +#endif /* DUK_NUMCONV_H_INCLUDED */ +/* + * Prototypes for built-in functions not automatically covered by the + * header declarations emitted by genbuiltins.py. + */ + +#ifndef DUK_BUILTIN_PROTOS_H_INCLUDED +#define DUK_BUILTIN_PROTOS_H_INCLUDED + +/* Buffer size needed for duk_bi_date_format_timeval(). + * Accurate value is 32 + 1 for NUL termination: + * >>> len('+123456-01-23T12:34:56.123+12:34') + * 32 + * Include additional space to be safe. + */ +#define DUK_BI_DATE_ISO8601_BUFSIZE 48 + +/* Maximum length of CommonJS module identifier to resolve. Length includes + * both current module ID, requested (possibly relative) module ID, and a + * slash in between. + */ +#define DUK_BI_COMMONJS_MODULE_ID_LIMIT 256 + +/* Helpers exposed for internal use */ +DUK_INTERNAL_DECL void duk_bi_date_timeval_to_parts(duk_double_t d, duk_int_t *parts, duk_double_t *dparts, duk_small_uint_t flags); +DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_timeval_from_dparts(duk_double_t *dparts, duk_small_uint_t flags); +DUK_INTERNAL_DECL void duk_bi_date_format_timeval(duk_double_t timeval, duk_uint8_t *out_buf); +DUK_INTERNAL_DECL duk_bool_t duk_bi_date_is_leap_year(duk_int_t year); +DUK_INTERNAL_DECL duk_bool_t duk_bi_date_timeval_in_valid_range(duk_double_t x); +DUK_INTERNAL_DECL duk_bool_t duk_bi_date_year_in_valid_range(duk_double_t year); +DUK_INTERNAL_DECL duk_bool_t duk_bi_date_timeval_in_leeway_range(duk_double_t x); +/* Built-in providers */ +#if defined(DUK_USE_DATE_NOW_GETTIMEOFDAY) +DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_gettimeofday(duk_context *ctx); +#endif +#if defined(DUK_USE_DATE_NOW_TIME) +DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_time(duk_context *ctx); +#endif +#if defined(DUK_USE_DATE_NOW_WINDOWS) +DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_windows(duk_context *ctx); +#endif +#if defined(DUK_USE_DATE_TZO_GMTIME_R) || defined(DUK_USE_DATE_TZO_GMTIME) +DUK_INTERNAL_DECL duk_int_t duk_bi_date_get_local_tzoffset_gmtime(duk_double_t d); +#endif +#if defined(DUK_USE_DATE_TZO_WINDOWS) +DUK_INTERNAL_DECL duk_int_t duk_bi_date_get_local_tzoffset_windows(duk_double_t d); +#endif +#if defined(DUK_USE_DATE_PRS_STRPTIME) +DUK_INTERNAL_DECL duk_bool_t duk_bi_date_parse_string_strptime(duk_context *ctx, const char *str); +#endif +#if defined(DUK_USE_DATE_PRS_GETDATE) +DUK_INTERNAL_DECL duk_bool_t duk_bi_date_parse_string_getdate(duk_context *ctx, const char *str); +#endif +#if defined(DUK_USE_DATE_FMT_STRFTIME) +DUK_INTERNAL_DECL duk_bool_t duk_bi_date_format_parts_strftime(duk_context *ctx, duk_int_t *parts, duk_int_t tzoffset, duk_small_uint_t flags); +#endif + +DUK_INTERNAL_DECL +void duk_bi_json_parse_helper(duk_context *ctx, + duk_idx_t idx_value, + duk_idx_t idx_reviver, + duk_small_uint_t flags); +DUK_INTERNAL_DECL +void duk_bi_json_stringify_helper(duk_context *ctx, + duk_idx_t idx_value, + duk_idx_t idx_replacer, + duk_idx_t idx_space, + duk_small_uint_t flags); + +#endif /* DUK_BUILTIN_PROTOS_H_INCLUDED */ +/* + * Selftest code + */ + +#ifndef DUK_SELFTEST_H_INCLUDED +#define DUK_SELFTEST_H_INCLUDED + +#if defined(DUK_USE_SELF_TESTS) +DUK_INTERNAL_DECL void duk_selftest_run_tests(void); +#endif + +#endif /* DUK_SELFTEST_H_INCLUDED */ + +#endif /* DUK_INTERNAL_H_INCLUDED */ +/* + * Replacements for missing platform functions. + * + * Unlike the originals, fpclassify() and signbit() replacements don't + * work on any floating point types, only doubles. The C typing here + * mimics the standard prototypes. + */ + +/* include removed: duk_internal.h */ + +#if defined(DUK_USE_COMPUTED_NAN) +DUK_INTERNAL double duk_computed_nan; +#endif + +#if defined(DUK_USE_COMPUTED_INFINITY) +DUK_INTERNAL double duk_computed_infinity; +#endif + +#if defined(DUK_USE_REPL_FPCLASSIFY) +DUK_INTERNAL int duk_repl_fpclassify(double x) { + duk_double_union u; + duk_uint_fast16_t expt; + duk_small_int_t mzero; + + u.d = x; + expt = (duk_uint_fast16_t) (u.us[DUK_DBL_IDX_US0] & 0x7ff0UL); + if (expt > 0x0000UL && expt < 0x7ff0UL) { + /* expt values [0x001,0x7fe] = normal */ + return DUK_FP_NORMAL; + } + + mzero = (u.ui[DUK_DBL_IDX_UI1] == 0 && (u.ui[DUK_DBL_IDX_UI0] & 0x000fffffUL) == 0); + if (expt == 0x0000UL) { + /* expt 0x000 is zero/subnormal */ + if (mzero) { + return DUK_FP_ZERO; + } else { + return DUK_FP_SUBNORMAL; + } + } else { + /* expt 0xfff is infinite/nan */ + if (mzero) { + return DUK_FP_INFINITE; + } else { + return DUK_FP_NAN; + } + } +} +#endif + +#if defined(DUK_USE_REPL_SIGNBIT) +DUK_INTERNAL int duk_repl_signbit(double x) { + duk_double_union u; + u.d = x; + return (int) (u.uc[DUK_DBL_IDX_UC0] & 0x80UL); +} +#endif + +#if defined(DUK_USE_REPL_ISFINITE) +DUK_INTERNAL int duk_repl_isfinite(double x) { + int c = DUK_FPCLASSIFY(x); + if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) { + return 0; + } else { + return 1; + } +} +#endif + +#if defined(DUK_USE_REPL_ISNAN) +DUK_INTERNAL int duk_repl_isnan(double x) { + int c = DUK_FPCLASSIFY(x); + return (c == DUK_FP_NAN); +} +#endif + +#if defined(DUK_USE_REPL_ISINF) +DUK_INTERNAL int duk_repl_isinf(double x) { + int c = DUK_FPCLASSIFY(x); + return (c == DUK_FP_INFINITE); +} +#endif +/* + * Shared error message strings + * + * To minimize code footprint, try to share error messages inside Duktape + * code. Modern compilers will do this automatically anyway, this is mostly + * for older compilers. + */ + +/* include removed: duk_internal.h */ + +/* Mostly API and built-in method related */ +DUK_INTERNAL const char *duk_str_internal_error = "internal error"; +DUK_INTERNAL const char *duk_str_invalid_count = "invalid count"; +DUK_INTERNAL const char *duk_str_invalid_call_args = "invalid call args"; +DUK_INTERNAL const char *duk_str_not_constructable = "not constructable"; +DUK_INTERNAL const char *duk_str_not_callable = "not callable"; +DUK_INTERNAL const char *duk_str_not_extensible = "not extensible"; +DUK_INTERNAL const char *duk_str_not_writable = "not writable"; +DUK_INTERNAL const char *duk_str_not_configurable = "not configurable"; + +DUK_INTERNAL const char *duk_str_invalid_context = "invalid context"; +DUK_INTERNAL const char *duk_str_push_beyond_alloc_stack = "attempt to push beyond currently allocated stack"; +DUK_INTERNAL const char *duk_str_not_buffer = "not buffer"; /* still in use with verbose messages */ +DUK_INTERNAL const char *duk_str_unexpected_type = "unexpected type"; +DUK_INTERNAL const char *duk_str_defaultvalue_coerce_failed = "[[DefaultValue]] coerce failed"; +DUK_INTERNAL const char *duk_str_number_outside_range = "number outside range"; +DUK_INTERNAL const char *duk_str_not_object_coercible = "not object coercible"; +DUK_INTERNAL const char *duk_str_string_too_long = "string too long"; +DUK_INTERNAL const char *duk_str_buffer_too_long = "buffer too long"; +DUK_INTERNAL const char *duk_str_sprintf_too_long = "sprintf message too long"; +DUK_INTERNAL const char *duk_str_alloc_failed = "alloc failed"; +DUK_INTERNAL const char *duk_str_pop_too_many = "attempt to pop too many entries"; +DUK_INTERNAL const char *duk_str_wrong_buffer_type = "wrong buffer type"; +DUK_INTERNAL const char *duk_str_encode_failed = "encode failed"; +DUK_INTERNAL const char *duk_str_decode_failed = "decode failed"; +DUK_INTERNAL const char *duk_str_no_sourcecode = "no sourcecode"; +DUK_INTERNAL const char *duk_str_concat_result_too_long = "concat result too long"; +DUK_INTERNAL const char *duk_str_unimplemented = "unimplemented"; +DUK_INTERNAL const char *duk_str_unsupported = "unsupported"; +DUK_INTERNAL const char *duk_str_array_length_over_2g = "array length over 2G"; + +/* JSON */ +DUK_INTERNAL const char *duk_str_fmt_ptr = "%p"; +DUK_INTERNAL const char *duk_str_fmt_invalid_json = "invalid json (at offset %ld)"; +DUK_INTERNAL const char *duk_str_jsondec_reclimit = "json decode recursion limit"; +DUK_INTERNAL const char *duk_str_jsonenc_reclimit = "json encode recursion limit"; +DUK_INTERNAL const char *duk_str_cyclic_input = "cyclic input"; + +/* Object property access */ +DUK_INTERNAL const char *duk_str_proxy_revoked = "proxy revoked"; +DUK_INTERNAL const char *duk_str_invalid_base = "invalid base value"; +DUK_INTERNAL const char *duk_str_strict_caller_read = "attempt to read strict 'caller'"; +DUK_INTERNAL const char *duk_str_proxy_rejected = "proxy rejected"; +DUK_INTERNAL const char *duk_str_invalid_array_length = "invalid array length"; +DUK_INTERNAL const char *duk_str_array_length_write_failed = "array length write failed"; +DUK_INTERNAL const char *duk_str_array_length_not_writable = "array length non-writable"; +DUK_INTERNAL const char *duk_str_setter_undefined = "setter undefined"; +DUK_INTERNAL const char *duk_str_redefine_virt_prop = "attempt to redefine virtual property"; +DUK_INTERNAL const char *duk_str_invalid_descriptor = "invalid descriptor"; +DUK_INTERNAL const char *duk_str_property_is_virtual = "property is virtual"; + +/* Compiler */ +DUK_INTERNAL const char *duk_str_parse_error = "parse error"; +DUK_INTERNAL const char *duk_str_duplicate_label = "duplicate label"; +DUK_INTERNAL const char *duk_str_invalid_label = "invalid label"; +DUK_INTERNAL const char *duk_str_invalid_array_literal = "invalid array literal"; +DUK_INTERNAL const char *duk_str_invalid_object_literal = "invalid object literal"; +DUK_INTERNAL const char *duk_str_invalid_var_declaration = "invalid variable declaration"; +DUK_INTERNAL const char *duk_str_cannot_delete_identifier = "cannot delete identifier"; +DUK_INTERNAL const char *duk_str_invalid_expression = "invalid expression"; +DUK_INTERNAL const char *duk_str_invalid_lvalue = "invalid lvalue"; +DUK_INTERNAL const char *duk_str_expected_identifier = "expected identifier"; +DUK_INTERNAL const char *duk_str_empty_expr_not_allowed = "empty expression not allowed"; +DUK_INTERNAL const char *duk_str_invalid_for = "invalid for statement"; +DUK_INTERNAL const char *duk_str_invalid_switch = "invalid switch statement"; +DUK_INTERNAL const char *duk_str_invalid_break_cont_label = "invalid break/continue label"; +DUK_INTERNAL const char *duk_str_invalid_return = "invalid return"; +DUK_INTERNAL const char *duk_str_invalid_try = "invalid try"; +DUK_INTERNAL const char *duk_str_invalid_throw = "invalid throw"; +DUK_INTERNAL const char *duk_str_with_in_strict_mode = "with in strict mode"; +DUK_INTERNAL const char *duk_str_func_stmt_not_allowed = "function statement not allowed"; +DUK_INTERNAL const char *duk_str_unterminated_stmt = "unterminated statement"; +DUK_INTERNAL const char *duk_str_invalid_arg_name = "invalid argument name"; +DUK_INTERNAL const char *duk_str_invalid_func_name = "invalid function name"; +DUK_INTERNAL const char *duk_str_invalid_getset_name = "invalid getter/setter name"; +DUK_INTERNAL const char *duk_str_func_name_required = "function name required"; + +/* Regexp */ +DUK_INTERNAL const char *duk_str_invalid_quantifier_no_atom = "quantifier without preceding atom"; +DUK_INTERNAL const char *duk_str_invalid_quantifier_values = "quantifier values invalid (qmin > qmax)"; +DUK_INTERNAL const char *duk_str_quantifier_too_many_copies = "quantifier expansion requires too many atom copies"; +DUK_INTERNAL const char *duk_str_unexpected_closing_paren = "unexpected closing parenthesis"; +DUK_INTERNAL const char *duk_str_unexpected_end_of_pattern = "unexpected end of pattern"; +DUK_INTERNAL const char *duk_str_unexpected_regexp_token = "unexpected token in regexp"; +DUK_INTERNAL const char *duk_str_invalid_regexp_flags = "invalid regexp flags"; +DUK_INTERNAL const char *duk_str_invalid_backrefs = "invalid backreference(s)"; + +/* Limits */ +DUK_INTERNAL const char *duk_str_valstack_limit = "valstack limit"; +DUK_INTERNAL const char *duk_str_callstack_limit = "callstack limit"; +DUK_INTERNAL const char *duk_str_catchstack_limit = "catchstack limit"; +DUK_INTERNAL const char *duk_str_prototype_chain_limit = "prototype chain limit"; +DUK_INTERNAL const char *duk_str_bound_chain_limit = "function call bound chain limit"; +DUK_INTERNAL const char *duk_str_c_callstack_limit = "C call stack depth limit"; +DUK_INTERNAL const char *duk_str_compiler_recursion_limit = "compiler recursion limit"; +DUK_INTERNAL const char *duk_str_bytecode_limit = "bytecode limit"; +DUK_INTERNAL const char *duk_str_reg_limit = "register limit"; +DUK_INTERNAL const char *duk_str_temp_limit = "temp limit"; +DUK_INTERNAL const char *duk_str_const_limit = "const limit"; +DUK_INTERNAL const char *duk_str_func_limit = "function limit"; +DUK_INTERNAL const char *duk_str_regexp_compiler_recursion_limit = "regexp compiler recursion limit"; +DUK_INTERNAL const char *duk_str_regexp_executor_recursion_limit = "regexp executor recursion limit"; +DUK_INTERNAL const char *duk_str_regexp_executor_step_limit = "regexp step limit"; + +/* Misc */ +/* + * Debugging macro calls. + */ + +/* include removed: duk_internal.h */ + +#ifdef DUK_USE_DEBUG + +/* + * Debugging enabled + */ + +#include +#include +#include + +#define DUK__DEBUG_BUFSIZE DUK_USE_DEBUG_BUFSIZE +DUK_LOCAL char duk__debug_buf[DUK__DEBUG_BUFSIZE]; + +DUK_LOCAL const char *duk__get_level_string(duk_small_int_t level) { + switch ((int) level) { + case DUK_LEVEL_DEBUG: + return "D"; + case DUK_LEVEL_DDEBUG: + return "DD"; + case DUK_LEVEL_DDDEBUG: + return "DDD"; + } + return "???"; +} + +#ifdef DUK_USE_DPRINT_COLORS + +/* http://en.wikipedia.org/wiki/ANSI_escape_code */ +#define DUK__TERM_REVERSE "\x1b[7m" +#define DUK__TERM_BRIGHT "\x1b[1m" +#define DUK__TERM_RESET "\x1b[0m" +#define DUK__TERM_BLUE "\x1b[34m" +#define DUK__TERM_RED "\x1b[31m" + +DUK_LOCAL const char *duk__get_term_1(duk_small_int_t level) { + DUK_UNREF(level); + return (const char *) DUK__TERM_RED; +} + +DUK_LOCAL const char *duk__get_term_2(duk_small_int_t level) { + switch ((int) level) { + case DUK_LEVEL_DEBUG: + return (const char *) (DUK__TERM_RESET DUK__TERM_BRIGHT); + case DUK_LEVEL_DDEBUG: + return (const char *) (DUK__TERM_RESET); + case DUK_LEVEL_DDDEBUG: + return (const char *) (DUK__TERM_RESET DUK__TERM_BLUE); + } + return (const char *) DUK__TERM_RESET; +} + +DUK_LOCAL const char *duk__get_term_3(duk_small_int_t level) { + DUK_UNREF(level); + return (const char *) DUK__TERM_RESET; +} + +#else + +DUK_LOCAL const char *duk__get_term_1(duk_small_int_t level) { + DUK_UNREF(level); + return (const char *) ""; +} + +DUK_LOCAL const char *duk__get_term_2(duk_small_int_t level) { + DUK_UNREF(level); + return (const char *) ""; +} + +DUK_LOCAL const char *duk__get_term_3(duk_small_int_t level) { + DUK_UNREF(level); + return (const char *) ""; +} + +#endif /* DUK_USE_DPRINT_COLORS */ + +#ifdef DUK_USE_VARIADIC_MACROS + +DUK_INTERNAL void duk_debug_log(duk_small_int_t level, const char *file, duk_int_t line, const char *func, const char *fmt, ...) { + va_list ap; + + va_start(ap, fmt); + + DUK_MEMZERO((void *) duk__debug_buf, (size_t) DUK__DEBUG_BUFSIZE); + duk_debug_vsnprintf(duk__debug_buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap); + + DUK_FPRINTF(DUK_STDERR, "%s[%s] %s:%ld (%s):%s %s%s\n", + (const char *) duk__get_term_1(level), + (const char *) duk__get_level_string(level), + (const char *) file, + (long) line, + (const char *) func, + (const char *) duk__get_term_2(level), + (const char *) duk__debug_buf, + (const char *) duk__get_term_3(level)); + DUK_FFLUSH(DUK_STDERR); + + va_end(ap); +} + +#else /* DUK_USE_VARIADIC_MACROS */ + +DUK_INTERNAL char duk_debug_file_stash[DUK_DEBUG_STASH_SIZE]; +DUK_INTERNAL char duk_debug_line_stash[DUK_DEBUG_STASH_SIZE]; +DUK_INTERNAL char duk_debug_func_stash[DUK_DEBUG_STASH_SIZE]; +DUK_INTERNAL duk_small_int_t duk_debug_level_stash; + +DUK_INTERNAL void duk_debug_log(const char *fmt, ...) { + va_list ap; + duk_small_int_t level = duk_debug_level_stash; + + va_start(ap, fmt); + + DUK_MEMZERO((void *) duk__debug_buf, (size_t) DUK__DEBUG_BUFSIZE); + duk_debug_vsnprintf(duk__debug_buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap); + + DUK_FPRINTF(DUK_STDERR, "%s[%s] %s:%s (%s):%s %s%s\n", + (const char *) duk__get_term_1(level), + (const char *) duk__get_level_string(duk_debug_level_stash), + (const char *) duk_debug_file_stash, + (const char *) duk_debug_line_stash, + (const char *) duk_debug_func_stash, + (const char *) duk__get_term_2(level), + (const char *) duk__debug_buf, + (const char *) duk__get_term_3(level)); + DUK_FFLUSH(DUK_STDERR); + + va_end(ap); +} + +#endif /* DUK_USE_VARIADIC_MACROS */ + +#else /* DUK_USE_DEBUG */ + +/* + * Debugging disabled + */ + +#endif /* DUK_USE_DEBUG */ +/* + * Automatically generated by genbuiltins.py, do not edit! + */ + +/* include removed: duk_internal.h */ + +#if defined(DUK_USE_ROM_STRINGS) +#error ROM support not enabled, rerun make_dist.py with --rom-support +#else /* DUK_USE_ROM_STRINGS */ +DUK_INTERNAL const duk_uint8_t duk_strings_data[1049] = { +79,104,209,144,168,105,6,78,182,139,90,122,8,154,140,35,103,35,117,193,73, +5,52,116,180,104,166,135,52,189,4,98,12,27,178,156,80,211,31,161,115,150, +64,52,221,109,24,18,68,157,24,38,67,118,36,55,73,119,151,164,140,93,18,117, +128,153,201,228,201,205,2,250,8,196,24,232,104,82,146,40,232,193,48,118, +168,37,147,212,54,127,113,208,70,32,194,187,68,54,127,113,208,70,32,196, +123,68,54,127,113,209,44,12,121,7,208,70,32,194,186,134,207,236,126,219, +160,140,65,133,246,136,108,254,199,237,186,8,196,24,87,80,217,253,159,217, +116,17,136,48,190,209,13,159,217,253,151,65,24,131,12,233,86,224,79,236, +254,203,160,140,65,134,116,171,112,39,246,223,105,208,70,32,193,140,183,4, +11,55,92,20,244,141,169,186,50,11,164,109,77,208,208,165,36,79,215,185,13, +153,34,110,204,241,32,6,66,84,11,112,200,84,52,157,124,92,242,70,120,45,64, +186,17,22,138,38,0,172,140,19,154,84,26,145,0,86,69,17,180,97,34,0,172,132, +75,144,215,77,221,91,132,5,147,178,156,80,211,30,160,93,9,215,21,115,119, +169,49,75,211,138,26,101,205,222,68,157,47,78,40,105,151,55,120,204,156, +189,56,161,166,52,157,72,136,138,65,154,232,147,162,4,136,150,81,115,66, +208,210,37,96,148,250,134,140,151,39,212,125,255,221,125,73,80,209,146,233, +124,93,55,79,15,34,196,230,202,113,160,166,232,157,132,148,128,98,28,46, +114,200,6,153,180,96,73,19,74,113,67,76,103,5,36,20,211,70,140,133,67,72, +49,245,160,235,81,212,52,168,106,39,132,253,111,80,210,161,168,158,5,245, +191,96,31,172,15,208,23,226,190,131,232,62,131,232,11,251,127,93,245,223, +93,251,172,234,27,80,45,3,250,14,140,19,34,65,19,81,132,108,228,97,1,107, +33,12,32,45,100,136,206,9,12,196,155,134,69,146,100,235,226,231,146,51,194, +72,218,48,145,4,200,119,89,189,81,49,39,72,147,235,226,233,186,120,121,58, +226,167,90,124,93,55,107,71,137,33,68,68,130,64,206,75,189,209,156,144,84, +44,141,3,8,137,187,178,156,80,211,26,110,242,100,230,146,120,121,8,48,76,6, +89,26,105,157,65,196,201,213,145,166,153,212,28,76,157,113,75,34,78,62,14, +38,73,105,228,142,136,178,48,141,152,228,73,150,83,0,148,39,137,75,67,73, +214,209,129,36,85,190,206,32,17,6,9,128,141,3,8,130,161,100,235,64,194,24, +52,41,73,19,189,200,108,201,19,111,181,2,232,66,239,173,37,230,157,244,56, +153,4,225,145,27,233,93,22,1,114,62,251,80,69,128,121,247,213,146,228,109, +79,190,212,17,35,106,125,246,78,164,68,68,111,175,23,217,45,13,33,119,208, +68,210,38,250,192,61,91,233,80,208,45,25,36,81,190,156,13,26,201,19,239, +162,2,214,66,31,125,153,226,64,13,27,236,72,96,130,68,62,251,48,68,196,153, +119,217,157,18,56,156,199,161,100,42,26,250,77,36,140,122,40,144,19,34,9, +24,246,103,139,172,150,56,125,145,1,17,29,44,112,250,183,0,100,24,200,218, +140,228,185,130,9,19,237,190,208,73,184,146,35,68,146,163,8,50,178,99,136, +44,89,196,2,33,70,64,208,196,67,74,226,88,17,105,73,24,186,37,40,38,5,133, +161,89,4,183,25,115,119,86,227,118,83,138,26,103,255,223,209,106,141,25,11, +244,95,117,56,208,159,250,223,251,250,45,52,13,250,47,186,156,104,79,253, +111,253,253,22,144,210,253,23,221,78,52,39,254,187,254,254,139,77,67,75, +244,95,117,56,208,159,250,239,251,250,45,22,141,23,209,125,212,227,66,127, +235,63,239,69,163,69,247,83,141,9,255,165,12,72,5,16,64,145,10,32,76,71,64, +156,217,161,180,34,6,64,208,198,36,78,50,20,20,92,204,50,44,147,32,134,226, +17,114,33,202,134,129,107,192,202,232,160,180,104,166,135,52,72,40,144,213, +33,178,152,26,34,56,163,105,44,104,146,116,139,77,43,34,98,57,38,116,72, +179,60,93,97,206,56,52,240,242,56,163,168,34,81,57,178,153,42,228,12,182, +58,22,66,89,19,57,68,176,74,68,35,104,195,18,239,116,102,114,94,100,104, +228,100,49,238,140,203,42,60,145,35,104,181,146,113,161,10,80,46,68,82,24, +245,145,132,108,228,148,54,100,137,64,34,13,100,153,222,1,40,6,33,223,20, +84,19,34,95,23,76,130,153,6,103,208,43,64,141,41,130,104,17,112,130,44,96, +}; +#endif /* DUK_USE_ROM_STRINGS */ + +#if defined(DUK_USE_ROM_OBJECTS) +#error ROM support not enabled, rerun make_dist.py with --rom-support +#else /* DUK_USE_ROM_OBJECTS */ +/* native functions: 149 */ +DUK_INTERNAL const duk_c_function duk_bi_native_functions[149] = { + duk_bi_array_constructor, + duk_bi_array_constructor_is_array, + duk_bi_array_prototype_concat, + duk_bi_array_prototype_indexof_shared, + duk_bi_array_prototype_iter_shared, + duk_bi_array_prototype_join_shared, + duk_bi_array_prototype_pop, + duk_bi_array_prototype_push, + duk_bi_array_prototype_reduce_shared, + duk_bi_array_prototype_reverse, + duk_bi_array_prototype_shift, + duk_bi_array_prototype_slice, + duk_bi_array_prototype_sort, + duk_bi_array_prototype_splice, + duk_bi_array_prototype_to_string, + duk_bi_array_prototype_unshift, + duk_bi_arraybuffer_constructor, + duk_bi_arraybuffer_isview, + duk_bi_boolean_constructor, + duk_bi_boolean_prototype_tostring_shared, + duk_bi_buffer_compare_shared, + duk_bi_buffer_constructor, + duk_bi_buffer_prototype_tostring_shared, + duk_bi_buffer_readfield, + duk_bi_buffer_slice_shared, + duk_bi_buffer_writefield, + duk_bi_dataview_constructor, + duk_bi_date_constructor, + duk_bi_date_constructor_now, + duk_bi_date_constructor_parse, + duk_bi_date_constructor_utc, + duk_bi_date_prototype_get_shared, + duk_bi_date_prototype_get_timezone_offset, + duk_bi_date_prototype_set_shared, + duk_bi_date_prototype_set_time, + duk_bi_date_prototype_to_json, + duk_bi_date_prototype_tostring_shared, + duk_bi_date_prototype_value_of, + duk_bi_duktape_object_act, + duk_bi_duktape_object_compact, + duk_bi_duktape_object_dec, + duk_bi_duktape_object_enc, + duk_bi_duktape_object_fin, + duk_bi_duktape_object_gc, + duk_bi_duktape_object_info, + duk_bi_error_constructor_shared, + duk_bi_error_prototype_filename_getter, + duk_bi_error_prototype_filename_setter, + duk_bi_error_prototype_linenumber_getter, + duk_bi_error_prototype_linenumber_setter, + duk_bi_error_prototype_stack_getter, + duk_bi_error_prototype_stack_setter, + duk_bi_error_prototype_to_string, + duk_bi_function_constructor, + duk_bi_function_prototype, + duk_bi_function_prototype_apply, + duk_bi_function_prototype_bind, + duk_bi_function_prototype_call, + duk_bi_function_prototype_to_string, + duk_bi_global_object_decode_uri, + duk_bi_global_object_decode_uri_component, + duk_bi_global_object_encode_uri, + duk_bi_global_object_encode_uri_component, + duk_bi_global_object_escape, + duk_bi_global_object_eval, + duk_bi_global_object_is_finite, + duk_bi_global_object_is_nan, + duk_bi_global_object_parse_float, + duk_bi_global_object_parse_int, + duk_bi_global_object_print_helper, + duk_bi_global_object_require, + duk_bi_global_object_unescape, + duk_bi_json_object_parse, + duk_bi_json_object_stringify, + duk_bi_logger_constructor, + duk_bi_logger_prototype_fmt, + duk_bi_logger_prototype_log_shared, + duk_bi_logger_prototype_raw, + duk_bi_math_object_max, + duk_bi_math_object_min, + duk_bi_math_object_onearg_shared, + duk_bi_math_object_random, + duk_bi_math_object_twoarg_shared, + duk_bi_nodejs_buffer_byte_length, + duk_bi_nodejs_buffer_concat, + duk_bi_nodejs_buffer_constructor, + duk_bi_nodejs_buffer_copy, + duk_bi_nodejs_buffer_fill, + duk_bi_nodejs_buffer_is_buffer, + duk_bi_nodejs_buffer_is_encoding, + duk_bi_nodejs_buffer_tojson, + duk_bi_nodejs_buffer_tostring, + duk_bi_nodejs_buffer_write, + duk_bi_number_constructor, + duk_bi_number_prototype_to_exponential, + duk_bi_number_prototype_to_fixed, + duk_bi_number_prototype_to_locale_string, + duk_bi_number_prototype_to_precision, + duk_bi_number_prototype_to_string, + duk_bi_number_prototype_value_of, + duk_bi_object_constructor, + duk_bi_object_constructor_create, + duk_bi_object_constructor_define_properties, + duk_bi_object_constructor_define_property, + duk_bi_object_constructor_get_own_property_descriptor, + duk_bi_object_constructor_is_extensible, + duk_bi_object_constructor_is_sealed_frozen_shared, + duk_bi_object_constructor_keys_shared, + duk_bi_object_constructor_prevent_extensions, + duk_bi_object_constructor_seal_freeze_shared, + duk_bi_object_getprototype_shared, + duk_bi_object_prototype_has_own_property, + duk_bi_object_prototype_is_prototype_of, + duk_bi_object_prototype_property_is_enumerable, + duk_bi_object_prototype_to_locale_string, + duk_bi_object_prototype_to_string, + duk_bi_object_prototype_value_of, + duk_bi_object_setprototype_shared, + duk_bi_pointer_constructor, + duk_bi_pointer_prototype_tostring_shared, + duk_bi_proxy_constructor, + duk_bi_regexp_constructor, + duk_bi_regexp_prototype_exec, + duk_bi_regexp_prototype_test, + duk_bi_regexp_prototype_to_string, + duk_bi_string_constructor, + duk_bi_string_constructor_from_char_code, + duk_bi_string_prototype_caseconv_shared, + duk_bi_string_prototype_char_at, + duk_bi_string_prototype_char_code_at, + duk_bi_string_prototype_concat, + duk_bi_string_prototype_indexof_shared, + duk_bi_string_prototype_locale_compare, + duk_bi_string_prototype_match, + duk_bi_string_prototype_replace, + duk_bi_string_prototype_search, + duk_bi_string_prototype_slice, + duk_bi_string_prototype_split, + duk_bi_string_prototype_substr, + duk_bi_string_prototype_substring, + duk_bi_string_prototype_to_string, + duk_bi_string_prototype_trim, + duk_bi_thread_constructor, + duk_bi_thread_current, + duk_bi_thread_resume, + duk_bi_thread_yield, + duk_bi_type_error_thrower, + duk_bi_typedarray_constructor, + duk_bi_typedarray_set, +}; +#if defined(DUK_USE_BUILTIN_INITJS) +DUK_INTERNAL const duk_uint8_t duk_initjs_data[204] = { +40,102,117,110,99,116,105,111,110,40,100,44,97,41,123,102,117,110,99,116, +105,111,110,32,98,40,97,44,98,44,99,41,123,79,98,106,101,99,116,46,100,101, +102,105,110,101,80,114,111,112,101,114,116,121,40,97,44,98,44,123,118,97, +108,117,101,58,99,44,119,114,105,116,97,98,108,101,58,33,48,44,101,110,117, +109,101,114,97,98,108,101,58,33,49,44,99,111,110,102,105,103,117,114,97,98, +108,101,58,33,48,125,41,125,98,40,97,46,76,111,103,103,101,114,44,34,99, +108,111,103,34,44,110,101,119,32,97,46,76,111,103,103,101,114,40,34,67,34, +41,41,59,98,40,97,44,34,109,111,100,76,111,97,100,101,100,34,44,79,98,106, +101,99,116,46,99,114,101,97,116,101,40,110,117,108,108,41,41,125,41,40,116, +104,105,115,44,68,117,107,116,97,112,101,41,59,10,0, +}; +#endif /* DUK_USE_BUILTIN_INITJS */ +#if defined(DUK_USE_DOUBLE_LE) +DUK_INTERNAL const duk_uint8_t duk_builtins_data[3833] = { +105,195,75,32,3,148,52,154,248,9,26,13,128,112,105,0,240,22,20,26,95,124,6, +152,52,137,0,120,99,74,239,129,18,70,241,191,2,98,13,79,32,42,88,210,90,2, +240,1,50,141,37,168,76,94,216,118,69,229,203,127,44,0,84,163,73,106,21,75, +14,236,249,98,242,229,191,150,0,46,81,164,181,14,165,151,54,94,89,119,99, +203,23,151,45,252,176,1,146,141,37,168,93,63,59,186,97,241,23,151,45,252, +176,1,178,141,37,168,77,79,60,50,197,229,203,127,44,0,116,163,73,106,17,86, +148,152,188,185,111,229,128,15,148,129,198,137,36,58,166,142,91,251,212, +243,195,44,94,92,183,242,13,79,8,45,14,91,252,121,148,52,199,120,63,72,105, +21,240,118,128,210,237,224,245,17,165,43,224,211,55,231,207,151,148,161,70, +145,0,31,40,107,26,2,18,138,26,228,192,142,0,16,161,174,76,9,74,26,228,192, +158,0,8,161,174,76,10,96,2,42,26,228,192,174,0,26,161,174,76,11,96,3,74,26, +228,192,190,0,44,161,174,76,12,96,3,202,26,228,192,206,0,70,161,169,84,14, +202,3,255,254,32,234,0,0,0,0,0,0,7,195,248,119,0,0,0,0,0,0,3,193,252,57, +136,1,152,32,16,194,0,166,24,6,49,0,57,138,2,12,96,18,99,128,163,32,5,153, +40,76,94,216,118,69,229,203,127,35,41,10,165,135,118,124,177,121,114,223, +200,203,67,169,101,205,151,150,93,216,242,197,229,203,127,35,49,11,167,231, +119,76,62,34,242,229,191,145,154,132,212,243,195,44,94,92,183,242,51,144, +138,180,164,197,229,203,127,35,60,6,26,0,52,208,193,226,117,215,211,15,12, +166,146,11,67,150,255,30,77,24,58,113,64,243,92,8,27,0,68,217,130,70,212, +19,54,224,161,185,5,77,216,44,111,65,115,126,12,28,16,100,225,156,16,32,18, +17,195,15,46,121,100,238,232,136,136,87,12,60,185,229,141,179,126,30,136, +100,130,233,231,59,12,228,34,66,52,243,141,167,118,158,153,80,73,9,201,151, +30,252,153,106,210,146,118,72,150,76,184,247,228,203,86,148,152,123,246, +240,223,187,46,238,135,132,132,229,221,143,126,76,181,105,73,61,36,75,46, +236,123,242,101,171,74,76,61,251,120,111,221,151,119,67,226,65,178,243,199, +135,134,83,242,66,58,238,203,207,30,30,25,81,201,5,225,203,78,238,136,163, +208,92,59,50,242,232,138,62,0,2,38,163,19,255,255,224,142,80,192,0,20,31, +240,14,135,103,203,210,135,45,253,55,244,243,195,44,252,205,197,0,1,18,221, +82,0,3,24,207,151,164,254,251,168,114,223,195,47,46,158,98,101,231,143,150, +158,29,55,242,104,68,79,62,94,147,251,238,161,203,127,12,188,186,121,157, +135,110,94,109,100,131,99,229,151,15,76,172,168,8,89,217,16,201,151,54,157, +217,104,114,223,195,47,46,154,114,243,102,68,19,158,92,59,27,73,6,205,203, +46,95,89,91,74,0,3,17,225,203,47,108,187,186,69,241,211,46,238,122,119,238, +230,216,72,70,158,116,242,225,217,151,35,81,33,26,121,198,229,191,214,93, +205,69,0,1,134,105,231,23,199,76,187,185,233,197,179,43,73,32,154,242,249, +230,214,80,0,31,255,193,2,38,103,110,117,24,81,115,0,78,228,0,161,208,16, +237,24,121,207,239,186,135,45,252,50,242,233,229,188,144,221,60,232,114, +223,211,127,79,60,50,207,204,224,72,167,14,91,248,101,229,211,204,158,113, +119,117,219,151,150,28,91,50,184,144,40,95,224,0,15,248,64,4,20,78,129,5, +195,195,134,207,38,232,130,99,195,179,97,201,244,19,22,157,217,14,15,130, +135,254,0,48,125,60,224,242,229,135,200,9,1,255,12,2,162,136,112,2,112,80, +128,0,193,177,239,221,143,15,64,35,224,152,20,144,62,27,248,3,2,9,195,175, +61,0,231,208,126,89,123,101,229,207,40,72,32,188,244,105,205,208,40,16,94, +123,52,227,202,22,136,39,61,252,186,6,18,13,207,134,205,56,242,134,175,65, +250,238,231,163,78,110,129,231,208,125,59,178,101,241,63,48,25,248,0,12,47, +102,30,125,36,238,201,151,196,252,192,103,255,255,240,92,189,178,242,242,8, +105,4,231,191,110,80,67,80,0,24,62,109,252,162,225,199,160,16,212,0,10,7, +183,15,0,67,80,0,56,54,109,59,58,101,228,8,106,0,9,6,229,151,39,92,121,66, +15,192,0,97,124,178,228,235,143,45,45,57,244,116,8,63,255,255,10,39,248,0, +195,51,114,223,182,30,140,60,161,239,201,149,248,248,31,241,0,140,80,129, +202,10,49,128,10,35,1,6,199,163,15,40,61,32,9,10,199,163,15,40,123,242,101, +131,210,4,144,108,123,247,99,195,210,8,250,15,167,118,76,190,39,230,131,52, +133,236,195,207,164,157,217,50,248,159,154,12,212,0,6,27,179,126,60,59,50, +195,223,183,134,30,89,97,9,5,219,135,166,61,16,164,131,242,203,195,102,28, +121,97,145,6,231,151,15,44,122,33,201,5,231,179,78,60,177,8,130,243,225, +179,79,72,148,66,121,245,197,207,167,45,59,179,197,162,23,211,124,205,253, +242,242,135,135,158,87,240,68,122,111,153,191,30,29,153,102,111,239,151, +148,60,60,242,191,130,23,211,125,94,28,50,242,135,135,158,87,240,128,0,196, +122,111,153,191,30,29,153,106,240,225,151,148,60,60,242,191,132,0,6,9,211, +150,157,177,160,131,115,235,139,159,78,81,72,10,47,248,0,3,254,40,17,138, +48,66,136,152,64,0,66,129,48,5,27,252,88,76,216,54,47,214,131,50,172,88,31, +255,255,255,255,255,253,239,240,153,178,103,95,173,6,101,88,176,0,64,0,0,0, +0,0,0,3,168,0,0,0,0,0,0,31,15,241,26,19,233,201,169,38,180,91,242,103,70, +147,58,77,75,48,0,0,0,0,0,0,60,31,226,51,162,199,131,82,77,104,183,228,206, +141,38,116,154,150,96,0,0,0,0,0,0,120,127,128,15,248,192,70,40,0,0,0,0,0,0, +0,0,3,10,44,68,9,216,8,20,49,130,15,211,124,109,62,50,228,95,36,55,166,248, +190,56,111,221,151,119,77,56,118,47,18,23,211,125,14,89,113,233,231,167, +126,230,18,5,31,252,0,224,188,48,242,231,148,116,144,58,181,33,143,127,64, +247,111,238,56,0,127,199,2,49,72,0,0,0,0,0,0,248,127,180,81,36,4,51,166, +248,152,122,101,167,211,150,157,217,201,2,0,3,12,233,190,166,157,185,105, +244,229,167,118,114,64,128,1,4,228,129,0,3,137,116,223,51,126,60,59,50,196, +195,211,45,62,156,180,238,206,72,16,0,72,151,77,243,55,227,195,179,45,77, +59,114,211,233,203,78,236,228,129,0,5,10,73,2,0,12,21,18,4,0,28,82,35,32, +80,74,8,62,124,189,42,105,219,148,148,16,188,249,122,70,235,179,101,156, +184,121,15,132,0,34,29,159,47,74,181,33,198,235,179,101,156,184,121,15,132, +0,38,17,159,47,73,187,247,116,208,62,16,0,168,94,124,189,42,212,135,55,126, +238,154,7,194,0,23,7,207,151,164,76,61,50,143,132,0,50,21,159,47,74,181,33, +196,195,211,40,248,64,3,96,217,242,244,137,135,200,248,64,3,161,57,242,244, +171,82,28,76,62,71,194,0,31,8,207,151,164,141,253,121,115,31,8,0,132,47,62, +94,149,106,67,145,191,175,46,99,225,0,17,133,103,203,210,110,157,221,122, +101,230,62,16,1,40,110,124,189,42,212,135,55,78,238,189,50,243,31,8,0,156, +43,62,94,148,242,227,223,187,39,49,240,128,10,67,115,229,233,86,164,58,121, +113,239,221,147,152,248,64,5,97,249,242,244,155,167,102,205,60,242,227,223, +187,39,49,240,128,11,68,179,229,233,86,164,57,186,118,108,211,207,46,61, +251,178,115,31,8,0,188,71,62,94,149,52,237,203,235,126,236,179,243,102,231, +151,161,0,32,252,242,244,169,167,110,82,34,67,249,229,233,55,78,205,154, +121,229,199,191,118,78,100,37,0,24,137,115,203,210,173,72,115,116,236,217, +167,158,92,123,247,100,230,66,80,1,152,87,60,189,41,229,199,191,118,78,100, +43,224,3,80,222,121,122,85,169,14,158,92,123,247,100,230,66,190,0,55,10, +231,151,164,221,59,186,244,203,204,133,252,0,114,27,207,47,74,181,33,205, +211,187,175,76,188,200,95,192,7,97,28,242,244,145,191,175,46,100,51,224,3, +208,190,121,122,85,169,14,70,254,188,185,144,207,128,15,193,249,229,233,19, +15,76,164,37,0,32,133,115,203,210,173,72,113,48,244,202,66,80,2,24,71,60, +189,38,239,221,211,65,10,248,1,20,47,158,94,149,106,67,155,191,119,77,4,43, +224,4,112,190,121,122,70,235,179,101,156,184,121,16,191,128,18,67,185,229, +233,86,164,56,221,118,108,179,151,15,34,23,240,2,88,62,124,189,44,229,195, +200,124,32,4,208,126,121,122,89,203,135,145,9,64,9,194,145,254,0,0,255,144, +24,100,130,14,0,16,176,2,192,129,11,33,12,1,168,193,108,96,186,48,95,32,0, +0,0,0,0,0,0,0,56,38,95,25,113,189,18,9,211,47,62,143,100,20,95,0,20,159, +240,0,7,252,144,162,241,2,195,66,7,11,89,204,140,197,252,229,197,226,230, +115,3,16,69,19,64,5,43,252,0,9,255,40,16,188,33,49,123,97,217,23,151,45, +252,131,66,7,0,20,191,240,0,39,252,176,66,240,133,82,195,187,62,88,188,185, +111,228,26,16,56,0,166,127,128,1,63,230,2,23,132,58,150,92,217,121,101,221, +143,44,94,92,183,242,13,8,28,0,83,127,192,0,159,243,65,11,194,23,79,206, +238,152,124,69,229,203,127,32,208,129,192,5,59,252,0,9,255,56,16,188,33,53, +60,240,203,23,151,45,252,131,66,7,0,20,255,240,0,39,252,240,66,240,132,85, +165,38,47,46,91,249,6,132,14,0,31,255,228,64,98,192,105,87,20,139,10,191,5, +64,130,76,156,197,132,1,101,91,91,187,22,176,36,8,28,201,204,160,119,156, +253,127,33,23,115,31,193,102,79,142,202,44,15,232,34,182,84,113,95,115,248, +52,201,241,216,176,139,0,59,148,152,85,239,47,108,254,5,66,76,1,130,212,69, +79,178,16,148,8,61,58,52,170,49,190,202,6,105,219,251,52,245,7,49,252,22, +157,26,85,25,64,205,59,127,102,158,160,246,63,74,7,135,23,53,2,65,48,227, +223,205,64,160,0,48,76,60,244,238,80,40,0,20,19,15,76,59,148,10,0,7,5,195, +211,14,230,74,72,130,99,203,167,98,129,64,1,32,120,247,243,80,40,0,44,15, +47,142,10,5,0,6,130,230,217,191,127,37,2,128,3,192,246,111,206,160,80,0, +136,30,220,62,19,151,160,123,116,238,79,94,129,240,223,221,73,32,0,48,110, +88,119,100,223,181,68,16,94,91,250,238,200,160,80,0,152,31,61,59,148,10,0, +21,4,231,199,151,69,2,128,5,192,250,97,220,160,80,0,192,127,255,128,20,23, +134,30,92,242,164,34,19,207,167,45,59,179,233,205,229,37,129,127,255,0,0, +191,255,128,0,63,255,197,131,246,203,203,158,157,251,160,0,0,0,0,0,65,226, +32,3,166,156,30,53,32,249,165,131,76,223,159,62,94,70,172,114,16,176,144, +60,56,250,19,18,5,159,25,89,32,121,180,238,42,30,129,229,221,140,164,122,7, +147,46,50,129,232,62,61,251,120,97,199,208,156,129,83,127,0,50,250,69,3, +252,131,32,248,250,242,229,151,119,72,240,3,254,148,0,2,168,254,0,0,255, +167,0,33,68,88,32,0,33,64,176,2,170,254,0,0,255,169,0,33,69,220,32,0,33,67, +184,2,172,254,0,0,255,171,8,137,144,0,0,0,0,0,0,0,128,68,73,4,195,187,126, +226,8,4,178,16,41,164,32,147,7,136,52,193,240,0,18,17,48,124,0,8,133,76,31, +0,3,33,147,7,192,1,8,116,193,240,0,82,127,255,132,47,65,11,137,191,174,45, +153,98,242,229,191,144,105,4,95,47,46,91,249,32,211,185,6,94,92,183,242,65, +163,14,236,155,52,238,206,0,85,255,192,6,13,167,157,109,57,123,136,144,31, +245,192,3,5,231,179,78,60,163,9,0,2,10,199,248,0,3,254,192,4,32,249,242, +244,147,187,163,129,116,128,24,66,51,229,233,87,78,238,142,5,210,0,65,8, +207,151,164,157,221,24,182,23,72,1,140,39,62,94,149,116,238,232,197,176, +186,64,8,97,25,242,244,147,187,163,54,66,233,0,50,132,231,203,210,174,157, +221,25,178,23,72,1,20,43,62,94,145,182,111,195,209,155,33,116,128,17,194, +179,229,233,27,102,252,61,27,52,23,72,1,36,31,158,94,146,119,116,112,50, +208,3,8,71,60,189,42,233,221,209,192,203,64,8,33,28,242,244,147,187,163,22, +195,45,0,49,132,243,203,210,174,157,221,24,182,25,104,1,12,35,158,94,146, +119,116,102,200,101,160,6,80,158,121,122,85,211,187,163,54,67,45,0,34,133, +115,203,210,54,205,248,122,51,100,50,208,2,56,87,60,189,35,108,223,135,163, +102,131,45,0,36,7,255,248,1,11,50,136,132,115,235,139,15,46,88,124,140,36, +0,4,43,79,224,139,16,0,0,0,0,0,0,60,15,192,101,253,152,0,5,109,252,17,98,0, +0,0,0,0,0,7,129,248,12,191,181,0,0,174,63,130,44,64,0,0,0,0,0,0,240,63,1, +151,246,224,0,21,215,240,69,136,0,0,0,0,0,0,0,8,0,50,254,228,0,2,188,254,8, +177,0,0,0,0,0,0,0,1,0,6,95,221,128,0,87,223,193,22,32,0,0,0,0,0,0,8,32,0, +203,251,208,0,11,3,248,34,196,0,0,0,0,0,0,1,4,0,25,127,126,0,1,97,127,4,88, +128,0,0,0,0,0,0,32,128,3,47,240,64,0,44,79,224,139,16,0,0,0,0,0,0,8,16,0, +101,254,24,0,5,141,252,1,96,216,247,238,199,135,162,162,33,90,121,197,221, +143,126,77,59,179,172,146,17,167,156,46,185,179,101,228,176,65,89,77,16, +124,123,246,240,195,203,40,162,64,0,193,255,138,5,144,158,89,112,228,171, +39,119,71,2,232,132,114,203,135,36,157,221,28,11,164,0,66,25,203,46,28,149, +100,238,232,197,180,200,162,233,0,1,134,114,203,135,37,89,59,186,49,109,10, +40,186,64,2,97,124,178,225,201,39,119,70,45,166,69,23,72,0,140,47,150,92, +57,36,238,232,197,180,40,162,233,0,25,134,114,203,135,37,89,59,186,51,101, +50,40,186,64,0,161,156,178,225,201,86,78,238,140,217,66,138,46,144,0,168, +95,44,184,114,73,221,209,155,41,145,69,210,0,37,11,229,151,14,73,59,186,51, +101,10,40,186,64,6,161,124,178,225,201,27,102,252,61,38,69,23,72,0,28,47, +150,92,57,35,108,223,135,164,40,162,233,0,11,134,114,203,135,36,77,253,113, +108,203,50,40,186,64,1,33,156,178,225,201,19,127,92,91,50,194,138,46,144,0, +200,87,44,184,114,85,147,187,164,200,162,237,0,5,133,114,203,135,37,89,59, +186,66,138,46,208,0,216,79,44,184,114,73,221,210,100,81,118,128,10,194,121, +101,195,146,78,238,144,162,139,180,0,118,21,223,150,158,153,106,201,221, +209,192,203,33,61,249,105,233,150,78,238,142,6,90,0,33,13,239,203,79,76, +181,100,238,232,197,180,200,163,45,0,1,134,247,229,167,166,90,178,119,116, +98,218,20,81,150,128,4,195,59,242,211,211,44,157,221,24,182,153,20,101,160, +2,48,206,252,180,244,203,39,119,70,45,161,69,25,104,0,204,55,191,45,61,50, +213,147,187,163,54,83,34,140,180,0,10,27,223,150,158,153,106,201,221,209, +155,40,81,70,90,0,21,12,239,203,79,76,178,119,116,102,202,100,81,150,128,9, +67,59,242,211,211,44,157,221,25,178,133,20,101,160,3,80,206,252,180,244, +203,27,102,252,61,38,69,25,104,0,28,51,191,45,61,50,198,217,191,15,72,81, +70,90,0,23,13,239,203,79,76,177,55,245,197,179,44,200,163,45,0,4,134,247, +229,167,166,88,155,250,226,217,150,20,81,150,128,6,66,251,242,211,211,45, +89,59,186,76,138,51,16,0,88,95,126,90,122,101,171,39,119,72,81,70,98,0,27, +10,239,203,79,76,178,119,116,153,20,102,32,2,176,174,252,180,244,203,39, +119,72,81,70,98,0,58,40,173,176,82,90,4,19,54,157,155,21,217,6,203,199,174, +29,156,197,9,7,199,191,111,12,60,178,138,20,0,6,9,143,127,15,42,208,130, +243,217,167,30,81,132,65,123,242,211,211,42,228,0, +}; +#elif defined(DUK_USE_DOUBLE_BE) +DUK_INTERNAL const duk_uint8_t duk_builtins_data[3833] = { +105,195,75,32,3,148,52,154,248,9,26,13,128,112,105,0,240,22,20,26,95,124,6, +152,52,137,0,120,99,74,239,129,18,70,241,191,2,98,13,79,32,42,88,210,90,2, +240,1,50,141,37,168,76,94,216,118,69,229,203,127,44,0,84,163,73,106,21,75, +14,236,249,98,242,229,191,150,0,46,81,164,181,14,165,151,54,94,89,119,99, +203,23,151,45,252,176,1,146,141,37,168,93,63,59,186,97,241,23,151,45,252, +176,1,178,141,37,168,77,79,60,50,197,229,203,127,44,0,116,163,73,106,17,86, +148,152,188,185,111,229,128,15,148,129,198,137,36,58,166,142,91,251,212, +243,195,44,94,92,183,242,13,79,8,45,14,91,252,121,148,52,199,120,63,72,105, +21,240,118,128,210,237,224,245,17,165,43,224,211,55,231,207,151,148,161,70, +145,0,31,40,107,26,2,18,138,26,228,192,142,0,16,161,174,76,9,74,26,228,192, +158,0,8,161,174,76,10,96,2,42,26,228,192,174,0,26,161,174,76,11,96,3,74,26, +228,192,190,0,44,161,174,76,12,96,3,202,26,228,192,206,0,70,161,169,84,14, +202,3,255,254,32,234,3,255,192,0,0,0,0,0,0,119,1,255,192,0,0,0,0,0,0,57, +136,1,152,32,16,194,0,166,24,6,49,0,57,138,2,12,96,18,99,128,163,32,5,153, +40,76,94,216,118,69,229,203,127,35,41,10,165,135,118,124,177,121,114,223, +200,203,67,169,101,205,151,150,93,216,242,197,229,203,127,35,49,11,167,231, +119,76,62,34,242,229,191,145,154,132,212,243,195,44,94,92,183,242,51,144, +138,180,164,197,229,203,127,35,60,6,26,0,52,208,193,226,117,215,211,15,12, +166,146,11,67,150,255,30,77,24,58,113,64,243,92,8,27,0,68,217,130,70,212, +19,54,224,161,185,5,77,216,44,111,65,115,126,12,28,16,100,225,156,16,32,18, +17,195,15,46,121,100,238,232,136,136,87,12,60,185,229,141,179,126,30,136, +100,130,233,231,59,12,228,34,66,52,243,141,167,118,158,153,80,73,9,201,151, +30,252,153,106,210,146,118,72,150,76,184,247,228,203,86,148,152,123,246, +240,223,187,46,238,135,132,132,229,221,143,126,76,181,105,73,61,36,75,46, +236,123,242,101,171,74,76,61,251,120,111,221,151,119,67,226,65,178,243,199, +135,134,83,242,66,58,238,203,207,30,30,25,81,201,5,225,203,78,238,136,163, +208,92,59,50,242,232,138,62,0,2,38,163,19,255,255,224,142,80,192,0,20,31, +240,14,135,103,203,210,135,45,253,55,244,243,195,44,252,205,197,0,1,18,221, +82,0,3,24,207,151,164,254,251,168,114,223,195,47,46,158,98,101,231,143,150, +158,29,55,242,104,68,79,62,94,147,251,238,161,203,127,12,188,186,121,157, +135,110,94,109,100,131,99,229,151,15,76,172,168,8,89,217,16,201,151,54,157, +217,104,114,223,195,47,46,154,114,243,102,68,19,158,92,59,27,73,6,205,203, +46,95,89,91,74,0,3,17,225,203,47,108,187,186,69,241,211,46,238,122,119,238, +230,216,72,70,158,116,242,225,217,151,35,81,33,26,121,198,229,191,214,93, +205,69,0,1,134,105,231,23,199,76,187,185,233,197,179,43,73,32,154,242,249, +230,214,80,0,31,255,193,2,38,103,110,117,24,81,115,0,78,228,0,161,208,16, +237,24,121,207,239,186,135,45,252,50,242,233,229,188,144,221,60,232,114, +223,211,127,79,60,50,207,204,224,72,167,14,91,248,101,229,211,204,158,113, +119,117,219,151,150,28,91,50,184,144,40,95,224,0,15,248,64,4,20,78,129,5, +195,195,134,207,38,232,130,99,195,179,97,201,244,19,22,157,217,14,15,130, +135,254,0,48,125,60,224,242,229,135,200,9,1,255,12,2,162,136,112,2,112,80, +128,0,193,177,239,221,143,15,64,35,224,152,20,144,62,27,248,3,2,9,195,175, +61,0,231,208,126,89,123,101,229,207,40,72,32,188,244,105,205,208,40,16,94, +123,52,227,202,22,136,39,61,252,186,6,18,13,207,134,205,56,242,134,175,65, +250,238,231,163,78,110,129,231,208,125,59,178,101,241,63,48,25,248,0,12,47, +102,30,125,36,238,201,151,196,252,192,103,255,255,240,92,189,178,242,242,8, +105,4,231,191,110,80,67,80,0,24,62,109,252,162,225,199,160,16,212,0,10,7, +183,15,0,67,80,0,56,54,109,59,58,101,228,8,106,0,9,6,229,151,39,92,121,66, +15,192,0,97,124,178,228,235,143,45,45,57,244,116,8,63,255,255,10,39,248,0, +195,51,114,223,182,30,140,60,161,239,201,149,248,248,31,241,0,140,80,129, +202,10,49,128,10,35,1,6,199,163,15,40,61,32,9,10,199,163,15,40,123,242,101, +131,210,4,144,108,123,247,99,195,210,8,250,15,167,118,76,190,39,230,131,52, +133,236,195,207,164,157,217,50,248,159,154,12,212,0,6,27,179,126,60,59,50, +195,223,183,134,30,89,97,9,5,219,135,166,61,16,164,131,242,203,195,102,28, +121,97,145,6,231,151,15,44,122,33,201,5,231,179,78,60,177,8,130,243,225, +179,79,72,148,66,121,245,197,207,167,45,59,179,197,162,23,211,124,205,253, +242,242,135,135,158,87,240,68,122,111,153,191,30,29,153,102,111,239,151, +148,60,60,242,191,130,23,211,125,94,28,50,242,135,135,158,87,240,128,0,196, +122,111,153,191,30,29,153,106,240,225,151,148,60,60,242,191,132,0,6,9,211, +150,157,177,160,131,115,235,139,159,78,81,72,10,47,248,0,3,254,40,17,138, +48,66,136,152,64,0,66,129,48,5,27,252,88,76,216,54,47,214,131,50,172,88,15, +253,255,255,255,255,255,255,240,153,178,103,95,173,6,101,88,176,0,0,0,0,0, +0,0,0,67,168,15,255,0,0,0,0,0,0,17,26,19,233,201,169,38,180,91,242,103,70, +147,58,77,75,48,31,252,0,0,0,0,0,0,34,51,162,199,131,82,77,104,183,228,206, +141,38,116,154,150,96,127,248,0,0,0,0,0,0,0,15,248,192,70,40,0,0,0,0,0,0,0, +0,3,10,44,68,9,216,8,20,49,130,15,211,124,109,62,50,228,95,36,55,166,248, +190,56,111,221,151,119,77,56,118,47,18,23,211,125,14,89,113,233,231,167, +126,230,18,5,31,252,0,224,188,48,242,231,148,116,144,58,181,33,143,127,64, +247,111,238,56,0,127,199,2,49,72,127,248,0,0,0,0,0,0,180,81,36,4,51,166, +248,152,122,101,167,211,150,157,217,201,2,0,3,12,233,190,166,157,185,105, +244,229,167,118,114,64,128,1,4,228,129,0,3,137,116,223,51,126,60,59,50,196, +195,211,45,62,156,180,238,206,72,16,0,72,151,77,243,55,227,195,179,45,77, +59,114,211,233,203,78,236,228,129,0,5,10,73,2,0,12,21,18,4,0,28,82,35,32, +80,74,8,62,124,189,42,105,219,148,148,16,188,249,122,70,235,179,101,156, +184,121,15,132,0,34,29,159,47,74,181,33,198,235,179,101,156,184,121,15,132, +0,38,17,159,47,73,187,247,116,208,62,16,0,168,94,124,189,42,212,135,55,126, +238,154,7,194,0,23,7,207,151,164,76,61,50,143,132,0,50,21,159,47,74,181,33, +196,195,211,40,248,64,3,96,217,242,244,137,135,200,248,64,3,161,57,242,244, +171,82,28,76,62,71,194,0,31,8,207,151,164,141,253,121,115,31,8,0,132,47,62, +94,149,106,67,145,191,175,46,99,225,0,17,133,103,203,210,110,157,221,122, +101,230,62,16,1,40,110,124,189,42,212,135,55,78,238,189,50,243,31,8,0,156, +43,62,94,148,242,227,223,187,39,49,240,128,10,67,115,229,233,86,164,58,121, +113,239,221,147,152,248,64,5,97,249,242,244,155,167,102,205,60,242,227,223, +187,39,49,240,128,11,68,179,229,233,86,164,57,186,118,108,211,207,46,61, +251,178,115,31,8,0,188,71,62,94,149,52,237,203,235,126,236,179,243,102,231, +151,161,0,32,252,242,244,169,167,110,82,34,67,249,229,233,55,78,205,154, +121,229,199,191,118,78,100,37,0,24,137,115,203,210,173,72,115,116,236,217, +167,158,92,123,247,100,230,66,80,1,152,87,60,189,41,229,199,191,118,78,100, +43,224,3,80,222,121,122,85,169,14,158,92,123,247,100,230,66,190,0,55,10, +231,151,164,221,59,186,244,203,204,133,252,0,114,27,207,47,74,181,33,205, +211,187,175,76,188,200,95,192,7,97,28,242,244,145,191,175,46,100,51,224,3, +208,190,121,122,85,169,14,70,254,188,185,144,207,128,15,193,249,229,233,19, +15,76,164,37,0,32,133,115,203,210,173,72,113,48,244,202,66,80,2,24,71,60, +189,38,239,221,211,65,10,248,1,20,47,158,94,149,106,67,155,191,119,77,4,43, +224,4,112,190,121,122,70,235,179,101,156,184,121,16,191,128,18,67,185,229, +233,86,164,56,221,118,108,179,151,15,34,23,240,2,88,62,124,189,44,229,195, +200,124,32,4,208,126,121,122,89,203,135,145,9,64,9,194,145,254,0,0,255,144, +24,100,130,14,0,16,176,2,192,129,11,33,12,1,168,193,108,96,186,48,95,32,0, +0,0,0,0,0,0,0,56,38,95,25,113,189,18,9,211,47,62,143,100,20,95,0,20,159, +240,0,7,252,144,162,241,2,195,66,7,11,89,204,140,197,252,229,197,226,230, +115,3,16,69,19,64,5,43,252,0,9,255,40,16,188,33,49,123,97,217,23,151,45, +252,131,66,7,0,20,191,240,0,39,252,176,66,240,133,82,195,187,62,88,188,185, +111,228,26,16,56,0,166,127,128,1,63,230,2,23,132,58,150,92,217,121,101,221, +143,44,94,92,183,242,13,8,28,0,83,127,192,0,159,243,65,11,194,23,79,206, +238,152,124,69,229,203,127,32,208,129,192,5,59,252,0,9,255,56,16,188,33,53, +60,240,203,23,151,45,252,131,66,7,0,20,255,240,0,39,252,240,66,240,132,85, +165,38,47,46,91,249,6,132,14,0,31,255,228,64,98,192,64,5,191,10,139,20,87, +105,130,76,156,197,132,4,0,38,187,27,187,85,81,104,28,201,204,160,31,243, +23,33,127,125,28,247,193,102,79,142,202,44,3,255,113,84,118,82,184,47,232, +52,201,241,216,176,139,0,255,111,45,236,84,155,148,58,5,66,76,4,0,146,31, +181,68,66,209,136,61,58,52,170,49,190,202,1,255,53,4,243,51,249,222,108,22, +157,26,85,25,64,63,246,160,158,102,127,59,205,74,7,135,23,53,2,65,48,227, +223,205,64,160,0,48,76,60,244,238,80,40,0,20,19,15,76,59,148,10,0,7,5,195, +211,14,230,74,72,130,99,203,167,98,129,64,1,32,120,247,243,80,40,0,44,15, +47,142,10,5,0,6,130,230,217,191,127,37,2,128,3,192,246,111,206,160,80,0, +136,30,220,62,19,151,160,123,116,238,79,94,129,240,223,221,73,32,0,48,110, +88,119,100,223,181,68,16,94,91,250,238,200,160,80,0,152,31,61,59,148,10,0, +21,4,231,199,151,69,2,128,5,192,250,97,220,160,80,0,192,127,255,128,20,23, +134,30,92,242,164,34,19,207,167,45,59,179,233,205,229,37,129,127,255,0,0, +191,255,128,0,63,255,197,131,246,203,203,158,157,251,160,32,98,65,128,0,0, +0,0,3,166,156,30,53,32,249,165,131,76,223,159,62,94,70,172,114,16,176,144, +60,56,250,19,18,5,159,25,89,32,121,180,238,42,30,129,229,221,140,164,122,7, +147,46,50,129,232,62,61,251,120,97,199,208,156,129,83,127,0,50,250,69,3, +252,131,32,248,250,242,229,151,119,72,240,3,254,148,0,2,168,254,0,0,255, +167,0,33,68,88,32,0,33,64,176,2,170,254,0,0,255,169,0,33,69,220,32,0,33,67, +184,2,172,254,0,0,255,171,8,137,144,128,0,0,0,0,0,0,0,68,73,4,195,187,126, +226,8,4,178,16,41,164,32,147,7,136,52,193,240,0,18,17,48,124,0,8,133,76,31, +0,3,33,147,7,192,1,8,116,193,240,0,82,127,255,132,47,65,11,137,191,174,45, +153,98,242,229,191,144,105,4,95,47,46,91,249,32,211,185,6,94,92,183,242,65, +163,14,236,155,52,238,206,0,85,255,192,6,13,167,157,109,57,123,136,144,31, +245,192,3,5,231,179,78,60,163,9,0,2,10,199,248,0,3,254,192,4,32,249,242, +244,147,187,163,129,116,128,24,66,51,229,233,87,78,238,142,5,210,0,65,8, +207,151,164,157,221,24,182,23,72,1,140,39,62,94,149,116,238,232,197,176, +186,64,8,97,25,242,244,147,187,163,54,66,233,0,50,132,231,203,210,174,157, +221,25,178,23,72,1,20,43,62,94,145,182,111,195,209,155,33,116,128,17,194, +179,229,233,27,102,252,61,27,52,23,72,1,36,31,158,94,146,119,116,112,50, +208,3,8,71,60,189,42,233,221,209,192,203,64,8,33,28,242,244,147,187,163,22, +195,45,0,49,132,243,203,210,174,157,221,24,182,25,104,1,12,35,158,94,146, +119,116,102,200,101,160,6,80,158,121,122,85,211,187,163,54,67,45,0,34,133, +115,203,210,54,205,248,122,51,100,50,208,2,56,87,60,189,35,108,223,135,163, +102,131,45,0,36,7,255,248,1,11,50,136,132,115,235,139,15,46,88,124,140,36, +0,4,43,79,224,139,16,15,252,0,0,0,0,0,0,0,101,253,152,0,5,109,252,17,98,1, +255,128,0,0,0,0,0,0,12,191,181,0,0,174,63,130,44,64,63,240,0,0,0,0,0,0,1, +151,246,224,0,21,215,240,69,136,8,0,0,0,0,0,0,0,0,50,254,228,0,2,188,254,8, +177,1,0,0,0,0,0,0,0,0,6,95,221,128,0,87,223,193,22,32,32,8,0,0,0,0,0,0,0, +203,251,208,0,11,3,248,34,196,4,1,0,0,0,0,0,0,0,25,127,126,0,1,97,127,4,88, +128,128,32,0,0,0,0,0,0,3,47,240,64,0,44,79,224,139,16,16,8,0,0,0,0,0,0,0, +101,254,24,0,5,141,252,1,96,216,247,238,199,135,162,162,33,90,121,197,221, +143,126,77,59,179,172,146,17,167,156,46,185,179,101,228,176,65,89,77,16, +124,123,246,240,195,203,40,162,64,0,193,255,138,5,144,158,89,112,228,171, +39,119,71,2,232,132,114,203,135,36,157,221,28,11,164,0,66,25,203,46,28,149, +100,238,232,197,180,200,162,233,0,1,134,114,203,135,37,89,59,186,49,109,10, +40,186,64,2,97,124,178,225,201,39,119,70,45,166,69,23,72,0,140,47,150,92, +57,36,238,232,197,180,40,162,233,0,25,134,114,203,135,37,89,59,186,51,101, +50,40,186,64,0,161,156,178,225,201,86,78,238,140,217,66,138,46,144,0,168, +95,44,184,114,73,221,209,155,41,145,69,210,0,37,11,229,151,14,73,59,186,51, +101,10,40,186,64,6,161,124,178,225,201,27,102,252,61,38,69,23,72,0,28,47, +150,92,57,35,108,223,135,164,40,162,233,0,11,134,114,203,135,36,77,253,113, +108,203,50,40,186,64,1,33,156,178,225,201,19,127,92,91,50,194,138,46,144,0, +200,87,44,184,114,85,147,187,164,200,162,237,0,5,133,114,203,135,37,89,59, +186,66,138,46,208,0,216,79,44,184,114,73,221,210,100,81,118,128,10,194,121, +101,195,146,78,238,144,162,139,180,0,118,21,223,150,158,153,106,201,221, +209,192,203,33,61,249,105,233,150,78,238,142,6,90,0,33,13,239,203,79,76, +181,100,238,232,197,180,200,163,45,0,1,134,247,229,167,166,90,178,119,116, +98,218,20,81,150,128,4,195,59,242,211,211,44,157,221,24,182,153,20,101,160, +2,48,206,252,180,244,203,39,119,70,45,161,69,25,104,0,204,55,191,45,61,50, +213,147,187,163,54,83,34,140,180,0,10,27,223,150,158,153,106,201,221,209, +155,40,81,70,90,0,21,12,239,203,79,76,178,119,116,102,202,100,81,150,128,9, +67,59,242,211,211,44,157,221,25,178,133,20,101,160,3,80,206,252,180,244, +203,27,102,252,61,38,69,25,104,0,28,51,191,45,61,50,198,217,191,15,72,81, +70,90,0,23,13,239,203,79,76,177,55,245,197,179,44,200,163,45,0,4,134,247, +229,167,166,88,155,250,226,217,150,20,81,150,128,6,66,251,242,211,211,45, +89,59,186,76,138,51,16,0,88,95,126,90,122,101,171,39,119,72,81,70,98,0,27, +10,239,203,79,76,178,119,116,153,20,102,32,2,176,174,252,180,244,203,39, +119,72,81,70,98,0,58,40,173,176,82,90,4,19,54,157,155,21,217,6,203,199,174, +29,156,197,9,7,199,191,111,12,60,178,138,20,0,6,9,143,127,15,42,208,130, +243,217,167,30,81,132,65,123,242,211,211,42,228,0, +}; +#elif defined(DUK_USE_DOUBLE_ME) +DUK_INTERNAL const duk_uint8_t duk_builtins_data[3833] = { +105,195,75,32,3,148,52,154,248,9,26,13,128,112,105,0,240,22,20,26,95,124,6, +152,52,137,0,120,99,74,239,129,18,70,241,191,2,98,13,79,32,42,88,210,90,2, +240,1,50,141,37,168,76,94,216,118,69,229,203,127,44,0,84,163,73,106,21,75, +14,236,249,98,242,229,191,150,0,46,81,164,181,14,165,151,54,94,89,119,99, +203,23,151,45,252,176,1,146,141,37,168,93,63,59,186,97,241,23,151,45,252, +176,1,178,141,37,168,77,79,60,50,197,229,203,127,44,0,116,163,73,106,17,86, +148,152,188,185,111,229,128,15,148,129,198,137,36,58,166,142,91,251,212, +243,195,44,94,92,183,242,13,79,8,45,14,91,252,121,148,52,199,120,63,72,105, +21,240,118,128,210,237,224,245,17,165,43,224,211,55,231,207,151,148,161,70, +145,0,31,40,107,26,2,18,138,26,228,192,142,0,16,161,174,76,9,74,26,228,192, +158,0,8,161,174,76,10,96,2,42,26,228,192,174,0,26,161,174,76,11,96,3,74,26, +228,192,190,0,44,161,174,76,12,96,3,202,26,228,192,206,0,70,161,169,84,14, +202,3,255,254,32,234,0,0,7,195,248,0,0,0,0,119,0,0,3,193,252,0,0,0,0,57, +136,1,152,32,16,194,0,166,24,6,49,0,57,138,2,12,96,18,99,128,163,32,5,153, +40,76,94,216,118,69,229,203,127,35,41,10,165,135,118,124,177,121,114,223, +200,203,67,169,101,205,151,150,93,216,242,197,229,203,127,35,49,11,167,231, +119,76,62,34,242,229,191,145,154,132,212,243,195,44,94,92,183,242,51,144, +138,180,164,197,229,203,127,35,60,6,26,0,52,208,193,226,117,215,211,15,12, +166,146,11,67,150,255,30,77,24,58,113,64,243,92,8,27,0,68,217,130,70,212, +19,54,224,161,185,5,77,216,44,111,65,115,126,12,28,16,100,225,156,16,32,18, +17,195,15,46,121,100,238,232,136,136,87,12,60,185,229,141,179,126,30,136, +100,130,233,231,59,12,228,34,66,52,243,141,167,118,158,153,80,73,9,201,151, +30,252,153,106,210,146,118,72,150,76,184,247,228,203,86,148,152,123,246, +240,223,187,46,238,135,132,132,229,221,143,126,76,181,105,73,61,36,75,46, +236,123,242,101,171,74,76,61,251,120,111,221,151,119,67,226,65,178,243,199, +135,134,83,242,66,58,238,203,207,30,30,25,81,201,5,225,203,78,238,136,163, +208,92,59,50,242,232,138,62,0,2,38,163,19,255,255,224,142,80,192,0,20,31, +240,14,135,103,203,210,135,45,253,55,244,243,195,44,252,205,197,0,1,18,221, +82,0,3,24,207,151,164,254,251,168,114,223,195,47,46,158,98,101,231,143,150, +158,29,55,242,104,68,79,62,94,147,251,238,161,203,127,12,188,186,121,157, +135,110,94,109,100,131,99,229,151,15,76,172,168,8,89,217,16,201,151,54,157, +217,104,114,223,195,47,46,154,114,243,102,68,19,158,92,59,27,73,6,205,203, +46,95,89,91,74,0,3,17,225,203,47,108,187,186,69,241,211,46,238,122,119,238, +230,216,72,70,158,116,242,225,217,151,35,81,33,26,121,198,229,191,214,93, +205,69,0,1,134,105,231,23,199,76,187,185,233,197,179,43,73,32,154,242,249, +230,214,80,0,31,255,193,2,38,103,110,117,24,81,115,0,78,228,0,161,208,16, +237,24,121,207,239,186,135,45,252,50,242,233,229,188,144,221,60,232,114, +223,211,127,79,60,50,207,204,224,72,167,14,91,248,101,229,211,204,158,113, +119,117,219,151,150,28,91,50,184,144,40,95,224,0,15,248,64,4,20,78,129,5, +195,195,134,207,38,232,130,99,195,179,97,201,244,19,22,157,217,14,15,130, +135,254,0,48,125,60,224,242,229,135,200,9,1,255,12,2,162,136,112,2,112,80, +128,0,193,177,239,221,143,15,64,35,224,152,20,144,62,27,248,3,2,9,195,175, +61,0,231,208,126,89,123,101,229,207,40,72,32,188,244,105,205,208,40,16,94, +123,52,227,202,22,136,39,61,252,186,6,18,13,207,134,205,56,242,134,175,65, +250,238,231,163,78,110,129,231,208,125,59,178,101,241,63,48,25,248,0,12,47, +102,30,125,36,238,201,151,196,252,192,103,255,255,240,92,189,178,242,242,8, +105,4,231,191,110,80,67,80,0,24,62,109,252,162,225,199,160,16,212,0,10,7, +183,15,0,67,80,0,56,54,109,59,58,101,228,8,106,0,9,6,229,151,39,92,121,66, +15,192,0,97,124,178,228,235,143,45,45,57,244,116,8,63,255,255,10,39,248,0, +195,51,114,223,182,30,140,60,161,239,201,149,248,248,31,241,0,140,80,129, +202,10,49,128,10,35,1,6,199,163,15,40,61,32,9,10,199,163,15,40,123,242,101, +131,210,4,144,108,123,247,99,195,210,8,250,15,167,118,76,190,39,230,131,52, +133,236,195,207,164,157,217,50,248,159,154,12,212,0,6,27,179,126,60,59,50, +195,223,183,134,30,89,97,9,5,219,135,166,61,16,164,131,242,203,195,102,28, +121,97,145,6,231,151,15,44,122,33,201,5,231,179,78,60,177,8,130,243,225, +179,79,72,148,66,121,245,197,207,167,45,59,179,197,162,23,211,124,205,253, +242,242,135,135,158,87,240,68,122,111,153,191,30,29,153,102,111,239,151, +148,60,60,242,191,130,23,211,125,94,28,50,242,135,135,158,87,240,128,0,196, +122,111,153,191,30,29,153,106,240,225,151,148,60,60,242,191,132,0,6,9,211, +150,157,177,160,131,115,235,139,159,78,81,72,10,47,248,0,3,254,40,17,138, +48,66,136,152,64,0,66,129,48,5,27,252,88,76,216,54,47,214,131,50,172,88,31, +255,253,239,255,255,255,255,240,153,178,103,95,173,6,101,88,176,0,0,0,0,0, +64,0,0,3,168,0,0,31,15,224,0,0,0,17,26,19,233,201,169,38,180,91,242,103,70, +147,58,77,75,48,0,0,60,31,192,0,0,0,34,51,162,199,131,82,77,104,183,228, +206,141,38,116,154,150,96,0,0,120,127,128,0,0,0,0,15,248,192,70,40,0,0,0,0, +0,0,0,0,3,10,44,68,9,216,8,20,49,130,15,211,124,109,62,50,228,95,36,55,166, +248,190,56,111,221,151,119,77,56,118,47,18,23,211,125,14,89,113,233,231, +167,126,230,18,5,31,252,0,224,188,48,242,231,148,116,144,58,181,33,143,127, +64,247,111,238,56,0,127,199,2,49,72,0,0,248,127,0,0,0,0,180,81,36,4,51,166, +248,152,122,101,167,211,150,157,217,201,2,0,3,12,233,190,166,157,185,105, +244,229,167,118,114,64,128,1,4,228,129,0,3,137,116,223,51,126,60,59,50,196, +195,211,45,62,156,180,238,206,72,16,0,72,151,77,243,55,227,195,179,45,77, +59,114,211,233,203,78,236,228,129,0,5,10,73,2,0,12,21,18,4,0,28,82,35,32, +80,74,8,62,124,189,42,105,219,148,148,16,188,249,122,70,235,179,101,156, +184,121,15,132,0,34,29,159,47,74,181,33,198,235,179,101,156,184,121,15,132, +0,38,17,159,47,73,187,247,116,208,62,16,0,168,94,124,189,42,212,135,55,126, +238,154,7,194,0,23,7,207,151,164,76,61,50,143,132,0,50,21,159,47,74,181,33, +196,195,211,40,248,64,3,96,217,242,244,137,135,200,248,64,3,161,57,242,244, +171,82,28,76,62,71,194,0,31,8,207,151,164,141,253,121,115,31,8,0,132,47,62, +94,149,106,67,145,191,175,46,99,225,0,17,133,103,203,210,110,157,221,122, +101,230,62,16,1,40,110,124,189,42,212,135,55,78,238,189,50,243,31,8,0,156, +43,62,94,148,242,227,223,187,39,49,240,128,10,67,115,229,233,86,164,58,121, +113,239,221,147,152,248,64,5,97,249,242,244,155,167,102,205,60,242,227,223, +187,39,49,240,128,11,68,179,229,233,86,164,57,186,118,108,211,207,46,61, +251,178,115,31,8,0,188,71,62,94,149,52,237,203,235,126,236,179,243,102,231, +151,161,0,32,252,242,244,169,167,110,82,34,67,249,229,233,55,78,205,154, +121,229,199,191,118,78,100,37,0,24,137,115,203,210,173,72,115,116,236,217, +167,158,92,123,247,100,230,66,80,1,152,87,60,189,41,229,199,191,118,78,100, +43,224,3,80,222,121,122,85,169,14,158,92,123,247,100,230,66,190,0,55,10, +231,151,164,221,59,186,244,203,204,133,252,0,114,27,207,47,74,181,33,205, +211,187,175,76,188,200,95,192,7,97,28,242,244,145,191,175,46,100,51,224,3, +208,190,121,122,85,169,14,70,254,188,185,144,207,128,15,193,249,229,233,19, +15,76,164,37,0,32,133,115,203,210,173,72,113,48,244,202,66,80,2,24,71,60, +189,38,239,221,211,65,10,248,1,20,47,158,94,149,106,67,155,191,119,77,4,43, +224,4,112,190,121,122,70,235,179,101,156,184,121,16,191,128,18,67,185,229, +233,86,164,56,221,118,108,179,151,15,34,23,240,2,88,62,124,189,44,229,195, +200,124,32,4,208,126,121,122,89,203,135,145,9,64,9,194,145,254,0,0,255,144, +24,100,130,14,0,16,176,2,192,129,11,33,12,1,168,193,108,96,186,48,95,32,0, +0,0,0,0,0,0,0,56,38,95,25,113,189,18,9,211,47,62,143,100,20,95,0,20,159, +240,0,7,252,144,162,241,2,195,66,7,11,89,204,140,197,252,229,197,226,230, +115,3,16,69,19,64,5,43,252,0,9,255,40,16,188,33,49,123,97,217,23,151,45, +252,131,66,7,0,20,191,240,0,39,252,176,66,240,133,82,195,187,62,88,188,185, +111,228,26,16,56,0,166,127,128,1,63,230,2,23,132,58,150,92,217,121,101,221, +143,44,94,92,183,242,13,8,28,0,83,127,192,0,159,243,65,11,194,23,79,206, +238,152,124,69,229,203,127,32,208,129,192,5,59,252,0,9,255,56,16,188,33,53, +60,240,203,23,151,45,252,131,66,7,0,20,255,240,0,39,252,240,66,240,132,85, +165,38,47,46,91,249,6,132,14,0,31,255,228,64,98,192,10,191,5,64,105,87,20, +139,130,76,156,197,132,11,22,176,36,1,101,91,91,184,28,201,204,160,33,23, +115,31,247,156,253,127,65,102,79,142,202,44,4,113,95,115,255,232,34,182,88, +52,201,241,216,176,139,1,239,47,108,252,59,148,152,86,5,66,76,15,178,16, +148,1,130,212,69,72,61,58,52,170,49,190,202,4,245,7,49,254,105,219,251,52, +22,157,26,85,25,64,158,160,246,63,205,59,127,102,74,7,135,23,53,2,65,48, +227,223,205,64,160,0,48,76,60,244,238,80,40,0,20,19,15,76,59,148,10,0,7,5, +195,211,14,230,74,72,130,99,203,167,98,129,64,1,32,120,247,243,80,40,0,44, +15,47,142,10,5,0,6,130,230,217,191,127,37,2,128,3,192,246,111,206,160,80,0, +136,30,220,62,19,151,160,123,116,238,79,94,129,240,223,221,73,32,0,48,110, +88,119,100,223,181,68,16,94,91,250,238,200,160,80,0,152,31,61,59,148,10,0, +21,4,231,199,151,69,2,128,5,192,250,97,220,160,80,0,192,127,255,128,20,23, +134,30,92,242,164,34,19,207,167,45,59,179,233,205,229,37,129,127,255,0,0, +191,255,128,0,63,255,197,131,246,203,203,158,157,251,160,0,65,226,32,0,0,0, +0,3,166,156,30,53,32,249,165,131,76,223,159,62,94,70,172,114,16,176,144,60, +56,250,19,18,5,159,25,89,32,121,180,238,42,30,129,229,221,140,164,122,7, +147,46,50,129,232,62,61,251,120,97,199,208,156,129,83,127,0,50,250,69,3, +252,131,32,248,250,242,229,151,119,72,240,3,254,148,0,2,168,254,0,0,255, +167,0,33,68,88,32,0,33,64,176,2,170,254,0,0,255,169,0,33,69,220,32,0,33,67, +184,2,172,254,0,0,255,171,8,137,144,0,0,0,128,0,0,0,0,68,73,4,195,187,126, +226,8,4,178,16,41,164,32,147,7,136,52,193,240,0,18,17,48,124,0,8,133,76,31, +0,3,33,147,7,192,1,8,116,193,240,0,82,127,255,132,47,65,11,137,191,174,45, +153,98,242,229,191,144,105,4,95,47,46,91,249,32,211,185,6,94,92,183,242,65, +163,14,236,155,52,238,206,0,85,255,192,6,13,167,157,109,57,123,136,144,31, +245,192,3,5,231,179,78,60,163,9,0,2,10,199,248,0,3,254,192,4,32,249,242, +244,147,187,163,129,116,128,24,66,51,229,233,87,78,238,142,5,210,0,65,8, +207,151,164,157,221,24,182,23,72,1,140,39,62,94,149,116,238,232,197,176, +186,64,8,97,25,242,244,147,187,163,54,66,233,0,50,132,231,203,210,174,157, +221,25,178,23,72,1,20,43,62,94,145,182,111,195,209,155,33,116,128,17,194, +179,229,233,27,102,252,61,27,52,23,72,1,36,31,158,94,146,119,116,112,50, +208,3,8,71,60,189,42,233,221,209,192,203,64,8,33,28,242,244,147,187,163,22, +195,45,0,49,132,243,203,210,174,157,221,24,182,25,104,1,12,35,158,94,146, +119,116,102,200,101,160,6,80,158,121,122,85,211,187,163,54,67,45,0,34,133, +115,203,210,54,205,248,122,51,100,50,208,2,56,87,60,189,35,108,223,135,163, +102,131,45,0,36,7,255,248,1,11,50,136,132,115,235,139,15,46,88,124,140,36, +0,4,43,79,224,139,16,0,0,60,15,192,0,0,0,0,101,253,152,0,5,109,252,17,98,0, +0,7,129,248,0,0,0,0,12,191,181,0,0,174,63,130,44,64,0,0,240,63,0,0,0,0,1, +151,246,224,0,21,215,240,69,136,0,0,0,8,0,0,0,0,0,50,254,228,0,2,188,254,8, +177,0,0,0,1,0,0,0,0,0,6,95,221,128,0,87,223,193,22,32,0,0,8,32,0,0,0,0,0, +203,251,208,0,11,3,248,34,196,0,0,1,4,0,0,0,0,0,25,127,126,0,1,97,127,4,88, +128,0,0,32,128,0,0,0,0,3,47,240,64,0,44,79,224,139,16,0,0,8,16,0,0,0,0,0, +101,254,24,0,5,141,252,1,96,216,247,238,199,135,162,162,33,90,121,197,221, +143,126,77,59,179,172,146,17,167,156,46,185,179,101,228,176,65,89,77,16, +124,123,246,240,195,203,40,162,64,0,193,255,138,5,144,158,89,112,228,171, +39,119,71,2,232,132,114,203,135,36,157,221,28,11,164,0,66,25,203,46,28,149, +100,238,232,197,180,200,162,233,0,1,134,114,203,135,37,89,59,186,49,109,10, +40,186,64,2,97,124,178,225,201,39,119,70,45,166,69,23,72,0,140,47,150,92, +57,36,238,232,197,180,40,162,233,0,25,134,114,203,135,37,89,59,186,51,101, +50,40,186,64,0,161,156,178,225,201,86,78,238,140,217,66,138,46,144,0,168, +95,44,184,114,73,221,209,155,41,145,69,210,0,37,11,229,151,14,73,59,186,51, +101,10,40,186,64,6,161,124,178,225,201,27,102,252,61,38,69,23,72,0,28,47, +150,92,57,35,108,223,135,164,40,162,233,0,11,134,114,203,135,36,77,253,113, +108,203,50,40,186,64,1,33,156,178,225,201,19,127,92,91,50,194,138,46,144,0, +200,87,44,184,114,85,147,187,164,200,162,237,0,5,133,114,203,135,37,89,59, +186,66,138,46,208,0,216,79,44,184,114,73,221,210,100,81,118,128,10,194,121, +101,195,146,78,238,144,162,139,180,0,118,21,223,150,158,153,106,201,221, +209,192,203,33,61,249,105,233,150,78,238,142,6,90,0,33,13,239,203,79,76, +181,100,238,232,197,180,200,163,45,0,1,134,247,229,167,166,90,178,119,116, +98,218,20,81,150,128,4,195,59,242,211,211,44,157,221,24,182,153,20,101,160, +2,48,206,252,180,244,203,39,119,70,45,161,69,25,104,0,204,55,191,45,61,50, +213,147,187,163,54,83,34,140,180,0,10,27,223,150,158,153,106,201,221,209, +155,40,81,70,90,0,21,12,239,203,79,76,178,119,116,102,202,100,81,150,128,9, +67,59,242,211,211,44,157,221,25,178,133,20,101,160,3,80,206,252,180,244, +203,27,102,252,61,38,69,25,104,0,28,51,191,45,61,50,198,217,191,15,72,81, +70,90,0,23,13,239,203,79,76,177,55,245,197,179,44,200,163,45,0,4,134,247, +229,167,166,88,155,250,226,217,150,20,81,150,128,6,66,251,242,211,211,45, +89,59,186,76,138,51,16,0,88,95,126,90,122,101,171,39,119,72,81,70,98,0,27, +10,239,203,79,76,178,119,116,153,20,102,32,2,176,174,252,180,244,203,39, +119,72,81,70,98,0,58,40,173,176,82,90,4,19,54,157,155,21,217,6,203,199,174, +29,156,197,9,7,199,191,111,12,60,178,138,20,0,6,9,143,127,15,42,208,130, +243,217,167,30,81,132,65,123,242,211,211,42,228,0, +}; +#else +#error invalid endianness defines +#endif +#endif /* DUK_USE_ROM_OBJECTS */ +/* + * Error, fatal, and panic handling. + */ + +/* include removed: duk_internal.h */ + +#define DUK__ERRFMT_BUFSIZE 256 /* size for formatting buffers */ + +#if defined(DUK_USE_VERBOSE_ERRORS) + +DUK_INTERNAL void duk_err_handle_error_fmt(duk_hthread *thr, const char *filename, duk_uint_t line_and_code, const char *fmt, ...) { + va_list ap; + char msg[DUK__ERRFMT_BUFSIZE]; + va_start(ap, fmt); + (void) DUK_VSNPRINTF(msg, sizeof(msg), fmt, ap); + msg[sizeof(msg) - 1] = (char) 0; + duk_err_create_and_throw(thr, (duk_errcode_t) (line_and_code >> 24), msg, filename, (duk_int_t) (line_and_code & 0x00ffffffL)); + va_end(ap); /* dead code, but ensures portability (see Linux man page notes) */ +} + +DUK_INTERNAL void duk_err_handle_error(duk_hthread *thr, const char *filename, duk_uint_t line_and_code, const char *msg) { + duk_err_create_and_throw(thr, (duk_errcode_t) (line_and_code >> 24), msg, filename, (duk_int_t) (line_and_code & 0x00ffffffL)); +} + +#else /* DUK_USE_VERBOSE_ERRORS */ + +DUK_INTERNAL void duk_err_handle_error(duk_hthread *thr, duk_errcode_t code) { + duk_err_create_and_throw(thr, code); +} + +#endif /* DUK_USE_VERBOSE_ERRORS */ + +/* + * Error throwing helpers + */ + +#if defined(DUK_USE_VERBOSE_ERRORS) +#if defined(DUK_USE_PARANOID_ERRORS) +DUK_INTERNAL void duk_err_require_type_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t index, const char *expect_name) { + DUK_ERROR_RAW_FMT3(thr, filename, linenumber, DUK_ERR_TYPE_ERROR, "%s required, found %s (stack index %ld)", + expect_name, duk_get_type_name((duk_context *) thr, index), (long) index); +} +#else +DUK_INTERNAL void duk_err_require_type_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t index, const char *expect_name) { + DUK_ERROR_RAW_FMT3(thr, filename, linenumber, DUK_ERR_TYPE_ERROR, "%s required, found %s (stack index %ld)", + expect_name, duk_push_string_readable((duk_context *) thr, index), (long) index); +} +#endif +DUK_INTERNAL void duk_err_range(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message) { + DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_RANGE_ERROR, message); +} +DUK_INTERNAL void duk_err_api_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t index) { + DUK_ERROR_RAW_FMT1(thr, filename, linenumber, DUK_ERR_API_ERROR, "invalid stack index %ld", (long) (index)); +} +DUK_INTERNAL void duk_err_api(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message) { + DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_API_ERROR, message); +} +DUK_INTERNAL void duk_err_unimplemented_defmsg(duk_hthread *thr, const char *filename, duk_int_t linenumber) { + DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_UNIMPLEMENTED_ERROR, DUK_STR_UNIMPLEMENTED); +} +#if !defined(DUK_USE_BYTECODE_DUMP_SUPPORT) +DUK_INTERNAL void duk_err_unsupported_defmsg(duk_hthread *thr, const char *filename, duk_int_t linenumber) { + DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_UNSUPPORTED_ERROR, DUK_STR_UNSUPPORTED); +} +#endif +DUK_INTERNAL void duk_err_internal_defmsg(duk_hthread *thr, const char *filename, duk_int_t linenumber) { + DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR); +} +DUK_INTERNAL void duk_err_internal(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message) { + DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_INTERNAL_ERROR, message); +} +DUK_INTERNAL void duk_err_alloc(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message) { + DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_ALLOC_ERROR, message); +} +#else +/* The file/line arguments are NULL and 0, they're ignored by DUK_ERROR_RAW() + * when non-verbose errors are used. + */ +DUK_INTERNAL void duk_err_type(duk_hthread *thr) { + DUK_ERROR_RAW(thr, NULL, 0, DUK_ERR_TYPE_ERROR, NULL); +} +DUK_INTERNAL void duk_err_api(duk_hthread *thr) { + DUK_ERROR_RAW(thr, NULL, 0, DUK_ERR_API_ERROR, NULL); +} +DUK_INTERNAL void duk_err_range(duk_hthread *thr) { + DUK_ERROR_RAW(thr, NULL, 0, DUK_ERR_RANGE_ERROR, NULL); +} +DUK_INTERNAL void duk_err_syntax(duk_hthread *thr) { + DUK_ERROR_RAW(thr, NULL, 0, DUK_ERR_SYNTAX_ERROR, NULL); +} +DUK_INTERNAL void duk_err_unimplemented(duk_hthread *thr) { + DUK_ERROR_RAW(thr, NULL, 0, DUK_ERR_UNIMPLEMENTED_ERROR, NULL); +} +DUK_INTERNAL void duk_err_unsupported(duk_hthread *thr) { + DUK_ERROR_RAW(thr, NULL, 0, DUK_ERR_UNSUPPORTED_ERROR, NULL); +} +DUK_INTERNAL void duk_err_internal(duk_hthread *thr) { + DUK_ERROR_RAW(thr, NULL, 0, DUK_ERR_INTERNAL_ERROR, NULL); +} +DUK_INTERNAL void duk_err_alloc(duk_hthread *thr) { + DUK_ERROR_RAW(thr, NULL, thr, DUK_ERR_ALLOC_ERROR, NULL); +} +#endif + +/* + * Default fatal error handler + */ + +DUK_INTERNAL void duk_default_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg) { + DUK_UNREF(ctx); +#if defined(DUK_USE_FILE_IO) + DUK_FPRINTF(DUK_STDERR, "FATAL %ld: %s\n", (long) code, (const char *) (msg ? msg : "null")); + DUK_FFLUSH(DUK_STDERR); +#else + /* omit print */ +#endif + DUK_D(DUK_DPRINT("default fatal handler called, code %ld -> calling DUK_PANIC()", (long) code)); + DUK_PANIC(code, msg); + DUK_UNREACHABLE(); +} + +/* + * Default panic handler + */ + +#if !defined(DUK_USE_PANIC_HANDLER) +DUK_INTERNAL void duk_default_panic_handler(duk_errcode_t code, const char *msg) { +#if defined(DUK_USE_FILE_IO) + DUK_FPRINTF(DUK_STDERR, "PANIC %ld: %s (" +#if defined(DUK_USE_PANIC_ABORT) + "calling abort" +#elif defined(DUK_USE_PANIC_EXIT) + "calling exit" +#elif defined(DUK_USE_PANIC_SEGFAULT) + "segfaulting on purpose" +#else +#error no DUK_USE_PANIC_xxx macro defined +#endif + ")\n", (long) code, (const char *) (msg ? msg : "null")); + DUK_FFLUSH(DUK_STDERR); +#else + /* omit print */ + DUK_UNREF(code); + DUK_UNREF(msg); +#endif + +#if defined(DUK_USE_PANIC_ABORT) + DUK_ABORT(); +#elif defined(DUK_USE_PANIC_EXIT) + DUK_EXIT(-1); +#elif defined(DUK_USE_PANIC_SEGFAULT) + /* exit() afterwards to satisfy "noreturn" */ + DUK_CAUSE_SEGFAULT(); /* SCANBUILD: "Dereference of null pointer", normal */ + DUK_EXIT(-1); +#else +#error no DUK_USE_PANIC_xxx macro defined +#endif + + DUK_UNREACHABLE(); +} +#endif /* !DUK_USE_PANIC_HANDLER */ + +#undef DUK__ERRFMT_BUFSIZE +/* + * Various Unicode help functions for character classification predicates, + * case conversion, decoding, etc. + */ + +/* include removed: duk_internal.h */ + +/* + * Fast path tables + */ + +#if defined(DUK_USE_IDCHAR_FASTPATH) +DUK_INTERNAL const duk_int8_t duk_is_idchar_tab[128] = { + /* 0: not IdentifierStart or IdentifierPart + * 1: IdentifierStart and IdentifierPart + * -1: IdentifierPart only + */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00...0x0f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10...0x1f */ + 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20...0x2f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, /* 0x30...0x3f */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40...0x4f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50...0x5f */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60...0x6f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70...0x7f */ +}; +#endif + +/* + * XUTF-8 and CESU-8 encoding/decoding + */ + +DUK_INTERNAL duk_small_int_t duk_unicode_get_xutf8_length(duk_ucodepoint_t cp) { + duk_uint_fast32_t x = (duk_uint_fast32_t) cp; + if (x < 0x80UL) { + /* 7 bits */ + return 1; + } else if (x < 0x800UL) { + /* 11 bits */ + return 2; + } else if (x < 0x10000UL) { + /* 16 bits */ + return 3; + } else if (x < 0x200000UL) { + /* 21 bits */ + return 4; + } else if (x < 0x4000000UL) { + /* 26 bits */ + return 5; + } else if (x < (duk_ucodepoint_t) 0x80000000UL) { + /* 31 bits */ + return 6; + } else { + /* 36 bits */ + return 7; + } +} + +#if defined(DUK_USE_ASSERTIONS) +DUK_INTERNAL duk_small_int_t duk_unicode_get_cesu8_length(duk_ucodepoint_t cp) { + duk_uint_fast32_t x = (duk_uint_fast32_t) cp; + if (x < 0x80UL) { + /* 7 bits */ + return 1; + } else if (x < 0x800UL) { + /* 11 bits */ + return 2; + } else if (x < 0x10000UL) { + /* 16 bits */ + return 3; + } else { + /* Encoded as surrogate pair, each encoding to 3 bytes for + * 6 bytes total. Codepoints above U+10FFFF encode as 6 bytes + * too, see duk_unicode_encode_cesu8(). + */ + return 3 + 3; + } +} +#endif /* DUK_USE_ASSERTIONS */ + +DUK_INTERNAL const duk_uint8_t duk_unicode_xutf8_markers[7] = { + 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe +}; + +/* Encode to extended UTF-8; 'out' must have space for at least + * DUK_UNICODE_MAX_XUTF8_LENGTH bytes. Allows encoding of any + * 32-bit (unsigned) codepoint. + */ +DUK_INTERNAL duk_small_int_t duk_unicode_encode_xutf8(duk_ucodepoint_t cp, duk_uint8_t *out) { + duk_uint_fast32_t x = (duk_uint_fast32_t) cp; + duk_small_int_t len; + duk_uint8_t marker; + duk_small_int_t i; + + len = duk_unicode_get_xutf8_length(cp); + DUK_ASSERT(len > 0); + + marker = duk_unicode_xutf8_markers[len - 1]; /* 64-bit OK because always >= 0 */ + + i = len; + DUK_ASSERT(i > 0); + do { + i--; + if (i > 0) { + out[i] = (duk_uint8_t) (0x80 + (x & 0x3f)); + x >>= 6; + } else { + /* Note: masking of 'x' is not necessary because of + * range check and shifting -> no bits overlapping + * the marker should be set. + */ + out[0] = (duk_uint8_t) (marker + x); + } + } while (i > 0); + + return len; +} + +/* Encode to CESU-8; 'out' must have space for at least + * DUK_UNICODE_MAX_CESU8_LENGTH bytes; codepoints above U+10FFFF + * will encode to garbage but won't overwrite the output buffer. + */ +DUK_INTERNAL duk_small_int_t duk_unicode_encode_cesu8(duk_ucodepoint_t cp, duk_uint8_t *out) { + duk_uint_fast32_t x = (duk_uint_fast32_t) cp; + duk_small_int_t len; + + if (x < 0x80UL) { + out[0] = (duk_uint8_t) x; + len = 1; + } else if (x < 0x800UL) { + out[0] = (duk_uint8_t) (0xc0 + ((x >> 6) & 0x1f)); + out[1] = (duk_uint8_t) (0x80 + (x & 0x3f)); + len = 2; + } else if (x < 0x10000UL) { + /* surrogate pairs get encoded here */ + out[0] = (duk_uint8_t) (0xe0 + ((x >> 12) & 0x0f)); + out[1] = (duk_uint8_t) (0x80 + ((x >> 6) & 0x3f)); + out[2] = (duk_uint8_t) (0x80 + (x & 0x3f)); + len = 3; + } else { + /* + * Unicode codepoints above U+FFFF are encoded as surrogate + * pairs here. This ensures that all CESU-8 codepoints are + * 16-bit values as expected in Ecmascript. The surrogate + * pairs always get a 3-byte encoding (each) in CESU-8. + * See: http://en.wikipedia.org/wiki/Surrogate_pair + * + * 20-bit codepoint, 10 bits (A and B) per surrogate pair: + * + * x = 0b00000000 0000AAAA AAAAAABB BBBBBBBB + * sp1 = 0b110110AA AAAAAAAA (0xd800 + ((x >> 10) & 0x3ff)) + * sp2 = 0b110111BB BBBBBBBB (0xdc00 + (x & 0x3ff)) + * + * Encoded into CESU-8: + * + * sp1 -> 0b11101101 (0xe0 + ((sp1 >> 12) & 0x0f)) + * -> 0b1010AAAA (0x80 + ((sp1 >> 6) & 0x3f)) + * -> 0b10AAAAAA (0x80 + (sp1 & 0x3f)) + * sp2 -> 0b11101101 (0xe0 + ((sp2 >> 12) & 0x0f)) + * -> 0b1011BBBB (0x80 + ((sp2 >> 6) & 0x3f)) + * -> 0b10BBBBBB (0x80 + (sp2 & 0x3f)) + * + * Note that 0x10000 must be subtracted first. The code below + * avoids the sp1, sp2 temporaries which saves around 20 bytes + * of code. + */ + + x -= 0x10000UL; + + out[0] = (duk_uint8_t) (0xed); + out[1] = (duk_uint8_t) (0xa0 + ((x >> 16) & 0x0f)); + out[2] = (duk_uint8_t) (0x80 + ((x >> 10) & 0x3f)); + out[3] = (duk_uint8_t) (0xed); + out[4] = (duk_uint8_t) (0xb0 + ((x >> 6) & 0x0f)); + out[5] = (duk_uint8_t) (0x80 + (x & 0x3f)); + len = 6; + } + + return len; +} + +/* Decode helper. Return zero on error. */ +DUK_INTERNAL duk_small_int_t duk_unicode_decode_xutf8(duk_hthread *thr, const duk_uint8_t **ptr, const duk_uint8_t *ptr_start, const duk_uint8_t *ptr_end, duk_ucodepoint_t *out_cp) { + const duk_uint8_t *p; + duk_uint32_t res; + duk_uint_fast8_t ch; + duk_small_int_t n; + + DUK_UNREF(thr); + + p = *ptr; + if (p < ptr_start || p >= ptr_end) { + goto fail; + } + + /* + * UTF-8 decoder which accepts longer than standard byte sequences. + * This allows full 32-bit code points to be used. + */ + + ch = (duk_uint_fast8_t) (*p++); + if (ch < 0x80) { + /* 0xxx xxxx [7 bits] */ + res = (duk_uint32_t) (ch & 0x7f); + n = 0; + } else if (ch < 0xc0) { + /* 10xx xxxx -> invalid */ + goto fail; + } else if (ch < 0xe0) { + /* 110x xxxx 10xx xxxx [11 bits] */ + res = (duk_uint32_t) (ch & 0x1f); + n = 1; + } else if (ch < 0xf0) { + /* 1110 xxxx 10xx xxxx 10xx xxxx [16 bits] */ + res = (duk_uint32_t) (ch & 0x0f); + n = 2; + } else if (ch < 0xf8) { + /* 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx [21 bits] */ + res = (duk_uint32_t) (ch & 0x07); + n = 3; + } else if (ch < 0xfc) { + /* 1111 10xx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [26 bits] */ + res = (duk_uint32_t) (ch & 0x03); + n = 4; + } else if (ch < 0xfe) { + /* 1111 110x 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [31 bits] */ + res = (duk_uint32_t) (ch & 0x01); + n = 5; + } else if (ch < 0xff) { + /* 1111 1110 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [36 bits] */ + res = (duk_uint32_t) (0); + n = 6; + } else { + /* 8-byte format could be: + * 1111 1111 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [41 bits] + * + * However, this format would not have a zero bit following the + * leading one bits and would not allow 0xFF to be used as an + * "invalid xutf-8" marker for internal keys. Further, 8-byte + * encodings (up to 41 bit code points) are not currently needed. + */ + goto fail; + } + + DUK_ASSERT(p >= ptr_start); /* verified at beginning */ + if (p + n > ptr_end) { + /* check pointer at end */ + goto fail; + } + + while (n > 0) { + DUK_ASSERT(p >= ptr_start && p < ptr_end); + res = res << 6; + res += (duk_uint32_t) ((*p++) & 0x3f); + n--; + } + + *ptr = p; + *out_cp = res; + return 1; + + fail: + return 0; +} + +/* used by e.g. duk_regexp_executor.c, string built-ins */ +DUK_INTERNAL duk_ucodepoint_t duk_unicode_decode_xutf8_checked(duk_hthread *thr, const duk_uint8_t **ptr, const duk_uint8_t *ptr_start, const duk_uint8_t *ptr_end) { + duk_ucodepoint_t cp; + + if (duk_unicode_decode_xutf8(thr, ptr, ptr_start, ptr_end, &cp)) { + return cp; + } + DUK_ERROR_INTERNAL(thr, "utf-8 decode failed"); /* XXX: 'internal error' is a bit of a misnomer */ + DUK_UNREACHABLE(); + return 0; +} + +/* Compute (extended) utf-8 length without codepoint encoding validation, + * used for string interning. + * + * NOTE: This algorithm is performance critical, more so than string hashing + * in some cases. It is needed when interning a string and needs to scan + * every byte of the string with no skipping. Having an ASCII fast path + * is useful if possible in the algorithm. The current algorithms were + * chosen from several variants, based on x64 gcc -O2 testing. See: + * https://github.com/svaarala/duktape/pull/422 + * + * NOTE: must match src/dukutil.py:duk_unicode_unvalidated_utf8_length(). + */ + +#if defined(DUK_USE_PREFER_SIZE) +/* Small variant; roughly 150 bytes smaller than the fast variant. */ +DUK_INTERNAL duk_size_t duk_unicode_unvalidated_utf8_length(const duk_uint8_t *data, duk_size_t blen) { + const duk_uint8_t *p; + const duk_uint8_t *p_end; + duk_size_t ncont; + duk_size_t clen; + + p = data; + p_end = data + blen; + ncont = 0; + while (p != p_end) { + duk_uint8_t x; + x = *p++; + if (DUK_UNLIKELY(x >= 0x80 && x <= 0xbf)) { + ncont++; + } + } + + DUK_ASSERT(ncont <= blen); + clen = blen - ncont; + DUK_ASSERT(clen <= blen); + return clen; +} +#else /* DUK_USE_PREFER_SIZE */ +/* This seems like a good overall approach. Fast path for ASCII in 4 byte + * blocks. + */ +DUK_INTERNAL duk_size_t duk_unicode_unvalidated_utf8_length(const duk_uint8_t *data, duk_size_t blen) { + const duk_uint8_t *p; + const duk_uint8_t *p_end; + const duk_uint32_t *p32_end; + const duk_uint32_t *p32; + duk_size_t ncont; + duk_size_t clen; + + ncont = 0; /* number of continuation (non-initial) bytes in [0x80,0xbf] */ + p = data; + p_end = data + blen; + if (blen < 16) { + goto skip_fastpath; + } + + /* Align 'p' to 4; the input data may have arbitrary alignment. + * End of string check not needed because blen >= 16. + */ + while (((duk_size_t) (const void *) p) & 0x03U) { + duk_uint8_t x; + x = *p++; + if (DUK_UNLIKELY(x >= 0x80 && x <= 0xbf)) { + ncont++; + } + } + + /* Full, aligned 4-byte reads. */ + p32_end = (const duk_uint32_t *) (const void *) (p + ((duk_size_t) (p_end - p) & (duk_size_t) (~0x03))); + p32 = (const duk_uint32_t *) (const void *) p; + while (p32 != (const duk_uint32_t *) p32_end) { + duk_uint32_t x; + x = *p32++; + if (DUK_LIKELY((x & 0x80808080UL) == 0)) { + ; /* ASCII fast path */ + } else { + /* Flip highest bit of each byte which changes + * the bit pattern 10xxxxxx into 00xxxxxx which + * allows an easy bit mask test. + */ + x ^= 0x80808080UL; + if (DUK_UNLIKELY(!(x & 0xc0000000UL))) { + ncont++; + } + if (DUK_UNLIKELY(!(x & 0x00c00000UL))) { + ncont++; + } + if (DUK_UNLIKELY(!(x & 0x0000c000UL))) { + ncont++; + } + if (DUK_UNLIKELY(!(x & 0x000000c0UL))) { + ncont++; + } + } + } + p = (const duk_uint8_t *) p32; + /* Fall through to handle the rest. */ + + skip_fastpath: + while (p != p_end) { + duk_uint8_t x; + x = *p++; + if (DUK_UNLIKELY(x >= 0x80 && x <= 0xbf)) { + ncont++; + } + } + + DUK_ASSERT(ncont <= blen); + clen = blen - ncont; + DUK_ASSERT(clen <= blen); + return clen; +} +#endif /* DUK_USE_PREFER_SIZE */ + +/* + * Unicode range matcher + * + * Matches a codepoint against a packed bitstream of character ranges. + * Used for slow path Unicode matching. + */ + +/* Must match src/extract_chars.py, generate_match_table3(). */ +DUK_LOCAL duk_uint32_t duk__uni_decode_value(duk_bitdecoder_ctx *bd_ctx) { + duk_uint32_t t; + + t = (duk_uint32_t) duk_bd_decode(bd_ctx, 4); + if (t <= 0x0eU) { + return t; + } + t = (duk_uint32_t) duk_bd_decode(bd_ctx, 8); + if (t <= 0xfdU) { + return t + 0x0f; + } + if (t == 0xfeU) { + t = (duk_uint32_t) duk_bd_decode(bd_ctx, 12); + return t + 0x0fU + 0xfeU; + } else { + t = (duk_uint32_t) duk_bd_decode(bd_ctx, 24); + return t + 0x0fU + 0xfeU + 0x1000UL; + } +} + +DUK_LOCAL duk_small_int_t duk__uni_range_match(const duk_uint8_t *unitab, duk_size_t unilen, duk_codepoint_t cp) { + duk_bitdecoder_ctx bd_ctx; + duk_codepoint_t prev_re; + + DUK_MEMZERO(&bd_ctx, sizeof(bd_ctx)); + bd_ctx.data = (const duk_uint8_t *) unitab; + bd_ctx.length = (duk_size_t) unilen; + + prev_re = 0; + for (;;) { + duk_codepoint_t r1, r2; + r1 = (duk_codepoint_t) duk__uni_decode_value(&bd_ctx); + if (r1 == 0) { + break; + } + r2 = (duk_codepoint_t) duk__uni_decode_value(&bd_ctx); + + r1 = prev_re + r1; + r2 = r1 + r2; + prev_re = r2; + + /* [r1,r2] is the range */ + + DUK_DDD(DUK_DDDPRINT("duk__uni_range_match: cp=%06lx range=[0x%06lx,0x%06lx]", + (unsigned long) cp, (unsigned long) r1, (unsigned long) r2)); + if (cp >= r1 && cp <= r2) { + return 1; + } + } + + return 0; +} + +/* + * "WhiteSpace" production check. + */ + +DUK_INTERNAL duk_small_int_t duk_unicode_is_whitespace(duk_codepoint_t cp) { + /* + * E5 Section 7.2 specifies six characters specifically as + * white space: + * + * 0009;;Cc;0;S;;;;;N;CHARACTER TABULATION;;;; + * 000B;;Cc;0;S;;;;;N;LINE TABULATION;;;; + * 000C;;Cc;0;WS;;;;;N;FORM FEED (FF);;;; + * 0020;SPACE;Zs;0;WS;;;;;N;;;;; + * 00A0;NO-BREAK SPACE;Zs;0;CS; 0020;;;;N;NON-BREAKING SPACE;;;; + * FEFF;ZERO WIDTH NO-BREAK SPACE;Cf;0;BN;;;;;N;BYTE ORDER MARK;;;; + * + * It also specifies any Unicode category 'Zs' characters as white + * space. These can be extracted with the "src/extract_chars.py" script. + * Current result: + * + * RAW OUTPUT: + * =========== + * 0020;SPACE;Zs;0;WS;;;;;N;;;;; + * 00A0;NO-BREAK SPACE;Zs;0;CS; 0020;;;;N;NON-BREAKING SPACE;;;; + * 1680;OGHAM SPACE MARK;Zs;0;WS;;;;;N;;;;; + * 180E;MONGOLIAN VOWEL SEPARATOR;Zs;0;WS;;;;;N;;;;; + * 2000;EN QUAD;Zs;0;WS;2002;;;;N;;;;; + * 2001;EM QUAD;Zs;0;WS;2003;;;;N;;;;; + * 2002;EN SPACE;Zs;0;WS; 0020;;;;N;;;;; + * 2003;EM SPACE;Zs;0;WS; 0020;;;;N;;;;; + * 2004;THREE-PER-EM SPACE;Zs;0;WS; 0020;;;;N;;;;; + * 2005;FOUR-PER-EM SPACE;Zs;0;WS; 0020;;;;N;;;;; + * 2006;SIX-PER-EM SPACE;Zs;0;WS; 0020;;;;N;;;;; + * 2007;FIGURE SPACE;Zs;0;WS; 0020;;;;N;;;;; + * 2008;PUNCTUATION SPACE;Zs;0;WS; 0020;;;;N;;;;; + * 2009;THIN SPACE;Zs;0;WS; 0020;;;;N;;;;; + * 200A;HAIR SPACE;Zs;0;WS; 0020;;;;N;;;;; + * 202F;NARROW NO-BREAK SPACE;Zs;0;CS; 0020;;;;N;;;;; + * 205F;MEDIUM MATHEMATICAL SPACE;Zs;0;WS; 0020;;;;N;;;;; + * 3000;IDEOGRAPHIC SPACE;Zs;0;WS; 0020;;;;N;;;;; + * + * RANGES: + * ======= + * 0x0020 + * 0x00a0 + * 0x1680 + * 0x180e + * 0x2000 ... 0x200a + * 0x202f + * 0x205f + * 0x3000 + * + * A manual decoder (below) is probably most compact for this. + */ + + duk_uint_fast8_t lo; + duk_uint_fast32_t hi; + + /* cp == -1 (EOF) never matches and causes return value 0 */ + + lo = (duk_uint_fast8_t) (cp & 0xff); + hi = (duk_uint_fast32_t) (cp >> 8); /* does not fit into an uchar */ + + if (hi == 0x0000UL) { + if (lo == 0x09U || lo == 0x0bU || lo == 0x0cU || + lo == 0x20U || lo == 0xa0U) { + return 1; + } + } else if (hi == 0x0020UL) { + if (lo <= 0x0aU || lo == 0x2fU || lo == 0x5fU) { + return 1; + } + } else if (cp == 0x1680L || cp == 0x180eL || cp == 0x3000L || + cp == 0xfeffL) { + return 1; + } + + return 0; +} + +/* + * "LineTerminator" production check. + */ + +DUK_INTERNAL duk_small_int_t duk_unicode_is_line_terminator(duk_codepoint_t cp) { + /* + * E5 Section 7.3 + * + * A LineTerminatorSequence essentially merges sequences + * into a single line terminator. This must be handled by the caller. + */ + + if (cp == 0x000aL || cp == 0x000dL || cp == 0x2028L || + cp == 0x2029L) { + return 1; + } + + return 0; +} + +/* + * "IdentifierStart" production check. + */ + +DUK_INTERNAL duk_small_int_t duk_unicode_is_identifier_start(duk_codepoint_t cp) { + /* + * E5 Section 7.6: + * + * IdentifierStart: + * UnicodeLetter + * $ + * _ + * \ UnicodeEscapeSequence + * + * IdentifierStart production has one multi-character production: + * + * \ UnicodeEscapeSequence + * + * The '\' character is -not- matched by this function. Rather, the caller + * should decode the escape and then call this function to check whether the + * decoded character is acceptable (see discussion in E5 Section 7.6). + * + * The "UnicodeLetter" alternative of the production allows letters + * from various Unicode categories. These can be extracted with the + * "src/extract_chars.py" script. + * + * Because the result has hundreds of Unicode codepoint ranges, matching + * for any values >= 0x80 are done using a very slow range-by-range scan + * and a packed range format. + * + * The ASCII portion (codepoints 0x00 ... 0x7f) is fast-pathed below because + * it matters the most. The ASCII related ranges of IdentifierStart are: + * + * 0x0041 ... 0x005a ['A' ... 'Z'] + * 0x0061 ... 0x007a ['a' ... 'z'] + * 0x0024 ['$'] + * 0x005f ['_'] + */ + + /* ASCII (and EOF) fast path -- quick accept and reject */ + if (cp <= 0x7fL) { +#if defined(DUK_USE_IDCHAR_FASTPATH) + return (cp >= 0) && (duk_is_idchar_tab[cp] > 0); +#else + if ((cp >= 'a' && cp <= 'z') || + (cp >= 'A' && cp <= 'Z') || + cp == '_' || cp == '$') { + return 1; + } + return 0; +#endif + } + + /* Non-ASCII slow path (range-by-range linear comparison), very slow */ + +#ifdef DUK_USE_SOURCE_NONBMP + if (duk__uni_range_match(duk_unicode_ids_noa, + (duk_size_t) sizeof(duk_unicode_ids_noa), + (duk_codepoint_t) cp)) { + return 1; + } + return 0; +#else + if (cp < 0x10000L) { + if (duk__uni_range_match(duk_unicode_ids_noabmp, + sizeof(duk_unicode_ids_noabmp), + (duk_codepoint_t) cp)) { + return 1; + } + return 0; + } else { + /* without explicit non-BMP support, assume non-BMP characters + * are always accepted as identifier characters. + */ + return 1; + } +#endif +} + +/* + * "IdentifierPart" production check. + */ + +DUK_INTERNAL duk_small_int_t duk_unicode_is_identifier_part(duk_codepoint_t cp) { + /* + * E5 Section 7.6: + * + * IdentifierPart: + * IdentifierStart + * UnicodeCombiningMark + * UnicodeDigit + * UnicodeConnectorPunctuation + * [U+200C] + * [U+200D] + * + * IdentifierPart production has one multi-character production + * as part of its IdentifierStart alternative. The '\' character + * of an escape sequence is not matched here, see discussion in + * duk_unicode_is_identifier_start(). + * + * To match non-ASCII characters (codepoints >= 0x80), a very slow + * linear range-by-range scan is used. The codepoint is first compared + * to the IdentifierStart ranges, and if it doesn't match, then to a + * set consisting of code points in IdentifierPart but not in + * IdentifierStart. This is done to keep the unicode range data small, + * at the expense of speed. + * + * The ASCII fast path consists of: + * + * 0x0030 ... 0x0039 ['0' ... '9', UnicodeDigit] + * 0x0041 ... 0x005a ['A' ... 'Z', IdentifierStart] + * 0x0061 ... 0x007a ['a' ... 'z', IdentifierStart] + * 0x0024 ['$', IdentifierStart] + * 0x005f ['_', IdentifierStart and + * UnicodeConnectorPunctuation] + * + * UnicodeCombiningMark has no code points <= 0x7f. + * + * The matching code reuses the "identifier start" tables, and then + * consults a separate range set for characters in "identifier part" + * but not in "identifier start". These can be extracted with the + * "src/extract_chars.py" script. + * + * UnicodeCombiningMark -> categories Mn, Mc + * UnicodeDigit -> categories Nd + * UnicodeConnectorPunctuation -> categories Pc + */ + + /* ASCII (and EOF) fast path -- quick accept and reject */ + if (cp <= 0x7fL) { +#if defined(DUK_USE_IDCHAR_FASTPATH) + return (cp >= 0) && (duk_is_idchar_tab[cp] != 0); +#else + if ((cp >= 'a' && cp <= 'z') || + (cp >= 'A' && cp <= 'Z') || + (cp >= '0' && cp <= '9') || + cp == '_' || cp == '$') { + return 1; + } + return 0; +#endif + } + + /* Non-ASCII slow path (range-by-range linear comparison), very slow */ + +#ifdef DUK_USE_SOURCE_NONBMP + if (duk__uni_range_match(duk_unicode_ids_noa, + sizeof(duk_unicode_ids_noa), + (duk_codepoint_t) cp) || + duk__uni_range_match(duk_unicode_idp_m_ids_noa, + sizeof(duk_unicode_idp_m_ids_noa), + (duk_codepoint_t) cp)) { + return 1; + } + return 0; +#else + if (cp < 0x10000L) { + if (duk__uni_range_match(duk_unicode_ids_noabmp, + sizeof(duk_unicode_ids_noabmp), + (duk_codepoint_t) cp) || + duk__uni_range_match(duk_unicode_idp_m_ids_noabmp, + sizeof(duk_unicode_idp_m_ids_noabmp), + (duk_codepoint_t) cp)) { + return 1; + } + return 0; + } else { + /* without explicit non-BMP support, assume non-BMP characters + * are always accepted as identifier characters. + */ + return 1; + } +#endif +} + +/* + * Unicode letter check. + */ + +DUK_INTERNAL duk_small_int_t duk_unicode_is_letter(duk_codepoint_t cp) { + /* + * Unicode letter is now taken to be the categories: + * + * Lu, Ll, Lt, Lm, Lo + * + * (Not sure if this is exactly correct.) + * + * The ASCII fast path consists of: + * + * 0x0041 ... 0x005a ['A' ... 'Z'] + * 0x0061 ... 0x007a ['a' ... 'z'] + */ + + /* ASCII (and EOF) fast path -- quick accept and reject */ + if (cp <= 0x7fL) { + if ((cp >= 'a' && cp <= 'z') || + (cp >= 'A' && cp <= 'Z')) { + return 1; + } + return 0; + } + + /* Non-ASCII slow path (range-by-range linear comparison), very slow */ + +#ifdef DUK_USE_SOURCE_NONBMP + if (duk__uni_range_match(duk_unicode_ids_noa, + sizeof(duk_unicode_ids_noa), + (duk_codepoint_t) cp) && + !duk__uni_range_match(duk_unicode_ids_m_let_noa, + sizeof(duk_unicode_ids_m_let_noa), + (duk_codepoint_t) cp)) { + return 1; + } + return 0; +#else + if (cp < 0x10000L) { + if (duk__uni_range_match(duk_unicode_ids_noabmp, + sizeof(duk_unicode_ids_noabmp), + (duk_codepoint_t) cp) && + !duk__uni_range_match(duk_unicode_ids_m_let_noabmp, + sizeof(duk_unicode_ids_m_let_noabmp), + (duk_codepoint_t) cp)) { + return 1; + } + return 0; + } else { + /* without explicit non-BMP support, assume non-BMP characters + * are always accepted as letters. + */ + return 1; + } +#endif +} + +/* + * Complex case conversion helper which decodes a bit-packed conversion + * control stream generated by unicode/extract_caseconv.py. The conversion + * is very slow because it runs through the conversion data in a linear + * fashion to save space (which is why ASCII characters have a special + * fast path before arriving here). + * + * The particular bit counts etc have been determined experimentally to + * be small but still sufficient, and must match the Python script + * (src/extract_caseconv.py). + * + * The return value is the case converted codepoint or -1 if the conversion + * results in multiple characters (this is useful for regexp Canonicalization + * operation). If 'buf' is not NULL, the result codepoint(s) are also + * appended to the hbuffer. + * + * Context and locale specific rules must be checked before consulting + * this function. + */ + +DUK_LOCAL +duk_codepoint_t duk__slow_case_conversion(duk_hthread *thr, + duk_bufwriter_ctx *bw, + duk_codepoint_t cp, + duk_bitdecoder_ctx *bd_ctx) { + duk_small_int_t skip = 0; + duk_small_int_t n; + duk_small_int_t t; + duk_small_int_t count; + duk_codepoint_t tmp_cp; + duk_codepoint_t start_i; + duk_codepoint_t start_o; + + DUK_UNREF(thr); + DUK_ASSERT(bd_ctx != NULL); + + DUK_DDD(DUK_DDDPRINT("slow case conversion for codepoint: %ld", (long) cp)); + + /* range conversion with a "skip" */ + DUK_DDD(DUK_DDDPRINT("checking ranges")); + for (;;) { + skip++; + n = (duk_small_int_t) duk_bd_decode(bd_ctx, 6); + if (n == 0x3f) { + /* end marker */ + break; + } + DUK_DDD(DUK_DDDPRINT("skip=%ld, n=%ld", (long) skip, (long) n)); + + while (n--) { + start_i = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16); + start_o = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16); + count = (duk_small_int_t) duk_bd_decode(bd_ctx, 7); + DUK_DDD(DUK_DDDPRINT("range: start_i=%ld, start_o=%ld, count=%ld, skip=%ld", + (long) start_i, (long) start_o, (long) count, (long) skip)); + + if (cp >= start_i) { + tmp_cp = cp - start_i; /* always >= 0 */ + if (tmp_cp < (duk_codepoint_t) count * (duk_codepoint_t) skip && + (tmp_cp % (duk_codepoint_t) skip) == 0) { + DUK_DDD(DUK_DDDPRINT("range matches input codepoint")); + cp = start_o + tmp_cp; + goto single; + } + } + } + } + + /* 1:1 conversion */ + n = (duk_small_int_t) duk_bd_decode(bd_ctx, 6); + DUK_DDD(DUK_DDDPRINT("checking 1:1 conversions (count %ld)", (long) n)); + while (n--) { + start_i = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16); + start_o = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16); + DUK_DDD(DUK_DDDPRINT("1:1 conversion %ld -> %ld", (long) start_i, (long) start_o)); + if (cp == start_i) { + DUK_DDD(DUK_DDDPRINT("1:1 matches input codepoint")); + cp = start_o; + goto single; + } + } + + /* complex, multicharacter conversion */ + n = (duk_small_int_t) duk_bd_decode(bd_ctx, 7); + DUK_DDD(DUK_DDDPRINT("checking 1:n conversions (count %ld)", (long) n)); + while (n--) { + start_i = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16); + t = (duk_small_int_t) duk_bd_decode(bd_ctx, 2); + DUK_DDD(DUK_DDDPRINT("1:n conversion %ld -> %ld chars", (long) start_i, (long) t)); + if (cp == start_i) { + DUK_DDD(DUK_DDDPRINT("1:n matches input codepoint")); + if (bw != NULL) { + while (t--) { + tmp_cp = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16); + DUK_BW_WRITE_RAW_XUTF8(thr, bw, (duk_ucodepoint_t) tmp_cp); + } + } + return -1; + } else { + while (t--) { + (void) duk_bd_decode(bd_ctx, 16); + } + } + } + + /* default: no change */ + DUK_DDD(DUK_DDDPRINT("no rule matches, output is same as input")); + /* fall through */ + + single: + if (bw != NULL) { + DUK_BW_WRITE_RAW_XUTF8(thr, bw, (duk_ucodepoint_t) cp); + } + return cp; +} + +/* + * Case conversion helper, with context/local sensitivity. + * For proper case conversion, one needs to know the character + * and the preceding and following characters, as well as + * locale/language. + */ + +/* XXX: add 'language' argument when locale/language sensitive rule + * support added. + */ +DUK_LOCAL +duk_codepoint_t duk__case_transform_helper(duk_hthread *thr, + duk_bufwriter_ctx *bw, + duk_codepoint_t cp, + duk_codepoint_t prev, + duk_codepoint_t next, + duk_bool_t uppercase) { + duk_bitdecoder_ctx bd_ctx; + + /* fast path for ASCII */ + if (cp < 0x80L) { + /* XXX: there are language sensitive rules for the ASCII range. + * If/when language/locale support is implemented, they need to + * be implemented here for the fast path. There are no context + * sensitive rules for ASCII range. + */ + + if (uppercase) { + if (cp >= 'a' && cp <= 'z') { + cp = cp - 'a' + 'A'; + } + } else { + if (cp >= 'A' && cp <= 'Z') { + cp = cp - 'A' + 'a'; + } + } + + if (bw != NULL) { + DUK_BW_WRITE_RAW_U8(thr, bw, (duk_uint8_t) cp); + } + return cp; + } + + /* context and locale specific rules which cannot currently be represented + * in the caseconv bitstream: hardcoded rules in C + */ + if (uppercase) { + /* XXX: turkish / azeri */ + } else { + /* + * Final sigma context specific rule. This is a rather tricky + * rule and this handling is probably not 100% correct now. + * The rule is not locale/language specific so it is supported. + */ + + if (cp == 0x03a3L && /* U+03A3 = GREEK CAPITAL LETTER SIGMA */ + duk_unicode_is_letter(prev) && /* prev exists and is not a letter */ + !duk_unicode_is_letter(next)) { /* next does not exist or next is not a letter */ + /* Capital sigma occurred at "end of word", lowercase to + * U+03C2 = GREEK SMALL LETTER FINAL SIGMA. Otherwise + * fall through and let the normal rules lowercase it to + * U+03C3 = GREEK SMALL LETTER SIGMA. + */ + cp = 0x03c2L; + goto singlechar; + } + + /* XXX: lithuanian not implemented */ + /* XXX: lithuanian, explicit dot rules */ + /* XXX: turkish / azeri, lowercase rules */ + } + + /* 1:1 or special conversions, but not locale/context specific: script generated rules */ + DUK_MEMZERO(&bd_ctx, sizeof(bd_ctx)); + if (uppercase) { + bd_ctx.data = (const duk_uint8_t *) duk_unicode_caseconv_uc; + bd_ctx.length = (duk_size_t) sizeof(duk_unicode_caseconv_uc); + } else { + bd_ctx.data = (const duk_uint8_t *) duk_unicode_caseconv_lc; + bd_ctx.length = (duk_size_t) sizeof(duk_unicode_caseconv_lc); + } + return duk__slow_case_conversion(thr, bw, cp, &bd_ctx); + + singlechar: + if (bw != NULL) { + DUK_BW_WRITE_RAW_XUTF8(thr, bw, (duk_ucodepoint_t) cp); + } + return cp; + + /* unused now, not needed until Turkish/Azeri */ +#if 0 + nochar: + return -1; +#endif +} + +/* + * Replace valstack top with case converted version. + */ + +DUK_INTERNAL void duk_unicode_case_convert_string(duk_hthread *thr, duk_small_int_t uppercase) { + duk_context *ctx = (duk_context *) thr; + duk_hstring *h_input; + duk_bufwriter_ctx bw_alloc; + duk_bufwriter_ctx *bw; + const duk_uint8_t *p, *p_start, *p_end; + duk_codepoint_t prev, curr, next; + + h_input = duk_require_hstring(ctx, -1); + DUK_ASSERT(h_input != NULL); + + bw = &bw_alloc; + DUK_BW_INIT_PUSHBUF(thr, bw, DUK_HSTRING_GET_BYTELEN(h_input)); + + /* [ ... input buffer ] */ + + p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input); + p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input); + p = p_start; + + prev = -1; DUK_UNREF(prev); + curr = -1; + next = -1; + for (;;) { + prev = curr; + curr = next; + next = -1; + if (p < p_end) { + next = (int) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end); + } else { + /* end of input and last char has been processed */ + if (curr < 0) { + break; + } + } + + /* on first round, skip */ + if (curr >= 0) { + /* XXX: could add a fast path to process chunks of input codepoints, + * but relative benefit would be quite small. + */ + + /* Ensure space for maximum multi-character result; estimate is overkill. */ + DUK_BW_ENSURE(thr, bw, 8 * DUK_UNICODE_MAX_XUTF8_LENGTH); + + duk__case_transform_helper(thr, + bw, + (duk_codepoint_t) curr, + prev, + next, + uppercase); + } + } + + DUK_BW_COMPACT(thr, bw); + duk_to_string(ctx, -1); /* invalidates h_buf pointer */ + duk_remove(ctx, -2); +} + +#ifdef DUK_USE_REGEXP_SUPPORT + +/* + * Canonicalize() abstract operation needed for canonicalization of individual + * codepoints during regexp compilation and execution, see E5 Section 15.10.2.8. + * Note that codepoints are canonicalized one character at a time, so no context + * specific rules can apply. Locale specific rules can apply, though. + */ + +DUK_INTERNAL duk_codepoint_t duk_unicode_re_canonicalize_char(duk_hthread *thr, duk_codepoint_t cp) { +#if defined(DUK_USE_REGEXP_CANON_WORKAROUND) + /* Fast canonicalization lookup at the cost of 128kB footprint. */ + DUK_ASSERT(cp >= 0); + DUK_UNREF(thr); + if (DUK_LIKELY(cp < 0x10000L)) { + return (duk_codepoint_t) duk_unicode_re_canon_lookup[cp]; + } + return cp; +#else /* DUK_USE_REGEXP_CANON_WORKAROUND */ + duk_codepoint_t y; + + y = duk__case_transform_helper(thr, + NULL, /* NULL is allowed, no output */ + cp, /* curr char */ + -1, /* prev char */ + -1, /* next char */ + 1); /* uppercase */ + + if ((y < 0) || (cp >= 0x80 && y < 0x80)) { + /* multiple codepoint conversion or non-ASCII mapped to ASCII + * --> leave as is. + */ + return cp; + } + + return y; +#endif /* DUK_USE_REGEXP_CANON_WORKAROUND */ +} + +/* + * E5 Section 15.10.2.6 "IsWordChar" abstract operation. Assume + * x < 0 for characters read outside the string. + */ + +DUK_INTERNAL duk_small_int_t duk_unicode_re_is_wordchar(duk_codepoint_t x) { + /* + * Note: the description in E5 Section 15.10.2.6 has a typo, it + * contains 'A' twice and lacks 'a'; the intent is [0-9a-zA-Z_]. + */ + if ((x >= '0' && x <= '9') || + (x >= 'a' && x <= 'z') || + (x >= 'A' && x <= 'Z') || + (x == '_')) { + return 1; + } + return 0; +} + +/* + * Regexp range tables + */ + +/* exposed because lexer needs these too */ +DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_digit[2] = { + (duk_uint16_t) 0x0030UL, (duk_uint16_t) 0x0039UL, +}; +DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_white[22] = { + (duk_uint16_t) 0x0009UL, (duk_uint16_t) 0x000DUL, + (duk_uint16_t) 0x0020UL, (duk_uint16_t) 0x0020UL, + (duk_uint16_t) 0x00A0UL, (duk_uint16_t) 0x00A0UL, + (duk_uint16_t) 0x1680UL, (duk_uint16_t) 0x1680UL, + (duk_uint16_t) 0x180EUL, (duk_uint16_t) 0x180EUL, + (duk_uint16_t) 0x2000UL, (duk_uint16_t) 0x200AUL, + (duk_uint16_t) 0x2028UL, (duk_uint16_t) 0x2029UL, + (duk_uint16_t) 0x202FUL, (duk_uint16_t) 0x202FUL, + (duk_uint16_t) 0x205FUL, (duk_uint16_t) 0x205FUL, + (duk_uint16_t) 0x3000UL, (duk_uint16_t) 0x3000UL, + (duk_uint16_t) 0xFEFFUL, (duk_uint16_t) 0xFEFFUL, +}; +DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_wordchar[8] = { + (duk_uint16_t) 0x0030UL, (duk_uint16_t) 0x0039UL, + (duk_uint16_t) 0x0041UL, (duk_uint16_t) 0x005AUL, + (duk_uint16_t) 0x005FUL, (duk_uint16_t) 0x005FUL, + (duk_uint16_t) 0x0061UL, (duk_uint16_t) 0x007AUL, +}; +DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_not_digit[4] = { + (duk_uint16_t) 0x0000UL, (duk_uint16_t) 0x002FUL, + (duk_uint16_t) 0x003AUL, (duk_uint16_t) 0xFFFFUL, +}; +DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_not_white[24] = { + (duk_uint16_t) 0x0000UL, (duk_uint16_t) 0x0008UL, + (duk_uint16_t) 0x000EUL, (duk_uint16_t) 0x001FUL, + (duk_uint16_t) 0x0021UL, (duk_uint16_t) 0x009FUL, + (duk_uint16_t) 0x00A1UL, (duk_uint16_t) 0x167FUL, + (duk_uint16_t) 0x1681UL, (duk_uint16_t) 0x180DUL, + (duk_uint16_t) 0x180FUL, (duk_uint16_t) 0x1FFFUL, + (duk_uint16_t) 0x200BUL, (duk_uint16_t) 0x2027UL, + (duk_uint16_t) 0x202AUL, (duk_uint16_t) 0x202EUL, + (duk_uint16_t) 0x2030UL, (duk_uint16_t) 0x205EUL, + (duk_uint16_t) 0x2060UL, (duk_uint16_t) 0x2FFFUL, + (duk_uint16_t) 0x3001UL, (duk_uint16_t) 0xFEFEUL, + (duk_uint16_t) 0xFF00UL, (duk_uint16_t) 0xFFFFUL, +}; +DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_not_wordchar[10] = { + (duk_uint16_t) 0x0000UL, (duk_uint16_t) 0x002FUL, + (duk_uint16_t) 0x003AUL, (duk_uint16_t) 0x0040UL, + (duk_uint16_t) 0x005BUL, (duk_uint16_t) 0x005EUL, + (duk_uint16_t) 0x0060UL, (duk_uint16_t) 0x0060UL, + (duk_uint16_t) 0x007BUL, (duk_uint16_t) 0xFFFFUL, +}; + +#endif /* DUK_USE_REGEXP_SUPPORT */ +/* + * Misc util stuff + */ + +/* include removed: duk_internal.h */ + +/* + * Lowercase digits for radix values 2 to 36. Also doubles as lowercase + * hex nybble table. + */ + +DUK_INTERNAL const duk_uint8_t duk_lc_digits[36] = { + DUK_ASC_0, DUK_ASC_1, DUK_ASC_2, DUK_ASC_3, + DUK_ASC_4, DUK_ASC_5, DUK_ASC_6, DUK_ASC_7, + DUK_ASC_8, DUK_ASC_9, DUK_ASC_LC_A, DUK_ASC_LC_B, + DUK_ASC_LC_C, DUK_ASC_LC_D, DUK_ASC_LC_E, DUK_ASC_LC_F, + DUK_ASC_LC_G, DUK_ASC_LC_H, DUK_ASC_LC_I, DUK_ASC_LC_J, + DUK_ASC_LC_K, DUK_ASC_LC_L, DUK_ASC_LC_M, DUK_ASC_LC_N, + DUK_ASC_LC_O, DUK_ASC_LC_P, DUK_ASC_LC_Q, DUK_ASC_LC_R, + DUK_ASC_LC_S, DUK_ASC_LC_T, DUK_ASC_LC_U, DUK_ASC_LC_V, + DUK_ASC_LC_W, DUK_ASC_LC_X, DUK_ASC_LC_Y, DUK_ASC_LC_Z +}; + +DUK_INTERNAL const duk_uint8_t duk_uc_nybbles[16] = { + DUK_ASC_0, DUK_ASC_1, DUK_ASC_2, DUK_ASC_3, + DUK_ASC_4, DUK_ASC_5, DUK_ASC_6, DUK_ASC_7, + DUK_ASC_8, DUK_ASC_9, DUK_ASC_UC_A, DUK_ASC_UC_B, + DUK_ASC_UC_C, DUK_ASC_UC_D, DUK_ASC_UC_E, DUK_ASC_UC_F +}; + +/* + * Table for hex decoding ASCII hex digits + */ + +DUK_INTERNAL const duk_int8_t duk_hex_dectab[256] = { + /* -1 if invalid */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2f */ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0x30-0x3f */ + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5f */ + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x60-0x6f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x70-0x7f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x80-0x8f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x90-0x9f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xa0-0xaf */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xb0-0xbf */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xc0-0xcf */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xd0-0xdf */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xe0-0xef */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 0xf0-0xff */ +}; + +#if defined(DUK_USE_HEX_FASTPATH) +/* Preshifted << 4. Must use 16-bit entry to allow negative value signaling. */ +DUK_INTERNAL const duk_int16_t duk_hex_dectab_shift4[256] = { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2f */ + 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, -1, -1, -1, -1, -1, -1, /* 0x30-0x3f */ + -1, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5f */ + -1, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x60-0x6f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x70-0x7f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x80-0x8f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x90-0x9f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xa0-0xaf */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xb0-0xbf */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xc0-0xcf */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xd0-0xdf */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xe0-0xef */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 0xf0-0xff */ +}; +#endif + +/* + * Table for hex encoding bytes + */ + +#if defined(DUK_USE_HEX_FASTPATH) +/* Lookup to encode one byte directly into 2 characters: + * + * def genhextab(bswap): + * for i in xrange(256): + * t = chr(i).encode('hex') + * if bswap: + * t = t[1] + t[0] + * print('0x' + t.encode('hex') + 'U') + * print('big endian'); genhextab(False) + * print('little endian'); genhextab(True) +*/ +DUK_INTERNAL const duk_uint16_t duk_hex_enctab[256] = { +#if defined(DUK_USE_INTEGER_BE) + 0x3030U, 0x3031U, 0x3032U, 0x3033U, 0x3034U, 0x3035U, 0x3036U, 0x3037U, + 0x3038U, 0x3039U, 0x3061U, 0x3062U, 0x3063U, 0x3064U, 0x3065U, 0x3066U, + 0x3130U, 0x3131U, 0x3132U, 0x3133U, 0x3134U, 0x3135U, 0x3136U, 0x3137U, + 0x3138U, 0x3139U, 0x3161U, 0x3162U, 0x3163U, 0x3164U, 0x3165U, 0x3166U, + 0x3230U, 0x3231U, 0x3232U, 0x3233U, 0x3234U, 0x3235U, 0x3236U, 0x3237U, + 0x3238U, 0x3239U, 0x3261U, 0x3262U, 0x3263U, 0x3264U, 0x3265U, 0x3266U, + 0x3330U, 0x3331U, 0x3332U, 0x3333U, 0x3334U, 0x3335U, 0x3336U, 0x3337U, + 0x3338U, 0x3339U, 0x3361U, 0x3362U, 0x3363U, 0x3364U, 0x3365U, 0x3366U, + 0x3430U, 0x3431U, 0x3432U, 0x3433U, 0x3434U, 0x3435U, 0x3436U, 0x3437U, + 0x3438U, 0x3439U, 0x3461U, 0x3462U, 0x3463U, 0x3464U, 0x3465U, 0x3466U, + 0x3530U, 0x3531U, 0x3532U, 0x3533U, 0x3534U, 0x3535U, 0x3536U, 0x3537U, + 0x3538U, 0x3539U, 0x3561U, 0x3562U, 0x3563U, 0x3564U, 0x3565U, 0x3566U, + 0x3630U, 0x3631U, 0x3632U, 0x3633U, 0x3634U, 0x3635U, 0x3636U, 0x3637U, + 0x3638U, 0x3639U, 0x3661U, 0x3662U, 0x3663U, 0x3664U, 0x3665U, 0x3666U, + 0x3730U, 0x3731U, 0x3732U, 0x3733U, 0x3734U, 0x3735U, 0x3736U, 0x3737U, + 0x3738U, 0x3739U, 0x3761U, 0x3762U, 0x3763U, 0x3764U, 0x3765U, 0x3766U, + 0x3830U, 0x3831U, 0x3832U, 0x3833U, 0x3834U, 0x3835U, 0x3836U, 0x3837U, + 0x3838U, 0x3839U, 0x3861U, 0x3862U, 0x3863U, 0x3864U, 0x3865U, 0x3866U, + 0x3930U, 0x3931U, 0x3932U, 0x3933U, 0x3934U, 0x3935U, 0x3936U, 0x3937U, + 0x3938U, 0x3939U, 0x3961U, 0x3962U, 0x3963U, 0x3964U, 0x3965U, 0x3966U, + 0x6130U, 0x6131U, 0x6132U, 0x6133U, 0x6134U, 0x6135U, 0x6136U, 0x6137U, + 0x6138U, 0x6139U, 0x6161U, 0x6162U, 0x6163U, 0x6164U, 0x6165U, 0x6166U, + 0x6230U, 0x6231U, 0x6232U, 0x6233U, 0x6234U, 0x6235U, 0x6236U, 0x6237U, + 0x6238U, 0x6239U, 0x6261U, 0x6262U, 0x6263U, 0x6264U, 0x6265U, 0x6266U, + 0x6330U, 0x6331U, 0x6332U, 0x6333U, 0x6334U, 0x6335U, 0x6336U, 0x6337U, + 0x6338U, 0x6339U, 0x6361U, 0x6362U, 0x6363U, 0x6364U, 0x6365U, 0x6366U, + 0x6430U, 0x6431U, 0x6432U, 0x6433U, 0x6434U, 0x6435U, 0x6436U, 0x6437U, + 0x6438U, 0x6439U, 0x6461U, 0x6462U, 0x6463U, 0x6464U, 0x6465U, 0x6466U, + 0x6530U, 0x6531U, 0x6532U, 0x6533U, 0x6534U, 0x6535U, 0x6536U, 0x6537U, + 0x6538U, 0x6539U, 0x6561U, 0x6562U, 0x6563U, 0x6564U, 0x6565U, 0x6566U, + 0x6630U, 0x6631U, 0x6632U, 0x6633U, 0x6634U, 0x6635U, 0x6636U, 0x6637U, + 0x6638U, 0x6639U, 0x6661U, 0x6662U, 0x6663U, 0x6664U, 0x6665U, 0x6666U +#else /* DUK_USE_INTEGER_BE */ + 0x3030U, 0x3130U, 0x3230U, 0x3330U, 0x3430U, 0x3530U, 0x3630U, 0x3730U, + 0x3830U, 0x3930U, 0x6130U, 0x6230U, 0x6330U, 0x6430U, 0x6530U, 0x6630U, + 0x3031U, 0x3131U, 0x3231U, 0x3331U, 0x3431U, 0x3531U, 0x3631U, 0x3731U, + 0x3831U, 0x3931U, 0x6131U, 0x6231U, 0x6331U, 0x6431U, 0x6531U, 0x6631U, + 0x3032U, 0x3132U, 0x3232U, 0x3332U, 0x3432U, 0x3532U, 0x3632U, 0x3732U, + 0x3832U, 0x3932U, 0x6132U, 0x6232U, 0x6332U, 0x6432U, 0x6532U, 0x6632U, + 0x3033U, 0x3133U, 0x3233U, 0x3333U, 0x3433U, 0x3533U, 0x3633U, 0x3733U, + 0x3833U, 0x3933U, 0x6133U, 0x6233U, 0x6333U, 0x6433U, 0x6533U, 0x6633U, + 0x3034U, 0x3134U, 0x3234U, 0x3334U, 0x3434U, 0x3534U, 0x3634U, 0x3734U, + 0x3834U, 0x3934U, 0x6134U, 0x6234U, 0x6334U, 0x6434U, 0x6534U, 0x6634U, + 0x3035U, 0x3135U, 0x3235U, 0x3335U, 0x3435U, 0x3535U, 0x3635U, 0x3735U, + 0x3835U, 0x3935U, 0x6135U, 0x6235U, 0x6335U, 0x6435U, 0x6535U, 0x6635U, + 0x3036U, 0x3136U, 0x3236U, 0x3336U, 0x3436U, 0x3536U, 0x3636U, 0x3736U, + 0x3836U, 0x3936U, 0x6136U, 0x6236U, 0x6336U, 0x6436U, 0x6536U, 0x6636U, + 0x3037U, 0x3137U, 0x3237U, 0x3337U, 0x3437U, 0x3537U, 0x3637U, 0x3737U, + 0x3837U, 0x3937U, 0x6137U, 0x6237U, 0x6337U, 0x6437U, 0x6537U, 0x6637U, + 0x3038U, 0x3138U, 0x3238U, 0x3338U, 0x3438U, 0x3538U, 0x3638U, 0x3738U, + 0x3838U, 0x3938U, 0x6138U, 0x6238U, 0x6338U, 0x6438U, 0x6538U, 0x6638U, + 0x3039U, 0x3139U, 0x3239U, 0x3339U, 0x3439U, 0x3539U, 0x3639U, 0x3739U, + 0x3839U, 0x3939U, 0x6139U, 0x6239U, 0x6339U, 0x6439U, 0x6539U, 0x6639U, + 0x3061U, 0x3161U, 0x3261U, 0x3361U, 0x3461U, 0x3561U, 0x3661U, 0x3761U, + 0x3861U, 0x3961U, 0x6161U, 0x6261U, 0x6361U, 0x6461U, 0x6561U, 0x6661U, + 0x3062U, 0x3162U, 0x3262U, 0x3362U, 0x3462U, 0x3562U, 0x3662U, 0x3762U, + 0x3862U, 0x3962U, 0x6162U, 0x6262U, 0x6362U, 0x6462U, 0x6562U, 0x6662U, + 0x3063U, 0x3163U, 0x3263U, 0x3363U, 0x3463U, 0x3563U, 0x3663U, 0x3763U, + 0x3863U, 0x3963U, 0x6163U, 0x6263U, 0x6363U, 0x6463U, 0x6563U, 0x6663U, + 0x3064U, 0x3164U, 0x3264U, 0x3364U, 0x3464U, 0x3564U, 0x3664U, 0x3764U, + 0x3864U, 0x3964U, 0x6164U, 0x6264U, 0x6364U, 0x6464U, 0x6564U, 0x6664U, + 0x3065U, 0x3165U, 0x3265U, 0x3365U, 0x3465U, 0x3565U, 0x3665U, 0x3765U, + 0x3865U, 0x3965U, 0x6165U, 0x6265U, 0x6365U, 0x6465U, 0x6565U, 0x6665U, + 0x3066U, 0x3166U, 0x3266U, 0x3366U, 0x3466U, 0x3566U, 0x3666U, 0x3766U, + 0x3866U, 0x3966U, 0x6166U, 0x6266U, 0x6366U, 0x6466U, 0x6566U, 0x6666U +#endif /* DUK_USE_INTEGER_BE */ +}; +#endif /* DUK_USE_HEX_FASTPATH */ + +/* + * Table for base-64 encoding + */ + +#if defined(DUK_USE_BASE64_FASTPATH) +DUK_INTERNAL const duk_uint8_t duk_base64_enctab[64] = { + 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, /* A...P */ + 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, /* Q...f */ + 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, /* g...v */ + 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f /* w.../ */ +}; +#endif /* DUK_USE_BASE64_FASTPATH */ + +/* + * Table for base-64 decoding + */ + +#if defined(DUK_USE_BASE64_FASTPATH) +DUK_INTERNAL const duk_int8_t duk_base64_dectab[256] = { + /* -1 = error, -2 = allowed whitespace, -3 = padding ('='), 0...63 decoded bytes */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1, /* 0x00...0x0f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10...0x1f */ + -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20...0x2f */ + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -3, -1, -1, /* 0x30...0x3f */ + -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40...0x4f */ + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50...0x5f */ + -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60...0x6f */ + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, /* 0x70...0x7f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x80...0x8f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x90...0x9f */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xa0...0xaf */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xb0...0xbf */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xc0...0xcf */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xd0...0xdf */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xe0...0xef */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 0xf0...0xff */ +}; +#endif /* DUK_USE_BASE64_FASTPATH */ + +/* + * Arbitrary byteswap for potentially unaligned values + * + * Used to byteswap pointers e.g. in debugger code. + */ + +#if defined(DUK_USE_DEBUGGER_SUPPORT) /* For now only needed by the debugger. */ +DUK_INTERNAL void duk_byteswap_bytes(duk_uint8_t *p, duk_small_uint_t len) { + duk_uint8_t tmp; + duk_uint8_t *q = p + len - 1; + + while (p - q < 0) { + tmp = *p; + *p = *q; + *q = tmp; + p++; + q--; + } +} +#endif +/* + * Round a number upwards to a prime (not usually the nearest one). + * + * Uses a table of successive 32-bit primes whose ratio is roughly + * constant. This keeps the relative upwards 'rounding error' bounded + * and the data size small. A simple 'predict-correct' compression is + * used to compress primes to one byte per prime. See genhashsizes.py + * for details. + * + * The minimum prime returned here must be coordinated with the possible + * probe sequence steps in duk_hobject and duk_heap stringtable. + */ + +/* include removed: duk_internal.h */ + +/* Awkward inclusion condition: drop out of compilation if not needed by any + * call site: object hash part or probing stringtable. + */ +#if defined(DUK_USE_HOBJECT_HASH_PART) || defined(DUK_USE_STRTAB_PROBE) + +/* hash size ratio goal, must match genhashsizes.py */ +#define DUK__HASH_SIZE_RATIO 1177 /* floor(1.15 * (1 << 10)) */ + +/* prediction corrections for prime list (see genhashsizes.py) */ +DUK_LOCAL const duk_int8_t duk__hash_size_corrections[] = { + 17, /* minimum prime */ + 4, 3, 4, 1, 4, 1, 1, 2, 2, 2, 2, 1, 6, 6, 9, 5, 1, 2, 2, 5, 1, 3, 3, 3, + 5, 4, 4, 2, 4, 8, 3, 4, 23, 2, 4, 7, 8, 11, 2, 12, 15, 10, 1, 1, 5, 1, 5, + 8, 9, 17, 14, 10, 7, 5, 2, 46, 21, 1, 9, 9, 4, 4, 10, 23, 36, 6, 20, 29, + 18, 6, 19, 21, 16, 11, 5, 5, 48, 9, 1, 39, 14, 8, 4, 29, 9, 1, 15, 48, 12, + 22, 6, 15, 27, 4, 2, 17, 28, 8, 9, 4, 5, 8, 3, 3, 8, 37, 11, 15, 8, 30, + 43, 6, 33, 41, 5, 20, 32, 41, 38, 24, 77, 14, 19, 11, 4, 35, 18, 19, 41, + 10, 23, 16, 9, 2, + -1 +}; + +/* probe steps (see genhashsizes.py), currently assumed to be 32 entries long + * (DUK_UTIL_GET_HASH_PROBE_STEP macro). + */ +DUK_INTERNAL duk_uint8_t duk_util_probe_steps[32] = { + 2, 3, 5, 7, 11, 13, 19, 31, 41, 47, 59, 67, 73, 79, 89, 101, 103, 107, + 109, 127, 137, 139, 149, 157, 163, 167, 173, 181, 191, 193, 197, 199 +}; + +DUK_INTERNAL duk_uint32_t duk_util_get_hash_prime(duk_uint32_t size) { + const duk_int8_t *p = duk__hash_size_corrections; + duk_uint32_t curr; + + curr = (duk_uint32_t) *p++; + for (;;) { + duk_small_int_t t = (duk_small_int_t) *p++; + if (t < 0) { + /* may happen if size is very close to 2^32-1 */ + break; + } + + /* prediction: portable variant using doubles if 64-bit values not available */ +#ifdef DUK_USE_64BIT_OPS + curr = (duk_uint32_t) ((((duk_uint64_t) curr) * ((duk_uint64_t) DUK__HASH_SIZE_RATIO)) >> 10); +#else + /* 32-bit x 11-bit = 43-bit, fits accurately into a double */ + curr = (duk_uint32_t) DUK_FLOOR(((double) curr) * ((double) DUK__HASH_SIZE_RATIO) / 1024.0); +#endif + + /* correction */ + curr += t; + + DUK_DDD(DUK_DDDPRINT("size=%ld, curr=%ld", (long) size, (long) curr)); + + if (curr >= size) { + return curr; + } + } + return 0; +} + +#endif /* DUK_USE_HOBJECT_HASH_PART || DUK_USE_STRTAB_PROBE */ +/* + * Hobject Ecmascript [[Class]]. + */ + +/* include removed: duk_internal.h */ + +#if (DUK_STRIDX_UC_ARGUMENTS > 255) +#error constant too large +#endif +#if (DUK_STRIDX_ARRAY > 255) +#error constant too large +#endif +#if (DUK_STRIDX_UC_BOOLEAN > 255) +#error constant too large +#endif +#if (DUK_STRIDX_DATE > 255) +#error constant too large +#endif +#if (DUK_STRIDX_UC_ERROR > 255) +#error constant too large +#endif +#if (DUK_STRIDX_UC_FUNCTION > 255) +#error constant too large +#endif +#if (DUK_STRIDX_JSON > 255) +#error constant too large +#endif +#if (DUK_STRIDX_MATH > 255) +#error constant too large +#endif +#if (DUK_STRIDX_UC_NUMBER > 255) +#error constant too large +#endif +#if (DUK_STRIDX_UC_OBJECT > 255) +#error constant too large +#endif +#if (DUK_STRIDX_REG_EXP > 255) +#error constant too large +#endif +#if (DUK_STRIDX_UC_STRING > 255) +#error constant too large +#endif +#if (DUK_STRIDX_GLOBAL > 255) +#error constant too large +#endif +#if (DUK_STRIDX_OBJ_ENV > 255) +#error constant too large +#endif +#if (DUK_STRIDX_DEC_ENV > 255) +#error constant too large +#endif +#if (DUK_STRIDX_UC_BUFFER > 255) +#error constant too large +#endif +#if (DUK_STRIDX_UC_POINTER > 255) +#error constant too large +#endif +#if (DUK_STRIDX_UC_THREAD > 255) +#error constant too large +#endif +#if (DUK_STRIDX_ARRAY_BUFFER > 255) +#error constant too large +#endif +#if (DUK_STRIDX_DATA_VIEW > 255) +#error constant too large +#endif +#if (DUK_STRIDX_INT8_ARRAY > 255) +#error constant too large +#endif +#if (DUK_STRIDX_UINT8_ARRAY > 255) +#error constant too large +#endif +#if (DUK_STRIDX_UINT8_CLAMPED_ARRAY > 255) +#error constant too large +#endif +#if (DUK_STRIDX_INT16_ARRAY > 255) +#error constant too large +#endif +#if (DUK_STRIDX_UINT16_ARRAY > 255) +#error constant too large +#endif +#if (DUK_STRIDX_INT32_ARRAY > 255) +#error constant too large +#endif +#if (DUK_STRIDX_UINT32_ARRAY > 255) +#error constant too large +#endif +#if (DUK_STRIDX_FLOAT32_ARRAY > 255) +#error constant too large +#endif +#if (DUK_STRIDX_FLOAT64_ARRAY > 255) +#error constant too large +#endif +#if (DUK_STRIDX_EMPTY_STRING > 255) +#error constant too large +#endif + +/* Note: assumes that these string indexes are 8-bit, genstrings.py must ensure that */ +DUK_INTERNAL duk_uint8_t duk_class_number_to_stridx[32] = { + DUK_STRIDX_EMPTY_STRING, /* UNUSED, intentionally empty */ + DUK_STRIDX_UC_ARGUMENTS, + DUK_STRIDX_ARRAY, + DUK_STRIDX_UC_BOOLEAN, + DUK_STRIDX_DATE, + DUK_STRIDX_UC_ERROR, + DUK_STRIDX_UC_FUNCTION, + DUK_STRIDX_JSON, + DUK_STRIDX_MATH, + DUK_STRIDX_UC_NUMBER, + DUK_STRIDX_UC_OBJECT, + DUK_STRIDX_REG_EXP, + DUK_STRIDX_UC_STRING, + DUK_STRIDX_GLOBAL, + DUK_STRIDX_OBJ_ENV, + DUK_STRIDX_DEC_ENV, + DUK_STRIDX_UC_BUFFER, + DUK_STRIDX_UC_POINTER, + DUK_STRIDX_UC_THREAD, + DUK_STRIDX_ARRAY_BUFFER, + DUK_STRIDX_DATA_VIEW, + DUK_STRIDX_INT8_ARRAY, + DUK_STRIDX_UINT8_ARRAY, + DUK_STRIDX_UINT8_CLAMPED_ARRAY, + DUK_STRIDX_INT16_ARRAY, + DUK_STRIDX_UINT16_ARRAY, + DUK_STRIDX_INT32_ARRAY, + DUK_STRIDX_UINT32_ARRAY, + DUK_STRIDX_FLOAT32_ARRAY, + DUK_STRIDX_FLOAT64_ARRAY, + DUK_STRIDX_EMPTY_STRING, /* UNUSED, intentionally empty */ + DUK_STRIDX_EMPTY_STRING, /* UNUSED, intentionally empty */ +}; +/* + * Default allocation functions. + * + * Assumes behavior such as malloc allowing zero size, yielding + * a NULL or a unique pointer which is a no-op for free. + */ + +/* include removed: duk_internal.h */ + +#if defined(DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS) +DUK_INTERNAL void *duk_default_alloc_function(void *udata, duk_size_t size) { + void *res; + DUK_UNREF(udata); + res = DUK_ANSI_MALLOC(size); + DUK_DDD(DUK_DDDPRINT("default alloc function: %lu -> %p", + (unsigned long) size, (void *) res)); + return res; +} + +DUK_INTERNAL void *duk_default_realloc_function(void *udata, void *ptr, duk_size_t newsize) { + void *res; + DUK_UNREF(udata); + res = DUK_ANSI_REALLOC(ptr, newsize); + DUK_DDD(DUK_DDDPRINT("default realloc function: %p %lu -> %p", + (void *) ptr, (unsigned long) newsize, (void *) res)); + return res; +} + +DUK_INTERNAL void duk_default_free_function(void *udata, void *ptr) { + DUK_DDD(DUK_DDDPRINT("default free function: %p", (void *) ptr)); + DUK_UNREF(udata); + DUK_ANSI_FREE(ptr); +} +#endif /* DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS */ +/* + * Buffer + */ + +/* include removed: duk_internal.h */ + +DUK_EXTERNAL void *duk_resize_buffer(duk_context *ctx, duk_idx_t index, duk_size_t new_size) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hbuffer_dynamic *h; + + DUK_ASSERT_CTX_VALID(ctx); + + h = (duk_hbuffer_dynamic *) duk_require_hbuffer(ctx, index); + DUK_ASSERT(h != NULL); + + if (!(DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h))) { + DUK_ERROR_TYPE(thr, DUK_STR_WRONG_BUFFER_TYPE); + } + + /* maximum size check is handled by callee */ + duk_hbuffer_resize(thr, h, new_size); + + return DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h); +} + +DUK_EXTERNAL void *duk_steal_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hbuffer_dynamic *h; + void *ptr; + duk_size_t sz; + + DUK_ASSERT(ctx != NULL); + + h = (duk_hbuffer_dynamic *) duk_require_hbuffer(ctx, index); + DUK_ASSERT(h != NULL); + + if (!(DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h))) { + DUK_ERROR_TYPE(thr, DUK_STR_WRONG_BUFFER_TYPE); + } + + /* Forget the previous allocation, setting size to 0 and alloc to + * NULL. Caller is responsible for freeing the previous allocation. + * Getting the allocation and clearing it is done in the same API + * call to avoid any chance of a realloc. + */ + ptr = DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h); + sz = DUK_HBUFFER_DYNAMIC_GET_SIZE(h); + if (out_size) { + *out_size = sz; + } + DUK_HBUFFER_DYNAMIC_SET_DATA_PTR_NULL(thr->heap, h); + DUK_HBUFFER_DYNAMIC_SET_SIZE(h, 0); + + return ptr; +} + +DUK_EXTERNAL void duk_config_buffer(duk_context *ctx, duk_idx_t index, void *ptr, duk_size_t len) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hbuffer_external *h; + + DUK_ASSERT(ctx != NULL); + + h = (duk_hbuffer_external *) duk_require_hbuffer(ctx, index); + DUK_ASSERT(h != NULL); + + if (!DUK_HBUFFER_HAS_EXTERNAL(h)) { + DUK_ERROR_TYPE(thr, DUK_STR_WRONG_BUFFER_TYPE); + } + DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(h)); + + DUK_HBUFFER_EXTERNAL_SET_DATA_PTR(thr->heap, h, ptr); + DUK_HBUFFER_EXTERNAL_SET_SIZE(h, len); +} +/* + * Bytecode dump/load + * + * The bytecode load primitive is more important performance-wise than the + * dump primitive. + * + * Unlike most Duktape API calls, bytecode dump/load is not guaranteed to be + * memory safe for invalid arguments - caller beware! There's little point + * in trying to achieve memory safety unless bytecode instructions are also + * validated which is not easy to do with indirect register references etc. + */ + +/* include removed: duk_internal.h */ + +#if defined(DUK_USE_BYTECODE_DUMP_SUPPORT) + +#define DUK__SER_MARKER 0xff +#define DUK__SER_VERSION 0x00 +#define DUK__SER_STRING 0x00 +#define DUK__SER_NUMBER 0x01 +#define DUK__BYTECODE_INITIAL_ALLOC 256 + +/* + * Dump/load helpers, xxx_raw() helpers do no buffer checks + */ + +DUK_LOCAL duk_uint8_t *duk__load_string_raw(duk_context *ctx, duk_uint8_t *p) { + duk_uint32_t len; + + len = DUK_RAW_READ_U32_BE(p); + duk_push_lstring(ctx, (const char *) p, len); + p += len; + return p; +} + +DUK_LOCAL duk_uint8_t *duk__load_buffer_raw(duk_context *ctx, duk_uint8_t *p) { + duk_uint32_t len; + duk_uint8_t *buf; + + len = DUK_RAW_READ_U32_BE(p); + buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) len); + DUK_ASSERT(buf != NULL); + DUK_MEMCPY((void *) buf, (const void *) p, (size_t) len); + p += len; + return p; +} + +DUK_LOCAL duk_uint8_t *duk__dump_hstring_raw(duk_uint8_t *p, duk_hstring *h) { + duk_size_t len; + duk_uint32_t tmp32; + + DUK_ASSERT(h != NULL); + + len = DUK_HSTRING_GET_BYTELEN(h); + DUK_ASSERT(len <= 0xffffffffUL); /* string limits */ + tmp32 = (duk_uint32_t) len; + DUK_RAW_WRITE_U32_BE(p, tmp32); + DUK_MEMCPY((void *) p, + (const void *) DUK_HSTRING_GET_DATA(h), + len); + p += len; + return p; +} + +DUK_LOCAL duk_uint8_t *duk__dump_hbuffer_raw(duk_hthread *thr, duk_uint8_t *p, duk_hbuffer *h) { + duk_size_t len; + duk_uint32_t tmp32; + + DUK_ASSERT(thr != NULL); + DUK_ASSERT(h != NULL); + DUK_UNREF(thr); + + len = DUK_HBUFFER_GET_SIZE(h); + DUK_ASSERT(len <= 0xffffffffUL); /* buffer limits */ + tmp32 = (duk_uint32_t) len; + DUK_RAW_WRITE_U32_BE(p, tmp32); + DUK_MEMCPY((void *) p, + (const void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h), + len); + p += len; + return p; +} + +DUK_LOCAL duk_uint8_t *duk__dump_string_prop(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func, duk_small_uint_t stridx) { + duk_hstring *h_str; + duk_tval *tv; + + tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_GET_STRING(thr, stridx)); + if (tv != NULL && DUK_TVAL_IS_STRING(tv)) { + h_str = DUK_TVAL_GET_STRING(tv); + DUK_ASSERT(h_str != NULL); + } else { + h_str = DUK_HTHREAD_STRING_EMPTY_STRING(thr); + DUK_ASSERT(h_str != NULL); + } + DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */ + p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4 + DUK_HSTRING_GET_BYTELEN(h_str), p); + p = duk__dump_hstring_raw(p, h_str); + return p; +} + +DUK_LOCAL duk_uint8_t *duk__dump_buffer_prop(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func, duk_small_uint_t stridx) { + duk_tval *tv; + + tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_GET_STRING(thr, stridx)); + if (tv != NULL && DUK_TVAL_IS_BUFFER(tv)) { + duk_hbuffer *h_buf; + h_buf = DUK_TVAL_GET_BUFFER(tv); + DUK_ASSERT(h_buf != NULL); + DUK_ASSERT(DUK_HBUFFER_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */ + p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4 + DUK_HBUFFER_GET_SIZE(h_buf), p); + p = duk__dump_hbuffer_raw(thr, p, h_buf); + } else { + p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4, p); + DUK_RAW_WRITE_U32_BE(p, 0); + } + return p; +} + +DUK_LOCAL duk_uint8_t *duk__dump_uint32_prop(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func, duk_small_uint_t stridx, duk_uint32_t def_value) { + duk_tval *tv; + duk_uint32_t val; + + tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_GET_STRING(thr, stridx)); + if (tv != NULL && DUK_TVAL_IS_NUMBER(tv)) { + val = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv); + } else { + val = def_value; + } + p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4, p); + DUK_RAW_WRITE_U32_BE(p, val); + return p; +} + +DUK_LOCAL duk_uint8_t *duk__dump_varmap(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func) { + duk_tval *tv; + + tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_STRING_INT_VARMAP(thr)); + if (tv != NULL && DUK_TVAL_IS_OBJECT(tv)) { + duk_hobject *h; + duk_uint_fast32_t i; + + h = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h != NULL); + + /* We know _Varmap only has own properties so walk property + * table directly. We also know _Varmap is dense and all + * values are numbers; assert for these. GC and finalizers + * shouldn't affect _Varmap so side effects should be fine. + */ + for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h); i++) { + duk_hstring *key; + duk_tval *tv_val; + duk_uint32_t val; + + key = DUK_HOBJECT_E_GET_KEY(thr->heap, h, i); + DUK_ASSERT(key != NULL); /* _Varmap is dense */ + DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, h, i)); + tv_val = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, h, i); + DUK_ASSERT(tv_val != NULL); + DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_val)); /* known to be number; in fact an integer */ +#if defined(DUK_USE_FASTINT) + DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv_val)); + DUK_ASSERT(DUK_TVAL_GET_FASTINT(tv_val) == (duk_int64_t) DUK_TVAL_GET_FASTINT_U32(tv_val)); /* known to be 32-bit */ + val = DUK_TVAL_GET_FASTINT_U32(tv_val); +#else + val = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv_val); +#endif + + DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */ + p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4 + DUK_HSTRING_GET_BYTELEN(key) + 4, p); + p = duk__dump_hstring_raw(p, key); + DUK_RAW_WRITE_U32_BE(p, val); + } + } + p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4, p); + DUK_RAW_WRITE_U32_BE(p, 0); /* end of _Varmap */ + return p; +} + +DUK_LOCAL duk_uint8_t *duk__dump_formals(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func) { + duk_tval *tv; + + tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_STRING_INT_FORMALS(thr)); + if (tv != NULL && DUK_TVAL_IS_OBJECT(tv)) { + duk_hobject *h; + duk_uint_fast32_t i; + + h = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h != NULL); + + /* We know _Formals is dense and all entries will be in the + * array part. GC and finalizers shouldn't affect _Formals + * so side effects should be fine. + */ + for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(h); i++) { + duk_tval *tv_val; + duk_hstring *varname; + + tv_val = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, h, i); + DUK_ASSERT(tv_val != NULL); + if (DUK_TVAL_IS_STRING(tv_val)) { + /* Array is dense and contains only strings, but ASIZE may + * be larger than used part and there are UNUSED entries. + */ + varname = DUK_TVAL_GET_STRING(tv_val); + DUK_ASSERT(varname != NULL); + + DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */ + p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4 + DUK_HSTRING_GET_BYTELEN(varname), p); + p = duk__dump_hstring_raw(p, varname); + } + } + } + p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4, p); + DUK_RAW_WRITE_U32_BE(p, 0); /* end of _Formals */ + return p; +} + +static duk_uint8_t *duk__dump_func(duk_context *ctx, duk_hcompiledfunction *func, duk_bufwriter_ctx *bw_ctx, duk_uint8_t *p) { + duk_hthread *thr; + duk_tval *tv, *tv_end; + duk_instr_t *ins, *ins_end; + duk_hobject **fn, **fn_end; + duk_hstring *h_str; + duk_uint32_t count_instr; + duk_uint32_t tmp32; + duk_uint16_t tmp16; + duk_double_t d; + + thr = (duk_hthread *) ctx; + DUK_UNREF(ctx); + DUK_UNREF(thr); + + DUK_DD(DUK_DDPRINT("dumping function %p to %p: " + "consts=[%p,%p[ (%ld bytes, %ld items), " + "funcs=[%p,%p[ (%ld bytes, %ld items), " + "code=[%p,%p[ (%ld bytes, %ld items)", + (void *) func, + (void *) p, + (void *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, func), + (void *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(thr->heap, func), + (long) DUK_HCOMPILEDFUNCTION_GET_CONSTS_SIZE(thr->heap, func), + (long) DUK_HCOMPILEDFUNCTION_GET_CONSTS_COUNT(thr->heap, func), + (void *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, func), + (void *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(thr->heap, func), + (long) DUK_HCOMPILEDFUNCTION_GET_FUNCS_SIZE(thr->heap, func), + (long) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr->heap, func), + (void *) DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, func), + (void *) DUK_HCOMPILEDFUNCTION_GET_CODE_END(thr->heap, func), + (long) DUK_HCOMPILEDFUNCTION_GET_CODE_SIZE(thr->heap, func), + (long) DUK_HCOMPILEDFUNCTION_GET_CODE_COUNT(thr->heap, func))); + + DUK_ASSERT(DUK_USE_ESBC_MAX_BYTES <= 0x7fffffffUL); /* ensures no overflow */ + count_instr = (duk_uint32_t) DUK_HCOMPILEDFUNCTION_GET_CODE_COUNT(thr->heap, func); + p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 3 * 4 + 2 * 2 + 3 * 4 + count_instr * 4, p); + + /* Fixed header info. */ + tmp32 = count_instr; + DUK_RAW_WRITE_U32_BE(p, tmp32); + tmp32 = (duk_uint32_t) DUK_HCOMPILEDFUNCTION_GET_CONSTS_COUNT(thr->heap, func); + DUK_RAW_WRITE_U32_BE(p, tmp32); + tmp32 = (duk_uint32_t) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr->heap, func); + DUK_RAW_WRITE_U32_BE(p, tmp32); + tmp16 = func->nregs; + DUK_RAW_WRITE_U16_BE(p, tmp16); + tmp16 = func->nargs; + DUK_RAW_WRITE_U16_BE(p, tmp16); +#if defined(DUK_USE_DEBUGGER_SUPPORT) + tmp32 = func->start_line; + DUK_RAW_WRITE_U32_BE(p, tmp32); + tmp32 = func->end_line; + DUK_RAW_WRITE_U32_BE(p, tmp32); +#else + DUK_RAW_WRITE_U32_BE(p, 0); + DUK_RAW_WRITE_U32_BE(p, 0); +#endif + tmp32 = ((duk_heaphdr *) func)->h_flags & DUK_HEAPHDR_FLAGS_FLAG_MASK; + DUK_RAW_WRITE_U32_BE(p, tmp32); + + /* Bytecode instructions: endian conversion needed unless + * platform is big endian. + */ + ins = DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, func); + ins_end = DUK_HCOMPILEDFUNCTION_GET_CODE_END(thr->heap, func); + DUK_ASSERT((duk_size_t) (ins_end - ins) == (duk_size_t) count_instr); +#if defined(DUK_USE_INTEGER_BE) + DUK_MEMCPY((void *) p, (const void *) ins, (size_t) (ins_end - ins)); + p += (size_t) (ins_end - ins); +#else + while (ins != ins_end) { + tmp32 = (duk_uint32_t) (*ins); + DUK_RAW_WRITE_U32_BE(p, tmp32); + ins++; + } +#endif + + /* Constants: variable size encoding. */ + tv = DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, func); + tv_end = DUK_HCOMPILEDFUNCTION_GET_CONSTS_END(thr->heap, func); + while (tv != tv_end) { + /* constants are strings or numbers now */ + DUK_ASSERT(DUK_TVAL_IS_STRING(tv) || + DUK_TVAL_IS_NUMBER(tv)); + + if (DUK_TVAL_IS_STRING(tv)) { + h_str = DUK_TVAL_GET_STRING(tv); + DUK_ASSERT(h_str != NULL); + DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */ + p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 1 + 4 + DUK_HSTRING_GET_BYTELEN(h_str), p), + *p++ = DUK__SER_STRING; + p = duk__dump_hstring_raw(p, h_str); + } else { + DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); + p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 1 + 8, p); + *p++ = DUK__SER_NUMBER; + d = DUK_TVAL_GET_NUMBER(tv); + DUK_RAW_WRITE_DOUBLE_BE(p, d); + } + tv++; + } + + /* Inner functions recursively. */ + fn = (duk_hobject **) DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, func); + fn_end = (duk_hobject **) DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(thr->heap, func); + while (fn != fn_end) { + /* XXX: This causes recursion up to inner function depth + * which is normally not an issue, e.g. mark-and-sweep uses + * a recursion limiter to avoid C stack issues. Avoiding + * this would mean some sort of a work list or just refusing + * to serialize deep functions. + */ + DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(*fn)); + p = duk__dump_func(ctx, (duk_hcompiledfunction *) *fn, bw_ctx, p); + fn++; + } + + /* Object extra properties. + * + * There are some difference between function templates and functions. + * For example, function templates don't have .length and nargs is + * normally used to instantiate the functions. + */ + + p = duk__dump_uint32_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_LENGTH, (duk_uint32_t) func->nargs); + p = duk__dump_string_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_NAME); + p = duk__dump_string_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_FILE_NAME); + p = duk__dump_buffer_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_INT_PC2LINE); + p = duk__dump_varmap(thr, p, bw_ctx, (duk_hobject *) func); + p = duk__dump_formals(thr, p, bw_ctx, (duk_hobject *) func); + + DUK_DD(DUK_DDPRINT("serialized function %p -> final pointer %p", (void *) func, (void *) p)); + + return p; +} + +/* Load a function from bytecode. The function object returned here must + * match what is created by duk_js_push_closure() with respect to its flags, + * properties, etc. + * + * NOTE: there are intentionally no input buffer length / bound checks. + * Adding them would be easy but wouldn't ensure memory safety as untrusted + * or broken bytecode is unsafe during execution unless the opcodes themselves + * are validated (which is quite complex, especially for indirect opcodes). + */ + +#define DUK__ASSERT_LEFT(n) do { \ + DUK_ASSERT((duk_size_t) (p_end - p) >= (duk_size_t) (n)); \ + } while (0) + +static duk_uint8_t *duk__load_func(duk_context *ctx, duk_uint8_t *p, duk_uint8_t *p_end) { + duk_hthread *thr; + duk_hcompiledfunction *h_fun; + duk_hbuffer *h_data; + duk_size_t data_size; + duk_uint32_t count_instr, count_const, count_funcs; + duk_uint32_t n; + duk_uint32_t tmp32; + duk_small_uint_t const_type; + duk_uint8_t *fun_data; + duk_uint8_t *q; + duk_idx_t idx_base; + duk_tval *tv1; + duk_uarridx_t arr_idx; + + /* XXX: There's some overlap with duk_js_closure() here, but + * seems difficult to share code. Ensure that the final function + * looks the same as created by duk_js_closure(). + */ + + DUK_ASSERT(ctx != NULL); + thr = (duk_hthread *) ctx; + + DUK_DD(DUK_DDPRINT("loading function, p=%p, p_end=%p", (void *) p, (void *) p_end)); + + DUK__ASSERT_LEFT(3 * 4); + count_instr = DUK_RAW_READ_U32_BE(p); + count_const = DUK_RAW_READ_U32_BE(p); + count_funcs = DUK_RAW_READ_U32_BE(p); + + data_size = sizeof(duk_tval) * count_const + + sizeof(duk_hobject *) * count_funcs + + sizeof(duk_instr_t) * count_instr; + + DUK_DD(DUK_DDPRINT("instr=%ld, const=%ld, funcs=%ld, data_size=%ld", + (long) count_instr, (long) count_const, + (long) count_const, (long) data_size)); + + /* Value stack is used to ensure reachability of constants and + * inner functions being loaded. Require enough space to handle + * large functions correctly. + */ + duk_require_stack(ctx, 2 + count_const + count_funcs); + idx_base = duk_get_top(ctx); + + /* Push function object, init flags etc. This must match + * duk_js_push_closure() quite carefully. + */ + duk_push_compiledfunction(ctx); + h_fun = duk_get_hcompiledfunction(ctx, -1); + DUK_ASSERT(h_fun != NULL); + DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) h_fun)); + DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, h_fun) == NULL); + DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_FUNCS(thr->heap, h_fun) == NULL); + DUK_ASSERT(DUK_HCOMPILEDFUNCTION_GET_BYTECODE(thr->heap, h_fun) == NULL); + + h_fun->nregs = DUK_RAW_READ_U16_BE(p); + h_fun->nargs = DUK_RAW_READ_U16_BE(p); +#if defined(DUK_USE_DEBUGGER_SUPPORT) + h_fun->start_line = DUK_RAW_READ_U32_BE(p); + h_fun->end_line = DUK_RAW_READ_U32_BE(p); +#else + p += 8; /* skip line info */ +#endif + + /* duk_hcompiledfunction flags; quite version specific */ + tmp32 = DUK_RAW_READ_U32_BE(p); + DUK_HEAPHDR_SET_FLAGS((duk_heaphdr *) h_fun, tmp32); + + /* standard prototype */ + DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, &h_fun->obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]); + + /* assert just a few critical flags */ + DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) h_fun) == DUK_HTYPE_OBJECT); + DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(&h_fun->obj)); + DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(&h_fun->obj)); + DUK_ASSERT(!DUK_HOBJECT_HAS_NATIVEFUNCTION(&h_fun->obj)); + DUK_ASSERT(!DUK_HOBJECT_HAS_THREAD(&h_fun->obj)); + DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARRAY(&h_fun->obj)); + DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(&h_fun->obj)); + DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(&h_fun->obj)); + + /* Create function 'data' buffer but don't attach it yet. */ + fun_data = (duk_uint8_t *) duk_push_fixed_buffer(ctx, data_size); + DUK_ASSERT(fun_data != NULL); + + /* Load bytecode instructions. */ + DUK_ASSERT(sizeof(duk_instr_t) == 4); + DUK__ASSERT_LEFT(count_instr * sizeof(duk_instr_t)); +#if defined(DUK_USE_INTEGER_BE) + q = fun_data + sizeof(duk_tval) * count_const + sizeof(duk_hobject *) * count_funcs; + DUK_MEMCPY((void *) q, + (const void *) p, + sizeof(duk_instr_t) * count_instr); + p += sizeof(duk_instr_t) * count_instr; +#else + q = fun_data + sizeof(duk_tval) * count_const + sizeof(duk_hobject *) * count_funcs; + for (n = count_instr; n > 0; n--) { + *((duk_instr_t *) (void *) q) = DUK_RAW_READ_U32_BE(p); + q += sizeof(duk_instr_t); + } +#endif + + /* Load constants onto value stack but don't yet copy to buffer. */ + for (n = count_const; n > 0; n--) { + DUK__ASSERT_LEFT(1); + const_type = DUK_RAW_READ_U8(p); + switch (const_type) { + case DUK__SER_STRING: { + p = duk__load_string_raw(ctx, p); + break; + } + case DUK__SER_NUMBER: { + /* Important to do a fastint check so that constants are + * properly read back as fastints. + */ + duk_tval tv_tmp; + duk_double_t val; + DUK__ASSERT_LEFT(8); + val = DUK_RAW_READ_DOUBLE_BE(p); + DUK_TVAL_SET_NUMBER_CHKFAST(&tv_tmp, val); + duk_push_tval(ctx, &tv_tmp); + break; + } + default: { + goto format_error; + } + } + } + + /* Load inner functions to value stack, but don't yet copy to buffer. */ + for (n = count_funcs; n > 0; n--) { + p = duk__load_func(ctx, p, p_end); + if (p == NULL) { + goto format_error; + } + } + + /* With constants and inner functions on value stack, we can now + * atomically finish the function 'data' buffer, bump refcounts, + * etc. + * + * Here we take advantage of the value stack being just a duk_tval + * array: we can just memcpy() the constants as long as we incref + * them afterwards. + */ + + h_data = (duk_hbuffer *) duk_get_hbuffer(ctx, idx_base + 1); + DUK_ASSERT(h_data != NULL); + DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC(h_data)); + DUK_HCOMPILEDFUNCTION_SET_DATA(thr->heap, h_fun, h_data); + DUK_HBUFFER_INCREF(thr, h_data); + + tv1 = duk_get_tval(ctx, idx_base + 2); /* may be NULL if no constants or inner funcs */ + DUK_ASSERT((count_const == 0 && count_funcs == 0) || tv1 != NULL); + + q = fun_data; + if (count_const > 0) { + /* Explicit zero size check to avoid NULL 'tv1'. */ + DUK_MEMCPY((void *) q, (const void *) tv1, sizeof(duk_tval) * count_const); + for (n = count_const; n > 0; n--) { + DUK_TVAL_INCREF_FAST(thr, (duk_tval *) (void *) q); /* no side effects */ + q += sizeof(duk_tval); + } + tv1 += count_const; + } + + DUK_HCOMPILEDFUNCTION_SET_FUNCS(thr->heap, h_fun, (duk_hobject **) (void *) q); + for (n = count_funcs; n > 0; n--) { + duk_hobject *h_obj; + + DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1)); + h_obj = DUK_TVAL_GET_OBJECT(tv1); + DUK_ASSERT(h_obj != NULL); + tv1++; + DUK_HOBJECT_INCREF(thr, h_obj); + + *((duk_hobject **) (void *) q) = h_obj; + q += sizeof(duk_hobject *); + } + + DUK_HCOMPILEDFUNCTION_SET_BYTECODE(thr->heap, h_fun, (duk_instr_t *) (void *) q); + + /* The function object is now reachable and refcounts are fine, + * so we can pop off all the temporaries. + */ + DUK_DDD(DUK_DDDPRINT("function is reachable, reset top; func: %!iT", duk_get_tval(ctx, idx_base))); + duk_set_top(ctx, idx_base + 1); + + /* Setup function properties. */ + tmp32 = DUK_RAW_READ_U32_BE(p); + duk_push_u32(ctx, tmp32); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE); + + p = duk__load_string_raw(ctx, p); + if (DUK_HOBJECT_HAS_NAMEBINDING((duk_hobject *) h_fun)) { + /* Original function instance/template had NAMEBINDING. + * Must create a lexical environment on loading to allow + * recursive functions like 'function foo() { foo(); }'. + */ + duk_hobject *proto; + + proto = thr->builtins[DUK_BIDX_GLOBAL_ENV]; + (void) duk_push_object_helper_proto(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV), + proto); + duk_dup(ctx, -2); /* -> [ func funcname env funcname ] */ + duk_dup(ctx, idx_base); /* -> [ func funcname env funcname func ] */ + duk_xdef_prop(ctx, -3, DUK_PROPDESC_FLAGS_NONE); /* -> [ func funcname env ] */ + duk_xdef_prop_stridx(ctx, idx_base, DUK_STRIDX_INT_LEXENV, DUK_PROPDESC_FLAGS_WC); + /* since closure has NEWENV, never define DUK_STRIDX_INT_VARENV, as it + * will be ignored anyway + */ + } + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE); + + p = duk__load_string_raw(ctx, p); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC); + + duk_push_object(ctx); + duk_dup(ctx, -2); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_CONSTRUCTOR, DUK_PROPDESC_FLAGS_WC); /* func.prototype.constructor = func */ + duk_compact(ctx, -1); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_PROTOTYPE, DUK_PROPDESC_FLAGS_W); + + p = duk__load_buffer_raw(ctx, p); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_PC2LINE, DUK_PROPDESC_FLAGS_WC); + + duk_push_object(ctx); /* _Varmap */ + for (;;) { + /* XXX: awkward */ + p = duk__load_string_raw(ctx, p); + if (duk_get_length(ctx, -1) == 0) { + duk_pop(ctx); + break; + } + tmp32 = DUK_RAW_READ_U32_BE(p); + duk_push_u32(ctx, tmp32); + duk_put_prop(ctx, -3); + } + duk_compact(ctx, -1); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VARMAP, DUK_PROPDESC_FLAGS_NONE); + + duk_push_array(ctx); /* _Formals */ + for (arr_idx = 0; ; arr_idx++) { + /* XXX: awkward */ + p = duk__load_string_raw(ctx, p); + if (duk_get_length(ctx, -1) == 0) { + duk_pop(ctx); + break; + } + duk_put_prop_index(ctx, -2, arr_idx); + } + duk_compact(ctx, -1); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_FORMALS, DUK_PROPDESC_FLAGS_NONE); + + /* Return with final function pushed on stack top. */ + DUK_DD(DUK_DDPRINT("final loaded function: %!iT", duk_get_tval(ctx, -1))); + DUK_ASSERT_TOP(ctx, idx_base + 1); + return p; + + format_error: + return NULL; +} + +DUK_EXTERNAL void duk_dump_function(duk_context *ctx) { + duk_hthread *thr; + duk_hcompiledfunction *func; + duk_bufwriter_ctx bw_ctx_alloc; + duk_bufwriter_ctx *bw_ctx = &bw_ctx_alloc; + duk_uint8_t *p; + + DUK_ASSERT(ctx != NULL); + thr = (duk_hthread *) ctx; + + /* Bound functions don't have all properties so we'd either need to + * lookup the non-bound target function or reject bound functions. + * For now, bound functions are rejected. + */ + func = duk_require_hcompiledfunction(ctx, -1); + DUK_ASSERT(func != NULL); + DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(&func->obj)); + + /* Estimating the result size beforehand would be costly, so + * start with a reasonable size and extend as needed. + */ + DUK_BW_INIT_PUSHBUF(thr, bw_ctx, DUK__BYTECODE_INITIAL_ALLOC); + p = DUK_BW_GET_PTR(thr, bw_ctx); + *p++ = DUK__SER_MARKER; + *p++ = DUK__SER_VERSION; + p = duk__dump_func(ctx, func, bw_ctx, p); + DUK_BW_SET_PTR(thr, bw_ctx, p); + DUK_BW_COMPACT(thr, bw_ctx); + + DUK_DD(DUK_DDPRINT("serialized result: %!T", duk_get_tval(ctx, -1))); + + duk_remove(ctx, -2); /* [ ... func buf ] -> [ ... buf ] */ +} + +DUK_EXTERNAL void duk_load_function(duk_context *ctx) { + duk_hthread *thr; + duk_uint8_t *p_buf, *p, *p_end; + duk_size_t sz; + + DUK_ASSERT(ctx != NULL); + thr = (duk_hthread *) ctx; + DUK_UNREF(ctx); + + p_buf = (duk_uint8_t *) duk_require_buffer(ctx, -1, &sz); + DUK_ASSERT(p_buf != NULL); + + /* The caller is responsible for being sure that bytecode being loaded + * is valid and trusted. Invalid bytecode can cause memory unsafe + * behavior directly during loading or later during bytecode execution + * (instruction validation would be quite complex to implement). + * + * This signature check is the only sanity check for detecting + * accidental invalid inputs. The initial 0xFF byte ensures no + * ordinary string will be accepted by accident. + */ + p = p_buf; + p_end = p_buf + sz; + if (sz < 2 || p[0] != DUK__SER_MARKER || p[1] != DUK__SER_VERSION) { + goto format_error; + } + p += 2; + + p = duk__load_func(ctx, p, p_end); + if (p == NULL) { + goto format_error; + } + + duk_remove(ctx, -2); /* [ ... buf func ] -> [ ... func ] */ + return; + + format_error: + DUK_ERROR_TYPE(thr, DUK_STR_DECODE_FAILED); +} + +#undef DUK__SER_MARKER +#undef DUK__SER_VERSION +#undef DUK__SER_STRING +#undef DUK__SER_NUMBER +#undef DUK__BYTECODE_INITIAL_ALLOC + +#else /* DUK_USE_BYTECODE_DUMP_SUPPORT */ + +DUK_EXTERNAL void duk_dump_function(duk_context *ctx) { + DUK_ERROR_UNSUPPORTED_DEFMSG((duk_hthread *) ctx); +} + +DUK_EXTERNAL void duk_load_function(duk_context *ctx) { + DUK_ERROR_UNSUPPORTED_DEFMSG((duk_hthread *) ctx); +} + +#endif /* DUK_USE_BYTECODE_DUMP_SUPPORT */ +/* + * Calls. + * + * Protected variants should avoid ever throwing an error. + */ + +/* include removed: duk_internal.h */ + +/* Prepare value stack for a method call through an object property. + * May currently throw an error e.g. when getting the property. + */ +DUK_LOCAL void duk__call_prop_prep_stack(duk_context *ctx, duk_idx_t normalized_obj_index, duk_idx_t nargs) { + DUK_ASSERT_CTX_VALID(ctx); + + DUK_DDD(DUK_DDDPRINT("duk__call_prop_prep_stack, normalized_obj_index=%ld, nargs=%ld, stacktop=%ld", + (long) normalized_obj_index, (long) nargs, (long) duk_get_top(ctx))); + + /* [... key arg1 ... argN] */ + + /* duplicate key */ + duk_dup(ctx, -nargs - 1); /* Note: -nargs alone would fail for nargs == 0, this is OK */ + duk_get_prop(ctx, normalized_obj_index); + + DUK_DDD(DUK_DDDPRINT("func: %!T", (duk_tval *) duk_get_tval(ctx, -1))); + + /* [... key arg1 ... argN func] */ + + duk_replace(ctx, -nargs - 2); + + /* [... func arg1 ... argN] */ + + duk_dup(ctx, normalized_obj_index); + duk_insert(ctx, -nargs - 1); + + /* [... func this arg1 ... argN] */ +} + +DUK_EXTERNAL void duk_call(duk_context *ctx, duk_idx_t nargs) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_small_uint_t call_flags; + duk_idx_t idx_func; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + + idx_func = duk_get_top(ctx) - nargs - 1; + if (idx_func < 0 || nargs < 0) { + /* note that we can't reliably pop anything here */ + DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); + } + + /* XXX: awkward; we assume there is space for this, overwrite + * directly instead? + */ + duk_push_undefined(ctx); + duk_insert(ctx, idx_func + 1); + + call_flags = 0; /* not protected, respect reclimit, not constructor */ + + duk_handle_call_unprotected(thr, /* thread */ + nargs, /* num_stack_args */ + call_flags); /* call_flags */ +} + +DUK_EXTERNAL void duk_call_method(duk_context *ctx, duk_idx_t nargs) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_small_uint_t call_flags; + duk_idx_t idx_func; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + + idx_func = duk_get_top(ctx) - nargs - 2; /* must work for nargs <= 0 */ + if (idx_func < 0 || nargs < 0) { + /* note that we can't reliably pop anything here */ + DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); + } + + call_flags = 0; /* not protected, respect reclimit, not constructor */ + + duk_handle_call_unprotected(thr, /* thread */ + nargs, /* num_stack_args */ + call_flags); /* call_flags */ +} + +DUK_EXTERNAL void duk_call_prop(duk_context *ctx, duk_idx_t obj_index, duk_idx_t nargs) { + /* + * XXX: if duk_handle_call() took values through indices, this could be + * made much more sensible. However, duk_handle_call() needs to fudge + * the 'this' and 'func' values to handle bound function chains, which + * is now done "in-place", so this is not a trivial change. + */ + + DUK_ASSERT_CTX_VALID(ctx); + + obj_index = duk_require_normalize_index(ctx, obj_index); /* make absolute */ + + duk__call_prop_prep_stack(ctx, obj_index, nargs); + + duk_call_method(ctx, nargs); +} + +DUK_EXTERNAL duk_int_t duk_pcall(duk_context *ctx, duk_idx_t nargs) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_small_uint_t call_flags; + duk_idx_t idx_func; + duk_int_t rc; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + + idx_func = duk_get_top(ctx) - nargs - 1; /* must work for nargs <= 0 */ + if (idx_func < 0 || nargs < 0) { + /* We can't reliably pop anything here because the stack input + * shape is incorrect. So we throw an error; if the caller has + * no catch point for this, a fatal error will occur. Another + * alternative would be to just return an error. But then the + * stack would be in an unknown state which might cause some + * very hard to diagnose problems later on. Also note that even + * if we did not throw an error here, the underlying call handler + * might STILL throw an out-of-memory error or some other internal + * fatal error. + */ + DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); + return DUK_EXEC_ERROR; /* unreachable */ + } + + /* awkward; we assume there is space for this */ + duk_push_undefined(ctx); + duk_insert(ctx, idx_func + 1); + + call_flags = 0; /* respect reclimit, not constructor */ + + rc = duk_handle_call_protected(thr, /* thread */ + nargs, /* num_stack_args */ + call_flags); /* call_flags */ + + return rc; +} + +DUK_EXTERNAL duk_int_t duk_pcall_method(duk_context *ctx, duk_idx_t nargs) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_small_uint_t call_flags; + duk_idx_t idx_func; + duk_int_t rc; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + + idx_func = duk_get_top(ctx) - nargs - 2; /* must work for nargs <= 0 */ + if (idx_func < 0 || nargs < 0) { + /* See comments in duk_pcall(). */ + DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); + return DUK_EXEC_ERROR; /* unreachable */ + } + + call_flags = 0; /* respect reclimit, not constructor */ + + rc = duk_handle_call_protected(thr, /* thread */ + nargs, /* num_stack_args */ + call_flags); /* call_flags */ + + return rc; +} + +DUK_LOCAL duk_ret_t duk__pcall_prop_raw(duk_context *ctx) { + duk_idx_t obj_index; + duk_idx_t nargs; + + /* Get the original arguments. Note that obj_index may be a relative + * index so the stack must have the same top when we use it. + */ + + DUK_ASSERT_CTX_VALID(ctx); + + obj_index = (duk_idx_t) duk_get_int(ctx, -2); + nargs = (duk_idx_t) duk_get_int(ctx, -1); + duk_pop_2(ctx); + + obj_index = duk_require_normalize_index(ctx, obj_index); /* make absolute */ + duk__call_prop_prep_stack(ctx, obj_index, nargs); + duk_call_method(ctx, nargs); + return 1; +} + +DUK_EXTERNAL duk_int_t duk_pcall_prop(duk_context *ctx, duk_idx_t obj_index, duk_idx_t nargs) { + /* + * Must be careful to catch errors related to value stack manipulation + * and property lookup, not just the call itself. + */ + + DUK_ASSERT_CTX_VALID(ctx); + + duk_push_idx(ctx, obj_index); + duk_push_idx(ctx, nargs); + + /* Inputs: explicit arguments (nargs), +1 for key, +2 for obj_index/nargs passing. + * If the value stack does not contain enough args, an error is thrown; this matches + * behavior of the other protected call API functions. + */ + return duk_safe_call(ctx, duk__pcall_prop_raw, nargs + 1 + 2 /*nargs*/, 1 /*nrets*/); +} + +DUK_EXTERNAL duk_int_t duk_safe_call(duk_context *ctx, duk_safe_call_function func, duk_idx_t nargs, duk_idx_t nrets) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_int_t rc; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + + if (duk_get_top(ctx) < nargs || nrets < 0) { + /* See comments in duk_pcall(). */ + DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); + return DUK_EXEC_ERROR; /* unreachable */ + } + + rc = duk_handle_safe_call(thr, /* thread */ + func, /* func */ + nargs, /* num_stack_args */ + nrets); /* num_stack_res */ + + return rc; +} + +DUK_EXTERNAL void duk_new(duk_context *ctx, duk_idx_t nargs) { + /* + * There are two [[Construct]] operations in the specification: + * + * - E5 Section 13.2.2: for Function objects + * - E5 Section 15.3.4.5.2: for "bound" Function objects + * + * The chain of bound functions is resolved in Section 15.3.4.5.2, + * with arguments "piling up" until the [[Construct]] internal + * method is called on the final, actual Function object. Note + * that the "prototype" property is looked up *only* from the + * final object, *before* calling the constructor. + * + * Currently we follow the bound function chain here to get the + * "prototype" property value from the final, non-bound function. + * However, we let duk_handle_call() handle the argument "piling" + * when the constructor is called. The bound function chain is + * thus now processed twice. + * + * When constructing new Array instances, an unnecessary object is + * created and discarded now: the standard [[Construct]] creates an + * object, and calls the Array constructor. The Array constructor + * returns an Array instance, which is used as the result value for + * the "new" operation; the object created before the Array constructor + * call is discarded. + * + * This would be easy to fix, e.g. by knowing that the Array constructor + * will always create a replacement object and skip creating the fallback + * object in that case. + * + * Note: functions called via "new" need to know they are called as a + * constructor. For instance, built-in constructors behave differently + * depending on how they are called. + */ + + /* XXX: merge this with duk_js_call.c, as this function implements + * core semantics (or perhaps merge the two files altogether). + */ + + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *proto; + duk_hobject *cons; + duk_hobject *fallback; + duk_idx_t idx_cons; + duk_small_uint_t call_flags; + + DUK_ASSERT_CTX_VALID(ctx); + + /* [... constructor arg1 ... argN] */ + + idx_cons = duk_require_normalize_index(ctx, -nargs - 1); + + DUK_DDD(DUK_DDDPRINT("top=%ld, nargs=%ld, idx_cons=%ld", + (long) duk_get_top(ctx), (long) nargs, (long) idx_cons)); + + /* XXX: code duplication */ + + /* + * Figure out the final, non-bound constructor, to get "prototype" + * property. + */ + + duk_dup(ctx, idx_cons); + for (;;) { + duk_tval *tv; + tv = DUK_GET_TVAL_NEGIDX(ctx, -1); + DUK_ASSERT(tv != NULL); + + if (DUK_TVAL_IS_OBJECT(tv)) { + cons = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(cons != NULL); + if (!DUK_HOBJECT_IS_CALLABLE(cons) || !DUK_HOBJECT_HAS_CONSTRUCTABLE(cons)) { + /* Checking callability of the immediate target + * is important, same for constructability. + * Checking it for functions down the bound + * function chain is not strictly necessary + * because .bind() should normally reject them. + * But it's good to check anyway because it's + * technically possible to edit the bound function + * chain via internal keys. + */ + goto not_constructable; + } + if (!DUK_HOBJECT_HAS_BOUND(cons)) { + break; + } + } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) { + /* Lightfuncs cannot be bound. */ + break; + } else { + /* Anything else is not constructable. */ + goto not_constructable; + } + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TARGET); /* -> [... cons target] */ + duk_remove(ctx, -2); /* -> [... target] */ + } + DUK_ASSERT(duk_is_callable(ctx, -1)); + DUK_ASSERT(duk_is_lightfunc(ctx, -1) || + (duk_get_hobject(ctx, -1) != NULL && !DUK_HOBJECT_HAS_BOUND(duk_get_hobject(ctx, -1)))); + + /* [... constructor arg1 ... argN final_cons] */ + + /* + * Create "fallback" object to be used as the object instance, + * unless the constructor returns a replacement value. + * Its internal prototype needs to be set based on "prototype" + * property of the constructor. + */ + + duk_push_object(ctx); /* class Object, extensible */ + + /* [... constructor arg1 ... argN final_cons fallback] */ + + duk_get_prop_stridx(ctx, -2, DUK_STRIDX_PROTOTYPE); + proto = duk_get_hobject(ctx, -1); + if (!proto) { + DUK_DDD(DUK_DDDPRINT("constructor has no 'prototype' property, or value not an object " + "-> leave standard Object prototype as fallback prototype")); + } else { + DUK_DDD(DUK_DDDPRINT("constructor has 'prototype' property with object value " + "-> set fallback prototype to that value: %!iO", (duk_heaphdr *) proto)); + fallback = duk_get_hobject(ctx, -2); + DUK_ASSERT(fallback != NULL); + DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, fallback, proto); + } + duk_pop(ctx); + + /* [... constructor arg1 ... argN final_cons fallback] */ + + /* + * Manipulate callstack for the call. + */ + + duk_dup_top(ctx); + duk_insert(ctx, idx_cons + 1); /* use fallback as 'this' value */ + duk_insert(ctx, idx_cons); /* also stash it before constructor, + * in case we need it (as the fallback value) + */ + duk_pop(ctx); /* pop final_cons */ + + + /* [... fallback constructor fallback(this) arg1 ... argN]; + * Note: idx_cons points to first 'fallback', not 'constructor'. + */ + + DUK_DDD(DUK_DDDPRINT("before call, idx_cons+1 (constructor) -> %!T, idx_cons+2 (fallback/this) -> %!T, " + "nargs=%ld, top=%ld", + (duk_tval *) duk_get_tval(ctx, idx_cons + 1), + (duk_tval *) duk_get_tval(ctx, idx_cons + 2), + (long) nargs, + (long) duk_get_top(ctx))); + + /* + * Call the constructor function (called in "constructor mode"). + */ + + call_flags = DUK_CALL_FLAG_CONSTRUCTOR_CALL; /* not protected, respect reclimit, is a constructor call */ + + duk_handle_call_unprotected(thr, /* thread */ + nargs, /* num_stack_args */ + call_flags); /* call_flags */ + + /* [... fallback retval] */ + + DUK_DDD(DUK_DDDPRINT("constructor call finished, fallback=%!iT, retval=%!iT", + (duk_tval *) duk_get_tval(ctx, -2), + (duk_tval *) duk_get_tval(ctx, -1))); + + /* + * Determine whether to use the constructor return value as the created + * object instance or not. + */ + + if (duk_is_object(ctx, -1)) { + duk_remove(ctx, -2); + } else { + duk_pop(ctx); + } + + /* + * Augment created errors upon creation (not when they are thrown or + * rethrown). __FILE__ and __LINE__ are not desirable here; the call + * stack reflects the caller which is correct. + */ + +#ifdef DUK_USE_AUGMENT_ERROR_CREATE + duk_hthread_sync_currpc(thr); + duk_err_augment_error_create(thr, thr, NULL, 0, 1 /*noblame_fileline*/); +#endif + + /* [... retval] */ + + return; + + not_constructable: + DUK_ERROR_TYPE(thr, DUK_STR_NOT_CONSTRUCTABLE); +} + +DUK_LOCAL duk_ret_t duk__pnew_helper(duk_context *ctx) { + duk_uint_t nargs; + + nargs = duk_to_uint(ctx, -1); + duk_pop(ctx); + + duk_new(ctx, nargs); + return 1; +} + +DUK_EXTERNAL duk_int_t duk_pnew(duk_context *ctx, duk_idx_t nargs) { + duk_int_t rc; + + DUK_ASSERT_CTX_VALID(ctx); + + /* For now, just use duk_safe_call() to wrap duk_new(). We can't + * simply use a protected duk_handle_call() because there's post + * processing which might throw. It should be possible to ensure + * the post processing never throws (except in internal errors and + * out of memory etc which are always allowed) and then remove this + * wrapper. + */ + + duk_push_uint(ctx, nargs); + rc = duk_safe_call(ctx, duk__pnew_helper, nargs + 2 /*nargs*/, 1 /*nrets*/); + return rc; +} + +DUK_EXTERNAL duk_bool_t duk_is_constructor_call(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_activation *act; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + DUK_ASSERT_DISABLE(thr->callstack_top >= 0); + + act = duk_hthread_get_current_activation(thr); + DUK_ASSERT(act != NULL); /* because callstack_top > 0 */ + return ((act->flags & DUK_ACT_FLAG_CONSTRUCT) != 0 ? 1 : 0); +} + +DUK_EXTERNAL duk_bool_t duk_is_strict_call(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_activation *act; + + /* For user code this could just return 1 (strict) always + * because all Duktape/C functions are considered strict, + * and strict is also the default when nothing is running. + * However, Duktape may call this function internally when + * the current activation is an Ecmascript function, so + * this cannot be replaced by a 'return 1' without fixing + * the internal call sites. + */ + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + DUK_ASSERT_DISABLE(thr->callstack_top >= 0); + + act = duk_hthread_get_current_activation(thr); + if (act == NULL) { + /* Strict by default. */ + return 1; + } + return ((act->flags & DUK_ACT_FLAG_STRICT) != 0 ? 1 : 0); +} + +/* + * Duktape/C function magic + */ + +DUK_EXTERNAL duk_int_t duk_get_current_magic(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_activation *act; + duk_hobject *func; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + DUK_ASSERT_DISABLE(thr->callstack_top >= 0); + + act = duk_hthread_get_current_activation(thr); + if (act) { + func = DUK_ACT_GET_FUNC(act); + if (!func) { + duk_tval *tv = &act->tv_func; + duk_small_uint_t lf_flags; + lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv); + return (duk_int_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags); + } + DUK_ASSERT(func != NULL); + + if (DUK_HOBJECT_IS_NATIVEFUNCTION(func)) { + duk_hnativefunction *nf = (duk_hnativefunction *) func; + return (duk_int_t) nf->magic; + } + } + return 0; +} + +DUK_EXTERNAL duk_int_t duk_get_magic(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + duk_hobject *h; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_require_tval(ctx, index); + if (DUK_TVAL_IS_OBJECT(tv)) { + h = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h != NULL); + if (!DUK_HOBJECT_HAS_NATIVEFUNCTION(h)) { + goto type_error; + } + return (duk_int_t) ((duk_hnativefunction *) h)->magic; + } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) { + duk_small_uint_t lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv); + return (duk_int_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags); + } + + /* fall through */ + type_error: + DUK_ERROR_TYPE(thr, DUK_STR_UNEXPECTED_TYPE); + return 0; +} + +DUK_EXTERNAL void duk_set_magic(duk_context *ctx, duk_idx_t index, duk_int_t magic) { + duk_hnativefunction *nf; + + DUK_ASSERT_CTX_VALID(ctx); + + nf = duk_require_hnativefunction(ctx, index); + DUK_ASSERT(nf != NULL); + nf->magic = (duk_int16_t) magic; +} +/* + * Encoding and decoding basic formats: hex, base64. + * + * These are in-place operations which may allow an optimized implementation. + * + * Base-64: https://tools.ietf.org/html/rfc4648#section-4 + */ + +/* include removed: duk_internal.h */ + +/* Shared handling for encode/decode argument. Fast path handling for + * buffer and string values because they're the most common. In particular, + * avoid creating a temporary string or buffer when possible. + */ +DUK_LOCAL const duk_uint8_t *duk__prep_codec_arg(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) { + DUK_ASSERT(duk_is_valid_index(ctx, index)); /* checked by caller */ + if (duk_is_buffer(ctx, index)) { + return (const duk_uint8_t *) duk_get_buffer(ctx, index, out_len); + } else { + return (const duk_uint8_t *) duk_to_lstring(ctx, index, out_len); + } +} + +#if defined(DUK_USE_BASE64_FASTPATH) +DUK_LOCAL void duk__base64_encode_helper(const duk_uint8_t *src, duk_size_t srclen, duk_uint8_t *dst) { + duk_uint_t t; + duk_size_t n_full, n_full3, n_final; + const duk_uint8_t *src_end_fast; + + n_full = srclen / 3; /* full 3-byte -> 4-char conversions */ + n_full3 = n_full * 3; + n_final = srclen - n_full3; + DUK_ASSERT_DISABLE(n_final >= 0); + DUK_ASSERT(n_final <= 2); + + src_end_fast = src + n_full3; + while (DUK_UNLIKELY(src != src_end_fast)) { + t = (duk_uint_t) (*src++); + t = (t << 8) + (duk_uint_t) (*src++); + t = (t << 8) + (duk_uint_t) (*src++); + + *dst++ = duk_base64_enctab[t >> 18]; + *dst++ = duk_base64_enctab[(t >> 12) & 0x3f]; + *dst++ = duk_base64_enctab[(t >> 6) & 0x3f]; + *dst++ = duk_base64_enctab[t & 0x3f]; + +#if 0 /* Tested: not faster on x64 */ + /* aaaaaabb bbbbcccc ccdddddd */ + dst[0] = duk_base64_enctab[(src[0] >> 2) & 0x3f]; + dst[1] = duk_base64_enctab[((src[0] << 4) & 0x30) | ((src[1] >> 4) & 0x0f)]; + dst[2] = duk_base64_enctab[((src[1] << 2) & 0x3f) | ((src[2] >> 6) & 0x03)]; + dst[3] = duk_base64_enctab[src[2] & 0x3f]; + src += 3; dst += 4; +#endif + } + + switch (n_final) { + /* case 0: nop */ + case 1: { + /* XX== */ + t = (duk_uint_t) (*src++); + *dst++ = duk_base64_enctab[t >> 2]; /* XXXXXX-- */ + *dst++ = duk_base64_enctab[(t << 4) & 0x3f]; /* ------XX */ + *dst++ = DUK_ASC_EQUALS; + *dst++ = DUK_ASC_EQUALS; + break; + } + case 2: { + /* XXX= */ + t = (duk_uint_t) (*src++); + t = (t << 8) + (duk_uint_t) (*src++); + *dst++ = duk_base64_enctab[t >> 10]; /* XXXXXX-- -------- */ + *dst++ = duk_base64_enctab[(t >> 4) & 0x3f]; /* ------XX XXXX---- */ + *dst++ = duk_base64_enctab[(t << 2) & 0x3f]; /* -------- ----XXXX */ + *dst++ = DUK_ASC_EQUALS; + break; + } + } +} +#else /* DUK_USE_BASE64_FASTPATH */ +DUK_LOCAL void duk__base64_encode_helper(const duk_uint8_t *src, duk_size_t srclen, duk_uint8_t *dst) { + duk_small_uint_t i, snip; + duk_uint_t t; + duk_uint_fast8_t x, y; + const duk_uint8_t *src_end; + + src_end = src + srclen; + + while (src < src_end) { + /* read 3 bytes into 't', padded by zero */ + snip = 4; + t = 0; + for (i = 0; i < 3; i++) { + t = t << 8; + if (src >= src_end) { + snip--; + } else { + t += (duk_uint_t) (*src++); + } + } + + /* + * Missing bytes snip base64 example + * 0 4 XXXX + * 1 3 XXX= + * 2 2 XX== + */ + + DUK_ASSERT(snip >= 2 && snip <= 4); + + for (i = 0; i < 4; i++) { + x = (duk_uint_fast8_t) ((t >> 18) & 0x3f); + t = t << 6; + + /* A straightforward 64-byte lookup would be faster + * and cleaner, but this is shorter. + */ + if (i >= snip) { + y = '='; + } else if (x <= 25) { + y = x + 'A'; + } else if (x <= 51) { + y = x - 26 + 'a'; + } else if (x <= 61) { + y = x - 52 + '0'; + } else if (x == 62) { + y = '+'; + } else { + y = '/'; + } + + *dst++ = (duk_uint8_t) y; + } + } +} +#endif /* DUK_USE_BASE64_FASTPATH */ + +#if defined(DUK_USE_BASE64_FASTPATH) +DUK_LOCAL duk_bool_t duk__base64_decode_helper(const duk_uint8_t *src, duk_size_t srclen, duk_uint8_t *dst, duk_uint8_t **out_dst_final) { + duk_int_t x; + duk_int_t t; + duk_small_uint_t n_equal; + duk_small_uint_t n_chars; + const duk_uint8_t *src_end; + const duk_uint8_t *src_end_safe; + + src_end = src + srclen; + src_end_safe = src_end - 4; /* if 'src < src_end_safe', safe to read 4 bytes */ + + /* Innermost fast path processes 4 valid base-64 characters at a time + * but bails out on whitespace, padding chars ('=') and invalid chars. + * Once the slow path segment has been processed, we return to the + * inner fast path again. This handles e.g. base64 with newlines + * reasonably well because the majority of a line is in the fast path. + */ + for (;;) { + /* Fast path, handle units with just actual encoding characters. */ + + while (src <= src_end_safe) { + /* The lookup byte is intentionally sign extended to (at least) + * 32 bits and then ORed. This ensures that is at least 1 byte + * is negative, the highest bit of 't' will be set at the end + * and we don't need to check every byte. + */ + DUK_DDD(DUK_DDDPRINT("fast loop: src=%p, src_end_safe=%p, src_end=%p", + (const void *) src, (const void *) src_end_safe, (const void *) src_end)); + + t = (duk_int_t) duk_base64_dectab[*src++]; + t = (t << 6) | (duk_int_t) duk_base64_dectab[*src++]; + t = (t << 6) | (duk_int_t) duk_base64_dectab[*src++]; + t = (t << 6) | (duk_int_t) duk_base64_dectab[*src++]; + + if (DUK_UNLIKELY(t < 0)) { + DUK_DDD(DUK_DDDPRINT("fast loop unit was not clean, process one slow path unit")); + src -= 4; + break; + } + + DUK_ASSERT(t <= 0xffffffL); + DUK_ASSERT((t >> 24) == 0); + *dst++ = (duk_uint8_t) (t >> 16); + *dst++ = (duk_uint8_t) ((t >> 8) & 0xff); + *dst++ = (duk_uint8_t) (t & 0xff); + } + + /* Handle one slow path unit (or finish if we're done). */ + + n_equal = 0; + n_chars = 0; + t = 0; + for (;;) { + DUK_DDD(DUK_DDDPRINT("slow loop: src=%p, src_end=%p, n_chars=%ld, n_equal=%ld, t=%ld", + (const void *) src, (const void *) src_end, (long) n_chars, (long) n_equal, (long) t)); + + if (DUK_UNLIKELY(src >= src_end)) { + goto done; /* two level break */ + } + + x = duk_base64_dectab[*src++]; + if (DUK_UNLIKELY(x < 0)) { + if (x == -2) { + continue; /* allowed ascii whitespace */ + } else if (x == -3) { + n_equal++; + t <<= 6; + } else { + DUK_ASSERT(x == -1); + goto error; + } + } else { + DUK_ASSERT(x >= 0 && x <= 63); + if (n_equal > 0) { + /* Don't allow actual chars after equal sign. */ + goto error; + } + t = (t << 6) + x; + } + + if (DUK_UNLIKELY(n_chars == 3)) { + /* Emit 3 bytes and backtrack if there was padding. There's + * always space for the whole 3 bytes so no check needed. + */ + DUK_ASSERT(t <= 0xffffffL); + DUK_ASSERT((t >> 24) == 0); + *dst++ = (duk_uint8_t) (t >> 16); + *dst++ = (duk_uint8_t) ((t >> 8) & 0xff); + *dst++ = (duk_uint8_t) (t & 0xff); + + if (DUK_UNLIKELY(n_equal > 0)) { + DUK_ASSERT(n_equal <= 4); + + /* There may be whitespace between the equal signs. */ + if (n_equal == 1) { + /* XXX= */ + dst -= 1; + } else if (n_equal == 2) { + /* XX== */ + dst -= 2; + } else { + goto error; /* invalid padding */ + } + + /* Continue parsing after padding, allows concatenated, + * padded base64. + */ + } + break; /* back to fast loop */ + } else { + n_chars++; + } + } + } + done: + DUK_DDD(DUK_DDDPRINT("done; src=%p, src_end=%p, n_chars=%ld", + (const void *) src, (const void *) src_end, (long) n_chars)); + + DUK_ASSERT(src == src_end); + + if (n_chars != 0) { + /* Here we'd have the option of decoding unpadded base64 + * (e.g. "xxxxyy" instead of "xxxxyy==". Currently not + * accepted. + */ + goto error; + } + + *out_dst_final = dst; + return 1; + + error: + return 0; +} +#else /* DUK_USE_BASE64_FASTPATH */ +DUK_LOCAL duk_bool_t duk__base64_decode_helper(const duk_uint8_t *src, duk_size_t srclen, duk_uint8_t *dst, duk_uint8_t **out_dst_final) { + duk_uint_t t; + duk_uint_fast8_t x, y; + duk_small_uint_t group_idx; + duk_small_uint_t n_equal; + const duk_uint8_t *src_end; + + src_end = src + srclen; + t = 0; + group_idx = 0; + n_equal = 0; + + while (src < src_end) { + x = *src++; + + if (x >= 'A' && x <= 'Z') { + y = x - 'A' + 0; + } else if (x >= 'a' && x <= 'z') { + y = x - 'a' + 26; + } else if (x >= '0' && x <= '9') { + y = x - '0' + 52; + } else if (x == '+') { + y = 62; + } else if (x == '/') { + y = 63; + } else if (x == '=') { + /* We don't check the zero padding bytes here right now + * (that they're actually zero). This seems to be common + * behavior for base-64 decoders. + */ + + n_equal++; + t <<= 6; /* shift in zeroes */ + goto skip_add; + } else if (x == 0x09 || x == 0x0a || x == 0x0d || x == 0x20) { + /* allow basic ASCII whitespace */ + continue; + } else { + goto error; + } + + if (n_equal > 0) { + /* Don't allow mixed padding and actual chars. */ + goto error; + } + t = (t << 6) + y; + skip_add: + + if (group_idx == 3) { + /* output 3 bytes from 't' */ + *dst++ = (duk_uint8_t) ((t >> 16) & 0xff); + *dst++ = (duk_uint8_t) ((t >> 8) & 0xff); + *dst++ = (duk_uint8_t) (t & 0xff); + + if (DUK_UNLIKELY(n_equal > 0)) { + /* Backtrack. */ + DUK_ASSERT(n_equal <= 4); + if (n_equal == 1) { + dst -= 1; + } else if (n_equal == 2) { + dst -= 2; + } else { + goto error; /* invalid padding */ + } + + /* Here we can choose either to end parsing and ignore + * whatever follows, or to continue parsing in case + * multiple (possibly padded) base64 strings have been + * concatenated. Currently, keep on parsing. + */ + n_equal = 0; + } + + t = 0; + group_idx = 0; + } else { + group_idx++; + } + } + + if (group_idx != 0) { + /* Here we'd have the option of decoding unpadded base64 + * (e.g. "xxxxyy" instead of "xxxxyy==". Currently not + * accepted. + */ + goto error; + } + + *out_dst_final = dst; + return 1; + + error: + return 0; +} +#endif /* DUK_USE_BASE64_FASTPATH */ + +DUK_EXTERNAL const char *duk_base64_encode(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + const duk_uint8_t *src; + duk_size_t srclen; + duk_size_t dstlen; + duk_uint8_t *dst; + const char *ret; + + DUK_ASSERT_CTX_VALID(ctx); + + /* XXX: optimize for string inputs: no need to coerce to a buffer + * which makes a copy of the input. + */ + + index = duk_require_normalize_index(ctx, index); + src = duk__prep_codec_arg(ctx, index, &srclen); + /* Note: for srclen=0, src may be NULL */ + + /* Computation must not wrap; this limit works for 32-bit size_t: + * >>> srclen = 3221225469 + * >>> '%x' % ((srclen + 2) / 3 * 4) + * 'fffffffc' + */ + if (srclen > 3221225469UL) { + goto type_error; + } + dstlen = (srclen + 2) / 3 * 4; + dst = (duk_uint8_t *) duk_push_fixed_buffer(ctx, dstlen); + + duk__base64_encode_helper((const duk_uint8_t *) src, srclen, dst); + + ret = duk_to_string(ctx, -1); + duk_replace(ctx, index); + return ret; + + type_error: + DUK_ERROR_TYPE(thr, DUK_STR_ENCODE_FAILED); + return NULL; /* never here */ +} + +DUK_EXTERNAL void duk_base64_decode(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + const duk_uint8_t *src; + duk_size_t srclen; + duk_size_t dstlen; + duk_uint8_t *dst; + duk_uint8_t *dst_final; + duk_bool_t retval; + + DUK_ASSERT_CTX_VALID(ctx); + + /* XXX: optimize for buffer inputs: no need to coerce to a string + * which causes an unnecessary interning. + */ + + index = duk_require_normalize_index(ctx, index); + src = duk__prep_codec_arg(ctx, index, &srclen); + + /* Computation must not wrap, only srclen + 3 is at risk of + * wrapping because after that the number gets smaller. + * This limit works for 32-bit size_t: + * 0x100000000 - 3 - 1 = 4294967292 + */ + if (srclen > 4294967292UL) { + goto type_error; + } + dstlen = (srclen + 3) / 4 * 3; /* upper limit, assuming no whitespace etc */ + dst = (duk_uint8_t *) duk_push_dynamic_buffer(ctx, dstlen); + /* Note: for dstlen=0, dst may be NULL */ + + retval = duk__base64_decode_helper((const duk_uint8_t *) src, srclen, dst, &dst_final); + if (!retval) { + goto type_error; + } + + /* XXX: convert to fixed buffer? */ + (void) duk_resize_buffer(ctx, -1, (duk_size_t) (dst_final - dst)); + duk_replace(ctx, index); + return; + + type_error: + DUK_ERROR_TYPE(thr, DUK_STR_DECODE_FAILED); +} + +DUK_EXTERNAL const char *duk_hex_encode(duk_context *ctx, duk_idx_t index) { + const duk_uint8_t *inp; + duk_size_t len; + duk_size_t i; + duk_uint8_t *buf; + const char *ret; +#if defined(DUK_USE_HEX_FASTPATH) + duk_size_t len_safe; + duk_uint16_t *p16; +#endif + + DUK_ASSERT_CTX_VALID(ctx); + + index = duk_require_normalize_index(ctx, index); + inp = duk__prep_codec_arg(ctx, index, &len); + DUK_ASSERT(inp != NULL || len == 0); + + /* Fixed buffer, no zeroing because we'll fill all the data. */ + buf = (duk_uint8_t *) duk_push_buffer_raw(ctx, len * 2, DUK_BUF_FLAG_NOZERO /*flags*/); + DUK_ASSERT(buf != NULL); + +#if defined(DUK_USE_HEX_FASTPATH) + DUK_ASSERT((((duk_size_t) buf) & 0x01U) == 0); /* pointer is aligned, guaranteed for fixed buffer */ + p16 = (duk_uint16_t *) (void *) buf; + len_safe = len & ~0x03U; + for (i = 0; i < len_safe; i += 4) { + p16[0] = duk_hex_enctab[inp[i]]; + p16[1] = duk_hex_enctab[inp[i + 1]]; + p16[2] = duk_hex_enctab[inp[i + 2]]; + p16[3] = duk_hex_enctab[inp[i + 3]]; + p16 += 4; + } + for (; i < len; i++) { + *p16++ = duk_hex_enctab[inp[i]]; + } +#else /* DUK_USE_HEX_FASTPATH */ + for (i = 0; i < len; i++) { + duk_small_uint_t t; + t = (duk_small_uint_t) inp[i]; + buf[i*2 + 0] = duk_lc_digits[t >> 4]; + buf[i*2 + 1] = duk_lc_digits[t & 0x0f]; + } +#endif /* DUK_USE_HEX_FASTPATH */ + + /* XXX: Using a string return value forces a string intern which is + * not always necessary. As a rough performance measure, hex encode + * time for tests/perf/test-hex-encode.js dropped from ~35s to ~15s + * without string coercion. Change to returning a buffer and let the + * caller coerce to string if necessary? + */ + + ret = duk_to_string(ctx, -1); + duk_replace(ctx, index); + return ret; +} + +DUK_EXTERNAL void duk_hex_decode(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + const duk_uint8_t *inp; + duk_size_t len; + duk_size_t i; + duk_int_t t; + duk_uint8_t *buf; +#if defined(DUK_USE_HEX_FASTPATH) + duk_int_t chk; + duk_uint8_t *p; + duk_size_t len_safe; +#endif + + DUK_ASSERT_CTX_VALID(ctx); + + index = duk_require_normalize_index(ctx, index); + inp = duk__prep_codec_arg(ctx, index, &len); + DUK_ASSERT(inp != NULL || len == 0); + + if (len & 0x01) { + goto type_error; + } + + /* Fixed buffer, no zeroing because we'll fill all the data. */ + buf = (duk_uint8_t *) duk_push_buffer_raw(ctx, len / 2, DUK_BUF_FLAG_NOZERO /*flags*/); + DUK_ASSERT(buf != NULL); + +#if defined(DUK_USE_HEX_FASTPATH) + p = buf; + len_safe = len & ~0x07U; + for (i = 0; i < len_safe; i += 8) { + t = ((duk_int_t) duk_hex_dectab_shift4[inp[i]]) | + ((duk_int_t) duk_hex_dectab[inp[i + 1]]); + chk = t; + p[0] = (duk_uint8_t) t; + t = ((duk_int_t) duk_hex_dectab_shift4[inp[i + 2]]) | + ((duk_int_t) duk_hex_dectab[inp[i + 3]]); + chk |= t; + p[1] = (duk_uint8_t) t; + t = ((duk_int_t) duk_hex_dectab_shift4[inp[i + 4]]) | + ((duk_int_t) duk_hex_dectab[inp[i + 5]]); + chk |= t; + p[2] = (duk_uint8_t) t; + t = ((duk_int_t) duk_hex_dectab_shift4[inp[i + 6]]) | + ((duk_int_t) duk_hex_dectab[inp[i + 7]]); + chk |= t; + p[3] = (duk_uint8_t) t; + p += 4; + + /* Check if any lookup above had a negative result. */ + if (DUK_UNLIKELY(chk < 0)) { + goto type_error; + } + } + for (; i < len; i += 2) { + t = (((duk_int_t) duk_hex_dectab[inp[i]]) << 4) | + ((duk_int_t) duk_hex_dectab[inp[i + 1]]); + if (DUK_UNLIKELY(t < 0)) { + goto type_error; + } + *p++ = (duk_uint8_t) t; + } +#else /* DUK_USE_HEX_FASTPATH */ + for (i = 0; i < len; i += 2) { + /* For invalid characters the value -1 gets extended to + * at least 16 bits. If either nybble is invalid, the + * resulting 't' will be < 0. + */ + t = (((duk_int_t) duk_hex_dectab[inp[i]]) << 4) | + ((duk_int_t) duk_hex_dectab[inp[i + 1]]); + if (DUK_UNLIKELY(t < 0)) { + goto type_error; + } + buf[i >> 1] = (duk_uint8_t) t; + } +#endif /* DUK_USE_HEX_FASTPATH */ + + duk_replace(ctx, index); + return; + + type_error: + DUK_ERROR_TYPE(thr, DUK_STR_DECODE_FAILED); +} + +DUK_EXTERNAL const char *duk_json_encode(duk_context *ctx, duk_idx_t index) { +#ifdef DUK_USE_ASSERTIONS + duk_idx_t top_at_entry; +#endif + const char *ret; + + DUK_ASSERT_CTX_VALID(ctx); +#ifdef DUK_USE_ASSERTIONS + top_at_entry = duk_get_top(ctx); +#endif + + index = duk_require_normalize_index(ctx, index); + duk_bi_json_stringify_helper(ctx, + index /*idx_value*/, + DUK_INVALID_INDEX /*idx_replacer*/, + DUK_INVALID_INDEX /*idx_space*/, + 0 /*flags*/); + DUK_ASSERT(duk_is_string(ctx, -1)); + duk_replace(ctx, index); + ret = duk_get_string(ctx, index); + + DUK_ASSERT(duk_get_top(ctx) == top_at_entry); + + return ret; +} + +DUK_EXTERNAL void duk_json_decode(duk_context *ctx, duk_idx_t index) { +#ifdef DUK_USE_ASSERTIONS + duk_idx_t top_at_entry; +#endif + + DUK_ASSERT_CTX_VALID(ctx); +#ifdef DUK_USE_ASSERTIONS + top_at_entry = duk_get_top(ctx); +#endif + + index = duk_require_normalize_index(ctx, index); + duk_bi_json_parse_helper(ctx, + index /*idx_value*/, + DUK_INVALID_INDEX /*idx_reviver*/, + 0 /*flags*/); + duk_replace(ctx, index); + + DUK_ASSERT(duk_get_top(ctx) == top_at_entry); +} +/* + * Compilation and evaluation + */ + +/* include removed: duk_internal.h */ + +typedef struct duk__compile_raw_args duk__compile_raw_args; +struct duk__compile_raw_args { + duk_size_t src_length; /* should be first on 64-bit platforms */ + const duk_uint8_t *src_buffer; + duk_uint_t flags; +}; + +/* Eval is just a wrapper now. */ +DUK_EXTERNAL duk_int_t duk_eval_raw(duk_context *ctx, const char *src_buffer, duk_size_t src_length, duk_uint_t flags) { + duk_uint_t comp_flags; + duk_int_t rc; + + DUK_ASSERT_CTX_VALID(ctx); + + /* Note: strictness is *not* inherited from the current Duktape/C. + * This would be confusing because the current strictness state + * depends on whether we're running inside a Duktape/C activation + * (= strict mode) or outside of any activation (= non-strict mode). + * See tests/api/test-eval-strictness.c for more discussion. + */ + + /* [ ... source? filename? ] (depends on flags) */ + + comp_flags = flags; + comp_flags |= DUK_COMPILE_EVAL; + rc = duk_compile_raw(ctx, src_buffer, src_length, comp_flags); /* may be safe, or non-safe depending on flags */ + + /* [ ... closure/error ] */ + + if (rc != DUK_EXEC_SUCCESS) { + rc = DUK_EXEC_ERROR; + goto got_rc; + } + + duk_push_global_object(ctx); /* explicit 'this' binding, see GH-164 */ + + if (flags & DUK_COMPILE_SAFE) { + rc = duk_pcall_method(ctx, 0); + } else { + duk_call_method(ctx, 0); + rc = DUK_EXEC_SUCCESS; + } + + /* [ ... result/error ] */ + + got_rc: + if (flags & DUK_COMPILE_NORESULT) { + duk_pop(ctx); + } + + return rc; +} + +/* Helper which can be called both directly and with duk_safe_call(). */ +DUK_LOCAL duk_ret_t duk__do_compile(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk__compile_raw_args *comp_args; + duk_uint_t flags; + duk_small_uint_t comp_flags; + duk_hcompiledfunction *h_templ; + + DUK_ASSERT_CTX_VALID(ctx); + + /* Note: strictness is not inherited from the current Duktape/C + * context. Otherwise it would not be possible to compile + * non-strict code inside a Duktape/C activation (which is + * always strict now). See tests/api/test-eval-strictness.c + * for discussion. + */ + + /* [ ... source? filename? &comp_args ] (depends on flags) */ + + comp_args = (duk__compile_raw_args *) duk_require_pointer(ctx, -1); + flags = comp_args->flags; + duk_pop(ctx); + + /* [ ... source? filename? ] */ + + if (flags & DUK_COMPILE_NOFILENAME) { + /* Automatic filename: 'eval' or 'input'. */ + duk_push_hstring_stridx(ctx, (flags & DUK_COMPILE_EVAL) ? DUK_STRIDX_EVAL : DUK_STRIDX_INPUT); + } + + /* [ ... source? filename ] */ + + if (!comp_args->src_buffer) { + duk_hstring *h_sourcecode; + + h_sourcecode = duk_get_hstring(ctx, -2); + if ((flags & DUK_COMPILE_NOSOURCE) || /* args incorrect */ + (h_sourcecode == NULL)) { /* e.g. duk_push_string_file_raw() pushed undefined */ + /* XXX: when this error is caused by a nonexistent + * file given to duk_peval_file() or similar, the + * error message is not the best possible. + */ + DUK_ERROR_API(thr, DUK_STR_NO_SOURCECODE); + } + DUK_ASSERT(h_sourcecode != NULL); + comp_args->src_buffer = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_sourcecode); + comp_args->src_length = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_sourcecode); + } + DUK_ASSERT(comp_args->src_buffer != NULL); + + /* XXX: unnecessary translation of flags */ + comp_flags = 0; + if (flags & DUK_COMPILE_EVAL) { + comp_flags |= DUK_JS_COMPILE_FLAG_EVAL; + } + if (flags & DUK_COMPILE_FUNCTION) { + comp_flags |= DUK_JS_COMPILE_FLAG_EVAL | + DUK_JS_COMPILE_FLAG_FUNCEXPR; + } + if (flags & DUK_COMPILE_STRICT) { + comp_flags |= DUK_JS_COMPILE_FLAG_STRICT; + } + + /* [ ... source? filename ] */ + + duk_js_compile(thr, comp_args->src_buffer, comp_args->src_length, comp_flags); + + /* [ ... source? func_template ] */ + + if (flags & DUK_COMPILE_NOSOURCE) { + ; + } else { + duk_remove(ctx, -2); + } + + /* [ ... func_template ] */ + + h_templ = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1); + DUK_ASSERT(h_templ != NULL); + duk_js_push_closure(thr, + h_templ, + thr->builtins[DUK_BIDX_GLOBAL_ENV], + thr->builtins[DUK_BIDX_GLOBAL_ENV], + 1 /*add_auto_proto*/); + duk_remove(ctx, -2); /* -> [ ... closure ] */ + + /* [ ... closure ] */ + + return 1; +} + +DUK_EXTERNAL duk_int_t duk_compile_raw(duk_context *ctx, const char *src_buffer, duk_size_t src_length, duk_uint_t flags) { + duk__compile_raw_args comp_args_alloc; + duk__compile_raw_args *comp_args = &comp_args_alloc; + + DUK_ASSERT_CTX_VALID(ctx); + + if ((flags & DUK_COMPILE_STRLEN) && (src_buffer != NULL)) { + /* String length is computed here to avoid multiple evaluation + * of a macro argument in the calling side. + */ + src_length = DUK_STRLEN(src_buffer); + } + + comp_args->src_buffer = (const duk_uint8_t *) src_buffer; + comp_args->src_length = src_length; + comp_args->flags = flags; + duk_push_pointer(ctx, (void *) comp_args); + + /* [ ... source? filename? &comp_args ] (depends on flags) */ + + if (flags & DUK_COMPILE_SAFE) { + duk_int_t rc; + duk_int_t nargs; + duk_int_t nrets = 1; + + /* Arguments can be: [ source? filename? &comp_args] so that + * nargs is 1 to 3. Call site encodes the correct nargs count + * directly into flags. + */ + nargs = flags & 0x07; + DUK_ASSERT(nargs == (1 + + ((flags & DUK_COMPILE_NOSOURCE) ? 0 : 1) + + ((flags & DUK_COMPILE_NOFILENAME) ? 0 : 1))); + rc = duk_safe_call(ctx, duk__do_compile, nargs, nrets); + + /* [ ... closure ] */ + return rc; + } + + (void) duk__do_compile(ctx); + + /* [ ... closure ] */ + return DUK_EXEC_SUCCESS; +} +/* + * Debugging related API calls + */ + +/* include removed: duk_internal.h */ + +DUK_EXTERNAL void duk_push_context_dump(duk_context *ctx) { + duk_idx_t idx; + duk_idx_t top; + + DUK_ASSERT_CTX_VALID(ctx); + + /* We don't duk_require_stack() here now, but rely on the caller having + * enough space. + */ + + top = duk_get_top(ctx); + duk_push_array(ctx); + for (idx = 0; idx < top; idx++) { + duk_dup(ctx, idx); + duk_put_prop_index(ctx, -2, idx); + } + + /* XXX: conversion errors should not propagate outwards. + * Perhaps values need to be coerced individually? + */ + duk_bi_json_stringify_helper(ctx, + duk_get_top_index(ctx), /*idx_value*/ + DUK_INVALID_INDEX, /*idx_replacer*/ + DUK_INVALID_INDEX, /*idx_space*/ + DUK_JSON_FLAG_EXT_CUSTOM | + DUK_JSON_FLAG_ASCII_ONLY | + DUK_JSON_FLAG_AVOID_KEY_QUOTES /*flags*/); + + duk_push_sprintf(ctx, "ctx: top=%ld, stack=%s", (long) top, (const char *) duk_safe_to_string(ctx, -1)); + duk_replace(ctx, -3); /* [ ... arr jsonx(arr) res ] -> [ ... res jsonx(arr) ] */ + duk_pop(ctx); + DUK_ASSERT(duk_is_string(ctx, -1)); +} + +#if defined(DUK_USE_DEBUGGER_SUPPORT) + +DUK_EXTERNAL void duk_debugger_attach_custom(duk_context *ctx, + duk_debug_read_function read_cb, + duk_debug_write_function write_cb, + duk_debug_peek_function peek_cb, + duk_debug_read_flush_function read_flush_cb, + duk_debug_write_flush_function write_flush_cb, + duk_debug_request_function request_cb, + duk_debug_detached_function detached_cb, + void *udata) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_heap *heap; + const char *str; + duk_size_t len; + + /* XXX: should there be an error or an automatic detach if + * already attached? + */ + + DUK_D(DUK_DPRINT("application called duk_debugger_attach()")); + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(read_cb != NULL); + DUK_ASSERT(write_cb != NULL); + /* Other callbacks are optional. */ + + heap = thr->heap; + heap->dbg_read_cb = read_cb; + heap->dbg_write_cb = write_cb; + heap->dbg_peek_cb = peek_cb; + heap->dbg_read_flush_cb = read_flush_cb; + heap->dbg_write_flush_cb = write_flush_cb; + heap->dbg_request_cb = request_cb; + heap->dbg_detached_cb = detached_cb; + heap->dbg_udata = udata; + heap->dbg_have_next_byte = 0; + + /* Start in paused state. */ + heap->dbg_processing = 0; + heap->dbg_paused = 1; + heap->dbg_state_dirty = 1; + heap->dbg_force_restart = 0; + heap->dbg_step_type = 0; + heap->dbg_step_thread = NULL; + heap->dbg_step_csindex = 0; + heap->dbg_step_startline = 0; + heap->dbg_exec_counter = 0; + heap->dbg_last_counter = 0; + heap->dbg_last_time = 0.0; + + /* Send version identification and flush right afterwards. Note that + * we must write raw, unframed bytes here. + */ + duk_push_sprintf(ctx, "%ld %ld %s %s\n", + (long) DUK_DEBUG_PROTOCOL_VERSION, + (long) DUK_VERSION, + (const char *) DUK_GIT_DESCRIBE, + (const char *) DUK_USE_TARGET_INFO); + str = duk_get_lstring(ctx, -1, &len); + DUK_ASSERT(str != NULL); + duk_debug_write_bytes(thr, (const duk_uint8_t *) str, len); + duk_debug_write_flush(thr); + duk_pop(ctx); +} + +DUK_EXTERNAL void duk_debugger_detach(duk_context *ctx) { + duk_hthread *thr; + + DUK_D(DUK_DPRINT("application called duk_debugger_detach()")); + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK_ASSERT(thr != NULL); + DUK_ASSERT(thr->heap != NULL); + + /* Can be called multiple times with no harm. */ + duk_debug_do_detach(thr->heap); +} + +DUK_EXTERNAL void duk_debugger_cooperate(duk_context *ctx) { + duk_hthread *thr; + duk_bool_t processed_messages; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK_ASSERT(thr != NULL); + DUK_ASSERT(thr->heap != NULL); + + if (!DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) { + return; + } + if (thr->callstack_top > 0 || thr->heap->dbg_processing) { + /* Calling duk_debugger_cooperate() while Duktape is being + * called into is not supported. This is not a 100% check + * but prevents any damage in most cases. + */ + return; + } + + processed_messages = duk_debug_process_messages(thr, 1 /*no_block*/); + DUK_UNREF(processed_messages); +} + +DUK_EXTERNAL duk_bool_t duk_debugger_notify(duk_context *ctx, duk_idx_t nvalues) { + duk_hthread *thr; + duk_idx_t top; + duk_idx_t idx; + duk_bool_t ret = 0; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK_ASSERT(thr != NULL); + DUK_ASSERT(thr->heap != NULL); + + DUK_D(DUK_DPRINT("application called duk_debugger_notify() with nvalues=%ld", (long) nvalues)); + + top = duk_get_top(ctx); + if (top < nvalues) { + DUK_ERROR_API(thr, "not enough stack values for notify"); + return ret; /* unreachable */ + } + if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) { + duk_debug_write_notify(thr, DUK_DBG_CMD_APPNOTIFY); + for (idx = top - nvalues; idx < top; idx++) { + duk_tval *tv = DUK_GET_TVAL_POSIDX(ctx, idx); + duk_debug_write_tval(thr, tv); + } + duk_debug_write_eom(thr); + + /* Return non-zero (true) if we have a good reason to believe + * the notify was delivered; if we're still attached at least + * a transport error was not indicated by the transport write + * callback. This is not a 100% guarantee of course. + */ + if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) { + ret = 1; + } + } + duk_pop_n(ctx, nvalues); + return ret; +} + +DUK_EXTERNAL void duk_debugger_pause(duk_context *ctx) { + duk_hthread *thr; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK_ASSERT(thr != NULL); + DUK_ASSERT(thr->heap != NULL); + + DUK_D(DUK_DPRINT("application called duk_debugger_pause()")); + + /* Treat like a debugger statement: ignore when not attached. */ + if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) { + DUK_HEAP_SET_PAUSED(thr->heap); + + /* Pause on the next opcode executed. This is always safe to do even + * inside the debugger message loop: the interrupt counter will be reset + * to its proper value when the message loop exits. + */ + thr->interrupt_init = 1; + thr->interrupt_counter = 0; + } +} + +#else /* DUK_USE_DEBUGGER_SUPPORT */ + +DUK_EXTERNAL void duk_debugger_attach_custom(duk_context *ctx, + duk_debug_read_function read_cb, + duk_debug_write_function write_cb, + duk_debug_peek_function peek_cb, + duk_debug_read_flush_function read_flush_cb, + duk_debug_write_flush_function write_flush_cb, + duk_debug_request_function request_cb, + duk_debug_detached_function detached_cb, + void *udata) { + DUK_ASSERT_CTX_VALID(ctx); + DUK_UNREF(read_cb); + DUK_UNREF(write_cb); + DUK_UNREF(peek_cb); + DUK_UNREF(read_flush_cb); + DUK_UNREF(write_flush_cb); + DUK_UNREF(request_cb); + DUK_UNREF(detached_cb); + DUK_UNREF(udata); + DUK_ERROR_API((duk_hthread *) ctx, "no debugger support"); +} + +DUK_EXTERNAL void duk_debugger_detach(duk_context *ctx) { + DUK_ASSERT_CTX_VALID(ctx); + DUK_ERROR_API((duk_hthread *) ctx, "no debugger support"); +} + +DUK_EXTERNAL void duk_debugger_cooperate(duk_context *ctx) { + /* nop */ + DUK_ASSERT_CTX_VALID(ctx); + DUK_UNREF(ctx); +} + +DUK_EXTERNAL duk_bool_t duk_debugger_notify(duk_context *ctx, duk_idx_t nvalues) { + duk_idx_t top; + + DUK_ASSERT_CTX_VALID(ctx); + + top = duk_get_top(ctx); + if (top < nvalues) { + DUK_ERROR_API((duk_hthread *) ctx, "not enough stack values for notify"); + return 0; /* unreachable */ + } + + /* No debugger support, just pop values. */ + duk_pop_n(ctx, nvalues); + return 0; +} + +DUK_EXTERNAL void duk_debugger_pause(duk_context *ctx) { + /* Treat like debugger statement: nop */ + DUK_ASSERT_CTX_VALID(ctx); + DUK_UNREF(ctx); +} + +#endif /* DUK_USE_DEBUGGER_SUPPORT */ +/* + * Heap creation and destruction + */ + +/* include removed: duk_internal.h */ + +DUK_EXTERNAL +duk_context *duk_create_heap(duk_alloc_function alloc_func, + duk_realloc_function realloc_func, + duk_free_function free_func, + void *heap_udata, + duk_fatal_function fatal_handler) { + duk_heap *heap = NULL; + duk_context *ctx; + + /* Assume that either all memory funcs are NULL or non-NULL, mixed + * cases will now be unsafe. + */ + + /* XXX: just assert non-NULL values here and make caller arguments + * do the defaulting to the default implementations (smaller code)? + */ + + if (!alloc_func) { + DUK_ASSERT(realloc_func == NULL); + DUK_ASSERT(free_func == NULL); +#if defined(DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS) + alloc_func = duk_default_alloc_function; + realloc_func = duk_default_realloc_function; + free_func = duk_default_free_function; +#else + DUK_D(DUK_DPRINT("no allocation functions given and no default providers")); + return NULL; +#endif + } else { + DUK_ASSERT(realloc_func != NULL); + DUK_ASSERT(free_func != NULL); + } + + if (!fatal_handler) { + fatal_handler = duk_default_fatal_handler; + } + + DUK_ASSERT(alloc_func != NULL); + DUK_ASSERT(realloc_func != NULL); + DUK_ASSERT(free_func != NULL); + DUK_ASSERT(fatal_handler != NULL); + + heap = duk_heap_alloc(alloc_func, realloc_func, free_func, heap_udata, fatal_handler); + if (!heap) { + return NULL; + } + ctx = (duk_context *) heap->heap_thread; + DUK_ASSERT(ctx != NULL); + DUK_ASSERT(((duk_hthread *) ctx)->heap != NULL); + return ctx; +} + +DUK_EXTERNAL void duk_destroy_heap(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_heap *heap; + + if (!ctx) { + return; + } + heap = thr->heap; + DUK_ASSERT(heap != NULL); + + duk_heap_free(heap); +} + +/* XXX: better place for this */ +DUK_EXTERNAL void duk_set_global_object(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *h_glob; + duk_hobject *h_prev_glob; + duk_hobject *h_env; + duk_hobject *h_prev_env; + + DUK_D(DUK_DPRINT("replace global object with: %!T", duk_get_tval(ctx, -1))); + + h_glob = duk_require_hobject(ctx, -1); + DUK_ASSERT(h_glob != NULL); + + /* + * Replace global object. + */ + + h_prev_glob = thr->builtins[DUK_BIDX_GLOBAL]; + DUK_UNREF(h_prev_glob); + thr->builtins[DUK_BIDX_GLOBAL] = h_glob; + DUK_HOBJECT_INCREF(thr, h_glob); + DUK_HOBJECT_DECREF_ALLOWNULL(thr, h_prev_glob); /* side effects, in theory (referenced by global env) */ + + /* + * Replace lexical environment for global scope + * + * Create a new object environment for the global lexical scope. + * We can't just reset the _Target property of the current one, + * because the lexical scope is shared by other threads with the + * same (initial) built-ins. + */ + + (void) duk_push_object_helper(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJENV), + -1); /* no prototype, updated below */ + + duk_dup(ctx, -2); + duk_dup(ctx, -3); + + /* [ ... new_glob new_env new_glob new_glob ] */ + + duk_xdef_prop_stridx(thr, -3, DUK_STRIDX_INT_TARGET, DUK_PROPDESC_FLAGS_NONE); + duk_xdef_prop_stridx(thr, -2, DUK_STRIDX_INT_THIS, DUK_PROPDESC_FLAGS_NONE); + + /* [ ... new_glob new_env ] */ + + h_env = duk_get_hobject(ctx, -1); + DUK_ASSERT(h_env != NULL); + + h_prev_env = thr->builtins[DUK_BIDX_GLOBAL_ENV]; + thr->builtins[DUK_BIDX_GLOBAL_ENV] = h_env; + DUK_HOBJECT_INCREF(thr, h_env); + DUK_HOBJECT_DECREF_ALLOWNULL(thr, h_prev_env); /* side effects */ + DUK_UNREF(h_env); /* without refcounts */ + DUK_UNREF(h_prev_env); + + /* [ ... new_glob new_env ] */ + + duk_pop_2(ctx); + + /* [ ... ] */ +} +/* + * Logging + * + * Current logging primitive is a sprintf-style log which is convenient + * for most C code. Another useful primitive would be to log N arguments + * from value stack (like the Ecmascript binding does). + */ + +/* include removed: duk_internal.h */ + +DUK_EXTERNAL void duk_log_va(duk_context *ctx, duk_int_t level, const char *fmt, va_list ap) { + /* stridx_logfunc[] must be static to allow initializer with old compilers like BCC */ + static const duk_uint16_t stridx_logfunc[6] = { + DUK_STRIDX_LC_TRACE, DUK_STRIDX_LC_DEBUG, DUK_STRIDX_LC_INFO, + DUK_STRIDX_LC_WARN, DUK_STRIDX_LC_ERROR, DUK_STRIDX_LC_FATAL + }; + + DUK_ASSERT_CTX_VALID(ctx); + + if (level < 0) { + level = 0; + } else if (level > (int) (sizeof(stridx_logfunc) / sizeof(duk_uint16_t)) - 1) { + level = (int) (sizeof(stridx_logfunc) / sizeof(duk_uint16_t)) - 1; + } + + duk_push_hobject_bidx(ctx, DUK_BIDX_LOGGER_CONSTRUCTOR); + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_CLOG); + duk_get_prop_stridx(ctx, -1, stridx_logfunc[level]); + duk_dup(ctx, -2); + + /* [ ... Logger clog logfunc clog ] */ + + duk_push_vsprintf(ctx, fmt, ap); + + /* [ ... Logger clog logfunc clog(=this) msg ] */ + + duk_call_method(ctx, 1 /*nargs*/); + + /* [ ... Logger clog res ] */ + + duk_pop_3(ctx); +} + +DUK_EXTERNAL void duk_log(duk_context *ctx, duk_int_t level, const char *fmt, ...) { + va_list ap; + + DUK_ASSERT_CTX_VALID(ctx); + + va_start(ap, fmt); + duk_log_va(ctx, level, fmt, ap); + va_end(ap); +} +/* + * Memory calls. + */ + +/* include removed: duk_internal.h */ + +DUK_EXTERNAL void *duk_alloc_raw(duk_context *ctx, duk_size_t size) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT_CTX_VALID(ctx); + + return DUK_ALLOC_RAW(thr->heap, size); +} + +DUK_EXTERNAL void duk_free_raw(duk_context *ctx, void *ptr) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT_CTX_VALID(ctx); + + DUK_FREE_RAW(thr->heap, ptr); +} + +DUK_EXTERNAL void *duk_realloc_raw(duk_context *ctx, void *ptr, duk_size_t size) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT_CTX_VALID(ctx); + + return DUK_REALLOC_RAW(thr->heap, ptr, size); +} + +DUK_EXTERNAL void *duk_alloc(duk_context *ctx, duk_size_t size) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT_CTX_VALID(ctx); + + return DUK_ALLOC(thr->heap, size); +} + +DUK_EXTERNAL void duk_free(duk_context *ctx, void *ptr) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT_CTX_VALID(ctx); + + DUK_FREE(thr->heap, ptr); +} + +DUK_EXTERNAL void *duk_realloc(duk_context *ctx, void *ptr, duk_size_t size) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT_CTX_VALID(ctx); + + /* + * Note: since this is an exposed API call, there should be + * no way a mark-and-sweep could have a side effect on the + * memory allocation behind 'ptr'; the pointer should never + * be something that Duktape wants to change. + * + * Thus, no need to use DUK_REALLOC_INDIRECT (and we don't + * have the storage location here anyway). + */ + + return DUK_REALLOC(thr->heap, ptr, size); +} + +DUK_EXTERNAL void duk_get_memory_functions(duk_context *ctx, duk_memory_functions *out_funcs) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_heap *heap; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(out_funcs != NULL); + DUK_ASSERT(thr != NULL); + DUK_ASSERT(thr->heap != NULL); + + heap = thr->heap; + out_funcs->alloc_func = heap->alloc_func; + out_funcs->realloc_func = heap->realloc_func; + out_funcs->free_func = heap->free_func; + out_funcs->udata = heap->heap_udata; +} + +DUK_EXTERNAL void duk_gc(duk_context *ctx, duk_uint_t flags) { +#ifdef DUK_USE_MARK_AND_SWEEP + duk_hthread *thr = (duk_hthread *) ctx; + duk_heap *heap; + + DUK_UNREF(flags); + + /* NULL accepted */ + if (!ctx) { + return; + } + DUK_ASSERT_CTX_VALID(ctx); + heap = thr->heap; + DUK_ASSERT(heap != NULL); + + DUK_D(DUK_DPRINT("mark-and-sweep requested by application")); + duk_heap_mark_and_sweep(heap, 0); +#else + DUK_D(DUK_DPRINT("mark-and-sweep requested by application but mark-and-sweep not enabled, ignoring")); + DUK_UNREF(ctx); + DUK_UNREF(flags); +#endif +} +/* + * Object handling: property access and other support functions. + */ + +/* include removed: duk_internal.h */ + +/* + * Property handling + * + * The API exposes only the most common property handling functions. + * The caller can invoke Ecmascript built-ins for full control (e.g. + * defineProperty, getOwnPropertyDescriptor). + */ + +DUK_EXTERNAL duk_bool_t duk_get_prop(duk_context *ctx, duk_idx_t obj_index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv_obj; + duk_tval *tv_key; + duk_bool_t rc; + + DUK_ASSERT_CTX_VALID(ctx); + + /* Note: copying tv_obj and tv_key to locals to shield against a valstack + * resize is not necessary for a property get right now. + */ + + tv_obj = duk_require_tval(ctx, obj_index); + tv_key = duk_require_tval(ctx, -1); + + rc = duk_hobject_getprop(thr, tv_obj, tv_key); + DUK_ASSERT(rc == 0 || rc == 1); + /* a value is left on stack regardless of rc */ + + duk_remove(ctx, -2); /* remove key */ + return rc; /* 1 if property found, 0 otherwise */ +} + +DUK_EXTERNAL duk_bool_t duk_get_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) { + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(key != NULL); + + obj_index = duk_require_normalize_index(ctx, obj_index); + duk_push_string(ctx, key); + return duk_get_prop(ctx, obj_index); +} + +DUK_EXTERNAL duk_bool_t duk_get_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) { + DUK_ASSERT_CTX_VALID(ctx); + + obj_index = duk_require_normalize_index(ctx, obj_index); + duk_push_uarridx(ctx, arr_index); + return duk_get_prop(ctx, obj_index); +} + +DUK_INTERNAL duk_bool_t duk_get_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT_DISABLE(stridx >= 0); + DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); + DUK_UNREF(thr); + + obj_index = duk_require_normalize_index(ctx, obj_index); + duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx)); + return duk_get_prop(ctx, obj_index); +} + +DUK_INTERNAL duk_bool_t duk_get_prop_stridx_boolean(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_bool_t *out_has_prop) { + duk_bool_t rc; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT_DISABLE(stridx >= 0); + DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); + + rc = duk_get_prop_stridx(ctx, obj_index, stridx); + if (out_has_prop) { + *out_has_prop = rc; + } + rc = duk_to_boolean(ctx, -1); + DUK_ASSERT(rc == 0 || rc == 1); + duk_pop(ctx); + return rc; +} + +DUK_LOCAL duk_bool_t duk__put_prop_shared(duk_context *ctx, duk_idx_t obj_idx, duk_idx_t idx_key) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv_obj; + duk_tval *tv_key; + duk_tval *tv_val; + duk_small_int_t throw_flag; + duk_bool_t rc; + + /* Note: copying tv_obj and tv_key to locals to shield against a valstack + * resize is not necessary for a property put right now (putprop protects + * against it internally). + */ + + /* Key and value indices are either (-2, -1) or (-1, -2). Given idx_key, + * idx_val is always (idx_key ^ 0x01). + */ + DUK_ASSERT((idx_key == -2 && (idx_key ^ 1) == -1) || + (idx_key == -1 && (idx_key ^ 1) == -2)); + tv_obj = duk_require_tval(ctx, obj_idx); + tv_key = duk_require_tval(ctx, idx_key); + tv_val = duk_require_tval(ctx, idx_key ^ 1); + throw_flag = duk_is_strict_call(ctx); + + rc = duk_hobject_putprop(thr, tv_obj, tv_key, tv_val, throw_flag); + DUK_ASSERT(rc == 0 || rc == 1); + + duk_pop_2(ctx); /* remove key and value */ + return rc; /* 1 if property found, 0 otherwise */ +} + +DUK_EXTERNAL duk_bool_t duk_put_prop(duk_context *ctx, duk_idx_t obj_idx) { + DUK_ASSERT_CTX_VALID(ctx); + return duk__put_prop_shared(ctx, obj_idx, -2); +} + +DUK_EXTERNAL duk_bool_t duk_put_prop_string(duk_context *ctx, duk_idx_t obj_idx, const char *key) { + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(key != NULL); + + /* Careful here and with other duk_put_prop_xxx() helpers: the + * target object and the property value may be in the same value + * stack slot (unusual, but still conceptually clear). + */ + obj_idx = duk_normalize_index(ctx, obj_idx); + (void) duk_push_string(ctx, key); + return duk__put_prop_shared(ctx, obj_idx, -1); +} + +DUK_EXTERNAL duk_bool_t duk_put_prop_index(duk_context *ctx, duk_idx_t obj_idx, duk_uarridx_t arr_idx) { + DUK_ASSERT_CTX_VALID(ctx); + + obj_idx = duk_require_normalize_index(ctx, obj_idx); + duk_push_uarridx(ctx, arr_idx); + return duk__put_prop_shared(ctx, obj_idx, -1); +} + +DUK_INTERNAL duk_bool_t duk_put_prop_stridx(duk_context *ctx, duk_idx_t obj_idx, duk_small_int_t stridx) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT_DISABLE(stridx >= 0); + DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); + DUK_UNREF(thr); + + obj_idx = duk_require_normalize_index(ctx, obj_idx); + duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx)); + return duk__put_prop_shared(ctx, obj_idx, -1); +} + +DUK_EXTERNAL duk_bool_t duk_del_prop(duk_context *ctx, duk_idx_t obj_index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv_obj; + duk_tval *tv_key; + duk_small_int_t throw_flag; + duk_bool_t rc; + + DUK_ASSERT_CTX_VALID(ctx); + + /* Note: copying tv_obj and tv_key to locals to shield against a valstack + * resize is not necessary for a property delete right now. + */ + + tv_obj = duk_require_tval(ctx, obj_index); + tv_key = duk_require_tval(ctx, -1); + throw_flag = duk_is_strict_call(ctx); + + rc = duk_hobject_delprop(thr, tv_obj, tv_key, throw_flag); + DUK_ASSERT(rc == 0 || rc == 1); + + duk_pop(ctx); /* remove key */ + return rc; +} + +DUK_EXTERNAL duk_bool_t duk_del_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) { + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(key != NULL); + + obj_index = duk_require_normalize_index(ctx, obj_index); + duk_push_string(ctx, key); + return duk_del_prop(ctx, obj_index); +} + +DUK_EXTERNAL duk_bool_t duk_del_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) { + DUK_ASSERT_CTX_VALID(ctx); + + obj_index = duk_require_normalize_index(ctx, obj_index); + duk_push_uarridx(ctx, arr_index); + return duk_del_prop(ctx, obj_index); +} + +DUK_INTERNAL duk_bool_t duk_del_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT_DISABLE(stridx >= 0); + DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); + DUK_UNREF(thr); + + obj_index = duk_require_normalize_index(ctx, obj_index); + duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx)); + return duk_del_prop(ctx, obj_index); +} + +DUK_EXTERNAL duk_bool_t duk_has_prop(duk_context *ctx, duk_idx_t obj_index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv_obj; + duk_tval *tv_key; + duk_bool_t rc; + + DUK_ASSERT_CTX_VALID(ctx); + + /* Note: copying tv_obj and tv_key to locals to shield against a valstack + * resize is not necessary for a property existence check right now. + */ + + tv_obj = duk_require_tval(ctx, obj_index); + tv_key = duk_require_tval(ctx, -1); + + rc = duk_hobject_hasprop(thr, tv_obj, tv_key); + DUK_ASSERT(rc == 0 || rc == 1); + + duk_pop(ctx); /* remove key */ + return rc; /* 1 if property found, 0 otherwise */ +} + +DUK_EXTERNAL duk_bool_t duk_has_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) { + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(key != NULL); + + obj_index = duk_require_normalize_index(ctx, obj_index); + duk_push_string(ctx, key); + return duk_has_prop(ctx, obj_index); +} + +DUK_EXTERNAL duk_bool_t duk_has_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index) { + DUK_ASSERT_CTX_VALID(ctx); + + obj_index = duk_require_normalize_index(ctx, obj_index); + duk_push_uarridx(ctx, arr_index); + return duk_has_prop(ctx, obj_index); +} + +DUK_INTERNAL duk_bool_t duk_has_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT_DISABLE(stridx >= 0); + DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); + DUK_UNREF(thr); + + obj_index = duk_require_normalize_index(ctx, obj_index); + duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx)); + return duk_has_prop(ctx, obj_index); +} + +/* Define own property without inheritance looks and such. This differs from + * [[DefineOwnProperty]] because special behaviors (like Array 'length') are + * not invoked by this method. The caller must be careful to invoke any such + * behaviors if necessary. + */ +DUK_INTERNAL void duk_xdef_prop(duk_context *ctx, duk_idx_t obj_index, duk_small_uint_t desc_flags) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *obj; + duk_hstring *key; + + DUK_ASSERT_CTX_VALID(ctx); + + obj = duk_require_hobject(ctx, obj_index); + DUK_ASSERT(obj != NULL); + key = duk_to_hstring(ctx, -2); + DUK_ASSERT(key != NULL); + DUK_ASSERT(duk_require_tval(ctx, -1) != NULL); + + duk_hobject_define_property_internal(thr, obj, key, desc_flags); + + duk_pop(ctx); /* pop key */ +} + +DUK_INTERNAL void duk_xdef_prop_index(duk_context *ctx, duk_idx_t obj_index, duk_uarridx_t arr_index, duk_small_uint_t desc_flags) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *obj; + + DUK_ASSERT_CTX_VALID(ctx); + + obj = duk_require_hobject(ctx, obj_index); + DUK_ASSERT(obj != NULL); + + duk_hobject_define_property_internal_arridx(thr, obj, arr_index, desc_flags); + /* value popped by call */ +} + +DUK_INTERNAL void duk_xdef_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_uint_t desc_flags) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *obj; + duk_hstring *key; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT_DISABLE(stridx >= 0); + DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); + + obj = duk_require_hobject(ctx, obj_index); + DUK_ASSERT(obj != NULL); + key = DUK_HTHREAD_GET_STRING(thr, stridx); + DUK_ASSERT(key != NULL); + DUK_ASSERT(duk_require_tval(ctx, -1) != NULL); + + duk_hobject_define_property_internal(thr, obj, key, desc_flags); + /* value popped by call */ +} + +DUK_INTERNAL void duk_xdef_prop_stridx_builtin(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_int_t builtin_idx, duk_small_uint_t desc_flags) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *obj; + duk_hstring *key; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT_DISABLE(stridx >= 0); + DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); + DUK_ASSERT_DISABLE(builtin_idx >= 0); + DUK_ASSERT(builtin_idx < DUK_NUM_BUILTINS); + + obj = duk_require_hobject(ctx, obj_index); + DUK_ASSERT(obj != NULL); + key = DUK_HTHREAD_GET_STRING(thr, stridx); + DUK_ASSERT(key != NULL); + + duk_push_hobject(ctx, thr->builtins[builtin_idx]); + duk_hobject_define_property_internal(thr, obj, key, desc_flags); + /* value popped by call */ +} + +/* This is a rare property helper; it sets the global thrower (E5 Section 13.2.3) + * setter/getter into an object property. This is needed by the 'arguments' + * object creation code, function instance creation code, and Function.prototype.bind(). + */ + +DUK_INTERNAL void duk_xdef_prop_stridx_thrower(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_uint_t desc_flags) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *obj = duk_require_hobject(ctx, obj_index); + duk_hobject *thrower = thr->builtins[DUK_BIDX_TYPE_ERROR_THROWER]; + duk_hobject_define_accessor_internal(thr, obj, DUK_HTHREAD_GET_STRING(thr, stridx), thrower, thrower, desc_flags); +} + +/* Object.defineProperty() equivalent C binding. */ +DUK_EXTERNAL void duk_def_prop(duk_context *ctx, duk_idx_t obj_index, duk_uint_t flags) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_idx_t idx_base; + duk_hobject *obj; + duk_hstring *key; + duk_idx_t idx_value; + duk_hobject *get; + duk_hobject *set; + duk_uint_t is_data_desc; + duk_uint_t is_acc_desc; + + DUK_ASSERT_CTX_VALID(ctx); + + obj = duk_require_hobject(ctx, obj_index); + + is_data_desc = flags & (DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE); + is_acc_desc = flags & (DUK_DEFPROP_HAVE_GETTER | DUK_DEFPROP_HAVE_SETTER); + if (is_data_desc && is_acc_desc) { + /* "Have" flags must not be conflicting so that they would + * apply to both a plain property and an accessor at the same + * time. + */ + goto fail_invalid_desc; + } + + idx_base = duk_get_top_index(ctx); + if (flags & DUK_DEFPROP_HAVE_SETTER) { + duk_require_type_mask(ctx, idx_base, DUK_TYPE_MASK_UNDEFINED | + DUK_TYPE_MASK_OBJECT | + DUK_TYPE_MASK_LIGHTFUNC); + set = duk_get_hobject_or_lfunc_coerce(ctx, idx_base); + if (set != NULL && !DUK_HOBJECT_IS_CALLABLE(set)) { + goto fail_not_callable; + } + idx_base--; + } else { + set = NULL; + } + if (flags & DUK_DEFPROP_HAVE_GETTER) { + duk_require_type_mask(ctx, idx_base, DUK_TYPE_MASK_UNDEFINED | + DUK_TYPE_MASK_OBJECT | + DUK_TYPE_MASK_LIGHTFUNC); + get = duk_get_hobject_or_lfunc_coerce(ctx, idx_base); + if (get != NULL && !DUK_HOBJECT_IS_CALLABLE(get)) { + goto fail_not_callable; + } + idx_base--; + } else { + get = NULL; + } + if (flags & DUK_DEFPROP_HAVE_VALUE) { + idx_value = idx_base; + idx_base--; + } else { + idx_value = (duk_idx_t) -1; + } + key = duk_require_hstring(ctx, idx_base); + + duk_require_valid_index(ctx, idx_base); + + duk_hobject_define_property_helper(ctx, + flags /*defprop_flags*/, + obj, + key, + idx_value, + get, + set); + + /* Clean up stack */ + + duk_set_top(ctx, idx_base); + + /* [ ... obj ... ] */ + + return; + + fail_invalid_desc: + DUK_ERROR_TYPE(thr, DUK_STR_INVALID_DESCRIPTOR); + return; + + fail_not_callable: + DUK_ERROR_TYPE(thr, DUK_STR_NOT_CALLABLE); + return; +} + +/* + * Object related + * + * Note: seal() and freeze() are accessible through Ecmascript bindings, + * and are not exposed through the API. + */ + +DUK_EXTERNAL void duk_compact(duk_context *ctx, duk_idx_t obj_index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *obj; + + DUK_ASSERT_CTX_VALID(ctx); + + obj = duk_get_hobject(ctx, obj_index); + if (obj) { + /* Note: this may fail, caller should protect the call if necessary */ + duk_hobject_compact_props(thr, obj); + } +} + +/* XXX: the duk_hobject_enum.c stack APIs should be reworked */ + +DUK_EXTERNAL void duk_enum(duk_context *ctx, duk_idx_t obj_index, duk_uint_t enum_flags) { + DUK_ASSERT_CTX_VALID(ctx); + + duk_dup(ctx, obj_index); + duk_require_hobject_or_lfunc_coerce(ctx, -1); + duk_hobject_enumerator_create(ctx, enum_flags); /* [target] -> [enum] */ +} + +DUK_EXTERNAL duk_bool_t duk_next(duk_context *ctx, duk_idx_t enum_index, duk_bool_t get_value) { + DUK_ASSERT_CTX_VALID(ctx); + + duk_require_hobject(ctx, enum_index); + duk_dup(ctx, enum_index); + return duk_hobject_enumerator_next(ctx, get_value); +} + +/* + * Helpers for writing multiple properties + */ + +DUK_EXTERNAL void duk_put_function_list(duk_context *ctx, duk_idx_t obj_index, const duk_function_list_entry *funcs) { + const duk_function_list_entry *ent = funcs; + + DUK_ASSERT_CTX_VALID(ctx); + + obj_index = duk_require_normalize_index(ctx, obj_index); + if (ent != NULL) { + while (ent->key != NULL) { + duk_push_c_function(ctx, ent->value, ent->nargs); + duk_put_prop_string(ctx, obj_index, ent->key); + ent++; + } + } +} + +DUK_EXTERNAL void duk_put_number_list(duk_context *ctx, duk_idx_t obj_index, const duk_number_list_entry *numbers) { + const duk_number_list_entry *ent = numbers; + + DUK_ASSERT_CTX_VALID(ctx); + + obj_index = duk_require_normalize_index(ctx, obj_index); + if (ent != NULL) { + while (ent->key != NULL) { + duk_push_number(ctx, ent->value); + duk_put_prop_string(ctx, obj_index, ent->key); + ent++; + } + } +} + +/* + * Shortcut for accessing global object properties + */ + +DUK_EXTERNAL duk_bool_t duk_get_global_string(duk_context *ctx, const char *key) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_bool_t ret; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL); + + /* XXX: direct implementation */ + + duk_push_hobject(ctx, thr->builtins[DUK_BIDX_GLOBAL]); + ret = duk_get_prop_string(ctx, -1, key); + duk_remove(ctx, -2); + return ret; +} + +DUK_EXTERNAL duk_bool_t duk_put_global_string(duk_context *ctx, const char *key) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_bool_t ret; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL); + + /* XXX: direct implementation */ + + duk_push_hobject(ctx, thr->builtins[DUK_BIDX_GLOBAL]); + duk_insert(ctx, -2); + ret = duk_put_prop_string(ctx, -2, key); /* [ ... global val ] -> [ ... global ] */ + duk_pop(ctx); + return ret; +} + +/* + * Object prototype + */ + +DUK_EXTERNAL void duk_get_prototype(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *obj; + duk_hobject *proto; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_UNREF(thr); + + obj = duk_require_hobject(ctx, index); + DUK_ASSERT(obj != NULL); + + /* XXX: shared helper for duk_push_hobject_or_undefined()? */ + proto = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, obj); + if (proto) { + duk_push_hobject(ctx, proto); + } else { + duk_push_undefined(ctx); + } +} + +DUK_EXTERNAL void duk_set_prototype(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *obj; + duk_hobject *proto; + + DUK_ASSERT_CTX_VALID(ctx); + + obj = duk_require_hobject(ctx, index); + DUK_ASSERT(obj != NULL); + duk_require_type_mask(ctx, -1, DUK_TYPE_MASK_UNDEFINED | + DUK_TYPE_MASK_OBJECT); + proto = duk_get_hobject(ctx, -1); + /* proto can also be NULL here (allowed explicitly) */ + +#if defined(DUK_USE_ROM_OBJECTS) + if (DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj)) { + DUK_ERROR_TYPE(thr, DUK_STR_NOT_CONFIGURABLE); /* XXX: "read only object"? */ + return; + } +#endif + + DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, obj, proto); + + duk_pop(ctx); +} + +/* + * Object finalizer + */ + +/* XXX: these could be implemented as macros calling an internal function + * directly. + * XXX: same issue as with Duktape.fin: there's no way to delete the property + * now (just set it to undefined). + */ +DUK_EXTERNAL void duk_get_finalizer(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + + duk_get_prop_stridx(ctx, index, DUK_STRIDX_INT_FINALIZER); +} + +DUK_EXTERNAL void duk_set_finalizer(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + + duk_put_prop_stridx(ctx, index, DUK_STRIDX_INT_FINALIZER); +} +/* + * API calls related to general value stack manipulation: resizing the value + * stack, pushing and popping values, type checking and reading values, + * coercing values, etc. + * + * Also contains internal functions (such as duk_get_tval()), defined + * in duk_api_internal.h, with semantics similar to the public API. + */ + +/* XXX: repetition of stack pre-checks -> helper or macro or inline */ +/* XXX: shared api error strings, and perhaps even throw code for rare cases? */ + +/* include removed: duk_internal.h */ + +/* + * Forward declarations + */ + +DUK_LOCAL_DECL duk_idx_t duk__push_c_function_raw(duk_context *ctx, duk_c_function func, duk_idx_t nargs, duk_uint_t flags); + +/* + * Global state for working around missing variadic macros + */ + +#ifndef DUK_USE_VARIADIC_MACROS +DUK_EXTERNAL const char *duk_api_global_filename = NULL; +DUK_EXTERNAL duk_int_t duk_api_global_line = 0; +#endif + +/* + * Misc helpers + */ + +/* Check that there's room to push one value. */ +#if defined(DUK_USE_VALSTACK_UNSAFE) +/* Faster but value stack overruns are memory unsafe. */ +#define DUK__CHECK_SPACE() do { \ + DUK_ASSERT(!(thr->valstack_top >= thr->valstack_end)); \ + } while (0) +#else +#define DUK__CHECK_SPACE() do { \ + if (DUK_UNLIKELY(thr->valstack_top >= thr->valstack_end)) { \ + DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); \ + } \ + } while (0) +#endif + +DUK_LOCAL_DECL duk_heaphdr *duk__get_tagged_heaphdr_raw(duk_context *ctx, duk_idx_t index, duk_uint_t tag); + +DUK_LOCAL duk_int_t duk__api_coerce_d2i(duk_context *ctx, duk_idx_t index, duk_bool_t require) { + duk_hthread *thr; + duk_tval *tv; + duk_small_int_t c; + duk_double_t d; + + thr = (duk_hthread *) ctx; + + tv = duk_get_tval(ctx, index); + if (tv == NULL) { + goto error_notnumber; + } + + /* + * Special cases like NaN and +/- Infinity are handled explicitly + * because a plain C coercion from double to int handles these cases + * in undesirable ways. For instance, NaN may coerce to INT_MIN + * (not zero), and INT_MAX + 1 may coerce to INT_MIN (not INT_MAX). + * + * This double-to-int coercion differs from ToInteger() because it + * has a finite range (ToInteger() allows e.g. +/- Infinity). It + * also differs from ToInt32() because the INT_MIN/INT_MAX clamping + * depends on the size of the int type on the platform. In particular, + * on platforms with a 64-bit int type, the full range is allowed. + */ + +#if defined(DUK_USE_FASTINT) + if (DUK_TVAL_IS_FASTINT(tv)) { + duk_int64_t t = DUK_TVAL_GET_FASTINT(tv); +#if (DUK_INT_MAX <= 0x7fffffffL) + /* Clamping only necessary for 32-bit ints. */ + if (t < DUK_INT_MIN) { + t = DUK_INT_MIN; + } else if (t > DUK_INT_MAX) { + t = DUK_INT_MAX; + } +#endif + return (duk_int_t) t; + } +#endif + + if (DUK_TVAL_IS_NUMBER(tv)) { + d = DUK_TVAL_GET_NUMBER(tv); + c = (duk_small_int_t) DUK_FPCLASSIFY(d); + if (c == DUK_FP_NAN) { + return 0; + } else if (d < (duk_double_t) DUK_INT_MIN) { + /* covers -Infinity */ + return DUK_INT_MIN; + } else if (d > (duk_double_t) DUK_INT_MAX) { + /* covers +Infinity */ + return DUK_INT_MAX; + } else { + /* coerce towards zero */ + return (duk_int_t) d; + } + } + + error_notnumber: + + if (require) { + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "number", DUK_STR_NOT_NUMBER); + /* not reachable */ + } + return 0; +} + +DUK_LOCAL duk_uint_t duk__api_coerce_d2ui(duk_context *ctx, duk_idx_t index, duk_bool_t require) { + duk_hthread *thr; + duk_tval *tv; + duk_small_int_t c; + duk_double_t d; + + /* Same as above but for unsigned int range. */ + + thr = (duk_hthread *) ctx; + + tv = duk_get_tval(ctx, index); + if (tv == NULL) { + goto error_notnumber; + } + +#if defined(DUK_USE_FASTINT) + if (DUK_TVAL_IS_FASTINT(tv)) { + duk_int64_t t = DUK_TVAL_GET_FASTINT(tv); + if (t < 0) { + t = 0; + } +#if (DUK_UINT_MAX <= 0xffffffffUL) + /* Clamping only necessary for 32-bit ints. */ + else if (t > DUK_UINT_MAX) { + t = DUK_UINT_MAX; + } +#endif + return (duk_uint_t) t; + } +#endif + + if (DUK_TVAL_IS_NUMBER(tv)) { + d = DUK_TVAL_GET_NUMBER(tv); + c = (duk_small_int_t) DUK_FPCLASSIFY(d); + if (c == DUK_FP_NAN) { + return 0; + } else if (d < 0.0) { + /* covers -Infinity */ + return (duk_uint_t) 0; + } else if (d > (duk_double_t) DUK_UINT_MAX) { + /* covers +Infinity */ + return (duk_uint_t) DUK_UINT_MAX; + } else { + /* coerce towards zero */ + return (duk_uint_t) d; + } + } + + error_notnumber: + + if (require) { + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "number", DUK_STR_NOT_NUMBER); + /* not reachable */ + } + return 0; +} + +/* + * Stack index validation/normalization and getting a stack duk_tval ptr. + * + * These are called by many API entrypoints so the implementations must be + * fast and "inlined". + * + * There's some repetition because of this; keep the functions in sync. + */ + +DUK_EXTERNAL duk_idx_t duk_normalize_index(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_uidx_t vs_size; + duk_uidx_t uindex; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(DUK_INVALID_INDEX < 0); + + /* Care must be taken to avoid pointer wrapping in the index + * validation. For instance, on a 32-bit platform with 8-byte + * duk_tval the index 0x20000000UL would wrap the memory space + * once. + */ + + /* Assume value stack sizes (in elements) fits into duk_idx_t. */ + DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); + vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom); + DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */ + + if (index < 0) { + uindex = vs_size + (duk_uidx_t) index; + } else { + /* since index non-negative */ + DUK_ASSERT(index != DUK_INVALID_INDEX); + uindex = (duk_uidx_t) index; + } + + /* DUK_INVALID_INDEX won't be accepted as a valid index. */ + DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size); + + if (DUK_LIKELY(uindex < vs_size)) { + return (duk_idx_t) uindex; + } + return DUK_INVALID_INDEX; +} + +DUK_EXTERNAL duk_idx_t duk_require_normalize_index(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_uidx_t vs_size; + duk_uidx_t uindex; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(DUK_INVALID_INDEX < 0); + + DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); + vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom); + DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */ + + if (index < 0) { + uindex = vs_size + (duk_uidx_t) index; + } else { + DUK_ASSERT(index != DUK_INVALID_INDEX); + uindex = (duk_uidx_t) index; + } + + /* DUK_INVALID_INDEX won't be accepted as a valid index. */ + DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size); + + if (DUK_LIKELY(uindex < vs_size)) { + return (duk_idx_t) uindex; + } + DUK_ERROR_API_INDEX(thr, index); + return 0; /* unreachable */ +} + +DUK_INTERNAL duk_tval *duk_get_tval(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_uidx_t vs_size; + duk_uidx_t uindex; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(DUK_INVALID_INDEX < 0); + + DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); + vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom); + DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */ + + if (index < 0) { + uindex = vs_size + (duk_uidx_t) index; + } else { + DUK_ASSERT(index != DUK_INVALID_INDEX); + uindex = (duk_uidx_t) index; + } + + /* DUK_INVALID_INDEX won't be accepted as a valid index. */ + DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size); + + if (DUK_LIKELY(uindex < vs_size)) { + return thr->valstack_bottom + uindex; + } + return NULL; +} + +DUK_INTERNAL duk_tval *duk_require_tval(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_uidx_t vs_size; + duk_uidx_t uindex; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(DUK_INVALID_INDEX < 0); + + DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); + vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom); + DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */ + + /* Use unsigned arithmetic to optimize comparison. */ + if (index < 0) { + uindex = vs_size + (duk_uidx_t) index; + } else { + DUK_ASSERT(index != DUK_INVALID_INDEX); + uindex = (duk_uidx_t) index; + } + + /* DUK_INVALID_INDEX won't be accepted as a valid index. */ + DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size); + + if (DUK_LIKELY(uindex < vs_size)) { + return thr->valstack_bottom + uindex; + } + DUK_ERROR_API_INDEX(thr, index); + return NULL; +} + +/* Non-critical. */ +DUK_EXTERNAL duk_bool_t duk_is_valid_index(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(DUK_INVALID_INDEX < 0); + + return (duk_normalize_index(ctx, index) >= 0); +} + +/* Non-critical. */ +DUK_EXTERNAL void duk_require_valid_index(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(DUK_INVALID_INDEX < 0); + + if (duk_normalize_index(ctx, index) < 0) { + DUK_ERROR_API_INDEX(thr, index); + return; /* unreachable */ + } +} + +/* + * Value stack top handling + */ + +DUK_EXTERNAL duk_idx_t duk_get_top(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT_CTX_VALID(ctx); + + return (duk_idx_t) (thr->valstack_top - thr->valstack_bottom); +} + +/* Set stack top within currently allocated range, but don't reallocate. + * This is performance critical especially for call handling, so whenever + * changing, profile and look at generated code. + */ +DUK_EXTERNAL void duk_set_top(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_uidx_t vs_size; + duk_uidx_t vs_limit; + duk_uidx_t uindex; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(DUK_INVALID_INDEX < 0); + + DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); + DUK_ASSERT(thr->valstack_end >= thr->valstack_bottom); + vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom); + vs_limit = (duk_uidx_t) (thr->valstack_end - thr->valstack_bottom); + + if (index < 0) { + /* Negative indices are always within allocated stack but + * must not go below zero index. + */ + uindex = vs_size + (duk_uidx_t) index; + } else { + /* Positive index can be higher than valstack top but must + * not go above allocated stack (equality is OK). + */ + uindex = (duk_uidx_t) index; + } + + /* DUK_INVALID_INDEX won't be accepted as a valid index. */ + DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size); + DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_limit); + +#if defined(DUK_USE_VALSTACK_UNSAFE) + DUK_ASSERT(uindex <= vs_limit); + DUK_UNREF(vs_limit); +#else + if (DUK_UNLIKELY(uindex > vs_limit)) { + DUK_ERROR_API_INDEX(thr, index); + return; /* unreachable */ + } +#endif + DUK_ASSERT(uindex <= vs_limit); + + /* Handle change in value stack top. Respect value stack + * initialization policy: 'undefined' above top. Note that + * DECREF may cause a side effect that reallocates valstack, + * so must relookup after DECREF. + */ + + if (uindex >= vs_size) { + /* Stack size increases or stays the same. */ +#if defined(DUK_USE_ASSERTIONS) + duk_uidx_t count; + + count = uindex - vs_size; + while (count != 0) { + count--; + tv = thr->valstack_top + count; + DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv)); + } +#endif + thr->valstack_top = thr->valstack_bottom + uindex; + } else { + /* Stack size decreases. */ +#if defined(DUK_USE_REFERENCE_COUNTING) + duk_uidx_t count; + + count = vs_size - uindex; + DUK_ASSERT(count > 0); + while (count > 0) { + count--; + tv = --thr->valstack_top; /* tv -> value just before prev top value; must relookup */ + DUK_ASSERT(tv >= thr->valstack_bottom); + DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */ + } +#else /* DUK_USE_REFERENCE_COUNTING */ + duk_uidx_t count; + duk_tval *tv_end; + + count = vs_size - uindex; + tv = thr->valstack_top; + tv_end = tv - count; + DUK_ASSERT(tv > tv_end); + do { + tv--; + DUK_TVAL_SET_UNDEFINED(tv); + } while (tv != tv_end); + thr->valstack_top = tv_end; +#endif /* DUK_USE_REFERENCE_COUNTING */ + } +} + +DUK_EXTERNAL duk_idx_t duk_get_top_index(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_idx_t ret; + + DUK_ASSERT_CTX_VALID(ctx); + + ret = ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1; + if (DUK_UNLIKELY(ret < 0)) { + /* Return invalid index; if caller uses this without checking + * in another API call, the index won't map to a valid stack + * entry. + */ + return DUK_INVALID_INDEX; + } + return ret; +} + +DUK_EXTERNAL duk_idx_t duk_require_top_index(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_idx_t ret; + + DUK_ASSERT_CTX_VALID(ctx); + + ret = ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1; + if (DUK_UNLIKELY(ret < 0)) { + DUK_ERROR_API_INDEX(thr, -1); + return 0; /* unreachable */ + } + return ret; +} + +/* + * Value stack resizing. + * + * This resizing happens above the current "top": the value stack can be + * grown or shrunk, but the "top" is not affected. The value stack cannot + * be resized to a size below the current "top". + * + * The low level reallocation primitive must carefully recompute all value + * stack pointers, and must also work if ALL pointers are NULL. The resize + * is quite tricky because the valstack realloc may cause a mark-and-sweep, + * which may run finalizers. Running finalizers may resize the valstack + * recursively (the same value stack we're working on). So, after realloc + * returns, we know that the valstack "top" should still be the same (there + * should not be live values above the "top"), but its underlying size and + * pointer may have changed. + */ + +/* XXX: perhaps refactor this to allow caller to specify some parameters, or + * at least a 'compact' flag which skips any spare or round-up .. useful for + * emergency gc. + */ + +DUK_LOCAL duk_bool_t duk__resize_valstack(duk_context *ctx, duk_size_t new_size) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_ptrdiff_t old_bottom_offset; + duk_ptrdiff_t old_top_offset; + duk_ptrdiff_t old_end_offset_post; +#ifdef DUK_USE_DEBUG + duk_ptrdiff_t old_end_offset_pre; + duk_tval *old_valstack_pre; + duk_tval *old_valstack_post; +#endif + duk_tval *new_valstack; + duk_size_t new_alloc_size; + duk_tval *p; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + DUK_ASSERT(thr->valstack_bottom >= thr->valstack); + DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); + DUK_ASSERT(thr->valstack_end >= thr->valstack_top); + DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack) <= new_size); /* can't resize below 'top' */ + DUK_ASSERT(new_size <= thr->valstack_max); /* valstack limit caller has check, prevents wrapping */ + DUK_ASSERT(new_size <= DUK_SIZE_MAX / sizeof(duk_tval)); /* specific assert for wrapping */ + + /* get pointer offsets for tweaking below */ + old_bottom_offset = (((duk_uint8_t *) thr->valstack_bottom) - ((duk_uint8_t *) thr->valstack)); + old_top_offset = (((duk_uint8_t *) thr->valstack_top) - ((duk_uint8_t *) thr->valstack)); +#ifdef DUK_USE_DEBUG + old_end_offset_pre = (((duk_uint8_t *) thr->valstack_end) - ((duk_uint8_t *) thr->valstack)); /* not very useful, used for debugging */ + old_valstack_pre = thr->valstack; +#endif + + /* Allocate a new valstack. + * + * Note: cannot use a plain DUK_REALLOC() because a mark-and-sweep may + * invalidate the original thr->valstack base pointer inside the realloc + * process. See doc/memory-management.rst. + */ + + new_alloc_size = sizeof(duk_tval) * new_size; + new_valstack = (duk_tval *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_valstack_ptr, (void *) thr, new_alloc_size); + if (!new_valstack) { + /* Because new_size != 0, if condition doesn't need to be + * (new_valstack != NULL || new_size == 0). + */ + DUK_ASSERT(new_size != 0); + DUK_D(DUK_DPRINT("failed to resize valstack to %lu entries (%lu bytes)", + (unsigned long) new_size, (unsigned long) new_alloc_size)); + return 0; + } + + /* Note: the realloc may have triggered a mark-and-sweep which may + * have resized our valstack internally. However, the mark-and-sweep + * MUST NOT leave the stack bottom/top in a different state. Particular + * assumptions and facts: + * + * - The thr->valstack pointer may be different after realloc, + * and the offset between thr->valstack_end <-> thr->valstack + * may have changed. + * - The offset between thr->valstack_bottom <-> thr->valstack + * and thr->valstack_top <-> thr->valstack MUST NOT have changed, + * because mark-and-sweep must adhere to a strict stack policy. + * In other words, logical bottom and top MUST NOT have changed. + * - All values above the top are unreachable but are initialized + * to UNDEFINED, up to the post-realloc valstack_end. + * - 'old_end_offset' must be computed after realloc to be correct. + */ + + DUK_ASSERT((((duk_uint8_t *) thr->valstack_bottom) - ((duk_uint8_t *) thr->valstack)) == old_bottom_offset); + DUK_ASSERT((((duk_uint8_t *) thr->valstack_top) - ((duk_uint8_t *) thr->valstack)) == old_top_offset); + + /* success, fixup pointers */ + old_end_offset_post = (((duk_uint8_t *) thr->valstack_end) - ((duk_uint8_t *) thr->valstack)); /* must be computed after realloc */ +#ifdef DUK_USE_DEBUG + old_valstack_post = thr->valstack; +#endif + thr->valstack = new_valstack; + thr->valstack_end = new_valstack + new_size; +#if !defined(DUK_USE_PREFER_SIZE) + thr->valstack_size = new_size; +#endif + thr->valstack_bottom = (duk_tval *) (void *) ((duk_uint8_t *) new_valstack + old_bottom_offset); + thr->valstack_top = (duk_tval *) (void *) ((duk_uint8_t *) new_valstack + old_top_offset); + + DUK_ASSERT(thr->valstack_bottom >= thr->valstack); + DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); + DUK_ASSERT(thr->valstack_end >= thr->valstack_top); + + /* useful for debugging */ +#ifdef DUK_USE_DEBUG + if (old_end_offset_pre != old_end_offset_post) { + DUK_D(DUK_DPRINT("valstack was resized during valstack_resize(), probably by mark-and-sweep; " + "end offset changed: %lu -> %lu", + (unsigned long) old_end_offset_pre, + (unsigned long) old_end_offset_post)); + } + if (old_valstack_pre != old_valstack_post) { + DUK_D(DUK_DPRINT("valstack pointer changed during valstack_resize(), probably by mark-and-sweep: %p -> %p", + (void *) old_valstack_pre, + (void *) old_valstack_post)); + } +#endif + + DUK_DD(DUK_DDPRINT("resized valstack to %lu elements (%lu bytes), bottom=%ld, top=%ld, " + "new pointers: start=%p end=%p bottom=%p top=%p", + (unsigned long) new_size, (unsigned long) new_alloc_size, + (long) (thr->valstack_bottom - thr->valstack), + (long) (thr->valstack_top - thr->valstack), + (void *) thr->valstack, (void *) thr->valstack_end, + (void *) thr->valstack_bottom, (void *) thr->valstack_top)); + + /* Init newly allocated slots (only). */ + p = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + old_end_offset_post); + while (p < thr->valstack_end) { + /* Never executed if new size is smaller. */ + DUK_TVAL_SET_UNDEFINED(p); + p++; + } + + /* Assert for value stack initialization policy. */ +#if defined(DUK_USE_ASSERTIONS) + p = thr->valstack_top; + while (p < thr->valstack_end) { + DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(p)); + p++; + } +#endif + + return 1; +} + +DUK_INTERNAL +duk_bool_t duk_valstack_resize_raw(duk_context *ctx, + duk_size_t min_new_size, + duk_small_uint_t flags) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_size_t old_size; + duk_size_t new_size; + duk_bool_t is_shrink = 0; + duk_small_uint_t shrink_flag = (flags & DUK_VSRESIZE_FLAG_SHRINK); + duk_small_uint_t compact_flag = (flags & DUK_VSRESIZE_FLAG_COMPACT); + duk_small_uint_t throw_flag = (flags & DUK_VSRESIZE_FLAG_THROW); + + DUK_DDD(DUK_DDDPRINT("check valstack resize: min_new_size=%lu, curr_size=%ld, curr_top=%ld, " + "curr_bottom=%ld, shrink=%d, compact=%d, throw=%d", + (unsigned long) min_new_size, + (long) (thr->valstack_end - thr->valstack), + (long) (thr->valstack_top - thr->valstack), + (long) (thr->valstack_bottom - thr->valstack), + (int) shrink_flag, (int) compact_flag, (int) throw_flag)); + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + DUK_ASSERT(thr->valstack_bottom >= thr->valstack); + DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); + DUK_ASSERT(thr->valstack_end >= thr->valstack_top); + +#if defined(DUK_USE_PREFER_SIZE) + old_size = (duk_size_t) (thr->valstack_end - thr->valstack); +#else + DUK_ASSERT((duk_size_t) (thr->valstack_end - thr->valstack) == thr->valstack_size); + old_size = thr->valstack_size; +#endif + + if (min_new_size <= old_size) { + is_shrink = 1; + if (!shrink_flag || + old_size - min_new_size < DUK_VALSTACK_SHRINK_THRESHOLD) { + DUK_DDD(DUK_DDDPRINT("no need to grow or shrink valstack")); + return 1; + } + } + + new_size = min_new_size; + if (!compact_flag) { + if (is_shrink) { + /* shrink case; leave some spare */ + new_size += DUK_VALSTACK_SHRINK_SPARE; + } + + /* round up roughly to next 'grow step' */ + new_size = (new_size / DUK_VALSTACK_GROW_STEP + 1) * DUK_VALSTACK_GROW_STEP; + } + + DUK_DD(DUK_DDPRINT("want to %s valstack: %lu -> %lu elements (min_new_size %lu)", + (const char *) (new_size > old_size ? "grow" : "shrink"), + (unsigned long) old_size, (unsigned long) new_size, + (unsigned long) min_new_size)); + + if (new_size > thr->valstack_max) { + /* Note: may be triggered even if minimal new_size would not reach the limit, + * plan limit accordingly (taking DUK_VALSTACK_GROW_STEP into account). + */ + if (throw_flag) { + DUK_ERROR_RANGE(thr, DUK_STR_VALSTACK_LIMIT); + } else { + return 0; + } + } + + /* + * When resizing the valstack, a mark-and-sweep may be triggered for + * the allocation of the new valstack. If the mark-and-sweep needs + * to use our thread for something, it may cause *the same valstack* + * to be resized recursively. This happens e.g. when mark-and-sweep + * finalizers are called. This is taken into account carefully in + * duk__resize_valstack(). + * + * 'new_size' is known to be <= valstack_max, which ensures that + * size_t and pointer arithmetic won't wrap in duk__resize_valstack(). + */ + + if (!duk__resize_valstack(ctx, new_size)) { + if (is_shrink) { + DUK_DD(DUK_DDPRINT("valstack resize failed, but is a shrink, ignore")); + return 1; + } + + DUK_DD(DUK_DDPRINT("valstack resize failed")); + + if (throw_flag) { + DUK_ERROR_ALLOC_DEFMSG(thr); + } else { + return 0; + } + } + + DUK_DDD(DUK_DDDPRINT("valstack resize successful")); + return 1; +} + +DUK_EXTERNAL duk_bool_t duk_check_stack(duk_context *ctx, duk_idx_t extra) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_size_t min_new_size; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + + if (DUK_UNLIKELY(extra < 0)) { + /* Clamping to zero makes the API more robust to calling code + * calculation errors. + */ + extra = 0; + } + + min_new_size = (thr->valstack_top - thr->valstack) + extra + DUK_VALSTACK_INTERNAL_EXTRA; + return duk_valstack_resize_raw(ctx, + min_new_size, /* min_new_size */ + 0 /* no shrink */ | /* flags */ + 0 /* no compact */ | + 0 /* no throw */); +} + +DUK_EXTERNAL void duk_require_stack(duk_context *ctx, duk_idx_t extra) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_size_t min_new_size; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + + if (DUK_UNLIKELY(extra < 0)) { + /* Clamping to zero makes the API more robust to calling code + * calculation errors. + */ + extra = 0; + } + + min_new_size = (thr->valstack_top - thr->valstack) + extra + DUK_VALSTACK_INTERNAL_EXTRA; + (void) duk_valstack_resize_raw(ctx, + min_new_size, /* min_new_size */ + 0 /* no shrink */ | /* flags */ + 0 /* no compact */ | + DUK_VSRESIZE_FLAG_THROW); +} + +DUK_EXTERNAL duk_bool_t duk_check_stack_top(duk_context *ctx, duk_idx_t top) { + duk_size_t min_new_size; + + DUK_ASSERT_CTX_VALID(ctx); + + if (DUK_UNLIKELY(top < 0)) { + /* Clamping to zero makes the API more robust to calling code + * calculation errors. + */ + top = 0; + } + + min_new_size = top + DUK_VALSTACK_INTERNAL_EXTRA; + return duk_valstack_resize_raw(ctx, + min_new_size, /* min_new_size */ + 0 /* no shrink */ | /* flags */ + 0 /* no compact */ | + 0 /* no throw */); +} + +DUK_EXTERNAL void duk_require_stack_top(duk_context *ctx, duk_idx_t top) { + duk_size_t min_new_size; + + DUK_ASSERT_CTX_VALID(ctx); + + if (DUK_UNLIKELY(top < 0)) { + /* Clamping to zero makes the API more robust to calling code + * calculation errors. + */ + top = 0; + } + + min_new_size = top + DUK_VALSTACK_INTERNAL_EXTRA; + (void) duk_valstack_resize_raw(ctx, + min_new_size, /* min_new_size */ + 0 /* no shrink */ | /* flags */ + 0 /* no compact */ | + DUK_VSRESIZE_FLAG_THROW); +} + +/* + * Basic stack manipulation: swap, dup, insert, replace, etc + */ + +DUK_EXTERNAL void duk_swap(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) { + duk_tval *tv1; + duk_tval *tv2; + duk_tval tv_tmp; + + DUK_ASSERT_CTX_VALID(ctx); + + tv1 = duk_require_tval(ctx, index1); + DUK_ASSERT(tv1 != NULL); + tv2 = duk_require_tval(ctx, index2); + DUK_ASSERT(tv2 != NULL); + + /* If tv1==tv2 this is a NOP, no check is needed */ + DUK_TVAL_SET_TVAL(&tv_tmp, tv1); + DUK_TVAL_SET_TVAL(tv1, tv2); + DUK_TVAL_SET_TVAL(tv2, &tv_tmp); +} + +DUK_EXTERNAL void duk_swap_top(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + + duk_swap(ctx, index, -1); +} + +DUK_EXTERNAL void duk_dup(duk_context *ctx, duk_idx_t from_index) { + duk_hthread *thr; + duk_tval *tv_from; + duk_tval *tv_to; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK__CHECK_SPACE(); + + tv_from = duk_require_tval(ctx, from_index); + tv_to = thr->valstack_top++; + DUK_ASSERT(tv_from != NULL); + DUK_ASSERT(tv_to != NULL); + DUK_TVAL_SET_TVAL(tv_to, tv_from); + DUK_TVAL_INCREF(thr, tv_to); /* no side effects */ +} + +DUK_EXTERNAL void duk_dup_top(duk_context *ctx) { + duk_hthread *thr; + duk_tval *tv_from; + duk_tval *tv_to; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK__CHECK_SPACE(); + + if (thr->valstack_top - thr->valstack_bottom <= 0) { + DUK_ERROR_API_INDEX(thr, -1); + return; /* unreachable */ + } + tv_from = thr->valstack_top - 1; + tv_to = thr->valstack_top++; + DUK_ASSERT(tv_from != NULL); + DUK_ASSERT(tv_to != NULL); + DUK_TVAL_SET_TVAL(tv_to, tv_from); + DUK_TVAL_INCREF(thr, tv_to); /* no side effects */ +} + +DUK_EXTERNAL void duk_insert(duk_context *ctx, duk_idx_t to_index) { + duk_tval *p; + duk_tval *q; + duk_tval tv_tmp; + duk_size_t nbytes; + + DUK_ASSERT_CTX_VALID(ctx); + + p = duk_require_tval(ctx, to_index); + DUK_ASSERT(p != NULL); + q = duk_require_tval(ctx, -1); + DUK_ASSERT(q != NULL); + + DUK_ASSERT(q >= p); + + /* nbytes + * <---------> + * [ ... | p | x | x | q ] + * => [ ... | q | p | x | x ] + */ + + nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */ + + DUK_DDD(DUK_DDDPRINT("duk_insert: to_index=%ld, p=%p, q=%p, nbytes=%lu", + (long) to_index, (void *) p, (void *) q, (unsigned long) nbytes)); + + /* No net refcount changes. */ + + if (nbytes > 0) { + DUK_TVAL_SET_TVAL(&tv_tmp, q); + DUK_ASSERT(nbytes > 0); + DUK_MEMMOVE((void *) (p + 1), (const void *) p, (size_t) nbytes); + DUK_TVAL_SET_TVAL(p, &tv_tmp); + } else { + /* nop: insert top to top */ + DUK_ASSERT(nbytes == 0); + DUK_ASSERT(p == q); + } +} + +DUK_EXTERNAL void duk_replace(duk_context *ctx, duk_idx_t to_index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv1; + duk_tval *tv2; + duk_tval tv_tmp; + + DUK_ASSERT_CTX_VALID(ctx); + + tv1 = duk_require_tval(ctx, -1); + DUK_ASSERT(tv1 != NULL); + tv2 = duk_require_tval(ctx, to_index); + DUK_ASSERT(tv2 != NULL); + + /* For tv1 == tv2, both pointing to stack top, the end result + * is same as duk_pop(ctx). + */ + DUK_TVAL_SET_TVAL(&tv_tmp, tv2); + DUK_TVAL_SET_TVAL(tv2, tv1); + DUK_TVAL_SET_UNDEFINED(tv1); + thr->valstack_top--; + DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ +} + +DUK_EXTERNAL void duk_copy(duk_context *ctx, duk_idx_t from_index, duk_idx_t to_index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv1; + duk_tval *tv2; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_UNREF(thr); /* w/o refcounting */ + + tv1 = duk_require_tval(ctx, from_index); + DUK_ASSERT(tv1 != NULL); + tv2 = duk_require_tval(ctx, to_index); + DUK_ASSERT(tv2 != NULL); + + /* For tv1 == tv2, this is a no-op (no explicit check needed). */ + DUK_TVAL_SET_TVAL_UPDREF(thr, tv2, tv1); /* side effects */ +} + +DUK_EXTERNAL void duk_remove(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *p; + duk_tval *q; +#ifdef DUK_USE_REFERENCE_COUNTING + duk_tval tv_tmp; +#endif + duk_size_t nbytes; + + DUK_ASSERT_CTX_VALID(ctx); + + p = duk_require_tval(ctx, index); + DUK_ASSERT(p != NULL); + q = duk_require_tval(ctx, -1); + DUK_ASSERT(q != NULL); + + DUK_ASSERT(q >= p); + + /* nbytes zero size case + * <---------> + * [ ... | p | x | x | q ] [ ... | p==q ] + * => [ ... | x | x | q ] [ ... ] + */ + +#ifdef DUK_USE_REFERENCE_COUNTING + /* use a temp: decref only when valstack reachable values are correct */ + DUK_TVAL_SET_TVAL(&tv_tmp, p); +#endif + + nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */ + DUK_MEMMOVE((void *) p, (const void *) (p + 1), (size_t) nbytes); /* zero size not an issue: pointers are valid */ + + DUK_TVAL_SET_UNDEFINED(q); + thr->valstack_top--; + +#ifdef DUK_USE_REFERENCE_COUNTING + DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ +#endif +} + +/* + * Stack slice primitives + */ + +DUK_EXTERNAL void duk_xcopymove_raw(duk_context *to_ctx, duk_context *from_ctx, duk_idx_t count, duk_bool_t is_copy) { + duk_hthread *to_thr = (duk_hthread *) to_ctx; + duk_hthread *from_thr = (duk_hthread *) from_ctx; + void *src; + duk_size_t nbytes; + duk_tval *p; + duk_tval *q; + + /* XXX: several pointer comparison issues here */ + + DUK_ASSERT_CTX_VALID(to_ctx); + DUK_ASSERT_CTX_VALID(from_ctx); + DUK_ASSERT(to_ctx != NULL); + DUK_ASSERT(from_ctx != NULL); + + if (to_ctx == from_ctx) { + DUK_ERROR_API(to_thr, DUK_STR_INVALID_CONTEXT); + return; + } + if ((count < 0) || + (count > (duk_idx_t) to_thr->valstack_max)) { + /* Maximum value check ensures 'nbytes' won't wrap below. */ + DUK_ERROR_API(to_thr, DUK_STR_INVALID_COUNT); + return; + } + + nbytes = sizeof(duk_tval) * count; + if (nbytes == 0) { + return; + } + DUK_ASSERT(to_thr->valstack_top <= to_thr->valstack_end); + if ((duk_size_t) ((duk_uint8_t *) to_thr->valstack_end - (duk_uint8_t *) to_thr->valstack_top) < nbytes) { + DUK_ERROR_API(to_thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); + } + src = (void *) ((duk_uint8_t *) from_thr->valstack_top - nbytes); + if (src < (void *) from_thr->valstack_bottom) { + DUK_ERROR_API(to_thr, DUK_STR_INVALID_COUNT); + } + + /* copy values (no overlap even if to_ctx == from_ctx; that's not + * allowed now anyway) + */ + DUK_ASSERT(nbytes > 0); + DUK_MEMCPY((void *) to_thr->valstack_top, (const void *) src, (size_t) nbytes); + + p = to_thr->valstack_top; + to_thr->valstack_top = (duk_tval *) (void *) (((duk_uint8_t *) p) + nbytes); + + if (is_copy) { + /* Incref copies, keep originals. */ + q = to_thr->valstack_top; + while (p < q) { + DUK_TVAL_INCREF(to_thr, p); /* no side effects */ + p++; + } + } else { + /* No net refcount change. */ + p = from_thr->valstack_top; + q = (duk_tval *) (void *) (((duk_uint8_t *) p) - nbytes); + from_thr->valstack_top = q; + + while (p > q) { + p--; + DUK_TVAL_SET_UNDEFINED(p); + /* XXX: fast primitive to set a bunch of values to UNDEFINED */ + } + } +} + +/* + * Get/require + */ + +DUK_EXTERNAL void duk_require_undefined(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_UNDEFINED(tv)) { + return; + } + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "undefined", DUK_STR_NOT_UNDEFINED); + return; /* not reachable */ +} + +DUK_EXTERNAL void duk_require_null(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_NULL(tv)) { + return; + } + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "null", DUK_STR_NOT_NULL); + return; /* not reachable */ +} + +DUK_EXTERNAL duk_bool_t duk_get_boolean(duk_context *ctx, duk_idx_t index) { + duk_bool_t ret = 0; /* default: false */ + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_BOOLEAN(tv)) { + ret = DUK_TVAL_GET_BOOLEAN(tv); + } + + DUK_ASSERT(ret == 0 || ret == 1); + return ret; +} + +DUK_EXTERNAL duk_bool_t duk_require_boolean(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_BOOLEAN(tv)) { + duk_bool_t ret = DUK_TVAL_GET_BOOLEAN(tv); + DUK_ASSERT(ret == 0 || ret == 1); + return ret; + } + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "boolean", DUK_STR_NOT_BOOLEAN); + return 0; /* not reachable */ +} + +DUK_EXTERNAL duk_double_t duk_get_number(duk_context *ctx, duk_idx_t index) { + duk_double_union ret; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + ret.d = DUK_DOUBLE_NAN; /* default: NaN */ + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_NUMBER(tv)) { + ret.d = DUK_TVAL_GET_NUMBER(tv); + } + + /* + * Number should already be in NaN-normalized form, but let's + * normalize anyway. + */ + + DUK_DBLUNION_NORMALIZE_NAN_CHECK(&ret); + return ret.d; +} + +DUK_EXTERNAL duk_double_t duk_require_number(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_NUMBER(tv)) { + duk_double_union ret; + ret.d = DUK_TVAL_GET_NUMBER(tv); + + /* + * Number should already be in NaN-normalized form, + * but let's normalize anyway. + */ + + DUK_DBLUNION_NORMALIZE_NAN_CHECK(&ret); + return ret.d; + } + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "number", DUK_STR_NOT_NUMBER); + return DUK_DOUBLE_NAN; /* not reachable */ +} + +DUK_EXTERNAL duk_int_t duk_get_int(duk_context *ctx, duk_idx_t index) { + /* Custom coercion for API */ + DUK_ASSERT_CTX_VALID(ctx); + return (duk_int_t) duk__api_coerce_d2i(ctx, index, 0 /*require*/); +} + +DUK_EXTERNAL duk_uint_t duk_get_uint(duk_context *ctx, duk_idx_t index) { + /* Custom coercion for API */ + DUK_ASSERT_CTX_VALID(ctx); + return (duk_uint_t) duk__api_coerce_d2ui(ctx, index, 0 /*require*/); +} + +DUK_EXTERNAL duk_int_t duk_require_int(duk_context *ctx, duk_idx_t index) { + /* Custom coercion for API */ + DUK_ASSERT_CTX_VALID(ctx); + return (duk_int_t) duk__api_coerce_d2i(ctx, index, 1 /*require*/); +} + +DUK_EXTERNAL duk_uint_t duk_require_uint(duk_context *ctx, duk_idx_t index) { + /* Custom coercion for API */ + DUK_ASSERT_CTX_VALID(ctx); + return (duk_uint_t) duk__api_coerce_d2ui(ctx, index, 1 /*require*/); +} + +DUK_EXTERNAL const char *duk_get_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) { + const char *ret; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + /* default: NULL, length 0 */ + ret = NULL; + if (out_len) { + *out_len = 0; + } + + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_STRING(tv)) { + /* Here we rely on duk_hstring instances always being zero + * terminated even if the actual string is not. + */ + duk_hstring *h = DUK_TVAL_GET_STRING(tv); + DUK_ASSERT(h != NULL); + ret = (const char *) DUK_HSTRING_GET_DATA(h); + if (out_len) { + *out_len = DUK_HSTRING_GET_BYTELEN(h); + } + } + + return ret; +} + +DUK_EXTERNAL const char *duk_require_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) { + duk_hthread *thr = (duk_hthread *) ctx; + const char *ret; + + DUK_ASSERT_CTX_VALID(ctx); + + /* Note: this check relies on the fact that even a zero-size string + * has a non-NULL pointer. + */ + ret = duk_get_lstring(ctx, index, out_len); + if (ret) { + return ret; + } + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "string", DUK_STR_NOT_STRING); + return NULL; /* not reachable */ +} + +DUK_EXTERNAL const char *duk_get_string(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + + return duk_get_lstring(ctx, index, NULL); +} + +DUK_EXTERNAL const char *duk_require_string(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + + return duk_require_lstring(ctx, index, NULL); +} + +DUK_EXTERNAL void *duk_get_pointer(duk_context *ctx, duk_idx_t index) { + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_POINTER(tv)) { + void *p = DUK_TVAL_GET_POINTER(tv); /* may be NULL */ + return (void *) p; + } + + return NULL; +} + +DUK_EXTERNAL void *duk_require_pointer(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + /* Note: here we must be wary of the fact that a pointer may be + * valid and be a NULL. + */ + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_POINTER(tv)) { + void *p = DUK_TVAL_GET_POINTER(tv); /* may be NULL */ + return (void *) p; + } + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "pointer", DUK_STR_NOT_POINTER); + return NULL; /* not reachable */ +} + +#if 0 /*unused*/ +DUK_INTERNAL void *duk_get_voidptr(duk_context *ctx, duk_idx_t index) { + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_HEAP_ALLOCATED(tv)) { + duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv); + DUK_ASSERT(h != NULL); + return (void *) h; + } + + return NULL; +} +#endif + +DUK_LOCAL void *duk__get_buffer_helper(duk_context *ctx, duk_idx_t index, duk_size_t *out_size, duk_bool_t throw_flag) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_UNREF(thr); + + if (out_size != NULL) { + *out_size = 0; + } + + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_BUFFER(tv)) { + duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); + DUK_ASSERT(h != NULL); + if (out_size) { + *out_size = DUK_HBUFFER_GET_SIZE(h); + } + return (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h); /* may be NULL (but only if size is 0) */ + } + + if (throw_flag) { + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "buffer", DUK_STR_NOT_BUFFER); + } + return NULL; +} + +DUK_EXTERNAL void *duk_get_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) { + return duk__get_buffer_helper(ctx, index, out_size, 0 /*throw_flag*/); +} + +DUK_EXTERNAL void *duk_require_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) { + return duk__get_buffer_helper(ctx, index, out_size, 1 /*throw_flag*/); +} + +DUK_LOCAL void *duk__get_buffer_data_helper(duk_context *ctx, duk_idx_t index, duk_size_t *out_size, duk_bool_t throw_flag) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_UNREF(thr); + + if (out_size != NULL) { + *out_size = 0; + } + + tv = duk_get_tval(ctx, index); + if (tv == NULL) { + goto fail; + } + + if (DUK_TVAL_IS_BUFFER(tv)) { + duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); + DUK_ASSERT(h != NULL); + if (out_size) { + *out_size = DUK_HBUFFER_GET_SIZE(h); + } + return (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h); /* may be NULL (but only if size is 0) */ + } else if (DUK_TVAL_IS_OBJECT(tv)) { + duk_hobject *h = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h != NULL); + if (DUK_HOBJECT_IS_BUFFEROBJECT(h)) { + /* XXX: this is probably a useful shared helper: for a + * duk_hbufferobject, get a validated buffer pointer/length. + */ + duk_hbufferobject *h_bufobj = (duk_hbufferobject *) h; + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + + if (h_bufobj->buf != NULL && + DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj)) { + duk_uint8_t *p; + + p = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf); + if (out_size != NULL) { + *out_size = (duk_size_t) h_bufobj->length; + } + return (void *) (p + h_bufobj->offset); + } + /* if slice not fully valid, treat as error */ + } + } + + fail: + if (throw_flag) { + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "buffer", DUK_STR_NOT_BUFFER); + } + return NULL; +} + +DUK_EXTERNAL void *duk_get_buffer_data(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) { + return duk__get_buffer_data_helper(ctx, index, out_size, 0 /*throw_flag*/); +} + +DUK_EXTERNAL void *duk_require_buffer_data(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) { + return duk__get_buffer_data_helper(ctx, index, out_size, 1 /*throw_flag*/); +} + +/* Raw helper for getting a value from the stack, checking its tag. + * The tag cannot be a number because numbers don't have an internal + * tag in the packed representation. + */ + +DUK_LOCAL duk_heaphdr *duk__get_tagged_heaphdr_raw(duk_context *ctx, duk_idx_t index, duk_uint_t tag) { + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (tv && (DUK_TVAL_GET_TAG(tv) == tag)) { + duk_heaphdr *ret; + ret = DUK_TVAL_GET_HEAPHDR(tv); + DUK_ASSERT(ret != NULL); /* tagged null pointers should never occur */ + return ret; + } + + return (duk_heaphdr *) NULL; +} + +DUK_INTERNAL duk_hstring *duk_get_hstring(duk_context *ctx, duk_idx_t index) { + return (duk_hstring *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_STRING); +} + +DUK_INTERNAL duk_hstring *duk_require_hstring(duk_context *ctx, duk_idx_t index) { + duk_heaphdr *h; + h = duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_STRING); + if (h == NULL) { + DUK_ERROR_REQUIRE_TYPE_INDEX(ctx, index, "string", DUK_STR_NOT_STRING); + } + return (duk_hstring *) h; +} + +DUK_INTERNAL duk_hobject *duk_get_hobject(duk_context *ctx, duk_idx_t index) { + return (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); +} + +DUK_INTERNAL duk_hobject *duk_require_hobject(duk_context *ctx, duk_idx_t index) { + duk_heaphdr *h; + h = duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); + if (h == NULL) { + DUK_ERROR_REQUIRE_TYPE_INDEX(ctx, index, "object", DUK_STR_NOT_OBJECT); + } + return (duk_hobject *) h; +} + +DUK_INTERNAL duk_hbuffer *duk_get_hbuffer(duk_context *ctx, duk_idx_t index) { + return (duk_hbuffer *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_BUFFER); +} + +DUK_INTERNAL duk_hbuffer *duk_require_hbuffer(duk_context *ctx, duk_idx_t index) { + duk_heaphdr *h; + h = duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_BUFFER); + if (h == NULL) { + DUK_ERROR_REQUIRE_TYPE_INDEX(ctx, index, "buffer", DUK_STR_NOT_BUFFER); + } + return (duk_hbuffer *) h; +} + +DUK_INTERNAL duk_hthread *duk_get_hthread(duk_context *ctx, duk_idx_t index) { + duk_hobject *h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); + if (h != NULL && !DUK_HOBJECT_IS_THREAD(h)) { + h = NULL; + } + return (duk_hthread *) h; +} + +DUK_INTERNAL duk_hthread *duk_require_hthread(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); + if (!(h != NULL && DUK_HOBJECT_IS_THREAD(h))) { + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "thread", DUK_STR_NOT_THREAD); + } + return (duk_hthread *) h; +} + +DUK_INTERNAL duk_hcompiledfunction *duk_get_hcompiledfunction(duk_context *ctx, duk_idx_t index) { + duk_hobject *h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); + if (h != NULL && !DUK_HOBJECT_IS_COMPILEDFUNCTION(h)) { + h = NULL; + } + return (duk_hcompiledfunction *) h; +} + +DUK_INTERNAL duk_hcompiledfunction *duk_require_hcompiledfunction(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); + if (!(h != NULL && DUK_HOBJECT_IS_COMPILEDFUNCTION(h))) { + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "compiledfunction", DUK_STR_NOT_COMPILEDFUNCTION); + } + return (duk_hcompiledfunction *) h; +} + +DUK_INTERNAL duk_hnativefunction *duk_get_hnativefunction(duk_context *ctx, duk_idx_t index) { + duk_hobject *h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); + if (h != NULL && !DUK_HOBJECT_IS_NATIVEFUNCTION(h)) { + h = NULL; + } + return (duk_hnativefunction *) h; +} + +DUK_INTERNAL duk_hnativefunction *duk_require_hnativefunction(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); + if (!(h != NULL && DUK_HOBJECT_IS_NATIVEFUNCTION(h))) { + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "nativefunction", DUK_STR_NOT_NATIVEFUNCTION); + } + return (duk_hnativefunction *) h; +} + +DUK_EXTERNAL duk_c_function duk_get_c_function(duk_context *ctx, duk_idx_t index) { + duk_tval *tv; + duk_hobject *h; + duk_hnativefunction *f; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (!tv) { + return NULL; + } + if (!DUK_TVAL_IS_OBJECT(tv)) { + return NULL; + } + h = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h != NULL); + + if (!DUK_HOBJECT_IS_NATIVEFUNCTION(h)) { + return NULL; + } + DUK_ASSERT(DUK_HOBJECT_HAS_NATIVEFUNCTION(h)); + f = (duk_hnativefunction *) h; + + return f->func; +} + +DUK_EXTERNAL duk_c_function duk_require_c_function(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_c_function ret; + + DUK_ASSERT_CTX_VALID(ctx); + + ret = duk_get_c_function(ctx, index); + if (!ret) { + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "nativefunction", DUK_STR_NOT_NATIVEFUNCTION); + } + return ret; +} + +DUK_EXTERNAL void duk_require_function(duk_context *ctx, duk_idx_t index) { + if (!duk_is_function(ctx, index)) { + DUK_ERROR_REQUIRE_TYPE_INDEX((duk_hthread *) ctx, index, "function", DUK_STR_NOT_FUNCTION); + } +} + +DUK_EXTERNAL duk_context *duk_get_context(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + + return (duk_context *) duk_get_hthread(ctx, index); +} + +DUK_EXTERNAL duk_context *duk_require_context(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + + return (duk_context *) duk_require_hthread(ctx, index); +} + +DUK_EXTERNAL void *duk_get_heapptr(duk_context *ctx, duk_idx_t index) { + duk_tval *tv; + void *ret; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_HEAP_ALLOCATED(tv)) { + ret = (void *) DUK_TVAL_GET_HEAPHDR(tv); + DUK_ASSERT(ret != NULL); + return ret; + } + + return (void *) NULL; +} + +DUK_EXTERNAL void *duk_require_heapptr(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + void *ret; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) { + ret = (void *) DUK_TVAL_GET_HEAPHDR(tv); + DUK_ASSERT(ret != NULL); + return ret; + } + + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "heapobject", DUK_STR_UNEXPECTED_TYPE); + return (void *) NULL; /* not reachable */ +} + +#if 0 +/* This would be pointless: we'd return NULL for both lightfuncs and + * unexpected types. + */ +DUK_INTERNAL duk_hobject *duk_get_hobject_or_lfunc(duk_context *ctx, duk_idx_t index) { +} +#endif + +/* Useful for internal call sites where we either expect an object (function) + * or a lightfunc. Accepts an object (returned as is) or a lightfunc (coerced + * to an object). Return value is NULL if value is neither an object nor a + * lightfunc. + */ +DUK_INTERNAL duk_hobject *duk_get_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index) { + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + if (DUK_TVAL_IS_OBJECT(tv)) { + return DUK_TVAL_GET_OBJECT(tv); + } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) { + duk_to_object(ctx, index); + return duk_require_hobject(ctx, index); + } + + return NULL; +} + +/* Useful for internal call sites where we either expect an object (function) + * or a lightfunc. Returns NULL for a lightfunc. + */ +DUK_INTERNAL duk_hobject *duk_require_hobject_or_lfunc(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + if (DUK_TVAL_IS_OBJECT(tv)) { + return DUK_TVAL_GET_OBJECT(tv); + } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) { + return NULL; + } + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "object", DUK_STR_NOT_OBJECT); + return NULL; /* not reachable */ +} + +/* Useful for internal call sites where we either expect an object (function) + * or a lightfunc. Accepts an object (returned as is) or a lightfunc (coerced + * to an object). Return value is never NULL. + */ +DUK_INTERNAL duk_hobject *duk_require_hobject_or_lfunc_coerce(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_require_tval(ctx, index); + if (DUK_TVAL_IS_OBJECT(tv)) { + return DUK_TVAL_GET_OBJECT(tv); + } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) { + duk_to_object(ctx, index); + return duk_require_hobject(ctx, index); + } + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "object", DUK_STR_NOT_OBJECT); + return NULL; /* not reachable */ +} + +DUK_INTERNAL duk_hobject *duk_get_hobject_with_class(duk_context *ctx, duk_idx_t index, duk_small_uint_t classnum) { + duk_hobject *h; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT_DISABLE(classnum >= 0); /* unsigned */ + DUK_ASSERT(classnum <= DUK_HOBJECT_CLASS_MAX); + + h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); + if (h != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(h) != classnum) { + h = NULL; + } + return h; +} + +DUK_INTERNAL duk_hobject *duk_require_hobject_with_class(duk_context *ctx, duk_idx_t index, duk_small_uint_t classnum) { + duk_hthread *thr; + duk_hobject *h; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT_DISABLE(classnum >= 0); /* unsigned */ + DUK_ASSERT(classnum <= DUK_HOBJECT_CLASS_MAX); + thr = (duk_hthread *) ctx; + + h = (duk_hobject *) duk__get_tagged_heaphdr_raw(ctx, index, DUK_TAG_OBJECT); + if (!(h != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(h) == classnum)) { + duk_hstring *h_class; + h_class = DUK_HTHREAD_GET_STRING(thr, DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(classnum)); + DUK_UNREF(h_class); + + DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, (const char *) DUK_HSTRING_GET_DATA(h_class), DUK_STR_UNEXPECTED_TYPE); + } + return h; +} + +DUK_EXTERNAL duk_size_t duk_get_length(duk_context *ctx, duk_idx_t index) { + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (!tv) { + return 0; + } + + switch (DUK_TVAL_GET_TAG(tv)) { + case DUK_TAG_UNDEFINED: + case DUK_TAG_NULL: + case DUK_TAG_BOOLEAN: + case DUK_TAG_POINTER: + return 0; + case DUK_TAG_STRING: { + duk_hstring *h = DUK_TVAL_GET_STRING(tv); + DUK_ASSERT(h != NULL); + return (duk_size_t) DUK_HSTRING_GET_CHARLEN(h); + } + case DUK_TAG_OBJECT: { + duk_hobject *h = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h != NULL); + return (duk_size_t) duk_hobject_get_length((duk_hthread *) ctx, h); + } + case DUK_TAG_BUFFER: { + duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); + DUK_ASSERT(h != NULL); + return (duk_size_t) DUK_HBUFFER_GET_SIZE(h); + } + case DUK_TAG_LIGHTFUNC: { + duk_small_uint_t lf_flags; + lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv); + return (duk_size_t) DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags); + } +#if defined(DUK_USE_FASTINT) + case DUK_TAG_FASTINT: +#endif + default: + /* number */ + DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); + DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); + return 0; + } + + DUK_UNREACHABLE(); +} + +DUK_INTERNAL void duk_set_length(duk_context *ctx, duk_idx_t index, duk_size_t length) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *h; + + DUK_ASSERT_CTX_VALID(ctx); + + h = duk_get_hobject(ctx, index); + if (!h) { + return; + } + + duk_hobject_set_length(thr, h, (duk_uint32_t) length); /* XXX: typing */ +} + +/* + * Conversions and coercions + * + * The conversion/coercions are in-place operations on the value stack. + * Some operations are implemented here directly, while others call a + * helper in duk_js_ops.c after validating arguments. + */ + +/* E5 Section 8.12.8 */ + +DUK_LOCAL duk_bool_t duk__defaultvalue_coerce_attempt(duk_context *ctx, duk_idx_t index, duk_small_int_t func_stridx) { + if (duk_get_prop_stridx(ctx, index, func_stridx)) { + /* [ ... func ] */ + if (duk_is_callable(ctx, -1)) { + duk_dup(ctx, index); /* -> [ ... func this ] */ + duk_call_method(ctx, 0); /* -> [ ... retval ] */ + if (duk_is_primitive(ctx, -1)) { + duk_replace(ctx, index); + return 1; + } + /* [ ... retval ]; popped below */ + } + } + duk_pop(ctx); /* [ ... func/retval ] -> [ ... ] */ + return 0; +} + +DUK_EXTERNAL void duk_to_defaultvalue(duk_context *ctx, duk_idx_t index, duk_int_t hint) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *obj; + /* inline initializer for coercers[] is not allowed by old compilers like BCC */ + duk_small_int_t coercers[2]; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + + coercers[0] = DUK_STRIDX_VALUE_OF; + coercers[1] = DUK_STRIDX_TO_STRING; + + index = duk_require_normalize_index(ctx, index); + obj = duk_require_hobject_or_lfunc(ctx, index); + + if (hint == DUK_HINT_NONE) { + if (obj != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(obj) == DUK_HOBJECT_CLASS_DATE) { + hint = DUK_HINT_STRING; + } else { + hint = DUK_HINT_NUMBER; + } + } + + if (hint == DUK_HINT_STRING) { + coercers[0] = DUK_STRIDX_TO_STRING; + coercers[1] = DUK_STRIDX_VALUE_OF; + } + + if (duk__defaultvalue_coerce_attempt(ctx, index, coercers[0])) { + return; + } + + if (duk__defaultvalue_coerce_attempt(ctx, index, coercers[1])) { + return; + } + + DUK_ERROR_TYPE(thr, DUK_STR_DEFAULTVALUE_COERCE_FAILED); +} + +DUK_EXTERNAL void duk_to_undefined(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_UNREF(thr); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */ +} + +DUK_EXTERNAL void duk_to_null(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_UNREF(thr); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + DUK_TVAL_SET_NULL_UPDREF(thr, tv); /* side effects */ +} + +/* E5 Section 9.1 */ +DUK_EXTERNAL void duk_to_primitive(duk_context *ctx, duk_idx_t index, duk_int_t hint) { + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(hint == DUK_HINT_NONE || hint == DUK_HINT_NUMBER || hint == DUK_HINT_STRING); + + index = duk_require_normalize_index(ctx, index); + + if (!duk_check_type_mask(ctx, index, DUK_TYPE_MASK_OBJECT | + DUK_TYPE_MASK_LIGHTFUNC)) { + /* everything except object stay as is */ + return; + } + duk_to_defaultvalue(ctx, index, hint); +} + +/* E5 Section 9.2 */ +DUK_EXTERNAL duk_bool_t duk_to_boolean(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + duk_bool_t val; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_UNREF(thr); + + index = duk_require_normalize_index(ctx, index); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + + val = duk_js_toboolean(tv); + DUK_ASSERT(val == 0 || val == 1); + + /* Note: no need to re-lookup tv, conversion is side effect free */ + DUK_ASSERT(tv != NULL); + DUK_TVAL_SET_BOOLEAN_UPDREF(thr, tv, val); /* side effects */ + return val; +} + +DUK_EXTERNAL duk_double_t duk_to_number(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + duk_double_t d; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + /* XXX: fastint? */ + d = duk_js_tonumber(thr, tv); + + /* Note: need to re-lookup because ToNumber() may have side effects */ + tv = duk_require_tval(ctx, index); + DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, d); /* side effects */ + return d; +} + +/* XXX: combine all the integer conversions: they share everything + * but the helper function for coercion. + */ + +typedef duk_double_t (*duk__toint_coercer)(duk_hthread *thr, duk_tval *tv); + +DUK_LOCAL duk_double_t duk__to_int_uint_helper(duk_context *ctx, duk_idx_t index, duk__toint_coercer coerce_func) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + duk_double_t d; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + d = coerce_func(thr, tv); + + /* XXX: fastint? */ + + /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */ + tv = duk_require_tval(ctx, index); + DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, d); /* side effects */ + return d; +} + +DUK_EXTERNAL duk_int_t duk_to_int(duk_context *ctx, duk_idx_t index) { + /* Value coercion (in stack): ToInteger(), E5 Section 9.4 + * API return value coercion: custom + */ + DUK_ASSERT_CTX_VALID(ctx); + (void) duk__to_int_uint_helper(ctx, index, duk_js_tointeger); + return (duk_int_t) duk__api_coerce_d2i(ctx, index, 0 /*require*/); +} + +DUK_EXTERNAL duk_uint_t duk_to_uint(duk_context *ctx, duk_idx_t index) { + /* Value coercion (in stack): ToInteger(), E5 Section 9.4 + * API return value coercion: custom + */ + DUK_ASSERT_CTX_VALID(ctx); + (void) duk__to_int_uint_helper(ctx, index, duk_js_tointeger); + return (duk_uint_t) duk__api_coerce_d2ui(ctx, index, 0 /*require*/); +} + +DUK_EXTERNAL duk_int32_t duk_to_int32(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + duk_int32_t ret; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + ret = duk_js_toint32(thr, tv); + + /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */ + tv = duk_require_tval(ctx, index); + DUK_TVAL_SET_FASTINT_I32_UPDREF(thr, tv, ret); /* side effects */ + return ret; +} + +DUK_EXTERNAL duk_uint32_t duk_to_uint32(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + duk_uint32_t ret; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + ret = duk_js_touint32(thr, tv); + + /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */ + tv = duk_require_tval(ctx, index); + DUK_TVAL_SET_FASTINT_U32_UPDREF(thr, tv, ret); /* side effects */ + return ret; +} + +DUK_EXTERNAL duk_uint16_t duk_to_uint16(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + duk_uint16_t ret; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + ret = duk_js_touint16(thr, tv); + + /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */ + tv = duk_require_tval(ctx, index); + DUK_TVAL_SET_FASTINT_U32_UPDREF(thr, tv, ret); /* side effects */ + return ret; +} + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +/* Special coercion for Uint8ClampedArray. */ +DUK_INTERNAL duk_uint8_t duk_to_uint8clamped(duk_context *ctx, duk_idx_t index) { + duk_double_t d; + duk_double_t t; + duk_uint8_t ret; + + /* XXX: Simplify this algorithm, should be possible to come up with + * a shorter and faster algorithm by inspecting IEEE representation + * directly. + */ + + d = duk_to_number(ctx, index); + if (d <= 0.0) { + return 0; + } else if (d >= 255) { + return 255; + } else if (DUK_ISNAN(d)) { + /* Avoid NaN-to-integer coercion as it is compiler specific. */ + return 0; + } + + t = d - DUK_FLOOR(d); + if (t == 0.5) { + /* Exact halfway, round to even. */ + ret = (duk_uint8_t) d; + ret = (ret + 1) & 0xfe; /* Example: d=3.5, t=0.5 -> ret = (3 + 1) & 0xfe = 4 & 0xfe = 4 + * Example: d=4.5, t=0.5 -> ret = (4 + 1) & 0xfe = 5 & 0xfe = 4 + */ + } else { + /* Not halfway, round to nearest. */ + ret = (duk_uint8_t) (d + 0.5); + } + return ret; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +DUK_EXTERNAL const char *duk_to_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) { + DUK_ASSERT_CTX_VALID(ctx); + + (void) duk_to_string(ctx, index); + return duk_require_lstring(ctx, index, out_len); +} + +DUK_LOCAL duk_ret_t duk__safe_to_string_raw(duk_context *ctx) { + DUK_ASSERT_CTX_VALID(ctx); + + duk_to_string(ctx, -1); + return 1; +} + +DUK_EXTERNAL const char *duk_safe_to_lstring(duk_context *ctx, duk_idx_t index, duk_size_t *out_len) { + DUK_ASSERT_CTX_VALID(ctx); + + index = duk_require_normalize_index(ctx, index); + + /* We intentionally ignore the duk_safe_call() return value and only + * check the output type. This way we don't also need to check that + * the returned value is indeed a string in the success case. + */ + + duk_dup(ctx, index); + (void) duk_safe_call(ctx, duk__safe_to_string_raw, 1 /*nargs*/, 1 /*nrets*/); + if (!duk_is_string(ctx, -1)) { + /* Error: try coercing error to string once. */ + (void) duk_safe_call(ctx, duk__safe_to_string_raw, 1 /*nargs*/, 1 /*nrets*/); + if (!duk_is_string(ctx, -1)) { + /* Double error */ + duk_pop(ctx); + duk_push_hstring_stridx(ctx, DUK_STRIDX_UC_ERROR); + } else { + ; + } + } else { + ; + } + DUK_ASSERT(duk_is_string(ctx, -1)); + DUK_ASSERT(duk_get_string(ctx, -1) != NULL); + + duk_replace(ctx, index); + return duk_get_lstring(ctx, index, out_len); +} + +#if defined(DUK_USE_DEBUGGER_SUPPORT) /* only needed by debugger for now */ +DUK_INTERNAL duk_hstring *duk_safe_to_hstring(duk_context *ctx, duk_idx_t index) { + (void) duk_safe_to_string(ctx, index); + DUK_ASSERT(duk_is_string(ctx, index)); + DUK_ASSERT(duk_get_hstring(ctx, index) != NULL); + return duk_get_hstring(ctx, index); +} +#endif + +/* Coerce top into Object.prototype.toString() output. */ +DUK_INTERNAL void duk_to_object_class_string_top(duk_context *ctx) { + duk_hthread *thr; + duk_uint_t typemask; + duk_hstring *h_strclass; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + typemask = duk_get_type_mask(ctx, -1); + if (typemask & DUK_TYPE_MASK_UNDEFINED) { + h_strclass = DUK_HTHREAD_STRING_UC_UNDEFINED(thr); + } else if (typemask & DUK_TYPE_MASK_NULL) { + h_strclass = DUK_HTHREAD_STRING_UC_NULL(thr); + } else { + duk_hobject *h_obj; + + duk_to_object(ctx, -1); + h_obj = duk_get_hobject(ctx, -1); + DUK_ASSERT(h_obj != NULL); + + h_strclass = DUK_HOBJECT_GET_CLASS_STRING(thr->heap, h_obj); + } + DUK_ASSERT(h_strclass != NULL); + + duk_pop(ctx); + duk_push_sprintf(ctx, "[object %s]", (const char *) DUK_HSTRING_GET_DATA(h_strclass)); +} + +#if !defined(DUK_USE_PARANOID_ERRORS) +DUK_INTERNAL void duk_push_hobject_class_string(duk_context *ctx, duk_hobject *h) { + duk_hthread *thr; + duk_hstring *h_strclass; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(h != NULL); + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + h_strclass = DUK_HOBJECT_GET_CLASS_STRING(thr->heap, h); + DUK_ASSERT(h_strclass != NULL); + duk_push_sprintf(ctx, "[object %s]", (const char *) DUK_HSTRING_GET_DATA(h_strclass)); +} +#endif /* !DUK_USE_PARANOID_ERRORS */ + +/* XXX: other variants like uint, u32 etc */ +DUK_INTERNAL duk_int_t duk_to_int_clamped_raw(duk_context *ctx, duk_idx_t index, duk_int_t minval, duk_int_t maxval, duk_bool_t *out_clamped) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + duk_tval tv_tmp; + duk_double_t d, dmin, dmax; + duk_int_t res; + duk_bool_t clamped = 0; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + d = duk_js_tointeger(thr, tv); /* E5 Section 9.4, ToInteger() */ + + dmin = (duk_double_t) minval; + dmax = (duk_double_t) maxval; + + if (d < dmin) { + clamped = 1; + res = minval; + d = dmin; + } else if (d > dmax) { + clamped = 1; + res = maxval; + d = dmax; + } else { + res = (duk_int_t) d; + } + DUK_UNREF(d); /* SCANBUILD: with suitable dmin/dmax limits 'd' is unused */ + /* 'd' and 'res' agree here */ + + /* Relookup in case duk_js_tointeger() ends up e.g. coercing an object. */ + tv = duk_get_tval(ctx, index); + DUK_ASSERT(tv != NULL); /* not popped by side effect */ + DUK_TVAL_SET_TVAL(&tv_tmp, tv); +#if defined(DUK_USE_FASTINT) +#if (DUK_INT_MAX <= 0x7fffffffL) + DUK_TVAL_SET_FASTINT_I32(tv, res); +#else + /* Clamping needed if duk_int_t is 64 bits. */ + if (res >= DUK_FASTINT_MIN && res <= DUK_FASTINT_MAX) { + DUK_TVAL_SET_FASTINT(tv, res); + } else { + DUK_TVAL_SET_NUMBER(tv, d); + } +#endif +#else + DUK_TVAL_SET_NUMBER(tv, d); /* no need to incref */ +#endif + DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ + + if (out_clamped) { + *out_clamped = clamped; + } else { + /* coerced value is updated to value stack even when RangeError thrown */ + if (clamped) { + DUK_ERROR_RANGE(thr, DUK_STR_NUMBER_OUTSIDE_RANGE); + } + } + + return res; +} + +DUK_INTERNAL duk_int_t duk_to_int_clamped(duk_context *ctx, duk_idx_t index, duk_idx_t minval, duk_idx_t maxval) { + duk_bool_t dummy; + return duk_to_int_clamped_raw(ctx, index, minval, maxval, &dummy); +} + +DUK_INTERNAL duk_int_t duk_to_int_check_range(duk_context *ctx, duk_idx_t index, duk_int_t minval, duk_int_t maxval) { + return duk_to_int_clamped_raw(ctx, index, minval, maxval, NULL); /* out_clamped==NULL -> RangeError if outside range */ +} + +DUK_EXTERNAL const char *duk_to_string(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_UNREF(thr); + + index = duk_require_normalize_index(ctx, index); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + + switch (DUK_TVAL_GET_TAG(tv)) { + case DUK_TAG_UNDEFINED: { + duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_UNDEFINED); + break; + } + case DUK_TAG_NULL: { + duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_NULL); + break; + } + case DUK_TAG_BOOLEAN: { + if (DUK_TVAL_GET_BOOLEAN(tv)) { + duk_push_hstring_stridx(ctx, DUK_STRIDX_TRUE); + } else { + duk_push_hstring_stridx(ctx, DUK_STRIDX_FALSE); + } + break; + } + case DUK_TAG_STRING: { + /* nop */ + goto skip_replace; + } + case DUK_TAG_OBJECT: { + duk_to_primitive(ctx, index, DUK_HINT_STRING); + return duk_to_string(ctx, index); /* Note: recursive call */ + } + case DUK_TAG_BUFFER: { + duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); + + /* Note: this allows creation of internal strings. */ + + DUK_ASSERT(h != NULL); + duk_push_lstring(ctx, + (const char *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h), + (duk_size_t) DUK_HBUFFER_GET_SIZE(h)); + break; + } + case DUK_TAG_POINTER: { + void *ptr = DUK_TVAL_GET_POINTER(tv); + if (ptr != NULL) { + duk_push_sprintf(ctx, DUK_STR_FMT_PTR, (void *) ptr); + } else { + /* Represent a null pointer as 'null' to be consistent with + * the JX format variant. Native '%p' format for a NULL + * pointer may be e.g. '(nil)'. + */ + duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_NULL); + } + break; + } + case DUK_TAG_LIGHTFUNC: { + /* Should match Function.prototype.toString() */ + duk_push_lightfunc_tostring(ctx, tv); + break; + } +#if defined(DUK_USE_FASTINT) + case DUK_TAG_FASTINT: +#endif + default: { + /* number */ + DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); + DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); + duk_push_tval(ctx, tv); + duk_numconv_stringify(ctx, + 10 /*radix*/, + 0 /*precision:shortest*/, + 0 /*force_exponential*/); + break; + } + } + + duk_replace(ctx, index); + + skip_replace: + return duk_require_string(ctx, index); +} + +DUK_INTERNAL duk_hstring *duk_to_hstring(duk_context *ctx, duk_idx_t index) { + duk_hstring *ret; + DUK_ASSERT_CTX_VALID(ctx); + duk_to_string(ctx, index); + ret = duk_get_hstring(ctx, index); + DUK_ASSERT(ret != NULL); + return ret; +} + +DUK_EXTERNAL void *duk_to_buffer_raw(duk_context *ctx, duk_idx_t index, duk_size_t *out_size, duk_uint_t mode) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hbuffer *h_buf; + const duk_uint8_t *src_data; + duk_size_t src_size; + duk_uint8_t *dst_data; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_UNREF(thr); + + index = duk_require_normalize_index(ctx, index); + + h_buf = duk_get_hbuffer(ctx, index); + if (h_buf != NULL) { + /* Buffer is kept as is, with the fixed/dynamic nature of the + * buffer only changed if requested. An external buffer + * is converted into a non-external dynamic buffer in a + * duk_to_dynamic_buffer() call. + */ + duk_uint_t tmp; + duk_uint8_t *tmp_ptr; + + tmp_ptr = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_buf); + src_data = (const duk_uint8_t *) tmp_ptr; + src_size = DUK_HBUFFER_GET_SIZE(h_buf); + + tmp = (DUK_HBUFFER_HAS_DYNAMIC(h_buf) ? DUK_BUF_MODE_DYNAMIC : DUK_BUF_MODE_FIXED); + if ((tmp == mode && !DUK_HBUFFER_HAS_EXTERNAL(h_buf)) || + mode == DUK_BUF_MODE_DONTCARE) { + /* Note: src_data may be NULL if input is a zero-size + * dynamic buffer. + */ + dst_data = tmp_ptr; + goto skip_copy; + } + } else { + /* Non-buffer value is first ToString() coerced, then converted + * to a buffer (fixed buffer is used unless a dynamic buffer is + * explicitly requested). + */ + + src_data = (const duk_uint8_t *) duk_to_lstring(ctx, index, &src_size); + } + + dst_data = (duk_uint8_t *) duk_push_buffer(ctx, src_size, (mode == DUK_BUF_MODE_DYNAMIC) /*dynamic*/); + if (DUK_LIKELY(src_size > 0)) { + /* When src_size == 0, src_data may be NULL (if source + * buffer is dynamic), and dst_data may be NULL (if + * target buffer is dynamic). Avoid zero-size memcpy() + * with an invalid pointer. + */ + DUK_MEMCPY((void *) dst_data, (const void *) src_data, (size_t) src_size); + } + duk_replace(ctx, index); + skip_copy: + + if (out_size) { + *out_size = src_size; + } + return dst_data; +} + +DUK_EXTERNAL void *duk_to_pointer(duk_context *ctx, duk_idx_t index) { + duk_tval *tv; + void *res; + + DUK_ASSERT_CTX_VALID(ctx); + + index = duk_require_normalize_index(ctx, index); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + + switch (DUK_TVAL_GET_TAG(tv)) { + case DUK_TAG_UNDEFINED: + case DUK_TAG_NULL: + case DUK_TAG_BOOLEAN: + res = NULL; + break; + case DUK_TAG_POINTER: + res = DUK_TVAL_GET_POINTER(tv); + break; + case DUK_TAG_STRING: + case DUK_TAG_OBJECT: + case DUK_TAG_BUFFER: + /* Heap allocated: return heap pointer which is NOT useful + * for the caller, except for debugging. + */ + res = (void *) DUK_TVAL_GET_HEAPHDR(tv); + break; + case DUK_TAG_LIGHTFUNC: + /* Function pointers do not always cast correctly to void * + * (depends on memory and segmentation model for instance), + * so they coerce to NULL. + */ + res = NULL; + break; +#if defined(DUK_USE_FASTINT) + case DUK_TAG_FASTINT: +#endif + default: + /* number */ + DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); + DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); + res = NULL; + break; + } + + duk_push_pointer(ctx, res); + duk_replace(ctx, index); + return res; +} + +DUK_EXTERNAL void duk_to_object(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + duk_uint_t flags = 0; /* shared flags for a subset of types */ + duk_small_int_t proto = 0; + + DUK_ASSERT_CTX_VALID(ctx); + + index = duk_require_normalize_index(ctx, index); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + + switch (DUK_TVAL_GET_TAG(tv)) { + case DUK_TAG_UNDEFINED: + case DUK_TAG_NULL: { + DUK_ERROR_TYPE(thr, DUK_STR_NOT_OBJECT_COERCIBLE); + break; + } + case DUK_TAG_BOOLEAN: { + flags = DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BOOLEAN); + proto = DUK_BIDX_BOOLEAN_PROTOTYPE; + goto create_object; + } + case DUK_TAG_STRING: { + flags = DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_STRING); + proto = DUK_BIDX_STRING_PROTOTYPE; + goto create_object; + } + case DUK_TAG_OBJECT: { + /* nop */ + break; + } + case DUK_TAG_BUFFER: { + /* A plain buffer coerces to a Duktape.Buffer because it's the + * object counterpart of the plain buffer value. But it might + * still make more sense to produce an ArrayBuffer here? + */ + + duk_hbufferobject *h_bufobj; + duk_hbuffer *h_val; + + h_val = DUK_TVAL_GET_BUFFER(tv); + DUK_ASSERT(h_val != NULL); + + h_bufobj = duk_push_bufferobject_raw(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_BUFFEROBJECT | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER), + DUK_BIDX_BUFFER_PROTOTYPE); + DUK_ASSERT(h_bufobj != NULL); + DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE((duk_hobject *) h_bufobj)); + DUK_ASSERT(DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_bufobj)); + + h_bufobj->buf = h_val; + DUK_HBUFFER_INCREF(thr, h_val); + DUK_ASSERT(h_bufobj->offset == 0); + h_bufobj->length = (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_val); + DUK_ASSERT(h_bufobj->shift == 0); + DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8); + + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + goto replace_value; + } + case DUK_TAG_POINTER: { + flags = DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_POINTER); + proto = DUK_BIDX_POINTER_PROTOTYPE; + goto create_object; + } + case DUK_TAG_LIGHTFUNC: { + /* Lightfunc coerces to a Function instance with concrete + * properties. Since 'length' is virtual for Duktape/C + * functions, don't need to define that. + * + * The result is made extensible to mimic what happens to + * strings: + * > Object.isExtensible(Object('foo')) + * true + */ + duk_small_uint_t lf_flags; + duk_idx_t nargs; + duk_small_uint_t lf_len; + duk_c_function func; + duk_hnativefunction *nf; + + DUK_TVAL_GET_LIGHTFUNC(tv, func, lf_flags); + + nargs = (duk_idx_t) DUK_LFUNC_FLAGS_GET_NARGS(lf_flags); + if (nargs == DUK_LFUNC_NARGS_VARARGS) { + nargs = (duk_idx_t) DUK_VARARGS; + } + flags = DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_CONSTRUCTABLE | + DUK_HOBJECT_FLAG_NATIVEFUNCTION | + DUK_HOBJECT_FLAG_NEWENV | + DUK_HOBJECT_FLAG_STRICT | + DUK_HOBJECT_FLAG_NOTAIL | + /* DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC: omitted here intentionally */ + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION); + (void) duk__push_c_function_raw(ctx, func, nargs, flags); + + lf_len = DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags); + if ((duk_idx_t) lf_len != nargs) { + /* Explicit length is only needed if it differs from 'nargs'. */ + duk_push_int(ctx, (duk_int_t) lf_len); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE); + } + duk_push_lightfunc_name(ctx, tv); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE); + + nf = duk_get_hnativefunction(ctx, -1); + DUK_ASSERT(nf != NULL); + nf->magic = (duk_int16_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags); + + /* Enable DUKFUNC exotic behavior once properties are set up. */ + DUK_HOBJECT_SET_EXOTIC_DUKFUNC((duk_hobject *) nf); + goto replace_value; + } +#if defined(DUK_USE_FASTINT) + case DUK_TAG_FASTINT: +#endif + default: { + DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); + DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); + flags = DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_NUMBER); + proto = DUK_BIDX_NUMBER_PROTOTYPE; + goto create_object; + } + } + return; + + create_object: + (void) duk_push_object_helper(ctx, flags, proto); + + /* Note: Boolean prototype's internal value property is not writable, + * but duk_xdef_prop_stridx() disregards the write protection. Boolean + * instances are immutable. + * + * String and buffer special behaviors are already enabled which is not + * ideal, but a write to the internal value is not affected by them. + */ + duk_dup(ctx, index); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE); + + replace_value: + duk_replace(ctx, index); +} + +/* + * Type checking + */ + +DUK_LOCAL duk_bool_t duk__tag_check(duk_context *ctx, duk_idx_t index, duk_small_uint_t tag) { + duk_tval *tv; + + tv = duk_get_tval(ctx, index); + if (!tv) { + return 0; + } + return (DUK_TVAL_GET_TAG(tv) == tag); +} + +DUK_LOCAL duk_bool_t duk__obj_flag_any_default_false(duk_context *ctx, duk_idx_t index, duk_uint_t flag_mask) { + duk_hobject *obj; + + DUK_ASSERT_CTX_VALID(ctx); + + obj = duk_get_hobject(ctx, index); + if (obj) { + return (DUK_HEAPHDR_CHECK_FLAG_BITS((duk_heaphdr *) obj, flag_mask) ? 1 : 0); + } + return 0; +} + +DUK_EXTERNAL duk_int_t duk_get_type(duk_context *ctx, duk_idx_t index) { + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (!tv) { + return DUK_TYPE_NONE; + } + switch (DUK_TVAL_GET_TAG(tv)) { + case DUK_TAG_UNDEFINED: + return DUK_TYPE_UNDEFINED; + case DUK_TAG_NULL: + return DUK_TYPE_NULL; + case DUK_TAG_BOOLEAN: + return DUK_TYPE_BOOLEAN; + case DUK_TAG_STRING: + return DUK_TYPE_STRING; + case DUK_TAG_OBJECT: + return DUK_TYPE_OBJECT; + case DUK_TAG_BUFFER: + return DUK_TYPE_BUFFER; + case DUK_TAG_POINTER: + return DUK_TYPE_POINTER; + case DUK_TAG_LIGHTFUNC: + return DUK_TYPE_LIGHTFUNC; +#if defined(DUK_USE_FASTINT) + case DUK_TAG_FASTINT: +#endif + default: + /* Note: number has no explicit tag (in 8-byte representation) */ + DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); + DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); + return DUK_TYPE_NUMBER; + } + DUK_UNREACHABLE(); +} + +#if defined(DUK_USE_VERBOSE_ERRORS) && defined(DUK_USE_PARANOID_ERRORS) +DUK_LOCAL const char *duk__type_names[] = { + "none", + "undefined", + "null", + "boolean", + "number", + "string", + "object", + "buffer", + "pointer", + "lightfunc" +}; + +DUK_INTERNAL const char *duk_get_type_name(duk_context *ctx, duk_idx_t index) { + duk_int_t type_tag; + + type_tag = duk_get_type(ctx, index); + DUK_ASSERT(type_tag >= DUK_TYPE_MIN && type_tag <= DUK_TYPE_MAX); + DUK_ASSERT(DUK_TYPE_MIN == 0 && sizeof(duk__type_names) / sizeof(const char *) == DUK_TYPE_MAX + 1); + + return duk__type_names[type_tag]; +} +#endif + +DUK_EXTERNAL duk_bool_t duk_check_type(duk_context *ctx, duk_idx_t index, duk_int_t type) { + DUK_ASSERT_CTX_VALID(ctx); + + return (duk_get_type(ctx, index) == type) ? 1 : 0; +} + +DUK_EXTERNAL duk_uint_t duk_get_type_mask(duk_context *ctx, duk_idx_t index) { + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (!tv) { + return DUK_TYPE_MASK_NONE; + } + switch (DUK_TVAL_GET_TAG(tv)) { + case DUK_TAG_UNDEFINED: + return DUK_TYPE_MASK_UNDEFINED; + case DUK_TAG_NULL: + return DUK_TYPE_MASK_NULL; + case DUK_TAG_BOOLEAN: + return DUK_TYPE_MASK_BOOLEAN; + case DUK_TAG_STRING: + return DUK_TYPE_MASK_STRING; + case DUK_TAG_OBJECT: + return DUK_TYPE_MASK_OBJECT; + case DUK_TAG_BUFFER: + return DUK_TYPE_MASK_BUFFER; + case DUK_TAG_POINTER: + return DUK_TYPE_MASK_POINTER; + case DUK_TAG_LIGHTFUNC: + return DUK_TYPE_MASK_LIGHTFUNC; +#if defined(DUK_USE_FASTINT) + case DUK_TAG_FASTINT: +#endif + default: + /* Note: number has no explicit tag (in 8-byte representation) */ + DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); + DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); + return DUK_TYPE_MASK_NUMBER; + } + DUK_UNREACHABLE(); +} + +DUK_EXTERNAL duk_bool_t duk_check_type_mask(duk_context *ctx, duk_idx_t index, duk_uint_t mask) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT_CTX_VALID(ctx); + + if (duk_get_type_mask(ctx, index) & mask) { + return 1; + } + if (mask & DUK_TYPE_MASK_THROW) { + DUK_ERROR_TYPE(thr, DUK_STR_UNEXPECTED_TYPE); + DUK_UNREACHABLE(); + } + return 0; +} + +DUK_EXTERNAL duk_bool_t duk_is_undefined(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + return duk__tag_check(ctx, index, DUK_TAG_UNDEFINED); +} + +DUK_EXTERNAL duk_bool_t duk_is_null(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + return duk__tag_check(ctx, index, DUK_TAG_NULL); +} + +DUK_EXTERNAL duk_bool_t duk_is_null_or_undefined(duk_context *ctx, duk_idx_t index) { + duk_tval *tv; + duk_small_uint_t tag; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (!tv) { + return 0; + } + tag = DUK_TVAL_GET_TAG(tv); + return (tag == DUK_TAG_UNDEFINED) || (tag == DUK_TAG_NULL); +} + +DUK_EXTERNAL duk_bool_t duk_is_boolean(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + return duk__tag_check(ctx, index, DUK_TAG_BOOLEAN); +} + +DUK_EXTERNAL duk_bool_t duk_is_number(duk_context *ctx, duk_idx_t index) { + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + /* + * Number is special because it doesn't have a specific + * tag in the 8-byte representation. + */ + + /* XXX: shorter version for 12-byte representation? */ + + tv = duk_get_tval(ctx, index); + if (!tv) { + return 0; + } + return DUK_TVAL_IS_NUMBER(tv); +} + +DUK_EXTERNAL duk_bool_t duk_is_nan(duk_context *ctx, duk_idx_t index) { + /* XXX: This will now return false for non-numbers, even though they would + * coerce to NaN (as a general rule). In particular, duk_get_number() + * returns a NaN for non-numbers, so should this function also return + * true for non-numbers? + */ + + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (!tv || !DUK_TVAL_IS_NUMBER(tv)) { + return 0; + } + return DUK_ISNAN(DUK_TVAL_GET_NUMBER(tv)); +} + +DUK_EXTERNAL duk_bool_t duk_is_string(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + return duk__tag_check(ctx, index, DUK_TAG_STRING); +} + +DUK_EXTERNAL duk_bool_t duk_is_object(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + return duk__tag_check(ctx, index, DUK_TAG_OBJECT); +} + +DUK_EXTERNAL duk_bool_t duk_is_buffer(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + return duk__tag_check(ctx, index, DUK_TAG_BUFFER); +} + +DUK_EXTERNAL duk_bool_t duk_is_pointer(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + return duk__tag_check(ctx, index, DUK_TAG_POINTER); +} + +DUK_EXTERNAL duk_bool_t duk_is_lightfunc(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + return duk__tag_check(ctx, index, DUK_TAG_LIGHTFUNC); +} + +DUK_EXTERNAL duk_bool_t duk_is_array(duk_context *ctx, duk_idx_t index) { + duk_hobject *obj; + + DUK_ASSERT_CTX_VALID(ctx); + + obj = duk_get_hobject(ctx, index); + if (obj) { + return (DUK_HOBJECT_GET_CLASS_NUMBER(obj) == DUK_HOBJECT_CLASS_ARRAY ? 1 : 0); + } + return 0; +} + +DUK_EXTERNAL duk_bool_t duk_is_function(duk_context *ctx, duk_idx_t index) { + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_LIGHTFUNC(tv)) { + return 1; + } + return duk__obj_flag_any_default_false(ctx, + index, + DUK_HOBJECT_FLAG_COMPILEDFUNCTION | + DUK_HOBJECT_FLAG_NATIVEFUNCTION | + DUK_HOBJECT_FLAG_BOUND); +} + +DUK_EXTERNAL duk_bool_t duk_is_c_function(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + return duk__obj_flag_any_default_false(ctx, + index, + DUK_HOBJECT_FLAG_NATIVEFUNCTION); +} + +DUK_EXTERNAL duk_bool_t duk_is_ecmascript_function(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + return duk__obj_flag_any_default_false(ctx, + index, + DUK_HOBJECT_FLAG_COMPILEDFUNCTION); +} + +DUK_EXTERNAL duk_bool_t duk_is_bound_function(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + return duk__obj_flag_any_default_false(ctx, + index, + DUK_HOBJECT_FLAG_BOUND); +} + +DUK_EXTERNAL duk_bool_t duk_is_thread(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + return duk__obj_flag_any_default_false(ctx, + index, + DUK_HOBJECT_FLAG_THREAD); +} + +DUK_EXTERNAL duk_bool_t duk_is_fixed_buffer(duk_context *ctx, duk_idx_t index) { + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_BUFFER(tv)) { + duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); + DUK_ASSERT(h != NULL); + return (DUK_HBUFFER_HAS_DYNAMIC(h) ? 0 : 1); + } + return 0; +} + +DUK_EXTERNAL duk_bool_t duk_is_dynamic_buffer(duk_context *ctx, duk_idx_t index) { + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_BUFFER(tv)) { + duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); + DUK_ASSERT(h != NULL); + return (DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h) ? 1 : 0); + } + return 0; +} + +DUK_EXTERNAL duk_bool_t duk_is_external_buffer(duk_context *ctx, duk_idx_t index) { + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + tv = duk_get_tval(ctx, index); + if (tv && DUK_TVAL_IS_BUFFER(tv)) { + duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); + DUK_ASSERT(h != NULL); + return (DUK_HBUFFER_HAS_DYNAMIC(h) && DUK_HBUFFER_HAS_EXTERNAL(h) ? 1 : 0); + } + return 0; +} + +DUK_EXTERNAL duk_errcode_t duk_get_error_code(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *h; + duk_uint_t sanity; + + DUK_ASSERT_CTX_VALID(ctx); + + h = duk_get_hobject(ctx, index); + + sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY; + do { + if (!h) { + return DUK_ERR_NONE; + } + if (h == thr->builtins[DUK_BIDX_EVAL_ERROR_PROTOTYPE]) { + return DUK_ERR_EVAL_ERROR; + } + if (h == thr->builtins[DUK_BIDX_RANGE_ERROR_PROTOTYPE]) { + return DUK_ERR_RANGE_ERROR; + } + if (h == thr->builtins[DUK_BIDX_REFERENCE_ERROR_PROTOTYPE]) { + return DUK_ERR_REFERENCE_ERROR; + } + if (h == thr->builtins[DUK_BIDX_SYNTAX_ERROR_PROTOTYPE]) { + return DUK_ERR_SYNTAX_ERROR; + } + if (h == thr->builtins[DUK_BIDX_TYPE_ERROR_PROTOTYPE]) { + return DUK_ERR_TYPE_ERROR; + } + if (h == thr->builtins[DUK_BIDX_URI_ERROR_PROTOTYPE]) { + return DUK_ERR_URI_ERROR; + } + if (h == thr->builtins[DUK_BIDX_ERROR_PROTOTYPE]) { + return DUK_ERR_ERROR; + } + + h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h); + } while (--sanity > 0); + + return DUK_ERR_NONE; +} + +/* + * Pushers + */ + +DUK_INTERNAL void duk_push_tval(duk_context *ctx, duk_tval *tv) { + duk_hthread *thr; + duk_tval *tv_slot; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(tv != NULL); + thr = (duk_hthread *) ctx; + DUK__CHECK_SPACE(); + tv_slot = thr->valstack_top++; + DUK_TVAL_SET_TVAL(tv_slot, tv); + DUK_TVAL_INCREF(thr, tv); /* no side effects */ +} + +DUK_EXTERNAL void duk_push_undefined(duk_context *ctx) { + duk_hthread *thr; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK__CHECK_SPACE(); + + /* Because value stack init policy is 'undefined above top', + * we don't need to write, just assert. + */ + thr->valstack_top++; + DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top - 1)); +} + +DUK_EXTERNAL void duk_push_null(duk_context *ctx) { + duk_hthread *thr; + duk_tval *tv_slot; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK__CHECK_SPACE(); + tv_slot = thr->valstack_top++; + DUK_TVAL_SET_NULL(tv_slot); +} + +DUK_EXTERNAL void duk_push_boolean(duk_context *ctx, duk_bool_t val) { + duk_hthread *thr; + duk_tval *tv_slot; + duk_small_int_t b; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK__CHECK_SPACE(); + b = (val ? 1 : 0); /* ensure value is 1 or 0 (not other non-zero) */ + tv_slot = thr->valstack_top++; + DUK_TVAL_SET_BOOLEAN(tv_slot, b); +} + +DUK_EXTERNAL void duk_push_true(duk_context *ctx) { + duk_hthread *thr; + duk_tval *tv_slot; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK__CHECK_SPACE(); + tv_slot = thr->valstack_top++; + DUK_TVAL_SET_BOOLEAN_TRUE(tv_slot); +} + +DUK_EXTERNAL void duk_push_false(duk_context *ctx) { + duk_hthread *thr; + duk_tval *tv_slot; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK__CHECK_SPACE(); + tv_slot = thr->valstack_top++; + DUK_TVAL_SET_BOOLEAN_FALSE(tv_slot); +} + +/* normalize NaN which may not match our canonical internal NaN */ +DUK_EXTERNAL void duk_push_number(duk_context *ctx, duk_double_t val) { + duk_hthread *thr; + duk_tval *tv_slot; + duk_double_union du; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK__CHECK_SPACE(); + du.d = val; + DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du); + tv_slot = thr->valstack_top++; + DUK_TVAL_SET_NUMBER(tv_slot, du.d); +} + +DUK_EXTERNAL void duk_push_int(duk_context *ctx, duk_int_t val) { +#if defined(DUK_USE_FASTINT) + duk_hthread *thr; + duk_tval *tv_slot; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK__CHECK_SPACE(); + tv_slot = thr->valstack_top++; +#if DUK_INT_MAX <= 0x7fffffffL + DUK_TVAL_SET_FASTINT_I32(tv_slot, (duk_int32_t) val); +#else + if (val >= DUK_FASTINT_MIN && val <= DUK_FASTINT_MAX) { + DUK_TVAL_SET_FASTINT(tv_slot, (duk_int64_t) val); + } else { + duk_double_t = (duk_double_t) val; + DUK_TVAL_SET_NUMBER(tv_slot, d); + } +#endif +#else /* DUK_USE_FASTINT */ + duk_hthread *thr; + duk_tval *tv_slot; + duk_double_t d; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK__CHECK_SPACE(); + d = (duk_double_t) val; + tv_slot = thr->valstack_top++; + DUK_TVAL_SET_NUMBER(tv_slot, d); +#endif /* DUK_USE_FASTINT */ +} + +DUK_EXTERNAL void duk_push_uint(duk_context *ctx, duk_uint_t val) { +#if defined(DUK_USE_FASTINT) + duk_hthread *thr; + duk_tval *tv_slot; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK__CHECK_SPACE(); + tv_slot = thr->valstack_top++; +#if DUK_UINT_MAX <= 0xffffffffUL + DUK_TVAL_SET_FASTINT_U32(tv_slot, (duk_uint32_t) val); +#else + if (val <= DUK_FASTINT_MAX) { /* val is unsigned so >= 0 */ + /* XXX: take advantage of val being unsigned, no need to mask */ + DUK_TVAL_SET_FASTINT(tv_slot, (duk_int64_t) val); + } else { + duk_double_t = (duk_double_t) val; + DUK_TVAL_SET_NUMBER(tv_slot, d); + } +#endif +#else /* DUK_USE_FASTINT */ + duk_hthread *thr; + duk_tval *tv_slot; + duk_double_t d; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK__CHECK_SPACE(); + d = (duk_double_t) val; + tv_slot = thr->valstack_top++; + DUK_TVAL_SET_NUMBER(tv_slot, d); +#endif /* DUK_USE_FASTINT */ +} + +DUK_EXTERNAL void duk_push_nan(duk_context *ctx) { + duk_hthread *thr; + duk_tval *tv_slot; + duk_double_union du; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK__CHECK_SPACE(); + DUK_DBLUNION_SET_NAN(&du); + DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du)); + tv_slot = thr->valstack_top++; + DUK_TVAL_SET_NUMBER(tv_slot, du.d); +} + +DUK_EXTERNAL const char *duk_push_lstring(duk_context *ctx, const char *str, duk_size_t len) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hstring *h; + duk_tval *tv_slot; + + DUK_ASSERT_CTX_VALID(ctx); + + /* check stack before interning (avoid hanging temp) */ + if (thr->valstack_top >= thr->valstack_end) { + DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); + } + + /* NULL with zero length represents an empty string; NULL with higher + * length is also now trated like an empty string although it is + * a bit dubious. This is unlike duk_push_string() which pushes a + * 'null' if the input string is a NULL. + */ + if (!str) { + len = 0; + } + + /* Check for maximum string length */ + if (len > DUK_HSTRING_MAX_BYTELEN) { + DUK_ERROR_RANGE(thr, DUK_STR_STRING_TOO_LONG); + } + + h = duk_heap_string_intern_checked(thr, (const duk_uint8_t *) str, (duk_uint32_t) len); + DUK_ASSERT(h != NULL); + + tv_slot = thr->valstack_top++; + DUK_TVAL_SET_STRING(tv_slot, h); + DUK_HSTRING_INCREF(thr, h); /* no side effects */ + + return (const char *) DUK_HSTRING_GET_DATA(h); +} + +DUK_EXTERNAL const char *duk_push_string(duk_context *ctx, const char *str) { + DUK_ASSERT_CTX_VALID(ctx); + + if (str) { + return duk_push_lstring(ctx, str, DUK_STRLEN(str)); + } else { + duk_push_null(ctx); + return NULL; + } +} + +#ifdef DUK_USE_FILE_IO +/* This is a bit clunky because it is ANSI C portable. Should perhaps + * relocate to another file because this is potentially platform + * dependent. + */ +DUK_EXTERNAL const char *duk_push_string_file_raw(duk_context *ctx, const char *path, duk_uint_t flags) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_file *f = NULL; + char *buf; + long sz; /* ANSI C typing */ + + DUK_ASSERT_CTX_VALID(ctx); + + if (!path) { + goto fail; + } + f = DUK_FOPEN(path, "rb"); + if (!f) { + goto fail; + } + if (DUK_FSEEK(f, 0, SEEK_END) < 0) { + goto fail; + } + sz = DUK_FTELL(f); + if (sz < 0) { + goto fail; + } + if (DUK_FSEEK(f, 0, SEEK_SET) < 0) { + goto fail; + } + buf = (char *) duk_push_fixed_buffer(ctx, (duk_size_t) sz); + DUK_ASSERT(buf != NULL); + if ((duk_size_t) DUK_FREAD(buf, 1, (size_t) sz, f) != (duk_size_t) sz) { + goto fail; + } + (void) DUK_FCLOSE(f); /* ignore fclose() error */ + f = NULL; + return duk_to_string(ctx, -1); + + fail: + if (f) { + DUK_FCLOSE(f); + } + + if (flags != 0) { + DUK_ASSERT(flags == DUK_STRING_PUSH_SAFE); /* only flag now */ + duk_push_undefined(ctx); + } else { + /* XXX: string not shared because it is conditional */ + DUK_ERROR_TYPE(thr, "read file error"); + } + return NULL; +} +#else +DUK_EXTERNAL const char *duk_push_string_file_raw(duk_context *ctx, const char *path, duk_uint_t flags) { + duk_hthread *thr = (duk_hthread *) ctx; + DUK_ASSERT_CTX_VALID(ctx); + DUK_UNREF(path); + + if (flags != 0) { + DUK_ASSERT(flags == DUK_STRING_PUSH_SAFE); /* only flag now */ + duk_push_undefined(ctx); + } else { + /* XXX: string not shared because it is conditional */ + DUK_ERROR_UNSUPPORTED(thr, "file I/O disabled"); + } + return NULL; +} +#endif /* DUK_USE_FILE_IO */ + +DUK_EXTERNAL void duk_push_pointer(duk_context *ctx, void *val) { + duk_hthread *thr; + duk_tval *tv_slot; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK__CHECK_SPACE(); + tv_slot = thr->valstack_top++; + DUK_TVAL_SET_POINTER(tv_slot, val); +} + +DUK_LOCAL void duk__push_this_helper(duk_context *ctx, duk_small_uint_t check_object_coercible) { + duk_hthread *thr; + duk_tval *tv_slot; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT_DISABLE(thr->callstack_top >= 0); /* avoid warning (unsigned) */ + thr = (duk_hthread *) ctx; + DUK_ASSERT(thr->callstack_top <= thr->callstack_size); + DUK__CHECK_SPACE(); + + DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top)); /* because of valstack init policy */ + tv_slot = thr->valstack_top++; + + if (DUK_UNLIKELY(thr->callstack_top == 0)) { + if (check_object_coercible) { + goto type_error; + } + /* 'undefined' already on stack top */ + } else { + duk_tval *tv; + + /* 'this' binding is just before current activation's bottom */ + DUK_ASSERT(thr->valstack_bottom > thr->valstack); + tv = thr->valstack_bottom - 1; + if (check_object_coercible && + (DUK_TVAL_IS_UNDEFINED(tv) || DUK_TVAL_IS_NULL(tv))) { + /* XXX: better macro for DUK_TVAL_IS_UNDEFINED_OR_NULL(tv) */ + goto type_error; + } + + DUK_TVAL_SET_TVAL(tv_slot, tv); + DUK_TVAL_INCREF(thr, tv); + } + return; + + type_error: + DUK_ERROR_TYPE(thr, DUK_STR_NOT_OBJECT_COERCIBLE); +} + +DUK_EXTERNAL void duk_push_this(duk_context *ctx) { + DUK_ASSERT_CTX_VALID(ctx); + + duk__push_this_helper(ctx, 0 /*check_object_coercible*/); +} + +DUK_INTERNAL void duk_push_this_check_object_coercible(duk_context *ctx) { + DUK_ASSERT_CTX_VALID(ctx); + + duk__push_this_helper(ctx, 1 /*check_object_coercible*/); +} + +DUK_INTERNAL duk_hobject *duk_push_this_coercible_to_object(duk_context *ctx) { + duk_hobject *h; + + DUK_ASSERT_CTX_VALID(ctx); + + duk__push_this_helper(ctx, 1 /*check_object_coercible*/); + duk_to_object(ctx, -1); + h = duk_get_hobject(ctx, -1); + DUK_ASSERT(h != NULL); + return h; +} + +DUK_INTERNAL duk_hstring *duk_push_this_coercible_to_string(duk_context *ctx) { + duk_hstring *h; + + DUK_ASSERT_CTX_VALID(ctx); + + duk__push_this_helper(ctx, 1 /*check_object_coercible*/); + duk_to_string(ctx, -1); + h = duk_get_hstring(ctx, -1); + DUK_ASSERT(h != NULL); + return h; +} + +DUK_INTERNAL duk_tval *duk_get_borrowed_this_tval(duk_context *ctx) { + duk_hthread *thr; + + DUK_ASSERT(ctx != NULL); + thr = (duk_hthread *) ctx; + + DUK_ASSERT(thr->callstack_top > 0); /* caller required to know */ + DUK_ASSERT(thr->valstack_bottom > thr->valstack); /* consequence of above */ + DUK_ASSERT(thr->valstack_bottom - 1 >= thr->valstack); /* 'this' binding exists */ + + return thr->valstack_bottom - 1; +} + +DUK_EXTERNAL void duk_push_current_function(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_activation *act; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + DUK_ASSERT_DISABLE(thr->callstack_top >= 0); + DUK_ASSERT(thr->callstack_top <= thr->callstack_size); + + act = duk_hthread_get_current_activation(thr); + if (act) { + duk_push_tval(ctx, &act->tv_func); + } else { + duk_push_undefined(ctx); + } +} + +DUK_EXTERNAL void duk_push_current_thread(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + + if (thr->heap->curr_thread) { + duk_push_hobject(ctx, (duk_hobject *) thr->heap->curr_thread); + } else { + duk_push_undefined(ctx); + } +} + +DUK_EXTERNAL void duk_push_global_object(duk_context *ctx) { + DUK_ASSERT_CTX_VALID(ctx); + + duk_push_hobject_bidx(ctx, DUK_BIDX_GLOBAL); +} + +/* XXX: size optimize */ +DUK_LOCAL void duk__push_stash(duk_context *ctx) { + DUK_ASSERT_CTX_VALID(ctx); + if (!duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE)) { + DUK_DDD(DUK_DDDPRINT("creating heap/global/thread stash on first use")); + duk_pop(ctx); + duk_push_object_internal(ctx); + duk_dup_top(ctx); + duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_C); /* [ ... parent stash stash ] -> [ ... parent stash ] */ + } + duk_remove(ctx, -2); +} + +DUK_EXTERNAL void duk_push_heap_stash(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_heap *heap; + DUK_ASSERT_CTX_VALID(ctx); + heap = thr->heap; + DUK_ASSERT(heap->heap_object != NULL); + duk_push_hobject(ctx, heap->heap_object); + duk__push_stash(ctx); +} + +DUK_EXTERNAL void duk_push_global_stash(duk_context *ctx) { + DUK_ASSERT_CTX_VALID(ctx); + duk_push_global_object(ctx); + duk__push_stash(ctx); +} + +DUK_EXTERNAL void duk_push_thread_stash(duk_context *ctx, duk_context *target_ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + DUK_ASSERT_CTX_VALID(ctx); + if (!target_ctx) { + DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); + return; /* not reached */ + } + duk_push_hobject(ctx, (duk_hobject *) target_ctx); + duk__push_stash(ctx); +} + +/* XXX: duk_ssize_t would be useful here */ +DUK_LOCAL duk_int_t duk__try_push_vsprintf(duk_context *ctx, void *buf, duk_size_t sz, const char *fmt, va_list ap) { + duk_int_t len; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_UNREF(ctx); + + /* NUL terminator handling doesn't matter here */ + len = DUK_VSNPRINTF((char *) buf, sz, fmt, ap); + if (len < (duk_int_t) sz) { + /* Return value of 'sz' or more indicates output was (potentially) + * truncated. + */ + return (duk_int_t) len; + } + return -1; +} + +DUK_EXTERNAL const char *duk_push_vsprintf(duk_context *ctx, const char *fmt, va_list ap) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_uint8_t stack_buf[DUK_PUSH_SPRINTF_INITIAL_SIZE]; + duk_size_t sz = DUK_PUSH_SPRINTF_INITIAL_SIZE; + duk_bool_t pushed_buf = 0; + void *buf; + duk_int_t len; /* XXX: duk_ssize_t */ + const char *res; + + DUK_ASSERT_CTX_VALID(ctx); + + /* special handling of fmt==NULL */ + if (!fmt) { + duk_hstring *h_str; + duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING); + h_str = DUK_HTHREAD_STRING_EMPTY_STRING(thr); /* rely on interning, must be this string */ + return (const char *) DUK_HSTRING_GET_DATA(h_str); + } + + /* initial estimate based on format string */ + sz = DUK_STRLEN(fmt) + 16; /* format plus something to avoid just missing */ + if (sz < DUK_PUSH_SPRINTF_INITIAL_SIZE) { + sz = DUK_PUSH_SPRINTF_INITIAL_SIZE; + } + DUK_ASSERT(sz > 0); + + /* Try to make do with a stack buffer to avoid allocating a temporary buffer. + * This works 99% of the time which is quite nice. + */ + for (;;) { + va_list ap_copy; /* copied so that 'ap' can be reused */ + + if (sz <= sizeof(stack_buf)) { + buf = stack_buf; + } else if (!pushed_buf) { + pushed_buf = 1; + buf = duk_push_dynamic_buffer(ctx, sz); + } else { + buf = duk_resize_buffer(ctx, -1, sz); + } + DUK_ASSERT(buf != NULL); + + DUK_VA_COPY(ap_copy, ap); + len = duk__try_push_vsprintf(ctx, buf, sz, fmt, ap_copy); + va_end(ap_copy); + if (len >= 0) { + break; + } + + /* failed, resize and try again */ + sz = sz * 2; + if (sz >= DUK_PUSH_SPRINTF_SANITY_LIMIT) { + DUK_ERROR_API(thr, DUK_STR_SPRINTF_TOO_LONG); + } + } + + /* Cannot use duk_to_string() on the buffer because it is usually + * larger than 'len'. Also, 'buf' is usually a stack buffer. + */ + res = duk_push_lstring(ctx, (const char *) buf, (duk_size_t) len); /* [ buf? res ] */ + if (pushed_buf) { + duk_remove(ctx, -2); + } + return res; +} + +DUK_EXTERNAL const char *duk_push_sprintf(duk_context *ctx, const char *fmt, ...) { + va_list ap; + const char *ret; + + DUK_ASSERT_CTX_VALID(ctx); + + /* allow fmt==NULL */ + va_start(ap, fmt); + ret = duk_push_vsprintf(ctx, fmt, ap); + va_end(ap); + + return ret; +} + +DUK_INTERNAL duk_idx_t duk_push_object_helper(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv_slot; + duk_hobject *h; + duk_idx_t ret; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(prototype_bidx == -1 || + (prototype_bidx >= 0 && prototype_bidx < DUK_NUM_BUILTINS)); + + /* check stack first */ + if (thr->valstack_top >= thr->valstack_end) { + DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); + } + + h = duk_hobject_alloc(thr->heap, hobject_flags_and_class); + if (!h) { + DUK_ERROR_ALLOC_DEFMSG(thr); + } + + DUK_DDD(DUK_DDDPRINT("created object with flags: 0x%08lx", (unsigned long) h->hdr.h_flags)); + + tv_slot = thr->valstack_top; + DUK_TVAL_SET_OBJECT(tv_slot, h); + DUK_HOBJECT_INCREF(thr, h); /* no side effects */ + ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom); + thr->valstack_top++; + + /* object is now reachable */ + + if (prototype_bidx >= 0) { + DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, thr->builtins[prototype_bidx]); + } else { + DUK_ASSERT(prototype_bidx == -1); + DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h) == NULL); + } + + return ret; +} + +DUK_INTERNAL duk_idx_t duk_push_object_helper_proto(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_hobject *proto) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_idx_t ret; + duk_hobject *h; + + DUK_ASSERT_CTX_VALID(ctx); + + ret = duk_push_object_helper(ctx, hobject_flags_and_class, -1); + h = duk_get_hobject(ctx, -1); + DUK_ASSERT(h != NULL); + DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h) == NULL); + DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, proto); + return ret; +} + +DUK_EXTERNAL duk_idx_t duk_push_object(duk_context *ctx) { + DUK_ASSERT_CTX_VALID(ctx); + + return duk_push_object_helper(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT), + DUK_BIDX_OBJECT_PROTOTYPE); +} + +DUK_EXTERNAL duk_idx_t duk_push_array(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *obj; + duk_idx_t ret; + + DUK_ASSERT_CTX_VALID(ctx); + + ret = duk_push_object_helper(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_ARRAY_PART | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAY), + DUK_BIDX_ARRAY_PROTOTYPE); + + obj = duk_require_hobject(ctx, ret); + + /* + * An array must have a 'length' property (E5 Section 15.4.5.2). + * The special array behavior flag must only be enabled once the + * length property has been added. + * + * The internal property must be a number (and preferably a + * fastint if fastint support is enabled). + */ + + duk_push_int(ctx, 0); +#if defined(DUK_USE_FASTINT) + DUK_ASSERT(DUK_TVAL_IS_FASTINT(duk_require_tval(ctx, -1))); +#endif + + duk_hobject_define_property_internal(thr, + obj, + DUK_HTHREAD_STRING_LENGTH(thr), + DUK_PROPDESC_FLAGS_W); + DUK_HOBJECT_SET_EXOTIC_ARRAY(obj); + + return ret; +} + +DUK_EXTERNAL duk_idx_t duk_push_thread_raw(duk_context *ctx, duk_uint_t flags) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hthread *obj; + duk_idx_t ret; + duk_tval *tv_slot; + + DUK_ASSERT_CTX_VALID(ctx); + + /* check stack first */ + if (thr->valstack_top >= thr->valstack_end) { + DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); + } + + obj = duk_hthread_alloc(thr->heap, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_THREAD | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_THREAD)); + if (!obj) { + DUK_ERROR_ALLOC_DEFMSG(thr); + } + obj->state = DUK_HTHREAD_STATE_INACTIVE; +#if defined(DUK_USE_ROM_STRINGS) + /* Nothing to initialize, strs[] is in ROM. */ +#else +#if defined(DUK_USE_HEAPPTR16) + obj->strs16 = thr->strs16; +#else + obj->strs = thr->strs; +#endif +#endif + DUK_DDD(DUK_DDDPRINT("created thread object with flags: 0x%08lx", (unsigned long) obj->obj.hdr.h_flags)); + + /* make the new thread reachable */ + tv_slot = thr->valstack_top; + DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj); + DUK_HTHREAD_INCREF(thr, obj); + ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom); + thr->valstack_top++; + + /* important to do this *after* pushing, to make the thread reachable for gc */ + if (!duk_hthread_init_stacks(thr->heap, obj)) { + DUK_ERROR_ALLOC_DEFMSG(thr); + } + + /* initialize built-ins - either by copying or creating new ones */ + if (flags & DUK_THREAD_NEW_GLOBAL_ENV) { + duk_hthread_create_builtin_objects(obj); + } else { + duk_hthread_copy_builtin_objects(thr, obj); + } + + /* default prototype (Note: 'obj' must be reachable) */ + DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, obj->builtins[DUK_BIDX_THREAD_PROTOTYPE]); + + /* Initial stack size satisfies the stack spare constraints so there + * is no need to require stack here. + */ + DUK_ASSERT(DUK_VALSTACK_INITIAL_SIZE >= + DUK_VALSTACK_API_ENTRY_MINIMUM + DUK_VALSTACK_INTERNAL_EXTRA); + + return ret; +} + +DUK_INTERNAL duk_idx_t duk_push_compiledfunction(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hcompiledfunction *obj; + duk_idx_t ret; + duk_tval *tv_slot; + + DUK_ASSERT_CTX_VALID(ctx); + + /* check stack first */ + if (thr->valstack_top >= thr->valstack_end) { + DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); + } + + /* Template functions are not strictly constructable (they don't + * have a "prototype" property for instance), so leave the + * DUK_HOBJECT_FLAG_CONSRUCTABLE flag cleared here. + */ + + obj = duk_hcompiledfunction_alloc(thr->heap, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_COMPILEDFUNCTION | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION)); + if (!obj) { + DUK_ERROR_ALLOC_DEFMSG(thr); + } + + DUK_DDD(DUK_DDDPRINT("created compiled function object with flags: 0x%08lx", (unsigned long) obj->obj.hdr.h_flags)); + + tv_slot = thr->valstack_top; + DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj); + DUK_HOBJECT_INCREF(thr, obj); + ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom); + thr->valstack_top++; + + /* default prototype (Note: 'obj' must be reachable) */ + DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]); + + return ret; +} + +DUK_LOCAL duk_idx_t duk__push_c_function_raw(duk_context *ctx, duk_c_function func, duk_idx_t nargs, duk_uint_t flags) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hnativefunction *obj; + duk_idx_t ret; + duk_tval *tv_slot; + duk_int16_t func_nargs; + + DUK_ASSERT_CTX_VALID(ctx); + + /* check stack first */ + if (thr->valstack_top >= thr->valstack_end) { + DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); + } + if (func == NULL) { + goto api_error; + } + if (nargs >= 0 && nargs < DUK_HNATIVEFUNCTION_NARGS_MAX) { + func_nargs = (duk_int16_t) nargs; + } else if (nargs == DUK_VARARGS) { + func_nargs = DUK_HNATIVEFUNCTION_NARGS_VARARGS; + } else { + goto api_error; + } + + obj = duk_hnativefunction_alloc(thr->heap, flags); + if (!obj) { + DUK_ERROR_ALLOC_DEFMSG(thr); + } + + obj->func = func; + obj->nargs = func_nargs; + + DUK_DDD(DUK_DDDPRINT("created native function object with flags: 0x%08lx, nargs=%ld", + (unsigned long) obj->obj.hdr.h_flags, (long) obj->nargs)); + + tv_slot = thr->valstack_top; + DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj); + DUK_HOBJECT_INCREF(thr, obj); + ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom); + thr->valstack_top++; + + /* default prototype (Note: 'obj' must be reachable) */ + DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]); + + return ret; + + api_error: + DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); + return 0; /* not reached */ +} + +DUK_EXTERNAL duk_idx_t duk_push_c_function(duk_context *ctx, duk_c_function func, duk_int_t nargs) { + duk_uint_t flags; + + DUK_ASSERT_CTX_VALID(ctx); + + flags = DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_CONSTRUCTABLE | + DUK_HOBJECT_FLAG_NATIVEFUNCTION | + DUK_HOBJECT_FLAG_NEWENV | + DUK_HOBJECT_FLAG_STRICT | + DUK_HOBJECT_FLAG_NOTAIL | + DUK_HOBJECT_FLAG_EXOTIC_DUKFUNC | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION); + + return duk__push_c_function_raw(ctx, func, nargs, flags); +} + +DUK_INTERNAL void duk_push_c_function_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs) { + duk_uint_t flags; + + DUK_ASSERT_CTX_VALID(ctx); + + flags = DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_CONSTRUCTABLE | + DUK_HOBJECT_FLAG_NATIVEFUNCTION | + DUK_HOBJECT_FLAG_NEWENV | + DUK_HOBJECT_FLAG_STRICT | + DUK_HOBJECT_FLAG_NOTAIL | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION); + + (void) duk__push_c_function_raw(ctx, func, nargs, flags); +} + +DUK_INTERNAL void duk_push_c_function_noconstruct_noexotic(duk_context *ctx, duk_c_function func, duk_int_t nargs) { + duk_uint_t flags; + + DUK_ASSERT_CTX_VALID(ctx); + + flags = DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_NATIVEFUNCTION | + DUK_HOBJECT_FLAG_NEWENV | + DUK_HOBJECT_FLAG_STRICT | + DUK_HOBJECT_FLAG_NOTAIL | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION); + + (void) duk__push_c_function_raw(ctx, func, nargs, flags); +} + +DUK_EXTERNAL duk_idx_t duk_push_c_lightfunc(duk_context *ctx, duk_c_function func, duk_idx_t nargs, duk_idx_t length, duk_int_t magic) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval tv_tmp; + duk_small_uint_t lf_flags; + + DUK_ASSERT_CTX_VALID(ctx); + + /* check stack first */ + if (thr->valstack_top >= thr->valstack_end) { + DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); + } + + if (nargs >= DUK_LFUNC_NARGS_MIN && nargs <= DUK_LFUNC_NARGS_MAX) { + /* as is */ + } else if (nargs == DUK_VARARGS) { + nargs = DUK_LFUNC_NARGS_VARARGS; + } else { + goto api_error; + } + if (!(length >= DUK_LFUNC_LENGTH_MIN && length <= DUK_LFUNC_LENGTH_MAX)) { + goto api_error; + } + if (!(magic >= DUK_LFUNC_MAGIC_MIN && magic <= DUK_LFUNC_MAGIC_MAX)) { + goto api_error; + } + + lf_flags = DUK_LFUNC_FLAGS_PACK(magic, length, nargs); + DUK_TVAL_SET_LIGHTFUNC(&tv_tmp, func, lf_flags); + duk_push_tval(ctx, &tv_tmp); /* XXX: direct valstack write */ + DUK_ASSERT(thr->valstack_top != thr->valstack_bottom); + return ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1; + + api_error: + DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); + return 0; /* not reached */ +} + +DUK_INTERNAL duk_hbufferobject *duk_push_bufferobject_raw(duk_context *ctx, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hbufferobject *obj; + duk_tval *tv_slot; + + DUK_ASSERT(ctx != NULL); + DUK_ASSERT(prototype_bidx >= 0); + + /* check stack first */ + if (thr->valstack_top >= thr->valstack_end) { + DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); + } + + obj = duk_hbufferobject_alloc(thr->heap, hobject_flags_and_class); + if (!obj) { + DUK_ERROR_ALLOC_DEFMSG(thr); + } + + DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) obj, thr->builtins[prototype_bidx]); + DUK_ASSERT_HBUFFEROBJECT_VALID(obj); + + tv_slot = thr->valstack_top; + DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj); + DUK_HOBJECT_INCREF(thr, obj); + thr->valstack_top++; + + return obj; +} + +/* XXX: There's quite a bit of overlap with buffer creation handling in + * duk_bi_buffer.c. Look for overlap and refactor. + */ +#define DUK__PACK_ARGS(classnum,protobidx,elemtype,elemshift,isview) \ + (((classnum) << 24) | ((protobidx) << 16) | ((elemtype) << 8) | ((elemshift) << 4) | (isview)) + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +static const duk_uint32_t duk__bufobj_flags_lookup[] = { + DUK__PACK_ARGS(DUK_HOBJECT_CLASS_BUFFER, DUK_BIDX_BUFFER_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 0), /* DUK_BUFOBJ_DUKTAPE_BUFFER */ + DUK__PACK_ARGS(DUK_HOBJECT_CLASS_BUFFER, DUK_BIDX_NODEJS_BUFFER_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 0), /* DUK_BUFOBJ_NODEJS_BUFFER */ + DUK__PACK_ARGS(DUK_HOBJECT_CLASS_ARRAYBUFFER, DUK_BIDX_ARRAYBUFFER_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 0), /* DUK_BUFOBJ_ARRAYBUFFER */ + DUK__PACK_ARGS(DUK_HOBJECT_CLASS_DATAVIEW, DUK_BIDX_DATAVIEW_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 1), /* DUK_BUFOBJ_DATAVIEW */ + DUK__PACK_ARGS(DUK_HOBJECT_CLASS_INT8ARRAY, DUK_BIDX_INT8ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_INT8, 0, 1), /* DUK_BUFOBJ_INT8ARRAY */ + DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT8ARRAY, DUK_BIDX_UINT8ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 1), /* DUK_BUFOBJ_UINT8ARRAY */ + DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY, DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED, 0, 1), /* DUK_BUFOBJ_UINT8CLAMPEDARRAY */ + DUK__PACK_ARGS(DUK_HOBJECT_CLASS_INT16ARRAY, DUK_BIDX_INT16ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_INT16, 1, 1), /* DUK_BUFOBJ_INT16ARRAY */ + DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT16ARRAY, DUK_BIDX_UINT16ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT16, 1, 1), /* DUK_BUFOBJ_UINT16ARRAY */ + DUK__PACK_ARGS(DUK_HOBJECT_CLASS_INT32ARRAY, DUK_BIDX_INT32ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_INT32, 2, 1), /* DUK_BUFOBJ_INT32ARRAY */ + DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT32ARRAY, DUK_BIDX_UINT32ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT32, 2, 1), /* DUK_BUFOBJ_UINT32ARRAY */ + DUK__PACK_ARGS(DUK_HOBJECT_CLASS_FLOAT32ARRAY, DUK_BIDX_FLOAT32ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_FLOAT32, 2, 1), /* DUK_BUFOBJ_FLOAT32ARRAY */ + DUK__PACK_ARGS(DUK_HOBJECT_CLASS_FLOAT64ARRAY, DUK_BIDX_FLOAT64ARRAY_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_FLOAT64, 3, 1) /* DUK_BUFOBJ_FLOAT64ARRAY */ +}; +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +/* Only allow Duktape.Buffer when support disabled. */ +static const duk_uint32_t duk__bufobj_flags_lookup[] = { + DUK__PACK_ARGS(DUK_HOBJECT_CLASS_BUFFER, DUK_BIDX_BUFFER_PROTOTYPE, DUK_HBUFFEROBJECT_ELEM_UINT8, 0, 0) /* DUK_BUFOBJ_DUKTAPE_BUFFER */ +}; +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ +#undef DUK__PACK_ARGS + +DUK_EXTERNAL void duk_push_buffer_object(duk_context *ctx, duk_idx_t idx_buffer, duk_size_t byte_offset, duk_size_t byte_length, duk_uint_t flags) { + duk_hthread *thr; + duk_hbufferobject *h_bufobj; + duk_hbuffer *h_val; + duk_uint32_t tmp; + duk_uint_t classnum; + duk_uint_t protobidx; + duk_uint_t lookupidx; + duk_uint_t uint_offset, uint_length, uint_added; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + /* The underlying types for offset/length in duk_hbufferobject is + * duk_uint_t; make sure argument values fit and that offset + length + * does not wrap. + */ + uint_offset = (duk_uint_t) byte_offset; + uint_length = (duk_uint_t) byte_length; + if (sizeof(duk_size_t) != sizeof(duk_uint_t)) { + if ((duk_size_t) uint_offset != byte_offset || (duk_size_t) uint_length != byte_length) { + goto range_error; + } + } + uint_added = uint_offset + uint_length; + if (uint_added < uint_offset) { + goto range_error; + } + DUK_ASSERT(uint_added >= uint_offset && uint_added >= uint_length); + + DUK_ASSERT_DISABLE(flags >= 0); /* flags is unsigned */ + lookupidx = flags & 0x0f; /* 4 low bits */ + if (lookupidx >= sizeof(duk__bufobj_flags_lookup) / sizeof(duk_uint32_t)) { + goto arg_error; + } + tmp = duk__bufobj_flags_lookup[lookupidx]; + classnum = tmp >> 24; + protobidx = (tmp >> 16) & 0xff; + + h_val = duk_require_hbuffer(ctx, idx_buffer); + DUK_ASSERT(h_val != NULL); + + h_bufobj = duk_push_bufferobject_raw(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_BUFFEROBJECT | + DUK_HOBJECT_CLASS_AS_FLAGS(classnum), + protobidx); + DUK_ASSERT(h_bufobj != NULL); + + h_bufobj->buf = h_val; + DUK_HBUFFER_INCREF(thr, h_val); + h_bufobj->offset = uint_offset; + h_bufobj->length = uint_length; + h_bufobj->shift = (tmp >> 4) & 0x0f; + h_bufobj->elem_type = (tmp >> 8) & 0xff; + h_bufobj->is_view = tmp & 0x0f; + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) + /* TypedArray views need an automatic ArrayBuffer which must be + * provided as .buffer property of the view. Just create a new + * ArrayBuffer sharing the same underlying buffer. + */ + if (flags & DUK_BUFOBJ_CREATE_ARRBUF) { + h_bufobj = duk_push_bufferobject_raw(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_BUFFEROBJECT | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER), + DUK_BIDX_ARRAYBUFFER_PROTOTYPE); + + DUK_ASSERT(h_bufobj != NULL); + + h_bufobj->buf = h_val; + DUK_HBUFFER_INCREF(thr, h_val); + h_bufobj->offset = uint_offset; + h_bufobj->length = uint_length; + DUK_ASSERT(h_bufobj->shift == 0); + h_bufobj->elem_type = DUK_HBUFFEROBJECT_ELEM_UINT8; + DUK_ASSERT(h_bufobj->is_view == 0); + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE); + duk_compact(ctx, -1); + } +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + + return; + + range_error: + DUK_ERROR_RANGE(thr, DUK_STR_INVALID_CALL_ARGS); + return; /* not reached */ + + arg_error: + DUK_ERROR_TYPE(thr, DUK_STR_INVALID_CALL_ARGS); + return; /* not reached */ +} + +DUK_EXTERNAL duk_idx_t duk_push_error_object_va_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, va_list ap) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_idx_t ret; + duk_hobject *proto; +#ifdef DUK_USE_AUGMENT_ERROR_CREATE + duk_bool_t noblame_fileline; +#endif + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + DUK_UNREF(filename); + DUK_UNREF(line); + + /* Error code also packs a tracedata related flag. */ +#ifdef DUK_USE_AUGMENT_ERROR_CREATE + noblame_fileline = err_code & DUK_ERRCODE_FLAG_NOBLAME_FILELINE; +#endif + err_code = err_code & (~DUK_ERRCODE_FLAG_NOBLAME_FILELINE); + + /* error gets its 'name' from the prototype */ + proto = duk_error_prototype_from_code(thr, err_code); + ret = duk_push_object_helper_proto(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ERROR), + proto); + + /* ... and its 'message' from an instance property */ + if (fmt) { + duk_push_vsprintf(ctx, fmt, ap); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC); + } else { + /* If no explicit message given, put error code into message field + * (as a number). This is not fully in keeping with the Ecmascript + * error model because messages are supposed to be strings (Error + * constructors use ToString() on their argument). However, it's + * probably more useful than having a separate 'code' property. + */ + duk_push_int(ctx, err_code); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC); + } + + /* XXX: .code = err_code disabled, not sure if useful */ + + /* Creation time error augmentation */ +#ifdef DUK_USE_AUGMENT_ERROR_CREATE + /* filename may be NULL in which case file/line is not recorded */ + duk_err_augment_error_create(thr, thr, filename, line, noblame_fileline); /* may throw an error */ +#endif + + return ret; +} + +DUK_EXTERNAL duk_idx_t duk_push_error_object_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, ...) { + va_list ap; + duk_idx_t ret; + + DUK_ASSERT_CTX_VALID(ctx); + + va_start(ap, fmt); + ret = duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap); + va_end(ap); + return ret; +} + +#if !defined(DUK_USE_VARIADIC_MACROS) +DUK_EXTERNAL duk_idx_t duk_push_error_object_stash(duk_context *ctx, duk_errcode_t err_code, const char *fmt, ...) { + const char *filename = duk_api_global_filename; + duk_int_t line = duk_api_global_line; + va_list ap; + duk_idx_t ret; + + DUK_ASSERT_CTX_VALID(ctx); + + duk_api_global_filename = NULL; + duk_api_global_line = 0; + va_start(ap, fmt); + ret = duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap); + va_end(ap); + return ret; +} +#endif /* DUK_USE_VARIADIC_MACROS */ + +DUK_EXTERNAL void *duk_push_buffer_raw(duk_context *ctx, duk_size_t size, duk_small_uint_t flags) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv_slot; + duk_hbuffer *h; + void *buf_data; + + DUK_ASSERT_CTX_VALID(ctx); + + /* check stack first */ + if (thr->valstack_top >= thr->valstack_end) { + DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); + } + + /* Check for maximum buffer length. */ + if (size > DUK_HBUFFER_MAX_BYTELEN) { + DUK_ERROR_RANGE(thr, DUK_STR_BUFFER_TOO_LONG); + } + + h = duk_hbuffer_alloc(thr->heap, size, flags, &buf_data); + if (!h) { + DUK_ERROR_ALLOC_DEFMSG(thr); + } + + tv_slot = thr->valstack_top; + DUK_TVAL_SET_BUFFER(tv_slot, h); + DUK_HBUFFER_INCREF(thr, h); + thr->valstack_top++; + + return (void *) buf_data; +} + +DUK_EXTERNAL duk_idx_t duk_push_heapptr(duk_context *ctx, void *ptr) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_idx_t ret; + + DUK_ASSERT_CTX_VALID(ctx); + + ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom); + + if (ptr == NULL) { + goto push_undefined; + } + + switch ((int) DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) ptr)) { + case DUK_HTYPE_STRING: + duk_push_hstring(ctx, (duk_hstring *) ptr); + break; + case DUK_HTYPE_OBJECT: + duk_push_hobject(ctx, (duk_hobject *) ptr); + break; + case DUK_HTYPE_BUFFER: + duk_push_hbuffer(ctx, (duk_hbuffer *) ptr); + break; + default: + goto push_undefined; + } + return ret; + + push_undefined: + duk_push_undefined(ctx); + return ret; +} + +DUK_INTERNAL duk_idx_t duk_push_object_internal(duk_context *ctx) { + return duk_push_object_helper(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT), + -1); /* no prototype */ +} + +DUK_INTERNAL void duk_push_hstring(duk_context *ctx, duk_hstring *h) { + duk_tval tv; + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(h != NULL); + DUK_TVAL_SET_STRING(&tv, h); + duk_push_tval(ctx, &tv); +} + +DUK_INTERNAL void duk_push_hstring_stridx(duk_context *ctx, duk_small_int_t stridx) { + duk_hthread *thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + DUK_ASSERT(stridx >= 0 && stridx < DUK_HEAP_NUM_STRINGS); + duk_push_hstring(ctx, DUK_HTHREAD_GET_STRING(thr, stridx)); +} + +DUK_INTERNAL void duk_push_hobject(duk_context *ctx, duk_hobject *h) { + duk_tval tv; + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(h != NULL); + DUK_TVAL_SET_OBJECT(&tv, h); + duk_push_tval(ctx, &tv); +} + +DUK_INTERNAL void duk_push_hbuffer(duk_context *ctx, duk_hbuffer *h) { + duk_tval tv; + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(h != NULL); + DUK_TVAL_SET_BUFFER(&tv, h); + duk_push_tval(ctx, &tv); +} + +DUK_INTERNAL void duk_push_hobject_bidx(duk_context *ctx, duk_small_int_t builtin_idx) { + duk_hthread *thr = (duk_hthread *) ctx; + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + DUK_ASSERT(builtin_idx >= 0 && builtin_idx < DUK_NUM_BUILTINS); + DUK_ASSERT(thr->builtins[builtin_idx] != NULL); + duk_push_hobject(ctx, thr->builtins[builtin_idx]); +} + +/* + * Poppers + */ + +DUK_EXTERNAL void duk_pop_n(duk_context *ctx, duk_idx_t count) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + + DUK_ASSERT_CTX_VALID(ctx); + + if (DUK_UNLIKELY(count < 0)) { + DUK_ERROR_API(thr, DUK_STR_INVALID_COUNT); + return; + } + + DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); + if (DUK_UNLIKELY((duk_size_t) (thr->valstack_top - thr->valstack_bottom) < (duk_size_t) count)) { + DUK_ERROR_API(thr, DUK_STR_POP_TOO_MANY); + } + + /* + * Must be very careful here, every DECREF may cause reallocation + * of our valstack. + */ + + /* XXX: inlined DECREF macro would be nice here: no NULL check, + * refzero queueing but no refzero algorithm run (= no pointer + * instability), inline code. + */ + + /* XXX: optimize loops */ + +#if defined(DUK_USE_REFERENCE_COUNTING) + while (count > 0) { + count--; + tv = --thr->valstack_top; /* tv points to element just below prev top */ + DUK_ASSERT(tv >= thr->valstack_bottom); + DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */ + } +#else + tv = thr->valstack_top; + while (count > 0) { + count--; + tv--; + DUK_ASSERT(tv >= thr->valstack_bottom); + DUK_TVAL_SET_UNDEFINED(tv); + } + thr->valstack_top = tv; +#endif + + DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); +} + +/* Popping one element is called so often that when footprint is not an issue, + * compile a specialized function for it. + */ +#if defined(DUK_USE_PREFER_SIZE) +DUK_EXTERNAL void duk_pop(duk_context *ctx) { + DUK_ASSERT_CTX_VALID(ctx); + duk_pop_n(ctx, 1); +} +#else +DUK_EXTERNAL void duk_pop(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + DUK_ASSERT_CTX_VALID(ctx); + + DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); + if (DUK_UNLIKELY(thr->valstack_top == thr->valstack_bottom)) { + DUK_ERROR_API(thr, DUK_STR_POP_TOO_MANY); + } + + tv = --thr->valstack_top; /* tv points to element just below prev top */ + DUK_ASSERT(tv >= thr->valstack_bottom); +#ifdef DUK_USE_REFERENCE_COUNTING + DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */ +#else + DUK_TVAL_SET_UNDEFINED(tv); +#endif + DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); +} +#endif /* !DUK_USE_PREFER_SIZE */ + +DUK_EXTERNAL void duk_pop_2(duk_context *ctx) { + DUK_ASSERT_CTX_VALID(ctx); + duk_pop_n(ctx, 2); +} + +DUK_EXTERNAL void duk_pop_3(duk_context *ctx) { + DUK_ASSERT_CTX_VALID(ctx); + duk_pop_n(ctx, 3); +} + +/* + * Error throwing + */ + +DUK_EXTERNAL void duk_throw(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT(thr->valstack_bottom >= thr->valstack); + DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom); + DUK_ASSERT(thr->valstack_end >= thr->valstack_top); + + if (thr->valstack_top == thr->valstack_bottom) { + DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS); + } + + /* Errors are augmented when they are created, not when they are + * thrown or re-thrown. The current error handler, however, runs + * just before an error is thrown. + */ + + /* Sync so that augmentation sees up-to-date activations, NULL + * thr->ptr_curr_pc so that it's not used if side effects occur + * in augmentation or longjmp handling. + */ + duk_hthread_sync_and_null_currpc(thr); + +#if defined(DUK_USE_AUGMENT_ERROR_THROW) + DUK_DDD(DUK_DDDPRINT("THROW ERROR (API): %!dT (before throw augment)", (duk_tval *) duk_get_tval(ctx, -1))); + duk_err_augment_error_throw(thr); +#endif + DUK_DDD(DUK_DDDPRINT("THROW ERROR (API): %!dT (after throw augment)", (duk_tval *) duk_get_tval(ctx, -1))); + + duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_THROW); + + /* thr->heap->lj.jmpbuf_ptr is checked by duk_err_longjmp() so we don't + * need to check that here. If the value is NULL, a panic occurs because + * we can't return. + */ + + duk_err_longjmp(thr); + DUK_UNREACHABLE(); +} + +DUK_EXTERNAL void duk_fatal(duk_context *ctx, duk_errcode_t err_code, const char *err_msg) { + duk_hthread *thr = (duk_hthread *) ctx; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(thr != NULL); + DUK_ASSERT(thr->heap != NULL); + DUK_ASSERT(thr->heap->fatal_func != NULL); + + DUK_D(DUK_DPRINT("fatal error occurred, code %ld, message %s", + (long) err_code, (const char *) err_msg)); + + /* fatal_func should be noreturn, but noreturn declarations on function + * pointers has a very spotty support apparently so it's not currently + * done. + */ + thr->heap->fatal_func(ctx, err_code, err_msg); + + DUK_PANIC(DUK_ERR_API_ERROR, "fatal handler returned"); +} + +DUK_EXTERNAL void duk_error_va_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, va_list ap) { + DUK_ASSERT_CTX_VALID(ctx); + + duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap); + duk_throw(ctx); +} + +DUK_EXTERNAL void duk_error_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, ...) { + va_list ap; + + DUK_ASSERT_CTX_VALID(ctx); + + va_start(ap, fmt); + duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap); + va_end(ap); + duk_throw(ctx); +} + +#if !defined(DUK_USE_VARIADIC_MACROS) +DUK_EXTERNAL void duk_error_stash(duk_context *ctx, duk_errcode_t err_code, const char *fmt, ...) { + const char *filename; + duk_int_t line; + va_list ap; + + DUK_ASSERT_CTX_VALID(ctx); + + filename = duk_api_global_filename; + line = duk_api_global_line; + duk_api_global_filename = NULL; + duk_api_global_line = 0; + + va_start(ap, fmt); + duk_push_error_object_va_raw(ctx, err_code, filename, line, fmt, ap); + va_end(ap); + duk_throw(ctx); +} +#endif /* DUK_USE_VARIADIC_MACROS */ + +/* + * Comparison + */ + +DUK_EXTERNAL duk_bool_t duk_equals(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv1, *tv2; + + DUK_ASSERT_CTX_VALID(ctx); + + tv1 = duk_get_tval(ctx, index1); + tv2 = duk_get_tval(ctx, index2); + if ((tv1 == NULL) || (tv2 == NULL)) { + return 0; + } + + /* Coercion may be needed, the helper handles that by pushing the + * tagged values to the stack. + */ + return duk_js_equals(thr, tv1, tv2); +} + +DUK_EXTERNAL duk_bool_t duk_strict_equals(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) { + duk_tval *tv1, *tv2; + + DUK_ASSERT_CTX_VALID(ctx); + + tv1 = duk_get_tval(ctx, index1); + tv2 = duk_get_tval(ctx, index2); + if ((tv1 == NULL) || (tv2 == NULL)) { + return 0; + } + + /* No coercions or other side effects, so safe */ + return duk_js_strict_equals(tv1, tv2); +} + +/* + * instanceof + */ + +DUK_EXTERNAL duk_bool_t duk_instanceof(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) { + duk_tval *tv1, *tv2; + + DUK_ASSERT_CTX_VALID(ctx); + + /* Index validation is strict, which differs from duk_equals(). + * The strict behavior mimics how instanceof itself works, e.g. + * it is a TypeError if rval is not a -callable- object. It would + * be somewhat inconsistent if rval would be allowed to be + * non-existent without a TypeError. + */ + tv1 = duk_require_tval(ctx, index1); + DUK_ASSERT(tv1 != NULL); + tv2 = duk_require_tval(ctx, index2); + DUK_ASSERT(tv2 != NULL); + + return duk_js_instanceof((duk_hthread *) ctx, tv1, tv2); +} + +/* + * Lightfunc + */ + +DUK_INTERNAL void duk_push_lightfunc_name(duk_context *ctx, duk_tval *tv) { + duk_c_function func; + + DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv)); + + /* Lightfunc name, includes Duktape/C native function pointer, which + * can often be used to locate the function from a symbol table. + * The name also includes the 16-bit duk_tval flags field because it + * includes the magic value. Because a single native function often + * provides different functionality depending on the magic value, it + * seems reasonably to include it in the name. + * + * On the other hand, a complicated name increases string table + * pressure in low memory environments (but only when function name + * is accessed). + */ + + func = DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv); + duk_push_sprintf(ctx, "light_"); + duk_push_string_funcptr(ctx, (duk_uint8_t *) &func, sizeof(func)); + duk_push_sprintf(ctx, "_%04x", (unsigned int) DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv)); + duk_concat(ctx, 3); +} + +DUK_INTERNAL void duk_push_lightfunc_tostring(duk_context *ctx, duk_tval *tv) { + DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv)); + + duk_push_string(ctx, "function "); + duk_push_lightfunc_name(ctx, tv); + duk_push_string(ctx, "() {\"light\"}"); + duk_concat(ctx, 3); +} + +/* + * Function pointers + * + * Printing function pointers is non-portable, so we do that by hex printing + * bytes from memory. + */ + +DUK_INTERNAL void duk_push_string_funcptr(duk_context *ctx, duk_uint8_t *ptr, duk_size_t sz) { + duk_uint8_t buf[32 * 2]; + duk_uint8_t *p, *q; + duk_small_uint_t i; + duk_small_uint_t t; + + DUK_ASSERT(sz <= 32); /* sanity limit for function pointer size */ + + p = buf; +#if defined(DUK_USE_INTEGER_LE) + q = ptr + sz; +#else + q = ptr; +#endif + for (i = 0; i < sz; i++) { +#if defined(DUK_USE_INTEGER_LE) + t = *(--q); +#else + t = *(q++); +#endif + *p++ = duk_lc_digits[t >> 4]; + *p++ = duk_lc_digits[t & 0x0f]; + } + + duk_push_lstring(ctx, (const char *) buf, sz * 2); +} + +#if !defined(DUK_USE_PARANOID_ERRORS) +/* + * Push readable string summarizing duk_tval. The operation is side effect + * free and will only throw from internal errors (e.g. out of memory). + * This is used by e.g. property access code to summarize a key/base safely, + * and is not intended to be fast (but small and safe). + */ + +#define DUK__READABLE_STRING_MAXCHARS 32 + +/* String sanitizer which escapes ASCII control characters and a few other + * ASCII characters, passes Unicode as is, and replaces invalid UTF-8 with + * question marks. No errors are thrown for any input string, except in out + * of memory situations. + */ +DUK_LOCAL void duk__push_hstring_readable_unicode(duk_context *ctx, duk_hstring *h_input) { + duk_hthread *thr; + const duk_uint8_t *p, *p_start, *p_end; + duk_uint8_t buf[DUK_UNICODE_MAX_XUTF8_LENGTH * DUK__READABLE_STRING_MAXCHARS + + 2 /*quotes*/ + 3 /*periods*/]; + duk_uint8_t *q; + duk_ucodepoint_t cp; + duk_small_uint_t nchars; + + DUK_ASSERT_CTX_VALID(ctx); + DUK_ASSERT(h_input != NULL); + thr = (duk_hthread *) ctx; + + p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input); + p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input); + p = p_start; + q = buf; + + nchars = 0; + *q++ = (duk_uint8_t) DUK_ASC_SINGLEQUOTE; + for (;;) { + if (p >= p_end) { + break; + } + if (nchars == DUK__READABLE_STRING_MAXCHARS) { + *q++ = (duk_uint8_t) DUK_ASC_PERIOD; + *q++ = (duk_uint8_t) DUK_ASC_PERIOD; + *q++ = (duk_uint8_t) DUK_ASC_PERIOD; + break; + } + if (duk_unicode_decode_xutf8(thr, &p, p_start, p_end, &cp)) { + if (cp < 0x20 || cp == 0x7f || cp == DUK_ASC_SINGLEQUOTE || cp == DUK_ASC_BACKSLASH) { + DUK_ASSERT(DUK_UNICODE_MAX_XUTF8_LENGTH >= 4); /* estimate is valid */ + DUK_ASSERT((cp >> 4) <= 0x0f); + *q++ = (duk_uint8_t) DUK_ASC_BACKSLASH; + *q++ = (duk_uint8_t) DUK_ASC_LC_X; + *q++ = (duk_uint8_t) duk_lc_digits[cp >> 4]; + *q++ = (duk_uint8_t) duk_lc_digits[cp & 0x0f]; + } else { + q += duk_unicode_encode_xutf8(cp, q); + } + } else { + p++; /* advance manually */ + *q++ = (duk_uint8_t) DUK_ASC_QUESTION; + } + nchars++; + } + *q++ = (duk_uint8_t) DUK_ASC_SINGLEQUOTE; + + duk_push_lstring(ctx, (const char *) buf, (duk_size_t) (q - buf)); +} + +DUK_INTERNAL const char *duk_push_string_tval_readable(duk_context *ctx, duk_tval *tv) { + duk_hthread *thr; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + if (tv == NULL) { + duk_push_string(ctx, "none"); + } else { + switch (DUK_TVAL_GET_TAG(tv)) { + case DUK_TAG_STRING: { + duk__push_hstring_readable_unicode(ctx, DUK_TVAL_GET_STRING(tv)); + break; + } + case DUK_TAG_OBJECT: { + duk_hobject *h = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h != NULL); + duk_push_hobject_class_string(ctx, h); + break; + } + case DUK_TAG_BUFFER: { + /* XXX: Hex encoded, length limited buffer summary here? */ + duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv); + DUK_ASSERT(h != NULL); + duk_push_sprintf(ctx, "[buffer:%ld]", (long) DUK_HBUFFER_GET_SIZE(h)); + break; + } + case DUK_TAG_POINTER: { + /* Surround with parentheses like in JX, ensures NULL pointer + * is distinguishable from null value ("(null)" vs "null"). + */ + duk_push_tval(ctx, tv); + duk_push_sprintf(ctx, "(%s)", duk_to_string(ctx, -1)); + duk_remove(ctx, -2); + break; + } + default: { + duk_push_tval(ctx, tv); + break; + } + } + } + + return duk_to_string(ctx, -1); +} + +DUK_INTERNAL const char *duk_push_string_readable(duk_context *ctx, duk_idx_t index) { + DUK_ASSERT_CTX_VALID(ctx); + return duk_push_string_tval_readable(ctx, duk_get_tval(ctx, index)); +} +#endif /* !DUK_USE_PARANOID_ERRORS */ + +#undef DUK__CHECK_SPACE +#undef DUK__PACK_ARGS +#undef DUK__READABLE_STRING_MAXCHARS +/* + * String manipulation + */ + +/* include removed: duk_internal.h */ + +DUK_LOCAL void duk__concat_and_join_helper(duk_context *ctx, duk_idx_t count_in, duk_bool_t is_join) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_uint_t count; + duk_uint_t i; + duk_size_t idx; + duk_size_t len; + duk_hstring *h; + duk_uint8_t *buf; + + DUK_ASSERT_CTX_VALID(ctx); + + if (DUK_UNLIKELY(count_in <= 0)) { + if (count_in < 0) { + DUK_ERROR_API(thr, DUK_STR_INVALID_COUNT); + return; + } + DUK_ASSERT(count_in == 0); + duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING); + return; + } + count = (duk_uint_t) count_in; + + if (is_join) { + duk_size_t t1, t2, limit; + h = duk_to_hstring(ctx, -((duk_idx_t) count) - 1); + DUK_ASSERT(h != NULL); + + /* A bit tricky overflow test, see doc/code-issues.rst. */ + t1 = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h); + t2 = (duk_size_t) (count - 1); + limit = (duk_size_t) DUK_HSTRING_MAX_BYTELEN; + if (DUK_UNLIKELY(t2 != 0 && t1 > limit / t2)) { + /* Combined size of separators already overflows */ + goto error_overflow; + } + len = (duk_size_t) (t1 * t2); + } else { + len = (duk_size_t) 0; + } + + for (i = count; i >= 1; i--) { + duk_size_t new_len; + duk_to_string(ctx, -((duk_idx_t) i)); + h = duk_require_hstring(ctx, -((duk_idx_t) i)); + new_len = len + (duk_size_t) DUK_HSTRING_GET_BYTELEN(h); + + /* Impose a string maximum length, need to handle overflow + * correctly. + */ + if (new_len < len || /* wrapped */ + new_len > (duk_size_t) DUK_HSTRING_MAX_BYTELEN) { + goto error_overflow; + } + len = new_len; + } + + DUK_DDD(DUK_DDDPRINT("join/concat %lu strings, total length %lu bytes", + (unsigned long) count, (unsigned long) len)); + + /* use stack allocated buffer to ensure reachability in errors (e.g. intern error) */ + buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, len); + DUK_ASSERT(buf != NULL); + + /* [... (sep) str1 str2 ... strN buf] */ + + idx = 0; + for (i = count; i >= 1; i--) { + if (is_join && i != count) { + h = duk_require_hstring(ctx, -((duk_idx_t) count) - 2); /* extra -1 for buffer */ + DUK_MEMCPY(buf + idx, DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h)); + idx += DUK_HSTRING_GET_BYTELEN(h); + } + h = duk_require_hstring(ctx, -((duk_idx_t) i) - 1); /* extra -1 for buffer */ + DUK_MEMCPY(buf + idx, DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h)); + idx += DUK_HSTRING_GET_BYTELEN(h); + } + + DUK_ASSERT(idx == len); + + /* [... (sep) str1 str2 ... strN buf] */ + + /* get rid of the strings early to minimize memory use before intern */ + + if (is_join) { + duk_replace(ctx, -((duk_idx_t) count) - 2); /* overwrite sep */ + duk_pop_n(ctx, count); + } else { + duk_replace(ctx, -((duk_idx_t) count) - 1); /* overwrite str1 */ + duk_pop_n(ctx, count-1); + } + + /* [... buf] */ + + (void) duk_to_string(ctx, -1); + + /* [... res] */ + return; + + error_overflow: + DUK_ERROR_RANGE(thr, DUK_STR_CONCAT_RESULT_TOO_LONG); +} + +DUK_EXTERNAL void duk_concat(duk_context *ctx, duk_idx_t count) { + DUK_ASSERT_CTX_VALID(ctx); + + duk__concat_and_join_helper(ctx, count, 0 /*is_join*/); +} + +DUK_EXTERNAL void duk_join(duk_context *ctx, duk_idx_t count) { + DUK_ASSERT_CTX_VALID(ctx); + + duk__concat_and_join_helper(ctx, count, 1 /*is_join*/); +} + +/* XXX: could map/decode be unified with duk_unicode_support.c code? + * Case conversion needs also the character surroundings though. + */ + +DUK_EXTERNAL void duk_decode_string(duk_context *ctx, duk_idx_t index, duk_decode_char_function callback, void *udata) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hstring *h_input; + const duk_uint8_t *p, *p_start, *p_end; + duk_codepoint_t cp; + + DUK_ASSERT_CTX_VALID(ctx); + + h_input = duk_require_hstring(ctx, index); + DUK_ASSERT(h_input != NULL); + + p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input); + p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input); + p = p_start; + + for (;;) { + if (p >= p_end) { + break; + } + cp = (int) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end); + callback(udata, cp); + } +} + +DUK_EXTERNAL void duk_map_string(duk_context *ctx, duk_idx_t index, duk_map_char_function callback, void *udata) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hstring *h_input; + duk_bufwriter_ctx bw_alloc; + duk_bufwriter_ctx *bw; + const duk_uint8_t *p, *p_start, *p_end; + duk_codepoint_t cp; + + DUK_ASSERT_CTX_VALID(ctx); + + index = duk_normalize_index(ctx, index); + + h_input = duk_require_hstring(ctx, index); + DUK_ASSERT(h_input != NULL); + + bw = &bw_alloc; + DUK_BW_INIT_PUSHBUF(thr, bw, DUK_HSTRING_GET_BYTELEN(h_input)); /* reasonable output estimate */ + + p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input); + p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input); + p = p_start; + + for (;;) { + /* XXX: could write output in chunks with fewer ensure calls, + * but relative benefit would be small here. + */ + + if (p >= p_end) { + break; + } + cp = (int) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end); + cp = callback(udata, cp); + + DUK_BW_WRITE_ENSURE_XUTF8(thr, bw, cp); + } + + DUK_BW_COMPACT(thr, bw); + duk_to_string(ctx, -1); + duk_replace(ctx, index); +} + +DUK_EXTERNAL void duk_substring(duk_context *ctx, duk_idx_t index, duk_size_t start_offset, duk_size_t end_offset) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hstring *h; + duk_hstring *res; + duk_size_t start_byte_offset; + duk_size_t end_byte_offset; + + DUK_ASSERT_CTX_VALID(ctx); + + index = duk_require_normalize_index(ctx, index); + h = duk_require_hstring(ctx, index); + DUK_ASSERT(h != NULL); + + if (end_offset >= DUK_HSTRING_GET_CHARLEN(h)) { + end_offset = DUK_HSTRING_GET_CHARLEN(h); + } + if (start_offset > end_offset) { + start_offset = end_offset; + } + + DUK_ASSERT_DISABLE(start_offset >= 0); + DUK_ASSERT(start_offset <= end_offset && start_offset <= DUK_HSTRING_GET_CHARLEN(h)); + DUK_ASSERT_DISABLE(end_offset >= 0); + DUK_ASSERT(end_offset >= start_offset && end_offset <= DUK_HSTRING_GET_CHARLEN(h)); + + /* guaranteed by string limits */ + DUK_ASSERT(start_offset <= DUK_UINT32_MAX); + DUK_ASSERT(end_offset <= DUK_UINT32_MAX); + + start_byte_offset = (duk_size_t) duk_heap_strcache_offset_char2byte(thr, h, (duk_uint_fast32_t) start_offset); + end_byte_offset = (duk_size_t) duk_heap_strcache_offset_char2byte(thr, h, (duk_uint_fast32_t) end_offset); + + DUK_ASSERT(end_byte_offset >= start_byte_offset); + DUK_ASSERT(end_byte_offset - start_byte_offset <= DUK_UINT32_MAX); /* guaranteed by string limits */ + + /* no size check is necessary */ + res = duk_heap_string_intern_checked(thr, + DUK_HSTRING_GET_DATA(h) + start_byte_offset, + (duk_uint32_t) (end_byte_offset - start_byte_offset)); + + duk_push_hstring(ctx, res); + duk_replace(ctx, index); +} + +/* XXX: this is quite clunky. Add Unicode helpers to scan backwards and + * forwards with a callback to process codepoints? + */ +DUK_EXTERNAL void duk_trim(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hstring *h; + const duk_uint8_t *p, *p_start, *p_end, *p_tmp1, *p_tmp2; /* pointers for scanning */ + const duk_uint8_t *q_start, *q_end; /* start (incl) and end (excl) of trimmed part */ + duk_codepoint_t cp; + + DUK_ASSERT_CTX_VALID(ctx); + + index = duk_require_normalize_index(ctx, index); + h = duk_require_hstring(ctx, index); + DUK_ASSERT(h != NULL); + + p_start = DUK_HSTRING_GET_DATA(h); + p_end = p_start + DUK_HSTRING_GET_BYTELEN(h); + + p = p_start; + while (p < p_end) { + p_tmp1 = p; + cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &p_tmp1, p_start, p_end); + if (!(duk_unicode_is_whitespace(cp) || duk_unicode_is_line_terminator(cp))) { + break; + } + p = p_tmp1; + } + q_start = p; + if (p == p_end) { + /* entire string is whitespace */ + q_end = p; + goto scan_done; + } + + p = p_end; + while (p > p_start) { + p_tmp1 = p; + while (p > p_start) { + p--; + if (((*p) & 0xc0) != 0x80) { + break; + } + } + p_tmp2 = p; + + cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &p_tmp2, p_start, p_end); + if (!(duk_unicode_is_whitespace(cp) || duk_unicode_is_line_terminator(cp))) { + p = p_tmp1; + break; + } + } + q_end = p; + + scan_done: + /* This may happen when forward and backward scanning disagree + * (possible for non-extended-UTF-8 strings). + */ + if (q_end < q_start) { + q_end = q_start; + } + + DUK_ASSERT(q_start >= p_start && q_start <= p_end); + DUK_ASSERT(q_end >= p_start && q_end <= p_end); + DUK_ASSERT(q_end >= q_start); + + DUK_DDD(DUK_DDDPRINT("trim: p_start=%p, p_end=%p, q_start=%p, q_end=%p", + (const void *) p_start, (const void *) p_end, + (const void *) q_start, (const void *) q_end)); + + if (q_start == p_start && q_end == p_end) { + DUK_DDD(DUK_DDDPRINT("nothing was trimmed: avoid interning (hashing etc)")); + return; + } + + duk_push_lstring(ctx, (const char *) q_start, (duk_size_t) (q_end - q_start)); + duk_replace(ctx, index); +} + +DUK_EXTERNAL duk_codepoint_t duk_char_code_at(duk_context *ctx, duk_idx_t index, duk_size_t char_offset) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hstring *h; + duk_ucodepoint_t cp; + + DUK_ASSERT_CTX_VALID(ctx); + + h = duk_require_hstring(ctx, index); + DUK_ASSERT(h != NULL); + + DUK_ASSERT_DISABLE(char_offset >= 0); /* always true, arg is unsigned */ + if (char_offset >= DUK_HSTRING_GET_CHARLEN(h)) { + return 0; + } + + DUK_ASSERT(char_offset <= DUK_UINT_MAX); /* guaranteed by string limits */ + cp = duk_hstring_char_code_at_raw(thr, h, (duk_uint_t) char_offset); + return (duk_codepoint_t) cp; +} +/* + * Variable access + */ + +/* include removed: duk_internal.h */ + +DUK_EXTERNAL void duk_get_var(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_activation *act; + duk_hstring *h_varname; + duk_small_int_t throw_flag = 1; /* always throw ReferenceError for unresolvable */ + + DUK_ASSERT_CTX_VALID(ctx); + + h_varname = duk_require_hstring(ctx, -1); /* XXX: tostring? */ + DUK_ASSERT(h_varname != NULL); + + act = duk_hthread_get_current_activation(thr); + if (act) { + (void) duk_js_getvar_activation(thr, act, h_varname, throw_flag); /* -> [ ... varname val this ] */ + } else { + /* Outside any activation -> look up from global. */ + DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL_ENV] != NULL); + (void) duk_js_getvar_envrec(thr, thr->builtins[DUK_BIDX_GLOBAL_ENV], h_varname, throw_flag); + } + + /* [ ... varname val this ] (because throw_flag == 1, always resolved) */ + + duk_pop(ctx); + duk_remove(ctx, -2); + + /* [ ... val ] */ + + /* Return value would be pointless: because throw_flag==1, we always + * throw if the identifier doesn't resolve. + */ + return; +} + +DUK_EXTERNAL void duk_put_var(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_activation *act; + duk_hstring *h_varname; + duk_tval *tv_val; + duk_small_int_t throw_flag; + + DUK_ASSERT_CTX_VALID(ctx); + + h_varname = duk_require_hstring(ctx, -2); /* XXX: tostring? */ + DUK_ASSERT(h_varname != NULL); + + tv_val = duk_require_tval(ctx, -1); + + throw_flag = duk_is_strict_call(ctx); + + act = duk_hthread_get_current_activation(thr); + if (act) { + duk_js_putvar_activation(thr, act, h_varname, tv_val, throw_flag); /* -> [ ... varname val this ] */ + } else { + /* Outside any activation -> put to global. */ + DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL_ENV] != NULL); + duk_js_putvar_envrec(thr, thr->builtins[DUK_BIDX_GLOBAL_ENV], h_varname, tv_val, throw_flag); + } + + /* [ ... varname val ] */ + + duk_pop_2(ctx); + + /* [ ... ] */ + + return; +} + +DUK_EXTERNAL duk_bool_t duk_del_var(duk_context *ctx) { + DUK_ASSERT_CTX_VALID(ctx); + + DUK_ERROR_UNIMPLEMENTED_DEFMSG((duk_hthread *) ctx); + return 0; +} + +DUK_EXTERNAL duk_bool_t duk_has_var(duk_context *ctx) { + DUK_ASSERT_CTX_VALID(ctx); + + DUK_ERROR_UNIMPLEMENTED_DEFMSG((duk_hthread *) ctx); + return 0; +} +/* + * Array built-ins + * + * Note that most Array built-ins are intentionally generic and work even + * when the 'this' binding is not an Array instance. To ensure this, + * Array algorithms do not assume "magical" Array behavior for the "length" + * property, for instance. + * + * XXX: the "Throw" flag should be set for (almost?) all [[Put]] and + * [[Delete]] operations, but it's currently false throughout. Go through + * all put/delete cases and check throw flag use. Need a new API primitive + * which allows throws flag to be specified. + * + * XXX: array lengths above 2G won't work reliably. There are many places + * where one needs a full signed 32-bit range ([-0xffffffff, 0xffffffff], + * i.e. -33- bits). Although array 'length' cannot be written to be outside + * the unsigned 32-bit range (E5.1 Section 15.4.5.1 throws a RangeError if so) + * some intermediate values may be above 0xffffffff and this may not be always + * correctly handled now (duk_uint32_t is not enough for all algorithms). + * + * For instance, push() can legitimately write entries beyond length 0xffffffff + * and cause a RangeError only at the end. To do this properly, the current + * push() implementation tracks the array index using a 'double' instead of a + * duk_uint32_t (which is somewhat awkward). See test-bi-array-push-maxlen.js. + * + * On using "put" vs. "def" prop + * ============================= + * + * Code below must be careful to use the appropriate primitive as it matters + * for compliance. When using "put" there may be inherited properties in + * Array.prototype which cause side effects when values are written. When + * using "define" there are no such side effects, and many test262 test cases + * check for this (for real world code, such side effects are very rare). + * Both "put" and "define" are used in the E5.1 specification; as a rule, + * "put" is used when modifying an existing array (or a non-array 'this' + * binding) and "define" for setting values into a fresh result array. + * + * Also note that Array instance 'length' should be writable, but not + * enumerable and definitely not configurable: even Duktape code internally + * assumes that an Array instance will always have a 'length' property. + * Preventing deletion of the property is critical. + */ + +/* include removed: duk_internal.h */ + +/* Perform an intermediate join when this many elements have been pushed + * on the value stack. + */ +#define DUK__ARRAY_MID_JOIN_LIMIT 4096 + +/* Shared entry code for many Array built-ins. Note that length is left + * on stack (it could be popped, but that's not necessary). + */ +DUK_LOCAL duk_uint32_t duk__push_this_obj_len_u32(duk_context *ctx) { + duk_uint32_t len; + + (void) duk_push_this_coercible_to_object(ctx); + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LENGTH); + len = duk_to_uint32(ctx, -1); + + /* -> [ ... ToObject(this) ToUint32(length) ] */ + return len; +} + +DUK_LOCAL duk_uint32_t duk__push_this_obj_len_u32_limited(duk_context *ctx) { + /* Range limited to [0, 0x7fffffff] range, i.e. range that can be + * represented with duk_int32_t. Use this when the method doesn't + * handle the full 32-bit unsigned range correctly. + */ + duk_uint32_t ret = duk__push_this_obj_len_u32(ctx); + if (DUK_UNLIKELY(ret >= 0x80000000UL)) { + DUK_ERROR_RANGE((duk_hthread *) ctx, DUK_STR_ARRAY_LENGTH_OVER_2G); + } + return ret; +} + +/* + * Constructor + */ + +DUK_INTERNAL duk_ret_t duk_bi_array_constructor(duk_context *ctx) { + duk_idx_t nargs; + duk_double_t d; + duk_uint32_t len; + duk_idx_t i; + + nargs = duk_get_top(ctx); + duk_push_array(ctx); + + if (nargs == 1 && duk_is_number(ctx, 0)) { + /* XXX: expensive check (also shared elsewhere - so add a shared internal API call?) */ + d = duk_get_number(ctx, 0); + len = duk_to_uint32(ctx, 0); + if (((duk_double_t) len) != d) { + return DUK_RET_RANGE_ERROR; + } + + /* XXX: if 'len' is low, may want to ensure array part is kept: + * the caller is likely to want a dense array. + */ + duk_push_u32(ctx, len); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); /* [ ToUint32(len) array ToUint32(len) ] -> [ ToUint32(len) array ] */ + return 1; + } + + /* XXX: optimize by creating array into correct size directly, and + * operating on the array part directly; values can be memcpy()'d from + * value stack directly as long as refcounts are increased. + */ + for (i = 0; i < nargs; i++) { + duk_dup(ctx, i); + duk_xdef_prop_index_wec(ctx, -2, (duk_uarridx_t) i); + } + + duk_push_u32(ctx, (duk_uint32_t) nargs); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); + return 1; +} + +/* + * isArray() + */ + +DUK_INTERNAL duk_ret_t duk_bi_array_constructor_is_array(duk_context *ctx) { + duk_hobject *h; + + h = duk_get_hobject_with_class(ctx, 0, DUK_HOBJECT_CLASS_ARRAY); + duk_push_boolean(ctx, (h != NULL)); + return 1; +} + +/* + * toString() + */ + +DUK_INTERNAL duk_ret_t duk_bi_array_prototype_to_string(duk_context *ctx) { + (void) duk_push_this_coercible_to_object(ctx); + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_JOIN); + + /* [ ... this func ] */ + if (!duk_is_callable(ctx, -1)) { + /* Fall back to the initial (original) Object.toString(). We don't + * currently have pointers to the built-in functions, only the top + * level global objects (like "Array") so this is now done in a bit + * of a hacky manner. It would be cleaner to push the (original) + * function and use duk_call_method(). + */ + + /* XXX: 'this' will be ToObject() coerced twice, which is incorrect + * but should have no visible side effects. + */ + DUK_DDD(DUK_DDDPRINT("this.join is not callable, fall back to (original) Object.toString")); + duk_set_top(ctx, 0); + return duk_bi_object_prototype_to_string(ctx); /* has access to 'this' binding */ + } + + /* [ ... this func ] */ + + duk_insert(ctx, -2); + + /* [ ... func this ] */ + + DUK_DDD(DUK_DDDPRINT("calling: func=%!iT, this=%!iT", + (duk_tval *) duk_get_tval(ctx, -2), + (duk_tval *) duk_get_tval(ctx, -1))); + duk_call_method(ctx, 0); + + return 1; +} + +/* + * concat() + */ + +DUK_INTERNAL duk_ret_t duk_bi_array_prototype_concat(duk_context *ctx) { + duk_idx_t i, n; + duk_uarridx_t idx, idx_last; + duk_uarridx_t j, len; + duk_hobject *h; + + /* XXX: the insert here is a bit expensive if there are a lot of items. + * It could also be special cased in the outermost for loop quite easily + * (as the element is dup()'d anyway). + */ + + (void) duk_push_this_coercible_to_object(ctx); + duk_insert(ctx, 0); + n = duk_get_top(ctx); + duk_push_array(ctx); /* -> [ ToObject(this) item1 ... itemN arr ] */ + + /* NOTE: The Array special behaviors are NOT invoked by duk_xdef_prop_index() + * (which differs from the official algorithm). If no error is thrown, this + * doesn't matter as the length is updated at the end. However, if an error + * is thrown, the length will be unset. That shouldn't matter because the + * caller won't get a reference to the intermediate value. + */ + + idx = 0; + idx_last = 0; + for (i = 0; i < n; i++) { + DUK_ASSERT_TOP(ctx, n + 1); + + /* [ ToObject(this) item1 ... itemN arr ] */ + + duk_dup(ctx, i); + h = duk_get_hobject_with_class(ctx, -1, DUK_HOBJECT_CLASS_ARRAY); + if (!h) { + duk_xdef_prop_index_wec(ctx, -2, idx++); + idx_last = idx; + continue; + } + + /* [ ToObject(this) item1 ... itemN arr item(i) ] */ + + /* XXX: an array can have length higher than 32 bits; this is not handled + * correctly now. + */ + len = (duk_uarridx_t) duk_get_length(ctx, -1); + for (j = 0; j < len; j++) { + if (duk_get_prop_index(ctx, -1, j)) { + /* [ ToObject(this) item1 ... itemN arr item(i) item(i)[j] ] */ + duk_xdef_prop_index_wec(ctx, -3, idx++); + idx_last = idx; + } else { + idx++; + duk_pop(ctx); +#if defined(DUK_USE_NONSTD_ARRAY_CONCAT_TRAILER) + /* According to E5.1 Section 15.4.4.4 nonexistent trailing + * elements do not affect 'length' of the result. Test262 + * and other engines disagree, so update idx_last here too. + */ + idx_last = idx; +#else + /* Strict standard behavior, ignore trailing elements for + * result 'length'. + */ +#endif + } + } + duk_pop(ctx); + } + + /* The E5.1 Section 15.4.4.4 algorithm doesn't set the length explicitly + * in the end, but because we're operating with an internal value which + * is known to be an array, this should be equivalent. + */ + duk_push_uarridx(ctx, idx_last); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); + + DUK_ASSERT_TOP(ctx, n + 1); + return 1; +} + +/* + * join(), toLocaleString() + * + * Note: checking valstack is necessary, but only in the per-element loop. + * + * Note: the trivial approach of pushing all the elements on the value stack + * and then calling duk_join() fails when the array contains a large number + * of elements. This problem can't be offloaded to duk_join() because the + * elements to join must be handled here and have special handling. Current + * approach is to do intermediate joins with very large number of elements. + * There is no fancy handling; the prefix gets re-joined multiple times. + */ + +DUK_INTERNAL duk_ret_t duk_bi_array_prototype_join_shared(duk_context *ctx) { + duk_uint32_t len, count; + duk_uint32_t idx; + duk_small_int_t to_locale_string = duk_get_current_magic(ctx); + duk_idx_t valstack_required; + + /* For join(), nargs is 1. For toLocaleString(), nargs is 0 and + * setting the top essentially pushes an undefined to the stack, + * thus defaulting to a comma separator. + */ + duk_set_top(ctx, 1); + if (duk_is_undefined(ctx, 0)) { + duk_pop(ctx); + duk_push_hstring_stridx(ctx, DUK_STRIDX_COMMA); + } else { + duk_to_string(ctx, 0); + } + + len = duk__push_this_obj_len_u32(ctx); + + /* [ sep ToObject(this) len ] */ + + DUK_DDD(DUK_DDDPRINT("sep=%!T, this=%!T, len=%lu", + (duk_tval *) duk_get_tval(ctx, 0), + (duk_tval *) duk_get_tval(ctx, 1), + (unsigned long) len)); + + /* The extra (+4) is tight. */ + valstack_required = (len >= DUK__ARRAY_MID_JOIN_LIMIT ? + DUK__ARRAY_MID_JOIN_LIMIT : len) + 4; + duk_require_stack(ctx, valstack_required); + + duk_dup(ctx, 0); + + /* [ sep ToObject(this) len sep ] */ + + count = 0; + idx = 0; + for (;;) { + if (count >= DUK__ARRAY_MID_JOIN_LIMIT || /* intermediate join to avoid valstack overflow */ + idx >= len) { /* end of loop (careful with len==0) */ + /* [ sep ToObject(this) len sep str0 ... str(count-1) ] */ + DUK_DDD(DUK_DDDPRINT("mid/final join, count=%ld, idx=%ld, len=%ld", + (long) count, (long) idx, (long) len)); + duk_join(ctx, (duk_idx_t) count); /* -> [ sep ToObject(this) len str ] */ + duk_dup(ctx, 0); /* -> [ sep ToObject(this) len str sep ] */ + duk_insert(ctx, -2); /* -> [ sep ToObject(this) len sep str ] */ + count = 1; + } + if (idx >= len) { + /* if true, the stack already contains the final result */ + break; + } + + duk_get_prop_index(ctx, 1, (duk_uarridx_t) idx); + if (duk_is_null_or_undefined(ctx, -1)) { + duk_pop(ctx); + duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING); + } else { + if (to_locale_string) { + duk_to_object(ctx, -1); + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_TO_LOCALE_STRING); + duk_insert(ctx, -2); /* -> [ ... toLocaleString ToObject(val) ] */ + duk_call_method(ctx, 0); + duk_to_string(ctx, -1); + } else { + duk_to_string(ctx, -1); + } + } + + count++; + idx++; + } + + /* [ sep ToObject(this) len sep result ] */ + + return 1; +} + +/* + * pop(), push() + */ + +DUK_INTERNAL duk_ret_t duk_bi_array_prototype_pop(duk_context *ctx) { + duk_uint32_t len; + duk_uint32_t idx; + + DUK_ASSERT_TOP(ctx, 0); + len = duk__push_this_obj_len_u32(ctx); + if (len == 0) { + duk_push_int(ctx, 0); + duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH); + return 0; + } + idx = len - 1; + + duk_get_prop_index(ctx, 0, (duk_uarridx_t) idx); + duk_del_prop_index(ctx, 0, (duk_uarridx_t) idx); + duk_push_u32(ctx, idx); + duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH); + return 1; +} + +DUK_INTERNAL duk_ret_t duk_bi_array_prototype_push(duk_context *ctx) { + /* Note: 'this' is not necessarily an Array object. The push() + * algorithm is supposed to work for other kinds of objects too, + * so the algorithm has e.g. an explicit update for the 'length' + * property which is normally "magical" in arrays. + */ + + duk_uint32_t len; + duk_idx_t i, n; + + n = duk_get_top(ctx); + len = duk__push_this_obj_len_u32(ctx); + + /* [ arg1 ... argN obj length ] */ + + /* Technically Array.prototype.push() can create an Array with length + * longer than 2^32-1, i.e. outside the 32-bit range. The final length + * is *not* wrapped to 32 bits in the specification. + * + * This implementation tracks length with a uint32 because it's much + * more practical. + * + * See: test-bi-array-push-maxlen.js. + */ + + if (len + (duk_uint32_t) n < len) { + DUK_D(DUK_DPRINT("Array.prototype.push() would go beyond 32-bit length, throw")); + return DUK_RET_RANGE_ERROR; + } + + for (i = 0; i < n; i++) { + duk_dup(ctx, i); + duk_put_prop_index(ctx, -3, len + i); + } + len += n; + + duk_push_u32(ctx, len); + duk_dup_top(ctx); + duk_put_prop_stridx(ctx, -4, DUK_STRIDX_LENGTH); + + /* [ arg1 ... argN obj length new_length ] */ + return 1; +} + +/* + * sort() + * + * Currently qsort with random pivot. This is now really, really slow, + * because there is no fast path for array parts. + * + * Signed indices are used because qsort() leaves and degenerate cases + * may use a negative offset. + */ + +DUK_LOCAL duk_small_int_t duk__array_sort_compare(duk_context *ctx, duk_int_t idx1, duk_int_t idx2) { + duk_bool_t have1, have2; + duk_bool_t undef1, undef2; + duk_small_int_t ret; + duk_idx_t idx_obj = 1; /* fixed offsets in valstack */ + duk_idx_t idx_fn = 0; + duk_hstring *h1, *h2; + + /* Fast exit if indices are identical. This is valid for a non-existent property, + * for an undefined value, and almost always for ToString() coerced comparison of + * arbitrary values (corner cases where this is not the case include e.g. a an + * object with varying ToString() coercion). + * + * The specification does not prohibit "caching" of values read from the array, so + * assuming equality for comparing an index with itself falls into the category of + * "caching". + * + * Also, compareFn may be inconsistent, so skipping a call to compareFn here may + * have an effect on the final result. The specification does not require any + * specific behavior for inconsistent compare functions, so again, this fast path + * is OK. + */ + + if (idx1 == idx2) { + DUK_DDD(DUK_DDDPRINT("duk__array_sort_compare: idx1=%ld, idx2=%ld -> indices identical, quick exit", + (long) idx1, (long) idx2)); + return 0; + } + + have1 = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) idx1); + have2 = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) idx2); + + DUK_DDD(DUK_DDDPRINT("duk__array_sort_compare: idx1=%ld, idx2=%ld, have1=%ld, have2=%ld, val1=%!T, val2=%!T", + (long) idx1, (long) idx2, (long) have1, (long) have2, + (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1))); + + if (have1) { + if (have2) { + ; + } else { + ret = -1; + goto pop_ret; + } + } else { + if (have2) { + ret = 1; + goto pop_ret; + } else { + ret = 0; + goto pop_ret; + } + } + + undef1 = duk_is_undefined(ctx, -2); + undef2 = duk_is_undefined(ctx, -1); + if (undef1) { + if (undef2) { + ret = 0; + goto pop_ret; + } else { + ret = 1; + goto pop_ret; + } + } else { + if (undef2) { + ret = -1; + goto pop_ret; + } else { + ; + } + } + + if (!duk_is_undefined(ctx, idx_fn)) { + duk_double_t d; + + /* no need to check callable; duk_call() will do that */ + duk_dup(ctx, idx_fn); /* -> [ ... x y fn ] */ + duk_insert(ctx, -3); /* -> [ ... fn x y ] */ + duk_call(ctx, 2); /* -> [ ... res ] */ + + /* The specification is a bit vague what to do if the return + * value is not a number. Other implementations seem to + * tolerate non-numbers but e.g. V8 won't apparently do a + * ToNumber(). + */ + + /* XXX: best behavior for real world compatibility? */ + + d = duk_to_number(ctx, -1); + if (d < 0.0) { + ret = -1; + } else if (d > 0.0) { + ret = 1; + } else { + ret = 0; + } + + duk_pop(ctx); + DUK_DDD(DUK_DDDPRINT("-> result %ld (from comparefn, after coercion)", (long) ret)); + return ret; + } + + /* string compare is the default (a bit oddly) */ + + h1 = duk_to_hstring(ctx, -2); + h2 = duk_to_hstring(ctx, -1); + DUK_ASSERT(h1 != NULL); + DUK_ASSERT(h2 != NULL); + + ret = duk_js_string_compare(h1, h2); /* retval is directly usable */ + goto pop_ret; + + pop_ret: + duk_pop_2(ctx); + DUK_DDD(DUK_DDDPRINT("-> result %ld", (long) ret)); + return ret; +} + +DUK_LOCAL void duk__array_sort_swap(duk_context *ctx, duk_int_t l, duk_int_t r) { + duk_bool_t have_l, have_r; + duk_idx_t idx_obj = 1; /* fixed offset in valstack */ + + if (l == r) { + return; + } + + /* swap elements; deal with non-existent elements correctly */ + have_l = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) l); + have_r = duk_get_prop_index(ctx, idx_obj, (duk_uarridx_t) r); + + if (have_r) { + /* right exists, [[Put]] regardless whether or not left exists */ + duk_put_prop_index(ctx, idx_obj, (duk_uarridx_t) l); + } else { + duk_del_prop_index(ctx, idx_obj, (duk_uarridx_t) l); + duk_pop(ctx); + } + + if (have_l) { + duk_put_prop_index(ctx, idx_obj, (duk_uarridx_t) r); + } else { + duk_del_prop_index(ctx, idx_obj, (duk_uarridx_t) r); + duk_pop(ctx); + } +} + +#if defined(DUK_USE_DDDPRINT) +/* Debug print which visualizes the qsort partitioning process. */ +DUK_LOCAL void duk__debuglog_qsort_state(duk_context *ctx, duk_int_t lo, duk_int_t hi, duk_int_t pivot) { + char buf[4096]; + char *ptr = buf; + duk_int_t i, n; + n = (duk_int_t) duk_get_length(ctx, 1); + if (n > 4000) { + n = 4000; + } + *ptr++ = '['; + for (i = 0; i < n; i++) { + if (i == pivot) { + *ptr++ = '|'; + } else if (i == lo) { + *ptr++ = '<'; + } else if (i == hi) { + *ptr++ = '>'; + } else if (i >= lo && i <= hi) { + *ptr++ = '-'; + } else { + *ptr++ = ' '; + } + } + *ptr++ = ']'; + *ptr++ = '\0'; + + DUK_DDD(DUK_DDDPRINT("%s (lo=%ld, hi=%ld, pivot=%ld)", + (const char *) buf, (long) lo, (long) hi, (long) pivot)); +} +#endif + +DUK_LOCAL void duk__array_qsort(duk_context *ctx, duk_int_t lo, duk_int_t hi) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_int_t p, l, r; + + /* The lo/hi indices may be crossed and hi < 0 is possible at entry. */ + + DUK_DDD(DUK_DDDPRINT("duk__array_qsort: lo=%ld, hi=%ld, obj=%!T", + (long) lo, (long) hi, (duk_tval *) duk_get_tval(ctx, 1))); + + DUK_ASSERT_TOP(ctx, 3); + + /* In some cases it may be that lo > hi, or hi < 0; these + * degenerate cases happen e.g. for empty arrays, and in + * recursion leaves. + */ + + /* trivial cases */ + if (hi - lo < 1) { + DUK_DDD(DUK_DDDPRINT("degenerate case, return immediately")); + return; + } + DUK_ASSERT(hi > lo); + DUK_ASSERT(hi - lo + 1 >= 2); + + /* randomized pivot selection */ + p = lo + (duk_util_tinyrandom_get_bits(thr, 30) % (hi - lo + 1)); /* rnd in [lo,hi] */ + DUK_ASSERT(p >= lo && p <= hi); + DUK_DDD(DUK_DDDPRINT("lo=%ld, hi=%ld, chose pivot p=%ld", + (long) lo, (long) hi, (long) p)); + + /* move pivot out of the way */ + duk__array_sort_swap(ctx, p, lo); + p = lo; + DUK_DDD(DUK_DDDPRINT("pivot moved out of the way: %!T", (duk_tval *) duk_get_tval(ctx, 1))); + + l = lo + 1; + r = hi; + for (;;) { + /* find elements to swap */ + for (;;) { + DUK_DDD(DUK_DDDPRINT("left scan: l=%ld, r=%ld, p=%ld", + (long) l, (long) r, (long) p)); + if (l >= hi) { + break; + } + if (duk__array_sort_compare(ctx, l, p) >= 0) { /* !(l < p) */ + break; + } + l++; + } + for (;;) { + DUK_DDD(DUK_DDDPRINT("right scan: l=%ld, r=%ld, p=%ld", + (long) l, (long) r, (long) p)); + if (r <= lo) { + break; + } + if (duk__array_sort_compare(ctx, p, r) >= 0) { /* !(p < r) */ + break; + } + r--; + } + if (l >= r) { + goto done; + } + DUK_ASSERT(l < r); + + DUK_DDD(DUK_DDDPRINT("swap %ld and %ld", (long) l, (long) r)); + + duk__array_sort_swap(ctx, l, r); + + DUK_DDD(DUK_DDDPRINT("after swap: %!T", (duk_tval *) duk_get_tval(ctx, 1))); + l++; + r--; + } + done: + /* Note that 'l' and 'r' may cross, i.e. r < l */ + DUK_ASSERT(l >= lo && l <= hi); + DUK_ASSERT(r >= lo && r <= hi); + + /* XXX: there's no explicit recursion bound here now. For the average + * qsort recursion depth O(log n) that's not really necessary: e.g. for + * 2**32 recursion depth would be about 32 which is OK. However, qsort + * worst case recursion depth is O(n) which may be a problem. + */ + + /* move pivot to its final place */ + DUK_DDD(DUK_DDDPRINT("before final pivot swap: %!T", (duk_tval *) duk_get_tval(ctx, 1))); + duk__array_sort_swap(ctx, lo, r); + +#if defined(DUK_USE_DDDPRINT) + duk__debuglog_qsort_state(ctx, lo, hi, r); +#endif + + DUK_DDD(DUK_DDDPRINT("recurse: pivot=%ld, obj=%!T", (long) r, (duk_tval *) duk_get_tval(ctx, 1))); + duk__array_qsort(ctx, lo, r - 1); + duk__array_qsort(ctx, r + 1, hi); +} + +DUK_INTERNAL duk_ret_t duk_bi_array_prototype_sort(duk_context *ctx) { + duk_uint32_t len; + + /* XXX: len >= 0x80000000 won't work below because a signed type + * is needed by qsort. + */ + len = duk__push_this_obj_len_u32_limited(ctx); + + /* stack[0] = compareFn + * stack[1] = ToObject(this) + * stack[2] = ToUint32(length) + */ + + if (len > 0) { + /* avoid degenerate cases, so that (len - 1) won't underflow */ + duk__array_qsort(ctx, (duk_int_t) 0, (duk_int_t) (len - 1)); + } + + DUK_ASSERT_TOP(ctx, 3); + duk_pop(ctx); + return 1; /* return ToObject(this) */ +} + +/* + * splice() + */ + +/* XXX: this compiles to over 500 bytes now, even without special handling + * for an array part. Uses signed ints so does not handle full array range correctly. + */ + +/* XXX: can shift() / unshift() use the same helper? + * shift() is (close to?) <--> splice(0, 1) + * unshift is (close to?) <--> splice(0, 0, [items])? + */ + +DUK_INTERNAL duk_ret_t duk_bi_array_prototype_splice(duk_context *ctx) { + duk_idx_t nargs; + duk_uint32_t len; + duk_bool_t have_delcount; + duk_int_t item_count; + duk_int_t act_start; + duk_int_t del_count; + duk_int_t i, n; + + DUK_UNREF(have_delcount); + + nargs = duk_get_top(ctx); + if (nargs < 2) { + duk_set_top(ctx, 2); + nargs = 2; + have_delcount = 0; + } else { + have_delcount = 1; + } + + /* XXX: len >= 0x80000000 won't work below because we need to be + * able to represent -len. + */ + len = duk__push_this_obj_len_u32_limited(ctx); + + act_start = duk_to_int_clamped(ctx, 0, -((duk_int_t) len), (duk_int_t) len); + if (act_start < 0) { + act_start = len + act_start; + } + DUK_ASSERT(act_start >= 0 && act_start <= (duk_int_t) len); + +#ifdef DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT + if (have_delcount) { +#endif + del_count = duk_to_int_clamped(ctx, 1, 0, len - act_start); +#ifdef DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT + } else { + /* E5.1 standard behavior when deleteCount is not given would be + * to treat it just like if 'undefined' was given, which coerces + * ultimately to 0. Real world behavior is to splice to the end + * of array, see test-bi-array-proto-splice-no-delcount.js. + */ + del_count = len - act_start; + } +#endif + + DUK_ASSERT(nargs >= 2); + item_count = (duk_int_t) (nargs - 2); + + DUK_ASSERT(del_count >= 0 && del_count <= (duk_int_t) len - act_start); + DUK_ASSERT(del_count + act_start <= (duk_int_t) len); + + /* For now, restrict result array into 32-bit length range. */ + if (((duk_double_t) len) - ((duk_double_t) del_count) + ((duk_double_t) item_count) > (duk_double_t) DUK_UINT32_MAX) { + DUK_D(DUK_DPRINT("Array.prototype.splice() would go beyond 32-bit length, throw")); + return DUK_RET_RANGE_ERROR; + } + + duk_push_array(ctx); + + /* stack[0] = start + * stack[1] = deleteCount + * stack[2...nargs-1] = items + * stack[nargs] = ToObject(this) -3 + * stack[nargs+1] = ToUint32(length) -2 + * stack[nargs+2] = result array -1 + */ + + DUK_ASSERT_TOP(ctx, nargs + 3); + + /* Step 9: copy elements-to-be-deleted into the result array */ + + for (i = 0; i < del_count; i++) { + if (duk_get_prop_index(ctx, -3, (duk_uarridx_t) (act_start + i))) { + duk_xdef_prop_index_wec(ctx, -2, i); /* throw flag irrelevant (false in std alg) */ + } else { + duk_pop(ctx); + } + } + duk_push_u32(ctx, (duk_uint32_t) del_count); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); + + /* Steps 12 and 13: reorganize elements to make room for itemCount elements */ + + if (item_count < del_count) { + /* [ A B C D E F G H ] rel_index = 2, del_count 3, item count 1 + * -> [ A B F G H ] (conceptual intermediate step) + * -> [ A B . F G H ] (placeholder marked) + * [ A B C F G H ] (actual result at this point, C will be replaced) + */ + + DUK_ASSERT_TOP(ctx, nargs + 3); + + n = len - del_count; + for (i = act_start; i < n; i++) { + if (duk_get_prop_index(ctx, -3, (duk_uarridx_t) (i + del_count))) { + duk_put_prop_index(ctx, -4, (duk_uarridx_t) (i + item_count)); + } else { + duk_pop(ctx); + duk_del_prop_index(ctx, -3, (duk_uarridx_t) (i + item_count)); + } + } + + DUK_ASSERT_TOP(ctx, nargs + 3); + + /* loop iterator init and limit changed from standard algorithm */ + n = len - del_count + item_count; + for (i = len - 1; i >= n; i--) { + duk_del_prop_index(ctx, -3, (duk_uarridx_t) i); + } + + DUK_ASSERT_TOP(ctx, nargs + 3); + } else if (item_count > del_count) { + /* [ A B C D E F G H ] rel_index = 2, del_count 3, item count 4 + * -> [ A B F G H ] (conceptual intermediate step) + * -> [ A B . . . . F G H ] (placeholder marked) + * [ A B C D E F F G H ] (actual result at this point) + */ + + DUK_ASSERT_TOP(ctx, nargs + 3); + + /* loop iterator init and limit changed from standard algorithm */ + for (i = len - del_count - 1; i >= act_start; i--) { + if (duk_get_prop_index(ctx, -3, (duk_uarridx_t) (i + del_count))) { + duk_put_prop_index(ctx, -4, (duk_uarridx_t) (i + item_count)); + } else { + duk_pop(ctx); + duk_del_prop_index(ctx, -3, (duk_uarridx_t) (i + item_count)); + } + } + + DUK_ASSERT_TOP(ctx, nargs + 3); + } else { + /* [ A B C D E F G H ] rel_index = 2, del_count 3, item count 3 + * -> [ A B F G H ] (conceptual intermediate step) + * -> [ A B . . . F G H ] (placeholder marked) + * [ A B C D E F G H ] (actual result at this point) + */ + } + DUK_ASSERT_TOP(ctx, nargs + 3); + + /* Step 15: insert itemCount elements into the hole made above */ + + for (i = 0; i < item_count; i++) { + duk_dup(ctx, i + 2); /* args start at index 2 */ + duk_put_prop_index(ctx, -4, (duk_uarridx_t) (act_start + i)); + } + + /* Step 16: update length; note that the final length may be above 32 bit range + * (but we checked above that this isn't the case here) + */ + + duk_push_u32(ctx, len - del_count + item_count); + duk_put_prop_stridx(ctx, -4, DUK_STRIDX_LENGTH); + + /* result array is already at the top of stack */ + DUK_ASSERT_TOP(ctx, nargs + 3); + return 1; +} + +/* + * reverse() + */ + +DUK_INTERNAL duk_ret_t duk_bi_array_prototype_reverse(duk_context *ctx) { + duk_uint32_t len; + duk_uint32_t middle; + duk_uint32_t lower, upper; + duk_bool_t have_lower, have_upper; + + len = duk__push_this_obj_len_u32(ctx); + middle = len / 2; + + /* If len <= 1, middle will be 0 and for-loop bails out + * immediately (0 < 0 -> false). + */ + + for (lower = 0; lower < middle; lower++) { + DUK_ASSERT(len >= 2); + DUK_ASSERT_TOP(ctx, 2); + + DUK_ASSERT(len >= lower + 1); + upper = len - lower - 1; + + have_lower = duk_get_prop_index(ctx, -2, (duk_uarridx_t) lower); + have_upper = duk_get_prop_index(ctx, -3, (duk_uarridx_t) upper); + + /* [ ToObject(this) ToUint32(length) lowerValue upperValue ] */ + + if (have_upper) { + duk_put_prop_index(ctx, -4, (duk_uarridx_t) lower); + } else { + duk_del_prop_index(ctx, -4, (duk_uarridx_t) lower); + duk_pop(ctx); + } + + if (have_lower) { + duk_put_prop_index(ctx, -3, (duk_uarridx_t) upper); + } else { + duk_del_prop_index(ctx, -3, (duk_uarridx_t) upper); + duk_pop(ctx); + } + + DUK_ASSERT_TOP(ctx, 2); + } + + DUK_ASSERT_TOP(ctx, 2); + duk_pop(ctx); /* -> [ ToObject(this) ] */ + return 1; +} + +/* + * slice() + */ + +DUK_INTERNAL duk_ret_t duk_bi_array_prototype_slice(duk_context *ctx) { + duk_uint32_t len; + duk_int_t start, end; + duk_int_t i; + duk_uarridx_t idx; + duk_uint32_t res_length = 0; + + /* XXX: len >= 0x80000000 won't work below because we need to be + * able to represent -len. + */ + len = duk__push_this_obj_len_u32_limited(ctx); + duk_push_array(ctx); + + /* stack[0] = start + * stack[1] = end + * stack[2] = ToObject(this) + * stack[3] = ToUint32(length) + * stack[4] = result array + */ + + start = duk_to_int_clamped(ctx, 0, -((duk_int_t) len), (duk_int_t) len); + if (start < 0) { + start = len + start; + } + /* XXX: could duk_is_undefined() provide defaulting undefined to 'len' + * (the upper limit)? + */ + if (duk_is_undefined(ctx, 1)) { + end = len; + } else { + end = duk_to_int_clamped(ctx, 1, -((duk_int_t) len), (duk_int_t) len); + if (end < 0) { + end = len + end; + } + } + DUK_ASSERT(start >= 0 && (duk_uint32_t) start <= len); + DUK_ASSERT(end >= 0 && (duk_uint32_t) end <= len); + + idx = 0; + for (i = start; i < end; i++) { + DUK_ASSERT_TOP(ctx, 5); + if (duk_get_prop_index(ctx, 2, (duk_uarridx_t) i)) { + duk_xdef_prop_index_wec(ctx, 4, idx); + res_length = idx + 1; + } else { + duk_pop(ctx); + } + idx++; + DUK_ASSERT_TOP(ctx, 5); + } + + duk_push_u32(ctx, res_length); + duk_xdef_prop_stridx(ctx, 4, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); + + DUK_ASSERT_TOP(ctx, 5); + return 1; +} + +/* + * shift() + */ + +DUK_INTERNAL duk_ret_t duk_bi_array_prototype_shift(duk_context *ctx) { + duk_uint32_t len; + duk_uint32_t i; + + len = duk__push_this_obj_len_u32(ctx); + if (len == 0) { + duk_push_int(ctx, 0); + duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH); + return 0; + } + + duk_get_prop_index(ctx, 0, 0); + + /* stack[0] = object (this) + * stack[1] = ToUint32(length) + * stack[2] = elem at index 0 (retval) + */ + + for (i = 1; i < len; i++) { + DUK_ASSERT_TOP(ctx, 3); + if (duk_get_prop_index(ctx, 0, (duk_uarridx_t) i)) { + /* fromPresent = true */ + duk_put_prop_index(ctx, 0, (duk_uarridx_t) (i - 1)); + } else { + /* fromPresent = false */ + duk_del_prop_index(ctx, 0, (duk_uarridx_t) (i - 1)); + duk_pop(ctx); + } + } + duk_del_prop_index(ctx, 0, (duk_uarridx_t) (len - 1)); + + duk_push_u32(ctx, (duk_uint32_t) (len - 1)); + duk_put_prop_stridx(ctx, 0, DUK_STRIDX_LENGTH); + + DUK_ASSERT_TOP(ctx, 3); + return 1; +} + +/* + * unshift() + */ + +DUK_INTERNAL duk_ret_t duk_bi_array_prototype_unshift(duk_context *ctx) { + duk_idx_t nargs; + duk_uint32_t len; + duk_uint32_t i; + + nargs = duk_get_top(ctx); + len = duk__push_this_obj_len_u32(ctx); + + /* stack[0...nargs-1] = unshift args (vararg) + * stack[nargs] = ToObject(this) + * stack[nargs+1] = ToUint32(length) + */ + + DUK_ASSERT_TOP(ctx, nargs + 2); + + /* Note: unshift() may operate on indices above unsigned 32-bit range + * and the final length may be >= 2**32. However, we restrict the + * final result to 32-bit range for practicality. + */ + + if (len + (duk_uint32_t) nargs < len) { + DUK_D(DUK_DPRINT("Array.prototype.unshift() would go beyond 32-bit length, throw")); + return DUK_RET_RANGE_ERROR; + } + + i = len; + while (i > 0) { + DUK_ASSERT_TOP(ctx, nargs + 2); + i--; + /* k+argCount-1; note that may be above 32-bit range */ + + if (duk_get_prop_index(ctx, -2, (duk_uarridx_t) i)) { + /* fromPresent = true */ + /* [ ... ToObject(this) ToUint32(length) val ] */ + duk_put_prop_index(ctx, -3, (duk_uarridx_t) (i + nargs)); /* -> [ ... ToObject(this) ToUint32(length) ] */ + } else { + /* fromPresent = false */ + /* [ ... ToObject(this) ToUint32(length) val ] */ + duk_pop(ctx); + duk_del_prop_index(ctx, -2, (duk_uarridx_t) (i + nargs)); /* -> [ ... ToObject(this) ToUint32(length) ] */ + } + DUK_ASSERT_TOP(ctx, nargs + 2); + } + + for (i = 0; i < (duk_uint32_t) nargs; i++) { + DUK_ASSERT_TOP(ctx, nargs + 2); + duk_dup(ctx, i); /* -> [ ... ToObject(this) ToUint32(length) arg[i] ] */ + duk_put_prop_index(ctx, -3, (duk_uarridx_t) i); + DUK_ASSERT_TOP(ctx, nargs + 2); + } + + DUK_ASSERT_TOP(ctx, nargs + 2); + duk_push_u32(ctx, len + nargs); + duk_dup_top(ctx); /* -> [ ... ToObject(this) ToUint32(length) final_len final_len ] */ + duk_put_prop_stridx(ctx, -4, DUK_STRIDX_LENGTH); + return 1; +} + +/* + * indexOf(), lastIndexOf() + */ + +DUK_INTERNAL duk_ret_t duk_bi_array_prototype_indexof_shared(duk_context *ctx) { + duk_idx_t nargs; + duk_int_t i, len; + duk_int_t from_index; + duk_small_int_t idx_step = duk_get_current_magic(ctx); /* idx_step is +1 for indexOf, -1 for lastIndexOf */ + + /* lastIndexOf() needs to be a vararg function because we must distinguish + * between an undefined fromIndex and a "not given" fromIndex; indexOf() is + * made vararg for symmetry although it doesn't strictly need to be. + */ + + nargs = duk_get_top(ctx); + duk_set_top(ctx, 2); + + /* XXX: must be able to represent -len */ + len = (duk_int_t) duk__push_this_obj_len_u32_limited(ctx); + if (len == 0) { + goto not_found; + } + + /* Index clamping is a bit tricky, we must ensure that we'll only iterate + * through elements that exist and that the specific requirements from E5.1 + * Sections 15.4.4.14 and 15.4.4.15 are fulfilled; especially: + * + * - indexOf: clamp to [-len,len], negative handling -> [0,len], + * if clamped result is len, for-loop bails out immediately + * + * - lastIndexOf: clamp to [-len-1, len-1], negative handling -> [-1, len-1], + * if clamped result is -1, for-loop bails out immediately + * + * If fromIndex is not given, ToInteger(undefined) = 0, which is correct + * for indexOf() but incorrect for lastIndexOf(). Hence special handling, + * and why lastIndexOf() needs to be a vararg function. + */ + + if (nargs >= 2) { + /* indexOf: clamp fromIndex to [-len, len] + * (if fromIndex == len, for-loop terminates directly) + * + * lastIndexOf: clamp fromIndex to [-len - 1, len - 1] + * (if clamped to -len-1 -> fromIndex becomes -1, terminates for-loop directly) + */ + from_index = duk_to_int_clamped(ctx, + 1, + (idx_step > 0 ? -len : -len - 1), + (idx_step > 0 ? len : len - 1)); + if (from_index < 0) { + /* for lastIndexOf, result may be -1 (mark immediate termination) */ + from_index = len + from_index; + } + } else { + /* for indexOf, ToInteger(undefined) would be 0, i.e. correct, but + * handle both indexOf and lastIndexOf specially here. + */ + if (idx_step > 0) { + from_index = 0; + } else { + from_index = len - 1; + } + } + + /* stack[0] = searchElement + * stack[1] = fromIndex + * stack[2] = object + * stack[3] = length (not needed, but not popped above) + */ + + for (i = from_index; i >= 0 && i < len; i += idx_step) { + DUK_ASSERT_TOP(ctx, 4); + + if (duk_get_prop_index(ctx, 2, (duk_uarridx_t) i)) { + DUK_ASSERT_TOP(ctx, 5); + if (duk_strict_equals(ctx, 0, 4)) { + duk_push_int(ctx, i); + return 1; + } + } + + duk_pop(ctx); + } + + not_found: + duk_push_int(ctx, -1); + return 1; +} + +/* + * every(), some(), forEach(), map(), filter() + */ + +#define DUK__ITER_EVERY 0 +#define DUK__ITER_SOME 1 +#define DUK__ITER_FOREACH 2 +#define DUK__ITER_MAP 3 +#define DUK__ITER_FILTER 4 + +/* XXX: This helper is a bit awkward because the handling for the different iteration + * callers is quite different. This now compiles to a bit less than 500 bytes, so with + * 5 callers the net result is about 100 bytes / caller. + */ + +DUK_INTERNAL duk_ret_t duk_bi_array_prototype_iter_shared(duk_context *ctx) { + duk_uint32_t len; + duk_uint32_t i; + duk_uarridx_t k; + duk_bool_t bval; + duk_small_int_t iter_type = duk_get_current_magic(ctx); + duk_uint32_t res_length = 0; + + /* each call this helper serves has nargs==2 */ + DUK_ASSERT_TOP(ctx, 2); + + len = duk__push_this_obj_len_u32(ctx); + duk_require_callable(ctx, 0); + /* if thisArg not supplied, behave as if undefined was supplied */ + + if (iter_type == DUK__ITER_MAP || iter_type == DUK__ITER_FILTER) { + duk_push_array(ctx); + } else { + duk_push_undefined(ctx); + } + + /* stack[0] = callback + * stack[1] = thisArg + * stack[2] = object + * stack[3] = ToUint32(length) (unused, but avoid unnecessary pop) + * stack[4] = result array (or undefined) + */ + + k = 0; /* result index for filter() */ + for (i = 0; i < len; i++) { + DUK_ASSERT_TOP(ctx, 5); + + if (!duk_get_prop_index(ctx, 2, (duk_uarridx_t) i)) { +#if defined(DUK_USE_NONSTD_ARRAY_MAP_TRAILER) + /* Real world behavior for map(): trailing non-existent + * elements don't invoke the user callback, but are still + * counted towards result 'length'. + */ + if (iter_type == DUK__ITER_MAP) { + res_length = i + 1; + } +#else + /* Standard behavior for map(): trailing non-existent + * elements don't invoke the user callback and are not + * counted towards result 'length'. + */ +#endif + duk_pop(ctx); + continue; + } + + /* The original value needs to be preserved for filter(), hence + * this funny order. We can't re-get the value because of side + * effects. + */ + + duk_dup(ctx, 0); + duk_dup(ctx, 1); + duk_dup(ctx, -3); + duk_push_u32(ctx, i); + duk_dup(ctx, 2); /* [ ... val callback thisArg val i obj ] */ + duk_call_method(ctx, 3); /* -> [ ... val retval ] */ + + switch (iter_type) { + case DUK__ITER_EVERY: + bval = duk_to_boolean(ctx, -1); + if (!bval) { + /* stack top contains 'false' */ + return 1; + } + break; + case DUK__ITER_SOME: + bval = duk_to_boolean(ctx, -1); + if (bval) { + /* stack top contains 'true' */ + return 1; + } + break; + case DUK__ITER_FOREACH: + /* nop */ + break; + case DUK__ITER_MAP: + duk_dup(ctx, -1); + duk_xdef_prop_index_wec(ctx, 4, (duk_uarridx_t) i); /* retval to result[i] */ + res_length = i + 1; + break; + case DUK__ITER_FILTER: + bval = duk_to_boolean(ctx, -1); + if (bval) { + duk_dup(ctx, -2); /* orig value */ + duk_xdef_prop_index_wec(ctx, 4, (duk_uarridx_t) k); + k++; + res_length = k; + } + break; + default: + DUK_UNREACHABLE(); + break; + } + duk_pop_2(ctx); + + DUK_ASSERT_TOP(ctx, 5); + } + + switch (iter_type) { + case DUK__ITER_EVERY: + duk_push_true(ctx); + break; + case DUK__ITER_SOME: + duk_push_false(ctx); + break; + case DUK__ITER_FOREACH: + duk_push_undefined(ctx); + break; + case DUK__ITER_MAP: + case DUK__ITER_FILTER: + DUK_ASSERT_TOP(ctx, 5); + DUK_ASSERT(duk_is_array(ctx, -1)); /* topmost element is the result array already */ + duk_push_u32(ctx, res_length); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W); + break; + default: + DUK_UNREACHABLE(); + break; + } + + return 1; +} + +/* + * reduce(), reduceRight() + */ + +DUK_INTERNAL duk_ret_t duk_bi_array_prototype_reduce_shared(duk_context *ctx) { + duk_idx_t nargs; + duk_bool_t have_acc; + duk_uint32_t i, len; + duk_small_int_t idx_step = duk_get_current_magic(ctx); /* idx_step is +1 for reduce, -1 for reduceRight */ + + /* We're a varargs function because we need to detect whether + * initialValue was given or not. + */ + nargs = duk_get_top(ctx); + DUK_DDD(DUK_DDDPRINT("nargs=%ld", (long) nargs)); + + duk_set_top(ctx, 2); + len = duk__push_this_obj_len_u32(ctx); + if (!duk_is_callable(ctx, 0)) { + goto type_error; + } + + /* stack[0] = callback fn + * stack[1] = initialValue + * stack[2] = object (coerced this) + * stack[3] = length (not needed, but not popped above) + * stack[4] = accumulator + */ + + have_acc = 0; + if (nargs >= 2) { + duk_dup(ctx, 1); + have_acc = 1; + } + DUK_DDD(DUK_DDDPRINT("have_acc=%ld, acc=%!T", + (long) have_acc, (duk_tval *) duk_get_tval(ctx, 3))); + + /* For len == 0, i is initialized to len - 1 which underflows. + * The condition (i < len) will then exit the for-loop on the + * first round which is correct. Similarly, loop termination + * happens by i underflowing. + */ + + for (i = (idx_step >= 0 ? 0 : len - 1); + i < len; /* i >= 0 would always be true */ + i += idx_step) { + DUK_DDD(DUK_DDDPRINT("i=%ld, len=%ld, have_acc=%ld, top=%ld, acc=%!T", + (long) i, (long) len, (long) have_acc, + (long) duk_get_top(ctx), + (duk_tval *) duk_get_tval(ctx, 4))); + + DUK_ASSERT((have_acc && duk_get_top(ctx) == 5) || + (!have_acc && duk_get_top(ctx) == 4)); + + if (!duk_has_prop_index(ctx, 2, (duk_uarridx_t) i)) { + continue; + } + + if (!have_acc) { + DUK_ASSERT_TOP(ctx, 4); + duk_get_prop_index(ctx, 2, (duk_uarridx_t) i); + have_acc = 1; + DUK_ASSERT_TOP(ctx, 5); + } else { + DUK_ASSERT_TOP(ctx, 5); + duk_dup(ctx, 0); + duk_dup(ctx, 4); + duk_get_prop_index(ctx, 2, (duk_uarridx_t) i); + duk_push_u32(ctx, i); + duk_dup(ctx, 2); + DUK_DDD(DUK_DDDPRINT("calling reduce function: func=%!T, prev=%!T, curr=%!T, idx=%!T, obj=%!T", + (duk_tval *) duk_get_tval(ctx, -5), (duk_tval *) duk_get_tval(ctx, -4), + (duk_tval *) duk_get_tval(ctx, -3), (duk_tval *) duk_get_tval(ctx, -2), + (duk_tval *) duk_get_tval(ctx, -1))); + duk_call(ctx, 4); + DUK_DDD(DUK_DDDPRINT("-> result: %!T", (duk_tval *) duk_get_tval(ctx, -1))); + duk_replace(ctx, 4); + DUK_ASSERT_TOP(ctx, 5); + } + } + + if (!have_acc) { + goto type_error; + } + + DUK_ASSERT_TOP(ctx, 5); + return 1; + + type_error: + return DUK_RET_TYPE_ERROR; +} + +#undef DUK__ARRAY_MID_JOIN_LIMIT + +#undef DUK__ITER_EVERY +#undef DUK__ITER_SOME +#undef DUK__ITER_FOREACH +#undef DUK__ITER_MAP +#undef DUK__ITER_FILTER +/* + * Boolean built-ins + */ + +/* include removed: duk_internal.h */ + +/* Shared helper to provide toString() and valueOf(). Checks 'this', gets + * the primitive value to stack top, and optionally coerces with ToString(). + */ +DUK_INTERNAL duk_ret_t duk_bi_boolean_prototype_tostring_shared(duk_context *ctx) { + duk_tval *tv; + duk_hobject *h; + duk_small_int_t coerce_tostring = duk_get_current_magic(ctx); + + /* XXX: there is room to use a shared helper here, many built-ins + * check the 'this' type, and if it's an object, check its class, + * then get its internal value, etc. + */ + + duk_push_this(ctx); + tv = duk_get_tval(ctx, -1); + DUK_ASSERT(tv != NULL); + + if (DUK_TVAL_IS_BOOLEAN(tv)) { + goto type_ok; + } else if (DUK_TVAL_IS_OBJECT(tv)) { + h = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h != NULL); + + if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_BOOLEAN) { + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE); + DUK_ASSERT(duk_is_boolean(ctx, -1)); + goto type_ok; + } + } + + return DUK_RET_TYPE_ERROR; + + type_ok: + if (coerce_tostring) { + duk_to_string(ctx, -1); + } + return 1; +} + +DUK_INTERNAL duk_ret_t duk_bi_boolean_constructor(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *h_this; + + DUK_UNREF(thr); + + duk_to_boolean(ctx, 0); + + if (duk_is_constructor_call(ctx)) { + /* XXX: helper; rely on Boolean.prototype as being non-writable, non-configurable */ + duk_push_this(ctx); + h_this = duk_get_hobject(ctx, -1); + DUK_ASSERT(h_this != NULL); + DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_this) == thr->builtins[DUK_BIDX_BOOLEAN_PROTOTYPE]); + + DUK_HOBJECT_SET_CLASS_NUMBER(h_this, DUK_HOBJECT_CLASS_BOOLEAN); + + duk_dup(ctx, 0); /* -> [ val obj val ] */ + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE); /* XXX: proper flags? */ + } /* unbalanced stack */ + + return 1; +} +/* + * Duktape.Buffer, Node.js Buffer, and Khronos/ES6 TypedArray built-ins + */ + +/* include removed: duk_internal.h */ + +/* + * Misc helpers + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +/* Map DUK_HBUFFEROBJECT_ELEM_xxx to duk_hobject class number. + * Sync with duk_hbufferobject.h and duk_hobject.h. + */ +static const duk_uint8_t duk__buffer_class_from_elemtype[9] = { + DUK_HOBJECT_CLASS_UINT8ARRAY, + DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY, + DUK_HOBJECT_CLASS_INT8ARRAY, + DUK_HOBJECT_CLASS_UINT16ARRAY, + DUK_HOBJECT_CLASS_INT16ARRAY, + DUK_HOBJECT_CLASS_UINT32ARRAY, + DUK_HOBJECT_CLASS_INT32ARRAY, + DUK_HOBJECT_CLASS_FLOAT32ARRAY, + DUK_HOBJECT_CLASS_FLOAT64ARRAY +}; +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +/* Map DUK_HBUFFEROBJECT_ELEM_xxx to prototype object built-in index. + * Sync with duk_hbufferobject.h. + */ +static const duk_uint8_t duk__buffer_proto_from_elemtype[9] = { + DUK_BIDX_UINT8ARRAY_PROTOTYPE, + DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE, + DUK_BIDX_INT8ARRAY_PROTOTYPE, + DUK_BIDX_UINT16ARRAY_PROTOTYPE, + DUK_BIDX_INT16ARRAY_PROTOTYPE, + DUK_BIDX_UINT32ARRAY_PROTOTYPE, + DUK_BIDX_INT32ARRAY_PROTOTYPE, + DUK_BIDX_FLOAT32ARRAY_PROTOTYPE, + DUK_BIDX_FLOAT64ARRAY_PROTOTYPE +}; +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +/* Map DUK__FLX_xxx to byte size. + */ +static const duk_uint8_t duk__buffer_nbytes_from_fldtype[6] = { + 1, /* DUK__FLD_8BIT */ + 2, /* DUK__FLD_16BIT */ + 4, /* DUK__FLD_32BIT */ + 4, /* DUK__FLD_FLOAT */ + 8, /* DUK__FLD_DOUBLE */ + 0 /* DUK__FLD_VARINT; not relevant here */ +}; +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +/* Bitfield for each DUK_HBUFFEROBJECT_ELEM_xxx indicating which element types + * are compatible with a blind byte copy for the TypedArray set() method (also + * used for TypedArray constructor). Array index is target buffer elem type, + * bitfield indicates compatible source types. The types must have same byte + * size and they must be coercion compatible. + */ +static duk_uint16_t duk__buffer_elemtype_copy_compatible[9] = { + /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT8 */ + (1U << DUK_HBUFFEROBJECT_ELEM_UINT8) | + (1U << DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED) | + (1U << DUK_HBUFFEROBJECT_ELEM_INT8), + + /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED + * Note: INT8 is -not- copy compatible, e.g. -1 would coerce to 0x00. + */ + (1U << DUK_HBUFFEROBJECT_ELEM_UINT8) | + (1U << DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED), + + /* xxx -> DUK_HBUFFEROBJECT_ELEM_INT8 */ + (1U << DUK_HBUFFEROBJECT_ELEM_UINT8) | + (1U << DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED) | + (1U << DUK_HBUFFEROBJECT_ELEM_INT8), + + /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT16 */ + (1U << DUK_HBUFFEROBJECT_ELEM_UINT16) | + (1U << DUK_HBUFFEROBJECT_ELEM_INT16), + + /* xxx -> DUK_HBUFFEROBJECT_ELEM_INT16 */ + (1U << DUK_HBUFFEROBJECT_ELEM_UINT16) | + (1U << DUK_HBUFFEROBJECT_ELEM_INT16), + + /* xxx -> DUK_HBUFFEROBJECT_ELEM_UINT32 */ + (1U << DUK_HBUFFEROBJECT_ELEM_UINT32) | + (1U << DUK_HBUFFEROBJECT_ELEM_INT32), + + /* xxx -> DUK_HBUFFEROBJECT_ELEM_INT32 */ + (1U << DUK_HBUFFEROBJECT_ELEM_UINT32) | + (1U << DUK_HBUFFEROBJECT_ELEM_INT32), + + /* xxx -> DUK_HBUFFEROBJECT_ELEM_FLOAT32 */ + (1U << DUK_HBUFFEROBJECT_ELEM_FLOAT32), + + /* xxx -> DUK_HBUFFEROBJECT_ELEM_FLOAT64 */ + (1U << DUK_HBUFFEROBJECT_ELEM_FLOAT64) +}; +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +/* Shared helper. */ +DUK_LOCAL duk_hbufferobject *duk__getrequire_bufobj_this(duk_context *ctx, duk_bool_t throw_flag) { + duk_hthread *thr; + duk_tval *tv; + duk_hbufferobject *h_this; + + DUK_ASSERT(ctx != NULL); + thr = (duk_hthread *) ctx; + + tv = duk_get_borrowed_this_tval(ctx); + DUK_ASSERT(tv != NULL); + if (DUK_TVAL_IS_OBJECT(tv)) { + h_this = (duk_hbufferobject *) DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h_this != NULL); + if (DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_this)) { + DUK_ASSERT_HBUFFEROBJECT_VALID(h_this); + return h_this; + } + } + + if (throw_flag) { + DUK_ERROR_TYPE(thr, DUK_STR_NOT_BUFFER); + } + return NULL; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +/* Check that 'this' is a duk_hbufferobject and return a pointer to it. */ +DUK_LOCAL duk_hbufferobject *duk__get_bufobj_this(duk_context *ctx) { + return duk__getrequire_bufobj_this(ctx, 0); +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +/* Check that 'this' is a duk_hbufferobject and return a pointer to it + * (NULL if not). + */ +DUK_LOCAL duk_hbufferobject *duk__require_bufobj_this(duk_context *ctx) { + return duk__getrequire_bufobj_this(ctx, 1); +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +/* Check that value is a duk_hbufferobject and return a pointer to it. */ +DUK_LOCAL duk_hbufferobject *duk__require_bufobj_value(duk_context *ctx, duk_idx_t index) { + duk_hthread *thr; + duk_tval *tv; + duk_hbufferobject *h_obj; + + thr = (duk_hthread *) ctx; + + /* Don't accept relative indices now. */ + DUK_ASSERT(index >= 0); + + tv = duk_require_tval(ctx, index); + DUK_ASSERT(tv != NULL); + if (DUK_TVAL_IS_OBJECT(tv)) { + h_obj = (duk_hbufferobject *) DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h_obj != NULL); + if (DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_obj)) { + DUK_ASSERT_HBUFFEROBJECT_VALID(h_obj); + return h_obj; + } + } + + DUK_ERROR_TYPE(thr, DUK_STR_NOT_BUFFER); + return NULL; /* not reachable */ +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +DUK_LOCAL void duk__set_bufobj_buffer(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_hbuffer *h_val) { + duk_hthread *thr; + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + DUK_ASSERT(ctx != NULL); + DUK_ASSERT(h_bufobj != NULL); + DUK_ASSERT(h_bufobj->buf == NULL); /* no need to decref */ + DUK_ASSERT(h_val != NULL); + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + + h_bufobj->buf = h_val; + DUK_HBUFFER_INCREF(thr, h_val); + h_bufobj->length = (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_val); + DUK_ASSERT(h_bufobj->shift == 0); + DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8); + + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); +} + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_LOCAL duk_hbufferobject *duk__push_arraybuffer_with_length(duk_context *ctx, duk_uint_t len) { + duk_hbuffer *h_val; + duk_hbufferobject *h_bufobj; + + (void) duk_push_fixed_buffer(ctx, (duk_size_t) len); + h_val = (duk_hbuffer *) duk_get_hbuffer(ctx, -1); + DUK_ASSERT(h_val != NULL); + + h_bufobj = duk_push_bufferobject_raw(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_BUFFEROBJECT | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER), + DUK_BIDX_ARRAYBUFFER_PROTOTYPE); + DUK_ASSERT(h_bufobj != NULL); + + duk__set_bufobj_buffer(ctx, h_bufobj, h_val); + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + + return h_bufobj; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +/* Shared offset/length coercion helper. */ +DUK_LOCAL void duk__resolve_offset_opt_length(duk_context *ctx, + duk_hbufferobject *h_bufarg, + duk_idx_t idx_offset, + duk_idx_t idx_length, + duk_uint_t *out_offset, + duk_uint_t *out_length, + duk_bool_t throw_flag) { + duk_hthread *thr; + duk_int_t offset_signed; + duk_int_t length_signed; + duk_uint_t offset; + duk_uint_t length; + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + offset_signed = duk_to_int(ctx, idx_offset); + if (offset_signed < 0) { + goto fail_range; + } + offset = (duk_uint_t) offset_signed; + if (offset > h_bufarg->length) { + goto fail_range; + } + DUK_ASSERT_DISABLE(offset >= 0); /* unsigned */ + DUK_ASSERT(offset <= h_bufarg->length); + + if (duk_is_undefined(ctx, idx_length)) { + DUK_ASSERT(h_bufarg->length >= offset); + length = h_bufarg->length - offset; /* >= 0 */ + } else { + length_signed = duk_to_int(ctx, idx_length); + if (length_signed < 0) { + goto fail_range; + } + length = (duk_uint_t) length_signed; + DUK_ASSERT(h_bufarg->length >= offset); + if (length > h_bufarg->length - offset) { + /* Unlike for negative arguments, some call sites + * want length to be clamped if it's positive. + */ + if (throw_flag) { + goto fail_range; + } else { + length = h_bufarg->length - offset; + } + } + } + DUK_ASSERT_DISABLE(length >= 0); /* unsigned */ + DUK_ASSERT(offset + length <= h_bufarg->length); + + *out_offset = offset; + *out_length = length; + return; + + fail_range: + DUK_ERROR_RANGE(thr, DUK_STR_INVALID_CALL_ARGS); +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +/* Shared lenient buffer length clamping helper. No negative indices, no + * element/byte shifting. + */ +DUK_LOCAL void duk__clamp_startend_nonegidx_noshift(duk_context *ctx, + duk_hbufferobject *h_bufobj, + duk_idx_t idx_start, + duk_idx_t idx_end, + duk_int_t *out_start_offset, + duk_int_t *out_end_offset) { + duk_int_t buffer_length; + duk_int_t start_offset; + duk_int_t end_offset; + + DUK_ASSERT(out_start_offset != NULL); + DUK_ASSERT(out_end_offset != NULL); + + buffer_length = (duk_int_t) h_bufobj->length; + + /* undefined coerces to zero which is correct */ + start_offset = duk_to_int_clamped(ctx, idx_start, 0, buffer_length); + if (duk_is_undefined(ctx, idx_end)) { + end_offset = buffer_length; + } else { + end_offset = duk_to_int_clamped(ctx, idx_end, start_offset, buffer_length); + } + + DUK_ASSERT(start_offset >= 0); + DUK_ASSERT(start_offset <= buffer_length); + DUK_ASSERT(end_offset >= 0); + DUK_ASSERT(end_offset <= buffer_length); + DUK_ASSERT(start_offset <= end_offset); + + *out_start_offset = start_offset; + *out_end_offset = end_offset; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +/* Shared lenient buffer length clamping helper. Indices are treated as + * element indices (though output values are byte offsets) which only + * really matters for TypedArray views as other buffer object have a zero + * shift. Negative indices are counted from end of input slice; crossed + * indices are clamped to zero length; and final indices are clamped + * against input slice. Used for e.g. ArrayBuffer slice(). + */ +DUK_LOCAL void duk__clamp_startend_negidx_shifted(duk_context *ctx, + duk_hbufferobject *h_bufobj, + duk_idx_t idx_start, + duk_idx_t idx_end, + duk_int_t *out_start_offset, + duk_int_t *out_end_offset) { + duk_int_t buffer_length; + duk_int_t start_offset; + duk_int_t end_offset; + + DUK_ASSERT(out_start_offset != NULL); + DUK_ASSERT(out_end_offset != NULL); + + buffer_length = (duk_int_t) h_bufobj->length; + buffer_length >>= h_bufobj->shift; /* as elements */ + + /* Resolve start/end offset as element indices first; arguments + * at idx_start/idx_end are element offsets. Working with element + * indices first also avoids potential for wrapping. + */ + + start_offset = duk_to_int(ctx, idx_start); + if (start_offset < 0) { + start_offset = buffer_length + start_offset; + } + if (duk_is_undefined(ctx, idx_end)) { + end_offset = buffer_length; + } else { + end_offset = duk_to_int(ctx, idx_end); + if (end_offset < 0) { + end_offset = buffer_length + end_offset; + } + } + /* Note: start_offset/end_offset can still be < 0 here. */ + + if (start_offset < 0) { + start_offset = 0; + } else if (start_offset > buffer_length) { + start_offset = buffer_length; + } + if (end_offset < start_offset) { + end_offset = start_offset; + } else if (end_offset > buffer_length) { + end_offset = buffer_length; + } + DUK_ASSERT(start_offset >= 0); + DUK_ASSERT(start_offset <= buffer_length); + DUK_ASSERT(end_offset >= 0); + DUK_ASSERT(end_offset <= buffer_length); + DUK_ASSERT(start_offset <= end_offset); + + /* Convert indices to byte offsets. */ + start_offset <<= h_bufobj->shift; + end_offset <<= h_bufobj->shift; + + *out_start_offset = start_offset; + *out_end_offset = end_offset; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * Indexed read/write helpers (also used from outside this file) + */ + +DUK_INTERNAL void duk_hbufferobject_push_validated_read(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size) { + duk_double_union du; + + DUK_MEMCPY((void *) du.uc, (const void *) p, (size_t) elem_size); + + switch (h_bufobj->elem_type) { + case DUK_HBUFFEROBJECT_ELEM_UINT8: +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) + case DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED: +#endif + duk_push_uint(ctx, (duk_uint_t) du.uc[0]); + break; +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) + /* These are not needed when only Duktape.Buffer is supported. */ + case DUK_HBUFFEROBJECT_ELEM_INT8: + duk_push_int(ctx, (duk_int_t) (duk_int8_t) du.uc[0]); + break; + case DUK_HBUFFEROBJECT_ELEM_UINT16: + duk_push_uint(ctx, (duk_uint_t) du.us[0]); + break; + case DUK_HBUFFEROBJECT_ELEM_INT16: + duk_push_int(ctx, (duk_int_t) (duk_int16_t) du.us[0]); + break; + case DUK_HBUFFEROBJECT_ELEM_UINT32: + duk_push_uint(ctx, (duk_uint_t) du.ui[0]); + break; + case DUK_HBUFFEROBJECT_ELEM_INT32: + duk_push_int(ctx, (duk_int_t) (duk_int32_t) du.ui[0]); + break; + case DUK_HBUFFEROBJECT_ELEM_FLOAT32: + duk_push_number(ctx, (duk_double_t) du.f[0]); + break; + case DUK_HBUFFEROBJECT_ELEM_FLOAT64: + duk_push_number(ctx, (duk_double_t) du.d); + break; +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + default: + DUK_UNREACHABLE(); + } +} + +DUK_INTERNAL void duk_hbufferobject_validated_write(duk_context *ctx, duk_hbufferobject *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size) { + duk_double_union du; + + /* NOTE! Caller must ensure that any side effects from the + * coercions below are safe. If that cannot be guaranteed + * (which is normally the case), caller must coerce the + * argument using duk_to_number() before any pointer + * validations; the result of duk_to_number() always coerces + * without side effects here. + */ + + switch (h_bufobj->elem_type) { + case DUK_HBUFFEROBJECT_ELEM_UINT8: + du.uc[0] = (duk_uint8_t) duk_to_uint32(ctx, -1); + break; +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) + /* These are not needed when only Duktape.Buffer is supported. */ + case DUK_HBUFFEROBJECT_ELEM_UINT8CLAMPED: + du.uc[0] = (duk_uint8_t) duk_to_uint8clamped(ctx, -1); + break; + case DUK_HBUFFEROBJECT_ELEM_INT8: + du.uc[0] = (duk_uint8_t) duk_to_int32(ctx, -1); + break; + case DUK_HBUFFEROBJECT_ELEM_UINT16: + du.us[0] = (duk_uint16_t) duk_to_uint32(ctx, -1); + break; + case DUK_HBUFFEROBJECT_ELEM_INT16: + du.us[0] = (duk_uint16_t) duk_to_int32(ctx, -1); + break; + case DUK_HBUFFEROBJECT_ELEM_UINT32: + du.ui[0] = (duk_uint32_t) duk_to_uint32(ctx, -1); + break; + case DUK_HBUFFEROBJECT_ELEM_INT32: + du.ui[0] = (duk_uint32_t) duk_to_int32(ctx, -1); + break; + case DUK_HBUFFEROBJECT_ELEM_FLOAT32: + du.f[0] = (duk_float_t) duk_to_number(ctx, -1); + break; + case DUK_HBUFFEROBJECT_ELEM_FLOAT64: + du.d = (duk_double_t) duk_to_number(ctx, -1); + break; +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + default: + DUK_UNREACHABLE(); + } + + DUK_MEMCPY((void *) p, (const void *) du.uc, (size_t) elem_size); +} + +/* + * Duktape.Buffer: constructor + */ + +DUK_INTERNAL duk_ret_t duk_bi_buffer_constructor(duk_context *ctx) { + duk_hthread *thr; + duk_size_t buf_size; + duk_small_int_t buf_dynamic; + duk_uint8_t *buf_data; + const duk_uint8_t *src_data; + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + /* + * Constructor arguments are currently somewhat compatible with + * (keep it that way if possible): + * + * http://nodejs.org/api/buffer.html + * + * Note that the ToBuffer() coercion (duk_to_buffer()) does NOT match + * the constructor behavior. + */ + + buf_dynamic = duk_get_boolean(ctx, 1); /* default to false */ + + switch (duk_get_type(ctx, 0)) { + case DUK_TYPE_NUMBER: { + /* new buffer of specified size */ + buf_size = (duk_size_t) duk_to_int(ctx, 0); + (void) duk_push_buffer(ctx, buf_size, buf_dynamic); + break; + } + case DUK_TYPE_BUFFER: { + /* return input buffer, converted to a Duktape.Buffer object + * if called as a constructor (no change if called as a + * function). + */ + duk_set_top(ctx, 1); + break; + } + case DUK_TYPE_STRING: { + /* new buffer with string contents */ + src_data = (const duk_uint8_t *) duk_get_lstring(ctx, 0, &buf_size); + DUK_ASSERT(src_data != NULL); /* even for zero-length string */ + buf_data = (duk_uint8_t *) duk_push_buffer(ctx, buf_size, buf_dynamic); + DUK_MEMCPY((void *) buf_data, (const void *) src_data, (size_t) buf_size); + break; + } + case DUK_TYPE_OBJECT: { + /* For all duk_hbufferobjects, get the plain buffer inside + * without making a copy. This is compatible with Duktape 1.2 + * but means that a slice/view information is ignored and the + * full underlying buffer is returned. + * + * If called as a constructor, a new Duktape.Buffer object + * pointing to the same plain buffer is created below. + */ + duk_hbufferobject *h_bufobj; + h_bufobj = (duk_hbufferobject *) duk_get_hobject(ctx, 0); + DUK_ASSERT(h_bufobj != NULL); + if (!DUK_HOBJECT_IS_BUFFEROBJECT((duk_hobject *) h_bufobj)) { + return DUK_RET_TYPE_ERROR; + } + if (h_bufobj->buf == NULL) { + return DUK_RET_TYPE_ERROR; + } + duk_push_hbuffer(ctx, h_bufobj->buf); + break; + } + case DUK_TYPE_NONE: + default: { + return DUK_RET_TYPE_ERROR; + } + } + DUK_ASSERT(duk_is_buffer(ctx, -1)); + + /* stack is unbalanced, but: [ buf ] */ + + if (duk_is_constructor_call(ctx)) { + duk_hbufferobject *h_bufobj; + duk_hbuffer *h_val; + + h_val = duk_get_hbuffer(ctx, -1); + DUK_ASSERT(h_val != NULL); + + h_bufobj = duk_push_bufferobject_raw(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_BUFFEROBJECT | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER), + DUK_BIDX_BUFFER_PROTOTYPE); + DUK_ASSERT(h_bufobj != NULL); + + duk__set_bufobj_buffer(ctx, h_bufobj, h_val); + + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + } + /* Note: unbalanced stack on purpose */ + + return 1; +} + +/* + * Node.js Buffer: constructor + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_constructor(duk_context *ctx) { + /* Internal class is Object: Object.prototype.toString.call(new Buffer(0)) + * prints "[object Object]". + */ + duk_int_t len; + duk_int_t i; + duk_hbuffer *h_buf; + duk_hbufferobject *h_bufobj; + duk_size_t buf_size; + + switch (duk_get_type(ctx, 0)) { + case DUK_TYPE_BUFFER: { + /* Custom behavior: plain buffer is used as internal buffer + * without making a copy (matches Duktape.Buffer). + */ + duk_set_top(ctx, 1); /* -> [ buffer ] */ + break; + } + case DUK_TYPE_NUMBER: { + len = duk_to_int_clamped(ctx, 0, 0, DUK_INT_MAX); + (void) duk_push_fixed_buffer(ctx, (duk_size_t) len); + break; + } + case DUK_TYPE_OBJECT: { + duk_uint8_t *buf; + + (void) duk_get_prop_string(ctx, 0, "length"); + len = duk_to_int_clamped(ctx, -1, 0, DUK_INT_MAX); + duk_pop(ctx); + buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) len); + for (i = 0; i < len; i++) { + /* XXX: fast path for array arguments? */ + duk_get_prop_index(ctx, 0, (duk_uarridx_t) i); + buf[i] = (duk_uint8_t) (duk_to_uint32(ctx, -1) & 0xffU); + duk_pop(ctx); + } + break; + } + case DUK_TYPE_STRING: { + /* ignore encoding for now */ + duk_dup(ctx, 0); + (void) duk_to_buffer(ctx, -1, &buf_size); + break; + } + default: + return DUK_RET_TYPE_ERROR; + } + + DUK_ASSERT(duk_is_buffer(ctx, -1)); + h_buf = duk_get_hbuffer(ctx, -1); + DUK_ASSERT(h_buf != NULL); + + h_bufobj = duk_push_bufferobject_raw(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_BUFFEROBJECT | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER), + DUK_BIDX_NODEJS_BUFFER_PROTOTYPE); + DUK_ASSERT(h_bufobj != NULL); + + h_bufobj->buf = h_buf; + DUK_HBUFFER_INCREF(thr, h_buf); + DUK_ASSERT(h_bufobj->offset == 0); + h_bufobj->length = (duk_int_t) DUK_HBUFFER_GET_SIZE(h_buf); + DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8); + + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + + return 1; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_constructor(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * ArrayBuffer, DataView, and TypedArray constructors + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_constructor(duk_context *ctx) { + duk_hthread *thr; + duk_hbufferobject *h_bufobj; + duk_hbuffer *h_val; + + DUK_ASSERT_CTX_VALID(ctx); + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + /* XXX: function flag to make this automatic? */ + if (!duk_is_constructor_call(ctx)) { + return DUK_RET_TYPE_ERROR; + } + + if (duk_is_buffer(ctx, 0)) { + /* Custom behavior: plain buffer is used as internal buffer + * without making a copy (matches Duktape.Buffer). + */ + + h_val = duk_get_hbuffer(ctx, 0); + DUK_ASSERT(h_val != NULL); + + /* XXX: accept any duk_hbufferobject type as an input also? */ + } else { + duk_int_t len; + len = duk_to_int(ctx, 0); + if (len < 0) { + goto fail_length; + } + (void) duk_push_fixed_buffer(ctx, (duk_size_t) len); + h_val = (duk_hbuffer *) duk_get_hbuffer(ctx, -1); + DUK_ASSERT(h_val != NULL); + +#if !defined(DUK_USE_ZERO_BUFFER_DATA) + /* Khronos/ES6 requires zeroing even when DUK_USE_ZERO_BUFFER_DATA + * is not set. + */ + DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) h_val)); + DUK_MEMZERO((void *) DUK_HBUFFER_FIXED_GET_DATA_PTR(thr->heap, h_val), (duk_size_t) len); +#endif + } + + h_bufobj = duk_push_bufferobject_raw(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_BUFFEROBJECT | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER), + DUK_BIDX_ARRAYBUFFER_PROTOTYPE); + DUK_ASSERT(h_bufobj != NULL); + + duk__set_bufobj_buffer(ctx, h_bufobj, h_val); + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + + return 1; + + fail_length: + return DUK_RET_RANGE_ERROR; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_constructor(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + + +/* Format of magic, bits: + * 0...1: elem size shift (0-3) + * 2...5: elem type (DUK_HBUFFEROBJECT_ELEM_xxx) + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx) { + duk_hthread *thr; + duk_tval *tv; + duk_hobject *h_obj; + duk_hbufferobject *h_bufobj = NULL; + duk_hbufferobject *h_bufarr = NULL; + duk_hbufferobject *h_bufarg = NULL; + duk_hbuffer *h_val; + duk_small_uint_t magic; + duk_small_uint_t shift; + duk_small_uint_t elem_type; + duk_small_uint_t elem_size; + duk_small_uint_t class_num; + duk_small_uint_t proto_bidx; + duk_uint_t align_mask; + duk_uint_t elem_length; + duk_int_t elem_length_signed; + duk_uint_t byte_length; + duk_small_uint_t copy_mode; + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + /* XXX: function flag to make this automatic? */ + if (!duk_is_constructor_call(ctx)) { + return DUK_RET_TYPE_ERROR; + } + + /* We could fit built-in index into magic but that'd make the magic + * number dependent on built-in numbering (genbuiltins.py doesn't + * handle that yet). So map both class and prototype from the + * element type. + */ + magic = duk_get_current_magic(ctx); + shift = magic & 0x03; /* bits 0...1: shift */ + elem_type = (magic >> 2) & 0x0f; /* bits 2...5: type */ + elem_size = 1 << shift; + align_mask = elem_size - 1; + DUK_ASSERT(elem_type < sizeof(duk__buffer_proto_from_elemtype) / sizeof(duk_uint8_t)); + proto_bidx = duk__buffer_proto_from_elemtype[elem_type]; + DUK_ASSERT(proto_bidx < DUK_NUM_BUILTINS); + DUK_ASSERT(elem_type < sizeof(duk__buffer_class_from_elemtype) / sizeof(duk_uint8_t)); + class_num = duk__buffer_class_from_elemtype[elem_type]; + + DUK_DD(DUK_DDPRINT("typedarray constructor, magic=%d, shift=%d, elem_type=%d, " + "elem_size=%d, proto_bidx=%d, class_num=%d", + (int) magic, (int) shift, (int) elem_type, (int) elem_size, + (int) proto_bidx, (int) class_num)); + + /* Argument variants. When the argument is an ArrayBuffer a view to + * the same buffer is created; otherwise a new ArrayBuffer is always + * created. + */ + + tv = duk_get_tval(ctx, 0); + DUK_ASSERT(tv != NULL); /* arg count */ + if (DUK_TVAL_IS_OBJECT(tv)) { + h_obj = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h_obj != NULL); + + if (DUK_HOBJECT_GET_CLASS_NUMBER(h_obj) == DUK_HOBJECT_CLASS_ARRAYBUFFER) { + /* ArrayBuffer: unlike any other argument variant, create + * a view into the existing buffer. + */ + + duk_int_t byte_offset_signed; + duk_uint_t byte_offset; + + h_bufarg = (duk_hbufferobject *) h_obj; + + byte_offset_signed = duk_to_int(ctx, 1); + if (byte_offset_signed < 0) { + goto fail_arguments; + } + byte_offset = (duk_uint_t) byte_offset_signed; + if (byte_offset > h_bufarg->length || + (byte_offset & align_mask) != 0) { + /* Must be >= 0 and multiple of element size. */ + goto fail_arguments; + } + if (duk_is_undefined(ctx, 2)) { + DUK_ASSERT(h_bufarg->length >= byte_offset); + byte_length = h_bufarg->length - byte_offset; + if ((byte_length & align_mask) != 0) { + /* Must be element size multiple from + * start offset to end of buffer. + */ + goto fail_arguments; + } + elem_length = (byte_length >> shift); + } else { + elem_length_signed = duk_to_int(ctx, 2); + if (elem_length_signed < 0) { + goto fail_arguments; + } + elem_length = (duk_uint_t) elem_length_signed; + byte_length = elem_length << shift; + if ((byte_length >> shift) != elem_length) { + /* Byte length would overflow. */ + /* XXX: easier check with less code? */ + goto fail_arguments; + } + DUK_ASSERT(h_bufarg->length >= byte_offset); + if (byte_length > h_bufarg->length - byte_offset) { + /* Not enough data. */ + goto fail_arguments; + } + } + DUK_UNREF(elem_length); + DUK_ASSERT_DISABLE(byte_offset >= 0); + DUK_ASSERT(byte_offset <= h_bufarg->length); + DUK_ASSERT_DISABLE(byte_length >= 0); + DUK_ASSERT(byte_offset + byte_length <= h_bufarg->length); + DUK_ASSERT((elem_length << shift) == byte_length); + + h_bufobj = duk_push_bufferobject_raw(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_BUFFEROBJECT | + DUK_HOBJECT_CLASS_AS_FLAGS(class_num), + proto_bidx); + h_val = h_bufarg->buf; + if (h_val == NULL) { + return DUK_RET_TYPE_ERROR; + } + h_bufobj->buf = h_val; + DUK_HBUFFER_INCREF(thr, h_val); + h_bufobj->offset = h_bufarg->offset + byte_offset; + h_bufobj->length = byte_length; + h_bufobj->shift = (duk_uint8_t) shift; + h_bufobj->elem_type = (duk_uint8_t) elem_type; + h_bufobj->is_view = 1; + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + + /* Set .buffer to the argument ArrayBuffer. */ + duk_dup(ctx, 0); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE); + duk_compact(ctx, -1); + return 1; + } else if (DUK_HOBJECT_IS_BUFFEROBJECT(h_obj)) { + /* TypedArray (or other non-ArrayBuffer duk_hbufferobject). + * Conceptually same behavior as for an Array-like argument, + * with a few fast paths. + */ + + h_bufarg = (duk_hbufferobject *) h_obj; + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufarg); + elem_length_signed = (duk_int_t) (h_bufarg->length >> h_bufarg->shift); + if (h_bufarg->buf == NULL) { + return DUK_RET_TYPE_ERROR; + } + + /* Select copy mode. Must take into account element + * compatibility and validity of the underlying source + * buffer. + */ + + DUK_DDD(DUK_DDDPRINT("selecting copy mode for bufobj arg, " + "src byte_length=%ld, src shift=%d, " + "src/dst elem_length=%ld; " + "dst shift=%d -> dst byte_length=%ld", + (long) h_bufarg->length, (int) h_bufarg->shift, + (long) elem_length_signed, (int) shift, + (long) (elem_length_signed << shift))); + + copy_mode = 2; /* default is explicit index read/write copy */ + DUK_ASSERT(elem_type < sizeof(duk__buffer_elemtype_copy_compatible) / sizeof(duk_uint16_t)); + if (DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg)) { + if ((duk__buffer_elemtype_copy_compatible[elem_type] & (1 << h_bufarg->elem_type)) != 0) { + DUK_DDD(DUK_DDDPRINT("source/target are copy compatible, memcpy")); + DUK_ASSERT(shift == h_bufarg->shift); /* byte sizes will match */ + copy_mode = 0; + } else { + DUK_DDD(DUK_DDDPRINT("source/target not copy compatible but valid, fast copy")); + copy_mode = 1; + } + } + } else { + /* Array or Array-like */ + elem_length_signed = (duk_int_t) duk_get_length(ctx, 0); + copy_mode = 2; + } + } else if (DUK_TVAL_IS_BUFFER(tv)) { + /* Accept plain buffer values like array initializers + * (new in Duktape 1.4.0). + */ + duk_hbuffer *h_srcbuf; + h_srcbuf = DUK_TVAL_GET_BUFFER(tv); + elem_length_signed = (duk_int_t) DUK_HBUFFER_GET_SIZE(h_srcbuf); + copy_mode = 2; /* XXX: could add fast path for u8 compatible views */ + } else { + /* Non-object argument is simply int coerced, matches + * V8 behavior (except for "null", which we coerce to + * 0 but V8 TypeErrors). + */ + elem_length_signed = duk_to_int(ctx, 0); + copy_mode = 3; + } + if (elem_length_signed < 0) { + goto fail_arguments; + } + elem_length = (duk_uint_t) elem_length_signed; + byte_length = (duk_uint_t) (elem_length << shift); + if ((byte_length >> shift) != elem_length) { + /* Byte length would overflow. */ + /* XXX: easier check with less code? */ + goto fail_arguments; + } + + DUK_DDD(DUK_DDDPRINT("elem_length=%ld, byte_length=%ld", + (long) elem_length, (long) byte_length)); + + /* ArrayBuffer argument is handled specially above; the rest of the + * argument variants are handled by shared code below. + */ + + /* Push a new ArrayBuffer (becomes view .buffer) */ + h_bufarr = duk__push_arraybuffer_with_length(ctx, byte_length); + DUK_ASSERT(h_bufarr != NULL); + h_val = h_bufarr->buf; + DUK_ASSERT(h_val != NULL); + + /* Push the resulting view object and attach the ArrayBuffer. */ + h_bufobj = duk_push_bufferobject_raw(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_BUFFEROBJECT | + DUK_HOBJECT_CLASS_AS_FLAGS(class_num), + proto_bidx); + + h_bufobj->buf = h_val; + DUK_HBUFFER_INCREF(thr, h_val); + DUK_ASSERT(h_bufobj->offset == 0); + h_bufobj->length = byte_length; + h_bufobj->shift = (duk_uint8_t) shift; + h_bufobj->elem_type = (duk_uint8_t) elem_type; + h_bufobj->is_view = 1; + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + + /* Set .buffer */ + duk_dup(ctx, -2); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE); + duk_compact(ctx, -1); + + /* Copy values, the copy method depends on the arguments. + * + * Copy mode decision may depend on the validity of the underlying + * buffer of the source argument; there must be no harmful side effects + * from there to here for copy_mode to still be valid. + */ + DUK_DDD(DUK_DDDPRINT("copy mode: %d", (int) copy_mode)); + switch (copy_mode) { + case 0: { + /* Use byte copy. */ + + duk_uint8_t *p_src; + duk_uint8_t *p_dst; + + DUK_ASSERT(h_bufobj != NULL); + DUK_ASSERT(h_bufobj->buf != NULL); + DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj)); + DUK_ASSERT(h_bufarg != NULL); + DUK_ASSERT(h_bufarg->buf != NULL); + DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg)); + + p_dst = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufobj); + p_src = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg); + + DUK_DDD(DUK_DDDPRINT("using memcpy: p_src=%p, p_dst=%p, byte_length=%ld", + (void *) p_src, (void *) p_dst, (long) byte_length)); + + DUK_MEMCPY((void *) p_dst, (const void *) p_src, (size_t) byte_length); + break; + } + case 1: { + /* Copy values through direct validated reads and writes. */ + + duk_small_uint_t src_elem_size; + duk_small_uint_t dst_elem_size; + duk_uint8_t *p_src; + duk_uint8_t *p_src_end; + duk_uint8_t *p_dst; + + DUK_ASSERT(h_bufobj != NULL); + DUK_ASSERT(h_bufobj->buf != NULL); + DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj)); + DUK_ASSERT(h_bufarg != NULL); + DUK_ASSERT(h_bufarg->buf != NULL); + DUK_ASSERT(DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg)); + + src_elem_size = 1 << h_bufarg->shift; + dst_elem_size = elem_size; + + p_src = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg); + p_dst = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufobj); + p_src_end = p_src + h_bufarg->length; + + DUK_DDD(DUK_DDDPRINT("using fast copy: p_src=%p, p_src_end=%p, p_dst=%p, " + "src_elem_size=%d, dst_elem_size=%d", + (void *) p_src, (void *) p_src_end, (void *) p_dst, + (int) src_elem_size, (int) dst_elem_size)); + + while (p_src != p_src_end) { + DUK_DDD(DUK_DDDPRINT("fast path per element copy loop: " + "p_src=%p, p_src_end=%p, p_dst=%p", + (void *) p_src, (void *) p_src_end, (void *) p_dst)); + /* A validated read() is always a number, so it's write coercion + * is always side effect free an won't invalidate pointers etc. + */ + duk_hbufferobject_push_validated_read(ctx, h_bufarg, p_src, src_elem_size); + duk_hbufferobject_validated_write(ctx, h_bufobj, p_dst, dst_elem_size); + duk_pop(ctx); + p_src += src_elem_size; + p_dst += dst_elem_size; + } + break; + } + case 2: { + /* Copy values by index reads and writes. Let virtual + * property handling take care of coercion. + */ + duk_uint_t i; + + DUK_DDD(DUK_DDDPRINT("using slow copy")); + + for (i = 0; i < elem_length; i++) { + duk_get_prop_index(ctx, 0, (duk_uarridx_t) i); + duk_put_prop_index(ctx, -2, (duk_uarridx_t) i); + } + break; + } + default: + case 3: { + /* No copy, leave zero bytes in the buffer. There's no + * ambiguity with Float32/Float64 because zero bytes also + * represent 0.0. + */ +#if !defined(DUK_USE_ZERO_BUFFER_DATA) + /* Khronos/ES6 requires zeroing even when DUK_USE_ZERO_BUFFER_DATA + * is not set. + */ + DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) h_val)); + DUK_MEMZERO((void *) DUK_HBUFFER_FIXED_GET_DATA_PTR(thr->heap, h_val), (duk_size_t) byte_length); +#endif + + DUK_DDD(DUK_DDDPRINT("using no copy")); + break; + } + } + + return 1; + + fail_arguments: + return DUK_RET_RANGE_ERROR; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_dataview_constructor(duk_context *ctx) { + duk_hbufferobject *h_bufarg; + duk_hbufferobject *h_bufobj; + duk_hbuffer *h_val; + duk_uint_t offset; + duk_uint_t length; + + /* XXX: function flag to make this automatic? */ + if (!duk_is_constructor_call(ctx)) { + return DUK_RET_TYPE_ERROR; + } + + h_bufarg = duk__require_bufobj_value(ctx, 0); + DUK_ASSERT(h_bufarg != NULL); + + duk__resolve_offset_opt_length(ctx, h_bufarg, 1, 2, &offset, &length, 1 /*throw_flag*/); + DUK_ASSERT(offset <= h_bufarg->length); + DUK_ASSERT(offset + length <= h_bufarg->length); + + h_bufobj = duk_push_bufferobject_raw(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_BUFFEROBJECT | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DATAVIEW), + DUK_BIDX_DATAVIEW_PROTOTYPE); + + h_val = h_bufarg->buf; + if (h_val == NULL) { + return DUK_RET_TYPE_ERROR; + } + h_bufobj->buf = h_val; + DUK_HBUFFER_INCREF(thr, h_val); + h_bufobj->offset = h_bufarg->offset + offset; + h_bufobj->length = length; + DUK_ASSERT(h_bufobj->shift == 0); + DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFFEROBJECT_ELEM_UINT8); + h_bufobj->is_view = 1; + + /* The DataView .buffer property is ordinarily set to the argument + * which is an ArrayBuffer. We accept any duk_hbufferobject as + * an argument and .buffer will be set to the argument regardless + * of what it is. This may be a bit confusing if the argument + * is e.g. a DataView or another TypedArray view. + * + * XXX: Copy .buffer property from a DataView/TypedArray argument? + * Create a fresh ArrayBuffer for Duktape.Buffer and Node.js Buffer + * arguments? See: test-bug-dataview-buffer-prop.js. + */ + + duk_dup(ctx, 0); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE); + duk_compact(ctx, -1); + + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + return 1; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_dataview_constructor(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * ArrayBuffer.isView() + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_isview(duk_context *ctx) { + duk_hobject *h_obj; + duk_bool_t ret = 0; + + h_obj = duk_get_hobject(ctx, 0); + if (h_obj != NULL && DUK_HOBJECT_IS_BUFFEROBJECT(h_obj)) { + ret = ((duk_hbufferobject *) h_obj)->is_view; + } + duk_push_boolean(ctx, ret); + return 1; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_isview(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * Node.js Buffer: toString([encoding], [start], [end]) + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_context *ctx) { + duk_hthread *thr; + duk_hbufferobject *h_this; + duk_int_t start_offset, end_offset; + duk_uint8_t *buf_slice; + duk_size_t slice_length; + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + h_this = duk__get_bufobj_this(ctx); + if (h_this == NULL) { + /* XXX: happens e.g. when evaluating: String(Buffer.prototype). */ + duk_push_string(ctx, "[object Object]"); + return 1; + } + DUK_ASSERT_HBUFFEROBJECT_VALID(h_this); + + /* ignore encoding for now */ + + duk__clamp_startend_nonegidx_noshift(ctx, h_this, 1 /*idx_start*/, 2 /*idx_end*/, &start_offset, &end_offset); + + slice_length = (duk_size_t) (end_offset - start_offset); + buf_slice = (duk_uint8_t *) duk_push_fixed_buffer(ctx, slice_length); + DUK_ASSERT(buf_slice != NULL); + + if (h_this->buf == NULL) { + goto type_error; + } + + if (DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_this, start_offset + slice_length)) { + DUK_MEMCPY((void *) buf_slice, + (const void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + start_offset), + (size_t) slice_length); + } else { + /* not covered, return all zeroes */ + ; + } + + duk_to_string(ctx, -1); + return 1; + + type_error: + return DUK_RET_TYPE_ERROR; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * Duktape.Buffer: toString(), valueOf() + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_buffer_prototype_tostring_shared(duk_context *ctx) { + duk_hthread *thr; + duk_tval *tv; + duk_small_int_t to_string = duk_get_current_magic(ctx); + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + tv = duk_get_borrowed_this_tval(ctx); + DUK_ASSERT(tv != NULL); + + if (DUK_TVAL_IS_BUFFER(tv)) { + duk_hbuffer *h_buf; + h_buf = DUK_TVAL_GET_BUFFER(tv); + DUK_ASSERT(h_buf != NULL); + duk_push_hbuffer(ctx, h_buf); + } else if (DUK_TVAL_IS_OBJECT(tv)) { + duk_hobject *h; + duk_hbufferobject *h_bufobj; + + /* Accept any duk_hbufferobject, though we're only normally + * called for Duktape.Buffer values. + */ + h = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h != NULL); + if (!DUK_HOBJECT_IS_BUFFEROBJECT(h)) { + DUK_DD(DUK_DDPRINT("toString/valueOf() called for a non-bufferobject object")); + goto type_error; + } + h_bufobj = (duk_hbufferobject *) h; + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + + if (h_bufobj->buf == NULL) { + DUK_DD(DUK_DDPRINT("toString/valueOf() called for a bufferobject with NULL buf")); + goto type_error; + } + duk_push_hbuffer(ctx, h_bufobj->buf); + } else { + goto type_error; + } + + if (to_string) { + duk_to_string(ctx, -1); + } + return 1; + + type_error: + return DUK_RET_TYPE_ERROR; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_buffer_prototype_tostring_shared(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * Node.js Buffer.prototype: toJSON() + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tojson(duk_context *ctx) { + duk_hthread *thr; + duk_hbufferobject *h_this; + duk_uint8_t *buf; + duk_uint_t i; + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + h_this = duk__require_bufobj_this(ctx); + DUK_ASSERT(h_this != NULL); + + if (h_this->buf == NULL || !DUK_HBUFFEROBJECT_VALID_SLICE(h_this)) { + /* Serialize uncovered backing buffer as a null; doesn't + * really matter as long we're memory safe. + */ + duk_push_null(ctx); + return 1; + } + + duk_push_object(ctx); + duk_push_hstring_stridx(ctx, DUK_STRIDX_UC_BUFFER); + duk_put_prop_stridx(ctx, -2, DUK_STRIDX_TYPE); + + duk_push_array(ctx); + for (i = 0; i < h_this->length; i++) { + /* XXX: regetting the pointer may be overkill - we're writing + * to a side-effect free array here. + */ + DUK_ASSERT(h_this->buf != NULL); + buf = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this); + duk_push_uint(ctx, (duk_uint_t) buf[i]); + duk_put_prop_index(ctx, -2, (duk_idx_t) i); + } + duk_put_prop_stridx(ctx, -2, DUK_STRIDX_DATA); + + return 1; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tojson(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * Node.js Buffer.prototype.equals() + * Node.js Buffer.prototype.compare() + * Node.js Buffer.compare() + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_buffer_compare_shared(duk_context *ctx) { + duk_hthread *thr; + duk_small_uint_t magic; + duk_hbufferobject *h_bufarg1; + duk_hbufferobject *h_bufarg2; + duk_small_int_t comp_res; + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + magic = duk_get_current_magic(ctx); + if (magic & 0x02) { + /* Static call style. */ + h_bufarg1 = duk__require_bufobj_value(ctx, 0); + h_bufarg2 = duk__require_bufobj_value(ctx, 1); + } else { + h_bufarg1 = duk__require_bufobj_this(ctx); + h_bufarg2 = duk__require_bufobj_value(ctx, 0); + } + DUK_ASSERT(h_bufarg1 != NULL); + DUK_ASSERT(h_bufarg2 != NULL); + + /* We want to compare the slice/view areas of the arguments. + * If either slice/view is invalid (underlying buffer is shorter) + * ensure equals() is false, but otherwise the only thing that + * matters is to be memory safe. + */ + + if (DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg1) && + DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg2)) { + comp_res = duk_js_data_compare((const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufarg1->buf) + h_bufarg1->offset, + (const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufarg2->buf) + h_bufarg2->offset, + (duk_size_t) h_bufarg1->length, + (duk_size_t) h_bufarg2->length); + } else { + comp_res = -1; /* either nonzero value is ok */ + } + + if (magic & 0x01) { + /* compare: similar to string comparison but for buffer data. */ + duk_push_int(ctx, comp_res); + } else { + /* equals */ + duk_push_boolean(ctx, (comp_res == 0)); + } + + return 1; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_buffer_compare_shared(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * Node.js Buffer.prototype.fill() + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_fill(duk_context *ctx) { + duk_hthread *thr; + duk_hbufferobject *h_this; + const duk_uint8_t *fill_str_ptr; + duk_size_t fill_str_len; + duk_uint8_t fill_value; + duk_int_t fill_offset; + duk_int_t fill_end; + duk_size_t fill_length; + duk_uint8_t *p; + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + h_this = duk__require_bufobj_this(ctx); + DUK_ASSERT(h_this != NULL); + if (h_this->buf == NULL) { + return DUK_RET_TYPE_ERROR; + } + + /* [ value offset end ] */ + + if (duk_is_string(ctx, 0)) { + fill_str_ptr = (const duk_uint8_t *) duk_get_lstring(ctx, 0, &fill_str_len); + DUK_ASSERT(fill_str_ptr != NULL); + } else { + fill_value = (duk_uint8_t) duk_to_uint32(ctx, 0); + fill_str_ptr = (const duk_uint8_t *) &fill_value; + fill_str_len = 1; + } + + /* Fill offset handling is more lenient than in Node.js. */ + + duk__clamp_startend_nonegidx_noshift(ctx, h_this, 1 /*idx_start*/, 2 /*idx_end*/, &fill_offset, &fill_end); + + DUK_DDD(DUK_DDDPRINT("fill: fill_value=%02x, fill_offset=%ld, fill_end=%ld, view length=%ld", + (unsigned int) fill_value, (long) fill_offset, (long) fill_end, (long) h_this->length)); + + DUK_ASSERT(fill_end - fill_offset >= 0); + DUK_ASSERT(h_this->buf != NULL); + + p = (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + fill_offset); + fill_length = (duk_size_t) (fill_end - fill_offset); + if (fill_str_len == 1) { + /* Handle single character fills as memset() even when + * the fill data comes from a one-char argument. + */ + DUK_MEMSET((void *) p, (int) fill_str_ptr[0], (size_t) fill_length); + } else if (fill_str_len > 1) { + duk_size_t i, n, t; + + for (i = 0, n = (fill_end - fill_offset), t = 0; i < n; i++) { + p[i] = fill_str_ptr[t++]; + if (t >= fill_str_len) { + t = 0; + } + } + } else { + DUK_DDD(DUK_DDDPRINT("zero size fill pattern, ignore silently")); + } + + /* Return the Buffer to allow chaining: b.fill(0x11).fill(0x22, 3, 5).toString() */ + duk_push_this(ctx); + return 1; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_fill(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * Node.js Buffer.prototype.write(string, [offset], [length], [encoding]) + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_write(duk_context *ctx) { + duk_hthread *thr; + duk_hbufferobject *h_this; + duk_uint_t offset; + duk_uint_t length; + const duk_uint8_t *str_data; + duk_size_t str_len; + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + h_this = duk__require_bufobj_this(ctx); + DUK_ASSERT(h_this != NULL); + + /* Argument must be a string, e.g. a buffer is not allowed. */ + str_data = (const duk_uint8_t *) duk_require_lstring(ctx, 0, &str_len); + + duk__resolve_offset_opt_length(ctx, h_this, 1, 2, &offset, &length, 0 /*throw_flag*/); + DUK_ASSERT(offset <= h_this->length); + DUK_ASSERT(offset + length <= h_this->length); + + /* XXX: encoding is ignored now. */ + + if (length > str_len) { + length = (duk_uint_t) str_len; + } + + if (DUK_HBUFFEROBJECT_VALID_SLICE(h_this)) { + /* Cannot overlap. */ + DUK_MEMCPY((void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + offset), + (const void *) str_data, + (size_t) length); + } else { + DUK_DDD(DUK_DDDPRINT("write() target buffer is not covered, silent ignore")); + } + + duk_push_uint(ctx, length); + return 1; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_write(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * Node.js Buffer.prototype.copy() + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_copy(duk_context *ctx) { + duk_hthread *thr; + duk_hbufferobject *h_this; + duk_hbufferobject *h_bufarg; + duk_int_t source_length; + duk_int_t target_length; + duk_int_t target_start, source_start, source_end; + duk_uint_t target_ustart, source_ustart, source_uend; + duk_uint_t copy_size = 0; + + /* [ targetBuffer targetStart sourceStart sourceEnd ] */ + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + h_this = duk__require_bufobj_this(ctx); + h_bufarg = duk__require_bufobj_value(ctx, 0); + DUK_ASSERT(h_this != NULL); + DUK_ASSERT(h_bufarg != NULL); + source_length = (duk_int_t) h_this->length; + target_length = (duk_int_t) h_bufarg->length; + + target_start = duk_to_int(ctx, 1); + source_start = duk_to_int(ctx, 2); + if (duk_is_undefined(ctx, 3)) { + source_end = source_length; + } else { + source_end = duk_to_int(ctx, 3); + } + + DUK_DDD(DUK_DDDPRINT("checking copy args: target_start=%ld, target_length=%ld, " + "source_start=%ld, source_end=%ld, source_length=%ld", + (long) target_start, (long) h_bufarg->length, + (long) source_start, (long) source_end, (long) source_length)); + + /* This behavior mostly mimics Node.js now. */ + + if (source_start < 0 || source_end < 0 || target_start < 0) { + /* Negative offsets cause a RangeError. */ + goto fail_bounds; + } + source_ustart = (duk_uint_t) source_start; + source_uend = (duk_uint_t) source_end; + target_ustart = (duk_uint_t) target_start; + if (source_ustart >= source_uend || /* crossed offsets or zero size */ + source_ustart >= (duk_uint_t) source_length || /* source out-of-bounds (but positive) */ + target_ustart >= (duk_uint_t) target_length) { /* target out-of-bounds (but positive) */ + goto silent_ignore; + } + if (source_uend >= (duk_uint_t) source_length) { + /* Source end clamped silently to available length. */ + source_uend = source_length; + } + copy_size = source_uend - source_ustart; + if (target_ustart + copy_size > (duk_uint_t) target_length) { + /* Clamp to target's end if too long. + * + * NOTE: there's no overflow possibility in the comparison; + * both target_ustart and copy_size are >= 0 and based on + * values in duk_int_t range. Adding them as duk_uint_t + * values is then guaranteed not to overflow. + */ + DUK_ASSERT(target_ustart + copy_size >= target_ustart); /* no overflow */ + DUK_ASSERT(target_ustart + copy_size >= copy_size); /* no overflow */ + copy_size = (duk_uint_t) target_length - target_ustart; + } + + DUK_DDD(DUK_DDDPRINT("making copy: target_ustart=%lu source_ustart=%lu copy_size=%lu", + (unsigned long) target_ustart, (unsigned long) source_ustart, + (unsigned long) copy_size)); + + DUK_ASSERT(copy_size >= 1); + DUK_ASSERT(source_ustart <= (duk_uint_t) source_length); + DUK_ASSERT(source_ustart + copy_size <= (duk_uint_t) source_length); + DUK_ASSERT(target_ustart <= (duk_uint_t) target_length); + DUK_ASSERT(target_ustart + copy_size <= (duk_uint_t) target_length); + + /* Ensure copy is covered by underlying buffers. */ + DUK_ASSERT(h_bufarg->buf != NULL); /* length check */ + DUK_ASSERT(h_this->buf != NULL); /* length check */ + if (DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_bufarg, target_ustart + copy_size) && + DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_this, source_ustart + copy_size)) { + /* Must use memmove() because copy area may overlap (source and target + * buffer may be the same, or from different slices. + */ + DUK_MEMMOVE((void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg) + target_ustart), + (const void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + source_ustart), + (size_t) copy_size); + } else { + DUK_DDD(DUK_DDDPRINT("buffer copy not covered by underlying buffer(s), ignoring")); + } + + silent_ignore: + /* Return value is like write(), number of bytes written. + * The return value matters because of code like: + * "off += buf.copy(...)". + */ + duk_push_uint(ctx, copy_size); + return 1; + + fail_bounds: + return DUK_RET_RANGE_ERROR; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_copy(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * TypedArray.prototype.set() + * + * TypedArray set() is pretty interesting to implement because: + * + * - The source argument may be a plain array or a typedarray. If the + * source is a TypedArray, values are decoded and re-encoded into the + * target (not as a plain byte copy). This may happen even when the + * element byte size is the same, e.g. integer values may be re-encoded + * into floats. + * + * - Source and target may refer to the same underlying buffer, so that + * the set() operation may overlap. The specification requires that this + * must work as if a copy was made before the operation. Note that this + * is NOT a simple memmove() situation because the source and target + * byte sizes may be different -- e.g. a 4-byte source (Int8Array) may + * expand to a 16-byte target (Uint32Array) so that the target overlaps + * the source both from beginning and the end (unlike in typical memmove). + * + * - Even if 'buf' pointers of the source and target differ, there's no + * guarantee that their memory areas don't overlap. This may be the + * case with external buffers. + * + * Even so, it is nice to optimize for the common case: + * + * - Source and target separate buffers or non-overlapping. + * + * - Source and target have a compatible type so that a plain byte copy + * is possible. Note that while e.g. uint8 and int8 are compatible + * (coercion one way or another doesn't change the byte representation), + * e.g. int8 and uint8clamped are NOT compatible when writing int8 + * values into uint8clamped typedarray (-1 would clamp to 0 for instance). + * + * See test-bi-typedarray-proto-set.js. + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_typedarray_set(duk_context *ctx) { + duk_hthread *thr; + duk_hbufferobject *h_this; + duk_hobject *h_obj; + duk_uarridx_t i, n; + duk_int_t offset_signed; + duk_uint_t offset_elems; + duk_uint_t offset_bytes; + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + h_this = duk__require_bufobj_this(ctx); + DUK_ASSERT(h_this != NULL); + DUK_ASSERT_HBUFFEROBJECT_VALID(h_this); + + if (h_this->buf == NULL) { + DUK_DDD(DUK_DDDPRINT("source neutered, skip copy")); + return 0; + } + + h_obj = duk_require_hobject(ctx, 0); + DUK_ASSERT(h_obj != NULL); + + /* XXX: V8 throws a TypeError for negative values. Would it + * be more useful to interpret negative offsets here from the + * end of the buffer too? + */ + offset_signed = duk_to_int(ctx, 1); + if (offset_signed < 0) { + return DUK_RET_TYPE_ERROR; + } + offset_elems = (duk_uint_t) offset_signed; + offset_bytes = offset_elems << h_this->shift; + if ((offset_bytes >> h_this->shift) != offset_elems) { + /* Byte length would overflow. */ + /* XXX: easier check with less code? */ + return DUK_RET_RANGE_ERROR; + } + if (offset_bytes > h_this->length) { + /* Equality may be OK but >length not. Checking + * this explicitly avoids some overflow cases + * below. + */ + return DUK_RET_RANGE_ERROR; + } + DUK_ASSERT(offset_bytes <= h_this->length); + + /* Fast path: source is a TypedArray (or any bufferobject). */ + + if (DUK_HOBJECT_IS_BUFFEROBJECT(h_obj)) { + duk_hbufferobject *h_bufarg; + duk_uint16_t comp_mask; + duk_small_int_t no_overlap = 0; + duk_uint_t src_length; + duk_uint_t dst_length; + duk_uint_t dst_length_elems; + duk_uint8_t *p_src_base; + duk_uint8_t *p_src_end; + duk_uint8_t *p_src; + duk_uint8_t *p_dst_base; + duk_uint8_t *p_dst; + duk_small_uint_t src_elem_size; + duk_small_uint_t dst_elem_size; + + h_bufarg = (duk_hbufferobject *) h_obj; + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufarg); + + if (h_bufarg->buf == NULL) { + DUK_DDD(DUK_DDDPRINT("target neutered, skip copy")); + return 0; + } + + /* Nominal size check. */ + src_length = h_bufarg->length; /* bytes in source */ + dst_length_elems = (src_length >> h_bufarg->shift); /* elems in source and dest */ + dst_length = dst_length_elems << h_this->shift; /* bytes in dest */ + if ((dst_length >> h_this->shift) != dst_length_elems) { + /* Byte length would overflow. */ + /* XXX: easier check with less code? */ + return DUK_RET_RANGE_ERROR; + } + DUK_DDD(DUK_DDDPRINT("nominal size check: src_length=%ld, dst_length=%ld", + (long) src_length, (long) dst_length)); + DUK_ASSERT(offset_bytes <= h_this->length); + if (dst_length > h_this->length - offset_bytes) { + /* Overflow not an issue because subtraction is used on the right + * side and guaranteed to be >= 0. + */ + DUK_DDD(DUK_DDDPRINT("copy exceeds target buffer nominal length")); + return DUK_RET_RANGE_ERROR; + } + if (!DUK_HBUFFEROBJECT_VALID_BYTEOFFSET_EXCL(h_this, offset_bytes + dst_length)) { + DUK_DDD(DUK_DDDPRINT("copy not covered by underlying target buffer, ignore")); + return 0; + } + + p_src_base = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufarg); + p_dst_base = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + offset_bytes; + + /* Check actual underlying buffers for validity and that they + * cover the copy. No side effects are allowed after the check + * so that the validity status doesn't change. + */ + if (!DUK_HBUFFEROBJECT_VALID_SLICE(h_this) || + !DUK_HBUFFEROBJECT_VALID_SLICE(h_bufarg)) { + /* The condition could be more narrow and check for the + * copy area only, but there's no need for fine grained + * behavior when the underlying buffer is misconfigured. + */ + DUK_DDD(DUK_DDDPRINT("source and/or target not covered by underlying buffer, skip copy")); + return 0; + } + + /* We want to do a straight memory copy if possible: this is + * an important operation because .set() is the TypedArray + * way to copy chunks of memory. However, because set() + * conceptually works in terms of elements, not all views are + * compatible with direct byte copying. + * + * If we do manage a direct copy, the "overlap issue" handled + * below can just be solved using memmove() because the source + * and destination element sizes are necessarily equal. + */ + + DUK_ASSERT(h_this->elem_type < sizeof(duk__buffer_elemtype_copy_compatible) / sizeof(duk_uint16_t)); + comp_mask = duk__buffer_elemtype_copy_compatible[h_this->elem_type]; + if (comp_mask & (1 << h_bufarg->elem_type)) { + DUK_ASSERT(src_length == dst_length); + + DUK_DDD(DUK_DDDPRINT("fast path: able to use memmove() because views are compatible")); + DUK_MEMMOVE((void *) p_dst_base, (const void *) p_src_base, (size_t) dst_length); + return 0; + } + DUK_DDD(DUK_DDDPRINT("fast path: views are not compatible with a byte copy, copy by item")); + + /* We want to avoid making a copy to process set() but that's + * not always possible: the source and the target may overlap + * and because element sizes are different, the overlap cannot + * always be handled with a memmove() or choosing the copy + * direction in a certain way. For example, if source type is + * uint8 and target type is uint32, the target area may exceed + * the source area from both ends! + * + * Note that because external buffers may point to the same + * memory areas, we must ultimately make this check using + * pointers. + * + * NOTE: careful with side effects: any side effect may cause + * a buffer resize (or external buffer pointer/length update)! + */ + + DUK_DDD(DUK_DDDPRINT("overlap check: p_src_base=%p, src_length=%ld, " + "p_dst_base=%p, dst_length=%ld", + (void *) p_src_base, (long) src_length, + (void *) p_dst_base, (long) dst_length)); + + if (p_src_base >= p_dst_base + dst_length || /* source starts after dest ends */ + p_src_base + src_length <= p_dst_base) { /* source ends before dest starts */ + no_overlap = 1; + } + + if (!no_overlap) { + /* There's overlap: the desired end result is that + * conceptually a copy is made to avoid "trampling" + * of source data by destination writes. We make + * an actual temporary copy to handle this case. + */ + duk_uint8_t *p_src_copy; + + DUK_DDD(DUK_DDDPRINT("there is overlap, make a copy of the source")); + p_src_copy = (duk_uint8_t *) duk_push_fixed_buffer(ctx, src_length); + DUK_ASSERT(p_src_copy != NULL); + DUK_MEMCPY((void *) p_src_copy, (const void *) p_src_base, (size_t) src_length); + + p_src_base = p_src_copy; /* use p_src_base from now on */ + } + /* Value stack intentionally mixed size here. */ + + DUK_DDD(DUK_DDDPRINT("after overlap check: p_src_base=%p, src_length=%ld, " + "p_dst_base=%p, dst_length=%ld, valstack top=%ld", + (void *) p_src_base, (long) src_length, + (void *) p_dst_base, (long) dst_length, + (long) duk_get_top(ctx))); + + /* Ready to make the copy. We must proceed element by element + * and must avoid any side effects that might cause the buffer + * validity check above to become invalid. + * + * Although we work through the value stack here, only plain + * numbers are handled which should be side effect safe. + */ + + src_elem_size = 1 << h_bufarg->shift; + dst_elem_size = 1 << h_this->shift; + p_src = p_src_base; + p_dst = p_dst_base; + p_src_end = p_src_base + src_length; + + while (p_src != p_src_end) { + DUK_DDD(DUK_DDDPRINT("fast path per element copy loop: " + "p_src=%p, p_src_end=%p, p_dst=%p", + (void *) p_src, (void *) p_src_end, (void *) p_dst)); + /* A validated read() is always a number, so it's write coercion + * is always side effect free an won't invalidate pointers etc. + */ + duk_hbufferobject_push_validated_read(ctx, h_bufarg, p_src, src_elem_size); + duk_hbufferobject_validated_write(ctx, h_this, p_dst, dst_elem_size); + duk_pop(ctx); + p_src += src_elem_size; + p_dst += dst_elem_size; + } + + return 0; + } else { + /* Slow path: quite slow, but we save space by using the property code + * to write coerce target values. We don't need to worry about overlap + * here because the source is not a TypedArray. + * + * We could use the bufferobject write coercion helper but since the + * property read may have arbitrary side effects, full validity checks + * would be needed for every element anyway. + */ + + n = (duk_uarridx_t) duk_get_length(ctx, 0); + DUK_ASSERT(offset_bytes <= h_this->length); + if ((n << h_this->shift) > h_this->length - offset_bytes) { + /* Overflow not an issue because subtraction is used on the right + * side and guaranteed to be >= 0. + */ + DUK_DDD(DUK_DDDPRINT("copy exceeds target buffer nominal length")); + return DUK_RET_RANGE_ERROR; + } + + /* There's no need to check for buffer validity status for the + * target here: the property access code will do that for each + * element. Moreover, if we did check the validity here, side + * effects from reading the source argument might invalidate + * the results anyway. + */ + + DUK_ASSERT_TOP(ctx, 2); + duk_push_this(ctx); + + for (i = 0; i < n; i++) { + duk_get_prop_index(ctx, 0, i); + duk_put_prop_index(ctx, 2, offset_elems + i); + } + } + + return 0; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_typedarray_set(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * Node.js Buffer.prototype.slice([start], [end]) + * ArrayBuffer.prototype.slice(begin, [end]) + * TypedArray.prototype.slice(begin, [end]) + * + * The API calls are almost identical; negative indices are counted from end + * of buffer, and final indices are clamped (allowing crossed indices). Main + * differences: + * + * - Copy/view behavior; Node.js .slice() and TypedArray .subarray() create + * views, ArrayBuffer .slice() creates a copy + * + * - Resulting object has a different class and prototype depending on the + * call (or 'this' argument) + * + * - TypedArray .subarray() arguments are element indices, not byte offsets + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_buffer_slice_shared(duk_context *ctx) { + duk_hthread *thr; + duk_small_int_t magic; + duk_small_uint_t res_class_num; + duk_hobject *res_proto; + duk_hbufferobject *h_this; + duk_hbufferobject *h_bufobj; + duk_hbuffer *h_val; + duk_int_t start_offset, end_offset; + duk_uint_t slice_length; + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + /* [ start end ] */ + + magic = duk_get_current_magic(ctx); + h_this = duk__require_bufobj_this(ctx); + + /* Slice offsets are element (not byte) offsets, which only matters + * for TypedArray views, Node.js Buffer and ArrayBuffer have shift + * zero so byte and element offsets are the same. Negative indices + * are counted from end of slice, crossed indices are allowed (and + * result in zero length result), and final values are clamped + * against the current slice. There's intentionally no check + * against the underlying buffer here. + */ + + duk__clamp_startend_negidx_shifted(ctx, h_this, 0 /*idx_start*/, 1 /*idx_end*/, &start_offset, &end_offset); + DUK_ASSERT(end_offset >= start_offset); + slice_length = (duk_uint_t) (end_offset - start_offset); + + /* The resulting buffer object gets the same class and prototype as + * the buffer in 'this', e.g. if the input is a Node.js Buffer the + * result is a Node.js Buffer; if the input is a Float32Array, the + * result is a Float32Array. + * + * For the class number this seems correct. The internal prototype + * is not so clear: if 'this' is a bufferobject with a non-standard + * prototype object, that value gets copied over into the result + * (instead of using the standard prototype for that object type). + */ + + res_class_num = DUK_HOBJECT_GET_CLASS_NUMBER((duk_hobject *) h_this); + h_bufobj = duk_push_bufferobject_raw(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_BUFFEROBJECT | + DUK_HOBJECT_CLASS_AS_FLAGS(res_class_num), + DUK_BIDX_OBJECT_PROTOTYPE); /* replaced */ + DUK_ASSERT(h_bufobj != NULL); + res_proto = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) h_this); /* may be NULL */ + DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) h_bufobj, res_proto); + + h_bufobj->length = slice_length; + h_bufobj->shift = h_this->shift; /* inherit */ + h_bufobj->elem_type = h_this->elem_type; /* inherit */ + h_bufobj->is_view = magic & 0x01; + DUK_ASSERT(h_bufobj->is_view == 0 || h_bufobj->is_view == 1); + + h_val = h_this->buf; + if (h_val == NULL) { + return DUK_RET_TYPE_ERROR; + } + + if (magic & 0x02) { + /* non-zero: make copy */ + duk_uint8_t *p_copy; + duk_size_t copy_length; + + p_copy = (duk_uint8_t *) duk_push_fixed_buffer(ctx, (duk_size_t) slice_length); + DUK_ASSERT(p_copy != NULL); + + /* Copy slice, respecting underlying buffer limits; remainder + * is left as zero. + */ + copy_length = DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h_this, slice_length); + DUK_MEMCPY((void *) p_copy, + (const void *) (DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this) + start_offset), + copy_length); + + h_val = duk_get_hbuffer(ctx, -1); + DUK_ASSERT(h_val != NULL); + + h_bufobj->buf = h_val; + DUK_HBUFFER_INCREF(thr, h_val); + DUK_ASSERT(h_bufobj->offset == 0); + + duk_pop(ctx); /* reachable so pop OK */ + } else { + h_bufobj->buf = h_val; + DUK_HBUFFER_INCREF(thr, h_val); + h_bufobj->offset = (duk_uint_t) (h_this->offset + start_offset); + + /* Copy the .buffer property, needed for TypedArray.prototype.subarray(). + * + * XXX: limit copy only for TypedArray classes specifically? + */ + + duk_push_this(ctx); + if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LC_BUFFER)) { + duk_xdef_prop_stridx(ctx, -3, DUK_STRIDX_LC_BUFFER, DUK_PROPDESC_FLAGS_NONE); + duk_pop(ctx); + } else { + duk_pop_2(ctx); + } + } + /* unbalanced stack on purpose */ + + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + return 1; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_buffer_slice_shared(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * Node.js Buffer.isEncoding() + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_encoding(duk_context *ctx) { + const char *encoding; + + /* only accept lowercase 'utf8' now. */ + + encoding = duk_to_string(ctx, 0); + DUK_ASSERT(duk_is_string(ctx, 0)); /* guaranteed by duk_to_string() */ + duk_push_boolean(ctx, DUK_STRCMP(encoding, "utf8") == 0); + return 1; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_encoding(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * Node.js Buffer.isBuffer() + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_buffer(duk_context *ctx) { + duk_hthread *thr; + duk_tval *tv; + duk_hobject *h; + duk_hobject *h_proto; + duk_bool_t ret = 0; + + thr = (duk_hthread *) ctx; + + DUK_ASSERT(duk_get_top(ctx) >= 1); /* nargs */ + tv = duk_get_tval(ctx, 0); + DUK_ASSERT(tv != NULL); + + if (DUK_TVAL_IS_OBJECT(tv)) { + h = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h != NULL); + + h_proto = thr->builtins[DUK_BIDX_NODEJS_BUFFER_PROTOTYPE]; + DUK_ASSERT(h_proto != NULL); + + h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h); + if (h) { + ret = duk_hobject_prototype_chain_contains(thr, h, h_proto, 0 /*ignore_loop*/); + } + } + + duk_push_boolean(ctx, ret); + return 1; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_buffer(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * Node.js Buffer.byteLength() + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_byte_length(duk_context *ctx) { + const char *str; + duk_size_t len; + + /* At the moment Buffer() will just use the string bytes as + * is (ignoring encoding), so we return the string length here + * unconditionally. + */ + + str = duk_to_lstring(ctx, 0, &len); + DUK_UNREF(str); + duk_push_size_t(ctx, len); + return 1; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_byte_length(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * Node.js Buffer.concat() + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_concat(duk_context *ctx) { + duk_hthread *thr; + duk_hobject *h_arg; + duk_int_t total_length = 0; + duk_hbufferobject *h_bufobj; + duk_hbufferobject *h_bufres; + duk_hbuffer *h_val; + duk_uint_t i, n; + duk_uint8_t *p; + duk_size_t space_left; + duk_size_t copy_size; + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + /* Node.js accepts only actual Arrays. */ + h_arg = duk_require_hobject(ctx, 0); + if (DUK_HOBJECT_GET_CLASS_NUMBER(h_arg) != DUK_HOBJECT_CLASS_ARRAY) { + return DUK_RET_TYPE_ERROR; + } + + /* Compute result length and validate argument buffers. */ + n = (duk_uint_t) duk_get_length(ctx, 0); + for (i = 0; i < n; i++) { + /* Neutered checks not necessary here: neutered buffers have + * zero 'length' so we'll effectively skip them. + */ + DUK_ASSERT_TOP(ctx, 2); /* [ array totalLength ] */ + duk_get_prop_index(ctx, 0, (duk_uarridx_t) i); /* -> [ array totalLength buf ] */ + h_bufobj = duk__require_bufobj_value(ctx, 2); + DUK_ASSERT(h_bufobj != NULL); + total_length += h_bufobj->length; + duk_pop(ctx); + } + if (n == 1) { + /* For the case n==1 Node.js doesn't seem to type check + * the sole member but we do it before returning it. + * For this case only the original buffer object is + * returned (not a copy). + */ + duk_get_prop_index(ctx, 0, 0); + return 1; + } + + /* User totalLength overrides a computed length, but we'll check + * every copy in the copy loop. Note that duk_to_uint() can + * technically have arbitrary side effects so we need to recheck + * the buffers in the copy loop. + */ + if (!duk_is_undefined(ctx, 1) && n > 0) { + /* For n == 0, Node.js ignores totalLength argument and + * returns a zero length buffer. + */ + total_length = duk_to_int(ctx, 1); + } + if (total_length < 0) { + return DUK_RET_RANGE_ERROR; + } + + h_bufres = duk_push_bufferobject_raw(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_BUFFEROBJECT | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BUFFER), + DUK_BIDX_NODEJS_BUFFER_PROTOTYPE); + DUK_ASSERT(h_bufres != NULL); + + p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, total_length); + DUK_ASSERT(p != NULL); + space_left = total_length; + + for (i = 0; i < n; i++) { + DUK_ASSERT_TOP(ctx, 4); /* [ array totalLength bufres buf ] */ + + duk_get_prop_index(ctx, 0, (duk_uarridx_t) i); + h_bufobj = duk__require_bufobj_value(ctx, 4); + DUK_ASSERT(h_bufobj != NULL); + + copy_size = h_bufobj->length; + if (copy_size > space_left) { + copy_size = space_left; + } + + if (h_bufobj->buf != NULL && + DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj)) { + DUK_MEMCPY((void *) p, + (const void *) DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_bufobj), + copy_size); + } else { + /* Just skip, leaving zeroes in the result. */ + ; + } + p += copy_size; + space_left -= copy_size; + + duk_pop(ctx); + } + + h_val = duk_get_hbuffer(ctx, -1); + DUK_ASSERT(h_val != NULL); + + duk__set_bufobj_buffer(ctx, h_bufres, h_val); + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufres); + + duk_pop(ctx); /* pop plain buffer, now reachable through h_bufres */ + + return 1; /* return h_bufres */ +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_concat(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +/* + * Shared readfield and writefield methods + * + * The readfield/writefield methods need support for endianness and field + * types. All offsets are byte based so no offset shifting is needed. + */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +/* Format of magic, bits: + * 0...1: field type; 0=uint8, 1=uint16, 2=uint32, 3=float, 4=double, 5=unused, 6=unused, 7=unused + * 3: endianness: 0=little, 1=big + * 4: signed: 1=yes, 0=no + * 5: typedarray: 1=yes, 0=no + */ +#define DUK__FLD_8BIT 0 +#define DUK__FLD_16BIT 1 +#define DUK__FLD_32BIT 2 +#define DUK__FLD_FLOAT 3 +#define DUK__FLD_DOUBLE 4 +#define DUK__FLD_VARINT 5 +#define DUK__FLD_BIGENDIAN (1 << 3) +#define DUK__FLD_SIGNED (1 << 4) +#define DUK__FLD_TYPEDARRAY (1 << 5) + +/* XXX: split into separate functions for each field type? */ +DUK_INTERNAL duk_ret_t duk_bi_buffer_readfield(duk_context *ctx) { + duk_hthread *thr; + duk_small_int_t magic = (duk_small_int_t) duk_get_current_magic(ctx); + duk_small_int_t magic_ftype; + duk_small_int_t magic_bigendian; + duk_small_int_t magic_signed; + duk_small_int_t magic_typedarray; + duk_small_int_t endswap; + duk_hbufferobject *h_this; + duk_bool_t no_assert; + duk_int_t offset_signed; + duk_uint_t offset; + duk_uint_t buffer_length; + duk_uint_t check_length; + duk_uint8_t *buf; + duk_double_union du; + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + magic_ftype = magic & 0x0007; + magic_bigendian = magic & 0x0008; + magic_signed = magic & 0x0010; + magic_typedarray = magic & 0x0020; + + h_this = duk__require_bufobj_this(ctx); + DUK_ASSERT(h_this != NULL); + buffer_length = h_this->length; + + /* [ offset noAssert ], when ftype != DUK__FLD_VARINT */ + /* [ offset fieldByteLength noAssert ], when ftype == DUK__FLD_VARINT */ + /* [ offset littleEndian ], when DUK__FLD_TYPEDARRAY (regardless of ftype) */ + + /* Handle TypedArray vs. Node.js Buffer arg differences */ + if (magic_typedarray) { + no_assert = 0; +#if defined(DUK_USE_INTEGER_LE) + endswap = !duk_to_boolean(ctx, 1); /* 1=little endian */ +#else + endswap = duk_to_boolean(ctx, 1); /* 1=little endian */ +#endif + } else { + no_assert = duk_to_boolean(ctx, (magic_ftype == DUK__FLD_VARINT) ? 2 : 1); +#if defined(DUK_USE_INTEGER_LE) + endswap = magic_bigendian; +#else + endswap = !magic_bigendian; +#endif + } + + /* Offset is coerced first to signed integer range and then to unsigned. + * This ensures we can add a small byte length (1-8) to the offset in + * bound checks and not wrap. + */ + offset_signed = duk_to_int(ctx, 0); + offset = (duk_uint_t) offset_signed; + if (offset_signed < 0) { + goto fail_bounds; + } + + DUK_DDD(DUK_DDDPRINT("readfield, buffer_length=%ld, offset=%ld, no_assert=%d, " + "magic=%04x, magic_fieldtype=%d, magic_bigendian=%d, magic_signed=%d, " + "endswap=%d", + (long) buffer_length, (long) offset, (int) no_assert, + (unsigned int) magic, (int) magic_ftype, (int) (magic_bigendian >> 3), + (int) (magic_signed >> 4), (int) endswap)); + + /* Update 'buffer_length' to be the effective, safe limit which + * takes into account the underlying buffer. This value will be + * potentially invalidated by any side effect. + */ + check_length = DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h_this, buffer_length); + DUK_DDD(DUK_DDDPRINT("buffer_length=%ld, check_length=%ld", + (long) buffer_length, (long) check_length)); + + if (h_this->buf) { + buf = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this); + } else { + /* Neutered. We could go into the switch-case safely with + * buf == NULL because check_length == 0. To avoid scanbuild + * warnings, fail directly instead. + */ + DUK_ASSERT(check_length == 0); + goto fail_neutered; + } + DUK_ASSERT(buf != NULL); + + switch (magic_ftype) { + case DUK__FLD_8BIT: { + duk_uint8_t tmp; + if (offset + 1U > check_length) { + goto fail_bounds; + } + tmp = buf[offset]; + if (magic_signed) { + duk_push_int(ctx, (duk_int_t) ((duk_int8_t) tmp)); + } else { + duk_push_uint(ctx, (duk_uint_t) tmp); + } + break; + } + case DUK__FLD_16BIT: { + duk_uint16_t tmp; + if (offset + 2U > check_length) { + goto fail_bounds; + } + DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 2); + tmp = du.us[0]; + if (endswap) { + tmp = DUK_BSWAP16(tmp); + } + if (magic_signed) { + duk_push_int(ctx, (duk_int_t) ((duk_int16_t) tmp)); + } else { + duk_push_uint(ctx, (duk_uint_t) tmp); + } + break; + } + case DUK__FLD_32BIT: { + duk_uint32_t tmp; + if (offset + 4U > check_length) { + goto fail_bounds; + } + DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 4); + tmp = du.ui[0]; + if (endswap) { + tmp = DUK_BSWAP32(tmp); + } + if (magic_signed) { + duk_push_int(ctx, (duk_int_t) ((duk_int32_t) tmp)); + } else { + duk_push_uint(ctx, (duk_uint_t) tmp); + } + break; + } + case DUK__FLD_FLOAT: { + duk_uint32_t tmp; + if (offset + 4U > check_length) { + goto fail_bounds; + } + DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 4); + if (endswap) { + tmp = du.ui[0]; + tmp = DUK_BSWAP32(tmp); + du.ui[0] = tmp; + } + duk_push_number(ctx, (duk_double_t) du.f[0]); + break; + } + case DUK__FLD_DOUBLE: { + if (offset + 8U > check_length) { + goto fail_bounds; + } + DUK_MEMCPY((void *) du.uc, (const void *) (buf + offset), 8); + if (endswap) { + DUK_DBLUNION_BSWAP64(&du); + } + duk_push_number(ctx, (duk_double_t) du.d); + break; + } + case DUK__FLD_VARINT: { + /* Node.js Buffer variable width integer field. We don't really + * care about speed here, so aim for shortest algorithm. + */ + duk_int_t field_bytelen; + duk_int_t i, i_step, i_end; +#if defined(DUK_USE_64BIT_OPS) + duk_int64_t tmp; + duk_small_uint_t shift_tmp; +#else + duk_double_t tmp; + duk_small_int_t highbyte; +#endif + const duk_uint8_t *p; + + field_bytelen = duk_get_int(ctx, 1); /* avoid side effects! */ + if (field_bytelen < 1 || field_bytelen > 6) { + goto fail_field_length; + } + if (offset + (duk_uint_t) field_bytelen > check_length) { + goto fail_bounds; + } + p = (const duk_uint8_t *) (buf + offset); + + /* Slow gathering of value using either 64-bit arithmetic + * or IEEE doubles if 64-bit types not available. Handling + * of negative numbers is a bit non-obvious in both cases. + */ + + if (magic_bigendian) { + /* Gather in big endian */ + i = 0; + i_step = 1; + i_end = field_bytelen; /* one i_step over */ + } else { + /* Gather in little endian */ + i = field_bytelen - 1; + i_step = -1; + i_end = -1; /* one i_step over */ + } + +#if defined(DUK_USE_64BIT_OPS) + tmp = 0; + do { + DUK_ASSERT(i >= 0 && i < field_bytelen); + tmp = (tmp << 8) + (duk_int64_t) p[i]; + i += i_step; + } while (i != i_end); + + if (magic_signed) { + /* Shift to sign extend. */ + shift_tmp = 64 - (field_bytelen * 8); + tmp = (tmp << shift_tmp) >> shift_tmp; + } + + duk_push_i64(ctx, tmp); +#else + highbyte = p[i]; + if (magic_signed && (highbyte & 0x80) != 0) { + /* 0xff => 255 - 256 = -1; 0x80 => 128 - 256 = -128 */ + tmp = (duk_double_t) (highbyte - 256); + } else { + tmp = (duk_double_t) highbyte; + } + for (;;) { + i += i_step; + if (i == i_end) { + break; + } + DUK_ASSERT(i >= 0 && i < field_bytelen); + tmp = (tmp * 256.0) + (duk_double_t) p[i]; + } + + duk_push_number(ctx, tmp); +#endif + break; + } + default: { /* should never happen but default here */ + goto fail_bounds; + } + } + + return 1; + + fail_neutered: + fail_field_length: + fail_bounds: + if (no_assert) { + /* Node.js return value for noAssert out-of-bounds reads is + * usually (but not always) NaN. Return NaN consistently. + */ + duk_push_nan(ctx); + return 1; + } + + return DUK_RET_RANGE_ERROR; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_buffer_readfield(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) +/* XXX: split into separate functions for each field type? */ +DUK_INTERNAL duk_ret_t duk_bi_buffer_writefield(duk_context *ctx) { + duk_hthread *thr; + duk_small_int_t magic = (duk_small_int_t) duk_get_current_magic(ctx); + duk_small_int_t magic_ftype; + duk_small_int_t magic_bigendian; + duk_small_int_t magic_signed; + duk_small_int_t magic_typedarray; + duk_small_int_t endswap; + duk_hbufferobject *h_this; + duk_bool_t no_assert; + duk_int_t offset_signed; + duk_uint_t offset; + duk_uint_t buffer_length; + duk_uint_t check_length; + duk_uint8_t *buf; + duk_double_union du; + duk_int_t nbytes = 0; + + thr = (duk_hthread *) ctx; + DUK_UNREF(thr); + + magic_ftype = magic & 0x0007; + magic_bigendian = magic & 0x0008; + magic_signed = magic & 0x0010; + magic_typedarray = magic & 0x0020; + DUK_UNREF(magic_signed); + + h_this = duk__require_bufobj_this(ctx); + DUK_ASSERT(h_this != NULL); + buffer_length = h_this->length; + + /* [ value offset noAssert ], when ftype != DUK__FLD_VARINT */ + /* [ value offset fieldByteLength noAssert ], when ftype == DUK__FLD_VARINT */ + /* [ offset value littleEndian ], when DUK__FLD_TYPEDARRAY (regardless of ftype) */ + + /* Handle TypedArray vs. Node.js Buffer arg differences */ + if (magic_typedarray) { + no_assert = 0; +#if defined(DUK_USE_INTEGER_LE) + endswap = !duk_to_boolean(ctx, 2); /* 1=little endian */ +#else + endswap = duk_to_boolean(ctx, 2); /* 1=little endian */ +#endif + duk_swap(ctx, 0, 1); /* offset/value order different from Node.js */ + } else { + no_assert = duk_to_boolean(ctx, (magic_ftype == DUK__FLD_VARINT) ? 3 : 2); +#if defined(DUK_USE_INTEGER_LE) + endswap = magic_bigendian; +#else + endswap = !magic_bigendian; +#endif + } + + /* Offset is coerced first to signed integer range and then to unsigned. + * This ensures we can add a small byte length (1-8) to the offset in + * bound checks and not wrap. + */ + offset_signed = duk_to_int(ctx, 1); + offset = (duk_uint_t) offset_signed; + + /* We need 'nbytes' even for a failed offset; return value must be + * (offset + nbytes) even when write fails due to invalid offset. + */ + if (magic_ftype != DUK__FLD_VARINT) { + DUK_ASSERT(magic_ftype >= 0 && magic_ftype < (duk_small_int_t) (sizeof(duk__buffer_nbytes_from_fldtype) / sizeof(duk_uint8_t))); + nbytes = duk__buffer_nbytes_from_fldtype[magic_ftype]; + } else { + nbytes = duk_get_int(ctx, 2); + if (nbytes < 1 || nbytes > 6) { + goto fail_field_length; + } + } + DUK_ASSERT(nbytes >= 1 && nbytes <= 8); + + /* Now we can check offset validity. */ + if (offset_signed < 0) { + goto fail_bounds; + } + + DUK_DDD(DUK_DDDPRINT("writefield, value=%!T, buffer_length=%ld, offset=%ld, no_assert=%d, " + "magic=%04x, magic_fieldtype=%d, magic_bigendian=%d, magic_signed=%d, " + "endswap=%d", + duk_get_tval(ctx, 0), (long) buffer_length, (long) offset, (int) no_assert, + (unsigned int) magic, (int) magic_ftype, (int) (magic_bigendian >> 3), + (int) (magic_signed >> 4), (int) endswap)); + + /* Coerce value to a number before computing check_length, so that + * the field type specific coercion below can't have side effects + * that would invalidate check_length. + */ + duk_to_number(ctx, 0); + + /* Update 'buffer_length' to be the effective, safe limit which + * takes into account the underlying buffer. This value will be + * potentially invalidated by any side effect. + */ + check_length = DUK_HBUFFEROBJECT_CLAMP_BYTELENGTH(h_this, buffer_length); + DUK_DDD(DUK_DDDPRINT("buffer_length=%ld, check_length=%ld", + (long) buffer_length, (long) check_length)); + + if (h_this->buf) { + buf = DUK_HBUFFEROBJECT_GET_SLICE_BASE(thr->heap, h_this); + } else { + /* Neutered. We could go into the switch-case safely with + * buf == NULL because check_length == 0. To avoid scanbuild + * warnings, fail directly instead. + */ + DUK_ASSERT(check_length == 0); + goto fail_neutered; + } + DUK_ASSERT(buf != NULL); + + switch (magic_ftype) { + case DUK__FLD_8BIT: { + if (offset + 1U > check_length) { + goto fail_bounds; + } + /* sign doesn't matter when writing */ + buf[offset] = (duk_uint8_t) duk_to_uint32(ctx, 0); + break; + } + case DUK__FLD_16BIT: { + duk_uint16_t tmp; + if (offset + 2U > check_length) { + goto fail_bounds; + } + tmp = (duk_uint16_t) duk_to_uint32(ctx, 0); + if (endswap) { + tmp = DUK_BSWAP16(tmp); + } + du.us[0] = tmp; + /* sign doesn't matter when writing */ + DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 2); + break; + } + case DUK__FLD_32BIT: { + duk_uint32_t tmp; + if (offset + 4U > check_length) { + goto fail_bounds; + } + tmp = (duk_uint32_t) duk_to_uint32(ctx, 0); + if (endswap) { + tmp = DUK_BSWAP32(tmp); + } + du.ui[0] = tmp; + /* sign doesn't matter when writing */ + DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 4); + break; + } + case DUK__FLD_FLOAT: { + duk_uint32_t tmp; + if (offset + 4U > check_length) { + goto fail_bounds; + } + du.f[0] = (duk_float_t) duk_to_number(ctx, 0); + if (endswap) { + tmp = du.ui[0]; + tmp = DUK_BSWAP32(tmp); + du.ui[0] = tmp; + } + /* sign doesn't matter when writing */ + DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 4); + break; + } + case DUK__FLD_DOUBLE: { + if (offset + 8U > check_length) { + goto fail_bounds; + } + du.d = (duk_double_t) duk_to_number(ctx, 0); + if (endswap) { + DUK_DBLUNION_BSWAP64(&du); + } + /* sign doesn't matter when writing */ + DUK_MEMCPY((void *) (buf + offset), (const void *) du.uc, 8); + break; + } + case DUK__FLD_VARINT: { + /* Node.js Buffer variable width integer field. We don't really + * care about speed here, so aim for shortest algorithm. + */ + duk_int_t field_bytelen; + duk_int_t i, i_step, i_end; +#if defined(DUK_USE_64BIT_OPS) + duk_int64_t tmp; +#else + duk_double_t tmp; +#endif + duk_uint8_t *p; + + field_bytelen = (duk_int_t) nbytes; + if (offset + (duk_uint_t) field_bytelen > check_length) { + goto fail_bounds; + } + + /* Slow writing of value using either 64-bit arithmetic + * or IEEE doubles if 64-bit types not available. There's + * no special sign handling when writing varints. + */ + + if (magic_bigendian) { + /* Write in big endian */ + i = field_bytelen; /* one i_step added at top of loop */ + i_step = -1; + i_end = 0; + } else { + /* Write in little endian */ + i = -1; /* one i_step added at top of loop */ + i_step = 1; + i_end = field_bytelen - 1; + } + + /* XXX: The duk_to_number() cast followed by integer coercion + * is platform specific so NaN, +/- Infinity, and out-of-bounds + * values result in platform specific output now. + * See: test-bi-nodejs-buffer-proto-varint-special.js + */ + +#if defined(DUK_USE_64BIT_OPS) + tmp = (duk_int64_t) duk_to_number(ctx, 0); + p = (duk_uint8_t *) (buf + offset); + do { + i += i_step; + DUK_ASSERT(i >= 0 && i < field_bytelen); + p[i] = (duk_uint8_t) (tmp & 0xff); + tmp = tmp >> 8; /* unnecessary shift for last byte */ + } while (i != i_end); +#else + tmp = duk_to_number(ctx, 0); + p = (duk_uint8_t *) (buf + offset); + do { + i += i_step; + tmp = DUK_FLOOR(tmp); + DUK_ASSERT(i >= 0 && i < field_bytelen); + p[i] = (duk_uint8_t) (DUK_FMOD(tmp, 256.0)); + tmp = tmp / 256.0; /* unnecessary div for last byte */ + } while (i != i_end); +#endif + break; + } + default: { /* should never happen but default here */ + goto fail_bounds; + } + } + + /* Node.js Buffer: return offset + #bytes written (i.e. next + * write offset). + */ + if (magic_typedarray) { + /* For TypedArrays 'undefined' return value is specified + * by ES6 (matches V8). + */ + return 0; + } + duk_push_uint(ctx, offset + nbytes); + return 1; + + fail_neutered: + fail_field_length: + fail_bounds: + if (no_assert) { + /* Node.js return value for failed writes is offset + #bytes + * that would have been written. + */ + /* XXX: for negative input offsets, 'offset' will be a large + * positive value so the result here is confusing. + */ + if (magic_typedarray) { + return 0; + } + duk_push_uint(ctx, offset + nbytes); + return 1; + } + return DUK_RET_RANGE_ERROR; +} +#else /* DUK_USE_BUFFEROBJECT_SUPPORT */ +DUK_INTERNAL duk_ret_t duk_bi_buffer_writefield(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */ + +#undef DUK__FLD_8BIT +#undef DUK__FLD_16BIT +#undef DUK__FLD_32BIT +#undef DUK__FLD_FLOAT +#undef DUK__FLD_DOUBLE +#undef DUK__FLD_VARINT +#undef DUK__FLD_BIGENDIAN +#undef DUK__FLD_SIGNED +#undef DUK__FLD_TYPEDARRAY +/* + * Date built-ins + * + * Unlike most built-ins, Date has some platform dependencies for getting + * UTC time, converting between UTC and local time, and parsing and + * formatting time values. These are all abstracted behind DUK_USE_xxx + * config options. There are built-in platform specific providers for + * POSIX and Windows, but external providers can also be used. + * + * See doc/datetime.rst. + * + */ + +/* include removed: duk_internal.h */ + +/* + * Forward declarations + */ + +DUK_LOCAL_DECL duk_double_t duk__push_this_get_timeval_tzoffset(duk_context *ctx, duk_small_uint_t flags, duk_int_t *out_tzoffset); +DUK_LOCAL_DECL duk_double_t duk__push_this_get_timeval(duk_context *ctx, duk_small_uint_t flags); +DUK_LOCAL_DECL void duk__twodigit_year_fixup(duk_context *ctx, duk_idx_t idx_val); +DUK_LOCAL_DECL duk_ret_t duk__set_this_timeval_from_dparts(duk_context *ctx, duk_double_t *dparts, duk_small_uint_t flags); + +/* + * Other file level defines + */ + +/* Debug macro to print all parts and dparts (used manually because of debug level). */ +#define DUK__DPRINT_PARTS_AND_DPARTS(parts,dparts) do { \ + DUK_D(DUK_DPRINT("parts: %ld %ld %ld %ld %ld %ld %ld %ld, dparts: %lf %lf %lf %lf %lf %lf %lf %lf", \ + (long) (parts)[0], (long) (parts)[1], \ + (long) (parts)[2], (long) (parts)[3], \ + (long) (parts)[4], (long) (parts)[5], \ + (long) (parts)[6], (long) (parts)[7], \ + (double) (dparts)[0], (double) (dparts)[1], \ + (double) (dparts)[2], (double) (dparts)[3], \ + (double) (dparts)[4], (double) (dparts)[5], \ + (double) (dparts)[6], (double) (dparts)[7])); \ + } while (0) +#define DUK__DPRINT_PARTS(parts) do { \ + DUK_D(DUK_DPRINT("parts: %ld %ld %ld %ld %ld %ld %ld %ld", \ + (long) (parts)[0], (long) (parts)[1], \ + (long) (parts)[2], (long) (parts)[3], \ + (long) (parts)[4], (long) (parts)[5], \ + (long) (parts)[6], (long) (parts)[7])); \ + } while (0) +#define DUK__DPRINT_DPARTS(dparts) do { \ + DUK_D(DUK_DPRINT("dparts: %lf %lf %lf %lf %lf %lf %lf %lf", \ + (double) (dparts)[0], (double) (dparts)[1], \ + (double) (dparts)[2], (double) (dparts)[3], \ + (double) (dparts)[4], (double) (dparts)[5], \ + (double) (dparts)[6], (double) (dparts)[7])); \ + } while (0) + +/* Equivalent year for DST calculations outside [1970,2038[ range, see + * E5 Section 15.9.1.8. Equivalent year has the same leap-year-ness and + * starts with the same weekday on Jan 1. + * https://bugzilla.mozilla.org/show_bug.cgi?id=351066 + */ +#define DUK__YEAR(x) ((duk_uint8_t) ((x) - 1970)) +DUK_LOCAL duk_uint8_t duk__date_equivyear[14] = { +#if 1 + /* This is based on V8 EquivalentYear() algorithm (see src/genequivyear.py): + * http://code.google.com/p/v8/source/browse/trunk/src/date.h#146 + */ + + /* non-leap year: sunday, monday, ... */ + DUK__YEAR(2023), DUK__YEAR(2035), DUK__YEAR(2019), DUK__YEAR(2031), + DUK__YEAR(2015), DUK__YEAR(2027), DUK__YEAR(2011), + + /* leap year: sunday, monday, ... */ + DUK__YEAR(2012), DUK__YEAR(2024), DUK__YEAR(2008), DUK__YEAR(2020), + DUK__YEAR(2032), DUK__YEAR(2016), DUK__YEAR(2028) +#endif + +#if 0 + /* This is based on Rhino EquivalentYear() algorithm: + * https://github.com/mozilla/rhino/blob/f99cc11d616f0cdda2c42bde72b3484df6182947/src/org/mozilla/javascript/NativeDate.java + */ + + /* non-leap year: sunday, monday, ... */ + DUK__YEAR(1978), DUK__YEAR(1973), DUK__YEAR(1985), DUK__YEAR(1986), + DUK__YEAR(1981), DUK__YEAR(1971), DUK__YEAR(1977), + + /* leap year: sunday, monday, ... */ + DUK__YEAR(1984), DUK__YEAR(1996), DUK__YEAR(1980), DUK__YEAR(1992), + DUK__YEAR(1976), DUK__YEAR(1988), DUK__YEAR(1972) +#endif +}; +#undef DUK__YEAR + +/* + * ISO 8601 subset parser. + */ + +/* Parser part count. */ +#define DUK__NUM_ISO8601_PARSER_PARTS 9 + +/* Parser part indices. */ +#define DUK__PI_YEAR 0 +#define DUK__PI_MONTH 1 +#define DUK__PI_DAY 2 +#define DUK__PI_HOUR 3 +#define DUK__PI_MINUTE 4 +#define DUK__PI_SECOND 5 +#define DUK__PI_MILLISECOND 6 +#define DUK__PI_TZHOUR 7 +#define DUK__PI_TZMINUTE 8 + +/* Parser part masks. */ +#define DUK__PM_YEAR (1 << DUK__PI_YEAR) +#define DUK__PM_MONTH (1 << DUK__PI_MONTH) +#define DUK__PM_DAY (1 << DUK__PI_DAY) +#define DUK__PM_HOUR (1 << DUK__PI_HOUR) +#define DUK__PM_MINUTE (1 << DUK__PI_MINUTE) +#define DUK__PM_SECOND (1 << DUK__PI_SECOND) +#define DUK__PM_MILLISECOND (1 << DUK__PI_MILLISECOND) +#define DUK__PM_TZHOUR (1 << DUK__PI_TZHOUR) +#define DUK__PM_TZMINUTE (1 << DUK__PI_TZMINUTE) + +/* Parser separator indices. */ +#define DUK__SI_PLUS 0 +#define DUK__SI_MINUS 1 +#define DUK__SI_T 2 +#define DUK__SI_SPACE 3 +#define DUK__SI_COLON 4 +#define DUK__SI_PERIOD 5 +#define DUK__SI_Z 6 +#define DUK__SI_NUL 7 + +/* Parser separator masks. */ +#define DUK__SM_PLUS (1 << DUK__SI_PLUS) +#define DUK__SM_MINUS (1 << DUK__SI_MINUS) +#define DUK__SM_T (1 << DUK__SI_T) +#define DUK__SM_SPACE (1 << DUK__SI_SPACE) +#define DUK__SM_COLON (1 << DUK__SI_COLON) +#define DUK__SM_PERIOD (1 << DUK__SI_PERIOD) +#define DUK__SM_Z (1 << DUK__SI_Z) +#define DUK__SM_NUL (1 << DUK__SI_NUL) + +/* Rule control flags. */ +#define DUK__CF_NEG (1 << 0) /* continue matching, set neg_tzoffset flag */ +#define DUK__CF_ACCEPT (1 << 1) /* accept string */ +#define DUK__CF_ACCEPT_NUL (1 << 2) /* accept string if next char is NUL (otherwise reject) */ + +#define DUK__PACK_RULE(partmask,sepmask,nextpart,flags) \ + ((duk_uint32_t) (partmask) + \ + (((duk_uint32_t) (sepmask)) << 9) + \ + (((duk_uint32_t) (nextpart)) << 17) + \ + (((duk_uint32_t) (flags)) << 21)) + +#define DUK__UNPACK_RULE(rule,var_nextidx,var_flags) do { \ + (var_nextidx) = (duk_small_uint_t) (((rule) >> 17) & 0x0f); \ + (var_flags) = (duk_small_uint_t) ((rule) >> 21); \ + } while (0) + +#define DUK__RULE_MASK_PART_SEP 0x1ffffUL + +/* Matching separator index is used in the control table */ +DUK_LOCAL const duk_uint8_t duk__parse_iso8601_seps[] = { + DUK_ASC_PLUS /*0*/, DUK_ASC_MINUS /*1*/, DUK_ASC_UC_T /*2*/, DUK_ASC_SPACE /*3*/, + DUK_ASC_COLON /*4*/, DUK_ASC_PERIOD /*5*/, DUK_ASC_UC_Z /*6*/, DUK_ASC_NUL /*7*/ +}; + +/* Rule table: first matching rule is used to determine what to do next. */ +DUK_LOCAL const duk_uint32_t duk__parse_iso8601_control[] = { + DUK__PACK_RULE(DUK__PM_YEAR, DUK__SM_MINUS, DUK__PI_MONTH, 0), + DUK__PACK_RULE(DUK__PM_MONTH, DUK__SM_MINUS, DUK__PI_DAY, 0), + DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY, DUK__SM_T | DUK__SM_SPACE, DUK__PI_HOUR, 0), + DUK__PACK_RULE(DUK__PM_HOUR, DUK__SM_COLON, DUK__PI_MINUTE, 0), + DUK__PACK_RULE(DUK__PM_MINUTE, DUK__SM_COLON, DUK__PI_SECOND, 0), + DUK__PACK_RULE(DUK__PM_SECOND, DUK__SM_PERIOD, DUK__PI_MILLISECOND, 0), + DUK__PACK_RULE(DUK__PM_TZHOUR, DUK__SM_COLON, DUK__PI_TZMINUTE, 0), + DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND, DUK__SM_PLUS, DUK__PI_TZHOUR, 0), + DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND, DUK__SM_MINUS, DUK__PI_TZHOUR, DUK__CF_NEG), + DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND, DUK__SM_Z, 0, DUK__CF_ACCEPT_NUL), + DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND | DUK__PM_TZHOUR /*Note2*/ | DUK__PM_TZMINUTE, DUK__SM_NUL, 0, DUK__CF_ACCEPT) + + /* Note1: the specification doesn't require matching a time form with + * just hours ("HH"), but we accept it here, e.g. "2012-01-02T12Z". + * + * Note2: the specification doesn't require matching a timezone offset + * with just hours ("HH"), but accept it here, e.g. "2012-01-02T03:04:05+02" + */ +}; + +DUK_LOCAL duk_bool_t duk__parse_string_iso8601_subset(duk_context *ctx, const char *str) { + duk_int_t parts[DUK__NUM_ISO8601_PARSER_PARTS]; + duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS]; + duk_double_t d; + const duk_uint8_t *p; + duk_small_uint_t part_idx = 0; + duk_int_t accum = 0; + duk_small_uint_t ndigits = 0; + duk_bool_t neg_year = 0; + duk_bool_t neg_tzoffset = 0; + duk_uint_fast8_t ch; + duk_small_uint_t i; + + /* During parsing, month and day are one-based; set defaults here. */ + DUK_MEMZERO(parts, sizeof(parts)); + DUK_ASSERT(parts[DUK_DATE_IDX_YEAR] == 0); /* don't care value, year is mandatory */ + parts[DUK_DATE_IDX_MONTH] = 1; + parts[DUK_DATE_IDX_DAY] = 1; + + /* Special handling for year sign. */ + p = (const duk_uint8_t *) str; + ch = p[0]; + if (ch == DUK_ASC_PLUS) { + p++; + } else if (ch == DUK_ASC_MINUS) { + neg_year = 1; + p++; + } + + for (;;) { + ch = *p++; + DUK_DDD(DUK_DDDPRINT("parsing, part_idx=%ld, char=%ld ('%c')", + (long) part_idx, (long) ch, + (int) ((ch >= 0x20 && ch <= 0x7e) ? ch : DUK_ASC_QUESTION))); + + if (ch >= DUK_ASC_0 && ch <= DUK_ASC_9) { + if (ndigits >= 9) { + DUK_DDD(DUK_DDDPRINT("too many digits -> reject")); + goto reject; + } + if (part_idx == DUK__PI_MILLISECOND /*msec*/ && ndigits >= 3) { + /* ignore millisecond fractions after 3 */ + } else { + accum = accum * 10 + ((duk_int_t) ch) - ((duk_int_t) DUK_ASC_0) + 0x00; + ndigits++; + } + } else { + duk_uint_fast32_t match_val; + duk_small_int_t sep_idx; + + if (ndigits <= 0) { + goto reject; + } + if (part_idx == DUK__PI_MILLISECOND) { + /* complete the millisecond field */ + while (ndigits < 3) { + accum *= 10; + ndigits++; + } + } + parts[part_idx] = accum; + DUK_DDD(DUK_DDDPRINT("wrote part %ld -> value %ld", (long) part_idx, (long) accum)); + + accum = 0; + ndigits = 0; + + for (i = 0; i < (duk_small_uint_t) (sizeof(duk__parse_iso8601_seps) / sizeof(duk_uint8_t)); i++) { + if (duk__parse_iso8601_seps[i] == ch) { + break; + } + } + if (i == (duk_small_uint_t) (sizeof(duk__parse_iso8601_seps) / sizeof(duk_uint8_t))) { + DUK_DDD(DUK_DDDPRINT("separator character doesn't match -> reject")); + goto reject; + } + + sep_idx = i; + match_val = (1UL << part_idx) + (1UL << (sep_idx + 9)); /* match against rule part/sep bits */ + + for (i = 0; i < (duk_small_uint_t) (sizeof(duk__parse_iso8601_control) / sizeof(duk_uint32_t)); i++) { + duk_uint_fast32_t rule = duk__parse_iso8601_control[i]; + duk_small_uint_t nextpart; + duk_small_uint_t cflags; + + DUK_DDD(DUK_DDDPRINT("part_idx=%ld, sep_idx=%ld, match_val=0x%08lx, considering rule=0x%08lx", + (long) part_idx, (long) sep_idx, + (unsigned long) match_val, (unsigned long) rule)); + + if ((rule & match_val) != match_val) { + continue; + } + + DUK__UNPACK_RULE(rule, nextpart, cflags); + + DUK_DDD(DUK_DDDPRINT("rule match -> part_idx=%ld, sep_idx=%ld, match_val=0x%08lx, " + "rule=0x%08lx -> nextpart=%ld, cflags=0x%02lx", + (long) part_idx, (long) sep_idx, + (unsigned long) match_val, (unsigned long) rule, + (long) nextpart, (unsigned long) cflags)); + + if (cflags & DUK__CF_NEG) { + neg_tzoffset = 1; + } + + if (cflags & DUK__CF_ACCEPT) { + goto accept; + } + + if (cflags & DUK__CF_ACCEPT_NUL) { + DUK_ASSERT(*(p - 1) != (char) 0); + if (*p == DUK_ASC_NUL) { + goto accept; + } + goto reject; + } + + part_idx = nextpart; + break; + } /* rule match */ + + if (i == (duk_small_uint_t) (sizeof(duk__parse_iso8601_control) / sizeof(duk_uint32_t))) { + DUK_DDD(DUK_DDDPRINT("no rule matches -> reject")); + goto reject; + } + + if (ch == 0) { + /* This shouldn't be necessary, but check just in case + * to avoid any chance of overruns. + */ + DUK_DDD(DUK_DDDPRINT("NUL after rule matching (should not happen) -> reject")); + goto reject; + } + } /* if-digit-else-ctrl */ + } /* char loop */ + + /* We should never exit the loop above. */ + DUK_UNREACHABLE(); + + reject: + DUK_DDD(DUK_DDDPRINT("reject")); + return 0; + + accept: + DUK_DDD(DUK_DDDPRINT("accept")); + + /* Apply timezone offset to get the main parts in UTC */ + if (neg_year) { + parts[DUK__PI_YEAR] = -parts[DUK__PI_YEAR]; + } + if (neg_tzoffset) { + parts[DUK__PI_HOUR] += parts[DUK__PI_TZHOUR]; + parts[DUK__PI_MINUTE] += parts[DUK__PI_TZMINUTE]; + } else { + parts[DUK__PI_HOUR] -= parts[DUK__PI_TZHOUR]; + parts[DUK__PI_MINUTE] -= parts[DUK__PI_TZMINUTE]; + } + parts[DUK__PI_MONTH] -= 1; /* zero-based month */ + parts[DUK__PI_DAY] -= 1; /* zero-based day */ + + /* Use double parts, they tolerate unnormalized time. + * + * Note: DUK_DATE_IDX_WEEKDAY is initialized with a bogus value (DUK__PI_TZHOUR) + * on purpose. It won't be actually used by duk_bi_date_get_timeval_from_dparts(), + * but will make the value initialized just in case, and avoid any + * potential for Valgrind issues. + */ + for (i = 0; i < DUK_DATE_IDX_NUM_PARTS; i++) { + DUK_DDD(DUK_DDDPRINT("part[%ld] = %ld", (long) i, (long) parts[i])); + dparts[i] = parts[i]; + } + + d = duk_bi_date_get_timeval_from_dparts(dparts, 0 /*flags*/); + duk_push_number(ctx, d); + return 1; +} + +/* + * Date/time parsing helper. + * + * Parse a datetime string into a time value. We must first try to parse + * the input according to the standard format in E5.1 Section 15.9.1.15. + * If that fails, we can try to parse using custom parsing, which can + * either be platform neutral (custom code) or platform specific (using + * existing platform API calls). + * + * Note in particular that we must parse whatever toString(), toUTCString(), + * and toISOString() can produce; see E5.1 Section 15.9.4.2. + * + * Returns 1 to allow tail calling. + * + * There is much room for improvement here with respect to supporting + * alternative datetime formats. For instance, V8 parses '2012-01-01' as + * UTC and '2012/01/01' as local time. + */ + +DUK_LOCAL duk_ret_t duk__parse_string(duk_context *ctx, const char *str) { + /* XXX: there is a small risk here: because the ISO 8601 parser is + * very loose, it may end up parsing some datetime values which + * would be better parsed with a platform specific parser. + */ + + DUK_ASSERT(str != NULL); + DUK_DDD(DUK_DDDPRINT("parse datetime from string '%s'", (const char *) str)); + + if (duk__parse_string_iso8601_subset(ctx, str) != 0) { + return 1; + } + +#if defined(DUK_USE_DATE_PARSE_STRING) + /* Contract, either: + * - Push value on stack and return 1 + * - Don't push anything on stack and return 0 + */ + + if (DUK_USE_DATE_PARSE_STRING(ctx, str) != 0) { + return 1; + } +#else + /* No platform-specific parsing, this is not an error. */ +#endif + + duk_push_nan(ctx); + return 1; +} + +/* + * Calendar helpers + * + * Some helpers are used for getters and can operate on normalized values + * which can be represented with 32-bit signed integers. Other helpers are + * needed by setters and operate on un-normalized double values, must watch + * out for non-finite numbers etc. + */ + +DUK_LOCAL duk_uint8_t duk__days_in_month[12] = { + (duk_uint8_t) 31, (duk_uint8_t) 28, (duk_uint8_t) 31, (duk_uint8_t) 30, + (duk_uint8_t) 31, (duk_uint8_t) 30, (duk_uint8_t) 31, (duk_uint8_t) 31, + (duk_uint8_t) 30, (duk_uint8_t) 31, (duk_uint8_t) 30, (duk_uint8_t) 31 +}; + +/* Maximum iteration count for computing UTC-to-local time offset when + * creating an Ecmascript time value from local parts. + */ +#define DUK__LOCAL_TZOFFSET_MAXITER 4 + +/* Because 'day since epoch' can be negative and is used to compute weekday + * using a modulo operation, add this multiple of 7 to avoid negative values + * when year is below 1970 epoch. Ecmascript time values are restricted to + * +/- 100 million days from epoch, so this adder fits nicely into 32 bits. + * Round to a multiple of 7 (= floor(100000000 / 7) * 7) and add margin. + */ +#define DUK__WEEKDAY_MOD_ADDER (20000000 * 7) /* 0x08583b00 */ + +DUK_INTERNAL duk_bool_t duk_bi_date_is_leap_year(duk_int_t year) { + if ((year % 4) != 0) { + return 0; + } + if ((year % 100) != 0) { + return 1; + } + if ((year % 400) != 0) { + return 0; + } + return 1; +} + +DUK_INTERNAL duk_bool_t duk_bi_date_timeval_in_valid_range(duk_double_t x) { + return (x >= -DUK_DATE_MSEC_100M_DAYS && x <= DUK_DATE_MSEC_100M_DAYS); +} + +DUK_INTERNAL duk_bool_t duk_bi_date_timeval_in_leeway_range(duk_double_t x) { + return (x >= -DUK_DATE_MSEC_100M_DAYS_LEEWAY && x <= DUK_DATE_MSEC_100M_DAYS_LEEWAY); +} + +DUK_INTERNAL duk_bool_t duk_bi_date_year_in_valid_range(duk_double_t x) { + return (x >= DUK_DATE_MIN_ECMA_YEAR && x <= DUK_DATE_MAX_ECMA_YEAR); +} + +DUK_LOCAL duk_double_t duk__timeclip(duk_double_t x) { + if (!DUK_ISFINITE(x)) { + return DUK_DOUBLE_NAN; + } + + if (!duk_bi_date_timeval_in_valid_range(x)) { + return DUK_DOUBLE_NAN; + } + + x = duk_js_tointeger_number(x); + + /* Here we'd have the option to normalize -0 to +0. */ + return x; +} + +/* Integer division which floors also negative values correctly. */ +DUK_LOCAL duk_int_t duk__div_floor(duk_int_t a, duk_int_t b) { + DUK_ASSERT(b > 0); + if (a >= 0) { + return a / b; + } else { + /* e.g. a = -4, b = 5 --> -4 - 5 + 1 / 5 --> -8 / 5 --> -1 + * a = -5, b = 5 --> -5 - 5 + 1 / 5 --> -9 / 5 --> -1 + * a = -6, b = 5 --> -6 - 5 + 1 / 5 --> -10 / 5 --> -2 + */ + return (a - b + 1) / b; + } +} + +/* Compute day number of the first day of a given year. */ +DUK_LOCAL duk_int_t duk__day_from_year(duk_int_t year) { + /* Note: in integer arithmetic, (x / 4) is same as floor(x / 4) for non-negative + * values, but is incorrect for negative ones. + */ + return 365 * (year - 1970) + + duk__div_floor(year - 1969, 4) + - duk__div_floor(year - 1901, 100) + + duk__div_floor(year - 1601, 400); +} + +/* Given a day number, determine year and day-within-year. */ +DUK_LOCAL duk_int_t duk__year_from_day(duk_int_t day, duk_small_int_t *out_day_within_year) { + duk_int_t year; + duk_int_t diff_days; + + /* estimate year upwards (towards positive infinity), then back down; + * two iterations should be enough + */ + + if (day >= 0) { + year = 1970 + day / 365; + } else { + year = 1970 + day / 366; + } + + for (;;) { + diff_days = duk__day_from_year(year) - day; + DUK_DDD(DUK_DDDPRINT("year=%ld day=%ld, diff_days=%ld", (long) year, (long) day, (long) diff_days)); + if (diff_days <= 0) { + DUK_ASSERT(-diff_days < 366); /* fits into duk_small_int_t */ + *out_day_within_year = -diff_days; + DUK_DDD(DUK_DDDPRINT("--> year=%ld, day-within-year=%ld", + (long) year, (long) *out_day_within_year)); + DUK_ASSERT(*out_day_within_year >= 0); + DUK_ASSERT(*out_day_within_year < (duk_bi_date_is_leap_year(year) ? 366 : 365)); + return year; + } + + /* Note: this is very tricky; we must never 'overshoot' the + * correction downwards. + */ + year -= 1 + (diff_days - 1) / 366; /* conservative */ + } +} + +/* Given a (year, month, day-within-month) triple, compute day number. + * The input triple is un-normalized and may contain non-finite values. + */ +DUK_LOCAL duk_double_t duk__make_day(duk_double_t year, duk_double_t month, duk_double_t day) { + duk_int_t day_num; + duk_bool_t is_leap; + duk_small_int_t i, n; + + /* Assume that year, month, day are all coerced to whole numbers. + * They may also be NaN or infinity, in which case this function + * must return NaN or infinity to ensure time value becomes NaN. + * If 'day' is NaN, the final return will end up returning a NaN, + * so it doesn't need to be checked here. + */ + + if (!DUK_ISFINITE(year) || !DUK_ISFINITE(month)) { + return DUK_DOUBLE_NAN; + } + + year += DUK_FLOOR(month / 12.0); + + month = DUK_FMOD(month, 12.0); + if (month < 0.0) { + /* handle negative values */ + month += 12.0; + } + + /* The algorithm in E5.1 Section 15.9.1.12 normalizes month, but + * does not normalize the day-of-month (nor check whether or not + * it is finite) because it's not necessary for finding the day + * number which matches the (year,month) pair. + * + * We assume that duk__day_from_year() is exact here. + * + * Without an explicit infinity / NaN check in the beginning, + * day_num would be a bogus integer here. + * + * It's possible for 'year' to be out of integer range here. + * If so, we need to return NaN without integer overflow. + * This fixes test-bug-setyear-overflow.js. + */ + + if (!duk_bi_date_year_in_valid_range(year)) { + DUK_DD(DUK_DDPRINT("year not in ecmascript valid range, avoid integer overflow: %lf", (double) year)); + return DUK_DOUBLE_NAN; + } + day_num = duk__day_from_year((duk_int_t) year); + is_leap = duk_bi_date_is_leap_year((duk_int_t) year); + + n = (duk_small_int_t) month; + for (i = 0; i < n; i++) { + day_num += duk__days_in_month[i]; + if (i == 1 && is_leap) { + day_num++; + } + } + + /* If 'day' is NaN, returns NaN. */ + return (duk_double_t) day_num + day; +} + +/* Split time value into parts. The time value is assumed to be an internal + * one, i.e. finite, no fractions. Possible local time adjustment has already + * been applied when reading the time value. + */ +DUK_INTERNAL void duk_bi_date_timeval_to_parts(duk_double_t d, duk_int_t *parts, duk_double_t *dparts, duk_small_uint_t flags) { + duk_double_t d1, d2; + duk_int_t t1, t2; + duk_int_t day_since_epoch; + duk_int_t year; /* does not fit into 16 bits */ + duk_small_int_t day_in_year; + duk_small_int_t month; + duk_small_int_t day; + duk_small_int_t dim; + duk_int_t jan1_since_epoch; + duk_small_int_t jan1_weekday; + duk_int_t equiv_year; + duk_small_uint_t i; + duk_bool_t is_leap; + duk_small_int_t arridx; + + DUK_ASSERT(DUK_ISFINITE(d)); /* caller checks */ + DUK_ASSERT(DUK_FLOOR(d) == d); /* no fractions in internal time */ + + /* The timevalue must be in valid Ecmascript range, but since a local + * time offset can be applied, we need to allow a +/- 24h leeway to + * the value. In other words, although the UTC time is within the + * Ecmascript range, the local part values can be just outside of it. + */ + DUK_UNREF(duk_bi_date_timeval_in_leeway_range); + DUK_ASSERT(duk_bi_date_timeval_in_leeway_range(d)); + + /* these computations are guaranteed to be exact for the valid + * E5 time value range, assuming milliseconds without fractions. + */ + d1 = (duk_double_t) DUK_FMOD(d, (double) DUK_DATE_MSEC_DAY); + if (d1 < 0.0) { + /* deal with negative values */ + d1 += (duk_double_t) DUK_DATE_MSEC_DAY; + } + d2 = DUK_FLOOR((double) (d / (duk_double_t) DUK_DATE_MSEC_DAY)); + DUK_ASSERT(d2 * ((duk_double_t) DUK_DATE_MSEC_DAY) + d1 == d); + /* now expected to fit into a 32-bit integer */ + t1 = (duk_int_t) d1; + t2 = (duk_int_t) d2; + day_since_epoch = t2; + DUK_ASSERT((duk_double_t) t1 == d1); + DUK_ASSERT((duk_double_t) t2 == d2); + + /* t1 = milliseconds within day (fits 32 bit) + * t2 = day number from epoch (fits 32 bit, may be negative) + */ + + parts[DUK_DATE_IDX_MILLISECOND] = t1 % 1000; t1 /= 1000; + parts[DUK_DATE_IDX_SECOND] = t1 % 60; t1 /= 60; + parts[DUK_DATE_IDX_MINUTE] = t1 % 60; t1 /= 60; + parts[DUK_DATE_IDX_HOUR] = t1; + DUK_ASSERT(parts[DUK_DATE_IDX_MILLISECOND] >= 0 && parts[DUK_DATE_IDX_MILLISECOND] <= 999); + DUK_ASSERT(parts[DUK_DATE_IDX_SECOND] >= 0 && parts[DUK_DATE_IDX_SECOND] <= 59); + DUK_ASSERT(parts[DUK_DATE_IDX_MINUTE] >= 0 && parts[DUK_DATE_IDX_MINUTE] <= 59); + DUK_ASSERT(parts[DUK_DATE_IDX_HOUR] >= 0 && parts[DUK_DATE_IDX_HOUR] <= 23); + + DUK_DDD(DUK_DDDPRINT("d=%lf, d1=%lf, d2=%lf, t1=%ld, t2=%ld, parts: hour=%ld min=%ld sec=%ld msec=%ld", + (double) d, (double) d1, (double) d2, (long) t1, (long) t2, + (long) parts[DUK_DATE_IDX_HOUR], + (long) parts[DUK_DATE_IDX_MINUTE], + (long) parts[DUK_DATE_IDX_SECOND], + (long) parts[DUK_DATE_IDX_MILLISECOND])); + + /* This assert depends on the input parts representing time inside + * the Ecmascript range. + */ + DUK_ASSERT(t2 + DUK__WEEKDAY_MOD_ADDER >= 0); + parts[DUK_DATE_IDX_WEEKDAY] = (t2 + 4 + DUK__WEEKDAY_MOD_ADDER) % 7; /* E5.1 Section 15.9.1.6 */ + DUK_ASSERT(parts[DUK_DATE_IDX_WEEKDAY] >= 0 && parts[DUK_DATE_IDX_WEEKDAY] <= 6); + + year = duk__year_from_day(t2, &day_in_year); + day = day_in_year; + is_leap = duk_bi_date_is_leap_year(year); + for (month = 0; month < 12; month++) { + dim = duk__days_in_month[month]; + if (month == 1 && is_leap) { + dim++; + } + DUK_DDD(DUK_DDDPRINT("month=%ld, dim=%ld, day=%ld", + (long) month, (long) dim, (long) day)); + if (day < dim) { + break; + } + day -= dim; + } + DUK_DDD(DUK_DDDPRINT("final month=%ld", (long) month)); + DUK_ASSERT(month >= 0 && month <= 11); + DUK_ASSERT(day >= 0 && day <= 31); + + /* Equivalent year mapping, used to avoid DST trouble when platform + * may fail to provide reasonable DST answers for dates outside the + * ordinary range (e.g. 1970-2038). An equivalent year has the same + * leap-year-ness as the original year and begins on the same weekday + * (Jan 1). + * + * The year 2038 is avoided because there seem to be problems with it + * on some platforms. The year 1970 is also avoided as there were + * practical problems with it; an equivalent year is used for it too, + * which breaks some DST computations for 1970 right now, see e.g. + * test-bi-date-tzoffset-brute-fi.js. + */ + if ((flags & DUK_DATE_FLAG_EQUIVYEAR) && (year < 1971 || year > 2037)) { + DUK_ASSERT(is_leap == 0 || is_leap == 1); + + jan1_since_epoch = day_since_epoch - day_in_year; /* day number for Jan 1 since epoch */ + DUK_ASSERT(jan1_since_epoch + DUK__WEEKDAY_MOD_ADDER >= 0); + jan1_weekday = (jan1_since_epoch + 4 + DUK__WEEKDAY_MOD_ADDER) % 7; /* E5.1 Section 15.9.1.6 */ + DUK_ASSERT(jan1_weekday >= 0 && jan1_weekday <= 6); + arridx = jan1_weekday; + if (is_leap) { + arridx += 7; + } + DUK_ASSERT(arridx >= 0 && arridx < (duk_small_int_t) (sizeof(duk__date_equivyear) / sizeof(duk_uint8_t))); + + equiv_year = (duk_int_t) duk__date_equivyear[arridx] + 1970; + year = equiv_year; + DUK_DDD(DUK_DDDPRINT("equiv year mapping, year=%ld, day_in_year=%ld, day_since_epoch=%ld, " + "jan1_since_epoch=%ld, jan1_weekday=%ld -> equiv year %ld", + (long) year, (long) day_in_year, (long) day_since_epoch, + (long) jan1_since_epoch, (long) jan1_weekday, (long) equiv_year)); + } + + parts[DUK_DATE_IDX_YEAR] = year; + parts[DUK_DATE_IDX_MONTH] = month; + parts[DUK_DATE_IDX_DAY] = day; + + if (flags & DUK_DATE_FLAG_ONEBASED) { + parts[DUK_DATE_IDX_MONTH]++; /* zero-based -> one-based */ + parts[DUK_DATE_IDX_DAY]++; /* -""- */ + } + + if (dparts != NULL) { + for (i = 0; i < DUK_DATE_IDX_NUM_PARTS; i++) { + dparts[i] = (duk_double_t) parts[i]; + } + } +} + +/* Compute time value from (double) parts. The parts can be either UTC + * or local time; if local, they need to be (conceptually) converted into + * UTC time. The parts may represent valid or invalid time, and may be + * wildly out of range (but may cancel each other and still come out in + * the valid Date range). + */ +DUK_INTERNAL duk_double_t duk_bi_date_get_timeval_from_dparts(duk_double_t *dparts, duk_small_uint_t flags) { +#if defined(DUK_USE_PARANOID_DATE_COMPUTATION) + /* See comments below on MakeTime why these are volatile. */ + volatile duk_double_t tmp_time; + volatile duk_double_t tmp_day; + volatile duk_double_t d; +#else + duk_double_t tmp_time; + duk_double_t tmp_day; + duk_double_t d; +#endif + duk_small_uint_t i; + duk_int_t tzoff, tzoffprev1, tzoffprev2; + + /* Expects 'this' at top of stack on entry. */ + + /* Coerce all finite parts with ToInteger(). ToInteger() must not + * be called for NaN/Infinity because it will convert e.g. NaN to + * zero. If ToInteger() has already been called, this has no side + * effects and is idempotent. + * + * Don't read dparts[DUK_DATE_IDX_WEEKDAY]; it will cause Valgrind + * issues if the value is uninitialized. + */ + for (i = 0; i <= DUK_DATE_IDX_MILLISECOND; i++) { + /* SCANBUILD: scan-build complains here about assigned value + * being garbage or undefined. This is correct but operating + * on undefined values has no ill effect and is ignored by the + * caller in the case where this happens. + */ + d = dparts[i]; + if (DUK_ISFINITE(d)) { + dparts[i] = duk_js_tointeger_number(d); + } + } + + /* Use explicit steps in computation to try to ensure that + * computation happens with intermediate results coerced to + * double values (instead of using something more accurate). + * E.g. E5.1 Section 15.9.1.11 requires use of IEEE 754 + * rules (= Ecmascript '+' and '*' operators). + * + * Without 'volatile' even this approach fails on some platform + * and compiler combinations. For instance, gcc 4.8.1 on Ubuntu + * 64-bit, with -m32 and without -std=c99, test-bi-date-canceling.js + * would fail because of some optimizations when computing tmp_time + * (MakeTime below). Adding 'volatile' to tmp_time solved this + * particular problem (annoyingly, also adding debug prints or + * running the executable under valgrind hides it). + */ + + /* MakeTime */ + tmp_time = 0.0; + tmp_time += dparts[DUK_DATE_IDX_HOUR] * ((duk_double_t) DUK_DATE_MSEC_HOUR); + tmp_time += dparts[DUK_DATE_IDX_MINUTE] * ((duk_double_t) DUK_DATE_MSEC_MINUTE); + tmp_time += dparts[DUK_DATE_IDX_SECOND] * ((duk_double_t) DUK_DATE_MSEC_SECOND); + tmp_time += dparts[DUK_DATE_IDX_MILLISECOND]; + + /* MakeDay */ + tmp_day = duk__make_day(dparts[DUK_DATE_IDX_YEAR], dparts[DUK_DATE_IDX_MONTH], dparts[DUK_DATE_IDX_DAY]); + + /* MakeDate */ + d = tmp_day * ((duk_double_t) DUK_DATE_MSEC_DAY) + tmp_time; + + DUK_DDD(DUK_DDDPRINT("time=%lf day=%lf --> timeval=%lf", + (double) tmp_time, (double) tmp_day, (double) d)); + + /* Optional UTC conversion. */ + if (flags & DUK_DATE_FLAG_LOCALTIME) { + /* DUK_USE_DATE_GET_LOCAL_TZOFFSET() needs to be called with a + * time value computed from UTC parts. At this point we only + * have 'd' which is a time value computed from local parts, so + * it is off by the UTC-to-local time offset which we don't know + * yet. The current solution for computing the UTC-to-local + * time offset is to iterate a few times and detect a fixed + * point or a two-cycle loop (or a sanity iteration limit), + * see test-bi-date-local-parts.js and test-bi-date-tzoffset-basic-fi.js. + * + * E5.1 Section 15.9.1.9: + * UTC(t) = t - LocalTZA - DaylightSavingTA(t - LocalTZA) + * + * For NaN/inf, DUK_USE_DATE_GET_LOCAL_TZOFFSET() returns 0. + */ + +#if 0 + /* Old solution: don't iterate, incorrect */ + tzoff = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d); + DUK_DDD(DUK_DDDPRINT("tzoffset w/o iteration, tzoff=%ld", (long) tzoff)); + d -= tzoff * 1000L; + DUK_UNREF(tzoffprev1); + DUK_UNREF(tzoffprev2); +#endif + + /* Iteration solution */ + tzoff = 0; + tzoffprev1 = 999999999L; /* invalid value which never matches */ + for (i = 0; i < DUK__LOCAL_TZOFFSET_MAXITER; i++) { + tzoffprev2 = tzoffprev1; + tzoffprev1 = tzoff; + tzoff = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d - tzoff * 1000L); + DUK_DDD(DUK_DDDPRINT("tzoffset iteration, i=%d, tzoff=%ld, tzoffprev1=%ld tzoffprev2=%ld", + (int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2)); + if (tzoff == tzoffprev1) { + DUK_DDD(DUK_DDDPRINT("tzoffset iteration finished, i=%d, tzoff=%ld, tzoffprev1=%ld, tzoffprev2=%ld", + (int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2)); + break; + } else if (tzoff == tzoffprev2) { + /* Two value cycle, see e.g. test-bi-date-tzoffset-basic-fi.js. + * In these cases, favor a higher tzoffset to get a consistent + * result which is independent of iteration count. Not sure if + * this is a generically correct solution. + */ + DUK_DDD(DUK_DDDPRINT("tzoffset iteration two-value cycle, i=%d, tzoff=%ld, tzoffprev1=%ld, tzoffprev2=%ld", + (int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2)); + if (tzoffprev1 > tzoff) { + tzoff = tzoffprev1; + } + break; + } + } + DUK_DDD(DUK_DDDPRINT("tzoffset iteration, tzoff=%ld", (long) tzoff)); + d -= tzoff * 1000L; + } + + /* TimeClip(), which also handles Infinity -> NaN conversion */ + d = duk__timeclip(d); + + return d; +} + +/* + * API oriented helpers + */ + +/* Push 'this' binding, check that it is a Date object; then push the + * internal time value. At the end, stack is: [ ... this timeval ]. + * Returns the time value. Local time adjustment is done if requested. + */ +DUK_LOCAL duk_double_t duk__push_this_get_timeval_tzoffset(duk_context *ctx, duk_small_uint_t flags, duk_int_t *out_tzoffset) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *h; + duk_double_t d; + duk_int_t tzoffset = 0; + + duk_push_this(ctx); + h = duk_get_hobject(ctx, -1); /* XXX: getter with class check, useful in built-ins */ + if (h == NULL || DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_DATE) { + DUK_ERROR_TYPE(thr, "expected Date"); + } + + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE); + d = duk_to_number(ctx, -1); + duk_pop(ctx); + + if (DUK_ISNAN(d)) { + if (flags & DUK_DATE_FLAG_NAN_TO_ZERO) { + d = 0.0; + } + if (flags & DUK_DATE_FLAG_NAN_TO_RANGE_ERROR) { + DUK_ERROR_RANGE(thr, "Invalid Date"); + } + } + /* if no NaN handling flag, may still be NaN here, but not Inf */ + DUK_ASSERT(!DUK_ISINF(d)); + + if (flags & DUK_DATE_FLAG_LOCALTIME) { + /* Note: DST adjustment is determined using UTC time. + * If 'd' is NaN, tzoffset will be 0. + */ + tzoffset = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d); /* seconds */ + d += tzoffset * 1000L; + } + if (out_tzoffset) { + *out_tzoffset = tzoffset; + } + + /* [ ... this ] */ + return d; +} + +DUK_LOCAL duk_double_t duk__push_this_get_timeval(duk_context *ctx, duk_small_uint_t flags) { + return duk__push_this_get_timeval_tzoffset(ctx, flags, NULL); +} + +/* Set timeval to 'this' from dparts, push the new time value onto the + * value stack and return 1 (caller can then tail call us). Expects + * the value stack to contain 'this' on the stack top. + */ +DUK_LOCAL duk_ret_t duk__set_this_timeval_from_dparts(duk_context *ctx, duk_double_t *dparts, duk_small_uint_t flags) { + duk_double_t d; + + /* [ ... this ] */ + + d = duk_bi_date_get_timeval_from_dparts(dparts, flags); + duk_push_number(ctx, d); /* -> [ ... this timeval_new ] */ + duk_dup_top(ctx); /* -> [ ... this timeval_new timeval_new ] */ + duk_put_prop_stridx(ctx, -3, DUK_STRIDX_INT_VALUE); + + /* stack top: new time value, return 1 to allow tail calls */ + return 1; +} + +/* 'out_buf' must be at least DUK_BI_DATE_ISO8601_BUFSIZE long. */ +DUK_LOCAL void duk__format_parts_iso8601(duk_int_t *parts, duk_int_t tzoffset, duk_small_uint_t flags, duk_uint8_t *out_buf) { + char yearstr[8]; /* "-123456\0" */ + char tzstr[8]; /* "+11:22\0" */ + char sep = (flags & DUK_DATE_FLAG_SEP_T) ? DUK_ASC_UC_T : DUK_ASC_SPACE; + + DUK_ASSERT(parts[DUK_DATE_IDX_MONTH] >= 1 && parts[DUK_DATE_IDX_MONTH] <= 12); + DUK_ASSERT(parts[DUK_DATE_IDX_DAY] >= 1 && parts[DUK_DATE_IDX_DAY] <= 31); + DUK_ASSERT(parts[DUK_DATE_IDX_YEAR] >= -999999 && parts[DUK_DATE_IDX_YEAR] <= 999999); + + /* Note: %06d for positive value, %07d for negative value to include + * sign and 6 digits. + */ + DUK_SNPRINTF(yearstr, + sizeof(yearstr), + (parts[DUK_DATE_IDX_YEAR] >= 0 && parts[DUK_DATE_IDX_YEAR] <= 9999) ? "%04ld" : + ((parts[DUK_DATE_IDX_YEAR] >= 0) ? "+%06ld" : "%07ld"), + (long) parts[DUK_DATE_IDX_YEAR]); + yearstr[sizeof(yearstr) - 1] = (char) 0; + + if (flags & DUK_DATE_FLAG_LOCALTIME) { + /* tzoffset seconds are dropped; 16 bits suffice for + * time offset in minutes + */ + if (tzoffset >= 0) { + duk_small_int_t tmp = tzoffset / 60; + DUK_SNPRINTF(tzstr, sizeof(tzstr), "+%02d:%02d", (int) (tmp / 60), (int) (tmp % 60)); + } else { + duk_small_int_t tmp = -tzoffset / 60; + DUK_SNPRINTF(tzstr, sizeof(tzstr), "-%02d:%02d", (int) (tmp / 60), (int) (tmp % 60)); + } + tzstr[sizeof(tzstr) - 1] = (char) 0; + } else { + tzstr[0] = DUK_ASC_UC_Z; + tzstr[1] = (char) 0; + } + + /* Unlike year, the other parts fit into 16 bits so %d format + * is portable. + */ + if ((flags & DUK_DATE_FLAG_TOSTRING_DATE) && (flags & DUK_DATE_FLAG_TOSTRING_TIME)) { + DUK_SPRINTF((char *) out_buf, "%s-%02d-%02d%c%02d:%02d:%02d.%03d%s", + (const char *) yearstr, (int) parts[DUK_DATE_IDX_MONTH], (int) parts[DUK_DATE_IDX_DAY], (int) sep, + (int) parts[DUK_DATE_IDX_HOUR], (int) parts[DUK_DATE_IDX_MINUTE], + (int) parts[DUK_DATE_IDX_SECOND], (int) parts[DUK_DATE_IDX_MILLISECOND], (const char *) tzstr); + } else if (flags & DUK_DATE_FLAG_TOSTRING_DATE) { + DUK_SPRINTF((char *) out_buf, "%s-%02d-%02d", + (const char *) yearstr, (int) parts[DUK_DATE_IDX_MONTH], (int) parts[DUK_DATE_IDX_DAY]); + } else { + DUK_ASSERT(flags & DUK_DATE_FLAG_TOSTRING_TIME); + DUK_SPRINTF((char *) out_buf, "%02d:%02d:%02d.%03d%s", + (int) parts[DUK_DATE_IDX_HOUR], (int) parts[DUK_DATE_IDX_MINUTE], + (int) parts[DUK_DATE_IDX_SECOND], (int) parts[DUK_DATE_IDX_MILLISECOND], + (const char *) tzstr); + } +} + +/* Helper for string conversion calls: check 'this' binding, get the + * internal time value, and format date and/or time in a few formats. + * Return value allows tail calls. + */ +DUK_LOCAL duk_ret_t duk__to_string_helper(duk_context *ctx, duk_small_uint_t flags) { + duk_double_t d; + duk_int_t parts[DUK_DATE_IDX_NUM_PARTS]; + duk_int_t tzoffset; /* seconds, doesn't fit into 16 bits */ + duk_bool_t rc; + duk_uint8_t buf[DUK_BI_DATE_ISO8601_BUFSIZE]; + + DUK_UNREF(rc); /* unreferenced with some options */ + + d = duk__push_this_get_timeval_tzoffset(ctx, flags, &tzoffset); + if (DUK_ISNAN(d)) { + duk_push_hstring_stridx(ctx, DUK_STRIDX_INVALID_DATE); + return 1; + } + DUK_ASSERT(DUK_ISFINITE(d)); + + /* formatters always get one-based month/day-of-month */ + duk_bi_date_timeval_to_parts(d, parts, NULL, DUK_DATE_FLAG_ONEBASED); + DUK_ASSERT(parts[DUK_DATE_IDX_MONTH] >= 1 && parts[DUK_DATE_IDX_MONTH] <= 12); + DUK_ASSERT(parts[DUK_DATE_IDX_DAY] >= 1 && parts[DUK_DATE_IDX_DAY] <= 31); + + if (flags & DUK_DATE_FLAG_TOSTRING_LOCALE) { + /* try locale specific formatter; if it refuses to format the + * string, fall back to an ISO 8601 formatted value in local + * time. + */ +#if defined(DUK_USE_DATE_FORMAT_STRING) + /* Contract, either: + * - Push string to value stack and return 1 + * - Don't push anything and return 0 + */ + + rc = DUK_USE_DATE_FORMAT_STRING(ctx, parts, tzoffset, flags); + if (rc != 0) { + return 1; + } +#else + /* No locale specific formatter; this is OK, we fall back + * to ISO 8601. + */ +#endif + } + + /* Different calling convention than above used because the helper + * is shared. + */ + duk__format_parts_iso8601(parts, tzoffset, flags, buf); + duk_push_string(ctx, (const char *) buf); + return 1; +} + +/* Helper for component getter calls: check 'this' binding, get the + * internal time value, split it into parts (either as UTC time or + * local time), push a specified component as a return value to the + * value stack and return 1 (caller can then tail call us). + */ +DUK_LOCAL duk_ret_t duk__get_part_helper(duk_context *ctx, duk_small_uint_t flags_and_idx) { + duk_double_t d; + duk_int_t parts[DUK_DATE_IDX_NUM_PARTS]; + duk_small_uint_t idx_part = (duk_small_uint_t) (flags_and_idx >> DUK_DATE_FLAG_VALUE_SHIFT); /* unpack args */ + + DUK_ASSERT_DISABLE(idx_part >= 0); /* unsigned */ + DUK_ASSERT(idx_part < DUK_DATE_IDX_NUM_PARTS); + + d = duk__push_this_get_timeval(ctx, flags_and_idx); + if (DUK_ISNAN(d)) { + duk_push_nan(ctx); + return 1; + } + DUK_ASSERT(DUK_ISFINITE(d)); + + duk_bi_date_timeval_to_parts(d, parts, NULL, flags_and_idx); /* no need to mask idx portion */ + + /* Setter APIs detect special year numbers (0...99) and apply a +1900 + * only in certain cases. The legacy getYear() getter applies -1900 + * unconditionally. + */ + duk_push_int(ctx, (flags_and_idx & DUK_DATE_FLAG_SUB1900) ? parts[idx_part] - 1900 : parts[idx_part]); + return 1; +} + +/* Helper for component setter calls: check 'this' binding, get the + * internal time value, split it into parts (either as UTC time or + * local time), modify one or more components as specified, recompute + * the time value, set it as the internal value. Finally, push the + * new time value as a return value to the value stack and return 1 + * (caller can then tail call us). + */ +DUK_LOCAL duk_ret_t duk__set_part_helper(duk_context *ctx, duk_small_uint_t flags_and_maxnargs) { + duk_double_t d; + duk_int_t parts[DUK_DATE_IDX_NUM_PARTS]; + duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS]; + duk_idx_t nargs; + duk_small_uint_t maxnargs = (duk_small_uint_t) (flags_and_maxnargs >> DUK_DATE_FLAG_VALUE_SHIFT); /* unpack args */ + duk_small_uint_t idx_first, idx; + duk_small_uint_t i; + + nargs = duk_get_top(ctx); + d = duk__push_this_get_timeval(ctx, flags_and_maxnargs); + DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d)); + + if (DUK_ISFINITE(d)) { + duk_bi_date_timeval_to_parts(d, parts, dparts, flags_and_maxnargs); + } else { + /* NaN timevalue: we need to coerce the arguments, but + * the resulting internal timestamp needs to remain NaN. + * This works but is not pretty: parts and dparts will + * be partially uninitialized, but we only write to them. + */ + } + + /* + * Determining which datetime components to overwrite based on + * stack arguments is a bit complicated, but important to factor + * out from setters themselves for compactness. + * + * If DUK_DATE_FLAG_TIMESETTER, maxnargs indicates setter type: + * + * 1 -> millisecond + * 2 -> second, [millisecond] + * 3 -> minute, [second], [millisecond] + * 4 -> hour, [minute], [second], [millisecond] + * + * Else: + * + * 1 -> date + * 2 -> month, [date] + * 3 -> year, [month], [date] + * + * By comparing nargs and maxnargs (and flags) we know which + * components to override. We rely on part index ordering. + */ + + if (flags_and_maxnargs & DUK_DATE_FLAG_TIMESETTER) { + DUK_ASSERT(maxnargs >= 1 && maxnargs <= 4); + idx_first = DUK_DATE_IDX_MILLISECOND - (maxnargs - 1); + } else { + DUK_ASSERT(maxnargs >= 1 && maxnargs <= 3); + idx_first = DUK_DATE_IDX_DAY - (maxnargs - 1); + } + DUK_ASSERT_DISABLE(idx_first >= 0); /* unsigned */ + DUK_ASSERT(idx_first < DUK_DATE_IDX_NUM_PARTS); + + for (i = 0; i < maxnargs; i++) { + if ((duk_idx_t) i >= nargs) { + /* no argument given -> leave components untouched */ + break; + } + idx = idx_first + i; + DUK_ASSERT_DISABLE(idx >= 0); /* unsigned */ + DUK_ASSERT(idx < DUK_DATE_IDX_NUM_PARTS); + + if (idx == DUK_DATE_IDX_YEAR && (flags_and_maxnargs & DUK_DATE_FLAG_YEAR_FIXUP)) { + duk__twodigit_year_fixup(ctx, (duk_idx_t) i); + } + + dparts[idx] = duk_to_number(ctx, i); + + if (idx == DUK_DATE_IDX_DAY) { + /* Day-of-month is one-based in the API, but zero-based + * internally, so fix here. Note that month is zero-based + * both in the API and internally. + */ + /* SCANBUILD: complains about use of uninitialized values. + * The complaint is correct, but operating in undefined + * values here is intentional in some cases and the caller + * ignores the results. + */ + dparts[idx] -= 1.0; + } + } + + /* Leaves new timevalue on stack top and returns 1, which is correct + * for part setters. + */ + if (DUK_ISFINITE(d)) { + return duk__set_this_timeval_from_dparts(ctx, dparts, flags_and_maxnargs); + } else { + /* Internal timevalue is already NaN, so don't touch it. */ + duk_push_nan(ctx); + return 1; + } +} + +/* Apply ToNumber() to specified index; if ToInteger(val) in [0,99], add + * 1900 and replace value at idx_val. + */ +DUK_LOCAL void duk__twodigit_year_fixup(duk_context *ctx, duk_idx_t idx_val) { + duk_double_t d; + + /* XXX: idx_val would fit into 16 bits, but using duk_small_uint_t + * might not generate better code due to casting. + */ + + /* E5 Sections 15.9.3.1, B.2.4, B.2.5 */ + duk_to_number(ctx, idx_val); + if (duk_is_nan(ctx, idx_val)) { + return; + } + duk_dup(ctx, idx_val); + duk_to_int(ctx, -1); + d = duk_get_number(ctx, -1); /* get as double to handle huge numbers correctly */ + if (d >= 0.0 && d <= 99.0) { + d += 1900.0; + duk_push_number(ctx, d); + duk_replace(ctx, idx_val); + } + duk_pop(ctx); +} + +/* Set datetime parts from stack arguments, defaulting any missing values. + * Day-of-week is not set; it is not required when setting the time value. + */ +DUK_LOCAL void duk__set_parts_from_args(duk_context *ctx, duk_double_t *dparts, duk_idx_t nargs) { + duk_double_t d; + duk_small_uint_t i; + duk_small_uint_t idx; + + /* Causes a ToNumber() coercion, but doesn't break coercion order since + * year is coerced first anyway. + */ + duk__twodigit_year_fixup(ctx, 0); + + /* There are at most 7 args, but we use 8 here so that also + * DUK_DATE_IDX_WEEKDAY gets initialized (to zero) to avoid the potential + * for any Valgrind gripes later. + */ + for (i = 0; i < 8; i++) { + /* Note: rely on index ordering */ + idx = DUK_DATE_IDX_YEAR + i; + if ((duk_idx_t) i < nargs) { + d = duk_to_number(ctx, (duk_idx_t) i); + if (idx == DUK_DATE_IDX_DAY) { + /* Convert day from one-based to zero-based (internal). This may + * cause the day part to be negative, which is OK. + */ + d -= 1.0; + } + } else { + /* All components default to 0 except day-of-month which defaults + * to 1. However, because our internal day-of-month is zero-based, + * it also defaults to zero here. + */ + d = 0.0; + } + dparts[idx] = d; + } + + DUK_DDD(DUK_DDDPRINT("parts from args -> %lf %lf %lf %lf %lf %lf %lf %lf", + (double) dparts[0], (double) dparts[1], + (double) dparts[2], (double) dparts[3], + (double) dparts[4], (double) dparts[5], + (double) dparts[6], (double) dparts[7])); +} + +/* + * Helper to format a time value into caller buffer, used by logging. + * 'out_buf' must be at least DUK_BI_DATE_ISO8601_BUFSIZE long. + */ + +DUK_INTERNAL void duk_bi_date_format_timeval(duk_double_t timeval, duk_uint8_t *out_buf) { + duk_int_t parts[DUK_DATE_IDX_NUM_PARTS]; + + duk_bi_date_timeval_to_parts(timeval, + parts, + NULL, + DUK_DATE_FLAG_ONEBASED); + + duk__format_parts_iso8601(parts, + 0 /*tzoffset*/, + DUK_DATE_FLAG_TOSTRING_DATE | + DUK_DATE_FLAG_TOSTRING_TIME | + DUK_DATE_FLAG_SEP_T /*flags*/, + out_buf); +} + +/* + * Indirect magic value lookup for Date methods. + * + * Date methods don't put their control flags into the function magic value + * because they wouldn't fit into a LIGHTFUNC's magic field. Instead, the + * magic value is set to an index pointing to the array of control flags + * below. + * + * This must be kept in strict sync with genbuiltins.py! + */ + +static duk_uint16_t duk__date_magics[] = { + /* 0: toString */ + DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_LOCALTIME, + + /* 1: toDateString */ + DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_LOCALTIME, + + /* 2: toTimeString */ + DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_LOCALTIME, + + /* 3: toLocaleString */ + DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_TOSTRING_LOCALE + DUK_DATE_FLAG_LOCALTIME, + + /* 4: toLocaleDateString */ + DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_LOCALE + DUK_DATE_FLAG_LOCALTIME, + + /* 5: toLocaleTimeString */ + DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_TOSTRING_LOCALE + DUK_DATE_FLAG_LOCALTIME, + + /* 6: toUTCString */ + DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME, + + /* 7: toISOString */ + DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_NAN_TO_RANGE_ERROR + DUK_DATE_FLAG_SEP_T, + + /* 8: getFullYear */ + DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_YEAR << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 9: getUTCFullYear */ + 0 + (DUK_DATE_IDX_YEAR << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 10: getMonth */ + DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_MONTH << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 11: getUTCMonth */ + 0 + (DUK_DATE_IDX_MONTH << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 12: getDate */ + DUK_DATE_FLAG_ONEBASED + DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_DAY << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 13: getUTCDate */ + DUK_DATE_FLAG_ONEBASED + (DUK_DATE_IDX_DAY << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 14: getDay */ + DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_WEEKDAY << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 15: getUTCDay */ + 0 + (DUK_DATE_IDX_WEEKDAY << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 16: getHours */ + DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_HOUR << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 17: getUTCHours */ + 0 + (DUK_DATE_IDX_HOUR << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 18: getMinutes */ + DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_MINUTE << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 19: getUTCMinutes */ + 0 + (DUK_DATE_IDX_MINUTE << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 20: getSeconds */ + DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_SECOND << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 21: getUTCSeconds */ + 0 + (DUK_DATE_IDX_SECOND << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 22: getMilliseconds */ + DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_MILLISECOND << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 23: getUTCMilliseconds */ + 0 + (DUK_DATE_IDX_MILLISECOND << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 24: setMilliseconds */ + DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (1 << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 25: setUTCMilliseconds */ + DUK_DATE_FLAG_TIMESETTER + (1 << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 26: setSeconds */ + DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (2 << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 27: setUTCSeconds */ + DUK_DATE_FLAG_TIMESETTER + (2 << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 28: setMinutes */ + DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (3 << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 29: setUTCMinutes */ + DUK_DATE_FLAG_TIMESETTER + (3 << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 30: setHours */ + DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (4 << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 31: setUTCHours */ + DUK_DATE_FLAG_TIMESETTER + (4 << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 32: setDate */ + DUK_DATE_FLAG_LOCALTIME + (1 << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 33: setUTCDate */ + 0 + (1 << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 34: setMonth */ + DUK_DATE_FLAG_LOCALTIME + (2 << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 35: setUTCMonth */ + 0 + (2 << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 36: setFullYear */ + DUK_DATE_FLAG_NAN_TO_ZERO + DUK_DATE_FLAG_LOCALTIME + (3 << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 37: setUTCFullYear */ + DUK_DATE_FLAG_NAN_TO_ZERO + (3 << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 38: getYear */ + DUK_DATE_FLAG_LOCALTIME + DUK_DATE_FLAG_SUB1900 + (DUK_DATE_IDX_YEAR << DUK_DATE_FLAG_VALUE_SHIFT), + + /* 39: setYear */ + DUK_DATE_FLAG_NAN_TO_ZERO + DUK_DATE_FLAG_YEAR_FIXUP + (3 << DUK_DATE_FLAG_VALUE_SHIFT), +}; + +DUK_LOCAL duk_small_uint_t duk__date_get_indirect_magic(duk_context *ctx) { + duk_small_int_t magicidx = (duk_small_uint_t) duk_get_current_magic(ctx); + DUK_ASSERT(magicidx >= 0 && magicidx < (duk_small_int_t) (sizeof(duk__date_magics) / sizeof(duk_uint16_t))); + return (duk_small_uint_t) duk__date_magics[magicidx]; +} + +/* + * Constructor calls + */ + +DUK_INTERNAL duk_ret_t duk_bi_date_constructor(duk_context *ctx) { + duk_idx_t nargs = duk_get_top(ctx); + duk_bool_t is_cons = duk_is_constructor_call(ctx); + duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS]; + duk_double_t d; + + DUK_DDD(DUK_DDDPRINT("Date constructor, nargs=%ld, is_cons=%ld", (long) nargs, (long) is_cons)); + + duk_push_object_helper(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DATE), + DUK_BIDX_DATE_PROTOTYPE); + + /* Unlike most built-ins, the internal [[PrimitiveValue]] of a Date + * is mutable. + */ + + if (nargs == 0 || !is_cons) { + d = duk__timeclip(DUK_USE_DATE_GET_NOW(ctx)); + duk_push_number(ctx, d); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_W); + if (!is_cons) { + /* called as a normal function: return new Date().toString() */ + duk_to_string(ctx, -1); + } + return 1; + } else if (nargs == 1) { + duk_to_primitive(ctx, 0, DUK_HINT_NONE); + if (duk_is_string(ctx, 0)) { + duk__parse_string(ctx, duk_to_string(ctx, 0)); + duk_replace(ctx, 0); /* may be NaN */ + } + d = duk__timeclip(duk_to_number(ctx, 0)); + duk_push_number(ctx, d); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_W); + return 1; + } + + duk__set_parts_from_args(ctx, dparts, nargs); + + /* Parts are in local time, convert when setting. */ + + (void) duk__set_this_timeval_from_dparts(ctx, dparts, DUK_DATE_FLAG_LOCALTIME /*flags*/); /* -> [ ... this timeval ] */ + duk_pop(ctx); /* -> [ ... this ] */ + return 1; +} + +DUK_INTERNAL duk_ret_t duk_bi_date_constructor_parse(duk_context *ctx) { + return duk__parse_string(ctx, duk_to_string(ctx, 0)); +} + +DUK_INTERNAL duk_ret_t duk_bi_date_constructor_utc(duk_context *ctx) { + duk_idx_t nargs = duk_get_top(ctx); + duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS]; + duk_double_t d; + + /* Behavior for nargs < 2 is implementation dependent: currently we'll + * set a NaN time value (matching V8 behavior) in this case. + */ + + if (nargs < 2) { + duk_push_nan(ctx); + } else { + duk__set_parts_from_args(ctx, dparts, nargs); + d = duk_bi_date_get_timeval_from_dparts(dparts, 0 /*flags*/); + duk_push_number(ctx, d); + } + return 1; +} + +DUK_INTERNAL duk_ret_t duk_bi_date_constructor_now(duk_context *ctx) { + duk_double_t d; + + d = DUK_USE_DATE_GET_NOW(ctx); + DUK_ASSERT(duk__timeclip(d) == d); /* TimeClip() should never be necessary */ + duk_push_number(ctx, d); + return 1; +} + +/* + * String/JSON conversions + * + * Human readable conversions are now basically ISO 8601 with a space + * (instead of 'T') as the date/time separator. This is a good baseline + * and is platform independent. + * + * A shared native helper to provide many conversions. Magic value contains + * a set of flags. The helper provides: + * + * toString() + * toDateString() + * toTimeString() + * toLocaleString() + * toLocaleDateString() + * toLocaleTimeString() + * toUTCString() + * toISOString() + * + * Notes: + * + * - Date.prototype.toGMTString() and Date.prototype.toUTCString() are + * required to be the same Ecmascript function object (!), so it is + * omitted from here. + * + * - Date.prototype.toUTCString(): E5.1 specification does not require a + * specific format, but result should be human readable. The + * specification suggests using ISO 8601 format with a space (instead + * of 'T') separator if a more human readable format is not available. + * + * - Date.prototype.toISOString(): unlike other conversion functions, + * toISOString() requires a RangeError for invalid date values. + */ + +DUK_INTERNAL duk_ret_t duk_bi_date_prototype_tostring_shared(duk_context *ctx) { + duk_small_uint_t flags = duk__date_get_indirect_magic(ctx); + return duk__to_string_helper(ctx, flags); +} + +DUK_INTERNAL duk_ret_t duk_bi_date_prototype_value_of(duk_context *ctx) { + /* This native function is also used for Date.prototype.getTime() + * as their behavior is identical. + */ + + duk_double_t d = duk__push_this_get_timeval(ctx, 0 /*flags*/); /* -> [ this ] */ + DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d)); + duk_push_number(ctx, d); + return 1; +} + +DUK_INTERNAL duk_ret_t duk_bi_date_prototype_to_json(duk_context *ctx) { + /* Note: toJSON() is a generic function which works even if 'this' + * is not a Date. The sole argument is ignored. + */ + + duk_push_this(ctx); + duk_to_object(ctx, -1); + + duk_dup_top(ctx); + duk_to_primitive(ctx, -1, DUK_HINT_NUMBER); + if (duk_is_number(ctx, -1)) { + duk_double_t d = duk_get_number(ctx, -1); + if (!DUK_ISFINITE(d)) { + duk_push_null(ctx); + return 1; + } + } + duk_pop(ctx); + + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_TO_ISO_STRING); + duk_dup(ctx, -2); /* -> [ O toIsoString O ] */ + duk_call_method(ctx, 0); + return 1; +} + +/* + * Getters. + * + * Implementing getters is quite easy. The internal time value is either + * NaN, or represents milliseconds (without fractions) from Jan 1, 1970. + * The internal time value can be converted to integer parts, and each + * part will be normalized and will fit into a 32-bit signed integer. + * + * A shared native helper to provide all getters. Magic value contains + * a set of flags and also packs the date component index argument. The + * helper provides: + * + * getFullYear() + * getUTCFullYear() + * getMonth() + * getUTCMonth() + * getDate() + * getUTCDate() + * getDay() + * getUTCDay() + * getHours() + * getUTCHours() + * getMinutes() + * getUTCMinutes() + * getSeconds() + * getUTCSeconds() + * getMilliseconds() + * getUTCMilliseconds() + * getYear() + * + * Notes: + * + * - Date.prototype.getDate(): 'date' means day-of-month, and is + * zero-based in internal calculations but public API expects it to + * be one-based. + * + * - Date.prototype.getTime() and Date.prototype.valueOf() have identical + * behavior. They have separate function objects, but share the same C + * function (duk_bi_date_prototype_value_of). + */ + +DUK_INTERNAL duk_ret_t duk_bi_date_prototype_get_shared(duk_context *ctx) { + duk_small_uint_t flags_and_idx = duk__date_get_indirect_magic(ctx); + return duk__get_part_helper(ctx, flags_and_idx); +} + +DUK_INTERNAL duk_ret_t duk_bi_date_prototype_get_timezone_offset(duk_context *ctx) { + /* + * Return (t - LocalTime(t)) in minutes: + * + * t - LocalTime(t) = t - (t + LocalTZA + DaylightSavingTA(t)) + * = -(LocalTZA + DaylightSavingTA(t)) + * + * where DaylightSavingTA() is checked for time 't'. + * + * Note that the sign of the result is opposite to common usage, + * e.g. for EE(S)T which normally is +2h or +3h from UTC, this + * function returns -120 or -180. + * + */ + + duk_double_t d; + duk_int_t tzoffset; + + /* Note: DST adjustment is determined using UTC time. */ + d = duk__push_this_get_timeval(ctx, 0 /*flags*/); + DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d)); + if (DUK_ISNAN(d)) { + duk_push_nan(ctx); + } else { + DUK_ASSERT(DUK_ISFINITE(d)); + tzoffset = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d); + duk_push_int(ctx, -tzoffset / 60); + } + return 1; +} + +/* + * Setters. + * + * Setters are a bit more complicated than getters. Component setters + * break down the current time value into its (normalized) component + * parts, replace one or more components with -unnormalized- new values, + * and the components are then converted back into a time value. As an + * example of using unnormalized values: + * + * var d = new Date(1234567890); + * + * is equivalent to: + * + * var d = new Date(0); + * d.setUTCMilliseconds(1234567890); + * + * A shared native helper to provide almost all setters. Magic value + * contains a set of flags and also packs the "maxnargs" argument. The + * helper provides: + * + * setMilliseconds() + * setUTCMilliseconds() + * setSeconds() + * setUTCSeconds() + * setMinutes() + * setUTCMinutes() + * setHours() + * setUTCHours() + * setDate() + * setUTCDate() + * setMonth() + * setUTCMonth() + * setFullYear() + * setUTCFullYear() + * setYear() + * + * Notes: + * + * - Date.prototype.setYear() (Section B addition): special year check + * is omitted. NaN / Infinity will just flow through and ultimately + * result in a NaN internal time value. + * + * - Date.prototype.setYear() does not have optional arguments for + * setting month and day-in-month (like setFullYear()), but we indicate + * 'maxnargs' to be 3 to get the year written to the correct component + * index in duk__set_part_helper(). The function has nargs == 1, so only + * the year will be set regardless of actual argument count. + */ + +DUK_INTERNAL duk_ret_t duk_bi_date_prototype_set_shared(duk_context *ctx) { + duk_small_uint_t flags_and_maxnargs = duk__date_get_indirect_magic(ctx); + return duk__set_part_helper(ctx, flags_and_maxnargs); +} + +DUK_INTERNAL duk_ret_t duk_bi_date_prototype_set_time(duk_context *ctx) { + duk_double_t d; + + (void) duk__push_this_get_timeval(ctx, 0 /*flags*/); /* -> [ timeval this ] */ + d = duk__timeclip(duk_to_number(ctx, 0)); + duk_push_number(ctx, d); + duk_dup_top(ctx); + duk_put_prop_stridx(ctx, -3, DUK_STRIDX_INT_VALUE); /* -> [ timeval this timeval ] */ + + return 1; +} +/* + * Unix-like Date providers + * + * Generally useful Unix / POSIX / ANSI Date providers. + */ + +/* include removed: duk_internal.h */ + +/* The necessary #includes are in place in duk_config.h. */ + +/* Buffer sizes for some UNIX calls. Larger than strictly necessary + * to avoid Valgrind errors. + */ +#define DUK__STRPTIME_BUF_SIZE 64 +#define DUK__STRFTIME_BUF_SIZE 64 + +#if defined(DUK_USE_DATE_NOW_GETTIMEOFDAY) +/* Get current Ecmascript time (= UNIX/Posix time, but in milliseconds). */ +DUK_INTERNAL duk_double_t duk_bi_date_get_now_gettimeofday(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + struct timeval tv; + duk_double_t d; + + if (gettimeofday(&tv, NULL) != 0) { + DUK_ERROR_INTERNAL_DEFMSG(thr); + } + + d = ((duk_double_t) tv.tv_sec) * 1000.0 + + ((duk_double_t) (tv.tv_usec / 1000)); + DUK_ASSERT(DUK_FLOOR(d) == d); /* no fractions */ + + return d; +} +#endif /* DUK_USE_DATE_NOW_GETTIMEOFDAY */ + +#if defined(DUK_USE_DATE_NOW_TIME) +/* Not a very good provider: only full seconds are available. */ +DUK_INTERNAL duk_double_t duk_bi_date_get_now_time(duk_context *ctx) { + time_t t; + + DUK_UNREF(ctx); + t = time(NULL); + return ((duk_double_t) t) * 1000.0; +} +#endif /* DUK_USE_DATE_NOW_TIME */ + +#if defined(DUK_USE_DATE_TZO_GMTIME) || defined(DUK_USE_DATE_TZO_GMTIME_R) +/* Get local time offset (in seconds) for a certain (UTC) instant 'd'. */ +DUK_INTERNAL duk_int_t duk_bi_date_get_local_tzoffset_gmtime(duk_double_t d) { + time_t t, t1, t2; + duk_int_t parts[DUK_DATE_IDX_NUM_PARTS]; + duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS]; + struct tm tms[2]; +#ifdef DUK_USE_DATE_TZO_GMTIME + struct tm *tm_ptr; +#endif + + /* For NaN/inf, the return value doesn't matter. */ + if (!DUK_ISFINITE(d)) { + return 0; + } + + /* If not within Ecmascript range, some integer time calculations + * won't work correctly (and some asserts will fail), so bail out + * if so. This fixes test-bug-date-insane-setyear.js. There is + * a +/- 24h leeway in this range check to avoid a test262 corner + * case documented in test-bug-date-timeval-edges.js. + */ + if (!duk_bi_date_timeval_in_leeway_range(d)) { + DUK_DD(DUK_DDPRINT("timeval not within valid range, skip tzoffset computation to avoid integer overflows")); + return 0; + } + + /* + * This is a bit tricky to implement portably. The result depends + * on the timestamp (specifically, DST depends on the timestamp). + * If e.g. UNIX APIs are used, they'll have portability issues with + * very small and very large years. + * + * Current approach: + * + * - Stay within portable UNIX limits by using equivalent year mapping. + * Avoid year 1970 and 2038 as some conversions start to fail, at + * least on some platforms. Avoiding 1970 means that there are + * currently DST discrepancies for 1970. + * + * - Create a UTC and local time breakdowns from 't'. Then create + * a time_t using gmtime() and localtime() and compute the time + * difference between the two. + * + * Equivalent year mapping (E5 Section 15.9.1.8): + * + * If the host environment provides functionality for determining + * daylight saving time, the implementation of ECMAScript is free + * to map the year in question to an equivalent year (same + * leap-year-ness and same starting week day for the year) for which + * the host environment provides daylight saving time information. + * The only restriction is that all equivalent years should produce + * the same result. + * + * This approach is quite reasonable but not entirely correct, e.g. + * the specification also states (E5 Section 15.9.1.8): + * + * The implementation of ECMAScript should not try to determine + * whether the exact time was subject to daylight saving time, but + * just whether daylight saving time would have been in effect if + * the _current daylight saving time algorithm_ had been used at the + * time. This avoids complications such as taking into account the + * years that the locale observed daylight saving time year round. + * + * Since we rely on the platform APIs for conversions between local + * time and UTC, we can't guarantee the above. Rather, if the platform + * has historical DST rules they will be applied. This seems to be the + * general preferred direction in Ecmascript standardization (or at least + * implementations) anyway, and even the equivalent year mapping should + * be disabled if the platform is known to handle DST properly for the + * full Ecmascript range. + * + * The following has useful discussion and links: + * + * https://bugzilla.mozilla.org/show_bug.cgi?id=351066 + */ + + duk_bi_date_timeval_to_parts(d, parts, dparts, DUK_DATE_FLAG_EQUIVYEAR /*flags*/); + DUK_ASSERT(parts[DUK_DATE_IDX_YEAR] >= 1970 && parts[DUK_DATE_IDX_YEAR] <= 2038); + + d = duk_bi_date_get_timeval_from_dparts(dparts, 0 /*flags*/); + DUK_ASSERT(d >= 0 && d < 2147483648.0 * 1000.0); /* unsigned 31-bit range */ + t = (time_t) (d / 1000.0); + DUK_DDD(DUK_DDDPRINT("timeval: %lf -> time_t %ld", (double) d, (long) t)); + + DUK_MEMZERO((void *) tms, sizeof(struct tm) * 2); + +#if defined(DUK_USE_DATE_TZO_GMTIME_R) + (void) gmtime_r(&t, &tms[0]); + (void) localtime_r(&t, &tms[1]); +#elif defined(DUK_USE_DATE_TZO_GMTIME) + tm_ptr = gmtime(&t); + DUK_MEMCPY((void *) &tms[0], tm_ptr, sizeof(struct tm)); + tm_ptr = localtime(&t); + DUK_MEMCPY((void *) &tms[1], tm_ptr, sizeof(struct tm)); +#else +#error internal error +#endif + DUK_DDD(DUK_DDDPRINT("gmtime result: tm={sec:%ld,min:%ld,hour:%ld,mday:%ld,mon:%ld,year:%ld," + "wday:%ld,yday:%ld,isdst:%ld}", + (long) tms[0].tm_sec, (long) tms[0].tm_min, (long) tms[0].tm_hour, + (long) tms[0].tm_mday, (long) tms[0].tm_mon, (long) tms[0].tm_year, + (long) tms[0].tm_wday, (long) tms[0].tm_yday, (long) tms[0].tm_isdst)); + DUK_DDD(DUK_DDDPRINT("localtime result: tm={sec:%ld,min:%ld,hour:%ld,mday:%ld,mon:%ld,year:%ld," + "wday:%ld,yday:%ld,isdst:%ld}", + (long) tms[1].tm_sec, (long) tms[1].tm_min, (long) tms[1].tm_hour, + (long) tms[1].tm_mday, (long) tms[1].tm_mon, (long) tms[1].tm_year, + (long) tms[1].tm_wday, (long) tms[1].tm_yday, (long) tms[1].tm_isdst)); + + /* tm_isdst is both an input and an output to mktime(), use 0 to + * avoid DST handling in mktime(): + * - https://github.com/svaarala/duktape/issues/406 + * - http://stackoverflow.com/questions/8558919/mktime-and-tm-isdst + */ + tms[0].tm_isdst = 0; + tms[1].tm_isdst = 0; + t1 = mktime(&tms[0]); /* UTC */ + t2 = mktime(&tms[1]); /* local */ + if (t1 == (time_t) -1 || t2 == (time_t) -1) { + /* This check used to be for (t < 0) but on some platforms + * time_t is unsigned and apparently the proper way to detect + * an mktime() error return is the cast above. See e.g.: + * http://pubs.opengroup.org/onlinepubs/009695299/functions/mktime.html + */ + goto error; + } + DUK_DDD(DUK_DDDPRINT("t1=%ld (utc), t2=%ld (local)", (long) t1, (long) t2)); + + /* Compute final offset in seconds, positive if local time ahead of + * UTC (returned value is UTC-to-local offset). + * + * difftime() returns a double, so coercion to int generates quite + * a lot of code. Direct subtraction is not portable, however. + * XXX: allow direct subtraction on known platforms. + */ +#if 0 + return (duk_int_t) (t2 - t1); +#endif + return (duk_int_t) difftime(t2, t1); + + error: + /* XXX: return something more useful, so that caller can throw? */ + DUK_D(DUK_DPRINT("mktime() failed, d=%lf", (double) d)); + return 0; +} +#endif /* DUK_USE_DATE_TZO_GMTIME */ + +#if defined(DUK_USE_DATE_PRS_STRPTIME) +DUK_INTERNAL duk_bool_t duk_bi_date_parse_string_strptime(duk_context *ctx, const char *str) { + struct tm tm; + time_t t; + char buf[DUK__STRPTIME_BUF_SIZE]; + + /* copy to buffer with spare to avoid Valgrind gripes from strptime */ + DUK_ASSERT(str != NULL); + DUK_MEMZERO(buf, sizeof(buf)); /* valgrind whine without this */ + DUK_SNPRINTF(buf, sizeof(buf), "%s", (const char *) str); + buf[sizeof(buf) - 1] = (char) 0; + + DUK_DDD(DUK_DDDPRINT("parsing: '%s'", (const char *) buf)); + + DUK_MEMZERO(&tm, sizeof(tm)); + if (strptime((const char *) buf, "%c", &tm) != NULL) { + DUK_DDD(DUK_DDDPRINT("before mktime: tm={sec:%ld,min:%ld,hour:%ld,mday:%ld,mon:%ld,year:%ld," + "wday:%ld,yday:%ld,isdst:%ld}", + (long) tm.tm_sec, (long) tm.tm_min, (long) tm.tm_hour, + (long) tm.tm_mday, (long) tm.tm_mon, (long) tm.tm_year, + (long) tm.tm_wday, (long) tm.tm_yday, (long) tm.tm_isdst)); + tm.tm_isdst = -1; /* negative: dst info not available */ + + t = mktime(&tm); + DUK_DDD(DUK_DDDPRINT("mktime() -> %ld", (long) t)); + if (t >= 0) { + duk_push_number(ctx, ((duk_double_t) t) * 1000.0); + return 1; + } + } + + return 0; +} +#endif /* DUK_USE_DATE_PRS_STRPTIME */ + +#if defined(DUK_USE_DATE_PRS_GETDATE) +DUK_INTERNAL duk_bool_t duk_bi_date_parse_string_getdate(duk_context *ctx, const char *str) { + struct tm tm; + duk_small_int_t rc; + time_t t; + + /* For this to work, DATEMSK must be set, so this is not very + * convenient for an embeddable interpreter. + */ + + DUK_MEMZERO(&tm, sizeof(struct tm)); + rc = (duk_small_int_t) getdate_r(str, &tm); + DUK_DDD(DUK_DDDPRINT("getdate_r() -> %ld", (long) rc)); + + if (rc == 0) { + t = mktime(&tm); + DUK_DDD(DUK_DDDPRINT("mktime() -> %ld", (long) t)); + if (t >= 0) { + duk_push_number(ctx, (duk_double_t) t); + return 1; + } + } + + return 0; +} +#endif /* DUK_USE_DATE_PRS_GETDATE */ + +#if defined(DUK_USE_DATE_FMT_STRFTIME) +DUK_INTERNAL duk_bool_t duk_bi_date_format_parts_strftime(duk_context *ctx, duk_int_t *parts, duk_int_t tzoffset, duk_small_uint_t flags) { + char buf[DUK__STRFTIME_BUF_SIZE]; + struct tm tm; + const char *fmt; + + DUK_UNREF(tzoffset); + + /* If the platform doesn't support the entire Ecmascript range, we need + * to return 0 so that the caller can fall back to the default formatter. + * + * For now, assume that if time_t is 8 bytes or more, the whole Ecmascript + * range is supported. For smaller time_t values (4 bytes in practice), + * assumes that the signed 32-bit range is supported. + * + * XXX: detect this more correctly per platform. The size of time_t is + * probably not an accurate guarantee of strftime() supporting or not + * supporting a large time range (the full Ecmascript range). + */ + if (sizeof(time_t) < 8 && + (parts[DUK_DATE_IDX_YEAR] < 1970 || parts[DUK_DATE_IDX_YEAR] > 2037)) { + /* be paranoid for 32-bit time values (even avoiding negative ones) */ + return 0; + } + + DUK_MEMZERO(&tm, sizeof(tm)); + tm.tm_sec = parts[DUK_DATE_IDX_SECOND]; + tm.tm_min = parts[DUK_DATE_IDX_MINUTE]; + tm.tm_hour = parts[DUK_DATE_IDX_HOUR]; + tm.tm_mday = parts[DUK_DATE_IDX_DAY]; /* already one-based */ + tm.tm_mon = parts[DUK_DATE_IDX_MONTH] - 1; /* one-based -> zero-based */ + tm.tm_year = parts[DUK_DATE_IDX_YEAR] - 1900; + tm.tm_wday = parts[DUK_DATE_IDX_WEEKDAY]; + tm.tm_isdst = 0; + + DUK_MEMZERO(buf, sizeof(buf)); + if ((flags & DUK_DATE_FLAG_TOSTRING_DATE) && (flags & DUK_DATE_FLAG_TOSTRING_TIME)) { + fmt = "%c"; + } else if (flags & DUK_DATE_FLAG_TOSTRING_DATE) { + fmt = "%x"; + } else { + DUK_ASSERT(flags & DUK_DATE_FLAG_TOSTRING_TIME); + fmt = "%X"; + } + (void) strftime(buf, sizeof(buf) - 1, fmt, &tm); + DUK_ASSERT(buf[sizeof(buf) - 1] == 0); + + duk_push_string(ctx, buf); + return 1; +} +#endif /* DUK_USE_DATE_FMT_STRFTIME */ + +#undef DUK__STRPTIME_BUF_SIZE +#undef DUK__STRFTIME_BUF_SIZE +/* + * Windows Date providers + * + * Platform specific links: + * + * - http://msdn.microsoft.com/en-us/library/windows/desktop/ms725473(v=vs.85).aspx + */ + +/* include removed: duk_internal.h */ + +/* The necessary #includes are in place in duk_config.h. */ + +#if defined(DUK_USE_DATE_NOW_WINDOWS) || defined(DUK_USE_DATE_TZO_WINDOWS) +/* Shared Windows helpers. */ +DUK_LOCAL void duk__convert_systime_to_ularge(const SYSTEMTIME *st, ULARGE_INTEGER *res) { + FILETIME ft; + if (SystemTimeToFileTime(st, &ft) == 0) { + DUK_D(DUK_DPRINT("SystemTimeToFileTime() failed, returning 0")); + res->QuadPart = 0; + } else { + res->LowPart = ft.dwLowDateTime; + res->HighPart = ft.dwHighDateTime; + } +} +DUK_LOCAL void duk__set_systime_jan1970(SYSTEMTIME *st) { + DUK_MEMZERO((void *) st, sizeof(*st)); + st->wYear = 1970; + st->wMonth = 1; + st->wDayOfWeek = 4; /* not sure whether or not needed; Thursday */ + st->wDay = 1; + DUK_ASSERT(st->wHour == 0); + DUK_ASSERT(st->wMinute == 0); + DUK_ASSERT(st->wSecond == 0); + DUK_ASSERT(st->wMilliseconds == 0); +} +#endif /* defined(DUK_USE_DATE_NOW_WINDOWS) || defined(DUK_USE_DATE_TZO_WINDOWS) */ + +#ifdef DUK_USE_DATE_NOW_WINDOWS +DUK_INTERNAL duk_double_t duk_bi_date_get_now_windows(duk_context *ctx) { + /* Suggested step-by-step method from documentation of RtlTimeToSecondsSince1970: + * http://msdn.microsoft.com/en-us/library/windows/desktop/ms724928(v=vs.85).aspx + */ + SYSTEMTIME st1, st2; + ULARGE_INTEGER tmp1, tmp2; + + DUK_UNREF(ctx); + + GetSystemTime(&st1); + duk__convert_systime_to_ularge((const SYSTEMTIME *) &st1, &tmp1); + + duk__set_systime_jan1970(&st2); + duk__convert_systime_to_ularge((const SYSTEMTIME *) &st2, &tmp2); + + /* Difference is in 100ns units, convert to milliseconds w/o fractions */ + return (duk_double_t) ((tmp1.QuadPart - tmp2.QuadPart) / 10000LL); +} +#endif /* DUK_USE_DATE_NOW_WINDOWS */ + + +#if defined(DUK_USE_DATE_TZO_WINDOWS) +DUK_INTERNAL_DECL duk_int_t duk_bi_date_get_local_tzoffset_windows(duk_double_t d) { + SYSTEMTIME st1; + SYSTEMTIME st2; + SYSTEMTIME st3; + ULARGE_INTEGER tmp1; + ULARGE_INTEGER tmp2; + ULARGE_INTEGER tmp3; + FILETIME ft1; + + /* XXX: handling of timestamps outside Windows supported range. + * How does Windows deal with dates before 1600? Does windows + * support all Ecmascript years (like -200000 and +200000)? + * Should equivalent year mapping be used here too? If so, use + * a shared helper (currently integrated into timeval-to-parts). + */ + + /* Use the approach described in "Remarks" of FileTimeToLocalFileTime: + * http://msdn.microsoft.com/en-us/library/windows/desktop/ms724277(v=vs.85).aspx + */ + + duk__set_systime_jan1970(&st1); + duk__convert_systime_to_ularge((const SYSTEMTIME *) &st1, &tmp1); + tmp2.QuadPart = (ULONGLONG) (d * 10000.0); /* millisec -> 100ns units since jan 1, 1970 */ + tmp2.QuadPart += tmp1.QuadPart; /* input 'd' in Windows UTC, 100ns units */ + + ft1.dwLowDateTime = tmp2.LowPart; + ft1.dwHighDateTime = tmp2.HighPart; + FileTimeToSystemTime((const FILETIME *) &ft1, &st2); + if (SystemTimeToTzSpecificLocalTime((LPTIME_ZONE_INFORMATION) NULL, &st2, &st3) == 0) { + DUK_D(DUK_DPRINT("SystemTimeToTzSpecificLocalTime() failed, return tzoffset 0")); + return 0; + } + duk__convert_systime_to_ularge((const SYSTEMTIME *) &st3, &tmp3); + + /* Positive if local time ahead of UTC. */ + return (duk_int_t) (((LONGLONG) tmp3.QuadPart - (LONGLONG) tmp2.QuadPart) / 10000000LL); /* seconds */ +} +#endif /* DUK_USE_DATE_TZO_WINDOWS */ +/* + * Duktape built-ins + * + * Size optimization note: it might seem that vararg multipurpose functions + * like fin(), enc(), and dec() are not very size optimal, but using a single + * user-visible Ecmascript function saves a lot of run-time footprint; each + * Function instance takes >100 bytes. Using a shared native helper and a + * 'magic' value won't save much if there are multiple Function instances + * anyway. + */ + +/* include removed: duk_internal.h */ + +/* Raw helper to extract internal information / statistics about a value. + * The return values are version specific and must not expose anything + * that would lead to security issues (e.g. exposing compiled function + * 'data' buffer might be an issue). Currently only counts and sizes and + * such are given so there should not be a security impact. + */ +DUK_INTERNAL duk_ret_t duk_bi_duktape_object_info(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_tval *tv; + duk_heaphdr *h; + duk_int_t i, n; + + DUK_UNREF(thr); + + /* result array */ + duk_push_array(ctx); /* -> [ val arr ] */ + + /* type tag (public) */ + duk_push_int(ctx, duk_get_type(ctx, 0)); + + /* address */ + tv = duk_get_tval(ctx, 0); + DUK_ASSERT(tv != NULL); /* because arg count is 1 */ + if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) { + h = DUK_TVAL_GET_HEAPHDR(tv); + duk_push_pointer(ctx, (void *) h); + } else { + /* internal type tag */ + duk_push_int(ctx, (duk_int_t) DUK_TVAL_GET_TAG(tv)); + goto done; + } + DUK_ASSERT(h != NULL); + + /* refcount */ +#ifdef DUK_USE_REFERENCE_COUNTING + duk_push_size_t(ctx, DUK_HEAPHDR_GET_REFCOUNT(h)); +#else + duk_push_undefined(ctx); +#endif + + /* heaphdr size and additional allocation size, followed by + * type specific stuff (with varying value count) + */ + switch ((duk_small_int_t) DUK_HEAPHDR_GET_TYPE(h)) { + case DUK_HTYPE_STRING: { + duk_hstring *h_str = (duk_hstring *) h; + duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hstring) + DUK_HSTRING_GET_BYTELEN(h_str) + 1)); + break; + } + case DUK_HTYPE_OBJECT: { + duk_hobject *h_obj = (duk_hobject *) h; + duk_small_uint_t hdr_size; + if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h_obj)) { + hdr_size = (duk_small_uint_t) sizeof(duk_hcompiledfunction); + } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(h_obj)) { + hdr_size = (duk_small_uint_t) sizeof(duk_hnativefunction); + } else if (DUK_HOBJECT_IS_THREAD(h_obj)) { + hdr_size = (duk_small_uint_t) sizeof(duk_hthread); +#if defined(DUK_USE_BUFFEROBJECT_SUPPORT) + } else if (DUK_HOBJECT_IS_BUFFEROBJECT(h_obj)) { + hdr_size = (duk_small_uint_t) sizeof(duk_hbufferobject); +#endif + } else { + hdr_size = (duk_small_uint_t) sizeof(duk_hobject); + } + duk_push_uint(ctx, (duk_uint_t) hdr_size); + duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_P_ALLOC_SIZE(h_obj)); + duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_ESIZE(h_obj)); + /* Note: e_next indicates the number of gc-reachable entries + * in the entry part, and also indicates the index where the + * next new property would be inserted. It does *not* indicate + * the number of non-NULL keys present in the object. That + * value could be counted separately but requires a pass through + * the key list. + */ + duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_ENEXT(h_obj)); + duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_ASIZE(h_obj)); + duk_push_uint(ctx, (duk_uint_t) DUK_HOBJECT_GET_HSIZE(h_obj)); + if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h_obj)) { + duk_hbuffer *h_data = (duk_hbuffer *) DUK_HCOMPILEDFUNCTION_GET_DATA(thr->heap, (duk_hcompiledfunction *) h_obj); + if (h_data) { + duk_push_uint(ctx, (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_data)); + } else { + duk_push_uint(ctx, 0); + } + } + break; + } + case DUK_HTYPE_BUFFER: { + duk_hbuffer *h_buf = (duk_hbuffer *) h; + if (DUK_HBUFFER_HAS_DYNAMIC(h_buf)) { + if (DUK_HBUFFER_HAS_EXTERNAL(h_buf)) { + duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hbuffer_external))); + } else { + /* When alloc_size == 0 the second allocation may not + * actually exist. + */ + duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hbuffer_dynamic))); + } + duk_push_uint(ctx, (duk_uint_t) (DUK_HBUFFER_GET_SIZE(h_buf))); + } else { + duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hbuffer_fixed) + DUK_HBUFFER_GET_SIZE(h_buf) + 1)); + } + break; + + } + } + + done: + /* set values into ret array */ + /* XXX: primitive to make array from valstack slice */ + n = duk_get_top(ctx); + for (i = 2; i < n; i++) { + duk_dup(ctx, i); + duk_put_prop_index(ctx, 1, i - 2); + } + duk_dup(ctx, 1); + return 1; +} + +DUK_INTERNAL duk_ret_t duk_bi_duktape_object_act(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_activation *act; + duk_uint_fast32_t pc; + duk_uint_fast32_t line; + duk_int_t level; + + /* -1 = top callstack entry, callstack[callstack_top - 1] + * -callstack_top = bottom callstack entry, callstack[0] + */ + level = duk_to_int(ctx, 0); + if (level >= 0 || -level > (duk_int_t) thr->callstack_top) { + return 0; + } + DUK_ASSERT(level >= -((duk_int_t) thr->callstack_top) && level <= -1); + act = thr->callstack + thr->callstack_top + level; + + duk_push_object(ctx); + + duk_push_tval(ctx, &act->tv_func); + + /* Relevant PC is just before current one because PC is + * post-incremented. This should match what error augment + * code does. + */ + pc = duk_hthread_get_act_prev_pc(thr, act); + duk_push_uint(ctx, (duk_uint_t) pc); + +#if defined(DUK_USE_PC2LINE) + line = duk_hobject_pc2line_query(ctx, -2, pc); +#else + line = 0; +#endif + duk_push_uint(ctx, (duk_uint_t) line); + + /* Providing access to e.g. act->lex_env would be dangerous: these + * internal structures must never be accessible to the application. + * Duktape relies on them having consistent data, and this consistency + * is only asserted for, not checked for. + */ + + /* [ level obj func pc line ] */ + + /* XXX: version specific array format instead? */ + duk_xdef_prop_stridx_wec(ctx, -4, DUK_STRIDX_LINE_NUMBER); + duk_xdef_prop_stridx_wec(ctx, -3, DUK_STRIDX_PC); + duk_xdef_prop_stridx_wec(ctx, -2, DUK_STRIDX_LC_FUNCTION); + return 1; +} + +DUK_INTERNAL duk_ret_t duk_bi_duktape_object_gc(duk_context *ctx) { +#ifdef DUK_USE_MARK_AND_SWEEP + duk_hthread *thr = (duk_hthread *) ctx; + duk_small_uint_t flags; + duk_bool_t rc; + + flags = (duk_small_uint_t) duk_get_uint(ctx, 0); + rc = duk_heap_mark_and_sweep(thr->heap, flags); + + /* XXX: Not sure what the best return value would be in the API. + * Return a boolean for now. Note that rc == 0 is success (true). + */ + duk_push_boolean(ctx, !rc); + return 1; +#else + DUK_UNREF(ctx); + return 0; +#endif +} + +DUK_INTERNAL duk_ret_t duk_bi_duktape_object_fin(duk_context *ctx) { + (void) duk_require_hobject(ctx, 0); + if (duk_get_top(ctx) >= 2) { + /* Set: currently a finalizer is disabled by setting it to + * undefined; this does not remove the property at the moment. + * The value could be type checked to be either a function + * or something else; if something else, the property could + * be deleted. + */ + duk_set_top(ctx, 2); + (void) duk_put_prop_stridx(ctx, 0, DUK_STRIDX_INT_FINALIZER); + return 0; + } else { + /* Get. */ + DUK_ASSERT(duk_get_top(ctx) == 1); + duk_get_prop_stridx(ctx, 0, DUK_STRIDX_INT_FINALIZER); + return 1; + } +} + +DUK_INTERNAL duk_ret_t duk_bi_duktape_object_enc(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hstring *h_str; + + DUK_UNREF(thr); + + /* Vararg function: must be careful to check/require arguments. + * The JSON helpers accept invalid indices and treat them like + * non-existent optional parameters. + */ + + h_str = duk_require_hstring(ctx, 0); + duk_require_valid_index(ctx, 1); + + if (h_str == DUK_HTHREAD_STRING_HEX(thr)) { + duk_set_top(ctx, 2); + duk_hex_encode(ctx, 1); + DUK_ASSERT_TOP(ctx, 2); + } else if (h_str == DUK_HTHREAD_STRING_BASE64(thr)) { + duk_set_top(ctx, 2); + duk_base64_encode(ctx, 1); + DUK_ASSERT_TOP(ctx, 2); +#ifdef DUK_USE_JX + } else if (h_str == DUK_HTHREAD_STRING_JX(thr)) { + duk_bi_json_stringify_helper(ctx, + 1 /*idx_value*/, + 2 /*idx_replacer*/, + 3 /*idx_space*/, + DUK_JSON_FLAG_EXT_CUSTOM | + DUK_JSON_FLAG_ASCII_ONLY | + DUK_JSON_FLAG_AVOID_KEY_QUOTES /*flags*/); +#endif +#ifdef DUK_USE_JC + } else if (h_str == DUK_HTHREAD_STRING_JC(thr)) { + duk_bi_json_stringify_helper(ctx, + 1 /*idx_value*/, + 2 /*idx_replacer*/, + 3 /*idx_space*/, + DUK_JSON_FLAG_EXT_COMPATIBLE | + DUK_JSON_FLAG_ASCII_ONLY /*flags*/); +#endif + } else { + return DUK_RET_TYPE_ERROR; + } + return 1; +} + +DUK_INTERNAL duk_ret_t duk_bi_duktape_object_dec(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hstring *h_str; + + DUK_UNREF(thr); + + /* Vararg function: must be careful to check/require arguments. + * The JSON helpers accept invalid indices and treat them like + * non-existent optional parameters. + */ + + h_str = duk_require_hstring(ctx, 0); + duk_require_valid_index(ctx, 1); + + if (h_str == DUK_HTHREAD_STRING_HEX(thr)) { + duk_set_top(ctx, 2); + duk_hex_decode(ctx, 1); + DUK_ASSERT_TOP(ctx, 2); + } else if (h_str == DUK_HTHREAD_STRING_BASE64(thr)) { + duk_set_top(ctx, 2); + duk_base64_decode(ctx, 1); + DUK_ASSERT_TOP(ctx, 2); +#ifdef DUK_USE_JX + } else if (h_str == DUK_HTHREAD_STRING_JX(thr)) { + duk_bi_json_parse_helper(ctx, + 1 /*idx_value*/, + 2 /*idx_replacer*/, + DUK_JSON_FLAG_EXT_CUSTOM /*flags*/); +#endif +#ifdef DUK_USE_JC + } else if (h_str == DUK_HTHREAD_STRING_JC(thr)) { + duk_bi_json_parse_helper(ctx, + 1 /*idx_value*/, + 2 /*idx_replacer*/, + DUK_JSON_FLAG_EXT_COMPATIBLE /*flags*/); +#endif + } else { + return DUK_RET_TYPE_ERROR; + } + return 1; +} + +/* + * Compact an object + */ + +DUK_INTERNAL duk_ret_t duk_bi_duktape_object_compact(duk_context *ctx) { + DUK_ASSERT_TOP(ctx, 1); + duk_compact(ctx, 0); + return 1; /* return the argument object */ +} +/* + * Error built-ins + */ + +/* include removed: duk_internal.h */ + +DUK_INTERNAL duk_ret_t duk_bi_error_constructor_shared(duk_context *ctx) { + /* Behavior for constructor and non-constructor call is + * the same except for augmenting the created error. When + * called as a constructor, the caller (duk_new()) will handle + * augmentation; when called as normal function, we need to do + * it here. + */ + + duk_hthread *thr = (duk_hthread *) ctx; + duk_small_int_t bidx_prototype = duk_get_current_magic(ctx); + + /* same for both error and each subclass like TypeError */ + duk_uint_t flags_and_class = DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ERROR); + + DUK_UNREF(thr); + + duk_push_object_helper(ctx, flags_and_class, bidx_prototype); + + /* If message is undefined, the own property 'message' is not set at + * all to save property space. An empty message is inherited anyway. + */ + if (!duk_is_undefined(ctx, 0)) { + duk_to_string(ctx, 0); + duk_dup(ctx, 0); /* [ message error message ] */ + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC); + } + + /* Augment the error if called as a normal function. __FILE__ and __LINE__ + * are not desirable in this case. + */ + +#ifdef DUK_USE_AUGMENT_ERROR_CREATE + if (!duk_is_constructor_call(ctx)) { + duk_err_augment_error_create(thr, thr, NULL, 0, 1 /*noblame_fileline*/); + } +#endif + + return 1; +} + +DUK_INTERNAL duk_ret_t duk_bi_error_prototype_to_string(duk_context *ctx) { + /* XXX: optimize with more direct internal access */ + + duk_push_this(ctx); + (void) duk_require_hobject_or_lfunc_coerce(ctx, -1); + + /* [ ... this ] */ + + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME); + if (duk_is_undefined(ctx, -1)) { + duk_pop(ctx); + duk_push_string(ctx, "Error"); + } else { + duk_to_string(ctx, -1); + } + + /* [ ... this name ] */ + + /* XXX: Are steps 6 and 7 in E5 Section 15.11.4.4 duplicated by + * accident or are they actually needed? The first ToString() + * could conceivably return 'undefined'. + */ + duk_get_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE); + if (duk_is_undefined(ctx, -1)) { + duk_pop(ctx); + duk_push_string(ctx, ""); + } else { + duk_to_string(ctx, -1); + } + + /* [ ... this name message ] */ + + if (duk_get_length(ctx, -2) == 0) { + /* name is empty -> return message */ + return 1; + } + if (duk_get_length(ctx, -1) == 0) { + /* message is empty -> return name */ + duk_pop(ctx); + return 1; + } + duk_push_string(ctx, ": "); + duk_insert(ctx, -2); /* ... name ': ' message */ + duk_concat(ctx, 3); + + return 1; +} + +#if defined(DUK_USE_TRACEBACKS) + +/* + * Traceback handling + * + * The unified helper decodes the traceback and produces various requested + * outputs. It should be optimized for size, and may leave garbage on stack, + * only the topmost return value matters. For instance, traceback separator + * and decoded strings are pushed even when looking for filename only. + * + * NOTE: although _Tracedata is an internal property, user code can currently + * write to the array (or replace it with something other than an array). + * The code below must tolerate arbitrary _Tracedata. It can throw errors + * etc, but cannot cause a segfault or memory unsafe behavior. + */ + +/* constants arbitrary, chosen for small loads */ +#define DUK__OUTPUT_TYPE_TRACEBACK (-1) +#define DUK__OUTPUT_TYPE_FILENAME 0 +#define DUK__OUTPUT_TYPE_LINENUMBER 1 + +DUK_LOCAL duk_ret_t duk__error_getter_helper(duk_context *ctx, duk_small_int_t output_type) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_idx_t idx_td; + duk_small_int_t i; /* traceback depth fits into 16 bits */ + duk_small_int_t t; /* stack type fits into 16 bits */ + duk_small_int_t count_func = 0; /* traceback depth ensures fits into 16 bits */ + const char *str_tailcall = " tailcall"; + const char *str_strict = " strict"; + const char *str_construct = " construct"; + const char *str_prevyield = " preventsyield"; + const char *str_directeval = " directeval"; + const char *str_empty = ""; + + DUK_ASSERT_TOP(ctx, 0); /* fixed arg count */ + DUK_UNREF(thr); + + duk_push_this(ctx); + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TRACEDATA); + idx_td = duk_get_top_index(ctx); + + duk_push_hstring_stridx(ctx, DUK_STRIDX_NEWLINE_4SPACE); + duk_push_this(ctx); + + /* [ ... this tracedata sep this ] */ + + /* XXX: skip null filename? */ + + if (duk_check_type(ctx, idx_td, DUK_TYPE_OBJECT)) { + /* Current tracedata contains 2 entries per callstack entry. */ + for (i = 0; ; i += 2) { + duk_int_t pc; + duk_int_t line; + duk_int_t flags; + duk_double_t d; + const char *funcname; + const char *filename; + duk_hobject *h_func; + duk_hstring *h_name; + + duk_require_stack(ctx, 5); + duk_get_prop_index(ctx, idx_td, i); + duk_get_prop_index(ctx, idx_td, i + 1); + d = duk_to_number(ctx, -1); + pc = (duk_int_t) DUK_FMOD(d, DUK_DOUBLE_2TO32); + flags = (duk_int_t) DUK_FLOOR(d / DUK_DOUBLE_2TO32); + t = (duk_small_int_t) duk_get_type(ctx, -2); + + if (t == DUK_TYPE_OBJECT || t == DUK_TYPE_LIGHTFUNC) { + /* + * Ecmascript/native function call or lightfunc call + */ + + count_func++; + + /* [ ... v1(func) v2(pc+flags) ] */ + + h_func = duk_get_hobject(ctx, -2); /* NULL for lightfunc */ + + duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME); + duk_get_prop_stridx(ctx, -3, DUK_STRIDX_FILE_NAME); + +#if defined(DUK_USE_PC2LINE) + line = duk_hobject_pc2line_query(ctx, -4, (duk_uint_fast32_t) pc); +#else + line = 0; +#endif + + /* [ ... v1 v2 name filename ] */ + + /* When looking for .fileName/.lineNumber, blame first + * function which has a .fileName. + */ + if (duk_is_string(ctx, -1)) { + if (output_type == DUK__OUTPUT_TYPE_FILENAME) { + return 1; + } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) { + duk_push_int(ctx, line); + return 1; + } + } + + /* XXX: Change 'anon' handling here too, to use empty string for anonymous functions? */ + /* XXX: Could be improved by coercing to a readable duk_tval (especially string escaping) */ + h_name = duk_get_hstring(ctx, -2); /* may be NULL */ + funcname = (h_name == NULL || h_name == DUK_HTHREAD_STRING_EMPTY_STRING(thr)) ? + "[anon]" : (const char *) DUK_HSTRING_GET_DATA(h_name); + filename = duk_get_string(ctx, -1); + filename = filename ? filename : ""; + DUK_ASSERT(funcname != NULL); + DUK_ASSERT(filename != NULL); + + if (h_func == NULL) { + duk_push_sprintf(ctx, "at %s light%s%s%s%s%s", + (const char *) funcname, + (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty), + (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcall : str_empty), + (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty), + (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty), + (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty)); + } else if (DUK_HOBJECT_HAS_NATIVEFUNCTION(h_func)) { + duk_push_sprintf(ctx, "at %s (%s) native%s%s%s%s%s", + (const char *) funcname, + (const char *) filename, + (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty), + (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcall : str_empty), + (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty), + (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty), + (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty)); + } else { + duk_push_sprintf(ctx, "at %s (%s:%ld)%s%s%s%s%s", + (const char *) funcname, + (const char *) filename, + (long) line, + (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty), + (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcall : str_empty), + (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty), + (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty), + (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty)); + } + duk_replace(ctx, -5); /* [ ... v1 v2 name filename str ] -> [ ... str v2 name filename ] */ + duk_pop_n(ctx, 3); /* -> [ ... str ] */ + } else if (t == DUK_TYPE_STRING) { + /* + * __FILE__ / __LINE__ entry, here 'pc' is line number directly. + * Sometimes __FILE__ / __LINE__ is reported as the source for + * the error (fileName, lineNumber), sometimes not. + */ + + /* [ ... v1(filename) v2(line+flags) ] */ + + /* When looking for .fileName/.lineNumber, blame compilation + * or C call site unless flagged not to do so. + */ + if (!(flags & DUK_TB_FLAG_NOBLAME_FILELINE)) { + if (output_type == DUK__OUTPUT_TYPE_FILENAME) { + duk_pop(ctx); + return 1; + } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) { + duk_push_int(ctx, pc); + return 1; + } + } + + duk_push_sprintf(ctx, "at [anon] (%s:%ld) internal", + (const char *) duk_get_string(ctx, -2), (long) pc); + duk_replace(ctx, -3); /* [ ... v1 v2 str ] -> [ ... str v2 ] */ + duk_pop(ctx); /* -> [ ... str ] */ + } else { + /* unknown, ignore */ + duk_pop_2(ctx); + break; + } + } + + if (count_func >= DUK_USE_TRACEBACK_DEPTH) { + /* Possibly truncated; there is no explicit truncation + * marker so this is the best we can do. + */ + + duk_push_hstring_stridx(ctx, DUK_STRIDX_BRACKETED_ELLIPSIS); + } + } + + /* [ ... this tracedata sep this str1 ... strN ] */ + + if (output_type != DUK__OUTPUT_TYPE_TRACEBACK) { + return 0; + } else { + /* The 'this' after 'sep' will get ToString() coerced by + * duk_join() automatically. We don't want to do that + * coercion when providing .fileName or .lineNumber (GH-254). + */ + duk_join(ctx, duk_get_top(ctx) - (idx_td + 2) /*count, not including sep*/); + return 1; + } +} + +/* XXX: Output type could be encoded into native function 'magic' value to + * save space. For setters the stridx could be encoded into 'magic'. + */ + +DUK_INTERNAL duk_ret_t duk_bi_error_prototype_stack_getter(duk_context *ctx) { + return duk__error_getter_helper(ctx, DUK__OUTPUT_TYPE_TRACEBACK); +} + +DUK_INTERNAL duk_ret_t duk_bi_error_prototype_filename_getter(duk_context *ctx) { + return duk__error_getter_helper(ctx, DUK__OUTPUT_TYPE_FILENAME); +} + +DUK_INTERNAL duk_ret_t duk_bi_error_prototype_linenumber_getter(duk_context *ctx) { + return duk__error_getter_helper(ctx, DUK__OUTPUT_TYPE_LINENUMBER); +} + +#undef DUK__OUTPUT_TYPE_TRACEBACK +#undef DUK__OUTPUT_TYPE_FILENAME +#undef DUK__OUTPUT_TYPE_LINENUMBER + +#else /* DUK_USE_TRACEBACKS */ + +/* + * Traceback handling when tracebacks disabled. + * + * The fileName / lineNumber stubs are now necessary because built-in + * data will include the accessor properties in Error.prototype. If those + * are removed for builds without tracebacks, these can also be removed. + * 'stack' should still be present and produce a ToString() equivalent: + * this is useful for user code which prints a stacktrace and expects to + * see something useful. A normal stacktrace also begins with a ToString() + * of the error so this makes sense. + */ + +DUK_INTERNAL duk_ret_t duk_bi_error_prototype_stack_getter(duk_context *ctx) { + /* XXX: remove this native function and map 'stack' accessor + * to the toString() implementation directly. + */ + return duk_bi_error_prototype_to_string(ctx); +} + +DUK_INTERNAL duk_ret_t duk_bi_error_prototype_filename_getter(duk_context *ctx) { + DUK_UNREF(ctx); + return 0; +} + +DUK_INTERNAL duk_ret_t duk_bi_error_prototype_linenumber_getter(duk_context *ctx) { + DUK_UNREF(ctx); + return 0; +} + +#endif /* DUK_USE_TRACEBACKS */ + +DUK_LOCAL duk_ret_t duk__error_setter_helper(duk_context *ctx, duk_small_uint_t stridx_key) { + /* Attempt to write 'stack', 'fileName', 'lineNumber' works as if + * user code called Object.defineProperty() to create an overriding + * own property. This allows user code to overwrite .fileName etc + * intuitively as e.g. "err.fileName = 'dummy'" as one might expect. + * See https://github.com/svaarala/duktape/issues/387. + */ + + DUK_ASSERT_TOP(ctx, 1); /* fixed arg count: value */ + + duk_push_this(ctx); + duk_push_hstring_stridx(ctx, (duk_small_int_t) stridx_key); + duk_dup(ctx, 0); + + /* [ ... obj key value ] */ + + DUK_DD(DUK_DDPRINT("error setter: %!T %!T %!T", + duk_get_tval(ctx, -3), duk_get_tval(ctx, -2), duk_get_tval(ctx, -1))); + + duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | + DUK_DEFPROP_HAVE_WRITABLE | DUK_DEFPROP_WRITABLE | + DUK_DEFPROP_HAVE_ENUMERABLE | /*not enumerable*/ + DUK_DEFPROP_HAVE_CONFIGURABLE | DUK_DEFPROP_CONFIGURABLE); + return 0; +} + +DUK_INTERNAL duk_ret_t duk_bi_error_prototype_stack_setter(duk_context *ctx) { + return duk__error_setter_helper(ctx, DUK_STRIDX_STACK); +} + +DUK_INTERNAL duk_ret_t duk_bi_error_prototype_filename_setter(duk_context *ctx) { + return duk__error_setter_helper(ctx, DUK_STRIDX_FILE_NAME); +} + +DUK_INTERNAL duk_ret_t duk_bi_error_prototype_linenumber_setter(duk_context *ctx) { + return duk__error_setter_helper(ctx, DUK_STRIDX_LINE_NUMBER); +} +/* + * Function built-ins + */ + +/* include removed: duk_internal.h */ + +DUK_INTERNAL duk_ret_t duk_bi_function_constructor(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hstring *h_sourcecode; + duk_idx_t nargs; + duk_idx_t i; + duk_small_uint_t comp_flags; + duk_hcompiledfunction *func; + duk_hobject *outer_lex_env; + duk_hobject *outer_var_env; + + /* normal and constructor calls have identical semantics */ + + nargs = duk_get_top(ctx); + for (i = 0; i < nargs; i++) { + duk_to_string(ctx, i); + } + + if (nargs == 0) { + duk_push_string(ctx, ""); + duk_push_string(ctx, ""); + } else if (nargs == 1) { + /* XXX: cover this with the generic >1 case? */ + duk_push_string(ctx, ""); + } else { + duk_insert(ctx, 0); /* [ arg1 ... argN-1 body] -> [body arg1 ... argN-1] */ + duk_push_string(ctx, ","); + duk_insert(ctx, 1); + duk_join(ctx, nargs - 1); + } + + /* [ body formals ], formals is comma separated list that needs to be parsed */ + + DUK_ASSERT_TOP(ctx, 2); + + /* XXX: this placeholder is not always correct, but use for now. + * It will fail in corner cases; see test-dev-func-cons-args.js. + */ + duk_push_string(ctx, "function("); + duk_dup(ctx, 1); + duk_push_string(ctx, "){"); + duk_dup(ctx, 0); + duk_push_string(ctx, "}"); + duk_concat(ctx, 5); + + /* [ body formals source ] */ + + DUK_ASSERT_TOP(ctx, 3); + + /* strictness is not inherited, intentional */ + comp_flags = DUK_JS_COMPILE_FLAG_FUNCEXPR; + + duk_push_hstring_stridx(ctx, DUK_STRIDX_COMPILE); /* XXX: copy from caller? */ /* XXX: ignored now */ + h_sourcecode = duk_require_hstring(ctx, -2); + duk_js_compile(thr, + (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_sourcecode), + (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_sourcecode), + comp_flags); + func = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1); + DUK_ASSERT(func != NULL); + DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) func)); + + /* [ body formals source template ] */ + + /* only outer_lex_env matters, as functions always get a new + * variable declaration environment. + */ + + outer_lex_env = thr->builtins[DUK_BIDX_GLOBAL_ENV]; + outer_var_env = thr->builtins[DUK_BIDX_GLOBAL_ENV]; + + duk_js_push_closure(thr, func, outer_var_env, outer_lex_env, 1 /*add_auto_proto*/); + + /* [ body formals source template closure ] */ + + return 1; +} + +DUK_INTERNAL duk_ret_t duk_bi_function_prototype(duk_context *ctx) { + /* ignore arguments, return undefined (E5 Section 15.3.4) */ + DUK_UNREF(ctx); + return 0; +} + +DUK_INTERNAL duk_ret_t duk_bi_function_prototype_to_string(duk_context *ctx) { + duk_tval *tv; + + /* + * E5 Section 15.3.4.2 places few requirements on the output of + * this function: + * + * - The result is an implementation dependent representation + * of the function; in particular + * + * - The result must follow the syntax of a FunctionDeclaration. + * In particular, the function must have a name (even in the + * case of an anonymous function or a function with an empty + * name). + * + * - Note in particular that the output does NOT need to compile + * into anything useful. + */ + + + /* XXX: faster internal way to get this */ + duk_push_this(ctx); + tv = duk_get_tval(ctx, -1); + DUK_ASSERT(tv != NULL); + + if (DUK_TVAL_IS_OBJECT(tv)) { + duk_hobject *obj = DUK_TVAL_GET_OBJECT(tv); + const char *func_name; + + /* Function name: missing/undefined is mapped to empty string, + * otherwise coerce to string. + */ + /* XXX: currently no handling for non-allowed identifier characters, + * e.g. a '{' in the function name. + */ + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME); + if (duk_is_undefined(ctx, -1)) { + func_name = ""; + } else { + func_name = duk_to_string(ctx, -1); + DUK_ASSERT(func_name != NULL); + } + + /* Indicate function type in the function body using a dummy + * directive. + */ + if (DUK_HOBJECT_HAS_COMPILEDFUNCTION(obj)) { + duk_push_sprintf(ctx, "function %s() {\"ecmascript\"}", (const char *) func_name); + } else if (DUK_HOBJECT_HAS_NATIVEFUNCTION(obj)) { + duk_push_sprintf(ctx, "function %s() {\"native\"}", (const char *) func_name); + } else if (DUK_HOBJECT_HAS_BOUND(obj)) { + duk_push_sprintf(ctx, "function %s() {\"bound\"}", (const char *) func_name); + } else { + goto type_error; + } + } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) { + duk_push_lightfunc_tostring(ctx, tv); + } else { + goto type_error; + } + + return 1; + + type_error: + return DUK_RET_TYPE_ERROR; +} + +DUK_INTERNAL duk_ret_t duk_bi_function_prototype_apply(duk_context *ctx) { + duk_idx_t len; + duk_idx_t i; + + DUK_ASSERT_TOP(ctx, 2); /* not a vararg function */ + + duk_push_this(ctx); + if (!duk_is_callable(ctx, -1)) { + DUK_DDD(DUK_DDDPRINT("func is not callable")); + goto type_error; + } + duk_insert(ctx, 0); + DUK_ASSERT_TOP(ctx, 3); + + DUK_DDD(DUK_DDDPRINT("func=%!iT, thisArg=%!iT, argArray=%!iT", + (duk_tval *) duk_get_tval(ctx, 0), + (duk_tval *) duk_get_tval(ctx, 1), + (duk_tval *) duk_get_tval(ctx, 2))); + + /* [ func thisArg argArray ] */ + + if (duk_is_null_or_undefined(ctx, 2)) { + DUK_DDD(DUK_DDDPRINT("argArray is null/undefined, no args")); + len = 0; + } else if (!duk_is_object(ctx, 2)) { + goto type_error; + } else { + DUK_DDD(DUK_DDDPRINT("argArray is an object")); + + /* XXX: make this an internal helper */ + duk_get_prop_stridx(ctx, 2, DUK_STRIDX_LENGTH); + len = (duk_idx_t) duk_to_uint32(ctx, -1); /* ToUint32() coercion required */ + duk_pop(ctx); + + duk_require_stack(ctx, len); + + DUK_DDD(DUK_DDDPRINT("argArray length is %ld", (long) len)); + for (i = 0; i < len; i++) { + duk_get_prop_index(ctx, 2, i); + } + } + duk_remove(ctx, 2); + DUK_ASSERT_TOP(ctx, 2 + len); + + /* [ func thisArg arg1 ... argN ] */ + + DUK_DDD(DUK_DDDPRINT("apply, func=%!iT, thisArg=%!iT, len=%ld", + (duk_tval *) duk_get_tval(ctx, 0), + (duk_tval *) duk_get_tval(ctx, 1), + (long) len)); + duk_call_method(ctx, len); + return 1; + + type_error: + return DUK_RET_TYPE_ERROR; +} + +DUK_INTERNAL duk_ret_t duk_bi_function_prototype_call(duk_context *ctx) { + duk_idx_t nargs; + + /* Step 1 is not necessary because duk_call_method() will take + * care of it. + */ + + /* vararg function, thisArg needs special handling */ + nargs = duk_get_top(ctx); /* = 1 + arg count */ + if (nargs == 0) { + duk_push_undefined(ctx); + nargs++; + } + DUK_ASSERT(nargs >= 1); + + /* [ thisArg arg1 ... argN ] */ + + duk_push_this(ctx); /* 'func' in the algorithm */ + duk_insert(ctx, 0); + + /* [ func thisArg arg1 ... argN ] */ + + DUK_DDD(DUK_DDDPRINT("func=%!iT, thisArg=%!iT, argcount=%ld, top=%ld", + (duk_tval *) duk_get_tval(ctx, 0), + (duk_tval *) duk_get_tval(ctx, 1), + (long) (nargs - 1), + (long) duk_get_top(ctx))); + duk_call_method(ctx, nargs - 1); + return 1; +} + +/* XXX: the implementation now assumes "chained" bound functions, + * whereas "collapsed" bound functions (where there is ever only + * one bound function which directly points to a non-bound, final + * function) would require a "collapsing" implementation which + * merges argument lists etc here. + */ +DUK_INTERNAL duk_ret_t duk_bi_function_prototype_bind(duk_context *ctx) { + duk_hobject *h_bound; + duk_hobject *h_target; + duk_idx_t nargs; + duk_idx_t i; + + /* vararg function, careful arg handling (e.g. thisArg may not be present) */ + nargs = duk_get_top(ctx); /* = 1 + arg count */ + if (nargs == 0) { + duk_push_undefined(ctx); + nargs++; + } + DUK_ASSERT(nargs >= 1); + + duk_push_this(ctx); + if (!duk_is_callable(ctx, -1)) { + DUK_DDD(DUK_DDDPRINT("func is not callable")); + goto type_error; + } + + /* [ thisArg arg1 ... argN func ] (thisArg+args == nargs total) */ + DUK_ASSERT_TOP(ctx, nargs + 1); + + /* create bound function object */ + duk_push_object_helper(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_FLAG_BOUND | + DUK_HOBJECT_FLAG_CONSTRUCTABLE | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION), + DUK_BIDX_FUNCTION_PROTOTYPE); + h_bound = duk_get_hobject(ctx, -1); + DUK_ASSERT(h_bound != NULL); + + /* [ thisArg arg1 ... argN func boundFunc ] */ + duk_dup(ctx, -2); /* func */ + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_TARGET, DUK_PROPDESC_FLAGS_NONE); + + duk_dup(ctx, 0); /* thisArg */ + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_THIS, DUK_PROPDESC_FLAGS_NONE); + + duk_push_array(ctx); + + /* [ thisArg arg1 ... argN func boundFunc argArray ] */ + + for (i = 0; i < nargs - 1; i++) { + duk_dup(ctx, 1 + i); + duk_put_prop_index(ctx, -2, i); + } + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_ARGS, DUK_PROPDESC_FLAGS_NONE); + + /* [ thisArg arg1 ... argN func boundFunc ] */ + + /* bound function 'length' property is interesting */ + h_target = duk_get_hobject(ctx, -2); + if (h_target == NULL || /* lightfunc */ + DUK_HOBJECT_GET_CLASS_NUMBER(h_target) == DUK_HOBJECT_CLASS_FUNCTION) { + /* For lightfuncs, simply read the virtual property. */ + duk_int_t tmp; + duk_get_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH); + tmp = duk_to_int(ctx, -1) - (nargs - 1); /* step 15.a */ + duk_pop(ctx); + duk_push_int(ctx, (tmp < 0 ? 0 : tmp)); + } else { + duk_push_int(ctx, 0); + } + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE); /* attrs in E5 Section 15.3.5.1 */ + + /* caller and arguments must use the same thrower, [[ThrowTypeError]] */ + duk_xdef_prop_stridx_thrower(ctx, -1, DUK_STRIDX_CALLER, DUK_PROPDESC_FLAGS_NONE); + duk_xdef_prop_stridx_thrower(ctx, -1, DUK_STRIDX_LC_ARGUMENTS, DUK_PROPDESC_FLAGS_NONE); + + /* these non-standard properties are copied for convenience */ + /* XXX: 'copy properties' API call? */ + duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_WC); + duk_get_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME); + duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_WC); + + /* The 'strict' flag is copied to get the special [[Get]] of E5.1 + * Section 15.3.5.4 to apply when a 'caller' value is a strict bound + * function. Not sure if this is correct, because the specification + * is a bit ambiguous on this point but it would make sense. + */ + if (h_target == NULL) { + /* Lightfuncs are always strict. */ + DUK_HOBJECT_SET_STRICT(h_bound); + } else if (DUK_HOBJECT_HAS_STRICT(h_target)) { + DUK_HOBJECT_SET_STRICT(h_bound); + } + DUK_DDD(DUK_DDDPRINT("created bound function: %!iT", (duk_tval *) duk_get_tval(ctx, -1))); + + return 1; + + type_error: + return DUK_RET_TYPE_ERROR; +} +/* + * Global object built-ins + */ + +/* include removed: duk_internal.h */ + +/* + * Encoding/decoding helpers + */ + +/* XXX: Could add fast path (for each transform callback) with direct byte + * lookups (no shifting) and no explicit check for x < 0x80 before table + * lookup. + */ + +/* Macros for creating and checking bitmasks for character encoding. + * Bit number is a bit counterintuitive, but minimizes code size. + */ +#define DUK__MKBITS(a,b,c,d,e,f,g,h) ((duk_uint8_t) ( \ + ((a) << 0) | ((b) << 1) | ((c) << 2) | ((d) << 3) | \ + ((e) << 4) | ((f) << 5) | ((g) << 6) | ((h) << 7) \ + )) +#define DUK__CHECK_BITMASK(table,cp) ((table)[(cp) >> 3] & (1 << ((cp) & 0x07))) + +/* E5.1 Section 15.1.3.3: uriReserved + uriUnescaped + '#' */ +DUK_LOCAL const duk_uint8_t duk__encode_uriunescaped_table[16] = { + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */ + DUK__MKBITS(0, 1, 0, 1, 1, 0, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x20-0x2f */ + DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 0, 1, 0, 1), /* 0x30-0x3f */ + DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x40-0x4f */ + DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 1), /* 0x50-0x5f */ + DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x60-0x6f */ + DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 1, 0), /* 0x70-0x7f */ +}; + +/* E5.1 Section 15.1.3.4: uriUnescaped */ +DUK_LOCAL const duk_uint8_t duk__encode_uricomponent_unescaped_table[16] = { + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */ + DUK__MKBITS(0, 1, 0, 0, 0, 0, 0, 1), DUK__MKBITS(1, 1, 1, 0, 0, 1, 1, 0), /* 0x20-0x2f */ + DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 0, 0, 0, 0, 0, 0), /* 0x30-0x3f */ + DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x40-0x4f */ + DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 1), /* 0x50-0x5f */ + DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x60-0x6f */ + DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 1, 0), /* 0x70-0x7f */ +}; + +/* E5.1 Section 15.1.3.1: uriReserved + '#' */ +DUK_LOCAL const duk_uint8_t duk__decode_uri_reserved_table[16] = { + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */ + DUK__MKBITS(0, 0, 0, 1, 1, 0, 1, 0), DUK__MKBITS(0, 0, 0, 1, 1, 0, 0, 1), /* 0x20-0x2f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 1, 1, 0, 1, 0, 1), /* 0x30-0x3f */ + DUK__MKBITS(1, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x40-0x4f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x50-0x5f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x60-0x6f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x70-0x7f */ +}; + +/* E5.1 Section 15.1.3.2: empty */ +DUK_LOCAL const duk_uint8_t duk__decode_uri_component_reserved_table[16] = { + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x20-0x2f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x30-0x3f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x40-0x4f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x50-0x5f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x60-0x6f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x70-0x7f */ +}; + +#ifdef DUK_USE_SECTION_B +/* E5.1 Section B.2.2, step 7. */ +DUK_LOCAL const duk_uint8_t duk__escape_unescaped_table[16] = { + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */ + DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 1, 1, 0, 1, 1, 1), /* 0x20-0x2f */ + DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 0, 0, 0, 0, 0, 0), /* 0x30-0x3f */ + DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x40-0x4f */ + DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 1), /* 0x50-0x5f */ + DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x60-0x6f */ + DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 0) /* 0x70-0x7f */ +}; +#endif /* DUK_USE_SECTION_B */ + +#undef DUK__MKBITS + +typedef struct { + duk_hthread *thr; + duk_hstring *h_str; + duk_bufwriter_ctx bw; + const duk_uint8_t *p; + const duk_uint8_t *p_start; + const duk_uint8_t *p_end; +} duk__transform_context; + +typedef void (*duk__transform_callback)(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp); + +/* XXX: refactor and share with other code */ +DUK_LOCAL duk_small_int_t duk__decode_hex_escape(const duk_uint8_t *p, duk_small_int_t n) { + duk_small_int_t ch; + duk_small_int_t t = 0; + + while (n > 0) { + t = t * 16; + ch = (duk_small_int_t) duk_hex_dectab[*p++]; + if (DUK_LIKELY(ch >= 0)) { + t += ch; + } else { + return -1; + } + n--; + } + return t; +} + +DUK_LOCAL int duk__transform_helper(duk_context *ctx, duk__transform_callback callback, const void *udata) { + duk_hthread *thr = (duk_hthread *) ctx; + duk__transform_context tfm_ctx_alloc; + duk__transform_context *tfm_ctx = &tfm_ctx_alloc; + duk_codepoint_t cp; + + tfm_ctx->thr = thr; + + tfm_ctx->h_str = duk_to_hstring(ctx, 0); + DUK_ASSERT(tfm_ctx->h_str != NULL); + + DUK_BW_INIT_PUSHBUF(thr, &tfm_ctx->bw, DUK_HSTRING_GET_BYTELEN(tfm_ctx->h_str)); /* initial size guess */ + + tfm_ctx->p_start = DUK_HSTRING_GET_DATA(tfm_ctx->h_str); + tfm_ctx->p_end = tfm_ctx->p_start + DUK_HSTRING_GET_BYTELEN(tfm_ctx->h_str); + tfm_ctx->p = tfm_ctx->p_start; + + while (tfm_ctx->p < tfm_ctx->p_end) { + cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &tfm_ctx->p, tfm_ctx->p_start, tfm_ctx->p_end); + callback(tfm_ctx, udata, cp); + } + + DUK_BW_COMPACT(thr, &tfm_ctx->bw); + + duk_to_string(ctx, -1); + return 1; +} + +DUK_LOCAL void duk__transform_callback_encode_uri(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) { + duk_uint8_t xutf8_buf[DUK_UNICODE_MAX_XUTF8_LENGTH]; + duk_small_int_t len; + duk_codepoint_t cp1, cp2; + duk_small_int_t i, t; + const duk_uint8_t *unescaped_table = (const duk_uint8_t *) udata; + + /* UTF-8 encoded bytes escaped as %xx%xx%xx... -> 3 * nbytes. + * Codepoint range is restricted so this is a slightly too large + * but doesn't matter. + */ + DUK_BW_ENSURE(tfm_ctx->thr, &tfm_ctx->bw, 3 * DUK_UNICODE_MAX_XUTF8_LENGTH); + + if (cp < 0) { + goto uri_error; + } else if ((cp < 0x80L) && DUK__CHECK_BITMASK(unescaped_table, cp)) { + DUK_BW_WRITE_RAW_U8(tfm_ctx->thr, &tfm_ctx->bw, (duk_uint8_t) cp); + return; + } else if (cp >= 0xdc00L && cp <= 0xdfffL) { + goto uri_error; + } else if (cp >= 0xd800L && cp <= 0xdbffL) { + /* Needs lookahead */ + if (duk_unicode_decode_xutf8(tfm_ctx->thr, &tfm_ctx->p, tfm_ctx->p_start, tfm_ctx->p_end, (duk_ucodepoint_t *) &cp2) == 0) { + goto uri_error; + } + if (!(cp2 >= 0xdc00L && cp2 <= 0xdfffL)) { + goto uri_error; + } + cp1 = cp; + cp = ((cp1 - 0xd800L) << 10) + (cp2 - 0xdc00L) + 0x10000L; + } else if (cp > 0x10ffffL) { + /* Although we can allow non-BMP characters (they'll decode + * back into surrogate pairs), we don't allow extended UTF-8 + * characters; they would encode to URIs which won't decode + * back because of strict UTF-8 checks in URI decoding. + * (However, we could just as well allow them here.) + */ + goto uri_error; + } else { + /* Non-BMP characters within valid UTF-8 range: encode as is. + * They'll decode back into surrogate pairs if the escaped + * output is decoded. + */ + ; + } + + len = duk_unicode_encode_xutf8((duk_ucodepoint_t) cp, xutf8_buf); + for (i = 0; i < len; i++) { + t = (int) xutf8_buf[i]; + DUK_BW_WRITE_RAW_U8_3(tfm_ctx->thr, + &tfm_ctx->bw, + DUK_ASC_PERCENT, + (duk_uint8_t) duk_uc_nybbles[t >> 4], + (duk_uint8_t) duk_uc_nybbles[t & 0x0f]); + } + + return; + + uri_error: + DUK_ERROR(tfm_ctx->thr, DUK_ERR_URI_ERROR, "invalid input"); +} + +DUK_LOCAL void duk__transform_callback_decode_uri(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) { + const duk_uint8_t *reserved_table = (const duk_uint8_t *) udata; + duk_small_uint_t utf8_blen; + duk_codepoint_t min_cp; + duk_small_int_t t; /* must be signed */ + duk_small_uint_t i; + + /* Maximum write size: XUTF8 path writes max DUK_UNICODE_MAX_XUTF8_LENGTH, + * percent escape path writes max two times CESU-8 encoded BMP length. + */ + DUK_BW_ENSURE(tfm_ctx->thr, + &tfm_ctx->bw, + (DUK_UNICODE_MAX_XUTF8_LENGTH >= 2 * DUK_UNICODE_MAX_CESU8_BMP_LENGTH ? + DUK_UNICODE_MAX_XUTF8_LENGTH : DUK_UNICODE_MAX_CESU8_BMP_LENGTH)); + + if (cp == (duk_codepoint_t) '%') { + const duk_uint8_t *p = tfm_ctx->p; + duk_size_t left = (duk_size_t) (tfm_ctx->p_end - p); /* bytes left */ + + DUK_DDD(DUK_DDDPRINT("percent encoding, left=%ld", (long) left)); + + if (left < 2) { + goto uri_error; + } + + t = duk__decode_hex_escape(p, 2); + DUK_DDD(DUK_DDDPRINT("first byte: %ld", (long) t)); + if (t < 0) { + goto uri_error; + } + + if (t < 0x80) { + if (DUK__CHECK_BITMASK(reserved_table, t)) { + /* decode '%xx' to '%xx' if decoded char in reserved set */ + DUK_ASSERT(tfm_ctx->p - 1 >= tfm_ctx->p_start); + DUK_BW_WRITE_RAW_U8_3(tfm_ctx->thr, + &tfm_ctx->bw, + DUK_ASC_PERCENT, + p[0], + p[1]); + } else { + DUK_BW_WRITE_RAW_U8(tfm_ctx->thr, &tfm_ctx->bw, (duk_uint8_t) t); + } + tfm_ctx->p += 2; + return; + } + + /* Decode UTF-8 codepoint from a sequence of hex escapes. The + * first byte of the sequence has been decoded to 't'. + * + * Note that UTF-8 validation must be strict according to the + * specification: E5.1 Section 15.1.3, decode algorithm step + * 4.d.vii.8. URIError from non-shortest encodings is also + * specifically noted in the spec. + */ + + DUK_ASSERT(t >= 0x80); + if (t < 0xc0) { + /* continuation byte */ + goto uri_error; + } else if (t < 0xe0) { + /* 110x xxxx; 2 bytes */ + utf8_blen = 2; + min_cp = 0x80L; + cp = t & 0x1f; + } else if (t < 0xf0) { + /* 1110 xxxx; 3 bytes */ + utf8_blen = 3; + min_cp = 0x800L; + cp = t & 0x0f; + } else if (t < 0xf8) { + /* 1111 0xxx; 4 bytes */ + utf8_blen = 4; + min_cp = 0x10000L; + cp = t & 0x07; + } else { + /* extended utf-8 not allowed for URIs */ + goto uri_error; + } + + if (left < utf8_blen * 3 - 1) { + /* '%xx%xx...%xx', p points to char after first '%' */ + goto uri_error; + } + + p += 3; + for (i = 1; i < utf8_blen; i++) { + /* p points to digit part ('%xy', p points to 'x') */ + t = duk__decode_hex_escape(p, 2); + DUK_DDD(DUK_DDDPRINT("i=%ld utf8_blen=%ld cp=%ld t=0x%02lx", + (long) i, (long) utf8_blen, (long) cp, (unsigned long) t)); + if (t < 0) { + goto uri_error; + } + if ((t & 0xc0) != 0x80) { + goto uri_error; + } + cp = (cp << 6) + (t & 0x3f); + p += 3; + } + p--; /* p overshoots */ + tfm_ctx->p = p; + + DUK_DDD(DUK_DDDPRINT("final cp=%ld, min_cp=%ld", (long) cp, (long) min_cp)); + + if (cp < min_cp || cp > 0x10ffffL || (cp >= 0xd800L && cp <= 0xdfffL)) { + goto uri_error; + } + + /* The E5.1 algorithm checks whether or not a decoded codepoint + * is below 0x80 and perhaps may be in the "reserved" set. + * This seems pointless because the single byte UTF-8 case is + * handled separately, and non-shortest encodings are rejected. + * So, 'cp' cannot be below 0x80 here, and thus cannot be in + * the reserved set. + */ + + /* utf-8 validation ensures these */ + DUK_ASSERT(cp >= 0x80L && cp <= 0x10ffffL); + + if (cp >= 0x10000L) { + cp -= 0x10000L; + DUK_ASSERT(cp < 0x100000L); + + DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, ((cp >> 10) + 0xd800L)); + DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, ((cp & 0x03ffUL) + 0xdc00L)); + } else { + DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, cp); + } + } else { + DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, cp); + } + return; + + uri_error: + DUK_ERROR(tfm_ctx->thr, DUK_ERR_URI_ERROR, "invalid input"); +} + +#ifdef DUK_USE_SECTION_B +DUK_LOCAL void duk__transform_callback_escape(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) { + DUK_UNREF(udata); + + DUK_BW_ENSURE(tfm_ctx->thr, &tfm_ctx->bw, 6); + + if (cp < 0) { + goto esc_error; + } else if ((cp < 0x80L) && DUK__CHECK_BITMASK(duk__escape_unescaped_table, cp)) { + DUK_BW_WRITE_RAW_U8(tfm_ctx->thr, &tfm_ctx->bw, (duk_uint8_t) cp); + } else if (cp < 0x100L) { + DUK_BW_WRITE_RAW_U8_3(tfm_ctx->thr, + &tfm_ctx->bw, + (duk_uint8_t) DUK_ASC_PERCENT, + (duk_uint8_t) duk_uc_nybbles[cp >> 4], + (duk_uint8_t) duk_uc_nybbles[cp & 0x0f]); + } else if (cp < 0x10000L) { + DUK_BW_WRITE_RAW_U8_6(tfm_ctx->thr, + &tfm_ctx->bw, + (duk_uint8_t) DUK_ASC_PERCENT, + (duk_uint8_t) DUK_ASC_LC_U, + (duk_uint8_t) duk_uc_nybbles[cp >> 12], + (duk_uint8_t) duk_uc_nybbles[(cp >> 8) & 0x0f], + (duk_uint8_t) duk_uc_nybbles[(cp >> 4) & 0x0f], + (duk_uint8_t) duk_uc_nybbles[cp & 0x0f]); + } else { + /* Characters outside BMP cannot be escape()'d. We could + * encode them as surrogate pairs (for codepoints inside + * valid UTF-8 range, but not extended UTF-8). Because + * escape() and unescape() are legacy functions, we don't. + */ + goto esc_error; + } + + return; + + esc_error: + DUK_ERROR_TYPE(tfm_ctx->thr, "invalid input"); +} + +DUK_LOCAL void duk__transform_callback_unescape(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) { + duk_small_int_t t; + + DUK_UNREF(udata); + + if (cp == (duk_codepoint_t) '%') { + const duk_uint8_t *p = tfm_ctx->p; + duk_size_t left = (duk_size_t) (tfm_ctx->p_end - p); /* bytes left */ + + if (left >= 5 && p[0] == 'u' && + ((t = duk__decode_hex_escape(p + 1, 4)) >= 0)) { + cp = (duk_codepoint_t) t; + tfm_ctx->p += 5; + } else if (left >= 2 && + ((t = duk__decode_hex_escape(p, 2)) >= 0)) { + cp = (duk_codepoint_t) t; + tfm_ctx->p += 2; + } + } + + DUK_BW_WRITE_ENSURE_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, cp); +} +#endif /* DUK_USE_SECTION_B */ + +/* + * Eval + * + * Eval needs to handle both a "direct eval" and an "indirect eval". + * Direct eval handling needs access to the caller's activation so that its + * lexical environment can be accessed. A direct eval is only possible from + * Ecmascript code; an indirect eval call is possible also from C code. + * When an indirect eval call is made from C code, there may not be a + * calling activation at all which needs careful handling. + */ + +DUK_INTERNAL duk_ret_t duk_bi_global_object_eval(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_hstring *h; + duk_activation *act_caller; + duk_activation *act_eval; + duk_activation *act; + duk_hcompiledfunction *func; + duk_hobject *outer_lex_env; + duk_hobject *outer_var_env; + duk_bool_t this_to_global = 1; + duk_small_uint_t comp_flags; + duk_int_t level = -2; + + DUK_ASSERT(duk_get_top(ctx) == 1 || duk_get_top(ctx) == 2); /* 2 when called by debugger */ + DUK_ASSERT(thr->callstack_top >= 1); /* at least this function exists */ + DUK_ASSERT(((thr->callstack + thr->callstack_top - 1)->flags & DUK_ACT_FLAG_DIRECT_EVAL) == 0 || /* indirect eval */ + (thr->callstack_top >= 2)); /* if direct eval, calling activation must exist */ + + /* + * callstack_top - 1 --> this function + * callstack_top - 2 --> caller (may not exist) + * + * If called directly from C, callstack_top might be 1. If calling + * activation doesn't exist, call must be indirect. + */ + + h = duk_get_hstring(ctx, 0); + if (!h) { + return 1; /* return arg as-is */ + } + +#if defined(DUK_USE_DEBUGGER_SUPPORT) + /* NOTE: level is used only by the debugger and should never be present + * for an Ecmascript eval(). + */ + DUK_ASSERT(level == -2); /* by default, use caller's environment */ + if (duk_get_top(ctx) >= 2 && duk_is_number(ctx, 1)) { + level = duk_get_int(ctx, 1); + } + DUK_ASSERT(level <= -2); /* This is guaranteed by debugger code. */ +#endif + + /* [ source ] */ + + comp_flags = DUK_JS_COMPILE_FLAG_EVAL; + act_eval = thr->callstack + thr->callstack_top - 1; /* this function */ + if (thr->callstack_top >= (duk_size_t) -level) { + /* Have a calling activation, check for direct eval (otherwise + * assume indirect eval. + */ + act_caller = thr->callstack + thr->callstack_top + level; /* caller */ + if ((act_caller->flags & DUK_ACT_FLAG_STRICT) && + (act_eval->flags & DUK_ACT_FLAG_DIRECT_EVAL)) { + /* Only direct eval inherits strictness from calling code + * (E5.1 Section 10.1.1). + */ + comp_flags |= DUK_JS_COMPILE_FLAG_STRICT; + } + } else { + DUK_ASSERT((act_eval->flags & DUK_ACT_FLAG_DIRECT_EVAL) == 0); + } + act_caller = NULL; /* avoid dereference after potential callstack realloc */ + act_eval = NULL; + + duk_push_hstring_stridx(ctx, DUK_STRIDX_INPUT); /* XXX: copy from caller? */ + duk_js_compile(thr, + (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h), + (duk_size_t) DUK_HSTRING_GET_BYTELEN(h), + comp_flags); + func = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1); + DUK_ASSERT(func != NULL); + DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) func)); + + /* [ source template ] */ + + /* E5 Section 10.4.2 */ + DUK_ASSERT(thr->callstack_top >= 1); + act = thr->callstack + thr->callstack_top - 1; /* this function */ + if (act->flags & DUK_ACT_FLAG_DIRECT_EVAL) { + DUK_ASSERT(thr->callstack_top >= 2); + act = thr->callstack + thr->callstack_top + level; /* caller */ + if (act->lex_env == NULL) { + DUK_ASSERT(act->var_env == NULL); + DUK_DDD(DUK_DDDPRINT("delayed environment initialization")); + + /* this may have side effects, so re-lookup act */ + duk_js_init_activation_environment_records_delayed(thr, act); + act = thr->callstack + thr->callstack_top + level; + } + DUK_ASSERT(act->lex_env != NULL); + DUK_ASSERT(act->var_env != NULL); + + this_to_global = 0; + + if (DUK_HOBJECT_HAS_STRICT((duk_hobject *) func)) { + duk_hobject *new_env; + duk_hobject *act_lex_env; + + DUK_DDD(DUK_DDDPRINT("direct eval call to a strict function -> " + "var_env and lex_env to a fresh env, " + "this_binding to caller's this_binding")); + + act = thr->callstack + thr->callstack_top + level; /* caller */ + act_lex_env = act->lex_env; + act = NULL; /* invalidated */ + + (void) duk_push_object_helper_proto(ctx, + DUK_HOBJECT_FLAG_EXTENSIBLE | + DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV), + act_lex_env); + new_env = duk_require_hobject(ctx, -1); + DUK_ASSERT(new_env != NULL); + DUK_DDD(DUK_DDDPRINT("new_env allocated: %!iO", + (duk_heaphdr *) new_env)); + + outer_lex_env = new_env; + outer_var_env = new_env; + + duk_insert(ctx, 0); /* stash to bottom of value stack to keep new_env reachable for duration of eval */ + + /* compiler's responsibility */ + DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV((duk_hobject *) func)); + } else { + DUK_DDD(DUK_DDDPRINT("direct eval call to a non-strict function -> " + "var_env and lex_env to caller's envs, " + "this_binding to caller's this_binding")); + + outer_lex_env = act->lex_env; + outer_var_env = act->var_env; + + /* compiler's responsibility */ + DUK_ASSERT(!DUK_HOBJECT_HAS_NEWENV((duk_hobject *) func)); + } + } else { + DUK_DDD(DUK_DDDPRINT("indirect eval call -> var_env and lex_env to " + "global object, this_binding to global object")); + + this_to_global = 1; + outer_lex_env = thr->builtins[DUK_BIDX_GLOBAL_ENV]; + outer_var_env = thr->builtins[DUK_BIDX_GLOBAL_ENV]; + } + act = NULL; + + /* Eval code doesn't need an automatic .prototype object. */ + duk_js_push_closure(thr, func, outer_var_env, outer_lex_env, 0 /*add_auto_proto*/); + + /* [ source template closure ] */ + + if (this_to_global) { + DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL); + duk_push_hobject_bidx(ctx, DUK_BIDX_GLOBAL); + } else { + duk_tval *tv; + DUK_ASSERT(thr->callstack_top >= 2); + act = thr->callstack + thr->callstack_top + level; /* caller */ + tv = thr->valstack + act->idx_bottom - 1; /* this is just beneath bottom */ + DUK_ASSERT(tv >= thr->valstack); + duk_push_tval(ctx, tv); + } + + DUK_DDD(DUK_DDDPRINT("eval -> lex_env=%!iO, var_env=%!iO, this_binding=%!T", + (duk_heaphdr *) outer_lex_env, + (duk_heaphdr *) outer_var_env, + duk_get_tval(ctx, -1))); + + /* [ source template closure this ] */ + + duk_call_method(ctx, 0); + + /* [ source template result ] */ + + return 1; +} + +/* + * Parsing of ints and floats + */ + +DUK_INTERNAL duk_ret_t duk_bi_global_object_parse_int(duk_context *ctx) { + duk_int32_t radix; + duk_small_uint_t s2n_flags; + + DUK_ASSERT_TOP(ctx, 2); + duk_to_string(ctx, 0); + + radix = duk_to_int32(ctx, 1); + + s2n_flags = DUK_S2N_FLAG_TRIM_WHITE | + DUK_S2N_FLAG_ALLOW_GARBAGE | + DUK_S2N_FLAG_ALLOW_PLUS | + DUK_S2N_FLAG_ALLOW_MINUS | + DUK_S2N_FLAG_ALLOW_LEADING_ZERO | + DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT; + + /* Specification stripPrefix maps to DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT. + * + * Don't autodetect octals (from leading zeroes), require user code to + * provide an explicit radix 8 for parsing octal. See write-up from Mozilla: + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#ECMAScript_5_Removes_Octal_Interpretation + */ + + if (radix != 0) { + if (radix < 2 || radix > 36) { + goto ret_nan; + } + if (radix != 16) { + s2n_flags &= ~DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT; + } + } else { + radix = 10; + } + + duk_dup(ctx, 0); + duk_numconv_parse(ctx, radix, s2n_flags); + return 1; + + ret_nan: + duk_push_nan(ctx); + return 1; +} + +DUK_INTERNAL duk_ret_t duk_bi_global_object_parse_float(duk_context *ctx) { + duk_small_uint_t s2n_flags; + duk_int32_t radix; + + DUK_ASSERT_TOP(ctx, 1); + duk_to_string(ctx, 0); + + radix = 10; + + /* XXX: check flags */ + s2n_flags = DUK_S2N_FLAG_TRIM_WHITE | + DUK_S2N_FLAG_ALLOW_EXP | + DUK_S2N_FLAG_ALLOW_GARBAGE | + DUK_S2N_FLAG_ALLOW_PLUS | + DUK_S2N_FLAG_ALLOW_MINUS | + DUK_S2N_FLAG_ALLOW_INF | + DUK_S2N_FLAG_ALLOW_FRAC | + DUK_S2N_FLAG_ALLOW_NAKED_FRAC | + DUK_S2N_FLAG_ALLOW_EMPTY_FRAC | + DUK_S2N_FLAG_ALLOW_LEADING_ZERO; + + duk_numconv_parse(ctx, radix, s2n_flags); + return 1; +} + +/* + * Number checkers + */ + +DUK_INTERNAL duk_ret_t duk_bi_global_object_is_nan(duk_context *ctx) { + duk_double_t d = duk_to_number(ctx, 0); + duk_push_boolean(ctx, DUK_ISNAN(d)); + return 1; +} + +DUK_INTERNAL duk_ret_t duk_bi_global_object_is_finite(duk_context *ctx) { + duk_double_t d = duk_to_number(ctx, 0); + duk_push_boolean(ctx, DUK_ISFINITE(d)); + return 1; +} + +/* + * URI handling + */ + +DUK_INTERNAL duk_ret_t duk_bi_global_object_decode_uri(duk_context *ctx) { + return duk__transform_helper(ctx, duk__transform_callback_decode_uri, (const void *) duk__decode_uri_reserved_table); +} + +DUK_INTERNAL duk_ret_t duk_bi_global_object_decode_uri_component(duk_context *ctx) { + return duk__transform_helper(ctx, duk__transform_callback_decode_uri, (const void *) duk__decode_uri_component_reserved_table); +} + +DUK_INTERNAL duk_ret_t duk_bi_global_object_encode_uri(duk_context *ctx) { + return duk__transform_helper(ctx, duk__transform_callback_encode_uri, (const void *) duk__encode_uriunescaped_table); +} + +DUK_INTERNAL duk_ret_t duk_bi_global_object_encode_uri_component(duk_context *ctx) { + return duk__transform_helper(ctx, duk__transform_callback_encode_uri, (const void *) duk__encode_uricomponent_unescaped_table); +} + +#ifdef DUK_USE_SECTION_B +DUK_INTERNAL duk_ret_t duk_bi_global_object_escape(duk_context *ctx) { + return duk__transform_helper(ctx, duk__transform_callback_escape, (const void *) NULL); +} + +DUK_INTERNAL duk_ret_t duk_bi_global_object_unescape(duk_context *ctx) { + return duk__transform_helper(ctx, duk__transform_callback_unescape, (const void *) NULL); +} +#else /* DUK_USE_SECTION_B */ +DUK_INTERNAL duk_ret_t duk_bi_global_object_escape(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} + +DUK_INTERNAL duk_ret_t duk_bi_global_object_unescape(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_SECTION_B */ + +#if defined(DUK_USE_BROWSER_LIKE) && (defined(DUK_USE_FILE_IO) || defined(DUK_USE_DEBUGGER_SUPPORT)) +DUK_INTERNAL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_int_t magic; + duk_idx_t nargs; + const duk_uint8_t *buf; + duk_size_t sz_buf; + const char nl = (const char) DUK_ASC_LF; +#ifndef DUK_USE_PREFER_SIZE + duk_uint8_t buf_stack[256]; +#endif +#ifdef DUK_USE_FILE_IO + duk_file *f_out; +#endif + + DUK_UNREF(thr); + + magic = duk_get_current_magic(ctx); + DUK_UNREF(magic); + + nargs = duk_get_top(ctx); + + /* If argument count is 1 and first argument is a buffer, write the buffer + * as raw data into the file without a newline; this allows exact control + * over stdout/stderr without an additional entrypoint (useful for now). + * + * Otherwise current print/alert semantics are to ToString() coerce + * arguments, join them with a single space, and append a newline. + */ + + if (nargs == 1 && duk_is_buffer(ctx, 0)) { + buf = (const duk_uint8_t *) duk_get_buffer(ctx, 0, &sz_buf); + DUK_ASSERT(buf != NULL); + } else if (nargs > 0) { +#ifdef DUK_USE_PREFER_SIZE + /* Compact but lots of churn. */ + duk_push_hstring_stridx(thr, DUK_STRIDX_SPACE); + duk_insert(ctx, 0); + duk_join(ctx, nargs); + duk_push_string(thr, "\n"); + duk_concat(ctx, 2); + buf = (const duk_uint8_t *) duk_get_lstring(ctx, -1, &sz_buf); + DUK_ASSERT(buf != NULL); +#else /* DUK_USE_PREFER_SIZE */ + /* Higher footprint, less churn. */ + duk_idx_t i; + duk_size_t sz_str; + const duk_uint8_t *p_str; + duk_uint8_t *p; + + sz_buf = (duk_size_t) nargs; /* spaces (nargs - 1) + newline */ + for (i = 0; i < nargs; i++) { + (void) duk_to_lstring(ctx, i, &sz_str); + sz_buf += sz_str; + } + + if (sz_buf <= sizeof(buf_stack)) { + p = (duk_uint8_t *) buf_stack; + } else { + p = (duk_uint8_t *) duk_push_fixed_buffer(ctx, sz_buf); + DUK_ASSERT(p != NULL); + } + + buf = (const duk_uint8_t *) p; + for (i = 0; i < nargs; i++) { + p_str = (const duk_uint8_t *) duk_get_lstring(ctx, i, &sz_str); + DUK_ASSERT(p_str != NULL); + DUK_MEMCPY((void *) p, (const void *) p_str, sz_str); + p += sz_str; + *p++ = (duk_uint8_t) (i == nargs - 1 ? DUK_ASC_LF : DUK_ASC_SPACE); + } + DUK_ASSERT((const duk_uint8_t *) p == buf + sz_buf); +#endif /* DUK_USE_PREFER_SIZE */ + } else { + buf = (const duk_uint8_t *) &nl; + sz_buf = 1; + } + + /* 'buf' contains the string to write, 'sz_buf' contains the length + * (which may be zero). + */ + DUK_ASSERT(buf != NULL); + + if (sz_buf == 0) { + return 0; + } + +#ifdef DUK_USE_FILE_IO + f_out = (magic ? DUK_STDERR : DUK_STDOUT); + DUK_FWRITE((const void *) buf, 1, (size_t) sz_buf, f_out); + DUK_FFLUSH(f_out); +#endif + +#if defined(DUK_USE_DEBUGGER_SUPPORT) && defined(DUK_USE_DEBUGGER_FWD_PRINTALERT) + if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) { + duk_debug_write_notify(thr, magic ? DUK_DBG_CMD_ALERT : DUK_DBG_CMD_PRINT); + duk_debug_write_string(thr, (const char *) buf, sz_buf); + duk_debug_write_eom(thr); + } +#endif + return 0; +} +#elif defined(DUK_USE_BROWSER_LIKE) /* print provider */ +DUK_INTERNAL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx) { + DUK_UNREF(ctx); + return 0; +} +#else /* print provider */ +DUK_INTERNAL duk_ret_t duk_bi_global_object_print_helper(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* print provider */ + +/* + * CommonJS require() and modules support + */ + +#if defined(DUK_USE_COMMONJS_MODULES) +DUK_LOCAL void duk__bi_global_resolve_module_id(duk_context *ctx, const char *req_id, const char *mod_id) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_uint8_t buf[DUK_BI_COMMONJS_MODULE_ID_LIMIT]; + duk_uint8_t *p; + duk_uint8_t *q; + duk_uint8_t *q_last; /* last component */ + duk_int_t int_rc; + + DUK_ASSERT(req_id != NULL); + /* mod_id may be NULL */ + + /* + * A few notes on the algorithm: + * + * - Terms are not allowed to begin with a period unless the term + * is either '.' or '..'. This simplifies implementation (and + * is within CommonJS modules specification). + * + * - There are few output bound checks here. This is on purpose: + * the resolution input is length checked and the output is never + * longer than the input. The resolved output is written directly + * over the input because it's never longer than the input at any + * point in the algorithm. + * + * - Non-ASCII characters are processed as individual bytes and + * need no special treatment. However, U+0000 terminates the + * algorithm; this is not an issue because U+0000 is not a + * desirable term character anyway. + */ + + /* + * Set up the resolution input which is the requested ID directly + * (if absolute or no current module path) or with current module + * ID prepended (if relative and current module path exists). + * + * Suppose current module is 'foo/bar' and relative path is './quux'. + * The 'bar' component must be replaced so the initial input here is + * 'foo/bar/.././quux'. + */ + + if (mod_id != NULL && req_id[0] == '.') { + int_rc = DUK_SNPRINTF((char *) buf, sizeof(buf), "%s/../%s", mod_id, req_id); + } else { + int_rc = DUK_SNPRINTF((char *) buf, sizeof(buf), "%s", req_id); + } + if (int_rc >= (duk_int_t) sizeof(buf) || int_rc < 0) { + /* Potentially truncated, NUL not guaranteed in any case. + * The (int_rc < 0) case should not occur in practice. + */ + DUK_DD(DUK_DDPRINT("resolve error: temporary working module ID doesn't fit into resolve buffer")); + goto resolve_error; + } + DUK_ASSERT(DUK_STRLEN((const char *) buf) < sizeof(buf)); /* at most sizeof(buf) - 1 */ + + DUK_DDD(DUK_DDDPRINT("input module id: '%s'", (const char *) buf)); + + /* + * Resolution loop. At the top of the loop we're expecting a valid + * term: '.', '..', or a non-empty identifier not starting with a period. + */ + + p = buf; + q = buf; + for (;;) { + duk_uint_fast8_t c; + + /* Here 'p' always points to the start of a term. + * + * We can also unconditionally reset q_last here: if this is + * the last (non-empty) term q_last will have the right value + * on loop exit. + */ + + DUK_ASSERT(p >= q); /* output is never longer than input during resolution */ + + DUK_DDD(DUK_DDDPRINT("resolve loop top: p -> '%s', q=%p, buf=%p", + (const char *) p, (void *) q, (void *) buf)); + + q_last = q; + + c = *p++; + if (DUK_UNLIKELY(c == 0)) { + DUK_DD(DUK_DDPRINT("resolve error: requested ID must end with a non-empty term")); + goto resolve_error; + } else if (DUK_UNLIKELY(c == '.')) { + c = *p++; + if (c == '/') { + /* Term was '.' and is eaten entirely (including dup slashes). */ + goto eat_dup_slashes; + } + if (c == '.' && *p == '/') { + /* Term was '..', backtrack resolved name by one component. + * q[-1] = previous slash (or beyond start of buffer) + * q[-2] = last char of previous component (or beyond start of buffer) + */ + p++; /* eat (first) input slash */ + DUK_ASSERT(q >= buf); + if (q == buf) { + DUK_DD(DUK_DDPRINT("resolve error: term was '..' but nothing to backtrack")); + goto resolve_error; + } + DUK_ASSERT(*(q - 1) == '/'); + q--; /* backtrack to last output slash (dups already eliminated) */ + for (;;) { + /* Backtrack to previous slash or start of buffer. */ + DUK_ASSERT(q >= buf); + if (q == buf) { + break; + } + if (*(q - 1) == '/') { + break; + } + q--; + } + goto eat_dup_slashes; + } + DUK_DD(DUK_DDPRINT("resolve error: term begins with '.' but is not '.' or '..' (not allowed now)")); + goto resolve_error; + } else if (DUK_UNLIKELY(c == '/')) { + /* e.g. require('/foo'), empty terms not allowed */ + DUK_DD(DUK_DDPRINT("resolve error: empty term (not allowed now)")); + goto resolve_error; + } else { + for (;;) { + /* Copy term name until end or '/'. */ + *q++ = c; + c = *p++; + if (DUK_UNLIKELY(c == 0)) { + /* This was the last term, and q_last was + * updated to match this term at loop top. + */ + goto loop_done; + } else if (DUK_UNLIKELY(c == '/')) { + *q++ = '/'; + break; + } else { + /* write on next loop */ + } + } + } + + eat_dup_slashes: + for (;;) { + /* eat dup slashes */ + c = *p; + if (DUK_LIKELY(c != '/')) { + break; + } + p++; + } + } + loop_done: + /* Output #1: resolved absolute name */ + DUK_ASSERT(q >= buf); + duk_push_lstring(ctx, (const char *) buf, (size_t) (q - buf)); + + /* Output #2: last component name */ + DUK_ASSERT(q >= q_last); + DUK_ASSERT(q_last >= buf); + duk_push_lstring(ctx, (const char *) q_last, (size_t) (q - q_last)); + + DUK_DD(DUK_DDPRINT("after resolving module name: buf=%p, q_last=%p, q=%p", + (void *) buf, (void *) q_last, (void *) q)); + return; + + resolve_error: + DUK_ERROR_FMT1(thr, DUK_ERR_TYPE_ERROR, "cannot resolve module id: %s", (const char *) req_id); +} +#endif /* DUK_USE_COMMONJS_MODULES */ + +#if defined(DUK_USE_COMMONJS_MODULES) +/* Stack indices for better readability */ +#define DUK__IDX_REQUESTED_ID 0 /* Module id requested */ +#define DUK__IDX_REQUIRE 1 /* Current require() function */ +#define DUK__IDX_REQUIRE_ID 2 /* The base ID of the current require() function, resolution base */ +#define DUK__IDX_RESOLVED_ID 3 /* Resolved, normalized absolute module ID */ +#define DUK__IDX_LASTCOMP 4 /* Last component name in resolved path */ +#define DUK__IDX_DUKTAPE 5 /* Duktape object */ +#define DUK__IDX_MODLOADED 6 /* Duktape.modLoaded[] module cache */ +#define DUK__IDX_UNDEFINED 7 /* 'undefined', artifact of lookup */ +#define DUK__IDX_FRESH_REQUIRE 8 /* New require() function for module, updated resolution base */ +#define DUK__IDX_EXPORTS 9 /* Default exports table */ +#define DUK__IDX_MODULE 10 /* Module object containing module.exports, etc */ + +DUK_INTERNAL duk_ret_t duk_bi_global_object_require(duk_context *ctx) { + const char *str_req_id; /* requested identifier */ + const char *str_mod_id; /* require.id of current module */ + duk_int_t pcall_rc; + + /* NOTE: we try to minimize code size by avoiding unnecessary pops, + * so the stack looks a bit cluttered in this function. DUK_ASSERT_TOP() + * assertions are used to ensure stack configuration is correct at each + * step. + */ + + /* + * Resolve module identifier into canonical absolute form. + */ + + str_req_id = duk_require_string(ctx, DUK__IDX_REQUESTED_ID); + duk_push_current_function(ctx); + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_ID); + str_mod_id = duk_get_string(ctx, DUK__IDX_REQUIRE_ID); /* ignore non-strings */ + DUK_DDD(DUK_DDDPRINT("resolve module id: requested=%!T, currentmodule=%!T", + duk_get_tval(ctx, DUK__IDX_REQUESTED_ID), + duk_get_tval(ctx, DUK__IDX_REQUIRE_ID))); + duk__bi_global_resolve_module_id(ctx, str_req_id, str_mod_id); + str_req_id = NULL; + str_mod_id = NULL; + DUK_DDD(DUK_DDDPRINT("resolved module id: requested=%!T, currentmodule=%!T, result=%!T, lastcomp=%!T", + duk_get_tval(ctx, DUK__IDX_REQUESTED_ID), + duk_get_tval(ctx, DUK__IDX_REQUIRE_ID), + duk_get_tval(ctx, DUK__IDX_RESOLVED_ID), + duk_get_tval(ctx, DUK__IDX_LASTCOMP))); + + /* [ requested_id require require.id resolved_id last_comp ] */ + DUK_ASSERT_TOP(ctx, DUK__IDX_LASTCOMP + 1); + + /* + * Cached module check. + * + * If module has been loaded or its loading has already begun without + * finishing, return the same cached value ('exports'). The value is + * registered when module load starts so that circular references can + * be supported to some extent. + */ + + duk_push_hobject_bidx(ctx, DUK_BIDX_DUKTAPE); + duk_get_prop_stridx(ctx, DUK__IDX_DUKTAPE, DUK_STRIDX_MOD_LOADED); /* Duktape.modLoaded */ + (void) duk_require_hobject(ctx, DUK__IDX_MODLOADED); + DUK_ASSERT_TOP(ctx, DUK__IDX_MODLOADED + 1); + + duk_dup(ctx, DUK__IDX_RESOLVED_ID); + if (duk_get_prop(ctx, DUK__IDX_MODLOADED)) { + /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded Duktape.modLoaded[id] ] */ + DUK_DD(DUK_DDPRINT("module already loaded: %!T", + duk_get_tval(ctx, DUK__IDX_RESOLVED_ID))); + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_EXPORTS); /* return module.exports */ + return 1; + } + DUK_ASSERT_TOP(ctx, DUK__IDX_UNDEFINED + 1); + + /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined ] */ + + /* + * Module not loaded (and loading not started previously). + * + * Create a new require() function with 'id' set to resolved ID + * of module being loaded. Also create 'exports' and 'module' + * tables but don't register exports to the loaded table yet. + * We don't want to do that unless the user module search callbacks + * succeeds in finding the module. + */ + + DUK_D(DUK_DPRINT("loading module %!T, resolution base %!T, requested ID %!T -> resolved ID %!T, last component %!T", + duk_get_tval(ctx, DUK__IDX_RESOLVED_ID), + duk_get_tval(ctx, DUK__IDX_REQUIRE_ID), + duk_get_tval(ctx, DUK__IDX_REQUESTED_ID), + duk_get_tval(ctx, DUK__IDX_RESOLVED_ID), + duk_get_tval(ctx, DUK__IDX_LASTCOMP))); + + /* Fresh require: require.id is left configurable (but not writable) + * so that is not easy to accidentally tweak it, but it can still be + * done with Object.defineProperty(). + * + * XXX: require.id could also be just made non-configurable, as there + * is no practical reason to touch it. + */ + duk_push_c_function(ctx, duk_bi_global_object_require, 1 /*nargs*/); + duk_push_hstring_stridx(ctx, DUK_STRIDX_REQUIRE); + duk_xdef_prop_stridx(ctx, DUK__IDX_FRESH_REQUIRE, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE); + duk_dup(ctx, DUK__IDX_RESOLVED_ID); + duk_xdef_prop_stridx(ctx, DUK__IDX_FRESH_REQUIRE, DUK_STRIDX_ID, DUK_PROPDESC_FLAGS_C); /* a fresh require() with require.id = resolved target module id */ + + /* Module table: + * - module.exports: initial exports table (may be replaced by user) + * - module.id is non-writable and non-configurable, as the CommonJS + * spec suggests this if possible + * - module.filename: not set, defaults to resolved ID if not explicitly + * set by modSearch() (note capitalization, not .fileName, matches Node.js) + * - module.name: not set, defaults to last component of resolved ID if + * not explicitly set by modSearch() + */ + duk_push_object(ctx); /* exports */ + duk_push_object(ctx); /* module */ + duk_dup(ctx, DUK__IDX_EXPORTS); + duk_xdef_prop_stridx(ctx, DUK__IDX_MODULE, DUK_STRIDX_EXPORTS, DUK_PROPDESC_FLAGS_WC); /* module.exports = exports */ + duk_dup(ctx, DUK__IDX_RESOLVED_ID); /* resolved id: require(id) must return this same module */ + duk_xdef_prop_stridx(ctx, DUK__IDX_MODULE, DUK_STRIDX_ID, DUK_PROPDESC_FLAGS_NONE); /* module.id = resolved_id */ + duk_compact(ctx, DUK__IDX_MODULE); /* module table remains registered to modLoaded, minimize its size */ + DUK_ASSERT_TOP(ctx, DUK__IDX_MODULE + 1); + + DUK_DD(DUK_DDPRINT("module table created: %!T", duk_get_tval(ctx, DUK__IDX_MODULE))); + + /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module ] */ + + /* Register the module table early to modLoaded[] so that we can + * support circular references even in modSearch(). If an error + * is thrown, we'll delete the reference. + */ + duk_dup(ctx, DUK__IDX_RESOLVED_ID); + duk_dup(ctx, DUK__IDX_MODULE); + duk_put_prop(ctx, DUK__IDX_MODLOADED); /* Duktape.modLoaded[resolved_id] = module */ + + /* + * Call user provided module search function and build the wrapped + * module source code (if necessary). The module search function + * can be used to implement pure Ecmacsript, pure C, and mixed + * Ecmascript/C modules. + * + * The module search function can operate on the exports table directly + * (e.g. DLL code can register values to it). It can also return a + * string which is interpreted as module source code (if a non-string + * is returned the module is assumed to be a pure C one). If a module + * cannot be found, an error must be thrown by the user callback. + * + * Because Duktape.modLoaded[] already contains the module being + * loaded, circular references for C modules should also work + * (although expected to be quite rare). + */ + + duk_push_string(ctx, "(function(require,exports,module){"); + + /* Duktape.modSearch(resolved_id, fresh_require, exports, module). */ + duk_get_prop_stridx(ctx, DUK__IDX_DUKTAPE, DUK_STRIDX_MOD_SEARCH); /* Duktape.modSearch */ + duk_dup(ctx, DUK__IDX_RESOLVED_ID); + duk_dup(ctx, DUK__IDX_FRESH_REQUIRE); + duk_dup(ctx, DUK__IDX_EXPORTS); + duk_dup(ctx, DUK__IDX_MODULE); /* [ ... Duktape.modSearch resolved_id last_comp fresh_require exports module ] */ + pcall_rc = duk_pcall(ctx, 4 /*nargs*/); /* -> [ ... source ] */ + DUK_ASSERT_TOP(ctx, DUK__IDX_MODULE + 3); + + if (pcall_rc != DUK_EXEC_SUCCESS) { + /* Delete entry in Duktape.modLoaded[] and rethrow. */ + goto delete_rethrow; + } + + /* If user callback did not return source code, module loading + * is finished (user callback initialized exports table directly). + */ + if (!duk_is_string(ctx, -1)) { + /* User callback did not return source code, so module loading + * is finished: just update modLoaded with final module.exports + * and we're done. + */ + goto return_exports; + } + + /* Finish the wrapped module source. Force module.filename as the + * function .fileName so it gets set for functions defined within a + * module. This also ensures loggers created within the module get + * the module ID (or overridden filename) as their default logger name. + * (Note capitalization: .filename matches Node.js while .fileName is + * used elsewhere in Duktape.) + */ + duk_push_string(ctx, "})"); + duk_concat(ctx, 3); + if (!duk_get_prop_stridx(ctx, DUK__IDX_MODULE, DUK_STRIDX_FILENAME)) { + /* module.filename for .fileName, default to resolved ID if + * not present. + */ + duk_pop(ctx); + duk_dup(ctx, DUK__IDX_RESOLVED_ID); + } + duk_eval_raw(ctx, NULL, 0, DUK_COMPILE_EVAL); + + /* Module has now evaluated to a wrapped module function. Force its + * .name to match module.name (defaults to last component of resolved + * ID) so that it is shown in stack traces too. Note that we must not + * introduce an actual name binding into the function scope (which is + * usually the case with a named function) because it would affect the + * scope seen by the module and shadow accesses to globals of the same name. + * This is now done by compiling the function as anonymous and then forcing + * its .name without setting a "has name binding" flag. + */ + + duk_push_hstring_stridx(ctx, DUK_STRIDX_NAME); + if (!duk_get_prop_stridx(ctx, DUK__IDX_MODULE, DUK_STRIDX_NAME)) { + /* module.name for .name, default to last component if + * not present. + */ + duk_pop(ctx); + duk_dup(ctx, DUK__IDX_LASTCOMP); + } + duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE); + + /* + * Call the wrapped module function. + * + * Use a protected call so that we can update Duktape.modLoaded[resolved_id] + * even if the module throws an error. + */ + + /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module mod_func ] */ + DUK_ASSERT_TOP(ctx, DUK__IDX_MODULE + 2); + + duk_dup(ctx, DUK__IDX_EXPORTS); /* exports (this binding) */ + duk_dup(ctx, DUK__IDX_FRESH_REQUIRE); /* fresh require (argument) */ + duk_get_prop_stridx(ctx, DUK__IDX_MODULE, DUK_STRIDX_EXPORTS); /* relookup exports from module.exports in case it was changed by modSearch */ + duk_dup(ctx, DUK__IDX_MODULE); /* module (argument) */ + DUK_ASSERT_TOP(ctx, DUK__IDX_MODULE + 6); + + /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module mod_func exports fresh_require exports module ] */ + + pcall_rc = duk_pcall_method(ctx, 3 /*nargs*/); + if (pcall_rc != DUK_EXEC_SUCCESS) { + /* Module loading failed. Node.js will forget the module + * registration so that another require() will try to load + * the module again. Mimic that behavior. + */ + goto delete_rethrow; + } + + /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module result(ignored) ] */ + DUK_ASSERT_TOP(ctx, DUK__IDX_MODULE + 2); + + /* fall through */ + + return_exports: + duk_get_prop_stridx(ctx, DUK__IDX_MODULE, DUK_STRIDX_EXPORTS); + duk_compact(ctx, -1); /* compact the exports table */ + return 1; /* return module.exports */ + + delete_rethrow: + duk_dup(ctx, DUK__IDX_RESOLVED_ID); + duk_del_prop(ctx, DUK__IDX_MODLOADED); /* delete Duktape.modLoaded[resolved_id] */ + duk_throw(ctx); /* rethrow original error */ + return 0; /* not reachable */ +} + +#undef DUK__IDX_REQUESTED_ID +#undef DUK__IDX_REQUIRE +#undef DUK__IDX_REQUIRE_ID +#undef DUK__IDX_RESOLVED_ID +#undef DUK__IDX_LASTCOMP +#undef DUK__IDX_DUKTAPE +#undef DUK__IDX_MODLOADED +#undef DUK__IDX_UNDEFINED +#undef DUK__IDX_FRESH_REQUIRE +#undef DUK__IDX_EXPORTS +#undef DUK__IDX_MODULE +#else +DUK_INTERNAL duk_ret_t duk_bi_global_object_require(duk_context *ctx) { + DUK_UNREF(ctx); + return DUK_RET_UNSUPPORTED_ERROR; +} +#endif /* DUK_USE_COMMONJS_MODULES */ +/* + * JSON built-ins. + * + * See doc/json.rst. + * + * Codepoints are handled as duk_uint_fast32_t to ensure that the full + * unsigned 32-bit range is supported. This matters to e.g. JX. + * + * Input parsing doesn't do an explicit end-of-input check at all. This is + * safe: input string data is always NUL-terminated (0x00) and valid JSON + * inputs never contain plain NUL characters, so that as long as syntax checks + * are correct, we'll never read past the NUL. This approach reduces code size + * and improves parsing performance, but it's critical that syntax checks are + * indeed correct! + */ + +/* include removed: duk_internal.h */ + +/* + * Local defines and forward declarations. + */ + +#define DUK__JSON_DECSTR_BUFSIZE 128 +#define DUK__JSON_DECSTR_CHUNKSIZE 64 +#define DUK__JSON_ENCSTR_CHUNKSIZE 64 +#define DUK__JSON_STRINGIFY_BUFSIZE 128 +#define DUK__JSON_MAX_ESC_LEN 10 /* '\Udeadbeef' */ + +DUK_LOCAL_DECL void duk__dec_syntax_error(duk_json_dec_ctx *js_ctx); +DUK_LOCAL_DECL void duk__dec_eat_white(duk_json_dec_ctx *js_ctx); +DUK_LOCAL_DECL duk_uint8_t duk__dec_peek(duk_json_dec_ctx *js_ctx); +DUK_LOCAL_DECL duk_uint8_t duk__dec_get(duk_json_dec_ctx *js_ctx); +DUK_LOCAL_DECL duk_uint8_t duk__dec_get_nonwhite(duk_json_dec_ctx *js_ctx); +DUK_LOCAL_DECL duk_uint_fast32_t duk__dec_decode_hex_escape(duk_json_dec_ctx *js_ctx, duk_small_uint_t n); +DUK_LOCAL_DECL void duk__dec_req_stridx(duk_json_dec_ctx *js_ctx, duk_small_uint_t stridx); +DUK_LOCAL_DECL void duk__dec_string(duk_json_dec_ctx *js_ctx); +#ifdef DUK_USE_JX +DUK_LOCAL_DECL void duk__dec_plain_string(duk_json_dec_ctx *js_ctx); +DUK_LOCAL_DECL void duk__dec_pointer(duk_json_dec_ctx *js_ctx); +DUK_LOCAL_DECL void duk__dec_buffer(duk_json_dec_ctx *js_ctx); +#endif +DUK_LOCAL_DECL void duk__dec_number(duk_json_dec_ctx *js_ctx); +DUK_LOCAL_DECL void duk__dec_objarr_entry(duk_json_dec_ctx *js_ctx); +DUK_LOCAL_DECL void duk__dec_objarr_exit(duk_json_dec_ctx *js_ctx); +DUK_LOCAL_DECL void duk__dec_object(duk_json_dec_ctx *js_ctx); +DUK_LOCAL_DECL void duk__dec_array(duk_json_dec_ctx *js_ctx); +DUK_LOCAL_DECL void duk__dec_value(duk_json_dec_ctx *js_ctx); +DUK_LOCAL_DECL void duk__dec_reviver_walk(duk_json_dec_ctx *js_ctx); + +DUK_LOCAL_DECL void duk__emit_1(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch); +DUK_LOCAL_DECL void duk__emit_2(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch1, duk_uint_fast8_t ch2); +DUK_LOCAL_DECL void duk__unemit_1(duk_json_enc_ctx *js_ctx); +DUK_LOCAL_DECL void duk__emit_hstring(duk_json_enc_ctx *js_ctx, duk_hstring *h); +#if defined(DUK_USE_FASTINT) +DUK_LOCAL_DECL void duk__emit_cstring(duk_json_enc_ctx *js_ctx, const char *p); +#endif +DUK_LOCAL_DECL void duk__emit_stridx(duk_json_enc_ctx *js_ctx, duk_small_uint_t stridx); +DUK_LOCAL_DECL duk_uint8_t *duk__emit_esc_auto_fast(duk_json_enc_ctx *js_ctx, duk_uint_fast32_t cp, duk_uint8_t *q); +DUK_LOCAL_DECL void duk__enc_key_autoquote(duk_json_enc_ctx *js_ctx, duk_hstring *k); +DUK_LOCAL_DECL void duk__enc_quote_string(duk_json_enc_ctx *js_ctx, duk_hstring *h_str); +DUK_LOCAL_DECL void duk__enc_objarr_entry(duk_json_enc_ctx *js_ctx, duk_idx_t *entry_top); +DUK_LOCAL_DECL void duk__enc_objarr_exit(duk_json_enc_ctx *js_ctx, duk_idx_t *entry_top); +DUK_LOCAL_DECL void duk__enc_object(duk_json_enc_ctx *js_ctx); +DUK_LOCAL_DECL void duk__enc_array(duk_json_enc_ctx *js_ctx); +DUK_LOCAL_DECL duk_bool_t duk__enc_value(duk_json_enc_ctx *js_ctx, duk_idx_t idx_holder); +DUK_LOCAL_DECL duk_bool_t duk__enc_allow_into_proplist(duk_tval *tv); +DUK_LOCAL_DECL void duk__enc_double(duk_json_enc_ctx *js_ctx); +#if defined(DUK_USE_FASTINT) +DUK_LOCAL_DECL void duk__enc_fastint_tval(duk_json_enc_ctx *js_ctx, duk_tval *tv); +#endif +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) +DUK_LOCAL_DECL void duk__enc_buffer(duk_json_enc_ctx *js_ctx, duk_hbuffer *h); +DUK_LOCAL_DECL void duk__enc_pointer(duk_json_enc_ctx *js_ctx, void *ptr); +DUK_LOCAL_DECL void duk__enc_bufferobject(duk_json_enc_ctx *js_ctx, duk_hbufferobject *h_bufobj); +#endif +DUK_LOCAL_DECL void duk__enc_newline_indent(duk_json_enc_ctx *js_ctx, duk_int_t depth); + +/* + * Helper tables + */ + +#if defined(DUK_USE_JSON_QUOTESTRING_FASTPATH) +DUK_LOCAL const duk_uint8_t duk__json_quotestr_lookup[256] = { + /* 0x00 ... 0x7f: as is + * 0x80: escape generically + * 0x81: slow path + * 0xa0 ... 0xff: backslash + one char + */ + + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xe2, 0xf4, 0xee, 0x80, 0xe6, 0xf2, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x20, 0x21, 0xa2, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0xdc, 0x5d, 0x5e, 0x5f, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x81, + 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, + 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, + 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, + 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, + 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, + 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, + 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, + 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81 +}; +#else /* DUK_USE_JSON_QUOTESTRING_FASTPATH */ +DUK_LOCAL const duk_uint8_t duk__json_quotestr_esc[14] = { + DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, + DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, + DUK_ASC_LC_B, DUK_ASC_LC_T, DUK_ASC_LC_N, DUK_ASC_NUL, + DUK_ASC_LC_F, DUK_ASC_LC_R +}; +#endif /* DUK_USE_JSON_QUOTESTRING_FASTPATH */ + +#if defined(DUK_USE_JSON_DECSTRING_FASTPATH) +DUK_LOCAL const duk_uint8_t duk__json_decstr_lookup[256] = { + /* 0x00: slow path + * other: as is + */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x20, 0x21, 0x00, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x00, 0x5d, 0x5e, 0x5f, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, + 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, + 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, + 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, + 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, + 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff +}; +#endif /* DUK_USE_JSON_DECSTRING_FASTPATH */ + +#if defined(DUK_USE_JSON_EATWHITE_FASTPATH) +DUK_LOCAL const duk_uint8_t duk__json_eatwhite_lookup[256] = { + /* 0x00: finish (non-white) + * 0x01: continue + */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; +#endif /* DUK_USE_JSON_EATWHITE_FASTPATH */ + +#if defined(DUK_USE_JSON_DECNUMBER_FASTPATH) +DUK_LOCAL const duk_uint8_t duk__json_decnumber_lookup[256] = { + /* 0x00: finish (not part of number) + * 0x01: continue + */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; +#endif /* DUK_USE_JSON_DECNUMBER_FASTPATH */ + +/* + * Parsing implementation. + * + * JSON lexer is now separate from duk_lexer.c because there are numerous + * small differences making it difficult to share the lexer. + * + * The parser here works with raw bytes directly; this works because all + * JSON delimiters are ASCII characters. Invalid xUTF-8 encoded values + * inside strings will be passed on without normalization; this is not a + * compliance concern because compliant inputs will always be valid + * CESU-8 encodings. + */ + +DUK_LOCAL void duk__dec_syntax_error(duk_json_dec_ctx *js_ctx) { + /* Shared handler to minimize parser size. Cause will be + * hidden, unfortunately, but we'll have an offset which + * is often quite enough. + */ + DUK_ERROR_FMT1(js_ctx->thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_FMT_INVALID_JSON, + (long) (js_ctx->p - js_ctx->p_start)); +} + +DUK_LOCAL void duk__dec_eat_white(duk_json_dec_ctx *js_ctx) { + const duk_uint8_t *p; + duk_uint8_t t; + + p = js_ctx->p; + for (;;) { + DUK_ASSERT(p <= js_ctx->p_end); + t = *p; + +#if defined(DUK_USE_JSON_EATWHITE_FASTPATH) + /* This fast path is pretty marginal in practice. + * XXX: candidate for removal. + */ + DUK_ASSERT(duk__json_eatwhite_lookup[0x00] == 0x00); /* end-of-input breaks */ + if (duk__json_eatwhite_lookup[t] == 0) { + break; + } +#else /* DUK_USE_JSON_EATWHITE_FASTPATH */ + if (!(t == 0x20 || t == 0x0a || t == 0x0d || t == 0x09)) { + /* NUL also comes here. Comparison order matters, 0x20 + * is most common whitespace. + */ + break; + } +#endif /* DUK_USE_JSON_EATWHITE_FASTPATH */ + p++; + } + js_ctx->p = p; +} + +DUK_LOCAL duk_uint8_t duk__dec_peek(duk_json_dec_ctx *js_ctx) { + DUK_ASSERT(js_ctx->p <= js_ctx->p_end); + return *js_ctx->p; +} + +DUK_LOCAL duk_uint8_t duk__dec_get(duk_json_dec_ctx *js_ctx) { + DUK_ASSERT(js_ctx->p <= js_ctx->p_end); + return *js_ctx->p++; +} + +DUK_LOCAL duk_uint8_t duk__dec_get_nonwhite(duk_json_dec_ctx *js_ctx) { + duk__dec_eat_white(js_ctx); + return duk__dec_get(js_ctx); +} + +/* For JX, expressing the whole unsigned 32-bit range matters. */ +DUK_LOCAL duk_uint_fast32_t duk__dec_decode_hex_escape(duk_json_dec_ctx *js_ctx, duk_small_uint_t n) { + duk_small_uint_t i; + duk_uint_fast32_t res = 0; + duk_uint8_t x; + duk_small_int_t t; + + for (i = 0; i < n; i++) { + /* XXX: share helper from lexer; duk_lexer.c / hexval(). */ + + x = duk__dec_get(js_ctx); + DUK_DDD(DUK_DDDPRINT("decode_hex_escape: i=%ld, n=%ld, res=%ld, x=%ld", + (long) i, (long) n, (long) res, (long) x)); + + /* x == 0x00 (EOF) causes syntax_error */ + DUK_ASSERT(duk_hex_dectab[0] == -1); + t = duk_hex_dectab[x & 0xff]; + if (DUK_LIKELY(t >= 0)) { + res = (res * 16) + t; + } else { + /* catches EOF and invalid digits */ + goto syntax_error; + } + } + + DUK_DDD(DUK_DDDPRINT("final hex decoded value: %ld", (long) res)); + return res; + + syntax_error: + duk__dec_syntax_error(js_ctx); + DUK_UNREACHABLE(); + return 0; +} + +DUK_LOCAL void duk__dec_req_stridx(duk_json_dec_ctx *js_ctx, duk_small_uint_t stridx) { + duk_hstring *h; + const duk_uint8_t *p; + duk_uint8_t x, y; + + /* First character has already been eaten and checked by the caller. + * We can scan until a NUL in stridx string because no built-in strings + * have internal NULs. + */ + + DUK_ASSERT_DISABLE(stridx >= 0); /* unsigned */ + DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); + h = DUK_HTHREAD_GET_STRING(js_ctx->thr, stridx); + DUK_ASSERT(h != NULL); + + p = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h) + 1; + DUK_ASSERT(*(js_ctx->p - 1) == *(p - 1)); /* first character has been matched */ + + for (;;) { + x = *p; + if (x == 0) { + break; + } + y = duk__dec_get(js_ctx); + if (x != y) { + /* Catches EOF of JSON input. */ + goto syntax_error; + } + p++; + } + + return; + + syntax_error: + duk__dec_syntax_error(js_ctx); + DUK_UNREACHABLE(); +} + +DUK_LOCAL duk_small_int_t duk__dec_string_escape(duk_json_dec_ctx *js_ctx, duk_uint8_t **ext_p) { + duk_uint_fast32_t cp; + + /* EOF (-1) will be cast to an unsigned value first + * and then re-cast for the switch. In any case, it + * will match the default case (syntax error). + */ + cp = (duk_uint_fast32_t) duk__dec_get(js_ctx); + switch ((int) cp) { + case DUK_ASC_BACKSLASH: break; + case DUK_ASC_DOUBLEQUOTE: break; + case DUK_ASC_SLASH: break; + case DUK_ASC_LC_T: cp = 0x09; break; + case DUK_ASC_LC_N: cp = 0x0a; break; + case DUK_ASC_LC_R: cp = 0x0d; break; + case DUK_ASC_LC_F: cp = 0x0c; break; + case DUK_ASC_LC_B: cp = 0x08; break; + case DUK_ASC_LC_U: { + cp = duk__dec_decode_hex_escape(js_ctx, 4); + break; + } +#ifdef DUK_USE_JX + case DUK_ASC_UC_U: { + if (js_ctx->flag_ext_custom) { + cp = duk__dec_decode_hex_escape(js_ctx, 8); + } else { + return 1; /* syntax error */ + } + break; + } + case DUK_ASC_LC_X: { + if (js_ctx->flag_ext_custom) { + cp = duk__dec_decode_hex_escape(js_ctx, 2); + } else { + return 1; /* syntax error */ + } + break; + } +#endif /* DUK_USE_JX */ + default: + /* catches EOF (0x00) */ + return 1; /* syntax error */ + } + + DUK_RAW_WRITE_XUTF8(*ext_p, cp); + + return 0; +} + +DUK_LOCAL void duk__dec_string(duk_json_dec_ctx *js_ctx) { + duk_hthread *thr = js_ctx->thr; + duk_context *ctx = (duk_context *) thr; + duk_bufwriter_ctx bw_alloc; + duk_bufwriter_ctx *bw; + duk_uint8_t *q; + + /* '"' was eaten by caller */ + + /* Note that we currently parse -bytes-, not codepoints. + * All non-ASCII extended UTF-8 will encode to bytes >= 0x80, + * so they'll simply pass through (valid UTF-8 or not). + */ + + bw = &bw_alloc; + DUK_BW_INIT_PUSHBUF(js_ctx->thr, bw, DUK__JSON_DECSTR_BUFSIZE); + q = DUK_BW_GET_PTR(js_ctx->thr, bw); + +#if defined(DUK_USE_JSON_DECSTRING_FASTPATH) + for (;;) { + duk_small_uint_t safe; + duk_uint8_t b, x; + const duk_uint8_t *p; + + /* Select a safe loop count where no output checks are + * needed assuming we won't encounter escapes. Input + * bound checks are not necessary as a NUL (guaranteed) + * will cause a SyntaxError before we read out of bounds. + */ + + safe = DUK__JSON_DECSTR_CHUNKSIZE; + + /* Ensure space for 1:1 output plus one escape. */ + q = DUK_BW_ENSURE_RAW(js_ctx->thr, bw, safe + DUK_UNICODE_MAX_XUTF8_LENGTH, q); + + p = js_ctx->p; /* temp copy, write back for next loop */ + for (;;) { + if (safe == 0) { + js_ctx->p = p; + break; + } + safe--; + + /* End of input (NUL) goes through slow path and causes SyntaxError. */ + DUK_ASSERT(duk__json_decstr_lookup[0] == 0x00); + + b = *p++; + x = (duk_small_int_t) duk__json_decstr_lookup[b]; + if (DUK_LIKELY(x != 0)) { + /* Fast path, decode as is. */ + *q++ = b; + } else if (b == DUK_ASC_DOUBLEQUOTE) { + js_ctx->p = p; + goto found_quote; + } else if (b == DUK_ASC_BACKSLASH) { + /* We've ensured space for one escaped input; then + * bail out and recheck (this makes escape handling + * quite slow but it's uncommon). + */ + js_ctx->p = p; + if (duk__dec_string_escape(js_ctx, &q) != 0) { + goto syntax_error; + } + break; + } else { + js_ctx->p = p; + goto syntax_error; + } + } + } + found_quote: +#else /* DUK_USE_JSON_DECSTRING_FASTPATH */ + for (;;) { + duk_uint8_t x; + + q = DUK_BW_ENSURE_RAW(js_ctx->thr, bw, DUK_UNICODE_MAX_XUTF8_LENGTH, q); + + x = duk__dec_get(js_ctx); + + if (x == DUK_ASC_DOUBLEQUOTE) { + break; + } else if (x == DUK_ASC_BACKSLASH) { + if (duk__dec_string_escape(js_ctx, &q) != 0) { + goto syntax_error; + } + } else if (x < 0x20) { + /* catches EOF (NUL) */ + goto syntax_error; + } else { + *q++ = (duk_uint8_t) x; + } + } +#endif /* DUK_USE_JSON_DECSTRING_FASTPATH */ + + DUK_BW_SETPTR_AND_COMPACT(js_ctx->thr, bw, q); + duk_to_string(ctx, -1); + + /* [ ... str ] */ + + return; + + syntax_error: + duk__dec_syntax_error(js_ctx); + DUK_UNREACHABLE(); +} + +#ifdef DUK_USE_JX +/* Decode a plain string consisting entirely of identifier characters. + * Used to parse plain keys (e.g. "foo: 123"). + */ +DUK_LOCAL void duk__dec_plain_string(duk_json_dec_ctx *js_ctx) { + duk_hthread *thr = js_ctx->thr; + duk_context *ctx = (duk_context *) thr; + const duk_uint8_t *p; + duk_small_int_t x; + + /* Caller has already eaten the first char so backtrack one byte. */ + + js_ctx->p--; /* safe */ + p = js_ctx->p; + + /* Here again we parse bytes, and non-ASCII UTF-8 will cause end of + * parsing (which is correct except if there are non-shortest encodings). + * There is also no need to check explicitly for end of input buffer as + * the input is NUL padded and NUL will exit the parsing loop. + * + * Because no unescaping takes place, we can just scan to the end of the + * plain string and intern from the input buffer. + */ + + for (;;) { + x = *p; + + /* There is no need to check the first character specially here + * (i.e. reject digits): the caller only accepts valid initial + * characters and won't call us if the first character is a digit. + * This also ensures that the plain string won't be empty. + */ + + if (!duk_unicode_is_identifier_part((duk_codepoint_t) x)) { + break; + } + p++; + } + + duk_push_lstring(ctx, (const char *) js_ctx->p, (duk_size_t) (p - js_ctx->p)); + js_ctx->p = p; + + /* [ ... str ] */ +} +#endif /* DUK_USE_JX */ + +#ifdef DUK_USE_JX +DUK_LOCAL void duk__dec_pointer(duk_json_dec_ctx *js_ctx) { + duk_hthread *thr = js_ctx->thr; + duk_context *ctx = (duk_context *) thr; + const duk_uint8_t *p; + duk_small_int_t x; + void *voidptr; + + /* Caller has already eaten the first character ('(') which we don't need. */ + + p = js_ctx->p; + + for (;;) { + x = *p; + + /* Assume that the native representation never contains a closing + * parenthesis. + */ + + if (x == DUK_ASC_RPAREN) { + break; + } else if (x <= 0) { + /* NUL term or -1 (EOF), NUL check would suffice */ + goto syntax_error; + } + p++; + } + + /* There is no need to NUL delimit the sscanf() call: trailing garbage is + * ignored and there is always a NUL terminator which will force an error + * if no error is encountered before it. It's possible that the scan + * would scan further than between [js_ctx->p,p[ though and we'd advance + * by less than the scanned value. + * + * Because pointers are platform specific, a failure to scan a pointer + * results in a null pointer which is a better placeholder than a missing + * value or an error. + */ + + voidptr = NULL; + (void) DUK_SSCANF((const char *) js_ctx->p, DUK_STR_FMT_PTR, &voidptr); + duk_push_pointer(ctx, voidptr); + js_ctx->p = p + 1; /* skip ')' */ + + /* [ ... ptr ] */ + + return; + + syntax_error: + duk__dec_syntax_error(js_ctx); + DUK_UNREACHABLE(); +} +#endif /* DUK_USE_JX */ + +#ifdef DUK_USE_JX +DUK_LOCAL void duk__dec_buffer(duk_json_dec_ctx *js_ctx) { + duk_hthread *thr = js_ctx->thr; + duk_context *ctx = (duk_context *) thr; + const duk_uint8_t *p; + duk_uint8_t *buf; + duk_size_t src_len; + duk_small_int_t x; + + /* Caller has already eaten the first character ('|') which we don't need. */ + + p = js_ctx->p; + + /* XXX: Would be nice to share the fast path loop from duk_hex_decode() + * and avoid creating a temporary buffer. However, there are some + * differences which prevent trivial sharing: + * + * - Pipe char detection + * - EOF detection + * - Unknown length of input and output + * + * The best approach here would be a bufwriter and a reasonaly sized + * safe inner loop (e.g. 64 output bytes at a time). + */ + + for (;;) { + x = *p; + + /* This loop intentionally does not ensure characters are valid + * ([0-9a-fA-F]) because the hex decode call below will do that. + */ + if (x == DUK_ASC_PIPE) { + break; + } else if (x <= 0) { + /* NUL term or -1 (EOF), NUL check would suffice */ + goto syntax_error; + } + p++; + } + + src_len = (duk_size_t) (p - js_ctx->p); + buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, src_len); + DUK_ASSERT(buf != NULL); + DUK_MEMCPY((void *) buf, (const void *) js_ctx->p, src_len); + duk_hex_decode(ctx, -1); + + js_ctx->p = p + 1; /* skip '|' */ + + /* [ ... buf ] */ + + return; + + syntax_error: + duk__dec_syntax_error(js_ctx); + DUK_UNREACHABLE(); +} +#endif /* DUK_USE_JX */ + +/* Parse a number, other than NaN or +/- Infinity */ +DUK_LOCAL void duk__dec_number(duk_json_dec_ctx *js_ctx) { + duk_context *ctx = (duk_context *) js_ctx->thr; + const duk_uint8_t *p_start; + const duk_uint8_t *p; + duk_uint8_t x; + duk_small_uint_t s2n_flags; + + DUK_DDD(DUK_DDDPRINT("parse_number")); + + p_start = js_ctx->p; + + /* First pass parse is very lenient (e.g. allows '1.2.3') and extracts a + * string for strict number parsing. + */ + + p = js_ctx->p; + for (;;) { + x = *p; + + DUK_DDD(DUK_DDDPRINT("parse_number: p_start=%p, p=%p, p_end=%p, x=%ld", + (const void *) p_start, (const void *) p, + (const void *) js_ctx->p_end, (long) x)); + +#if defined(DUK_USE_JSON_DECNUMBER_FASTPATH) + /* This fast path is pretty marginal in practice. + * XXX: candidate for removal. + */ + DUK_ASSERT(duk__json_decnumber_lookup[0x00] == 0x00); /* end-of-input breaks */ + if (duk__json_decnumber_lookup[x] == 0) { + break; + } +#else /* DUK_USE_JSON_DECNUMBER_FASTPATH */ + if (!((x >= DUK_ASC_0 && x <= DUK_ASC_9) || + (x == DUK_ASC_PERIOD || x == DUK_ASC_LC_E || + x == DUK_ASC_UC_E || x == DUK_ASC_MINUS || x == DUK_ASC_PLUS))) { + /* Plus sign must be accepted for positive exponents + * (e.g. '1.5e+2'). This clause catches NULs. + */ + break; + } +#endif /* DUK_USE_JSON_DECNUMBER_FASTPATH */ + p++; /* safe, because matched (NUL causes a break) */ + } + js_ctx->p = p; + + DUK_ASSERT(js_ctx->p > p_start); + duk_push_lstring(ctx, (const char *) p_start, (duk_size_t) (p - p_start)); + + s2n_flags = DUK_S2N_FLAG_ALLOW_EXP | + DUK_S2N_FLAG_ALLOW_MINUS | /* but don't allow leading plus */ + DUK_S2N_FLAG_ALLOW_FRAC; + + DUK_DDD(DUK_DDDPRINT("parse_number: string before parsing: %!T", + (duk_tval *) duk_get_tval(ctx, -1))); + duk_numconv_parse(ctx, 10 /*radix*/, s2n_flags); + if (duk_is_nan(ctx, -1)) { + duk__dec_syntax_error(js_ctx); + } + DUK_ASSERT(duk_is_number(ctx, -1)); + DUK_DDD(DUK_DDDPRINT("parse_number: final number: %!T", + (duk_tval *) duk_get_tval(ctx, -1))); + + /* [ ... num ] */ +} + +DUK_LOCAL void duk__dec_objarr_entry(duk_json_dec_ctx *js_ctx) { + duk_context *ctx = (duk_context *) js_ctx->thr; + duk_require_stack(ctx, DUK_JSON_DEC_REQSTACK); + + /* c recursion check */ + + DUK_ASSERT(js_ctx->recursion_depth >= 0); + DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit); + if (js_ctx->recursion_depth >= js_ctx->recursion_limit) { + DUK_ERROR_RANGE((duk_hthread *) ctx, DUK_STR_JSONDEC_RECLIMIT); + } + js_ctx->recursion_depth++; +} + +DUK_LOCAL void duk__dec_objarr_exit(duk_json_dec_ctx *js_ctx) { + /* c recursion check */ + + DUK_ASSERT(js_ctx->recursion_depth > 0); + DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit); + js_ctx->recursion_depth--; +} + +DUK_LOCAL void duk__dec_object(duk_json_dec_ctx *js_ctx) { + duk_context *ctx = (duk_context *) js_ctx->thr; + duk_int_t key_count; /* XXX: a "first" flag would suffice */ + duk_uint8_t x; + + DUK_DDD(DUK_DDDPRINT("parse_object")); + + duk__dec_objarr_entry(js_ctx); + + duk_push_object(ctx); + + /* Initial '{' has been checked and eaten by caller. */ + + key_count = 0; + for (;;) { + x = duk__dec_get_nonwhite(js_ctx); + + DUK_DDD(DUK_DDDPRINT("parse_object: obj=%!T, x=%ld, key_count=%ld", + (duk_tval *) duk_get_tval(ctx, -1), + (long) x, (long) key_count)); + + /* handle comma and closing brace */ + + if (x == DUK_ASC_COMMA && key_count > 0) { + /* accept comma, expect new value */ + x = duk__dec_get_nonwhite(js_ctx); + } else if (x == DUK_ASC_RCURLY) { + /* eat closing brace */ + break; + } else if (key_count == 0) { + /* accept anything, expect first value (EOF will be + * caught by key parsing below. + */ + ; + } else { + /* catches EOF (NUL) and initial comma */ + goto syntax_error; + } + + /* parse key and value */ + + if (x == DUK_ASC_DOUBLEQUOTE) { + duk__dec_string(js_ctx); +#ifdef DUK_USE_JX + } else if (js_ctx->flag_ext_custom && + duk_unicode_is_identifier_start((duk_codepoint_t) x)) { + duk__dec_plain_string(js_ctx); +#endif + } else { + goto syntax_error; + } + + /* [ ... obj key ] */ + + x = duk__dec_get_nonwhite(js_ctx); + if (x != DUK_ASC_COLON) { + goto syntax_error; + } + + duk__dec_value(js_ctx); + + /* [ ... obj key val ] */ + + duk_xdef_prop_wec(ctx, -3); + + /* [ ... obj ] */ + + key_count++; + } + + /* [ ... obj ] */ + + DUK_DDD(DUK_DDDPRINT("parse_object: final object is %!T", + (duk_tval *) duk_get_tval(ctx, -1))); + + duk__dec_objarr_exit(js_ctx); + return; + + syntax_error: + duk__dec_syntax_error(js_ctx); + DUK_UNREACHABLE(); +} + +DUK_LOCAL void duk__dec_array(duk_json_dec_ctx *js_ctx) { + duk_context *ctx = (duk_context *) js_ctx->thr; + duk_uarridx_t arr_idx; + duk_uint8_t x; + + DUK_DDD(DUK_DDDPRINT("parse_array")); + + duk__dec_objarr_entry(js_ctx); + + duk_push_array(ctx); + + /* Initial '[' has been checked and eaten by caller. */ + + arr_idx = 0; + for (;;) { + x = duk__dec_get_nonwhite(js_ctx); + + DUK_DDD(DUK_DDDPRINT("parse_array: arr=%!T, x=%ld, arr_idx=%ld", + (duk_tval *) duk_get_tval(ctx, -1), + (long) x, (long) arr_idx)); + + /* handle comma and closing bracket */ + + if ((x == DUK_ASC_COMMA) && (arr_idx != 0)) { + /* accept comma, expect new value */ + ; + } else if (x == DUK_ASC_RBRACKET) { + /* eat closing bracket */ + break; + } else if (arr_idx == 0) { + /* accept anything, expect first value (EOF will be + * caught by duk__dec_value() below. + */ + js_ctx->p--; /* backtrack (safe) */ + } else { + /* catches EOF (NUL) and initial comma */ + goto syntax_error; + } + + /* parse value */ + + duk__dec_value(js_ctx); + + /* [ ... arr val ] */ + + duk_xdef_prop_index_wec(ctx, -2, arr_idx); + arr_idx++; + } + + /* Must set 'length' explicitly when using duk_xdef_prop_xxx() to + * set the values. + */ + + duk_set_length(ctx, -1, arr_idx); + + /* [ ... arr ] */ + + DUK_DDD(DUK_DDDPRINT("parse_array: final array is %!T", + (duk_tval *) duk_get_tval(ctx, -1))); + + duk__dec_objarr_exit(js_ctx); + return; + + syntax_error: + duk__dec_syntax_error(js_ctx); + DUK_UNREACHABLE(); +} + +DUK_LOCAL void duk__dec_value(duk_json_dec_ctx *js_ctx) { + duk_context *ctx = (duk_context *) js_ctx->thr; + duk_uint8_t x; + + x = duk__dec_get_nonwhite(js_ctx); + + DUK_DDD(DUK_DDDPRINT("parse_value: initial x=%ld", (long) x)); + + /* Note: duk__dec_req_stridx() backtracks one char */ + + if (x == DUK_ASC_DOUBLEQUOTE) { + duk__dec_string(js_ctx); + } else if ((x >= DUK_ASC_0 && x <= DUK_ASC_9) || (x == DUK_ASC_MINUS)) { +#ifdef DUK_USE_JX + if (js_ctx->flag_ext_custom && x == DUK_ASC_MINUS && duk__dec_peek(js_ctx) == DUK_ASC_UC_I) { + duk__dec_req_stridx(js_ctx, DUK_STRIDX_MINUS_INFINITY); /* "-Infinity", '-' has been eaten */ + duk_push_number(ctx, -DUK_DOUBLE_INFINITY); + } else { +#else + { /* unconditional block */ +#endif + /* We already ate 'x', so backup one byte. */ + js_ctx->p--; /* safe */ + duk__dec_number(js_ctx); + } + } else if (x == DUK_ASC_LC_T) { + duk__dec_req_stridx(js_ctx, DUK_STRIDX_TRUE); + duk_push_true(ctx); + } else if (x == DUK_ASC_LC_F) { + duk__dec_req_stridx(js_ctx, DUK_STRIDX_FALSE); + duk_push_false(ctx); + } else if (x == DUK_ASC_LC_N) { + duk__dec_req_stridx(js_ctx, DUK_STRIDX_LC_NULL); + duk_push_null(ctx); +#ifdef DUK_USE_JX + } else if (js_ctx->flag_ext_custom && x == DUK_ASC_LC_U) { + duk__dec_req_stridx(js_ctx, DUK_STRIDX_LC_UNDEFINED); + duk_push_undefined(ctx); + } else if (js_ctx->flag_ext_custom && x == DUK_ASC_UC_N) { + duk__dec_req_stridx(js_ctx, DUK_STRIDX_NAN); + duk_push_nan(ctx); + } else if (js_ctx->flag_ext_custom && x == DUK_ASC_UC_I) { + duk__dec_req_stridx(js_ctx, DUK_STRIDX_INFINITY); + duk_push_number(ctx, DUK_DOUBLE_INFINITY); + } else if (js_ctx->flag_ext_custom && x == DUK_ASC_LPAREN) { + duk__dec_pointer(js_ctx); + } else if (js_ctx->flag_ext_custom && x == DUK_ASC_PIPE) { + duk__dec_buffer(js_ctx); +#endif + } else if (x == DUK_ASC_LCURLY) { + duk__dec_object(js_ctx); + } else if (x == DUK_ASC_LBRACKET) { + duk__dec_array(js_ctx); + } else { + /* catches EOF (NUL) */ + goto syntax_error; + } + + duk__dec_eat_white(js_ctx); + + /* [ ... val ] */ + return; + + syntax_error: + duk__dec_syntax_error(js_ctx); + DUK_UNREACHABLE(); +} + +/* Recursive value reviver, implements the Walk() algorithm. No C recursion + * check is done here because the initial parsing step will already ensure + * there is a reasonable limit on C recursion depth and hence object depth. + */ +DUK_LOCAL void duk__dec_reviver_walk(duk_json_dec_ctx *js_ctx) { + duk_context *ctx = (duk_context *) js_ctx->thr; + duk_hobject *h; + duk_uarridx_t i, arr_len; + + DUK_DDD(DUK_DDDPRINT("walk: top=%ld, holder=%!T, name=%!T", + (long) duk_get_top(ctx), + (duk_tval *) duk_get_tval(ctx, -2), + (duk_tval *) duk_get_tval(ctx, -1))); + + duk_dup_top(ctx); + duk_get_prop(ctx, -3); /* -> [ ... holder name val ] */ + + h = duk_get_hobject(ctx, -1); + if (h != NULL) { + if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY) { + arr_len = (duk_uarridx_t) duk_get_length(ctx, -1); + for (i = 0; i < arr_len; i++) { + /* [ ... holder name val ] */ + + DUK_DDD(DUK_DDDPRINT("walk: array, top=%ld, i=%ld, arr_len=%ld, holder=%!T, name=%!T, val=%!T", + (long) duk_get_top(ctx), (long) i, (long) arr_len, + (duk_tval *) duk_get_tval(ctx, -3), (duk_tval *) duk_get_tval(ctx, -2), + (duk_tval *) duk_get_tval(ctx, -1))); + + /* XXX: push_uint_string / push_u32_string */ + duk_dup_top(ctx); + duk_push_uint(ctx, (duk_uint_t) i); + duk_to_string(ctx, -1); /* -> [ ... holder name val val ToString(i) ] */ + duk__dec_reviver_walk(js_ctx); /* -> [ ... holder name val new_elem ] */ + + if (duk_is_undefined(ctx, -1)) { + duk_pop(ctx); + duk_del_prop_index(ctx, -1, i); + } else { + /* XXX: duk_xdef_prop_index_wec() would be more appropriate + * here but it currently makes some assumptions that might + * not hold (e.g. that previous property is not an accessor). + */ + duk_put_prop_index(ctx, -2, i); + } + } + } else { + /* [ ... holder name val ] */ + duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY /*flags*/); + while (duk_next(ctx, -1 /*enum_index*/, 0 /*get_value*/)) { + DUK_DDD(DUK_DDDPRINT("walk: object, top=%ld, holder=%!T, name=%!T, val=%!T, enum=%!iT, obj_key=%!T", + (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, -5), + (duk_tval *) duk_get_tval(ctx, -4), (duk_tval *) duk_get_tval(ctx, -3), + (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1))); + + /* [ ... holder name val enum obj_key ] */ + duk_dup(ctx, -3); + duk_dup(ctx, -2); + + /* [ ... holder name val enum obj_key val obj_key ] */ + duk__dec_reviver_walk(js_ctx); + + /* [ ... holder name val enum obj_key new_elem ] */ + if (duk_is_undefined(ctx, -1)) { + duk_pop(ctx); + duk_del_prop(ctx, -3); + } else { + /* XXX: duk_xdef_prop_index_wec() would be more appropriate + * here but it currently makes some assumptions that might + * not hold (e.g. that previous property is not an accessor). + * + * Using duk_put_prop() works incorrectly with '__proto__' + * if the own property with that name has been deleted. This + * does not happen normally, but a clever reviver can trigger + * that, see complex reviver case in: test-bug-json-parse-__proto__.js. + */ + duk_put_prop(ctx, -4); + } + } + duk_pop(ctx); /* pop enum */ + } + } + + /* [ ... holder name val ] */ + + duk_dup(ctx, js_ctx->idx_reviver); + duk_insert(ctx, -4); /* -> [ ... reviver holder name val ] */ + duk_call_method(ctx, 2); /* -> [ ... res ] */ + + DUK_DDD(DUK_DDDPRINT("walk: top=%ld, result=%!T", + (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, -1))); +} + +/* + * Stringify implementation. + */ + +#define DUK__EMIT_1(js_ctx,ch) duk__emit_1((js_ctx), (duk_uint_fast8_t) (ch)) +#define DUK__EMIT_2(js_ctx,ch1,ch2) duk__emit_2((js_ctx), (duk_uint_fast8_t) (ch1), (duk_uint_fast8_t) (ch2)) +#define DUK__EMIT_HSTR(js_ctx,h) duk__emit_hstring((js_ctx), (h)) +#if defined(DUK_USE_FASTINT) || defined(DUK_USE_JX) || defined(DUK_USE_JC) +#define DUK__EMIT_CSTR(js_ctx,p) duk__emit_cstring((js_ctx), (p)) +#endif +#define DUK__EMIT_STRIDX(js_ctx,i) duk__emit_stridx((js_ctx), (i)) +#define DUK__UNEMIT_1(js_ctx) duk__unemit_1((js_ctx)) + +DUK_LOCAL void duk__emit_1(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch) { + DUK_BW_WRITE_ENSURE_U8(js_ctx->thr, &js_ctx->bw, ch); +} + +DUK_LOCAL void duk__emit_2(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch1, duk_uint_fast8_t ch2) { + DUK_BW_WRITE_ENSURE_U8_2(js_ctx->thr, &js_ctx->bw, ch1, ch2); +} + +DUK_LOCAL void duk__emit_hstring(duk_json_enc_ctx *js_ctx, duk_hstring *h) { + DUK_BW_WRITE_ENSURE_HSTRING(js_ctx->thr, &js_ctx->bw, h); +} + +#if defined(DUK_USE_FASTINT) || defined(DUK_USE_JX) || defined(DUK_USE_JC) +DUK_LOCAL void duk__emit_cstring(duk_json_enc_ctx *js_ctx, const char *str) { + DUK_BW_WRITE_ENSURE_CSTRING(js_ctx->thr, &js_ctx->bw, str); +} +#endif + +DUK_LOCAL void duk__emit_stridx(duk_json_enc_ctx *js_ctx, duk_small_uint_t stridx) { + duk_hstring *h; + + DUK_ASSERT_DISABLE(stridx >= 0); /* unsigned */ + DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS); + h = DUK_HTHREAD_GET_STRING(js_ctx->thr, stridx); + DUK_ASSERT(h != NULL); + + DUK_BW_WRITE_ENSURE_HSTRING(js_ctx->thr, &js_ctx->bw, h); +} + +DUK_LOCAL void duk__unemit_1(duk_json_enc_ctx *js_ctx) { + DUK_ASSERT(DUK_BW_GET_SIZE(js_ctx->thr, &js_ctx->bw) >= 1); + DUK_BW_ADD_PTR(js_ctx->thr, &js_ctx->bw, -1); +} + +#define DUK__MKESC(nybbles,esc1,esc2) \ + (((duk_uint_fast32_t) (nybbles)) << 16) | \ + (((duk_uint_fast32_t) (esc1)) << 8) | \ + ((duk_uint_fast32_t) (esc2)) + +DUK_LOCAL duk_uint8_t *duk__emit_esc_auto_fast(duk_json_enc_ctx *js_ctx, duk_uint_fast32_t cp, duk_uint8_t *q) { + duk_uint_fast32_t tmp; + duk_small_uint_t dig; + + DUK_UNREF(js_ctx); + + /* Caller ensures space for at least DUK__JSON_MAX_ESC_LEN. */ + + /* Select appropriate escape format automatically, and set 'tmp' to a + * value encoding both the escape format character and the nybble count: + * + * (nybble_count << 16) | (escape_char1) | (escape_char2) + */ + +#ifdef DUK_USE_JX + if (DUK_LIKELY(cp < 0x100UL)) { + if (DUK_UNLIKELY(js_ctx->flag_ext_custom)) { + tmp = DUK__MKESC(2, DUK_ASC_BACKSLASH, DUK_ASC_LC_X); + } else { + tmp = DUK__MKESC(4, DUK_ASC_BACKSLASH, DUK_ASC_LC_U); + } + } else +#endif + if (DUK_LIKELY(cp < 0x10000UL)) { + tmp = DUK__MKESC(4, DUK_ASC_BACKSLASH, DUK_ASC_LC_U); + } else { +#ifdef DUK_USE_JX + if (DUK_LIKELY(js_ctx->flag_ext_custom)) { + tmp = DUK__MKESC(8, DUK_ASC_BACKSLASH, DUK_ASC_UC_U); + } else +#endif + { + /* In compatible mode and standard JSON mode, output + * something useful for non-BMP characters. This won't + * roundtrip but will still be more or less readable and + * more useful than an error. + */ + tmp = DUK__MKESC(8, DUK_ASC_UC_U, DUK_ASC_PLUS); + } + } + + *q++ = (duk_uint8_t) ((tmp >> 8) & 0xff); + *q++ = (duk_uint8_t) (tmp & 0xff); + + tmp = tmp >> 16; + while (tmp > 0) { + tmp--; + dig = (duk_small_uint_t) ((cp >> (4 * tmp)) & 0x0f); + *q++ = duk_lc_digits[dig]; + } + + return q; +} + +DUK_LOCAL void duk__enc_key_autoquote(duk_json_enc_ctx *js_ctx, duk_hstring *k) { + const duk_int8_t *p, *p_start, *p_end; /* Note: intentionally signed. */ + duk_size_t k_len; + duk_codepoint_t cp; + + DUK_ASSERT(k != NULL); + + /* Accept ASCII strings which conform to identifier requirements + * as being emitted without key quotes. Since we only accept ASCII + * there's no need for actual decoding: 'p' is intentionally signed + * so that bytes >= 0x80 extend to negative values and are rejected + * as invalid identifier codepoints. + */ + + if (js_ctx->flag_avoid_key_quotes) { + k_len = DUK_HSTRING_GET_BYTELEN(k); + p_start = (const duk_int8_t *) DUK_HSTRING_GET_DATA(k); + p_end = p_start + k_len; + p = p_start; + + if (p == p_end) { + /* Zero length string is not accepted without quotes */ + goto quote_normally; + } + cp = (duk_codepoint_t) (*p++); + if (DUK_UNLIKELY(!duk_unicode_is_identifier_start(cp))) { + goto quote_normally; + } + while (p < p_end) { + cp = (duk_codepoint_t) (*p++); + if (DUK_UNLIKELY(!duk_unicode_is_identifier_part(cp))) { + goto quote_normally; + } + } + + /* This seems faster than emitting bytes one at a time and + * then potentially rewinding. + */ + DUK__EMIT_HSTR(js_ctx, k); + return; + } + + quote_normally: + duk__enc_quote_string(js_ctx, k); +} + +/* The Quote(value) operation: quote a string. + * + * Stack policy: [ ] -> [ ]. + */ + +DUK_LOCAL void duk__enc_quote_string(duk_json_enc_ctx *js_ctx, duk_hstring *h_str) { + duk_hthread *thr = js_ctx->thr; + const duk_uint8_t *p, *p_start, *p_end, *p_now, *p_tmp; + duk_uint8_t *q; + duk_ucodepoint_t cp; /* typed for duk_unicode_decode_xutf8() */ + + DUK_DDD(DUK_DDDPRINT("duk__enc_quote_string: h_str=%!O", (duk_heaphdr *) h_str)); + + DUK_ASSERT(h_str != NULL); + p_start = DUK_HSTRING_GET_DATA(h_str); + p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_str); + p = p_start; + + DUK__EMIT_1(js_ctx, DUK_ASC_DOUBLEQUOTE); + + /* Encode string in small chunks, estimating the maximum expansion so that + * there's no need to ensure space while processing the chunk. + */ + + while (p < p_end) { + duk_size_t left, now, space; + + left = (duk_size_t) (p_end - p); + now = (left > DUK__JSON_ENCSTR_CHUNKSIZE ? + DUK__JSON_ENCSTR_CHUNKSIZE : left); + + /* Maximum expansion per input byte is 6: + * - invalid UTF-8 byte causes "\uXXXX" to be emitted (6/1 = 6). + * - 2-byte UTF-8 encodes as "\uXXXX" (6/2 = 3). + * - 4-byte UTF-8 encodes as "\Uxxxxxxxx" (10/4 = 2.5). + */ + space = now * 6; + q = DUK_BW_ENSURE_GETPTR(thr, &js_ctx->bw, space); + + p_now = p + now; + + while (p < p_now) { +#if defined(DUK_USE_JSON_QUOTESTRING_FASTPATH) + duk_uint8_t b; + + b = duk__json_quotestr_lookup[*p++]; + if (DUK_LIKELY(b < 0x80)) { + /* Most input bytes go through here. */ + *q++ = b; + } else if (b >= 0xa0) { + *q++ = DUK_ASC_BACKSLASH; + *q++ = (duk_uint8_t) (b - 0x80); + } else if (b == 0x80) { + cp = (duk_ucodepoint_t) (*(p - 1)); + q = duk__emit_esc_auto_fast(js_ctx, cp, q); + } else if (b == 0x7f && js_ctx->flag_ascii_only) { + /* 0x7F is special */ + DUK_ASSERT(b == 0x81); + cp = (duk_ucodepoint_t) 0x7f; + q = duk__emit_esc_auto_fast(js_ctx, cp, q); + } else { + DUK_ASSERT(b == 0x81); + p--; + + /* slow path is shared */ +#else /* DUK_USE_JSON_QUOTESTRING_FASTPATH */ + cp = *p; + + if (DUK_LIKELY(cp <= 0x7f)) { + /* ascii fast path: avoid decoding utf-8 */ + p++; + if (cp == 0x22 || cp == 0x5c) { + /* double quote or backslash */ + *q++ = DUK_ASC_BACKSLASH; + *q++ = (duk_uint8_t) cp; + } else if (cp < 0x20) { + duk_uint_fast8_t esc_char; + + /* This approach is a bit shorter than a straight + * if-else-ladder and also a bit faster. + */ + if (cp < (sizeof(duk__json_quotestr_esc) / sizeof(duk_uint8_t)) && + (esc_char = duk__json_quotestr_esc[cp]) != 0) { + *q++ = DUK_ASC_BACKSLASH; + *q++ = (duk_uint8_t) esc_char; + } else { + q = duk__emit_esc_auto_fast(js_ctx, cp, q); + } + } else if (cp == 0x7f && js_ctx->flag_ascii_only) { + q = duk__emit_esc_auto_fast(js_ctx, cp, q); + } else { + /* any other printable -> as is */ + *q++ = (duk_uint8_t) cp; + } + } else { + /* slow path is shared */ +#endif /* DUK_USE_JSON_QUOTESTRING_FASTPATH */ + + /* slow path decode */ + + /* If XUTF-8 decoding fails, treat the offending byte as a codepoint directly + * and go forward one byte. This is of course very lossy, but allows some kind + * of output to be produced even for internal strings which don't conform to + * XUTF-8. All standard Ecmascript strings are always CESU-8, so this behavior + * does not violate the Ecmascript specification. The behavior is applied to + * all modes, including Ecmascript standard JSON. Because the current XUTF-8 + * decoding is not very strict, this behavior only really affects initial bytes + * and truncated codepoints. + * + * Another alternative would be to scan forwards to start of next codepoint + * (or end of input) and emit just one replacement codepoint. + */ + + p_tmp = p; + if (!duk_unicode_decode_xutf8(thr, &p, p_start, p_end, &cp)) { + /* Decode failed. */ + cp = *p_tmp; + p = p_tmp + 1; + } + +#ifdef DUK_USE_NONSTD_JSON_ESC_U2028_U2029 + if (js_ctx->flag_ascii_only || cp == 0x2028 || cp == 0x2029) { +#else + if (js_ctx->flag_ascii_only) { +#endif + q = duk__emit_esc_auto_fast(js_ctx, cp, q); + } else { + /* as is */ + DUK_RAW_WRITE_XUTF8(q, cp); + } + } + } + + DUK_BW_SET_PTR(thr, &js_ctx->bw, q); + } + + DUK__EMIT_1(js_ctx, DUK_ASC_DOUBLEQUOTE); +} + +/* Encode a double (checked by caller) from stack top. Stack top may be + * replaced by serialized string but is not popped (caller does that). + */ +DUK_LOCAL void duk__enc_double(duk_json_enc_ctx *js_ctx) { + duk_hthread *thr; + duk_context *ctx; + duk_tval *tv; + duk_double_t d; + duk_small_int_t c; + duk_small_int_t s; + duk_small_uint_t stridx; + duk_small_uint_t n2s_flags; + duk_hstring *h_str; + + DUK_ASSERT(js_ctx != NULL); + thr = js_ctx->thr; + DUK_ASSERT(thr != NULL); + ctx = (duk_context *) thr; + + /* Caller must ensure 'tv' is indeed a double and not a fastint! */ + tv = DUK_GET_TVAL_NEGIDX(ctx, -1); + DUK_ASSERT(DUK_TVAL_IS_DOUBLE(tv)); + d = DUK_TVAL_GET_DOUBLE(tv); + + c = (duk_small_int_t) DUK_FPCLASSIFY(d); + s = (duk_small_int_t) DUK_SIGNBIT(d); + DUK_UNREF(s); + + if (DUK_LIKELY(!(c == DUK_FP_INFINITE || c == DUK_FP_NAN))) { + DUK_ASSERT(DUK_ISFINITE(d)); + +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + /* Negative zero needs special handling in JX/JC because + * it would otherwise serialize to '0', not '-0'. + */ + if (DUK_UNLIKELY(c == DUK_FP_ZERO && s != 0 && + (js_ctx->flag_ext_custom_or_compatible))) { + duk_push_hstring_stridx(ctx, DUK_STRIDX_MINUS_ZERO); /* '-0' */ + } else +#endif /* DUK_USE_JX || DUK_USE_JC */ + { + n2s_flags = 0; + /* [ ... number ] -> [ ... string ] */ + duk_numconv_stringify(ctx, 10 /*radix*/, 0 /*digits*/, n2s_flags); + } + h_str = duk_to_hstring(ctx, -1); + DUK_ASSERT(h_str != NULL); + DUK__EMIT_HSTR(js_ctx, h_str); + return; + } + +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + if (!(js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM | + DUK_JSON_FLAG_EXT_COMPATIBLE))) { + stridx = DUK_STRIDX_LC_NULL; + } else if (c == DUK_FP_NAN) { + stridx = js_ctx->stridx_custom_nan; + } else if (s == 0) { + stridx = js_ctx->stridx_custom_posinf; + } else { + stridx = js_ctx->stridx_custom_neginf; + } +#else + stridx = DUK_STRIDX_LC_NULL; +#endif + DUK__EMIT_STRIDX(js_ctx, stridx); +} + +#if defined(DUK_USE_FASTINT) +/* Encode a fastint from duk_tval ptr, no value stack effects. */ +DUK_LOCAL void duk__enc_fastint_tval(duk_json_enc_ctx *js_ctx, duk_tval *tv) { + duk_int64_t v; + + /* Fastint range is signed 48-bit so longest value is -2^47 = -140737488355328 + * (16 chars long), longest signed 64-bit value is -2^63 = -9223372036854775808 + * (20 chars long). Alloc space for 64-bit range to be safe. + */ + duk_uint8_t buf[20 + 1]; + + /* Caller must ensure 'tv' is indeed a fastint! */ + DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv)); + v = DUK_TVAL_GET_FASTINT(tv); + + /* XXX: There are no format strings in duk_config.h yet, could add + * one for formatting duk_int64_t. For now, assumes "%lld" and that + * "long long" type exists. Could also rely on C99 directly but that + * won't work for older MSVC. + */ + DUK_SPRINTF((char *) buf, "%lld", (long long) v); + DUK__EMIT_CSTR(js_ctx, (const char *) buf); +} +#endif + +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) +#if defined(DUK_USE_HEX_FASTPATH) +DUK_LOCAL duk_uint8_t *duk__enc_buffer_data_hex(const duk_uint8_t *src, duk_size_t src_len, duk_uint8_t *dst) { + duk_uint8_t *q; + duk_uint16_t *q16; + duk_small_uint_t x; + duk_size_t i, len_safe; +#if !defined(DUK_USE_UNALIGNED_ACCESSES_POSSIBLE) + duk_bool_t shift_dst; +#endif + + /* Unlike in duk_hex_encode() 'dst' is not necessarily aligned by 2. + * For platforms where unaligned accesses are not allowed, shift 'dst' + * ahead by 1 byte to get alignment and then DUK_MEMMOVE() the result + * in place. The faster encoding loop makes up the difference. + * There's always space for one extra byte because a terminator always + * follows the hex data and that's been accounted for by the caller. + */ + +#if defined(DUK_USE_UNALIGNED_ACCESSES_POSSIBLE) + q16 = (duk_uint16_t *) (void *) dst; +#else + shift_dst = (duk_bool_t) (((duk_size_t) dst) & 0x01U); + if (shift_dst) { + DUK_DD(DUK_DDPRINT("unaligned accesses not possible, dst not aligned -> step to dst + 1")); + q16 = (duk_uint16_t *) (void *) (dst + 1); + } else { + DUK_DD(DUK_DDPRINT("unaligned accesses not possible, dst is aligned")); + q16 = (duk_uint16_t *) (void *) dst; + } + DUK_ASSERT((((duk_size_t) q16) & 0x01U) == 0); +#endif + + len_safe = src_len & ~0x03U; + for (i = 0; i < len_safe; i += 4) { + q16[0] = duk_hex_enctab[src[i]]; + q16[1] = duk_hex_enctab[src[i + 1]]; + q16[2] = duk_hex_enctab[src[i + 2]]; + q16[3] = duk_hex_enctab[src[i + 3]]; + q16 += 4; + } + q = (duk_uint8_t *) q16; + +#if !defined(DUK_USE_UNALIGNED_ACCESSES_POSSIBLE) + if (shift_dst) { + q--; + DUK_MEMMOVE((void *) dst, (const void *) (dst + 1), 2 * len_safe); + DUK_ASSERT(dst + 2 * len_safe == q); + } +#endif + + for (; i < src_len; i++) { + x = src[i]; + *q++ = duk_lc_digits[x >> 4]; + *q++ = duk_lc_digits[x & 0x0f]; + } + + return q; +} +#else /* DUK_USE_HEX_FASTPATH */ +DUK_LOCAL duk_uint8_t *duk__enc_buffer_data_hex(const duk_uint8_t *src, duk_size_t src_len, duk_uint8_t *dst) { + const duk_uint8_t *p; + const duk_uint8_t *p_end; + duk_uint8_t *q; + duk_small_uint_t x; + + p = src; + p_end = src + src_len; + q = dst; + while (p != p_end) { + x = *p++; + *q++ = duk_lc_digits[x >> 4]; + *q++ = duk_lc_digits[x & 0x0f]; + } + + return q; +} +#endif /* DUK_USE_HEX_FASTPATH */ + +DUK_LOCAL void duk__enc_buffer_data(duk_json_enc_ctx *js_ctx, duk_uint8_t *buf_data, duk_size_t buf_len) { + duk_hthread *thr; + duk_uint8_t *q; + duk_size_t space; + + thr = js_ctx->thr; + + DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible); /* caller checks */ + DUK_ASSERT(js_ctx->flag_ext_custom_or_compatible); + + /* Buffer values are encoded in (lowercase) hex to make the + * binary data readable. Base64 or similar would be more + * compact but less readable, and the point of JX/JC + * variants is to be as useful to a programmer as possible. + */ + + /* The #ifdef clutter here needs to handle the three cases: + * (1) JX+JC, (2) JX only, (3) JC only. + */ + + /* Note: space must cater for both JX and JC. */ + space = 9 + buf_len * 2 + 2; + DUK_ASSERT(DUK_HBUFFER_MAX_BYTELEN <= 0x7ffffffeUL); + DUK_ASSERT((space - 2) / 2 >= buf_len); /* overflow not possible, buffer limits */ + q = DUK_BW_ENSURE_GETPTR(thr, &js_ctx->bw, space); + +#if defined(DUK_USE_JX) && defined(DUK_USE_JC) + if (js_ctx->flag_ext_custom) +#endif +#if defined(DUK_USE_JX) + { + *q++ = DUK_ASC_PIPE; + q = duk__enc_buffer_data_hex(buf_data, buf_len, q); + *q++ = DUK_ASC_PIPE; + + } +#endif +#if defined(DUK_USE_JX) && defined(DUK_USE_JC) + else +#endif +#if defined(DUK_USE_JC) + { + DUK_ASSERT(js_ctx->flag_ext_compatible); + DUK_MEMCPY((void *) q, (const void *) "{\"_buf\":\"", 9); /* len: 9 */ + q += 9; + q = duk__enc_buffer_data_hex(buf_data, buf_len, q); + *q++ = DUK_ASC_DOUBLEQUOTE; + *q++ = DUK_ASC_RCURLY; + } +#endif + + DUK_BW_SET_PTR(thr, &js_ctx->bw, q); +} + +DUK_LOCAL void duk__enc_buffer(duk_json_enc_ctx *js_ctx, duk_hbuffer *h) { + duk__enc_buffer_data(js_ctx, + (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(js_ctx->thr->heap, h), + (duk_size_t) DUK_HBUFFER_GET_SIZE(h)); +} +#endif /* DUK_USE_JX || DUK_USE_JC */ + +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) +DUK_LOCAL void duk__enc_pointer(duk_json_enc_ctx *js_ctx, void *ptr) { + char buf[64]; /* XXX: how to figure correct size? */ + const char *fmt; + + DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible); /* caller checks */ + DUK_ASSERT(js_ctx->flag_ext_custom_or_compatible); + + DUK_MEMZERO(buf, sizeof(buf)); + + /* The #ifdef clutter here needs to handle the three cases: + * (1) JX+JC, (2) JX only, (3) JC only. + */ +#if defined(DUK_USE_JX) && defined(DUK_USE_JC) + if (js_ctx->flag_ext_custom) +#endif +#if defined(DUK_USE_JX) + { + fmt = ptr ? "(%p)" : "(null)"; + } +#endif +#if defined(DUK_USE_JX) && defined(DUK_USE_JC) + else +#endif +#if defined(DUK_USE_JC) + { + DUK_ASSERT(js_ctx->flag_ext_compatible); + fmt = ptr ? "{\"_ptr\":\"%p\"}" : "{\"_ptr\":\"null\"}"; + } +#endif + + /* When ptr == NULL, the format argument is unused. */ + DUK_SNPRINTF(buf, sizeof(buf) - 1, fmt, ptr); /* must not truncate */ + DUK__EMIT_CSTR(js_ctx, buf); +} +#endif /* DUK_USE_JX || DUK_USE_JC */ + +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) +DUK_LOCAL void duk__enc_bufferobject(duk_json_enc_ctx *js_ctx, duk_hbufferobject *h_bufobj) { + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + + if (h_bufobj->buf == NULL || !DUK_HBUFFEROBJECT_VALID_SLICE(h_bufobj)) { + DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL); + } else { + /* Handle both full and partial slice (as long as covered). */ + duk__enc_buffer_data(js_ctx, + (duk_uint8_t *) DUK_HBUFFEROBJECT_GET_SLICE_BASE(js_ctx->thr->heap, h_bufobj), + (duk_size_t) h_bufobj->length); + } +} +#endif /* DUK_USE_JX || DUK_USE_JC */ + +/* Indent helper. Calling code relies on js_ctx->recursion_depth also being + * directly related to indent depth. + */ +#if defined(DUK_USE_PREFER_SIZE) +DUK_LOCAL void duk__enc_newline_indent(duk_json_enc_ctx *js_ctx, duk_int_t depth) { + DUK_ASSERT(js_ctx->h_gap != NULL); + DUK_ASSERT(DUK_HSTRING_GET_BYTELEN(js_ctx->h_gap) > 0); /* caller guarantees */ + + DUK__EMIT_1(js_ctx, 0x0a); + while (depth-- > 0) { + DUK__EMIT_HSTR(js_ctx, js_ctx->h_gap); + } +} +#else /* DUK_USE_PREFER_SIZE */ +DUK_LOCAL void duk__enc_newline_indent(duk_json_enc_ctx *js_ctx, duk_int_t depth) { + const duk_uint8_t *gap_data; + duk_size_t gap_len; + duk_size_t avail_bytes; /* bytes of indent available for copying */ + duk_size_t need_bytes; /* bytes of indent still needed */ + duk_uint8_t *p_start; + duk_uint8_t *p; + + DUK_ASSERT(js_ctx->h_gap != NULL); + DUK_ASSERT(DUK_HSTRING_GET_BYTELEN(js_ctx->h_gap) > 0); /* caller guarantees */ + + DUK__EMIT_1(js_ctx, 0x0a); + if (DUK_UNLIKELY(depth == 0)) { + return; + } + + /* To handle deeper indents efficiently, make use of copies we've + * already emitted. In effect we can emit a sequence of 1, 2, 4, + * 8, etc copies, and then finish the last run. Byte counters + * avoid multiply with gap_len on every loop. + */ + + gap_data = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(js_ctx->h_gap); + gap_len = (duk_size_t) DUK_HSTRING_GET_BYTELEN(js_ctx->h_gap); + DUK_ASSERT(gap_len > 0); + + need_bytes = gap_len * depth; + p = DUK_BW_ENSURE_GETPTR(js_ctx->thr, &js_ctx->bw, need_bytes); + p_start = p; + + DUK_MEMCPY((void *) p, (const void *) gap_data, (size_t) gap_len); + p += gap_len; + avail_bytes = gap_len; + DUK_ASSERT(need_bytes >= gap_len); + need_bytes -= gap_len; + + while (need_bytes >= avail_bytes) { + DUK_MEMCPY((void *) p, (const void *) p_start, (size_t) avail_bytes); + p += avail_bytes; + need_bytes -= avail_bytes; + avail_bytes <<= 1; + } + + DUK_ASSERT(need_bytes < avail_bytes); /* need_bytes may be zero */ + DUK_MEMCPY((void *) p, (const void *) p_start, (size_t) need_bytes); + p += need_bytes; + /*avail_bytes += need_bytes*/ + + DUK_BW_SET_PTR(js_ctx->thr, &js_ctx->bw, p); +} +#endif /* DUK_USE_PREFER_SIZE */ + +/* Shared entry handling for object/array serialization. */ +DUK_LOCAL void duk__enc_objarr_entry(duk_json_enc_ctx *js_ctx, duk_idx_t *entry_top) { + duk_context *ctx = (duk_context *) js_ctx->thr; + duk_hobject *h_target; + duk_uint_fast32_t i, n; + + *entry_top = duk_get_top(ctx); + + duk_require_stack(ctx, DUK_JSON_ENC_REQSTACK); + + /* Loop check using a hybrid approach: a fixed-size visited[] array + * with overflow in a loop check object. + */ + + h_target = duk_get_hobject(ctx, -1); /* object or array */ + DUK_ASSERT(h_target != NULL); + + n = js_ctx->recursion_depth; + if (DUK_UNLIKELY(n > DUK_JSON_ENC_LOOPARRAY)) { + n = DUK_JSON_ENC_LOOPARRAY; + } + for (i = 0; i < n; i++) { + if (DUK_UNLIKELY(js_ctx->visiting[i] == h_target)) { + DUK_DD(DUK_DDPRINT("slow path loop detect")); + DUK_ERROR_TYPE((duk_hthread *) ctx, DUK_STR_CYCLIC_INPUT); + } + } + if (js_ctx->recursion_depth < DUK_JSON_ENC_LOOPARRAY) { + js_ctx->visiting[js_ctx->recursion_depth] = h_target; + } else { + duk_push_sprintf(ctx, DUK_STR_FMT_PTR, (void *) h_target); + duk_dup_top(ctx); /* -> [ ... voidp voidp ] */ + if (duk_has_prop(ctx, js_ctx->idx_loop)) { + DUK_ERROR_TYPE((duk_hthread *) ctx, DUK_STR_CYCLIC_INPUT); + } + duk_push_true(ctx); /* -> [ ... voidp true ] */ + duk_put_prop(ctx, js_ctx->idx_loop); /* -> [ ... ] */ + } + + /* C recursion check. */ + + DUK_ASSERT(js_ctx->recursion_depth >= 0); + DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit); + if (js_ctx->recursion_depth >= js_ctx->recursion_limit) { + DUK_ERROR_RANGE((duk_hthread *) ctx, DUK_STR_JSONENC_RECLIMIT); + } + js_ctx->recursion_depth++; + + DUK_DDD(DUK_DDDPRINT("shared entry finished: top=%ld, loop=%!T", + (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop))); +} + +/* Shared exit handling for object/array serialization. */ +DUK_LOCAL void duk__enc_objarr_exit(duk_json_enc_ctx *js_ctx, duk_idx_t *entry_top) { + duk_context *ctx = (duk_context *) js_ctx->thr; + duk_hobject *h_target; + + /* C recursion check. */ + + DUK_ASSERT(js_ctx->recursion_depth > 0); + DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit); + js_ctx->recursion_depth--; + + /* Loop check. */ + + h_target = duk_get_hobject(ctx, *entry_top - 1); /* original target at entry_top - 1 */ + DUK_ASSERT(h_target != NULL); + + if (js_ctx->recursion_depth < DUK_JSON_ENC_LOOPARRAY) { + /* Previous entry was inside visited[], nothing to do. */ + } else { + duk_push_sprintf(ctx, DUK_STR_FMT_PTR, (void *) h_target); + duk_del_prop(ctx, js_ctx->idx_loop); /* -> [ ... ] */ + } + + /* Restore stack top after unbalanced code paths. */ + duk_set_top(ctx, *entry_top); + + DUK_DDD(DUK_DDDPRINT("shared entry finished: top=%ld, loop=%!T", + (long) duk_get_top(ctx), (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop))); +} + +/* The JO(value) operation: encode object. + * + * Stack policy: [ object ] -> [ object ]. + */ +DUK_LOCAL void duk__enc_object(duk_json_enc_ctx *js_ctx) { + duk_context *ctx = (duk_context *) js_ctx->thr; + duk_hstring *h_key; + duk_idx_t entry_top; + duk_idx_t idx_obj; + duk_idx_t idx_keys; + duk_bool_t emitted; + duk_uarridx_t arr_len, i; + duk_size_t prev_size; + + DUK_DDD(DUK_DDDPRINT("duk__enc_object: obj=%!T", (duk_tval *) duk_get_tval(ctx, -1))); + + duk__enc_objarr_entry(js_ctx, &entry_top); + + idx_obj = entry_top - 1; + + if (js_ctx->idx_proplist >= 0) { + idx_keys = js_ctx->idx_proplist; + } else { + /* XXX: would be nice to enumerate an object at specified index */ + duk_dup(ctx, idx_obj); + (void) duk_hobject_get_enumerated_keys(ctx, DUK_ENUM_OWN_PROPERTIES_ONLY /*flags*/); /* [ ... target ] -> [ ... target keys ] */ + idx_keys = duk_require_normalize_index(ctx, -1); + /* leave stack unbalanced on purpose */ + } + + DUK_DDD(DUK_DDDPRINT("idx_keys=%ld, h_keys=%!T", + (long) idx_keys, (duk_tval *) duk_get_tval(ctx, idx_keys))); + + /* Steps 8-10 have been merged to avoid a "partial" variable. */ + + DUK__EMIT_1(js_ctx, DUK_ASC_LCURLY); + + /* XXX: keys is an internal object with all keys to be processed + * in its (gapless) array part. Because nobody can touch the keys + * object, we could iterate its array part directly (keeping in mind + * that it can be reallocated). + */ + + arr_len = (duk_uarridx_t) duk_get_length(ctx, idx_keys); + emitted = 0; + for (i = 0; i < arr_len; i++) { + duk_get_prop_index(ctx, idx_keys, i); /* -> [ ... key ] */ + + DUK_DDD(DUK_DDDPRINT("object property loop: holder=%!T, key=%!T", + (duk_tval *) duk_get_tval(ctx, idx_obj), + (duk_tval *) duk_get_tval(ctx, -1))); + + h_key = duk_get_hstring(ctx, -1); + DUK_ASSERT(h_key != NULL); + + prev_size = DUK_BW_GET_SIZE(js_ctx->thr, &js_ctx->bw); + if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { + duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth); + duk__enc_key_autoquote(js_ctx, h_key); + DUK__EMIT_2(js_ctx, DUK_ASC_COLON, DUK_ASC_SPACE); + } else { + duk__enc_key_autoquote(js_ctx, h_key); + DUK__EMIT_1(js_ctx, DUK_ASC_COLON); + } + + /* [ ... key ] */ + + if (DUK_UNLIKELY(duk__enc_value(js_ctx, idx_obj) == 0)) { + /* Value would yield 'undefined', so skip key altogether. + * Side effects have already happened. + */ + DUK_BW_SET_SIZE(js_ctx->thr, &js_ctx->bw, prev_size); + } else { + DUK__EMIT_1(js_ctx, DUK_ASC_COMMA); + emitted = 1; + } + + /* [ ... ] */ + } + + if (emitted) { + DUK_ASSERT(*((duk_uint8_t *) DUK_BW_GET_PTR(js_ctx->thr, &js_ctx->bw) - 1) == DUK_ASC_COMMA); + DUK__UNEMIT_1(js_ctx); /* eat trailing comma */ + if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { + DUK_ASSERT(js_ctx->recursion_depth >= 1); + duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth - 1); + } + } + DUK__EMIT_1(js_ctx, DUK_ASC_RCURLY); + + duk__enc_objarr_exit(js_ctx, &entry_top); + + DUK_ASSERT_TOP(ctx, entry_top); +} + +/* The JA(value) operation: encode array. + * + * Stack policy: [ array ] -> [ array ]. + */ +DUK_LOCAL void duk__enc_array(duk_json_enc_ctx *js_ctx) { + duk_context *ctx = (duk_context *) js_ctx->thr; + duk_idx_t entry_top; + duk_idx_t idx_arr; + duk_bool_t emitted; + duk_uarridx_t i, arr_len; + + DUK_DDD(DUK_DDDPRINT("duk__enc_array: array=%!T", + (duk_tval *) duk_get_tval(ctx, -1))); + + duk__enc_objarr_entry(js_ctx, &entry_top); + + idx_arr = entry_top - 1; + + /* Steps 8-10 have been merged to avoid a "partial" variable. */ + + DUK__EMIT_1(js_ctx, DUK_ASC_LBRACKET); + + arr_len = (duk_uarridx_t) duk_get_length(ctx, idx_arr); + emitted = 0; + for (i = 0; i < arr_len; i++) { + DUK_DDD(DUK_DDDPRINT("array entry loop: array=%!T, index=%ld, arr_len=%ld", + (duk_tval *) duk_get_tval(ctx, idx_arr), + (long) i, (long) arr_len)); + + if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { + DUK_ASSERT(js_ctx->recursion_depth >= 1); + duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth); + } + + /* XXX: duk_push_uint_string() */ + duk_push_uint(ctx, (duk_uint_t) i); + duk_to_string(ctx, -1); /* -> [ ... key ] */ + + /* [ ... key ] */ + + if (DUK_UNLIKELY(duk__enc_value(js_ctx, idx_arr) == 0)) { + /* Value would normally be omitted, replace with 'null'. */ + DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL); + } else { + ; + } + + /* [ ... ] */ + + DUK__EMIT_1(js_ctx, DUK_ASC_COMMA); + emitted = 1; + } + + if (emitted) { + DUK_ASSERT(*((duk_uint8_t *) DUK_BW_GET_PTR(js_ctx->thr, &js_ctx->bw) - 1) == DUK_ASC_COMMA); + DUK__UNEMIT_1(js_ctx); /* eat trailing comma */ + if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { + DUK_ASSERT(js_ctx->recursion_depth >= 1); + duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth - 1); + } + } + DUK__EMIT_1(js_ctx, DUK_ASC_RBRACKET); + + duk__enc_objarr_exit(js_ctx, &entry_top); + + DUK_ASSERT_TOP(ctx, entry_top); +} + +/* The Str(key, holder) operation. + * + * Stack policy: [ ... key ] -> [ ... ] + */ +DUK_LOCAL duk_bool_t duk__enc_value(duk_json_enc_ctx *js_ctx, duk_idx_t idx_holder) { + duk_context *ctx = (duk_context *) js_ctx->thr; + duk_hthread *thr = (duk_hthread *) ctx; + duk_hobject *h_tmp; + duk_tval *tv; + duk_tval *tv_holder; + duk_tval *tv_key; + duk_small_int_t c; + + DUK_DDD(DUK_DDDPRINT("duk__enc_value: idx_holder=%ld, holder=%!T, key=%!T", + (long) idx_holder, (duk_tval *) duk_get_tval(ctx, idx_holder), + (duk_tval *) duk_get_tval(ctx, -1))); + + DUK_UNREF(thr); + + tv_holder = DUK_GET_TVAL_POSIDX(ctx, idx_holder); + DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv_holder)); + tv_key = DUK_GET_TVAL_NEGIDX(ctx, -1); + DUK_ASSERT(DUK_TVAL_IS_STRING(tv_key)); + (void) duk_hobject_getprop(thr, tv_holder, tv_key); + + /* -> [ ... key val ] */ + + DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1))); + + h_tmp = duk_get_hobject_or_lfunc_coerce(ctx, -1); + if (h_tmp != NULL) { + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_TO_JSON); + h_tmp = duk_get_hobject_or_lfunc_coerce(ctx, -1); /* toJSON() can also be a lightfunc */ + + if (h_tmp != NULL && DUK_HOBJECT_IS_CALLABLE(h_tmp)) { + DUK_DDD(DUK_DDDPRINT("value is object, has callable toJSON() -> call it")); + /* XXX: duk_dup_unvalidated(ctx, -2) etc. */ + duk_dup(ctx, -2); /* -> [ ... key val toJSON val ] */ + duk_dup(ctx, -4); /* -> [ ... key val toJSON val key ] */ + duk_call_method(ctx, 1); /* -> [ ... key val val' ] */ + duk_remove(ctx, -2); /* -> [ ... key val' ] */ + } else { + duk_pop(ctx); /* -> [ ... key val ] */ + } + } + + /* [ ... key val ] */ + + DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1))); + + if (js_ctx->h_replacer) { + /* XXX: Here a "slice copy" would be useful. */ + DUK_DDD(DUK_DDDPRINT("replacer is set, call replacer")); + duk_push_hobject(ctx, js_ctx->h_replacer); /* -> [ ... key val replacer ] */ + duk_dup(ctx, idx_holder); /* -> [ ... key val replacer holder ] */ + duk_dup(ctx, -4); /* -> [ ... key val replacer holder key ] */ + duk_dup(ctx, -4); /* -> [ ... key val replacer holder key val ] */ + duk_call_method(ctx, 2); /* -> [ ... key val val' ] */ + duk_remove(ctx, -2); /* -> [ ... key val' ] */ + } + + /* [ ... key val ] */ + + DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1))); + + tv = DUK_GET_TVAL_NEGIDX(ctx, -1); + if (DUK_TVAL_IS_OBJECT(tv)) { + duk_hobject *h; + + h = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h != NULL); + + if (DUK_HOBJECT_IS_BUFFEROBJECT(h)) { +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + duk_hbufferobject *h_bufobj; + h_bufobj = (duk_hbufferobject *) h; + DUK_ASSERT_HBUFFEROBJECT_VALID(h_bufobj); + + /* Conceptually we'd extract the plain underlying buffer + * or its slice and then do a type mask check below to + * see if we should reject it. Do the mask check here + * instead to avoid making a copy of the buffer slice. + */ + + if (js_ctx->mask_for_undefined & DUK_TYPE_MASK_BUFFER) { + DUK_DDD(DUK_DDDPRINT("-> bufferobject (-> plain buffer) will result in undefined (type mask check)")); + goto pop2_undef; + } + DUK_DDD(DUK_DDDPRINT("-> bufferobject won't result in undefined, encode directly")); + duk__enc_bufferobject(js_ctx, h_bufobj); + goto pop2_emitted; +#else + DUK_DDD(DUK_DDDPRINT("no JX/JC support, bufferobject/buffer will always result in undefined")); + goto pop2_undef; +#endif + } else { + c = (duk_small_int_t) DUK_HOBJECT_GET_CLASS_NUMBER(h); + switch ((int) c) { + case DUK_HOBJECT_CLASS_NUMBER: { + DUK_DDD(DUK_DDDPRINT("value is a Number object -> coerce with ToNumber()")); + duk_to_number(ctx, -1); + /* The coercion potentially invokes user .valueOf() and .toString() + * but can't result in a function value because [[DefaultValue]] would + * reject such a result: test-dev-json-stringify-coercion-1.js. + */ + DUK_ASSERT(!duk_is_callable(ctx, -1)); + break; + } + case DUK_HOBJECT_CLASS_STRING: { + DUK_DDD(DUK_DDDPRINT("value is a String object -> coerce with ToString()")); + duk_to_string(ctx, -1); + /* Same coercion behavior as for Number. */ + DUK_ASSERT(!duk_is_callable(ctx, -1)); + break; + } +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + case DUK_HOBJECT_CLASS_POINTER: +#endif + case DUK_HOBJECT_CLASS_BOOLEAN: { + DUK_DDD(DUK_DDDPRINT("value is a Boolean/Buffer/Pointer object -> get internal value")); + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE); + duk_remove(ctx, -2); + break; + } + default: { + /* Normal object which doesn't get automatically coerced to a + * primitive value. Functions are checked for specially. The + * primitive value coercions for Number, String, Pointer, and + * Boolean can't result in functions so suffices to check here. + */ + DUK_ASSERT(h != NULL); + if (DUK_HOBJECT_IS_CALLABLE(h)) { +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + if (js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM | + DUK_JSON_FLAG_EXT_COMPATIBLE)) { + /* We only get here when doing non-standard JSON encoding */ + DUK_DDD(DUK_DDDPRINT("-> function allowed, serialize to custom format")); + DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible); + DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_function); + goto pop2_emitted; + } else { + DUK_DDD(DUK_DDDPRINT("-> will result in undefined (function)")); + goto pop2_undef; + } +#else /* DUK_USE_JX || DUK_USE_JC */ + DUK_DDD(DUK_DDDPRINT("-> will result in undefined (function)")); + goto pop2_undef; +#endif /* DUK_USE_JX || DUK_USE_JC */ + } + } + } /* end switch */ + } + } + + /* [ ... key val ] */ + + DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(ctx, -1))); + + if (duk_check_type_mask(ctx, -1, js_ctx->mask_for_undefined)) { + /* will result in undefined */ + DUK_DDD(DUK_DDDPRINT("-> will result in undefined (type mask check)")); + goto pop2_undef; + } + tv = DUK_GET_TVAL_NEGIDX(ctx, -1); + + switch (DUK_TVAL_GET_TAG(tv)) { +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + /* When JX/JC not in use, the type mask above will avoid this case if needed. */ + case DUK_TAG_UNDEFINED: { + DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_undefined); + break; + } +#endif + case DUK_TAG_NULL: { + DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL); + break; + } + case DUK_TAG_BOOLEAN: { + DUK__EMIT_STRIDX(js_ctx, DUK_TVAL_GET_BOOLEAN(tv) ? + DUK_STRIDX_TRUE : DUK_STRIDX_FALSE); + break; + } +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + /* When JX/JC not in use, the type mask above will avoid this case if needed. */ + case DUK_TAG_POINTER: { + duk__enc_pointer(js_ctx, DUK_TVAL_GET_POINTER(tv)); + break; + } +#endif /* DUK_USE_JX || DUK_USE_JC */ + case DUK_TAG_STRING: { + duk_hstring *h = DUK_TVAL_GET_STRING(tv); + DUK_ASSERT(h != NULL); + + duk__enc_quote_string(js_ctx, h); + break; + } + case DUK_TAG_OBJECT: { + duk_hobject *h = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h != NULL); + + /* Function values are handled completely above (including + * coercion results): + */ + DUK_ASSERT(!DUK_HOBJECT_IS_CALLABLE(h)); + + if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY) { + duk__enc_array(js_ctx); + } else { + duk__enc_object(js_ctx); + } + break; + } +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + /* When JX/JC not in use, the type mask above will avoid this case if needed. */ + case DUK_TAG_BUFFER: { + duk__enc_buffer(js_ctx, DUK_TVAL_GET_BUFFER(tv)); + break; + } +#endif /* DUK_USE_JX || DUK_USE_JC */ + case DUK_TAG_LIGHTFUNC: { +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + /* We only get here when doing non-standard JSON encoding */ + DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible); + DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_function); +#else + /* Standard JSON omits functions */ + DUK_UNREACHABLE(); +#endif + break; + } +#if defined(DUK_USE_FASTINT) + case DUK_TAG_FASTINT: + /* Number serialization has a significant impact relative to + * other fast path code, so careful fast path for fastints. + */ + duk__enc_fastint_tval(js_ctx, tv); + break; +#endif + default: { + /* number */ + DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); + DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); + /* XXX: A fast path for usual integers would be useful when + * fastint support is not enabled. + */ + duk__enc_double(js_ctx); + break; + } + } + + pop2_emitted: + duk_pop_2(ctx); /* [ ... key val ] -> [ ... ] */ + return 1; /* emitted */ + + pop2_undef: + duk_pop_2(ctx); /* [ ... key val ] -> [ ... ] */ + return 0; /* not emitted */ +} + +/* E5 Section 15.12.3, main algorithm, step 4.b.ii steps 1-4. */ +DUK_LOCAL duk_bool_t duk__enc_allow_into_proplist(duk_tval *tv) { + duk_hobject *h; + duk_small_int_t c; + + DUK_ASSERT(tv != NULL); + if (DUK_TVAL_IS_STRING(tv) || DUK_TVAL_IS_NUMBER(tv)) { + return 1; + } else if (DUK_TVAL_IS_OBJECT(tv)) { + h = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(h != NULL); + c = (duk_small_int_t) DUK_HOBJECT_GET_CLASS_NUMBER(h); + if (c == DUK_HOBJECT_CLASS_STRING || c == DUK_HOBJECT_CLASS_NUMBER) { + return 1; + } + } + + return 0; +} + +/* + * JSON.stringify() fast path + * + * Otherwise supports full JSON, JX, and JC features, but bails out on any + * possible side effect which might change the value being serialized. The + * fast path can take advantage of the fact that the value being serialized + * is unchanged so that we can walk directly through property tables etc. + */ + +#if defined(DUK_USE_JSON_STRINGIFY_FASTPATH) +DUK_LOCAL duk_bool_t duk__json_stringify_fast_value(duk_json_enc_ctx *js_ctx, duk_tval *tv) { + duk_uint_fast32_t i, n; + + DUK_DDD(DUK_DDDPRINT("stringify fast: %!T", tv)); + + DUK_ASSERT(js_ctx != NULL); + DUK_ASSERT(js_ctx->thr != NULL); + +#if 0 /* disabled for now */ + restart_match: +#endif + + DUK_ASSERT(tv != NULL); + + switch (DUK_TVAL_GET_TAG(tv)) { + case DUK_TAG_UNDEFINED: { +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + if (js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible) { + DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_undefined); + break; + } else { + goto emit_undefined; + } +#else + goto emit_undefined; +#endif + } + case DUK_TAG_NULL: { + DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL); + break; + } + case DUK_TAG_BOOLEAN: { + DUK__EMIT_STRIDX(js_ctx, DUK_TVAL_GET_BOOLEAN(tv) ? + DUK_STRIDX_TRUE : DUK_STRIDX_FALSE); + break; + } + case DUK_TAG_STRING: { + duk_hstring *h; + + h = DUK_TVAL_GET_STRING(tv); + DUK_ASSERT(h != NULL); + duk__enc_quote_string(js_ctx, h); + break; + } + case DUK_TAG_OBJECT: { + duk_hobject *obj; + duk_tval *tv_val; + duk_bool_t emitted = 0; + duk_uint32_t c_bit, c_all, c_array, c_unbox, c_undef, + c_func, c_bufobj, c_object; + + /* For objects JSON.stringify() only looks for own, enumerable + * properties which is nice for the fast path here. + * + * For arrays JSON.stringify() uses [[Get]] so it will actually + * inherit properties during serialization! This fast path + * supports gappy arrays as long as there's no actual inherited + * property (which might be a getter etc). + * + * Since recursion only happens for objects, we can have both + * recursion and loop checks here. We use a simple, depth-limited + * loop check in the fast path because the object-based tracking + * is very slow (when tested, it accounted for 50% of fast path + * execution time for input data with a lot of small objects!). + */ + + /* XXX: for real world code, could just ignore array inheritance + * and only look at array own properties. + */ + + /* We rely on a few object flag / class number relationships here, + * assert for them. + */ + + obj = DUK_TVAL_GET_OBJECT(tv); + DUK_ASSERT(obj != NULL); + DUK_ASSERT_HOBJECT_VALID(obj); + + /* Once recursion depth is increased, exit path must decrease + * it (though it's OK to abort the fast path). + */ + + DUK_ASSERT(js_ctx->recursion_depth >= 0); + DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit); + if (js_ctx->recursion_depth >= js_ctx->recursion_limit) { + DUK_DD(DUK_DDPRINT("fast path recursion limit")); + DUK_ERROR_RANGE(js_ctx->thr, DUK_STR_JSONDEC_RECLIMIT); + } + + for (i = 0, n = (duk_uint_fast32_t) js_ctx->recursion_depth; i < n; i++) { + if (DUK_UNLIKELY(js_ctx->visiting[i] == obj)) { + DUK_DD(DUK_DDPRINT("fast path loop detect")); + DUK_ERROR_TYPE(js_ctx->thr, DUK_STR_CYCLIC_INPUT); + } + } + + /* Guaranteed by recursion_limit setup so we don't have to + * check twice. + */ + DUK_ASSERT(js_ctx->recursion_depth < DUK_JSON_ENC_LOOPARRAY); + js_ctx->visiting[js_ctx->recursion_depth] = obj; + js_ctx->recursion_depth++; + + /* If object has a .toJSON() property, we can't be certain + * that it wouldn't mutate any value arbitrarily, so bail + * out of the fast path. + * + * If an object is a Proxy we also can't avoid side effects + * so abandon. + */ + /* XXX: non-callable .toJSON() doesn't need to cause an abort + * but does at the moment, probably not worth fixing. + */ + if (duk_hobject_hasprop_raw(js_ctx->thr, obj, DUK_HTHREAD_STRING_TO_JSON(js_ctx->thr)) || + DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(obj)) { + DUK_DD(DUK_DDPRINT("object has a .toJSON property or object is a Proxy, abort fast path")); + goto abort_fastpath; + } + + /* We could use a switch-case for the class number but it turns out + * a small if-else ladder on class masks is better. The if-ladder + * should be in order of relevancy. + */ + + /* XXX: move masks to js_ctx? they don't change during one + * fast path invocation. + */ + DUK_ASSERT(DUK_HOBJECT_CLASS_MAX <= 31); +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + if (js_ctx->flag_ext_custom_or_compatible) { + c_all = DUK_HOBJECT_CMASK_ALL; + c_array = DUK_HOBJECT_CMASK_ARRAY; + c_unbox = DUK_HOBJECT_CMASK_NUMBER | + DUK_HOBJECT_CMASK_STRING | + DUK_HOBJECT_CMASK_BOOLEAN | + DUK_HOBJECT_CMASK_POINTER; + c_func = DUK_HOBJECT_CMASK_FUNCTION; + c_bufobj = DUK_HOBJECT_CMASK_ALL_BUFFEROBJECTS; + c_undef = 0; + c_object = c_all & ~(c_array | c_unbox | c_func | c_bufobj | c_undef); + } + else +#endif + { + c_all = DUK_HOBJECT_CMASK_ALL; + c_array = DUK_HOBJECT_CMASK_ARRAY; + c_unbox = DUK_HOBJECT_CMASK_NUMBER | + DUK_HOBJECT_CMASK_STRING | + DUK_HOBJECT_CMASK_BOOLEAN; + c_func = 0; + c_bufobj = 0; + c_undef = DUK_HOBJECT_CMASK_FUNCTION | + DUK_HOBJECT_CMASK_POINTER | + DUK_HOBJECT_CMASK_ALL_BUFFEROBJECTS; + c_object = c_all & ~(c_array | c_unbox | c_func | c_bufobj | c_undef); + } + + c_bit = DUK_HOBJECT_GET_CLASS_MASK(obj); + if (c_bit & c_object) { + /* All other object types. */ + DUK__EMIT_1(js_ctx, DUK_ASC_LCURLY); + + /* A non-Array object should not have an array part in practice. + * But since it is supported internally (and perhaps used at some + * point), check and abandon if that's the case. + */ + if (DUK_HOBJECT_HAS_ARRAY_PART(obj)) { + DUK_DD(DUK_DDPRINT("non-Array object has array part, abort fast path")); + goto abort_fastpath; + } + + for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(obj); i++) { + duk_hstring *k; + duk_size_t prev_size; + + k = DUK_HOBJECT_E_GET_KEY(js_ctx->thr->heap, obj, i); + if (!k) { + continue; + } + if (!DUK_HOBJECT_E_SLOT_IS_ENUMERABLE(js_ctx->thr->heap, obj, i)) { + continue; + } + if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(js_ctx->thr->heap, obj, i)) { + /* Getter might have arbitrary side effects, + * so bail out. + */ + DUK_DD(DUK_DDPRINT("property is an accessor, abort fast path")); + goto abort_fastpath; + } + if (DUK_HSTRING_HAS_INTERNAL(k)) { + continue; + } + + tv_val = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(js_ctx->thr->heap, obj, i); + + prev_size = DUK_BW_GET_SIZE(js_ctx->thr, &js_ctx->bw); + if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { + duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth); + duk__enc_key_autoquote(js_ctx, k); + DUK__EMIT_2(js_ctx, DUK_ASC_COLON, DUK_ASC_SPACE); + } else { + duk__enc_key_autoquote(js_ctx, k); + DUK__EMIT_1(js_ctx, DUK_ASC_COLON); + } + + if (duk__json_stringify_fast_value(js_ctx, tv_val) == 0) { + DUK_DD(DUK_DDPRINT("prop value not supported, rewind key and colon")); + DUK_BW_SET_SIZE(js_ctx->thr, &js_ctx->bw, prev_size); + } else { + DUK__EMIT_1(js_ctx, DUK_ASC_COMMA); + emitted = 1; + } + } + + /* If any non-Array value had enumerable virtual own + * properties, they should be serialized here. Standard + * types don't. + */ + + if (emitted) { + DUK_ASSERT(*((duk_uint8_t *) DUK_BW_GET_PTR(js_ctx->thr, &js_ctx->bw) - 1) == DUK_ASC_COMMA); + DUK__UNEMIT_1(js_ctx); /* eat trailing comma */ + if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { + DUK_ASSERT(js_ctx->recursion_depth >= 1); + duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth - 1); + } + } + DUK__EMIT_1(js_ctx, DUK_ASC_RCURLY); + } else if (c_bit & c_array) { + duk_uint_fast32_t arr_len; + duk_uint_fast32_t asize; + + DUK__EMIT_1(js_ctx, DUK_ASC_LBRACKET); + + /* Assume arrays are dense in the fast path. */ + if (!DUK_HOBJECT_HAS_ARRAY_PART(obj)) { + DUK_DD(DUK_DDPRINT("Array object is sparse, abort fast path")); + goto abort_fastpath; + } + + arr_len = (duk_uint_fast32_t) duk_hobject_get_length(js_ctx->thr, obj); + asize = (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(obj); + if (arr_len > asize) { + /* Array length is larger than 'asize'. This shouldn't + * happen in practice. Bail out just in case. + */ + DUK_DD(DUK_DDPRINT("arr_len > asize, abort fast path")); + goto abort_fastpath; + } + /* Array part may be larger than 'length'; if so, iterate + * only up to array 'length'. + */ + for (i = 0; i < arr_len; i++) { + DUK_ASSERT(i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(obj)); + + tv_val = DUK_HOBJECT_A_GET_VALUE_PTR(js_ctx->thr->heap, obj, i); + + if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { + duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth); + } + + if (DUK_UNLIKELY(DUK_TVAL_IS_UNUSED(tv_val))) { + /* Gap in array; check for inherited property, + * bail out if one exists. This should be enough + * to support gappy arrays for all practical code. + */ + duk_hstring *h_tmp; + duk_bool_t has_inherited; + + /* XXX: refactor into an internal helper, pretty awkward */ + duk_push_uint((duk_context *) js_ctx->thr, (duk_uint_t) i); + h_tmp = duk_to_hstring((duk_context *) js_ctx->thr, -1); + DUK_ASSERT(h_tmp != NULL); + has_inherited = duk_hobject_hasprop_raw(js_ctx->thr, obj, h_tmp); + duk_pop((duk_context *) js_ctx->thr); + + if (has_inherited) { + DUK_D(DUK_DPRINT("gap in array, conflicting inherited property, abort fast path")); + goto abort_fastpath; + } + + /* Ordinary gap, undefined encodes to 'null' in + * standard JSON (and no JX/JC support here now). + */ + DUK_D(DUK_DPRINT("gap in array, no conflicting inherited property, remain on fast path")); +#if defined(DUK_USE_JX) + DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_undefined); +#else + DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL); +#endif + } else { + if (duk__json_stringify_fast_value(js_ctx, tv_val) == 0) { + DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL); + } + } + + DUK__EMIT_1(js_ctx, DUK_ASC_COMMA); + emitted = 1; + } + + if (emitted) { + DUK_ASSERT(*((duk_uint8_t *) DUK_BW_GET_PTR(js_ctx->thr, &js_ctx->bw) - 1) == DUK_ASC_COMMA); + DUK__UNEMIT_1(js_ctx); /* eat trailing comma */ + if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) { + DUK_ASSERT(js_ctx->recursion_depth >= 1); + duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth - 1); + } + } + DUK__EMIT_1(js_ctx, DUK_ASC_RBRACKET); + } else if (c_bit & c_unbox) { + /* Certain boxed types are required to go through + * automatic unboxing. Rely on internal value being + * sane (to avoid infinite recursion). + */ +#if 1 + /* The code below is incorrect if .toString() or .valueOf() have + * have been overridden. The correct approach would be to look up + * the method(s) and if they resolve to the built-in function we + * can safely bypass it and look up the internal value directly. + * Unimplemented for now, abort fast path for boxed values. + */ + goto abort_fastpath; +#else /* disabled */ + /* Disabled until fixed, see above. */ + duk_tval *tv_internal; + + DUK_DD(DUK_DDPRINT("auto unboxing in fast path")); + + tv_internal = duk_hobject_get_internal_value_tval_ptr(js_ctx->thr->heap, obj); + DUK_ASSERT(tv_internal != NULL); + DUK_ASSERT(DUK_TVAL_IS_STRING(tv_internal) || + DUK_TVAL_IS_NUMBER(tv_internal) || + DUK_TVAL_IS_BOOLEAN(tv_internal) || + DUK_TVAL_IS_POINTER(tv_internal)); + + tv = tv_internal; + DUK_ASSERT(js_ctx->recursion_depth > 0); + js_ctx->recursion_depth--; /* required to keep recursion depth correct */ + goto restart_match; +#endif /* disabled */ +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + } else if (c_bit & c_func) { + DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_function); + } else if (c_bit & c_bufobj) { + duk__enc_bufferobject(js_ctx, (duk_hbufferobject *) obj); +#endif + } else { + DUK_ASSERT((c_bit & c_undef) != 0); + + /* Must decrease recursion depth before returning. */ + DUK_ASSERT(js_ctx->recursion_depth > 0); + DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit); + js_ctx->recursion_depth--; + goto emit_undefined; + } + + DUK_ASSERT(js_ctx->recursion_depth > 0); + DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit); + js_ctx->recursion_depth--; + break; + } + case DUK_TAG_BUFFER: { +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + if (js_ctx->flag_ext_custom_or_compatible) { + duk__enc_buffer(js_ctx, DUK_TVAL_GET_BUFFER(tv)); + break; + } else { + goto emit_undefined; + } +#else + goto emit_undefined; +#endif + } + case DUK_TAG_POINTER: { +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + if (js_ctx->flag_ext_custom_or_compatible) { + duk__enc_pointer(js_ctx, DUK_TVAL_GET_POINTER(tv)); + break; + } else { + goto emit_undefined; + } +#else + goto emit_undefined; +#endif + } + case DUK_TAG_LIGHTFUNC: { + /* A lightfunc might also inherit a .toJSON() so just bail out. */ + /* XXX: Could just lookup .toJSON() and continue in fast path, + * as it would almost never be defined. + */ + DUK_DD(DUK_DDPRINT("value is a lightfunc, abort fast path")); + goto abort_fastpath; + } +#if defined(DUK_USE_FASTINT) + case DUK_TAG_FASTINT: { + /* Number serialization has a significant impact relative to + * other fast path code, so careful fast path for fastints. + */ + duk__enc_fastint_tval(js_ctx, tv); + break; + } +#endif + default: { + /* XXX: A fast path for usual integers would be useful when + * fastint support is not enabled. + */ + DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv)); + DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); + + /* XXX: Stack discipline is annoying, could be changed in numconv. */ + duk_push_tval((duk_context *) js_ctx->thr, tv); + duk__enc_double(js_ctx); + duk_pop((duk_context *) js_ctx->thr); + +#if 0 + /* Could also rely on native sprintf(), but it will handle + * values like NaN, Infinity, -0, exponent notation etc in + * a JSON-incompatible way. + */ + duk_double_t d; + char buf[64]; + + DUK_ASSERT(DUK_TVAL_IS_DOUBLE(tv)); + d = DUK_TVAL_GET_DOUBLE(tv); + DUK_SPRINTF(buf, "%lg", d); + DUK__EMIT_CSTR(js_ctx, buf); +#endif + } + } + return 1; /* not undefined */ + + emit_undefined: + return 0; /* value was undefined/unsupported */ + + abort_fastpath: + /* Error message doesn't matter: the error is ignored anyway. */ + DUK_DD(DUK_DDPRINT("aborting fast path")); + DUK_ERROR_INTERNAL_DEFMSG(js_ctx->thr); + return 0; /* unreachable */ +} + +DUK_LOCAL duk_ret_t duk__json_stringify_fast(duk_context *ctx) { + duk_json_enc_ctx *js_ctx; + duk_tval *tv; + + DUK_ASSERT(ctx != NULL); + tv = DUK_GET_TVAL_NEGIDX(ctx, -2); + DUK_ASSERT(DUK_TVAL_IS_POINTER(tv)); + js_ctx = (duk_json_enc_ctx *) DUK_TVAL_GET_POINTER(tv); + DUK_ASSERT(js_ctx != NULL); + + tv = DUK_GET_TVAL_NEGIDX(ctx, -1); + if (duk__json_stringify_fast_value(js_ctx, tv) == 0) { + DUK_DD(DUK_DDPRINT("top level value not supported, fail fast path")); + return DUK_RET_ERROR; /* error message doesn't matter, ignored anyway */ + } + + return 0; +} +#endif /* DUK_USE_JSON_STRINGIFY_FASTPATH */ + +/* + * Top level wrappers + */ + +DUK_INTERNAL +void duk_bi_json_parse_helper(duk_context *ctx, + duk_idx_t idx_value, + duk_idx_t idx_reviver, + duk_small_uint_t flags) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_json_dec_ctx js_ctx_alloc; + duk_json_dec_ctx *js_ctx = &js_ctx_alloc; + duk_hstring *h_text; +#ifdef DUK_USE_ASSERTIONS + duk_idx_t entry_top = duk_get_top(ctx); +#endif + + /* negative top-relative indices not allowed now */ + DUK_ASSERT(idx_value == DUK_INVALID_INDEX || idx_value >= 0); + DUK_ASSERT(idx_reviver == DUK_INVALID_INDEX || idx_reviver >= 0); + + DUK_DDD(DUK_DDDPRINT("JSON parse start: text=%!T, reviver=%!T, flags=0x%08lx, stack_top=%ld", + (duk_tval *) duk_get_tval(ctx, idx_value), + (duk_tval *) duk_get_tval(ctx, idx_reviver), + (unsigned long) flags, + (long) duk_get_top(ctx))); + + DUK_MEMZERO(&js_ctx_alloc, sizeof(js_ctx_alloc)); + js_ctx->thr = thr; +#ifdef DUK_USE_EXPLICIT_NULL_INIT + /* nothing now */ +#endif + js_ctx->recursion_limit = DUK_USE_JSON_DEC_RECLIMIT; + DUK_ASSERT(js_ctx->recursion_depth == 0); + + /* Flag handling currently assumes that flags are consistent. This is OK + * because the call sites are now strictly controlled. + */ + + js_ctx->flags = flags; +#if defined(DUK_USE_JX) + js_ctx->flag_ext_custom = flags & DUK_JSON_FLAG_EXT_CUSTOM; +#endif +#if defined(DUK_USE_JC) + js_ctx->flag_ext_compatible = flags & DUK_JSON_FLAG_EXT_COMPATIBLE; +#endif +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + js_ctx->flag_ext_custom_or_compatible = flags & (DUK_JSON_FLAG_EXT_CUSTOM | DUK_JSON_FLAG_EXT_COMPATIBLE); +#endif + + h_text = duk_to_hstring(ctx, idx_value); /* coerce in-place */ + DUK_ASSERT(h_text != NULL); + + /* JSON parsing code is allowed to read [p_start,p_end]: p_end is + * valid and points to the string NUL terminator (which is always + * guaranteed for duk_hstrings. + */ + js_ctx->p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_text); + js_ctx->p = js_ctx->p_start; + js_ctx->p_end = ((const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_text)) + + DUK_HSTRING_GET_BYTELEN(h_text); + DUK_ASSERT(*(js_ctx->p_end) == 0x00); + + duk__dec_value(js_ctx); /* -> [ ... value ] */ + + /* Trailing whitespace has been eaten by duk__dec_value(), so if + * we're not at end of input here, it's a SyntaxError. + */ + + if (js_ctx->p != js_ctx->p_end) { + duk__dec_syntax_error(js_ctx); + } + + if (duk_is_callable(ctx, idx_reviver)) { + DUK_DDD(DUK_DDDPRINT("applying reviver: %!T", + (duk_tval *) duk_get_tval(ctx, idx_reviver))); + + js_ctx->idx_reviver = idx_reviver; + + duk_push_object(ctx); + duk_dup(ctx, -2); /* -> [ ... val root val ] */ + duk_put_prop_stridx(ctx, -2, DUK_STRIDX_EMPTY_STRING); /* default attrs ok */ + duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING); /* -> [ ... val root "" ] */ + + DUK_DDD(DUK_DDDPRINT("start reviver walk, root=%!T, name=%!T", + (duk_tval *) duk_get_tval(ctx, -2), + (duk_tval *) duk_get_tval(ctx, -1))); + + duk__dec_reviver_walk(js_ctx); /* [ ... val root "" ] -> [ ... val val' ] */ + duk_remove(ctx, -2); /* -> [ ... val' ] */ + } else { + DUK_DDD(DUK_DDDPRINT("reviver does not exist or is not callable: %!T", + (duk_tval *) duk_get_tval(ctx, idx_reviver))); + } + + /* Final result is at stack top. */ + + DUK_DDD(DUK_DDDPRINT("JSON parse end: text=%!T, reviver=%!T, flags=0x%08lx, result=%!T, stack_top=%ld", + (duk_tval *) duk_get_tval(ctx, idx_value), + (duk_tval *) duk_get_tval(ctx, idx_reviver), + (unsigned long) flags, + (duk_tval *) duk_get_tval(ctx, -1), + (long) duk_get_top(ctx))); + + DUK_ASSERT(duk_get_top(ctx) == entry_top + 1); +} + +DUK_INTERNAL +void duk_bi_json_stringify_helper(duk_context *ctx, + duk_idx_t idx_value, + duk_idx_t idx_replacer, + duk_idx_t idx_space, + duk_small_uint_t flags) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_json_enc_ctx js_ctx_alloc; + duk_json_enc_ctx *js_ctx = &js_ctx_alloc; + duk_hobject *h; + duk_idx_t idx_holder; + duk_idx_t entry_top; + + /* negative top-relative indices not allowed now */ + DUK_ASSERT(idx_value == DUK_INVALID_INDEX || idx_value >= 0); + DUK_ASSERT(idx_replacer == DUK_INVALID_INDEX || idx_replacer >= 0); + DUK_ASSERT(idx_space == DUK_INVALID_INDEX || idx_space >= 0); + + DUK_DDD(DUK_DDDPRINT("JSON stringify start: value=%!T, replacer=%!T, space=%!T, flags=0x%08lx, stack_top=%ld", + (duk_tval *) duk_get_tval(ctx, idx_value), + (duk_tval *) duk_get_tval(ctx, idx_replacer), + (duk_tval *) duk_get_tval(ctx, idx_space), + (unsigned long) flags, + (long) duk_get_top(ctx))); + + entry_top = duk_get_top(ctx); + + /* + * Context init + */ + + DUK_MEMZERO(&js_ctx_alloc, sizeof(js_ctx_alloc)); + js_ctx->thr = thr; +#ifdef DUK_USE_EXPLICIT_NULL_INIT + js_ctx->h_replacer = NULL; + js_ctx->h_gap = NULL; +#endif + js_ctx->idx_proplist = -1; + + /* Flag handling currently assumes that flags are consistent. This is OK + * because the call sites are now strictly controlled. + */ + + js_ctx->flags = flags; + js_ctx->flag_ascii_only = flags & DUK_JSON_FLAG_ASCII_ONLY; + js_ctx->flag_avoid_key_quotes = flags & DUK_JSON_FLAG_AVOID_KEY_QUOTES; +#ifdef DUK_USE_JX + js_ctx->flag_ext_custom = flags & DUK_JSON_FLAG_EXT_CUSTOM; +#endif +#ifdef DUK_USE_JC + js_ctx->flag_ext_compatible = flags & DUK_JSON_FLAG_EXT_COMPATIBLE; +#endif +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + js_ctx->flag_ext_custom_or_compatible = flags & (DUK_JSON_FLAG_EXT_CUSTOM | DUK_JSON_FLAG_EXT_COMPATIBLE); +#endif + + /* The #ifdef clutter here handles the JX/JC enable/disable + * combinations properly. + */ +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + js_ctx->stridx_custom_undefined = DUK_STRIDX_LC_NULL; /* standard JSON; array gaps */ +#if defined(DUK_USE_JX) + if (flags & DUK_JSON_FLAG_EXT_CUSTOM) { + js_ctx->stridx_custom_undefined = DUK_STRIDX_LC_UNDEFINED; + js_ctx->stridx_custom_nan = DUK_STRIDX_NAN; + js_ctx->stridx_custom_neginf = DUK_STRIDX_MINUS_INFINITY; + js_ctx->stridx_custom_posinf = DUK_STRIDX_INFINITY; + js_ctx->stridx_custom_function = + (flags & DUK_JSON_FLAG_AVOID_KEY_QUOTES) ? + DUK_STRIDX_JSON_EXT_FUNCTION2 : + DUK_STRIDX_JSON_EXT_FUNCTION1; + } +#endif /* DUK_USE_JX */ +#if defined(DUK_USE_JX) && defined(DUK_USE_JC) + else +#endif /* DUK_USE_JX && DUK_USE_JC */ +#if defined(DUK_USE_JC) + if (js_ctx->flags & DUK_JSON_FLAG_EXT_COMPATIBLE) { + js_ctx->stridx_custom_undefined = DUK_STRIDX_JSON_EXT_UNDEFINED; + js_ctx->stridx_custom_nan = DUK_STRIDX_JSON_EXT_NAN; + js_ctx->stridx_custom_neginf = DUK_STRIDX_JSON_EXT_NEGINF; + js_ctx->stridx_custom_posinf = DUK_STRIDX_JSON_EXT_POSINF; + js_ctx->stridx_custom_function = DUK_STRIDX_JSON_EXT_FUNCTION1; + } +#endif /* DUK_USE_JC */ +#endif /* DUK_USE_JX || DUK_USE_JC */ + +#if defined(DUK_USE_JX) || defined(DUK_USE_JC) + if (js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM | + DUK_JSON_FLAG_EXT_COMPATIBLE)) { + DUK_ASSERT(js_ctx->mask_for_undefined == 0); /* already zero */ + } + else +#endif /* DUK_USE_JX || DUK_USE_JC */ + { + js_ctx->mask_for_undefined = DUK_TYPE_MASK_UNDEFINED | + DUK_TYPE_MASK_POINTER | + DUK_TYPE_MASK_BUFFER | + DUK_TYPE_MASK_LIGHTFUNC; + } + + DUK_BW_INIT_PUSHBUF(thr, &js_ctx->bw, DUK__JSON_STRINGIFY_BUFSIZE); + + js_ctx->idx_loop = duk_push_object_internal(ctx); + DUK_ASSERT(js_ctx->idx_loop >= 0); + + /* [ ... buf loop ] */ + + /* + * Process replacer/proplist (2nd argument to JSON.stringify) + */ + + h = duk_get_hobject(ctx, idx_replacer); + if (h != NULL) { + if (DUK_HOBJECT_IS_CALLABLE(h)) { + js_ctx->h_replacer = h; + } else if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY) { + /* Here the specification requires correct array index enumeration + * which is a bit tricky for sparse arrays (it is handled by the + * enum setup code). We now enumerate ancestors too, although the + * specification is not very clear on whether that is required. + */ + + duk_uarridx_t plist_idx = 0; + duk_small_uint_t enum_flags; + + js_ctx->idx_proplist = duk_push_array(ctx); /* XXX: array internal? */ + + enum_flags = DUK_ENUM_ARRAY_INDICES_ONLY | + DUK_ENUM_SORT_ARRAY_INDICES; /* expensive flag */ + duk_enum(ctx, idx_replacer, enum_flags); + while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) { + /* [ ... proplist enum_obj key val ] */ + if (duk__enc_allow_into_proplist(duk_get_tval(ctx, -1))) { + /* XXX: duplicates should be eliminated here */ + DUK_DDD(DUK_DDDPRINT("proplist enum: key=%!T, val=%!T --> accept", + (duk_tval *) duk_get_tval(ctx, -2), + (duk_tval *) duk_get_tval(ctx, -1))); + duk_to_string(ctx, -1); /* extra coercion of strings is OK */ + duk_put_prop_index(ctx, -4, plist_idx); /* -> [ ... proplist enum_obj key ] */ + plist_idx++; + duk_pop(ctx); + } else { + DUK_DDD(DUK_DDDPRINT("proplist enum: key=%!T, val=%!T --> reject", + (duk_tval *) duk_get_tval(ctx, -2), + (duk_tval *) duk_get_tval(ctx, -1))); + duk_pop_2(ctx); + } + } + duk_pop(ctx); /* pop enum */ + + /* [ ... proplist ] */ + } + } + + /* [ ... buf loop (proplist) ] */ + + /* + * Process space (3rd argument to JSON.stringify) + */ + + h = duk_get_hobject(ctx, idx_space); + if (h != NULL) { + int c = DUK_HOBJECT_GET_CLASS_NUMBER(h); + if (c == DUK_HOBJECT_CLASS_NUMBER) { + duk_to_number(ctx, idx_space); + } else if (c == DUK_HOBJECT_CLASS_STRING) { + duk_to_string(ctx, idx_space); + } + } + + if (duk_is_number(ctx, idx_space)) { + duk_small_int_t nspace; + /* spaces[] must be static to allow initializer with old compilers like BCC */ + static const char spaces[10] = { + DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE, + DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE, + DUK_ASC_SPACE, DUK_ASC_SPACE + }; /* XXX: helper */ + + /* ToInteger() coercion; NaN -> 0, infinities are clamped to 0 and 10 */ + nspace = (duk_small_int_t) duk_to_int_clamped(ctx, idx_space, 0 /*minval*/, 10 /*maxval*/); + DUK_ASSERT(nspace >= 0 && nspace <= 10); + + duk_push_lstring(ctx, spaces, (duk_size_t) nspace); + js_ctx->h_gap = duk_get_hstring(ctx, -1); + DUK_ASSERT(js_ctx->h_gap != NULL); + } else if (duk_is_string(ctx, idx_space)) { + /* XXX: substring in-place at idx_place? */ + duk_dup(ctx, idx_space); + duk_substring(ctx, -1, 0, 10); /* clamp to 10 chars */ + js_ctx->h_gap = duk_get_hstring(ctx, -1); + DUK_ASSERT(js_ctx->h_gap != NULL); + } else { + /* nop */ + } + + if (js_ctx->h_gap != NULL) { + /* if gap is empty, behave as if not given at all */ + if (DUK_HSTRING_GET_CHARLEN(js_ctx->h_gap) == 0) { + js_ctx->h_gap = NULL; + } + } + + /* [ ... buf loop (proplist) (gap) ] */ + + /* + * Fast path: assume no mutation, iterate object property tables + * directly; bail out if that assumption doesn't hold. + */ + +#if defined(DUK_USE_JSON_STRINGIFY_FASTPATH) + if (js_ctx->h_replacer == NULL && /* replacer is a mutation risk */ + js_ctx->idx_proplist == -1) { /* proplist is very rare */ + duk_int_t pcall_rc; +#ifdef DUK_USE_MARK_AND_SWEEP + duk_small_uint_t prev_mark_and_sweep_base_flags; +#endif + + DUK_DD(DUK_DDPRINT("try JSON.stringify() fast path")); + + /* Use recursion_limit to ensure we don't overwrite js_ctx->visiting[] + * array so we don't need two counter checks in the fast path. The + * slow path has a much larger recursion limit which we'll use if + * necessary. + */ + DUK_ASSERT(DUK_USE_JSON_ENC_RECLIMIT >= DUK_JSON_ENC_LOOPARRAY); + js_ctx->recursion_limit = DUK_JSON_ENC_LOOPARRAY; + DUK_ASSERT(js_ctx->recursion_depth == 0); + + /* Execute the fast path in a protected call. If any error is thrown, + * fall back to the slow path. This includes e.g. recursion limit + * because the fast path has a smaller recursion limit (and simpler, + * limited loop detection). + */ + + duk_push_pointer(ctx, (void *) js_ctx); + duk_dup(ctx, idx_value); + +#if defined(DUK_USE_MARK_AND_SWEEP) + /* Must prevent finalizers which may have arbitrary side effects. */ + prev_mark_and_sweep_base_flags = thr->heap->mark_and_sweep_base_flags; + thr->heap->mark_and_sweep_base_flags |= + DUK_MS_FLAG_NO_FINALIZERS | /* avoid attempts to add/remove object keys */ + DUK_MS_FLAG_NO_OBJECT_COMPACTION; /* avoid attempt to compact any objects */ +#endif + + pcall_rc = duk_safe_call(ctx, duk__json_stringify_fast, 2 /*nargs*/, 0 /*nret*/); + +#if defined(DUK_USE_MARK_AND_SWEEP) + thr->heap->mark_and_sweep_base_flags = prev_mark_and_sweep_base_flags; +#endif + if (pcall_rc == DUK_EXEC_SUCCESS) { + DUK_DD(DUK_DDPRINT("fast path successful")); + DUK_BW_PUSH_AS_STRING(thr, &js_ctx->bw); + goto replace_finished; + } + + /* We come here for actual aborts (like encountering .toJSON()) + * but also for recursion/loop errors. Bufwriter size can be + * kept because we'll probably need at least as much as we've + * allocated so far. + */ + DUK_D(DUK_DPRINT("fast path failed, serialize using slow path instead")); + DUK_BW_RESET_SIZE(thr, &js_ctx->bw); + js_ctx->recursion_depth = 0; + } +#endif + + /* + * Create wrapper object and serialize + */ + + idx_holder = duk_push_object(ctx); + duk_dup(ctx, idx_value); + duk_put_prop_stridx(ctx, -2, DUK_STRIDX_EMPTY_STRING); + + DUK_DDD(DUK_DDDPRINT("before: flags=0x%08lx, loop=%!T, replacer=%!O, " + "proplist=%!T, gap=%!O, holder=%!T", + (unsigned long) js_ctx->flags, + (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop), + (duk_heaphdr *) js_ctx->h_replacer, + (duk_tval *) (js_ctx->idx_proplist >= 0 ? duk_get_tval(ctx, js_ctx->idx_proplist) : NULL), + (duk_heaphdr *) js_ctx->h_gap, + (duk_tval *) duk_get_tval(ctx, -1))); + + /* serialize the wrapper with empty string key */ + + duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING); + + /* [ ... buf loop (proplist) (gap) holder "" ] */ + + js_ctx->recursion_limit = DUK_USE_JSON_ENC_RECLIMIT; + DUK_ASSERT(js_ctx->recursion_depth == 0); + + if (DUK_UNLIKELY(duk__enc_value(js_ctx, idx_holder) == 0)) { /* [ ... holder key ] -> [ ... holder ] */ + /* Result is undefined. */ + duk_push_undefined(ctx); + } else { + /* Convert buffer to result string. */ + DUK_BW_PUSH_AS_STRING(thr, &js_ctx->bw); + } + + DUK_DDD(DUK_DDDPRINT("after: flags=0x%08lx, loop=%!T, replacer=%!O, " + "proplist=%!T, gap=%!O, holder=%!T", + (unsigned long) js_ctx->flags, + (duk_tval *) duk_get_tval(ctx, js_ctx->idx_loop), + (duk_heaphdr *) js_ctx->h_replacer, + (duk_tval *) (js_ctx->idx_proplist >= 0 ? duk_get_tval(ctx, js_ctx->idx_proplist) : NULL), + (duk_heaphdr *) js_ctx->h_gap, + (duk_tval *) duk_get_tval(ctx, idx_holder))); + + /* The stack has a variable shape here, so force it to the + * desired one explicitly. + */ + +#if defined(DUK_USE_JSON_STRINGIFY_FASTPATH) + replace_finished: +#endif + duk_replace(ctx, entry_top); + duk_set_top(ctx, entry_top + 1); + + DUK_DDD(DUK_DDDPRINT("JSON stringify end: value=%!T, replacer=%!T, space=%!T, " + "flags=0x%08lx, result=%!T, stack_top=%ld", + (duk_tval *) duk_get_tval(ctx, idx_value), + (duk_tval *) duk_get_tval(ctx, idx_replacer), + (duk_tval *) duk_get_tval(ctx, idx_space), + (unsigned long) flags, + (duk_tval *) duk_get_tval(ctx, -1), + (long) duk_get_top(ctx))); + + DUK_ASSERT(duk_get_top(ctx) == entry_top + 1); +} + +/* + * Entry points + */ + +DUK_INTERNAL duk_ret_t duk_bi_json_object_parse(duk_context *ctx) { + duk_bi_json_parse_helper(ctx, + 0 /*idx_value*/, + 1 /*idx_replacer*/, + 0 /*flags*/); + return 1; +} + +DUK_INTERNAL duk_ret_t duk_bi_json_object_stringify(duk_context *ctx) { + duk_bi_json_stringify_helper(ctx, + 0 /*idx_value*/, + 1 /*idx_replacer*/, + 2 /*idx_space*/, + 0 /*flags*/); + return 1; +} + +#undef DUK__JSON_DECSTR_BUFSIZE +#undef DUK__JSON_DECSTR_CHUNKSIZE +#undef DUK__JSON_ENCSTR_CHUNKSIZE +#undef DUK__JSON_STRINGIFY_BUFSIZE +#undef DUK__JSON_MAX_ESC_LEN +/* + * Logging support + */ + +/* include removed: duk_internal.h */ + +/* 3-letter log level strings */ +DUK_LOCAL const duk_uint8_t duk__log_level_strings[] = { + (duk_uint8_t) DUK_ASC_UC_T, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_C, + (duk_uint8_t) DUK_ASC_UC_D, (duk_uint8_t) DUK_ASC_UC_B, (duk_uint8_t) DUK_ASC_UC_G, + (duk_uint8_t) DUK_ASC_UC_I, (duk_uint8_t) DUK_ASC_UC_N, (duk_uint8_t) DUK_ASC_UC_F, + (duk_uint8_t) DUK_ASC_UC_W, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_N, + (duk_uint8_t) DUK_ASC_UC_E, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_R, + (duk_uint8_t) DUK_ASC_UC_F, (duk_uint8_t) DUK_ASC_UC_T, (duk_uint8_t) DUK_ASC_UC_L +}; + +/* Constructor */ +DUK_INTERNAL duk_ret_t duk_bi_logger_constructor(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_idx_t nargs; + + /* Calling as a non-constructor is not meaningful. */ + if (!duk_is_constructor_call(ctx)) { + return DUK_RET_TYPE_ERROR; + } + + nargs = duk_get_top(ctx); + duk_set_top(ctx, 1); + + duk_push_this(ctx); + + /* [ name this ] */ + + if (nargs == 0) { + /* Automatic defaulting of logger name from caller. This would + * work poorly with tail calls, but constructor calls are currently + * never tail calls, so tail calls are not an issue now. + */ + + if (thr->callstack_top >= 2) { + duk_activation *act_caller = thr->callstack + thr->callstack_top - 2; + duk_hobject *func_caller; + + func_caller = DUK_ACT_GET_FUNC(act_caller); + if (func_caller) { + /* Stripping the filename might be a good idea + * ("/foo/bar/quux.js" -> logger name "quux"), + * but now used verbatim. + */ + duk_push_hobject(ctx, func_caller); + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_FILE_NAME); + duk_replace(ctx, 0); + } + } + } + /* the stack is unbalanced here on purpose; we only rely on the + * initial two values: [ name this ]. + */ + + if (duk_is_string(ctx, 0)) { + duk_dup(ctx, 0); + duk_put_prop_stridx(ctx, 1, DUK_STRIDX_LC_N); + } else { + /* don't set 'n' at all, inherited value is used as name */ + } + + duk_compact(ctx, 1); + + return 0; /* keep default instance */ +} + +/* Default function to format objects. Tries to use toLogString() but falls + * back to toString(). Any errors are propagated out without catching. + */ +DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_fmt(duk_context *ctx) { + if (duk_get_prop_stridx(ctx, 0, DUK_STRIDX_TO_LOG_STRING)) { + /* [ arg toLogString ] */ + + duk_dup(ctx, 0); + duk_call_method(ctx, 0); + + /* [ arg result ] */ + return 1; + } + + /* [ arg undefined ] */ + duk_pop(ctx); + duk_to_string(ctx, 0); + return 1; +} + +/* Default function to write a formatted log line. Writes to stderr, + * appending a newline to the log line. + * + * The argument is a buffer whose visible size contains the log message. + * This function should avoid coercing the buffer to a string to avoid + * string table traffic. + */ +DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_raw(duk_context *ctx) { + const char *data; + duk_size_t data_len; + + DUK_UNREF(ctx); + DUK_UNREF(data); + DUK_UNREF(data_len); + +#ifdef DUK_USE_FILE_IO + data = (const char *) duk_require_buffer(ctx, 0, &data_len); + DUK_FWRITE((const void *) data, 1, data_len, DUK_STDERR); + DUK_FPUTC((int) '\n', DUK_STDERR); + DUK_FFLUSH(DUK_STDERR); +#else + /* nop */ +#endif + return 0; +} + +/* Log frontend shared helper, magic value indicates log level. Provides + * frontend functions: trace(), debug(), info(), warn(), error(), fatal(). + * This needs to have small footprint, reasonable performance, minimal + * memory churn, etc. + */ +DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_log_shared(duk_context *ctx) { + duk_hthread *thr = (duk_hthread *) ctx; + duk_double_t now; + duk_small_int_t entry_lev = duk_get_current_magic(ctx); + duk_small_int_t logger_lev; + duk_int_t nargs; + duk_int_t i; + duk_size_t tot_len; + const duk_uint8_t *arg_str; + duk_size_t arg_len; + duk_uint8_t *buf, *p; + const duk_uint8_t *q; + duk_uint8_t date_buf[DUK_BI_DATE_ISO8601_BUFSIZE]; + duk_size_t date_len; + duk_small_int_t rc; + + DUK_ASSERT(entry_lev >= 0 && entry_lev <= 5); + DUK_UNREF(thr); + + /* XXX: sanitize to printable (and maybe ASCII) */ + /* XXX: better multiline */ + + /* + * Logger arguments are: + * + * magic: log level (0-5) + * this: logger + * stack: plain log args + * + * We want to minimize memory churn so a two-pass approach + * is used: first pass formats arguments and computes final + * string length, second pass copies strings either into a + * pre-allocated and reused buffer (short messages) or into a + * newly allocated fixed buffer. If the backend function plays + * nice, it won't coerce the buffer to a string (and thus + * intern it). + */ + + nargs = duk_get_top(ctx); + + /* [ arg1 ... argN this ] */ + + /* + * Log level check + */ + + duk_push_this(ctx); + + duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LC_L); + logger_lev = (duk_small_int_t) duk_get_int(ctx, -1); + if (entry_lev < logger_lev) { + return 0; + } + /* log level could be popped but that's not necessary */ + + now = DUK_USE_DATE_GET_NOW(ctx); + duk_bi_date_format_timeval(now, date_buf); + date_len = DUK_STRLEN((const char *) date_buf); + + duk_get_prop_stridx(ctx, -2, DUK_STRIDX_LC_N); + duk_to_string(ctx, -1); + DUK_ASSERT(duk_is_string(ctx, -1)); + + /* [ arg1 ... argN this loggerLevel loggerName ] */ + + /* + * Pass 1 + */ + + /* Line format: