From 087b79d3f08598c54c7f65f047b21cb4f63411eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Tue, 28 Nov 2017 01:57:56 +0200 Subject: [PATCH] eth/tracers, vendor: swap ottovm to duktape for tracing --- eth/tracers/internal/tracers/assets.go | 2 +- eth/tracers/internal/tracers/call_tracer.js | 146 +- internal/ethapi/tracer.go | 650 +- internal/ethapi/tracer_test.go | 11 +- .../olebedev/go-duktape.v3/Gopkg.lock | 21 + .../olebedev/go-duktape.v3/Gopkg.toml | 3 + .../olebedev/go-duktape.v3/LICENSE.md | 21 + .../gopkg.in/olebedev/go-duktape.v3/README.md | 118 + vendor/gopkg.in/olebedev/go-duktape.v3/api.go | 1616 + .../gopkg.in/olebedev/go-duktape.v3/conts.go | 121 + .../olebedev/go-duktape.v3/duk_alloc_pool.c | 612 + .../olebedev/go-duktape.v3/duk_alloc_pool.h | 223 + .../olebedev/go-duktape.v3/duk_config.h | 3672 + .../olebedev/go-duktape.v3/duk_console.c | 163 + .../olebedev/go-duktape.v3/duk_console.h | 14 + .../olebedev/go-duktape.v3/duk_logging.c | 380 + .../olebedev/go-duktape.v3/duk_logging.h | 20 + .../go-duktape.v3/duk_minimal_printf.c | 312 + .../go-duktape.v3/duk_minimal_printf.h | 12 + .../go-duktape.v3/duk_module_duktape.c | 471 + .../go-duktape.v3/duk_module_duktape.h | 14 + .../olebedev/go-duktape.v3/duk_module_node.c | 333 + .../olebedev/go-duktape.v3/duk_module_node.h | 9 + .../olebedev/go-duktape.v3/duk_print_alert.c | 127 + .../olebedev/go-duktape.v3/duk_print_alert.h | 10 + .../olebedev/go-duktape.v3/duk_v1_compat.c | 131 + .../olebedev/go-duktape.v3/duk_v1_compat.h | 28 + .../gopkg.in/olebedev/go-duktape.v3/duktape.c | 95118 ++++++++++++++++ .../olebedev/go-duktape.v3/duktape.go | 356 + .../gopkg.in/olebedev/go-duktape.v3/duktape.h | 1349 + .../gopkg.in/olebedev/go-duktape.v3/timers.go | 136 + .../gopkg.in/olebedev/go-duktape.v3/utils.go | 10 + .../olebedev/go-duktape.v3/wercker.yml | 14 + vendor/vendor.json | 6 + 34 files changed, 105933 insertions(+), 296 deletions(-) create mode 100644 vendor/gopkg.in/olebedev/go-duktape.v3/Gopkg.lock create mode 100644 vendor/gopkg.in/olebedev/go-duktape.v3/Gopkg.toml create mode 100644 vendor/gopkg.in/olebedev/go-duktape.v3/LICENSE.md create mode 100644 vendor/gopkg.in/olebedev/go-duktape.v3/README.md create mode 100644 vendor/gopkg.in/olebedev/go-duktape.v3/api.go create mode 100644 vendor/gopkg.in/olebedev/go-duktape.v3/conts.go create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_alloc_pool.c create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_alloc_pool.h create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_config.h create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_console.c create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_console.h create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_logging.c create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_logging.h create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_minimal_printf.c create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_minimal_printf.h create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_module_duktape.c create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_module_duktape.h create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_module_node.c create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_module_node.h create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_print_alert.c create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_print_alert.h create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_v1_compat.c create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duk_v1_compat.h create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duktape.c create mode 100644 vendor/gopkg.in/olebedev/go-duktape.v3/duktape.go create mode 100755 vendor/gopkg.in/olebedev/go-duktape.v3/duktape.h create mode 100644 vendor/gopkg.in/olebedev/go-duktape.v3/timers.go create mode 100644 vendor/gopkg.in/olebedev/go-duktape.v3/utils.go create mode 100644 vendor/gopkg.in/olebedev/go-duktape.v3/wercker.yml diff --git a/eth/tracers/internal/tracers/assets.go b/eth/tracers/internal/tracers/assets.go index e438692a72..07b0e64a29 100644 --- a/eth/tracers/internal/tracers/assets.go +++ b/eth/tracers/internal/tracers/assets.go @@ -93,7 +93,7 @@ func _4byte_tracerJs() (*asset, error) { return a, nil } -var _call_tracerJs = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x9c\x57\xcd\x6e\xdc\x48\x0e\x3e\xb7\x9e\x82\x93\x43\xdc\x8d\x74\xd4\x76\x92\xcd\xa1\x0d\xcd\xc2\xbb\x31\x76\x03\x78\xe3\x41\xc6\x33\x73\x08\x7c\xa8\x96\x28\x75\x8d\xa5\x2a\x6d\x15\xd5\x3f\x18\xf8\xdd\x17\x64\x95\x64\xb5\xec\x09\x16\xe3\x93\x9a\x45\xb2\xf8\xf3\xf1\x63\x79\xb5\x82\x5c\xd5\xf5\x9d\x53\x39\x3a\xd0\x1e\x14\x94\x5d\x5d\xc3\xa6\xb6\x7b\x03\xe4\x94\xf1\x2a\x27\x6d\xe5\x9b\x55\x68\xab\x08\xf0\xc0\xbf\xc8\x83\x32\x05\x38\x6c\xad\xe3\xef\xba\x4e\x56\x2b\xa0\x2d\x82\x36\x84\xce\xa8\x5a\x7c\x7b\x68\x54\x81\xb0\x39\x82\x1a\x3b\x5c\x82\xaa\xad\xa9\x60\xaf\x69\x0b\xca\x1c\xa1\xf3\x58\x76\x35\x68\x53\x5a\xd7\x28\x56\x49\x93\x3f\x92\xd9\x6a\x05\xda\xec\x6c\x2e\x12\x1f\x42\x34\xe8\x09\x0b\x28\x14\x29\xf0\xe4\xba\x9c\x3a\x87\x90\x5b\x43\x4a\x1b\x6d\x2a\x8e\x65\x12\x88\x35\x12\xb2\xf8\x0b\x51\xe1\x01\xf3\x8e\xdd\x4c\x23\x4b\x93\xd9\xe8\xc6\x35\x7c\xbb\x5f\x26\x4f\x76\xa4\xf2\x07\x8e\x82\xdd\xe7\x9d\x73\x68\x08\x1c\xe6\x9d\xf3\x7a\x87\xa2\x02\x41\xc7\x96\xa2\x73\xfd\xeb\x7f\xe2\x55\xc1\xf5\xe0\x64\xe4\xb8\x40\x9f\xa3\x29\xb0\x90\x2a\x3f\x78\xd8\x6f\x91\xb6\xe8\x60\x8f\x67\x3b\x84\xdf\x3b\x4f\x23\x9d\xd2\xd9\x06\x94\x01\xdb\x11\x37\x64\xd4\x23\x6d\xc8\x8a\x43\xc5\xdf\x06\x9d\xc4\x93\x26\xb3\xc1\x78\x0d\xa5\xaa\x3d\xc6\x7b\x3d\x61\xcb\xb9\x70\xba\x0f\xec\xd9\x3a\xc0\x1d\xba\x23\xd8\x36\xb7\x05\x86\x6e\x73\x16\x43\x12\xe8\xd3\x64\xc6\x76\x6b\x28\x3b\x23\xd7\xce\x6b\x5b\x2d\xa1\xd8\x2c\xe0\x8f\x64\xc6\x6e\x7f\x43\xb0\xa6\x3e\x42\xae\x1c\x82\xda\xd8\x8e\xc0\x1f\x3d\x61\x13\xdd\xfa\x25\x94\xca\x73\xf0\xba\x84\x3d\x42\xeb\xf0\x6d\xbe\x45\xae\x99\xc9\x31\x99\xcd\x76\xca\xb1\x85\x14\x33\x03\xf6\x9f\xda\x36\x25\xfb\xa5\x6b\x36\xe8\xe6\x0b\x78\x0d\xe7\x87\xf2\x7c\x01\x59\x26\x1f\x97\xc9\x6c\xa6\x4b\x98\x47\x9b\x10\x88\x78\xb1\x2d\x64\x30\xd8\xff\x4c\x4e\x9b\x6a\xbe\x60\xfd\xc7\x10\xeb\xe7\x52\xf0\xb4\x87\x06\x69\x6b\x8b\x11\xd6\xb8\x30\x1b\x64\x34\x15\xd6\xe0\x12\x54\x51\x00\xd9\xd0\xf7\xa1\xcb\xa7\x17\xc3\xeb\xd7\x7c\xe3\x0f\x19\xbc\xfa\x7a\x7d\xf7\xcb\xd7\x2f\xaf\x4e\x24\xbf\x5e\x7f\xbd\x7b\x15\x83\x5b\xad\xe0\xe7\x07\xdd\x0a\xf2\x25\x7f\xdb\xb4\xba\xc6\x31\xd6\x97\x40\x5b\xeb\x11\xb8\x8a\x02\x81\x52\x99\xbc\x6f\x8d\xef\x33\x24\x0b\x19\x90\xbd\x2a\x0a\x87\xde\x4b\xad\x24\xb2\xb4\x45\x7c\x98\x5f\x2c\xd2\x7f\x1c\x09\xfd\x7c\x21\x49\x4b\xb0\xda\xff\xe4\x30\xde\x57\xcc\xc9\x2e\x62\x48\x33\x87\xd4\x39\xc3\x9f\x8f\x31\xc4\x5b\x46\xe1\x5e\x7b\x84\x4a\x09\x20\xbd\x6d\x26\xe3\x0d\x05\x92\xd2\xf5\x10\x8f\x2d\x4b\xee\x19\x17\x3e\x83\xb3\x4f\xd7\x37\xd7\xff\xba\xba\xbb\xfe\xe7\xd5\xcd\xcd\x19\xfc\x1d\xce\x61\x0d\x17\x8b\xcb\xa4\xd7\xd6\xe6\x56\xf4\x27\x61\xbf\x83\x37\xec\x68\x91\x7e\x36\xf4\xf1\x43\x68\x58\xd4\xbf\x36\x05\x64\xd1\xee\xcd\xd4\xee\xfd\x33\xbb\x98\xc8\x95\xf7\xd8\x6c\x6a\x7c\x4e\x4f\x91\xbf\x84\xca\x3c\x59\x87\x32\x05\x5c\x9e\x1a\xb9\x0d\xfd\xcd\x11\x8c\xa1\x52\x74\x6c\x71\x0d\x00\x60\xdb\xa5\x08\x78\x26\x45\xc0\x01\xa9\x3c\xb7\x9d\xa1\x70\x42\x56\xe4\x00\x64\x83\x40\x9b\xb6\xa3\xb5\x08\xfe\x8d\x07\x69\x58\x83\x8d\x75\xc7\xd4\xd7\x3a\xc7\xb9\xa4\xb6\x0c\x99\x2e\x16\xc1\xa6\x52\xfe\x93\x2e\xcb\xb5\xb8\xaf\x94\x87\xb7\xf2\x95\x5b\x1f\x6f\xb1\x1d\xdd\xf2\xf9\xb4\x20\x1f\x26\x05\x19\xb4\x6f\xd0\x3c\xd7\xfe\xdb\x44\x5b\xa0\x30\xe0\x26\xe0\xf8\xb4\xa5\x3d\x74\x84\x67\x76\xaa\xee\x10\x32\x38\x3b\x3f\x9c\x3d\xef\xcd\xbb\x45\x7a\x87\x07\x9a\x5f\x7c\x0c\xed\x14\x8c\xd1\x56\xfb\x74\xa0\xc4\xb4\xed\xfc\x76\x2e\x23\x7c\x39\x9c\x3e\x11\x5f\x06\xe4\x3a\xe6\x87\x88\xd4\xd3\x31\x7e\x91\x2b\x99\x11\x4f\xd9\x70\x09\x0e\xc9\x69\xdc\x21\x68\x3a\xf3\xe2\x92\xf7\x85\xdd\x2b\x93\x63\x0a\xbf\x61\xf0\x68\x10\x65\xde\xe3\xae\x63\xaa\x12\xe2\xe5\x7d\xa5\xcd\x13\x0d\x28\x59\x05\x0e\xa1\x51\x47\xd8\x20\xb3\xe2\xc3\x11\xb8\x47\xc5\xd1\xa8\x46\xe7\x3e\xf8\x93\x3d\xe7\xb0\x52\x4e\xdc\x3a\xfc\x6f\x17\xb6\x18\x03\x4f\xe5\xd4\xa9\xba\x3e\x42\xa5\x77\x68\xc4\x7a\xfe\xee\xfd\xf9\x39\x78\xd2\x2d\x9a\x62\x09\x1f\xdf\xaf\x3e\x7e\x00\xd7\xd5\xb8\x48\x23\xe9\x9c\x56\x27\x76\x82\x0f\xb8\xf0\x05\xb6\xb4\x85\x1f\x61\x52\xe0\x1a\x4d\x45\xdb\xbe\x6b\xa7\x87\xdf\x5e\xd4\x85\xb7\x70\x71\x2f\x98\xcb\x7a\xf4\x85\xf6\x01\xd6\x1e\xff\xa2\x23\xce\xed\x12\x56\x2b\xb8\xbb\xfd\x74\x0b\xf3\x07\xe5\x54\xad\x36\xb8\x58\xc3\xb5\x6b\xd2\x34\x3d\xc5\xc7\x18\x01\xb2\xbe\x26\xfc\x6d\x00\x0f\xda\x13\x53\xb5\xb4\x44\x7b\x08\x00\xd1\xa6\x5a\x42\x6b\x5b\x21\xa5\xff\x83\xb8\x99\xb1\x02\x4d\xf7\xc0\x16\x5c\x3b\xde\x89\x14\xe1\x77\xf9\x1c\x7f\xa7\x35\xcf\xb2\xef\x16\x7d\xb5\x82\x9f\x46\x11\xd5\xca\x53\x04\x92\x29\xa0\xc2\xb0\x6e\x87\x07\x03\x38\xf4\x5d\x4d\x7e\xc2\x42\xd3\xb1\xb1\x6d\xcf\x75\x12\x70\xa5\xfc\x2f\x5e\xca\x15\x47\x71\xa3\xab\xf4\x0b\xee\x3f\x1b\x9a\xf7\x0a\x91\x42\xf8\xeb\x0d\xf4\x42\x26\x99\xc9\x98\x0e\xfa\xfc\xf7\x3d\x87\x13\xbb\x02\x6b\x24\x3c\xf1\x3c\x8a\xd0\x76\xd4\x76\x52\xd2\x97\x49\xb0\x57\x12\x2a\x1c\xfd\xe8\x63\x0d\x0c\xb6\x78\x7e\x55\x50\x7b\x49\x7c\x83\xe6\x69\x1d\xb4\x3c\x55\xfd\x3a\x1f\xed\xfc\x5a\x7b\x19\x75\xb2\x6d\x63\x3d\xf5\x65\xaf\xb1\xa4\xe7\x65\x0f\x7d\x0d\x4e\x05\x03\xa2\x95\xc1\xf9\xc9\x7c\x8d\x36\xfa\x94\xdf\x5e\xd8\xb8\x57\x6d\xab\xf8\x4d\x59\x1f\x41\x13\xec\xd5\xe8\xb9\x1b\xd8\x4b\x9b\xdf\x91\xe9\xc8\xc4\xe0\x5b\x87\x3b\x6d\x3b\x0f\xd6\x60\x1f\xc8\x64\x1c\x39\xae\xb7\x17\xf7\x41\x02\x59\x96\x41\x67\x0a\x2c\xb5\x19\x58\x63\x3a\xc0\x13\x0b\xf8\x76\xff\x67\x94\x7d\xaa\x3a\x49\xf0\x31\x99\x3d\xc6\xa7\x66\xc0\xf1\xf8\xb1\xb9\xdf\xa2\x19\x1e\xea\xf1\x51\x03\x5b\xb5\x43\xd8\x20\x1a\xd0\x84\x4e\x71\xda\x76\x87\x2e\xfe\xa3\xc1\xc5\xf2\xe2\x8e\x6d\x4a\xcd\x3b\x3c\x3a\x8e\xaf\x6d\xe6\x6a\x6d\xaa\x34\x99\x05\xf9\xe8\x95\x9a\xd3\x21\x64\xcb\x0d\x8d\x56\x71\x9f\x0f\xeb\x3c\xa7\x43\xca\x3f\x64\x4d\x0e\x3b\xfd\xe9\x75\xc5\xe7\x2c\x0e\x7b\x74\xb4\xda\xc7\x0a\x64\xc3\xb1\xac\x43\xd6\x88\x33\xc3\x67\x22\x1b\x06\x45\xd4\x2a\xe5\x83\x9b\x17\x46\x8b\x0e\xa7\x93\xd5\x1b\xf0\x74\xaf\xff\xdc\x80\x8f\x27\x46\x93\x57\x07\x2b\x8a\x28\x9c\x86\x71\x5c\x8f\x4f\x83\x28\x26\xaa\x9b\x51\x7d\x74\x23\xf5\x79\xbc\x1c\xef\xa1\x31\xca\x23\xdf\xff\x38\x0c\x42\xa8\xf6\x80\xa6\xa9\xc1\x98\x47\xf9\x06\x74\xce\x3a\xf8\xe1\x05\x9c\x46\x47\x41\x21\x83\x41\xb9\xf7\x10\x20\x12\xbb\x7b\x99\xcc\x1e\x93\xc7\xe4\x7f\x01\x00\x00\xff\xff\x91\x76\xe8\xd1\xe3\x0e\x00\x00") +var _call_tracerJs = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xd4\x58\x4d\x6f\xe3\x38\x12\x3d\xdb\xbf\xa2\xfa\xd2\xb1\xb7\x1d\xd9\xe9\x9e\xed\x83\x03\xef\x22\x48\xbc\xb3\x01\xb2\x9b\x41\xc7\x33\x73\x68\xf4\x81\x96\x4a\x32\x27\x32\xa9\x21\x29\x7f\xa0\xc7\xff\x7d\x51\x45\x4a\x96\x65\x3b\x1b\x74\x03\x8b\x9d\x9b\x45\x56\x3d\x15\xeb\xe3\xf1\xc9\xc3\x21\xc4\x22\xcf\x67\x46\xc4\x68\x40\x5a\x10\x90\x96\x79\x0e\xf3\x5c\xaf\x15\x38\x23\x94\x15\xb1\x93\x9a\x7f\x93\x89\x5b\x08\x07\xb8\xa1\x27\x67\x41\xa8\x04\x0c\x16\xda\xd0\xef\x3c\xef\x0e\x87\xe0\x16\x08\x52\x39\x34\x4a\xe4\x8c\x6d\x61\x29\x12\x84\xf9\x16\x44\x13\x70\x00\x22\xd7\x2a\x83\xb5\x74\x0b\x10\x6a\x0b\xa5\xc5\xb4\xcc\x41\xaa\x54\x9b\xa5\x20\x93\xa8\xfb\xb5\xdb\x09\x11\x5a\x27\xe2\x67\x0a\x90\xf0\xe3\xd2\x18\x54\x0e\x0c\xc6\xa5\xb1\x72\x85\x6c\x02\xde\x46\xa7\x6c\x33\xfd\xe5\x5f\x80\x1b\x8c\x4b\x8f\xd4\xa9\x41\xc6\xf0\xf9\xeb\xee\xcb\xa0\xcb\xd0\x09\xda\x18\x55\x82\x09\x9f\xef\xd9\xc2\x7a\x81\x6e\x81\x06\xd6\x78\xb1\x42\xf8\xad\xb4\xae\x61\x93\x1a\xbd\x04\xa1\x40\x97\x8e\x52\xd1\xc8\x8e\x54\x4e\x33\xa0\xa0\xdf\x0a\x0d\x47\x14\x75\x3b\xb5\xf3\x18\x52\x91\x5b\x0c\xef\xb5\x0e\x0b\x3a\x8d\x54\x2b\xfd\x4c\xc8\xda\x00\xae\xd0\x6c\x41\x17\xb1\x4e\xd0\xe7\x99\xce\x51\x1f\x03\x6d\xd4\xed\x90\xdf\x18\xd2\x52\xf1\x6b\x7b\xb9\xce\x06\x90\xcc\xfb\xf0\xb5\xdb\x21\xd8\x5f\x11\xb4\xca\xb7\x10\x0b\x83\x20\xe6\xba\x74\x60\xb7\xd6\xe1\x32\xc0\xda\x01\xa4\xc2\x52\xf0\x32\x85\x35\x42\x61\xf0\x32\x5e\x20\x65\x4d\xc5\xd8\xed\x74\x56\xc2\x90\x07\xa7\x73\x02\x84\x1f\xe9\x22\x72\xfa\xdf\xe5\x72\x8e\xa6\xd7\x87\xb7\x30\xda\xa4\xa3\x3e\x4c\x26\xfc\xe3\xba\xdb\xe9\xc8\x14\x7a\xc1\xc7\x07\xc2\x28\xba\x80\x09\xd4\xfe\x4f\xce\x48\x95\xf5\xfa\x64\xbf\xf3\xb1\xde\xa7\x20\x40\xe1\x1a\x62\xad\xb8\x9d\x28\x1f\x73\x94\x2a\x83\xd8\xa0\x70\x98\x0c\x40\x24\x09\x38\xed\x6b\x5e\x57\xf8\xf0\x95\xf0\xf6\x2d\xbf\x6b\x02\x17\xb7\x9f\xa6\x37\xb3\xe9\x45\x23\x08\xa9\x1e\xd3\x34\xc4\xc1\xbe\x51\x81\xf8\xdc\xbb\xea\x47\x2b\x91\x97\xf8\x98\xfa\x88\x82\xed\x54\x25\x30\x09\x3e\xef\xda\x3e\xef\x0f\x7c\xc8\x69\x38\x84\x1b\x6b\x71\x39\xcf\xf1\xb8\xeb\xc3\x58\xf0\x84\x58\xa7\x0d\x72\x89\x63\xbd\x2c\x72\xa4\xd2\x55\x6f\x0d\x99\xe6\x88\x3b\x6e\x5b\xe0\x18\x00\x40\x17\x03\x5e\xa0\x86\xe3\x05\xa7\xff\x89\x1b\x2e\x47\x95\xad\x28\x43\x77\x93\x24\x06\xad\xed\xf5\xfb\xde\x5c\xaa\xa2\x74\xe3\x03\xf3\x25\x2e\xb5\xd9\x46\x36\x97\x31\xf6\xf8\x68\x03\x7f\xd2\xca\x27\x13\xf6\x5e\x91\x0f\x59\x67\xe8\x7e\x14\xb6\xb7\xdf\xba\xd5\xd6\x8d\xab\x2d\x7a\xa8\xf6\x38\x17\xe4\x76\x31\xda\x5c\x1c\x67\x6b\xd4\xdf\x17\xfd\xea\x63\x9f\x5c\x76\x9c\x69\xb7\x90\x36\xaa\x67\x31\x2a\x4a\xbb\xe8\x71\xe7\xec\x77\xf7\xf3\x36\x01\x67\x4a\x6a\xcb\x8e\x41\x57\x1a\xd5\xee\x9e\xe3\xce\xb1\x98\xa7\x34\xb0\xce\x94\x31\x77\x50\x26\x78\x9c\x79\x9c\x04\xd1\x9b\x2d\xe7\x9c\x73\xa7\xf5\xd9\x46\x7a\x9a\x3e\xfc\xe3\x6e\xfa\x34\xfb\xf4\xf3\xed\xac\xd9\x4e\x39\xa6\x8e\x82\x3a\x3c\x43\x8e\x2a\x73\x0b\x8e\x9f\xe0\x0e\x77\x3f\x93\xcf\xe5\xd5\x17\xbf\x02\x93\xc9\x04\x4a\x95\x60\x2a\x15\x26\xfd\xaa\xec\x2f\x7a\xc0\xe7\x2f\x8c\xbd\x3b\x4e\xdf\xa1\xa9\x4f\xe6\xe9\x4e\xda\xf9\xfc\xfa\x34\x9e\x98\xc1\x25\xba\x85\x4e\x98\x8e\x62\xe1\x19\xad\x4a\x69\xa2\x15\xbe\x7a\x12\x7b\xd5\x28\xde\x3c\x3c\x5c\xc0\x1f\x7f\x40\xe3\xf9\xf6\xf1\x6e\xda\x5c\xbb\x9b\x3e\x4c\x7f\xbc\x99\x4d\xdb\xb6\x4f\xb3\x9b\xd9\xfd\x2d\xaf\xf6\x43\x8a\x86\x43\x78\x7a\x96\x05\xdf\x13\xcc\x59\x7a\x59\xc8\x1c\x1b\xf1\xda\x01\xb8\x85\xb6\x08\xc4\x7c\x4c\xdb\xa9\x50\x71\x45\xa7\xb6\xaa\xa0\xd3\x54\x3f\x5d\x0d\xce\x31\x2f\x34\xbb\xb6\x5f\xd7\x54\xda\x9f\x0c\x86\x97\x26\x3d\xa7\xab\xb8\xf6\x09\xf5\xe5\x61\xe2\x63\xc6\xe9\xbd\xfe\x90\xf0\x77\x18\xc1\x18\xae\x02\xad\xbc\xc0\x5b\xef\xe1\x1d\xc1\x7f\x03\x7b\x7d\x38\xe1\xf9\xff\xc9\x61\x4e\xb3\x71\x65\xee\xf4\xff\x9e\xdb\x74\xe9\x1e\xd3\x74\x0c\xed\x24\xfe\x70\x94\xc4\xda\xfe\x01\xd5\xb1\xfd\x5f\x8f\xec\xf7\x3c\x48\x5d\xa5\x0b\x78\x73\xd4\x22\x9e\x85\xde\xb4\xe6\x20\x24\x97\x45\x05\xa3\xc1\xe4\x0c\xf3\xbe\x3f\xec\xe1\x73\xd4\xf1\x5d\xcc\x7b\x52\x1c\x91\x04\x3a\x94\x3f\x03\x30\xe8\x8c\xc4\x15\x82\x74\x17\x96\x21\x49\x26\xea\xb5\x50\x31\x46\xf0\x2b\x7a\x44\x85\xc8\xe4\x12\x64\x25\x69\x13\x56\x5a\x24\x0d\xa5\xda\x73\x8e\x60\xf5\x67\x10\x96\x62\x0b\x73\x24\x19\xf4\xbc\x85\x4c\x58\x48\xb6\x4a\x2c\x65\x6c\x3d\x1e\x4b\x4a\x83\x99\x30\x0c\x6b\xf0\xf7\x12\xad\xc3\x84\x1b\x59\xc4\xae\x14\x79\xbe\x85\x4c\xae\x50\xb1\x77\xef\xfd\x87\xd1\x08\xac\x93\x05\xaa\x64\x00\x1f\x3f\x0c\x3f\xfe\x00\xa6\xcc\xb1\x1f\x75\x1b\x9c\x5e\x1f\x35\x54\x83\x36\x42\xf7\xdc\x61\xe1\x16\xbd\x3e\xfc\xed\xcc\xe5\x70\x86\xe9\x4f\xda\xc2\x25\x5c\x7d\x89\x28\xae\xc9\x41\xdf\xfa\x4a\x02\xe6\x16\x03\xda\x70\x08\xb3\xc7\xbb\xc7\xde\xb3\x30\x22\x17\x73\xec\x8f\x61\x56\xe5\x6a\x2d\x82\xee\xa6\xa2\x40\x91\x0b\xa9\x40\xc4\xb1\x2e\x95\xa3\xc4\x57\x12\x3a\xdf\x12\xbf\x5f\xb8\x0a\x6f\x21\x56\x48\x76\x68\x6d\x45\xf7\x5c\x35\x0a\x47\x2c\xc9\x1b\xa4\xb2\x32\xc1\x46\x55\x88\x1d\x34\x53\x73\xb0\x58\xcb\x3c\xaf\x00\x97\xda\xd2\x4b\xe6\x08\x6b\x43\x72\xdf\x4a\x15\x53\x3b\x40\x82\x94\x6d\x0b\x5a\x81\x80\x5c\x3b\xd2\xed\x3c\xe3\x20\x4c\x66\x23\xcf\xf7\xf4\x5a\xe2\x1c\xa5\xd7\xd1\x61\x23\x37\x5b\x95\x85\x75\x4b\x1b\x28\xc0\x8d\xb4\x8e\xd5\x24\x45\x29\x2d\xf8\x4e\x96\x2a\x1b\x40\xa1\x0b\xe6\xe9\x57\x0a\xcb\x4f\xd3\x5f\xa6\x9f\x6a\x25\xf0\xfa\x22\x1a\x92\xf4\x2e\x0c\xd3\xf5\xf1\x34\x9d\xe8\xa0\xc9\x99\x0e\x22\xc0\xfd\x65\xf8\x53\x23\xfe\x5c\x58\xb7\xaf\x44\x86\xfe\xb3\xa1\xfe\xf4\x01\x83\xb6\xcc\x9d\x6d\x91\x75\x9b\x0d\x74\x51\x5d\x09\x14\x14\xf3\x0c\x31\xf9\x09\x5d\x1d\x32\xec\x9a\x9d\x26\xc0\xdb\x34\x26\x9e\xf7\x2b\x7d\x26\x3c\xc9\x73\x84\xba\x74\x54\x65\xba\x96\xf7\x9c\x96\x09\xfb\xb3\xe5\x62\x06\x56\x9b\xcb\xec\x5e\xb9\x5e\xb5\x79\xaf\xe0\x12\xaa\x07\xe2\x6a\xb8\x3c\x18\x8e\x13\xa4\xd7\x49\x30\x47\x87\xb0\x87\xb8\x86\xd6\x12\x01\xf9\x43\x73\x6a\x0c\xba\xe3\x3b\x77\x14\xd0\x28\x2d\x6f\x0c\xba\x08\x7f\x2f\x45\x6e\x7b\xa3\x5a\x03\xf8\x13\x38\xcd\xb7\xd6\xa4\xbe\xb7\xaa\x8b\x8d\x7c\x0e\x54\x45\x00\xf4\x6e\x21\x1b\x95\x5b\x32\xf7\x97\x51\x82\x2f\x22\x04\x88\x03\x36\xf0\x78\xc7\x2d\xe7\x87\xa6\x4d\x1c\x27\x2a\x78\xf3\xf0\xf0\x97\x56\x01\xdb\xb5\xaa\x7b\x83\x06\xf3\xcd\x29\x2d\xfb\xad\xe5\x7c\x57\x3f\xbe\xa2\xb2\xaf\x2e\x6d\x7d\xfc\xef\xaf\x6f\xab\x50\x47\xfa\xa3\x32\x62\x15\xd2\x78\xa8\x4e\xe6\x45\xc2\x37\x54\xee\xe0\xb0\x1e\xf3\xf0\xb4\x1e\x79\x7f\xd1\xff\xf7\x22\xd5\xbb\xe7\xea\x73\x4e\x43\x50\xe3\xa8\xdf\x30\x76\xfb\xe6\xe1\x6b\x9f\x9e\x0a\x83\x2b\xa9\x4b\x62\x74\xfc\x33\x7d\x30\xd5\x1a\x68\xd7\xed\xec\xc2\xdf\x33\x9e\x33\x9b\x7f\xd0\xac\x17\xa8\x80\xbf\x1b\x69\x2c\xfc\x47\x85\xbf\x2d\xe7\x88\x0a\xa4\x43\x23\x48\x62\xe8\x15\x9a\xf0\xb7\x18\x31\xbd\x65\x38\xf2\x49\x25\xc9\xea\x00\x1c\xfe\xa3\x22\x72\x94\x2a\x8b\xba\x1d\xbf\xde\xf8\x67\x27\x76\x1b\x7f\x5a\xdf\xb8\xec\x15\x24\x76\xad\xb0\x63\xb7\x61\x92\x66\x15\xda\x92\xd9\xb4\x47\x4b\x5e\xa2\xb6\x44\x35\x3b\x06\x61\xdd\xfe\x90\xa7\x3d\x5e\x3b\x68\x01\x36\xcd\x84\xf5\x30\xad\xa6\x71\x9b\xe3\x9e\xa9\x1c\x88\x06\xc6\xa7\x1d\x68\xeb\x84\x53\x4b\xe8\x93\x31\x2f\xf9\x5d\x3f\x86\xe3\xe6\xae\x5f\x0a\x07\x95\xcb\x46\x6e\xe4\x92\x73\xb3\xbb\xee\x9e\xec\xb4\x51\xd5\x32\xa7\x86\xc4\xe7\xbc\xee\xa9\x33\xae\xcd\x9b\x9c\x5e\x89\xc6\x68\xf3\x12\x9e\x37\x98\x40\x6d\xcc\xad\x1a\xc6\x39\xd8\xf8\x03\x35\xa1\x8f\xdf\xee\xd9\xe2\x10\xfb\x88\x41\xc8\xdd\xf7\x61\x80\xbe\xee\x76\x76\xdd\x5d\xf7\x3f\x01\x00\x00\xff\xff\xb1\x67\x2f\xbe\xf6\x15\x00\x00") func call_tracerJsBytes() ([]byte, error) { return bindataRead( diff --git a/eth/tracers/internal/tracers/call_tracer.js b/eth/tracers/internal/tracers/call_tracer.js index e4b3124c37..eeb643e986 100644 --- a/eth/tracers/internal/tracers/call_tracer.js +++ b/eth/tracers/internal/tracers/call_tracer.js @@ -1,12 +1,8 @@ // callTracer is a full blown transaction tracer that extracts and reports all // the internal calls made by a transaction, along with any useful information. { - // invocations is a nested data structure containing all the internal contract - // calls executed by a transaction. - invocations: [], - // callstack is the current recursive call stack of the EVM execution. - callstack: [], + callstack: [{}], // descended tracks whether we've just descended from an outer transaction into // an inner call. @@ -19,31 +15,60 @@ if (syscall) { var op = log.op.toString(); } - // If a new method invocation is being done, add to the call stack - if (syscall && op != "RETURN" && op != "REVERT") { - // Skip any pre-compile invocations, those are just fancy opcodes - var to = toAddress(log.stack.peek(1).Bytes()); - if (isPrecompiled(to)) { - return - } - // Otherwise gather some internal call details - var off = (op == 'DELEGATECALL' ? 0 : 1); - - var inOff = log.stack.peek(2 + off).Int64(); - var inEnd = inOff + log.stack.peek(3 + off).Int64(); + // If a new contract is being created, add to the call stack + if (syscall && op == 'CREATE') { + var inOff = log.stack.peek(1).valueOf(); + var inEnd = inOff + log.stack.peek(2).valueOf(); // Assemble the internal call report and store for completion var call = { type: op, - from: log.account, - to: to, + from: toHex(log.contract.getAddress()), input: toHex(log.memory.slice(inOff, inEnd)), - gasDiff: log.gas - log.cost, - outOff: log.stack.peek(4 + off).Int64(), - outLen: log.stack.peek(5 + off).Int64() + gasIn: log.getGas(), + gasCost: log.getCost(), + value: '0x' + log.stack.peek(0).toString(16) }; - if (op != 'DELEGATECALL') { - call.value = '0x' + log.stack.peek(2).Text(16); + this.callstack.push(call); + this.descended = true + return; + } + // If a contract is being self destructed, gather that as a subcall too + if (syscall && op == 'SELFDESTRUCT') { + var left = this.callstack.length; + if (this.callstack[left-1].calls === undefined) { + this.callstack[left-1].calls = []; + } + this.callstack[left-1].calls.push({ + type: op, + }); + return + } + // If a new method invocation is being done, add to the call stack + if (syscall && (op == 'CALL' || op == 'CALLCODE' || op == 'DELEGATECALL' || op == 'STATICCALL')) { + // Skip any pre-compile invocations, those are just fancy opcodes + var to = toAddress(log.stack.peek(1).toString(16)); + if (isPrecompiled(to)) { + return + } + var off = (op == 'DELEGATECALL' || op == 'STATICCALL' ? 0 : 1); + + var inOff = log.stack.peek(2 + off).valueOf(); + var inEnd = inOff + log.stack.peek(3 + off).valueOf(); + + // Assemble the internal call report and store for completion + var call = { + type: op, + from: toHex(log.contract.getAddress()), + to: toHex(to), + input: toHex(log.memory.slice(inOff, inEnd)), + gasIn: log.getGas(), + gasCost: log.getCost(), + outOff: log.stack.peek(4 + off).valueOf(), + outLen: log.stack.peek(5 + off).valueOf() + }; + if (op != 'DELEGATECALL' && op != 'STATICCALL') { + call.value = '0x' + log.stack.peek(2).toString(16); } this.callstack.push(call); this.descended = true @@ -53,38 +78,55 @@ // need to extract if from within the call as there may be funky gas dynamics // with regard to requested and actually given gas (2300 stipend, 63/64 rule). if (this.descended) { - if (log.depth > this.callstack.length) { - this.callstack[this.callstack.length - 1].gas = log.gas; + if (log.getDepth() >= this.callstack.length) { + this.callstack[this.callstack.length - 1].gas = log.getGas(); } else { - this.callstack[this.callstack.length - 1].gas = 2300; // TODO (karalabe): Erm... + // TODO(karalabe): The call was made to a plain account. We currently don't + // have access to the true gas amount inside the call and so any amount will + // mostly be wrong since it depends on a lot of input args. Skip gas for now. } this.descended = false; } // If an existing call is returning, pop off the call stack if (syscall && op == 'REVERT') { - call.revert = true; + this.callstack[this.callstack.length - 1].revert = true; return; } - if (log.depth == this.callstack.length) { + if (log.getDepth() == this.callstack.length - 1) { // Pop off the last call and get the execution results var call = this.callstack.pop(); - call.gasUsed = '0x' + big.NewInt(call.gas - log.gas + call.gasDiff).Text(16); - call.gas = '0x' + big.NewInt(call.gas).Text(16); - delete call.gasDiff; + if (call.type == 'CREATE') { + // If the call was a CREATE, retrieve the contract address and output code + call.gasUsed = '0x' + bigInt(call.gasIn - call.gasCost - log.getGas()).toString(16); + delete call.gasIn; delete call.gasCost; - call.output = toHex(log.memory.slice(call.outOff, call.outOff + call.outLen)); - delete call.outOff; - delete call.outLen; - - // Append to the invocation list if topmost - var left = this.callstack.length; - - if (left == 0) { - this.invocations.push(call); - return + var ret = log.stack.peek(0); + if (!ret.equals(0)) { + call.to = toHex(toAddress(ret.toString(16))); + call.output = toHex(db.getCode(toAddress(ret.toString(16)))); + } else { + call.revert = true; + } + } else { + // If the call was a CALL*, retrieve the output code + if (call.gas !== undefined) { + call.gasUsed = '0x' + bigInt(call.gasIn - call.gasCost + call.gas - log.getGas()).toString(16); + delete call.gasIn; delete call.gasCost; + } + var ret = log.stack.peek(0); + if (!ret.equals(0)) { + call.output = toHex(log.memory.slice(call.outOff, call.outOff + call.outLen)); + } else { + call.revert = true; + } + delete call.outOff; delete call.outLen; } - // Apparently it was a nested call, inject into the previous one + if (call.gas !== undefined) { + call.gas = '0x' + bigInt(call.gas).toString(16); + } + // Inject the call into the previous one + var left = this.callstack.length; if (this.callstack[left-1].calls === undefined) { this.callstack[left-1].calls = []; } @@ -97,20 +139,24 @@ result: function(ctx) { var result = { type: ctx.type, - from: toAddress(ctx.from), - to: toAddress(ctx.to), - value: '0x' + ctx.value.Text(16), - gas: '0x' + big.NewInt(ctx.gas).Text(16), - gasUsed: '0x' + big.NewInt(ctx.gasUsed).Text(16), + from: toHex(ctx.from), + to: toHex(ctx.to), + value: '0x' + ctx.value.toString(16), + gas: '0x' + bigInt(ctx.gas).toString(16), + gasUsed: '0x' + bigInt(ctx.gasUsed).toString(16), input: toHex(ctx.input), output: toHex(ctx.output), time: ctx.time, }; - if (this.invocations.length > 0) { - result.calls = this.invocations; + if (this.callstack[0].calls !== undefined) { + result.calls = this.callstack[0].calls; } if (ctx.error !== undefined) { result.error = ctx.error; + delete result.output; + } + if (this.callstack[0].revert) { + result.revert = true; } return result; } diff --git a/internal/ethapi/tracer.go b/internal/ethapi/tracer.go index f2f5d09448..6bd8151670 100644 --- a/internal/ethapi/tracer.go +++ b/internal/ethapi/tracer.go @@ -21,180 +21,253 @@ import ( "errors" "fmt" "math/big" + "reflect" + "sync/atomic" "time" + "unsafe" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/vm" - "github.com/robertkrimen/otto" + "github.com/ethereum/go-ethereum/log" + duktape "gopkg.in/olebedev/go-duktape.v3" ) -// fakeBig is used to provide an interface to Javascript for 'big.NewInt' -type fakeBig struct{} +// bigIntegerJS is the minified version of https://github.com/peterolson/BigInteger.js. +const bigIntegerJS = `var bigInt=function(undefined){"use strict";var BASE=1e7,LOG_BASE=7,MAX_INT=9007199254740992,MAX_INT_ARR=smallToArray(MAX_INT),LOG_MAX_INT=Math.log(MAX_INT);function Integer(v,radix){if(typeof v==="undefined")return Integer[0];if(typeof radix!=="undefined")return+radix===10?parseValue(v):parseBase(v,radix);return parseValue(v)}function BigInteger(value,sign){this.value=value;this.sign=sign;this.isSmall=false}BigInteger.prototype=Object.create(Integer.prototype);function SmallInteger(value){this.value=value;this.sign=value<0;this.isSmall=true}SmallInteger.prototype=Object.create(Integer.prototype);function isPrecise(n){return-MAX_INT0)return Math.floor(n);return Math.ceil(n)}function add(a,b){var l_a=a.length,l_b=b.length,r=new Array(l_a),carry=0,base=BASE,sum,i;for(i=0;i=base?1:0;r[i]=sum-carry*base}while(i0)r.push(carry);return r}function addAny(a,b){if(a.length>=b.length)return add(a,b);return add(b,a)}function addSmall(a,carry){var l=a.length,r=new Array(l),base=BASE,sum,i;for(i=0;i0){r[i++]=carry%base;carry=Math.floor(carry/base)}return r}BigInteger.prototype.add=function(v){var n=parseValue(v);if(this.sign!==n.sign){return this.subtract(n.negate())}var a=this.value,b=n.value;if(n.isSmall){return new BigInteger(addSmall(a,Math.abs(b)),this.sign)}return new BigInteger(addAny(a,b),this.sign)};BigInteger.prototype.plus=BigInteger.prototype.add;SmallInteger.prototype.add=function(v){var n=parseValue(v);var a=this.value;if(a<0!==n.sign){return this.subtract(n.negate())}var b=n.value;if(n.isSmall){if(isPrecise(a+b))return new SmallInteger(a+b);b=smallToArray(Math.abs(b))}return new BigInteger(addSmall(b,Math.abs(a)),a<0)};SmallInteger.prototype.plus=SmallInteger.prototype.add;function subtract(a,b){var a_l=a.length,b_l=b.length,r=new Array(a_l),borrow=0,base=BASE,i,difference;for(i=0;i=0){value=subtract(a,b)}else{value=subtract(b,a);sign=!sign}value=arrayToSmall(value);if(typeof value==="number"){if(sign)value=-value;return new SmallInteger(value)}return new BigInteger(value,sign)}function subtractSmall(a,b,sign){var l=a.length,r=new Array(l),carry=-b,base=BASE,i,difference;for(i=0;i=0)};SmallInteger.prototype.minus=SmallInteger.prototype.subtract;BigInteger.prototype.negate=function(){return new BigInteger(this.value,!this.sign)};SmallInteger.prototype.negate=function(){var sign=this.sign;var small=new SmallInteger(-this.value);small.sign=!sign;return small};BigInteger.prototype.abs=function(){return new BigInteger(this.value,false)};SmallInteger.prototype.abs=function(){return new SmallInteger(Math.abs(this.value))};function multiplyLong(a,b){var a_l=a.length,b_l=b.length,l=a_l+b_l,r=createArray(l),base=BASE,product,carry,i,a_i,b_j;for(i=0;i0){r[i++]=carry%base;carry=Math.floor(carry/base)}return r}function shiftLeft(x,n){var r=[];while(n-- >0)r.push(0);return r.concat(x)}function multiplyKaratsuba(x,y){var n=Math.max(x.length,y.length);if(n<=30)return multiplyLong(x,y);n=Math.ceil(n/2);var b=x.slice(n),a=x.slice(0,n),d=y.slice(n),c=y.slice(0,n);var ac=multiplyKaratsuba(a,c),bd=multiplyKaratsuba(b,d),abcd=multiplyKaratsuba(addAny(a,b),addAny(c,d));var product=addAny(addAny(ac,shiftLeft(subtract(subtract(abcd,ac),bd),n)),shiftLeft(bd,2*n));trim(product);return product}function useKaratsuba(l1,l2){return-.012*l1-.012*l2+15e-6*l1*l2>0}BigInteger.prototype.multiply=function(v){var n=parseValue(v),a=this.value,b=n.value,sign=this.sign!==n.sign,abs;if(n.isSmall){if(b===0)return Integer[0];if(b===1)return this;if(b===-1)return this.negate();abs=Math.abs(b);if(abs=0;shift--){quotientDigit=base-1;if(remainder[shift+b_l]!==divisorMostSignificantDigit){quotientDigit=Math.floor((remainder[shift+b_l]*base+remainder[shift+b_l-1])/divisorMostSignificantDigit)}carry=0;borrow=0;l=divisor.length;for(i=0;ib_l){highx=(highx+1)*base}guess=Math.ceil(highx/highy);do{check=multiplySmall(b,guess);if(compareAbs(check,part)<=0)break;guess--}while(guess);result.push(guess);part=subtract(part,check)}result.reverse();return[arrayToSmall(result),arrayToSmall(part)]}function divModSmall(value,lambda){var length=value.length,quotient=createArray(length),base=BASE,i,q,remainder,divisor;remainder=0;for(i=length-1;i>=0;--i){divisor=remainder*base+value[i];q=truncate(divisor/lambda);remainder=divisor-q*lambda;quotient[i]=q|0}return[quotient,remainder|0]}function divModAny(self,v){var value,n=parseValue(v);var a=self.value,b=n.value;var quotient;if(b===0)throw new Error("Cannot divide by zero");if(self.isSmall){if(n.isSmall){return[new SmallInteger(truncate(a/b)),new SmallInteger(a%b)]}return[Integer[0],self]}if(n.isSmall){if(b===1)return[self,Integer[0]];if(b==-1)return[self.negate(),Integer[0]];var abs=Math.abs(b);if(absb.length?1:-1}for(var i=a.length-1;i>=0;i--){if(a[i]!==b[i])return a[i]>b[i]?1:-1}return 0}BigInteger.prototype.compareAbs=function(v){var n=parseValue(v),a=this.value,b=n.value;if(n.isSmall)return 1;return compareAbs(a,b)};SmallInteger.prototype.compareAbs=function(v){var n=parseValue(v),a=Math.abs(this.value),b=n.value;if(n.isSmall){b=Math.abs(b);return a===b?0:a>b?1:-1}return-1};BigInteger.prototype.compare=function(v){if(v===Infinity){return-1}if(v===-Infinity){return 1}var n=parseValue(v),a=this.value,b=n.value;if(this.sign!==n.sign){return n.sign?1:-1}if(n.isSmall){return this.sign?-1:1}return compareAbs(a,b)*(this.sign?-1:1)};BigInteger.prototype.compareTo=BigInteger.prototype.compare;SmallInteger.prototype.compare=function(v){if(v===Infinity){return-1}if(v===-Infinity){return 1}var n=parseValue(v),a=this.value,b=n.value;if(n.isSmall){return a==b?0:a>b?1:-1}if(a<0!==n.sign){return a<0?-1:1}return a<0?1:-1};SmallInteger.prototype.compareTo=SmallInteger.prototype.compare;BigInteger.prototype.equals=function(v){return this.compare(v)===0};SmallInteger.prototype.eq=SmallInteger.prototype.equals=BigInteger.prototype.eq=BigInteger.prototype.equals;BigInteger.prototype.notEquals=function(v){return this.compare(v)!==0};SmallInteger.prototype.neq=SmallInteger.prototype.notEquals=BigInteger.prototype.neq=BigInteger.prototype.notEquals;BigInteger.prototype.greater=function(v){return this.compare(v)>0};SmallInteger.prototype.gt=SmallInteger.prototype.greater=BigInteger.prototype.gt=BigInteger.prototype.greater;BigInteger.prototype.lesser=function(v){return this.compare(v)<0};SmallInteger.prototype.lt=SmallInteger.prototype.lesser=BigInteger.prototype.lt=BigInteger.prototype.lesser;BigInteger.prototype.greaterOrEquals=function(v){return this.compare(v)>=0};SmallInteger.prototype.geq=SmallInteger.prototype.greaterOrEquals=BigInteger.prototype.geq=BigInteger.prototype.greaterOrEquals;BigInteger.prototype.lesserOrEquals=function(v){return this.compare(v)<=0};SmallInteger.prototype.leq=SmallInteger.prototype.lesserOrEquals=BigInteger.prototype.leq=BigInteger.prototype.lesserOrEquals;BigInteger.prototype.isEven=function(){return(this.value[0]&1)===0};SmallInteger.prototype.isEven=function(){return(this.value&1)===0};BigInteger.prototype.isOdd=function(){return(this.value[0]&1)===1};SmallInteger.prototype.isOdd=function(){return(this.value&1)===1};BigInteger.prototype.isPositive=function(){return!this.sign};SmallInteger.prototype.isPositive=function(){return this.value>0};BigInteger.prototype.isNegative=function(){return this.sign};SmallInteger.prototype.isNegative=function(){return this.value<0};BigInteger.prototype.isUnit=function(){return false};SmallInteger.prototype.isUnit=function(){return Math.abs(this.value)===1};BigInteger.prototype.isZero=function(){return false};SmallInteger.prototype.isZero=function(){return this.value===0};BigInteger.prototype.isDivisibleBy=function(v){var n=parseValue(v);var value=n.value;if(value===0)return false;if(value===1)return true;if(value===2)return this.isEven();return this.mod(n).equals(Integer[0])};SmallInteger.prototype.isDivisibleBy=BigInteger.prototype.isDivisibleBy;function isBasicPrime(v){var n=v.abs();if(n.isUnit())return false;if(n.equals(2)||n.equals(3)||n.equals(5))return true;if(n.isEven()||n.isDivisibleBy(3)||n.isDivisibleBy(5))return false;if(n.lesser(25))return true}BigInteger.prototype.isPrime=function(){var isPrime=isBasicPrime(this);if(isPrime!==undefined)return isPrime;var n=this.abs(),nPrev=n.prev();var a=[2,3,5,7,11,13,17,19],b=nPrev,d,t,i,x;while(b.isEven())b=b.divide(2);for(i=0;i-MAX_INT)return new SmallInteger(value-1);return new BigInteger(MAX_INT_ARR,true)};var powersOfTwo=[1];while(2*powersOfTwo[powersOfTwo.length-1]<=BASE)powersOfTwo.push(2*powersOfTwo[powersOfTwo.length-1]);var powers2Length=powersOfTwo.length,highestPower2=powersOfTwo[powers2Length-1];function shift_isSmall(n){return(typeof n==="number"||typeof n==="string")&&+Math.abs(n)<=BASE||n instanceof BigInteger&&n.value.length<=1}BigInteger.prototype.shiftLeft=function(n){if(!shift_isSmall(n)){throw new Error(String(n)+" is too large for shifting.")}n=+n;if(n<0)return this.shiftRight(-n);var result=this;while(n>=powers2Length){result=result.multiply(highestPower2);n-=powers2Length-1}return result.multiply(powersOfTwo[n])};SmallInteger.prototype.shiftLeft=BigInteger.prototype.shiftLeft;BigInteger.prototype.shiftRight=function(n){var remQuo;if(!shift_isSmall(n)){throw new Error(String(n)+" is too large for shifting.")}n=+n;if(n<0)return this.shiftLeft(-n);var result=this;while(n>=powers2Length){if(result.isZero())return result;remQuo=divModAny(result,highestPower2);result=remQuo[1].isNegative()?remQuo[0].prev():remQuo[0];n-=powers2Length-1}remQuo=divModAny(result,powersOfTwo[n]);return remQuo[1].isNegative()?remQuo[0].prev():remQuo[0]};SmallInteger.prototype.shiftRight=BigInteger.prototype.shiftRight;function bitwise(x,y,fn){y=parseValue(y);var xSign=x.isNegative(),ySign=y.isNegative();var xRem=xSign?x.not():x,yRem=ySign?y.not():y;var xDigit=0,yDigit=0;var xDivMod=null,yDivMod=null;var result=[];while(!xRem.isZero()||!yRem.isZero()){xDivMod=divModAny(xRem,highestPower2);xDigit=xDivMod[1].toJSNumber();if(xSign){xDigit=highestPower2-1-xDigit}yDivMod=divModAny(yRem,highestPower2);yDigit=yDivMod[1].toJSNumber();if(ySign){yDigit=highestPower2-1-yDigit}xRem=xDivMod[0];yRem=yDivMod[0];result.push(fn(xDigit,yDigit))}var sum=fn(xSign?1:0,ySign?1:0)!==0?bigInt(-1):bigInt(0);for(var i=result.length-1;i>=0;i-=1){sum=sum.multiply(highestPower2).add(bigInt(result[i]))}return sum}BigInteger.prototype.not=function(){return this.negate().prev()};SmallInteger.prototype.not=BigInteger.prototype.not;BigInteger.prototype.and=function(n){return bitwise(this,n,function(a,b){return a&b})};SmallInteger.prototype.and=BigInteger.prototype.and;BigInteger.prototype.or=function(n){return bitwise(this,n,function(a,b){return a|b})};SmallInteger.prototype.or=BigInteger.prototype.or;BigInteger.prototype.xor=function(n){return bitwise(this,n,function(a,b){return a^b})};SmallInteger.prototype.xor=BigInteger.prototype.xor;var LOBMASK_I=1<<30,LOBMASK_BI=(BASE&-BASE)*(BASE&-BASE)|LOBMASK_I;function roughLOB(n){var v=n.value,x=typeof v==="number"?v|LOBMASK_I:v[0]+v[1]*BASE|LOBMASK_BI;return x&-x}function max(a,b){a=parseValue(a);b=parseValue(b);return a.greater(b)?a:b}function min(a,b){a=parseValue(a);b=parseValue(b);return a.lesser(b)?a:b}function gcd(a,b){a=parseValue(a).abs();b=parseValue(b).abs();if(a.equals(b))return a;if(a.isZero())return b;if(b.isZero())return a;var c=Integer[1],d,t;while(a.isEven()&&b.isEven()){d=Math.min(roughLOB(a),roughLOB(b));a=a.divide(d);b=b.divide(d);c=c.multiply(d)}while(a.isEven()){a=a.divide(roughLOB(a))}do{while(b.isEven()){b=b.divide(roughLOB(b))}if(a.greater(b)){t=b;b=a;a=t}b=b.subtract(a)}while(!b.isZero());return c.isUnit()?a:a.multiply(c)}function lcm(a,b){a=parseValue(a).abs();b=parseValue(b).abs();return a.divide(gcd(a,b)).multiply(b)}function randBetween(a,b){a=parseValue(a);b=parseValue(b);var low=min(a,b),high=max(a,b);var range=high.subtract(low).add(1);if(range.isSmall)return low.add(Math.floor(Math.random()*range));var length=range.value.length-1;var result=[],restricted=true;for(var i=length;i>=0;i--){var top=restricted?range.value[i]:BASE;var digit=truncate(Math.random()*top);result.unshift(digit);if(digit=absBase){if(c==="1"&&absBase===1)continue;throw new Error(c+" is not a valid digit in base "+base+".")}else if(c.charCodeAt(0)-87>=absBase){throw new Error(c+" is not a valid digit in base "+base+".")}}}if(2<=base&&base<=36){if(length<=LOG_MAX_INT/Math.log(base)){var result=parseInt(text,base);if(isNaN(result)){throw new Error(c+" is not a valid digit in base "+base+".")}return new SmallInteger(parseInt(text,base))}}base=parseValue(base);var digits=[];var isNegative=text[0]==="-";for(i=isNegative?1:0;i");digits.push(parseValue(text.slice(start+1,i)))}else throw new Error(c+" is not a valid character")}return parseBaseFromArray(digits,base,isNegative)};function parseBaseFromArray(digits,base,isNegative){var val=Integer[0],pow=Integer[1],i;for(i=digits.length-1;i>=0;i--){val=val.add(digits[i].times(pow));pow=pow.times(base)}return isNegative?val.negate():val}function stringify(digit){var v=digit.value;if(typeof v==="number")v=[v];if(v.length===1&&v[0]<=35){return"0123456789abcdefghijklmnopqrstuvwxyz".charAt(v[0])}return"<"+v+">"}function toBase(n,base){base=bigInt(base);if(base.isZero()){if(n.isZero())return"0";throw new Error("Cannot convert nonzero numbers to base 0.")}if(base.equals(-1)){if(n.isZero())return"0";if(n.isNegative())return new Array(1-n).join("10");return"1"+new Array(+n).join("01")}var minusSign="";if(n.isNegative()&&base.isPositive()){minusSign="-";n=n.abs()}if(base.equals(1)){if(n.isZero())return"0";return minusSign+new Array(+n+1).join(1)}var out=[];var left=n,divmod;while(left.isNegative()||left.compareAbs(base)>=0){divmod=left.divmod(base);left=divmod.quotient;var digit=divmod.remainder;if(digit.isNegative()){digit=base.minus(digit).abs();left=left.next()}out.push(stringify(digit))}out.push(stringify(left));return minusSign+out.reverse().join("")}BigInteger.prototype.toString=function(radix){if(radix===undefined)radix=10;if(radix!==10)return toBase(this,radix);var v=this.value,l=v.length,str=String(v[--l]),zeros="0000000",digit;while(--l>=0){digit=String(v[l]);str+=zeros.slice(digit.length)+digit}var sign=this.sign?"-":"";return sign+str};SmallInteger.prototype.toString=function(radix){if(radix===undefined)radix=10;if(radix!=10)return toBase(this,radix);return String(this.value)};BigInteger.prototype.toJSON=SmallInteger.prototype.toJSON=function(){return this.toString()};BigInteger.prototype.valueOf=function(){return+this.toString()};BigInteger.prototype.toJSNumber=BigInteger.prototype.valueOf;SmallInteger.prototype.valueOf=function(){return this.value};SmallInteger.prototype.toJSNumber=SmallInteger.prototype.valueOf;function parseStringValue(v){if(isPrecise(+v)){var x=+v;if(x===truncate(x))return new SmallInteger(x);throw"Invalid integer: "+v}var sign=v[0]==="-";if(sign)v=v.slice(1);var split=v.split(/e/i);if(split.length>2)throw new Error("Invalid integer: "+split.join("e"));if(split.length===2){var exp=split[1];if(exp[0]==="+")exp=exp.slice(1);exp=+exp;if(exp!==truncate(exp)||!isPrecise(exp))throw new Error("Invalid integer: "+exp+" is not a valid exponent.");var text=split[0];var decimalPlace=text.indexOf(".");if(decimalPlace>=0){exp-=text.length-decimalPlace-1;text=text.slice(0,decimalPlace)+text.slice(decimalPlace+1)}if(exp<0)throw new Error("Cannot include negative exponent part for integers");text+=new Array(exp+1).join("0");v=text}var isValid=/^([0-9][0-9]*)$/.test(v);if(!isValid)throw new Error("Invalid integer: "+v);var r=[],max=v.length,l=LOG_BASE,min=max-l;while(max>0){r.push(+v.slice(min,max));min-=l;if(min<0)min=0;max-=l}trim(r);return new BigInteger(r,sign)}function parseNumberValue(v){if(isPrecise(v)){if(v!==truncate(v))throw new Error(v+" is not an integer.");return new SmallInteger(v)}return parseStringValue(v.toString())}function parseValue(v){if(typeof v==="number"){return parseNumberValue(v)}if(typeof v==="string"){return parseStringValue(v)}return v}for(var i=0;i<1e3;i++){Integer[i]=new SmallInteger(i);if(i>0)Integer[-i]=new SmallInteger(-i)}Integer.one=Integer[1];Integer.zero=Integer[0];Integer.minusOne=Integer[-1];Integer.max=max;Integer.min=min;Integer.gcd=gcd;Integer.lcm=lcm;Integer.isInstance=function(x){return x instanceof BigInteger||x instanceof SmallInteger};Integer.randBetween=randBetween;Integer.fromArray=function(digits,base,isNegative){return parseBaseFromArray(digits.map(parseValue),parseValue(base||10),isNegative)};return Integer}();if(typeof module!=="undefined"&&module.hasOwnProperty("exports")){module.exports=bigInt}if(typeof define==="function"&&define.amd){define("big-integer",[],function(){return bigInt})}; bigInt` -// NewInt creates a new big.Int with the specified int64 value. -func (fb *fakeBig) NewInt(x int64) *big.Int { - return big.NewInt(x) +// makeSlice convert an unsafe memory pointer with the given type into a Go byte +// slice. +func makeSlice(ptr unsafe.Pointer, size uint) []byte { + var sl = struct { + addr uintptr + len int + cap int + }{uintptr(ptr), int(size), int(size)} + + return *(*[]byte)(unsafe.Pointer(&sl)) } -// opCodeWrapper provides a JavaScript-friendly wrapper around OpCode, to convince Otto to treat it -// as an object, instead of a number. -type opCodeWrapper struct { +// popSlice pops a buffer off the JavaScript stack and returns it as a slice. +func popSlice(ctx *duktape.Context) []byte { + blob := makeSlice(ctx.GetBuffer(-1)) + ctx.Pop() + return blob +} + +// pushBigInt create a JavaScript BigInteger in the VM. +func pushBigInt(n *big.Int, ctx *duktape.Context) { + ctx.GetGlobalString("bigInt") + ctx.PushString(n.String()) + ctx.Call(1) +} + +// opWrapper provides a JavaScript wrapper around OpCode. +type opWrapper struct { op vm.OpCode } -// toNumber returns the ID of this opcode as an integer. -func (ocw *opCodeWrapper) toNumber() int { - return int(ocw.op) +// pushObject assembles a JSVM object wrapping a swappable opcode and pushes it +// onto the VM stack. +func (ow *opWrapper) pushObject(vm *duktape.Context) { + obj := vm.PushObject() + + vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushInt(int(ow.op)); return 1 }) + vm.PutPropString(obj, "toNumber") + + vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushString(ow.op.String()); return 1 }) + vm.PutPropString(obj, "toString") + + vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushBoolean(ow.op.IsPush()); return 1 }) + vm.PutPropString(obj, "isPush") } -// toString returns the string representation of the opcode. -func (ocw *opCodeWrapper) toString() string { - return ocw.op.String() -} - -// isPush returns true if the op is a Push. -func (ocw *opCodeWrapper) isPush() bool { - return ocw.op.IsPush() -} - -// MarshalJSON serializes the opcode as JSON -func (ocw *opCodeWrapper) MarshalJSON() ([]byte, error) { - return json.Marshal(ocw.op.String()) -} - -// toValue returns an otto.Value for the opCodeWrapper -func (ocw *opCodeWrapper) toValue(vm *otto.Otto) otto.Value { - value, _ := vm.ToValue(ocw) - obj := value.Object() - obj.Set("toNumber", ocw.toNumber) - obj.Set("toString", ocw.toString) - obj.Set("isPush", ocw.isPush) - return value -} - -// memoryWrapper provides a JS wrapper around vm.Memory +// memoryWrapper provides a JavaScript wrapper around vm.Memory. type memoryWrapper struct { memory *vm.Memory } -// slice returns the requested range of memory as a byte slice +// slice returns the requested range of memory as a byte slice. func (mw *memoryWrapper) slice(begin, end int64) []byte { + if mw.memory.Len() < int(end) { + // TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go + // runtime goes belly up https://github.com/golang/go/issues/15639. + log.Warn("Tracer accessed out of bound memory", "available", mw.memory.Len(), "offset", begin, "size", end-begin) + return nil + } return mw.memory.Get(begin, end-begin) } -// getUint returns the 32 bytes at the specified address interpreted -// as an unsigned integer +// getUint returns the 32 bytes at the specified address interpreted as a uint. func (mw *memoryWrapper) getUint(addr int64) *big.Int { - ret := big.NewInt(0) - ret.SetBytes(mw.memory.GetPtr(addr, 32)) - return ret + if mw.memory.Len() < int(addr)+32 { + // TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go + // runtime goes belly up https://github.com/golang/go/issues/15639. + log.Warn("Tracer accessed out of bound memory", "available", mw.memory.Len(), "offset", addr, "size", 32) + return new(big.Int) + } + return new(big.Int).SetBytes(mw.memory.GetPtr(addr, 32)) } -// toValue returns an otto.Value for the memoryWrapper -func (mw *memoryWrapper) toValue(vm *otto.Otto) otto.Value { - value, _ := vm.ToValue(mw) - obj := value.Object() - obj.Set("slice", mw.slice) - obj.Set("getUint", mw.getUint) - return value +// pushObject assembles a JSVM object wrapping a swappable memory and pushes it +// onto the VM stack. +func (mw *memoryWrapper) pushObject(vm *duktape.Context) { + obj := vm.PushObject() + + // Generate the `slice` method which takes two ints and returns a buffer + vm.PushGoFunction(func(ctx *duktape.Context) int { + blob := mw.slice(int64(ctx.GetInt(-2)), int64(ctx.GetInt(-1))) + ctx.Pop2() + + ptr := ctx.PushFixedBuffer(len(blob)) + copy(makeSlice(ptr, uint(len(blob))), blob[:]) + return 1 + }) + vm.PutPropString(obj, "slice") + + // Generate the `getUint` method which takes an int and returns a bigint + vm.PushGoFunction(func(ctx *duktape.Context) int { + offset := int64(ctx.GetInt(-1)) + ctx.Pop() + + pushBigInt(mw.getUint(offset), ctx) + return 1 + }) + vm.PutPropString(obj, "getUint") } -// stackWrapper provides a JS wrapper around vm.Stack +// stackWrapper provides a JavaScript wrapper around vm.Stack. type stackWrapper struct { stack *vm.Stack } // peek returns the nth-from-the-top element of the stack. func (sw *stackWrapper) peek(idx int) *big.Int { + if len(sw.stack.Data()) <= idx { + // TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go + // runtime goes belly up https://github.com/golang/go/issues/15639. + log.Warn("Tracer accessed out of bound stack", "size", len(sw.stack.Data()), "index", idx) + return new(big.Int) + } return sw.stack.Data()[len(sw.stack.Data())-idx-1] } -// length returns the length of the stack -func (sw *stackWrapper) length() int { - return len(sw.stack.Data()) +// pushObject assembles a JSVM object wrapping a swappable stack and pushes it +// onto the VM stack. +func (sw *stackWrapper) pushObject(vm *duktape.Context) { + obj := vm.PushObject() + + vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushInt(len(sw.stack.Data())); return 1 }) + vm.PutPropString(obj, "length") + + // Generate the `peek` method which takes an int and returns a bigint + vm.PushGoFunction(func(ctx *duktape.Context) int { + offset := ctx.GetInt(-1) + ctx.Pop() + + pushBigInt(sw.peek(offset), ctx) + return 1 + }) + vm.PutPropString(obj, "peek") } -// toValue returns an otto.Value for the stackWrapper -func (sw *stackWrapper) toValue(vm *otto.Otto) otto.Value { - value, _ := vm.ToValue(sw) - obj := value.Object() - obj.Set("peek", sw.peek) - obj.Set("length", sw.length) - return value -} - -// dbWrapper provides a JS wrapper around vm.Database +// dbWrapper provides a JavaScript wrapper around vm.Database. type dbWrapper struct { db vm.StateDB } -// getBalance retrieves an account's balance -func (dw *dbWrapper) getBalance(addr []byte) *big.Int { - return dw.db.GetBalance(common.BytesToAddress(addr)) +// pushObject assembles a JSVM object wrapping a swappable database and pushes it +// onto the VM stack. +func (dw *dbWrapper) pushObject(vm *duktape.Context) { + obj := vm.PushObject() + + // Push the wrapper for statedb.GetBalance + vm.PushGoFunction(func(ctx *duktape.Context) int { + pushBigInt(dw.db.GetBalance(common.BytesToAddress(popSlice(ctx))), ctx) + return 1 + }) + vm.PutPropString(obj, "getBalance") + + // Push the wrapper for statedb.GetNonce + vm.PushGoFunction(func(ctx *duktape.Context) int { + ctx.PushInt(int(dw.db.GetNonce(common.BytesToAddress(popSlice(ctx))))) + return 1 + }) + vm.PutPropString(obj, "getNonce") + + // Push the wrapper for statedb.GetCode + vm.PushGoFunction(func(ctx *duktape.Context) int { + code := dw.db.GetCode(common.BytesToAddress(popSlice(ctx))) + + ptr := ctx.PushFixedBuffer(len(code)) + copy(makeSlice(ptr, uint(len(code))), code[:]) + return 1 + }) + vm.PutPropString(obj, "getCode") + + // Push the wrapper for statedb.GetState + vm.PushGoFunction(func(ctx *duktape.Context) int { + hash := popSlice(ctx) + addr := popSlice(ctx) + + state := dw.db.GetState(common.BytesToAddress(addr), common.BytesToHash(hash)) + + ptr := ctx.PushFixedBuffer(len(state)) + copy(makeSlice(ptr, uint(len(state))), state[:]) + return 1 + }) + vm.PutPropString(obj, "getState") + + // Push the wrapper for statedb.Exists + vm.PushGoFunction(func(ctx *duktape.Context) int { + ctx.PushBoolean(dw.db.Exist(common.BytesToAddress(popSlice(ctx)))) + return 1 + }) + vm.PutPropString(obj, "exists") } -// getNonce retrieves an account's nonce -func (dw *dbWrapper) getNonce(addr []byte) uint64 { - return dw.db.GetNonce(common.BytesToAddress(addr)) -} - -// getCode retrieves an account's code -func (dw *dbWrapper) getCode(addr []byte) []byte { - return dw.db.GetCode(common.BytesToAddress(addr)) -} - -// getState retrieves an account's state data for the given hash -func (dw *dbWrapper) getState(addr []byte, hash common.Hash) common.Hash { - return dw.db.GetState(common.BytesToAddress(addr), hash) -} - -// exists returns true iff the account exists -func (dw *dbWrapper) exists(addr []byte) bool { - return dw.db.Exist(common.BytesToAddress(addr)) -} - -// toValue returns an otto.Value for the dbWrapper -func (dw *dbWrapper) toValue(vm *otto.Otto) otto.Value { - value, _ := vm.ToValue(dw) - obj := value.Object() - obj.Set("getBalance", dw.getBalance) - obj.Set("getNonce", dw.getNonce) - obj.Set("getCode", dw.getCode) - obj.Set("getState", dw.getState) - obj.Set("exists", dw.exists) - return value -} - -// contractWrapper provides a JS wrapper around vm.Contract +// contractWrapper provides a JavaScript wrapper around vm.Contract type contractWrapper struct { contract *vm.Contract } -func (c *contractWrapper) caller() common.Address { - return c.contract.Caller() -} +// pushObject assembles a JSVM object wrapping a swappable contract and pushes it +// onto the VM stack. +func (cw *contractWrapper) pushObject(vm *duktape.Context) { + obj := vm.PushObject() -func (c *contractWrapper) address() common.Address { - return c.contract.Address() -} + // Push the wrapper for contract.Caller + vm.PushGoFunction(func(ctx *duktape.Context) int { + ptr := ctx.PushFixedBuffer(20) + copy(makeSlice(ptr, 20), cw.contract.Caller().Bytes()) + return 1 + }) + vm.PutPropString(obj, "getCaller") -func (c *contractWrapper) value() *big.Int { - return c.contract.Value() -} + // Push the wrapper for contract.Address + vm.PushGoFunction(func(ctx *duktape.Context) int { + ptr := ctx.PushFixedBuffer(20) + copy(makeSlice(ptr, 20), cw.contract.Address().Bytes()) + return 1 + }) + vm.PutPropString(obj, "getAddress") -func (c *contractWrapper) calldata() []byte { - return c.contract.Input -} + // Push the wrapper for contract.Value + vm.PushGoFunction(func(ctx *duktape.Context) int { + pushBigInt(cw.contract.Value(), ctx) + return 1 + }) + vm.PutPropString(obj, "getValue") -func (c *contractWrapper) toValue(vm *otto.Otto) otto.Value { - value, _ := vm.ToValue(c) - obj := value.Object() - obj.Set("caller", c.caller) - obj.Set("address", c.address) - obj.Set("value", c.value) - obj.Set("calldata", c.calldata) - return value + // Push the wrapper for contract.Input + vm.PushGoFunction(func(ctx *duktape.Context) int { + blob := cw.contract.Input + + ptr := ctx.PushFixedBuffer(len(blob)) + copy(makeSlice(ptr, uint(len(blob))), blob[:]) + return 1 + }) + vm.PutPropString(obj, "getInput") } // JavascriptTracer provides an implementation of Tracer that evaluates a @@ -202,131 +275,188 @@ func (c *contractWrapper) toValue(vm *otto.Otto) otto.Value { type JavascriptTracer struct { inited bool // Flag whether the context was already inited from the EVM - vm *otto.Otto // Javascript VM instance - traceobj *otto.Object // User-supplied object to call - ctx map[string]interface{} // Map for the execution `ctx` arg to `step` - op *opCodeWrapper // Wrapper around the VM opcode - log map[string]interface{} // (Reusable) map for the `log` arg to `step` - logvalue otto.Value // JS view of `log` - memory *memoryWrapper // Wrapper around the VM memory - stack *stackWrapper // Wrapper around the VM stack - db *dbWrapper // Wrapper around the VM environment - dbvalue otto.Value // JS view of `db` - contract *contractWrapper // Wrapper around the contract object - err error // Error, if one has occurred - result interface{} // Final result to return to the user + vm *duktape.Context // Javascript VM instance + + tracerObject int // Stack index of the tracer JavaScript object + stateObject int // Stack index of the global state to pull arguments from + + opWrapper *opWrapper // Wrapper around the VM opcode + stackWrapper *stackWrapper // Wrapper around the VM stack + memoryWrapper *memoryWrapper // Wrapper around the VM memory + contractWrapper *contractWrapper // Wrapper around the contract object + dbWrapper *dbWrapper // Wrapper around the VM environment + + pcValue *uint // Swappable pc value wrapped by a log accessor + gasValue *uint // Swappable gas value wrapped by a log accessor + costValue *uint // Swappable cost value wrapped by a log accessor + depthValue *uint // Swappable depth value wrapped by a log accessor + errorValue *string // Swappable error value wrapped by a log accessor + + ctx map[string]interface{} // Transaction context gathered throughout execution + err error // Error, if one has occurred + result interface{} // Final result to return to the user + + interrupt uint32 // Atomic flag to signal execution interruption + reason error // Textual reason for the interruption } // NewJavascriptTracer instantiates a new JavascriptTracer instance. // code specifies a Javascript snippet, which must evaluate to an expression // returning an object with 'step' and 'result' functions. func NewJavascriptTracer(code string) (*JavascriptTracer, error) { - jsvm := otto.New() - jsvm.Interrupt = make(chan func(), 1) - + tracer := &JavascriptTracer{ + vm: duktape.New(), + ctx: make(map[string]interface{}), + opWrapper: new(opWrapper), + stackWrapper: new(stackWrapper), + memoryWrapper: new(memoryWrapper), + contractWrapper: new(contractWrapper), + dbWrapper: new(dbWrapper), + pcValue: new(uint), + gasValue: new(uint), + costValue: new(uint), + depthValue: new(uint), + } // Set up builtins for this environment - jsvm.Set("big", &fakeBig{}) - jsvm.Set("toHex", hexutil.Encode) - jsvm.Set("toAddress", common.BytesToAddress) - jsvm.Set("isPrecompiled", func(addr []byte) bool { - _, ok := vm.PrecompiledContractsByzantium[common.BytesToAddress(addr)] - return ok - }) + tracer.vm.PushGlobalGoFunction("toHex", func(ctx *duktape.Context) int { + arg := makeSlice(ctx.GetBuffer(-1)) + ctx.Pop() - jstracer, err := jsvm.Object("(" + code + ")") - if err != nil { + ctx.PushString(hexutil.Encode(arg)) + return 1 + }) + tracer.vm.PushGlobalGoFunction("toAddress", func(ctx *duktape.Context) int { + // Try interpreting the argument as a binary blob + ptr, size := ctx.GetBuffer(-1) + if ptr != nil { + ctx.Pop() + copy(makeSlice(ctx.PushFixedBuffer(20), 20), makeSlice(ptr, size)) + return 1 + } + // Nope, not a binary blob, interpret as a hex string + addr := common.HexToAddress(ctx.GetString(-1)) + ctx.Pop() + copy(makeSlice(ctx.PushFixedBuffer(20), 20), addr[:]) + return 1 + }) + tracer.vm.PushGlobalGoFunction("isPrecompiled", func(ctx *duktape.Context) int { + _, ok := vm.PrecompiledContractsByzantium[common.BytesToAddress(makeSlice(ctx.GetBuffer(0)))] + ctx.PushBoolean(ok) + return 1 + }) + // Push the JavaScript tracer as object #0 onto the JSVM stack and validate it + if err := tracer.vm.PevalString("(" + code + ")"); err != nil { + log.Warn("Failed to compile tracer", "err", err) return nil, err } - // Check the required functions exist - step, err := jstracer.Get("step") - if err != nil { - return nil, err - } - if !step.IsFunction() { + tracer.tracerObject = 0 // yeah, nice, eval can't return the index itself + + if !tracer.vm.GetPropString(tracer.tracerObject, "step") { return nil, fmt.Errorf("Trace object must expose a function step()") } + tracer.vm.Pop() - result, err := jstracer.Get("result") - if err != nil { - return nil, err - } - if !result.IsFunction() { + if !tracer.vm.GetPropString(tracer.tracerObject, "result") { return nil, fmt.Errorf("Trace object must expose a function result()") } - // Create the persistent log object - var ( - op = new(opCodeWrapper) - mem = new(memoryWrapper) - stack = new(stackWrapper) - db = new(dbWrapper) - contract = new(contractWrapper) - ) - log := map[string]interface{}{ - "op": op.toValue(jsvm), - "memory": mem.toValue(jsvm), - "stack": stack.toValue(jsvm), - "contract": contract.toValue(jsvm), - } - logvalue, _ := jsvm.ToValue(log) + tracer.vm.Pop() - return &JavascriptTracer{ - vm: jsvm, - traceobj: jstracer, - ctx: make(map[string]interface{}), - op: op, - log: log, - logvalue: logvalue, - memory: mem, - stack: stack, - db: db, - dbvalue: db.toValue(jsvm), - contract: contract, - err: nil, - }, nil -} + // Tracer is valid, inject the big int library to access large numbers + tracer.vm.EvalString(bigIntegerJS) + tracer.vm.PutGlobalString("bigInt") -// Stop terminates execution of any JavaScript -func (jst *JavascriptTracer) Stop(err error) { - jst.vm.Interrupt <- func() { - panic(err) - } -} + // Push the global environment state as object #1 into the JSVM stack + tracer.stateObject = tracer.vm.PushObject() -// callSafely executes a method on a JS object, catching any panics and -// returning them as error objects. -func (jst *JavascriptTracer) callSafely(method string, argumentList ...interface{}) (ret interface{}, err error) { - defer func() { - if caught := recover(); caught != nil { - switch caught := caught.(type) { - case error: - err = caught - case string: - err = errors.New(caught) - case fmt.Stringer: - err = errors.New(caught.String()) - default: - panic(caught) - } + logObject := tracer.vm.PushObject() + + tracer.opWrapper.pushObject(tracer.vm) + tracer.vm.PutPropString(logObject, "op") + + tracer.stackWrapper.pushObject(tracer.vm) + tracer.vm.PutPropString(logObject, "stack") + + tracer.memoryWrapper.pushObject(tracer.vm) + tracer.vm.PutPropString(logObject, "memory") + + tracer.contractWrapper.pushObject(tracer.vm) + tracer.vm.PutPropString(logObject, "contract") + + tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.pcValue); return 1 }) + tracer.vm.PutPropString(logObject, "getPC") + + tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.gasValue); return 1 }) + tracer.vm.PutPropString(logObject, "getGas") + + tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.costValue); return 1 }) + tracer.vm.PutPropString(logObject, "getCost") + + tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.depthValue); return 1 }) + tracer.vm.PutPropString(logObject, "getDepth") + + tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { + if tracer.errorValue != nil { + ctx.PushString(*tracer.errorValue) + } else { + ctx.PushUndefined() } - }() + return 1 + }) + tracer.vm.PutPropString(logObject, "error") - value, err := jst.traceobj.Call(method, argumentList...) - ret, _ = value.Export() - return ret, err + tracer.vm.PutPropString(tracer.stateObject, "log") + + tracer.dbWrapper.pushObject(tracer.vm) + tracer.vm.PutPropString(tracer.stateObject, "db") + + return tracer, nil +} + +// Stop terminates execution of the tracer at the first opportune moment. +func (jst *JavascriptTracer) Stop(err error) { + jst.reason = err + atomic.StoreUint32(&jst.interrupt, 1) +} + +// call executes a method on a JS object, catching any errors, formatting and +// returning them as error objects. +func (jst *JavascriptTracer) call(method string, args ...string) (ret interface{}, err error) { + // Execute the JavaScript call and return any error + jst.vm.PushString(method) + for _, arg := range args { + jst.vm.GetPropString(jst.stateObject, arg) + } + code := jst.vm.PcallProp(jst.tracerObject, len(args)) + defer jst.vm.Pop() + + if code != 0 { + err := jst.vm.SafeToString(-1) + return nil, errors.New(err) + } + // No error occurred, extract return value and return + if jst.vm.IsUndefined(-1) { + return nil, nil + } + out := []byte(jst.vm.JsonEncode(-1)) + + for _, res := range []interface{}{new(bool), new(int), new(float64), new(string), new([]bool), new([]int), new([]float64), new([]string), new([]interface{}), new(map[string]interface{}), new(interface{})} { + if err := json.Unmarshal(out, res); err == nil { + return reflect.ValueOf(res).Elem().Interface(), nil + } + } + panic("failed to unmarshal result") } func wrapError(context string, err error) error { var message string switch err := err.(type) { - case *otto.Error: - message = err.String() default: message = err.Error() } return fmt.Errorf("%v in server-side tracer function '%v'", message, context) } -// CaptureState implements the Tracer interface to initialize the tracing operation. +// CaptureStart implements the Tracer interface to initialize the tracing operation. func (jst *JavascriptTracer) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error { jst.ctx["type"] = "CALL" if create { @@ -343,28 +473,34 @@ func (jst *JavascriptTracer) CaptureStart(from common.Address, to common.Address // CaptureState implements the Tracer interface to trace a single step of VM execution. func (jst *JavascriptTracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error { - if !jst.inited { - jst.ctx["block"] = env.BlockNumber.Uint64() - jst.inited = true - } if jst.err == nil { - jst.op.op = op - jst.memory.memory = memory - jst.stack.stack = stack - jst.db.db = env.StateDB - jst.contract.contract = contract - - jst.log["pc"] = pc - jst.log["gas"] = gas - jst.log["cost"] = cost - jst.log["depth"] = depth - jst.log["account"] = contract.Address() - - delete(jst.log, "error") - if err != nil { - jst.log["error"] = err + // Initialize the context if it wasn't done yet + if !jst.inited { + jst.ctx["block"] = env.BlockNumber.Uint64() + jst.inited = true } - _, err := jst.callSafely("step", jst.logvalue, jst.dbvalue) + // If tracing was interrupted, set the error and stop + if atomic.LoadUint32(&jst.interrupt) > 0 { + jst.err = jst.reason + return nil + } + jst.opWrapper.op = op + jst.stackWrapper.stack = stack + jst.memoryWrapper.memory = memory + jst.contractWrapper.contract = contract + jst.dbWrapper.db = env.StateDB + + *jst.pcValue = uint(pc) + *jst.gasValue = uint(gas) + *jst.costValue = uint(cost) + *jst.depthValue = uint(depth) + + jst.errorValue = nil + if err != nil { + jst.errorValue = new(string) + *jst.errorValue = err.Error() + } + _, err := jst.call("step", "log", "db") if err != nil { jst.err = wrapError("step", err) } @@ -377,18 +513,52 @@ func (jst *JavascriptTracer) CaptureEnd(output []byte, gasUsed uint64, t time.Du jst.ctx["output"] = output jst.ctx["gasUsed"] = gasUsed jst.ctx["time"] = t.String() + if err != nil { jst.ctx["error"] = err.Error() } - ctxvalue, _ := jst.vm.ToValue(jst.ctx) - jst.result, jst.err = jst.callSafely("result", ctxvalue, jst.dbvalue) - if jst.err != nil { - jst.err = wrapError("result", jst.err) - } return nil } // GetResult calls the Javascript 'result' function and returns its value, or any accumulated error func (jst *JavascriptTracer) GetResult() (result interface{}, err error) { + // Transform the context into a JavaScript object and inject into the state + obj := jst.vm.PushObject() + + for key, val := range jst.ctx { + switch val := val.(type) { + case uint64: + jst.vm.PushUint(uint(val)) + + case string: + jst.vm.PushString(val) + + case []byte: + ptr := jst.vm.PushFixedBuffer(len(val)) + copy(makeSlice(ptr, uint(len(val))), val[:]) + + case common.Address: + ptr := jst.vm.PushFixedBuffer(20) + copy(makeSlice(ptr, 20), val[:]) + + case *big.Int: + pushBigInt(val, jst.vm) + + default: + panic(fmt.Sprintf("unsupported type: %T", val)) + } + jst.vm.PutPropString(obj, key) + } + jst.vm.PutPropString(jst.stateObject, "ctx") + + // Finalize the trace and return the results + jst.result, err = jst.call("result", "ctx", "db") + if err != nil { + jst.err = wrapError("result", err) + } + // Clean up the JavaScript environment + jst.vm.DestroyHeap() + jst.vm.Destroy() + return jst.result, jst.err } diff --git a/internal/ethapi/tracer_test.go b/internal/ethapi/tracer_test.go index 0ef450ce32..864766c729 100644 --- a/internal/ethapi/tracer_test.go +++ b/internal/ethapi/tracer_test.go @@ -43,7 +43,7 @@ func (account) SetCode(common.Hash, []byte) {} func (account) ForEachStorage(cb func(key, value common.Hash) bool) {} func runTrace(tracer *JavascriptTracer) (interface{}, error) { - env := vm.NewEVM(vm.Context{}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer}) + env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer}) contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000) contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0} @@ -52,7 +52,6 @@ func runTrace(tracer *JavascriptTracer) (interface{}, error) { if err != nil { return nil, err } - return tracer.GetResult() } @@ -67,7 +66,7 @@ func TestTracing(t *testing.T) { t.Fatal(err) } - value, ok := ret.(float64) + value, ok := ret.(int) if !ok { t.Errorf("Expected return value to be float64, was %T", ret) } @@ -111,6 +110,8 @@ func TestOpcodes(t *testing.T) { } func TestHalt(t *testing.T) { + t.Skip("duktape doesn't support abortion") + timeout := errors.New("stahp") tracer, err := NewJavascriptTracer("{step: function() { while(1); }, result: function() { return null; }}") if err != nil { @@ -133,7 +134,7 @@ func TestHaltBetweenSteps(t *testing.T) { t.Fatal(err) } - env := vm.NewEVM(vm.Context{}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer}) + env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer}) contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), 0) tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil) @@ -141,7 +142,7 @@ func TestHaltBetweenSteps(t *testing.T) { tracer.Stop(timeout) tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil) - if _, err := tracer.GetResult(); err.Error() != "stahp in server-side tracer function 'step'" { + if _, err := tracer.GetResult(); err.Error() != timeout.Error() { t.Errorf("Expected timeout error, got %v", err) } } diff --git a/vendor/gopkg.in/olebedev/go-duktape.v3/Gopkg.lock b/vendor/gopkg.in/olebedev/go-duktape.v3/Gopkg.lock new file mode 100644 index 0000000000..c1b0e0a3fb --- /dev/null +++ b/vendor/gopkg.in/olebedev/go-duktape.v3/Gopkg.lock @@ -0,0 +1,21 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + branch = "v1" + name = "gopkg.in/check.v1" + packages = ["."] + revision = "20d25e2804050c1cd24a7eea1e7a6447dd0e74ec" + +[[projects]] + branch = "v3" + name = "gopkg.in/olebedev/go-duktape.v3" + packages = ["."] + revision = "391c1c40178e77a6003d889b96e0e41129aeb894" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "043f802c0b40e2622bf784443d3e3959f0d01e9a795e3bfe30a72060dec10c63" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/vendor/gopkg.in/olebedev/go-duktape.v3/Gopkg.toml b/vendor/gopkg.in/olebedev/go-duktape.v3/Gopkg.toml new file mode 100644 index 0000000000..ea8dc6de3d --- /dev/null +++ b/vendor/gopkg.in/olebedev/go-duktape.v3/Gopkg.toml @@ -0,0 +1,3 @@ +[[constraint]] + branch = "v1" + name = "gopkg.in/check.v1" \ No newline at end of file diff --git a/vendor/gopkg.in/olebedev/go-duktape.v3/LICENSE.md b/vendor/gopkg.in/olebedev/go-duktape.v3/LICENSE.md new file mode 100644 index 0000000000..785c85309e --- /dev/null +++ b/vendor/gopkg.in/olebedev/go-duktape.v3/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Oleg Lebedev + +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/vendor/gopkg.in/olebedev/go-duktape.v3/README.md b/vendor/gopkg.in/olebedev/go-duktape.v3/README.md new file mode 100644 index 0000000000..7385617e45 --- /dev/null +++ b/vendor/gopkg.in/olebedev/go-duktape.v3/README.md @@ -0,0 +1,118 @@ +# Duktape bindings for Go(Golang) [![wercker status](https://app.wercker.com/status/3a5bb2e639a4b4efaf4c8bf7cab7442d/s "wercker status")](https://app.wercker.com/project/bykey/3a5bb2e639a4b4efaf4c8bf7cab7442d) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/olebedev/go-duktape?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) +[Duktape](http://duktape.org/index.html) is a thin, embeddable javascript engine. +Most of the [api](http://duktape.org/api.html) is implemented. +The exceptions are listed [here](https://github.com/olebedev/go-duktape/blob/master/api.go#L1566). + +### Usage + +The package is fully go-getable, no need to install any external C libraries. +So, just type `go get gopkg.in/olebedev/go-duktape.v3` to install. + + +```go +package main + +import "fmt" +import "gopkg.in/olebedev/go-duktape.v3" + +func main() { + ctx := duktape.New() + ctx.PevalString(`2 + 3`) + result := ctx.GetNumber(-1) + ctx.Pop() + fmt.Println("result is:", result) + // To prevent memory leaks, don't forget to clean up after + // yourself when you're done using a context. + ctx.DestroyHeap() +} +``` + +### Go specific notes + +Bindings between Go and Javascript contexts are not fully functional. +However, binding a Go function to the Javascript context is available: +```go +package main + +import "fmt" +import "gopkg.in/olebedev/go-duktape.v3" + +func main() { + ctx := duktape.New() + ctx.PushGlobalGoFunction("log", func(c *duktape.Context) int { + fmt.Println(c.SafeToString(-1)) + return 0 + }) + ctx.PevalString(`log('Go lang Go!')`) +} +``` +then run it. +```bash +$ go run *.go +Go lang Go! +$ +``` + +### Timers + +There is a method to inject timers to the global scope: +```go +package main + +import "fmt" +import "gopkg.in/olebedev/go-duktape.v3" + +func main() { + ctx := duktape.New() + + // Let's inject `setTimeout`, `setInterval`, `clearTimeout`, + // `clearInterval` into global scope. + ctx.PushTimers() + + ch := make(chan string) + ctx.PushGlobalGoFunction("second", func(_ *Context) int { + ch <- "second step" + return 0 + }) + ctx.PevalString(` + setTimeout(second, 0); + print('first step'); + `) + fmt.Println(<-ch) +} +``` +then run it +```bash +$ go run *.go +first step +second step +$ +``` + +Also you can `FlushTimers()`. + +### Command line tool + +Install `go get gopkg.in/olebedev/go-duktape.v3/...`. +Execute file.js: `$GOPATH/bin/go-duk file.js`. + +### Benchmarks +| prog | time | +| ------------|-------| +|[otto](https://github.com/robertkrimen/otto)|200.13s| +|[anko](https://github.com/mattn/anko)|231.19s| +|[agora](https://github.com/PuerkitoBio/agora/)|149.33s| +|[GopherLua](https://github.com/yuin/gopher-lua/)|8.39s| +|**go-duktape**|**9.80s**| + +More details are [here](https://github.com/olebedev/go-duktape/wiki/Benchmarks). + +### Status + +The package is not fully tested, so be careful. + + +### Contribution + +Pull requests are welcome! Also, if you want to discuss something send a pull request with proposal and changes. +__Convention:__ fork the repository and make changes on your fork in a feature branch. diff --git a/vendor/gopkg.in/olebedev/go-duktape.v3/api.go b/vendor/gopkg.in/olebedev/go-duktape.v3/api.go new file mode 100644 index 0000000000..ec60806812 --- /dev/null +++ b/vendor/gopkg.in/olebedev/go-duktape.v3/api.go @@ -0,0 +1,1616 @@ +package duktape + +/* +#cgo !windows CFLAGS: -std=c99 -O3 -Wall -fomit-frame-pointer -fstrict-aliasing +#cgo windows CFLAGS: -O3 -Wall -fomit-frame-pointer -fstrict-aliasing + +#include "duktape.h" +#include "duk_logging.h" +#include "duk_v1_compat.h" +#include "duk_print_alert.h" +static void _duk_eval_string(duk_context *ctx, const char *str) { + duk_eval_string(ctx, str); +} +static void _duk_compile(duk_context *ctx, duk_uint_t flags) { + duk_compile(ctx, flags); +} +static void _duk_compile_file(duk_context *ctx, duk_uint_t flags, const char *path) { + duk_compile_file(ctx, flags, path); +} +static void _duk_compile_lstring(duk_context *ctx, duk_uint_t flags, const char *src, duk_size_t len) { + duk_compile_lstring(ctx, flags, src, len); +} +static void _duk_compile_lstring_filename(duk_context *ctx, duk_uint_t flags, const char *src, duk_size_t len) { + duk_compile_lstring_filename(ctx, flags, src, len); +} +static void _duk_compile_string(duk_context *ctx, duk_uint_t flags, const char *src) { + duk_compile_string(ctx, flags, src); +} +static void _duk_compile_string_filename(duk_context *ctx, duk_uint_t flags, const char *src) { + duk_compile_string_filename(ctx, flags, src); +} +static void _duk_dump_context_stderr(duk_context *ctx) { + duk_dump_context_stderr(ctx); +} +static void _duk_dump_context_stdout(duk_context *ctx) { + duk_dump_context_stdout(ctx); +} +static void _duk_eval(duk_context *ctx) { + duk_eval(ctx); +} +static void _duk_eval_file(duk_context *ctx, const char *path) { + duk_eval_file(ctx, path); +} +static void _duk_eval_file_noresult(duk_context *ctx, const char *path) { + duk_eval_file_noresult(ctx, path); +} +static void _duk_eval_lstring(duk_context *ctx, const char *src, duk_size_t len) { + duk_eval_lstring(ctx, src, len); +} +static void _duk_eval_lstring_noresult(duk_context *ctx, const char *src, duk_size_t len) { + duk_eval_lstring_noresult(ctx, src, len); +} +static void _duk_eval_noresult(duk_context *ctx) { + duk_eval_noresult(ctx); +} +static void _duk_eval_string_noresult(duk_context *ctx, const char *src) { + duk_eval_string_noresult(ctx, src); +} +static duk_bool_t _duk_is_error(duk_context *ctx, duk_idx_t index) { + return duk_is_error(ctx, index); +} +static duk_bool_t _duk_is_object_coercible(duk_context *ctx, duk_idx_t index) { + return duk_is_object_coercible(ctx, index); +} +static duk_int_t _duk_pcompile(duk_context *ctx, duk_uint_t flags) { + return duk_pcompile(ctx, flags); +} +static duk_int_t _duk_pcompile_file(duk_context *ctx, duk_uint_t flags, const char *path) { + return duk_pcompile_file(ctx, flags, path); +} +static duk_int_t _duk_pcompile_lstring(duk_context *ctx, duk_uint_t flags, const char *src, duk_size_t len) { + return duk_pcompile_lstring(ctx, flags, src, len); +} +static duk_int_t _duk_pcompile_lstring_filename(duk_context *ctx, duk_uint_t flags, const char *src, duk_size_t len) { + return duk_pcompile_lstring_filename(ctx, flags, src, len); +} +static duk_int_t _duk_pcompile_string(duk_context *ctx, duk_uint_t flags, const char *src) { + return duk_pcompile_string(ctx, flags, src); +} +static duk_int_t _duk_pcompile_string_filename(duk_context *ctx, duk_uint_t flags, const char *src) { + return duk_pcompile_string_filename(ctx, flags, src); +} +static duk_int_t _duk_peval(duk_context *ctx) { + return duk_peval(ctx); +} +static duk_int_t _duk_peval_file(duk_context *ctx, const char *path) { + return duk_peval_file(ctx, path); +} +static duk_int_t _duk_peval_file_noresult(duk_context *ctx, const char *path) { + return duk_peval_file_noresult(ctx, path); +} +static duk_int_t _duk_peval_lstring(duk_context *ctx, const char *src, duk_size_t len) { + return duk_peval_lstring(ctx, src, len); +} +static duk_int_t _duk_peval_lstring_noresult(duk_context *ctx, const char *src, duk_size_t len) { + return duk_peval_lstring_noresult(ctx, src, len); +} +static duk_int_t _duk_peval_noresult(duk_context *ctx) { + return duk_peval_noresult(ctx); +} +static duk_int_t _duk_peval_string(duk_context *ctx, const char *src) { + return duk_peval_string(ctx, src); +} +static duk_int_t _duk_peval_string_noresult(duk_context *ctx, const char *src) { + return duk_peval_string_noresult(ctx, src); +} +static const char *_duk_push_string_file(duk_context *ctx, const char *path) { + return duk_push_string_file(ctx, path); +} +static duk_idx_t _duk_push_thread(duk_context *ctx) { + return duk_push_thread(ctx); +} +static duk_idx_t _duk_push_thread_new_globalenv(duk_context *ctx) { + return duk_push_thread_new_globalenv(ctx); +} +static void _duk_require_object_coercible(duk_context *ctx, duk_idx_t index) { + duk_require_object_coercible(ctx, index); +} +static void _duk_require_type_mask(duk_context *ctx, duk_idx_t index, duk_uint_t mask) { + duk_require_type_mask(ctx, index, mask); +} +static const char *_duk_safe_to_string(duk_context *ctx, duk_idx_t index) { + return duk_safe_to_string(ctx, index); +} +static void _duk_xcopy_top(duk_context *to_ctx, duk_context *from_ctx, duk_idx_t count) { + duk_xcopy_top(to_ctx, from_ctx, count); +} +static void _duk_xmove_top(duk_context *to_ctx, duk_context *from_ctx, duk_idx_t count) { + duk_xmove_top(to_ctx, from_ctx, count); +} +static void *_duk_to_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) { + return duk_to_buffer(ctx, index, out_size); +} +static void *_duk_to_dynamic_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) { + return duk_to_dynamic_buffer(ctx, index, out_size); +} +static void *_duk_to_fixed_buffer(duk_context *ctx, duk_idx_t index, duk_size_t *out_size) { + return duk_to_fixed_buffer(ctx, index, out_size); +} +static duk_int_t _duk_is_primitive(duk_context *ctx, duk_idx_t index) { + return duk_is_primitive(ctx, index); +} +static void *_duk_push_buffer(duk_context *ctx, duk_size_t size, duk_bool_t dynamic) { + return duk_push_buffer(ctx, size, dynamic); +} +static void *_duk_push_fixed_buffer(duk_context *ctx, duk_size_t size) { + return duk_push_fixed_buffer(ctx, size); +} +static void *_duk_push_dynamic_buffer(duk_context *ctx, duk_size_t size) { + return duk_push_dynamic_buffer(ctx, size); +} +static void _duk_error(duk_context *ctx, duk_errcode_t err_code, const char *str) { + duk_error(ctx, err_code, "%s", str); +} +static void _duk_push_error_object(duk_context *ctx, duk_errcode_t err_code, const char *str) { + duk_push_error_object(ctx, err_code, "%s", str); +} +static void _duk_error_raw(duk_context *ctx, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *text) { + duk_error_raw(ctx, err_code, filename, line, text); +} +static void _duk_log(duk_context *ctx, duk_int_t level, const char *str) { + duk_log(ctx, level, "%s", str); +} +static void _duk_push_external_buffer(duk_context *ctx) { + duk_push_external_buffer(ctx); +} +*/ +import "C" +import ( + "fmt" + "unsafe" +) + +// See: http://duktape.org/api.html#duk_alloc +func (d *Context) Alloc(size int) unsafe.Pointer { + return C.duk_alloc(d.duk_context, C.duk_size_t(size)) +} + +// See: http://duktape.org/api.html#duk_alloc_raw +func (d *Context) AllocRaw(size int) unsafe.Pointer { + return C.duk_alloc_raw(d.duk_context, C.duk_size_t(size)) +} + +// See: http://duktape.org/api.html#duk_base64_decode +func (d *Context) Base64Decode(index int) { + C.duk_base64_decode(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_base64_encode +func (d *Context) Base64Encode(index int) string { + if s := C.duk_base64_encode(d.duk_context, C.duk_idx_t(index)); s != nil { + return C.GoString(s) + } + return "" +} + +// See: http://duktape.org/api.html#duk_call +func (d *Context) Call(nargs int) { + C.duk_call(d.duk_context, C.duk_idx_t(nargs)) +} + +// See: http://duktape.org/api.html#duk_call_method +func (d *Context) CallMethod(nargs int) { + C.duk_call_method(d.duk_context, C.duk_idx_t(nargs)) +} + +// See: http://duktape.org/api.html#duk_call_prop +func (d *Context) CallProp(objIndex int, nargs int) { + C.duk_call_prop(d.duk_context, C.duk_idx_t(objIndex), C.duk_idx_t(nargs)) +} + +// See: http://duktape.org/api.html#duk_check_stack +func (d *Context) CheckStack(extra int) bool { + return int(C.duk_check_stack(d.duk_context, C.duk_idx_t(extra))) == 1 +} + +// See: http://duktape.org/api.html#duk_check_stack_top +func (d *Context) CheckStackTop(top int) bool { + return int(C.duk_check_stack_top(d.duk_context, C.duk_idx_t(top))) == 1 +} + +// See: http://duktape.org/api.html#duk_check_type +func (d *Context) CheckType(index int, typ int) bool { + return int(C.duk_check_type(d.duk_context, C.duk_idx_t(index), C.duk_int_t(typ))) == 1 +} + +// See: http://duktape.org/api.html#duk_check_type_mask +func (d *Context) CheckTypeMask(index int, mask uint) bool { + return int(C.duk_check_type_mask(d.duk_context, C.duk_idx_t(index), C.duk_uint_t(mask))) == 1 +} + +// See: http://duktape.org/api.html#duk_compact +func (d *Context) Compact(objIndex int) { + C.duk_compact(d.duk_context, C.duk_idx_t(objIndex)) +} + +// See: http://duktape.org/api.html#duk_compile +func (d *Context) Compile(flags uint) { + C._duk_compile(d.duk_context, C.duk_uint_t(flags)) +} + +// See: http://duktape.org/api.html#duk_compile_file +func (d *Context) CompileFile(flags uint, path string) { + __path__ := C.CString(path) + C._duk_compile_file(d.duk_context, C.duk_uint_t(flags), __path__) + C.free(unsafe.Pointer(__path__)) +} + +// See: http://duktape.org/api.html#duk_compile_lstring +func (d *Context) CompileLstring(flags uint, src string, len int) { + __src__ := C.CString(src) + C._duk_compile_lstring(d.duk_context, C.duk_uint_t(flags), __src__, C.duk_size_t(len)) + C.free(unsafe.Pointer(__src__)) +} + +// See: http://duktape.org/api.html#duk_compile_lstring_filename +func (d *Context) CompileLstringFilename(flags uint, src string, len int) { + __src__ := C.CString(src) + C._duk_compile_lstring_filename(d.duk_context, C.duk_uint_t(flags), __src__, C.duk_size_t(len)) + C.free(unsafe.Pointer(__src__)) +} + +// See: http://duktape.org/api.html#duk_compile_string +func (d *Context) CompileString(flags uint, src string) { + __src__ := C.CString(src) + C._duk_compile_string(d.duk_context, C.duk_uint_t(flags), __src__) + C.free(unsafe.Pointer(__src__)) +} + +// See: http://duktape.org/api.html#duk_compile_string_filename +func (d *Context) CompileStringFilename(flags uint, src string) { + __src__ := C.CString(src) + C._duk_compile_string_filename(d.duk_context, C.duk_uint_t(flags), __src__) + C.free(unsafe.Pointer(__src__)) +} + +// See: http://duktape.org/api.html#duk_concat +func (d *Context) Concat(count int) { + C.duk_concat(d.duk_context, C.duk_idx_t(count)) +} + +// See: http://duktape.org/api.html#duk_copy +func (d *Context) Copy(fromIndex int, toIndex int) { + C.duk_copy(d.duk_context, C.duk_idx_t(fromIndex), C.duk_idx_t(toIndex)) +} + +// See: http://duktape.org/api.html#duk_del_prop +func (d *Context) DelProp(objIndex int) bool { + return int(C.duk_del_prop(d.duk_context, C.duk_idx_t(objIndex))) == 1 +} + +// See: http://duktape.org/api.html#duk_del_prop_index +func (d *Context) DelPropIndex(objIndex int, arrIndex uint) bool { + return int(C.duk_del_prop_index(d.duk_context, C.duk_idx_t(objIndex), C.duk_uarridx_t(arrIndex))) == 1 +} + +// See: http://duktape.org/api.html#duk_del_prop_string +func (d *Context) DelPropString(objIndex int, key string) bool { + __key__ := C.CString(key) + result := int(C.duk_del_prop_string(d.duk_context, C.duk_idx_t(objIndex), __key__)) == 1 + C.free(unsafe.Pointer(__key__)) + return result +} + +// See: http://duktape.org/api.html#duk_def_prop +func (d *Context) DefProp(objIndex int, flags uint) { + C.duk_def_prop(d.duk_context, C.duk_idx_t(objIndex), C.duk_uint_t(flags)) +} + +// See: http://duktape.org/api.html#duk_destroy_heap +func (d *Context) DestroyHeap() { + d.Gc(0) + C.duk_destroy_heap(d.duk_context) + d.duk_context = nil +} + +// See: http://duktape.org/api.html#duk_dump_context_stderr +func (d *Context) DumpContextStderr() { + C._duk_dump_context_stderr(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_dump_context_stdout +func (d *Context) DumpContextStdout() { + C._duk_dump_context_stdout(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_dup +func (d *Context) Dup(fromIndex int) { + C.duk_dup(d.duk_context, C.duk_idx_t(fromIndex)) +} + +// See: http://duktape.org/api.html#duk_dup_top +func (d *Context) DupTop() { + C.duk_dup_top(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_enum +func (d *Context) Enum(objIndex int, enumFlags uint) { + C.duk_enum(d.duk_context, C.duk_idx_t(objIndex), C.duk_uint_t(enumFlags)) +} + +// See: http://duktape.org/api.html#duk_equals +func (d *Context) Equals(index1 int, index2 int) bool { + return int(C.duk_equals(d.duk_context, C.duk_idx_t(index1), C.duk_idx_t(index2))) == 1 +} + +// Error pushes a new Error object to the stack and throws it. This will call +// fmt.Sprint, forwarding arguments after the error code, to produce the +// Error's message. +// +// See: http://duktape.org/api.html#duk_error +func (d *Context) Error(errCode int, str string) { + __str__ := C.CString(str) + C._duk_error(d.duk_context, C.duk_errcode_t(errCode), __str__) + C.free(unsafe.Pointer(__str__)) +} + +func (d *Context) ErrorRaw(errCode int, filename string, line int, errMsg string) { + __filename__ := C.CString(filename) + __errMsg__ := C.CString(errMsg) + C._duk_error_raw(d.duk_context, C.duk_errcode_t(errCode), __filename__, C.duk_int_t(line), __errMsg__) + C.free(unsafe.Pointer(__filename__)) + C.free(unsafe.Pointer(__errMsg__)) +} + +// Errorf pushes a new Error object to the stack and throws it. This will call +// fmt.Sprintf, forwarding the format string and additional arguments, to +// produce the Error's message. +// +// See: http://duktape.org/api.html#duk_error +func (d *Context) Errorf(errCode int, format string, a ...interface{}) { + str := fmt.Sprintf(format, a...) + __str__ := C.CString(str) + C._duk_error(d.duk_context, C.duk_errcode_t(errCode), __str__) + C.free(unsafe.Pointer(__str__)) +} + +// See: http://duktape.org/api.html#duk_eval +func (d *Context) Eval() { + C._duk_eval(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_eval_file +func (d *Context) EvalFile(path string) { + __path__ := C.CString(path) + C._duk_eval_file(d.duk_context, __path__) + C.free(unsafe.Pointer(__path__)) +} + +// See: http://duktape.org/api.html#duk_eval_file_noresult +func (d *Context) EvalFileNoresult(path string) { + __path__ := C.CString(path) + C._duk_eval_file_noresult(d.duk_context, __path__) + C.free(unsafe.Pointer(__path__)) +} + +// See: http://duktape.org/api.html#duk_eval_lstring +func (d *Context) EvalLstring(src string, len int) { + __src__ := C.CString(src) + C._duk_eval_lstring(d.duk_context, __src__, C.duk_size_t(len)) + C.free(unsafe.Pointer(__src__)) +} + +// See: http://duktape.org/api.html#duk_eval_lstring_noresult +func (d *Context) EvalLstringNoresult(src string, len int) { + __src__ := C.CString(src) + C._duk_eval_lstring_noresult(d.duk_context, __src__, C.duk_size_t(len)) + C.free(unsafe.Pointer(__src__)) +} + +// See: http://duktape.org/api.html#duk_eval_noresult +func (d *Context) EvalNoresult() { + C._duk_eval_noresult(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_eval_string +func (d *Context) EvalString(src string) { + __src__ := C.CString(src) + C._duk_eval_string(d.duk_context, __src__) + C.free(unsafe.Pointer(__src__)) +} + +// See: http://duktape.org/api.html#duk_eval_string_noresult +func (d *Context) EvalStringNoresult(src string) { + __src__ := C.CString(src) + C._duk_eval_string_noresult(d.duk_context, __src__) + C.free(unsafe.Pointer(__src__)) +} + +// See: http://duktape.org/api.html#duk_fatal +func (d *Context) Fatal(errCode int, errMsg string) { + __errMsg__ := C.CString(errMsg) + defer C.free(unsafe.Pointer(__errMsg__)) + C.duk_fatal_raw(d.duk_context, __errMsg__) +} + +// See: http://duktape.org/api.html#duk_gc +func (d *Context) Gc(flags uint) { + C.duk_gc(d.duk_context, C.duk_uint_t(flags)) +} + +// See: http://duktape.org/api.html#duk_get_boolean +func (d *Context) GetBoolean(index int) bool { + return int(C.duk_get_boolean(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_get_buffer +func (d *Context) GetBuffer(index int) (rawPtr unsafe.Pointer, outSize uint) { + rawPtr = C.duk_get_buffer(d.duk_context, C.duk_idx_t(index), (*C.duk_size_t)(unsafe.Pointer(&outSize))) + return rawPtr, outSize +} + +// See: http://duktape.org/api.html#duk_get_context +func (d *Context) GetContext(index int) *Context { + return contextFromPointer(C.duk_get_context(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_get_current_magic +func (d *Context) GetCurrentMagic() int { + return int(C.duk_get_current_magic(d.duk_context)) +} + +// See: http://duktape.org/api.html#duk_get_error_code +func (d *Context) GetErrorCode(index int) int { + code := int(C.duk_get_error_code(d.duk_context, C.duk_idx_t(index))) + return code +} + +// See: http://duktape.org/api.html#duk_get_finalizer +func (d *Context) GetFinalizer(index int) { + C.duk_get_finalizer(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_get_global_string +func (d *Context) GetGlobalString(key string) bool { + __key__ := C.CString(key) + result := int(C.duk_get_global_string(d.duk_context, __key__)) == 1 + C.free(unsafe.Pointer(__key__)) + return result +} + +// See: http://duktape.org/api.html#duk_get_heapptr +func (d *Context) GetHeapptr(index int) unsafe.Pointer { + return unsafe.Pointer(C.duk_get_heapptr(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_get_int +func (d *Context) GetInt(index int) int { + return int(C.duk_get_int(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_get_length +func (d *Context) GetLength(index int) int { + return int(C.duk_get_length(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_get_lstring +func (d *Context) GetLstring(index int) string { + if s := C.duk_get_lstring(d.duk_context, C.duk_idx_t(index), nil); s != nil { + return C.GoString(s) + } + return "" +} + +// See: http://duktape.org/api.html#duk_get_magic +func (d *Context) GetMagic(index int) int { + return int(C.duk_get_magic(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_get_number +func (d *Context) GetNumber(index int) float64 { + return float64(C.duk_get_number(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_get_pointer +func (d *Context) GetPointer(index int) unsafe.Pointer { + return C.duk_get_pointer(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_get_prop +func (d *Context) GetProp(objIndex int) bool { + return int(C.duk_get_prop(d.duk_context, C.duk_idx_t(objIndex))) == 1 +} + +// See: http://duktape.org/api.html#duk_get_prop_index +func (d *Context) GetPropIndex(objIndex int, arrIndex uint) bool { + return int(C.duk_get_prop_index(d.duk_context, C.duk_idx_t(objIndex), C.duk_uarridx_t(arrIndex))) == 1 +} + +// See: http://duktape.org/api.html#duk_get_prop_string +func (d *Context) GetPropString(objIndex int, key string) bool { + __key__ := C.CString(key) + result := int(C.duk_get_prop_string(d.duk_context, C.duk_idx_t(objIndex), __key__)) == 1 + C.free(unsafe.Pointer(__key__)) + return result +} + +// See: http://duktape.org/api.html#duk_get_prototype +func (d *Context) GetPrototype(index int) { + C.duk_get_prototype(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_get_string +func (d *Context) GetString(i int) string { + if s := C.duk_get_string(d.duk_context, C.duk_idx_t(i)); s != nil { + return C.GoString(s) + } + return "" +} + +// See: http://duktape.org/api.html#duk_get_top +func (d *Context) GetTop() int { + return int(C.duk_get_top(d.duk_context)) +} + +// See: http://duktape.org/api.html#duk_get_top_index +func (d *Context) GetTopIndex() int { + return int(C.duk_get_top_index(d.duk_context)) +} + +// See: http://duktape.org/api.html#duk_get_type +func (d *Context) GetType(index int) Type { + return Type(C.duk_get_type(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_get_type_mask +func (d *Context) GetTypeMask(index int) uint { + return uint(C.duk_get_type_mask(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_get_uint +func (d *Context) GetUint(index int) uint { + return uint(C.duk_get_uint(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_has_prop +func (d *Context) HasProp(objIndex int) bool { + return int(C.duk_has_prop(d.duk_context, C.duk_idx_t(objIndex))) == 1 +} + +// See: http://duktape.org/api.html#duk_has_prop_index +func (d *Context) HasPropIndex(objIndex int, arrIndex uint) bool { + return int(C.duk_has_prop_index(d.duk_context, C.duk_idx_t(objIndex), C.duk_uarridx_t(arrIndex))) == 1 +} + +// See: http://duktape.org/api.html#duk_has_prop_string +func (d *Context) HasPropString(objIndex int, key string) bool { + __key__ := C.CString(key) + result := int(C.duk_has_prop_string(d.duk_context, C.duk_idx_t(objIndex), __key__)) == 1 + C.free(unsafe.Pointer(__key__)) + return result +} + +// See: http://duktape.org/api.html#duk_hex_decode +func (d *Context) HexDecode(index int) { + C.duk_hex_decode(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_hex_encode +func (d *Context) HexEncode(index int) string { + if s := C.duk_hex_encode(d.duk_context, C.duk_idx_t(index)); s != nil { + return C.GoString(s) + } + return "" +} + +// See: http://duktape.org/api.html#duk_insert +func (d *Context) Insert(toIndex int) { + C.duk_insert(d.duk_context, C.duk_idx_t(toIndex)) +} + +// See: http://duktape.org/api.html#duk_is_array +func (d *Context) IsArray(index int) bool { + return int(C.duk_is_array(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_boolean +func (d *Context) IsBoolean(index int) bool { + return int(C.duk_is_boolean(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_bound_function +func (d *Context) IsBoundFunction(index int) bool { + return int(C.duk_is_bound_function(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_buffer +func (d *Context) IsBuffer(index int) bool { + return int(C.duk_is_buffer(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_c_function +func (d *Context) IsCFunction(index int) bool { + return int(C.duk_is_c_function(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_callable +func (d *Context) IsCallable(index int) bool { + return int(C.duk_is_function(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_constructor_call +func (d *Context) IsConstructorCall() bool { + return int(C.duk_is_constructor_call(d.duk_context)) == 1 +} + +// See: http://duktape.org/api.html#duk_is_dynamic_buffer +func (d *Context) IsDynamicBuffer(index int) bool { + return int(C.duk_is_dynamic_buffer(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_ecmascript_function +func (d *Context) IsEcmascriptFunction(index int) bool { + return int(C.duk_is_ecmascript_function(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_fixed_buffer +func (d *Context) IsFixedBuffer(index int) bool { + return int(C.duk_is_fixed_buffer(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_function +func (d *Context) IsFunction(index int) bool { + return int(C.duk_is_function(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_nan +func (d *Context) IsNan(index int) bool { + return int(C.duk_is_nan(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_null +func (d *Context) IsNull(index int) bool { + return int(C.duk_is_null(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_null_or_undefined +func (d *Context) IsNullOrUndefined(index int) bool { + return d.IsNull(index) || d.IsUndefined(index) +} + +// See: http://duktape.org/api.html#duk_is_number +func (d *Context) IsNumber(index int) bool { + return int(C.duk_is_number(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_object +func (d *Context) IsObject(index int) bool { + return int(C.duk_is_object(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_error +func (d *Context) IsError(index int) bool { + return int(C._duk_is_error(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_object_coercible +func (d *Context) IsObjectCoercible(index int) bool { + return int(C._duk_is_object_coercible(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_pointer +func (d *Context) IsPointer(index int) bool { + return int(C.duk_is_pointer(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_primitive +func (d *Context) IsPrimitive(index int) bool { + return int(C._duk_is_primitive(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_strict_call +func (d *Context) IsStrictCall() bool { + return int(C.duk_is_strict_call(d.duk_context)) == 1 +} + +// See: http://duktape.org/api.html#duk_is_string +func (d *Context) IsString(index int) bool { + return int(C.duk_is_string(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_thread +func (d *Context) IsThread(index int) bool { + return int(C.duk_is_thread(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_undefined +func (d *Context) IsUndefined(index int) bool { + return int(C.duk_is_undefined(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_valid_index +func (d *Context) IsValidIndex(index int) bool { + return int(C.duk_is_valid_index(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_join +func (d *Context) Join(count int) { + C.duk_join(d.duk_context, C.duk_idx_t(count)) +} + +// See: http://duktape.org/api.html#duk_json_decode +func (d *Context) JsonDecode(index int) { + C.duk_json_decode(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_json_encode +func (d *Context) JsonEncode(index int) string { + if s := C.duk_json_encode(d.duk_context, C.duk_idx_t(index)); s != nil { + return C.GoString(s) + } + return "" +} + +// See: http://duktape.org/api.html#duk_new +func (d *Context) New(nargs int) { + C.duk_new(d.duk_context, C.duk_idx_t(nargs)) +} + +// See: http://duktape.org/api.html#duk_next +func (d *Context) Next(enumIndex int, getValue bool) bool { + var __getValue__ int + if getValue { + __getValue__ = 1 + } + return int(C.duk_next(d.duk_context, C.duk_idx_t(enumIndex), C.duk_bool_t(__getValue__))) == 1 +} + +// See: http://duktape.org/api.html#duk_normalize_index +func (d *Context) NormalizeIndex(index int) int { + return int(C.duk_normalize_index(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_pcall +func (d *Context) Pcall(nargs int) int { + return int(C.duk_pcall(d.duk_context, C.duk_idx_t(nargs))) +} + +// See: http://duktape.org/api.html#duk_pcall_method +func (d *Context) PcallMethod(nargs int) int { + return int(C.duk_pcall_method(d.duk_context, C.duk_idx_t(nargs))) +} + +// See: http://duktape.org/api.html#duk_pcall_prop +func (d *Context) PcallProp(objIndex int, nargs int) int { + return int(C.duk_pcall_prop(d.duk_context, C.duk_idx_t(objIndex), C.duk_idx_t(nargs))) +} + +// See: http://duktape.org/api.html#duk_pcompile +func (d *Context) Pcompile(flags uint) error { + result := int(C._duk_pcompile(d.duk_context, C.duk_uint_t(flags))) + return d.castStringToError(result) +} + +// See: http://duktape.org/api.html#duk_pcompile_file +func (d *Context) PcompileFile(flags uint, path string) error { + __path__ := C.CString(path) + result := int(C._duk_pcompile_file(d.duk_context, C.duk_uint_t(flags), __path__)) + C.free(unsafe.Pointer(__path__)) + return d.castStringToError(result) +} + +// See: http://duktape.org/api.html#duk_pcompile_lstring +func (d *Context) PcompileLstring(flags uint, src string, len int) error { + __src__ := C.CString(src) + result := int(C._duk_pcompile_lstring(d.duk_context, C.duk_uint_t(flags), __src__, C.duk_size_t(len))) + C.free(unsafe.Pointer(__src__)) + return d.castStringToError(result) +} + +// See: http://duktape.org/api.html#duk_pcompile_lstring_filename +func (d *Context) PcompileLstringFilename(flags uint, src string, len int) error { + __src__ := C.CString(src) + result := int(C._duk_pcompile_lstring_filename(d.duk_context, C.duk_uint_t(flags), __src__, C.duk_size_t(len))) + C.free(unsafe.Pointer(__src__)) + return d.castStringToError(result) +} + +// See: http://duktape.org/api.html#duk_pcompile_string +func (d *Context) PcompileString(flags uint, src string) error { + __src__ := C.CString(src) + result := int(C._duk_pcompile_string(d.duk_context, C.duk_uint_t(flags), __src__)) + C.free(unsafe.Pointer(__src__)) + fmt.Println("result herhehreh", result) + return nil //d.castStringToError(result) +} + +// See: http://duktape.org/api.html#duk_pcompile_string_filename +func (d *Context) PcompileStringFilename(flags uint, src string) error { + __src__ := C.CString(src) + result := int(C._duk_pcompile_string_filename(d.duk_context, C.duk_uint_t(flags), __src__)) + C.free(unsafe.Pointer(__src__)) + return d.castStringToError(result) +} + +// See: http://duktape.org/api.html#duk_peval +func (d *Context) Peval() error { + result := int(C._duk_peval(d.duk_context)) + return d.castStringToError(result) +} + +// See: http://duktape.org/api.html#duk_peval_file +func (d *Context) PevalFile(path string) error { + __path__ := C.CString(path) + result := int(C._duk_peval_file(d.duk_context, __path__)) + C.free(unsafe.Pointer(__path__)) + return d.castStringToError(result) +} + +// See: http://duktape.org/api.html#duk_peval_file_noresult +func (d *Context) PevalFileNoresult(path string) int { + __path__ := C.CString(path) + result := int(C._duk_peval_file_noresult(d.duk_context, __path__)) + C.free(unsafe.Pointer(__path__)) + return result +} + +// See: http://duktape.org/api.html#duk_peval_lstring +func (d *Context) PevalLstring(src string, len int) error { + __src__ := C.CString(src) + result := int(C._duk_peval_lstring(d.duk_context, __src__, C.duk_size_t(len))) + C.free(unsafe.Pointer(__src__)) + return d.castStringToError(result) + +} + +// See: http://duktape.org/api.html#duk_peval_lstring_noresult +func (d *Context) PevalLstringNoresult(src string, len int) int { + __src__ := C.CString(src) + result := int(C._duk_peval_lstring_noresult(d.duk_context, __src__, C.duk_size_t(len))) + C.free(unsafe.Pointer(__src__)) + return result +} + +// See: http://duktape.org/api.html#duk_peval_noresult +func (d *Context) PevalNoresult() int { + return int(C._duk_peval_noresult(d.duk_context)) +} + +// See: http://duktape.org/api.html#duk_peval_string +func (d *Context) PevalString(src string) error { + __src__ := C.CString(src) + result := int(C._duk_peval_string(d.duk_context, __src__)) + C.free(unsafe.Pointer(__src__)) + return d.castStringToError(result) +} + +// See: http://duktape.org/api.html#duk_peval_string_noresult +func (d *Context) PevalStringNoresult(src string) int { + __src__ := C.CString(src) + result := int(C._duk_peval_string_noresult(d.duk_context, __src__)) + C.free(unsafe.Pointer(__src__)) + return result +} + +func (d *Context) castStringToError(result int) error { + if result == 0 { + return nil + } + + err := &Error{} + for _, key := range []string{"name", "message", "fileName", "lineNumber", "stack"} { + d.GetPropString(-1, key) + + switch key { + case "name": + err.Type = d.SafeToString(-1) + case "message": + err.Message = d.SafeToString(-1) + case "fileName": + err.FileName = d.SafeToString(-1) + case "lineNumber": + if d.IsNumber(-1) { + err.LineNumber = d.GetInt(-1) + } + case "stack": + err.Stack = d.SafeToString(-1) + } + + d.Pop() + } + + return err +} + +// See: http://duktape.org/api.html#duk_pop +func (d *Context) Pop() { + if d.GetTop() == 0 { + return + } + C.duk_pop(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_pop_2 +func (d *Context) Pop2() { + d.PopN(2) +} + +// See: http://duktape.org/api.html#duk_pop_3 +func (d *Context) Pop3() { + d.PopN(3) +} + +// See: http://duktape.org/api.html#duk_pop_n +func (d *Context) PopN(count int) { + if d.GetTop() < count || count < 1 { + return + } + C.duk_pop_n(d.duk_context, C.duk_idx_t(count)) +} + +// See: http://duktape.org/api.html#duk_push_array +func (d *Context) PushArray() int { + return int(C.duk_push_array(d.duk_context)) +} + +// See: http://duktape.org/api.html#duk_push_boolean +func (d *Context) PushBoolean(val bool) { + var __val__ int + if val { + __val__ = 1 + } + C.duk_push_boolean(d.duk_context, C.duk_bool_t(__val__)) +} + +// See: http://duktape.org/api.html#duk_push_buffer +func (d *Context) PushBuffer(size int, dynamic bool) unsafe.Pointer { + var __dynamic__ int + if dynamic { + __dynamic__ = 1 + } + return C._duk_push_buffer(d.duk_context, C.duk_size_t(size), C.duk_bool_t(__dynamic__)) +} + +// See: http://duktape.org/api.html#duk_push_c_function +func (d *Context) PushCFunction(fn *[0]byte, nargs int) int { + return int(C.duk_push_c_function(d.duk_context, fn, C.duk_idx_t(nargs))) +} + +// See: http://duktape.org/api.html#duk_push_context_dump +func (d *Context) PushContextDump() { + C.duk_push_context_dump(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_push_current_function +func (d *Context) PushCurrentFunction() { + C.duk_push_current_function(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_push_current_thread +func (d *Context) PushCurrentThread() { + C.duk_push_current_thread(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_push_dynamic_buffer +func (d *Context) PushDynamicBuffer(size int) unsafe.Pointer { + return C._duk_push_dynamic_buffer(d.duk_context, C.duk_size_t(size)) +} + +// See: http://duktape.org/api.html#duk_push_error_object +func (d *Context) PushErrorObject(errCode int, format string, value interface{}) { + __str__ := C.CString(fmt.Sprintf(format, value)) + C._duk_push_error_object(d.duk_context, C.duk_errcode_t(errCode), __str__) + C.free(unsafe.Pointer(__str__)) +} + +// See: http://duktape.org/api.html#duk_push_false +func (d *Context) PushFalse() { + C.duk_push_false(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_push_fixed_buffer +func (d *Context) PushFixedBuffer(size int) unsafe.Pointer { + return C._duk_push_fixed_buffer(d.duk_context, C.duk_size_t(size)) +} + +// See: http://duktape.org/api.html#duk_push_global_object +func (d *Context) PushGlobalObject() { + C.duk_push_global_object(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_push_global_stash +func (d *Context) PushGlobalStash() { + C.duk_push_global_stash(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_push_heapptr +func (d *Context) PushHeapptr(ptr unsafe.Pointer) { + C.duk_push_heapptr(d.duk_context, ptr) +} + +// See: http://duktape.org/api.html#duk_push_heap_stash +func (d *Context) PushHeapStash() { + C.duk_push_heap_stash(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_push_int +func (d *Context) PushInt(val int) { + C.duk_push_int(d.duk_context, C.duk_int_t(val)) +} + +// See: http://duktape.org/api.html#duk_push_lstring +func (d *Context) PushLstring(str string, len int) string { + __str__ := C.CString(str) + var result string + if s := C.duk_push_lstring(d.duk_context, __str__, C.duk_size_t(len)); s != nil { + result = C.GoString(s) + } + C.free(unsafe.Pointer(__str__)) + return result +} + +// See: http://duktape.org/api.html#duk_push_nan +func (d *Context) PushNan() { + C.duk_push_nan(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_push_null +func (d *Context) PushNull() { + C.duk_push_null(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_push_number +func (d *Context) PushNumber(val float64) { + C.duk_push_number(d.duk_context, C.duk_double_t(val)) +} + +// See: http://duktape.org/api.html#duk_push_object +func (d *Context) PushObject() int { + return int(C.duk_push_object(d.duk_context)) +} + +// See: http://duktape.org/api.html#duk_push_string +func (d *Context) PushString(str string) string { + __str__ := C.CString(str) + var result string + if s := C.duk_push_string(d.duk_context, __str__); s != nil { + result = C.GoString(s) + } + C.free(unsafe.Pointer(__str__)) + return result +} + +// See: http://duktape.org/api.html#duk_push_string_file +func (d *Context) PushStringFile(path string) string { + __path__ := C.CString(path) + var result string + if s := C._duk_push_string_file(d.duk_context, __path__); s != nil { + result = C.GoString(s) + } + C.free(unsafe.Pointer(__path__)) + return result +} + +// See: http://duktape.org/api.html#duk_push_this +func (d *Context) PushThis() { + C.duk_push_this(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_push_thread +func (d *Context) PushThread() int { + return int(C._duk_push_thread(d.duk_context)) +} + +// See: http://duktape.org/api.html#duk_push_thread_new_globalenv +func (d *Context) PushThreadNewGlobalenv() int { + return int(C._duk_push_thread_new_globalenv(d.duk_context)) +} + +// See: http://duktape.org/api.html#duk_push_thread_stash +func (d *Context) PushThreadStash(targetCtx *Context) { + C.duk_push_thread_stash(d.duk_context, targetCtx.duk_context) +} + +// See: http://duktape.org/api.html#duk_push_true +func (d *Context) PushTrue() { + C.duk_push_true(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_push_uint +func (d *Context) PushUint(val uint) { + C.duk_push_uint(d.duk_context, C.duk_uint_t(val)) +} + +// See: http://duktape.org/api.html#duk_push_undefined +func (d *Context) PushUndefined() { + C.duk_push_undefined(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_put_global_string +func (d *Context) PutGlobalString(key string) bool { + __key__ := C.CString(key) + result := int(C.duk_put_global_string(d.duk_context, __key__)) == 1 + C.free(unsafe.Pointer(__key__)) + return result +} + +// See: http://duktape.org/api.html#duk_put_prop +func (d *Context) PutProp(objIndex int) bool { + return int(C.duk_put_prop(d.duk_context, C.duk_idx_t(objIndex))) == 1 +} + +// See: http://duktape.org/api.html#duk_put_prop_index +func (d *Context) PutPropIndex(objIndex int, arrIndex uint) bool { + return int(C.duk_put_prop_index(d.duk_context, C.duk_idx_t(objIndex), C.duk_uarridx_t(arrIndex))) == 1 +} + +// See: http://duktape.org/api.html#duk_put_prop_string +func (d *Context) PutPropString(objIndex int, key string) bool { + __key__ := C.CString(key) + result := int(C.duk_put_prop_string(d.duk_context, C.duk_idx_t(objIndex), __key__)) == 1 + C.free(unsafe.Pointer(__key__)) + return result +} + +// See: http://duktape.org/api.html#duk_remove +func (d *Context) Remove(index int) { + C.duk_remove(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_replace +func (d *Context) Replace(toIndex int) { + C.duk_replace(d.duk_context, C.duk_idx_t(toIndex)) +} + +// See: http://duktape.org/api.html#duk_require_boolean +func (d *Context) RequireBoolean(index int) bool { + return int(C.duk_require_boolean(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_require_buffer +func (d *Context) RequireBuffer(index int) (rawPtr unsafe.Pointer, outSize uint) { + rawPtr = C.duk_require_buffer(d.duk_context, C.duk_idx_t(index), (*C.duk_size_t)(unsafe.Pointer(&outSize))) + return rawPtr, outSize +} + +// See: http://duktape.org/api.html#duk_require_callable +func (d *Context) RequireCallable(index int) { + // At present, duk_require_callable is a macro that just calls duk_require_function. + // cgo does not support such macros we have to call it directly. + C.duk_require_function(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_require_context +func (d *Context) RequireContext(index int) *Context { + return contextFromPointer(C.duk_require_context(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_require_function +func (d *Context) RequireFunction(index int) { + C.duk_require_function(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_require_heapptr +func (d *Context) RequireHeapptr(index int) unsafe.Pointer { + return unsafe.Pointer(C.duk_require_heapptr(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_require_int +func (d *Context) RequireInt(index int) int { + return int(C.duk_require_int(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_require_lstring +func (d *Context) RequireLstring(index int) string { + if s := C.duk_require_lstring(d.duk_context, C.duk_idx_t(index), nil); s != nil { + return C.GoString(s) + } + return "" +} + +// See: http://duktape.org/api.html#duk_require_normalize_index +func (d *Context) RequireNormalizeIndex(index int) int { + return int(C.duk_require_normalize_index(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_require_null +func (d *Context) RequireNull(index int) { + C.duk_require_null(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_require_number +func (d *Context) RequireNumber(index int) float64 { + return float64(C.duk_require_number(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_require_object_coercible +func (d *Context) RequireObjectCoercible(index int) { + C._duk_require_object_coercible(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_require_pointer +func (d *Context) RequirePointer(index int) unsafe.Pointer { + return C.duk_require_pointer(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_require_stack +func (d *Context) RequireStack(extra int) { + C.duk_require_stack(d.duk_context, C.duk_idx_t(extra)) +} + +// See: http://duktape.org/api.html#duk_require_stack_top +func (d *Context) RequireStackTop(top int) { + C.duk_require_stack_top(d.duk_context, C.duk_idx_t(top)) +} + +// See: http://duktape.org/api.html#duk_require_string +func (d *Context) RequireString(index int) string { + if s := C.duk_require_string(d.duk_context, C.duk_idx_t(index)); s != nil { + return C.GoString(s) + } + return "" +} + +// See: http://duktape.org/api.html#duk_require_top_index +func (d *Context) RequireTopIndex() int { + return int(C.duk_require_top_index(d.duk_context)) +} + +// See: http://duktape.org/api.html#duk_require_type_mask +func (d *Context) RequireTypeMask(index int, mask uint) { + C._duk_require_type_mask(d.duk_context, C.duk_idx_t(index), C.duk_uint_t(mask)) +} + +// See: http://duktape.org/api.html#duk_require_uint +func (d *Context) RequireUint(index int) uint { + return uint(C.duk_require_uint(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_require_undefined +func (d *Context) RequireUndefined(index int) { + C.duk_require_undefined(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_require_valid_index +func (d *Context) RequireValidIndex(index int) { + C.duk_require_valid_index(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_resize_buffer +func (d *Context) ResizeBuffer(index int, newSize int) unsafe.Pointer { + return C.duk_resize_buffer(d.duk_context, C.duk_idx_t(index), C.duk_size_t(newSize)) +} + +// See: http://duktape.org/api.html#duk_safe_call +func (d *Context) SafeCall(fn, args *[0]byte, nargs, nrets int) int { + return int(C.duk_safe_call( + d.duk_context, + fn, + unsafe.Pointer(&args), + C.duk_idx_t(nargs), + C.duk_idx_t(nrets), + )) +} + +// See: http://duktape.org/api.html#duk_safe_to_lstring +func (d *Context) SafeToLstring(index int) string { + if s := C.duk_safe_to_lstring(d.duk_context, C.duk_idx_t(index), nil); s != nil { + return C.GoString(s) + } + return "" +} + +// See: http://duktape.org/api.html#duk_safe_to_string +func (d *Context) SafeToString(index int) string { + if s := C._duk_safe_to_string(d.duk_context, C.duk_idx_t(index)); s != nil { + return C.GoString(s) + } + return "" +} + +// See: http://duktape.org/api.html#duk_set_finalizer +func (d *Context) SetFinalizer(index int) { + C.duk_set_finalizer(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_set_global_object +func (d *Context) SetGlobalObject() { + C.duk_set_global_object(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_set_magic +func (d *Context) SetMagic(index int, magic int) { + C.duk_set_magic(d.duk_context, C.duk_idx_t(index), C.duk_int_t(magic)) +} + +// See: http://duktape.org/api.html#duk_set_prototype +func (d *Context) SetPrototype(index int) { + C.duk_set_prototype(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_set_top +func (d *Context) SetTop(index int) { + C.duk_set_top(d.duk_context, C.duk_idx_t(index)) +} + +func (d *Context) StrictEquals(index1 int, index2 int) bool { + return int(C.duk_strict_equals(d.duk_context, C.duk_idx_t(index1), C.duk_idx_t(index2))) == 1 +} + +// See: http://duktape.org/api.html#duk_substring +func (d *Context) Substring(index int, startCharOffset int, endCharOffset int) { + C.duk_substring(d.duk_context, C.duk_idx_t(index), C.duk_size_t(startCharOffset), C.duk_size_t(endCharOffset)) +} + +// See: http://duktape.org/api.html#duk_swap +func (d *Context) Swap(index1 int, index2 int) { + C.duk_swap(d.duk_context, C.duk_idx_t(index1), C.duk_idx_t(index2)) +} + +// See: http://duktape.org/api.html#duk_swap_top +func (d *Context) SwapTop(index int) { + C.duk_swap_top(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_throw +func (d *Context) Throw() { + C.duk_throw_raw(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_to_boolean +func (d *Context) ToBoolean(index int) bool { + return int(C.duk_to_boolean(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_to_buffer +func (d *Context) ToBuffer(index int) (rawPtr unsafe.Pointer, outSize uint) { + rawPtr = C._duk_to_buffer(d.duk_context, C.duk_idx_t(index), (*C.duk_size_t)(unsafe.Pointer(&outSize))) + return rawPtr, outSize +} + +// See: http://duktape.org/api.html#duk_to_defaultvalue +func (d *Context) ToDefaultvalue(index int, hint int) { + C.duk_to_defaultvalue(d.duk_context, C.duk_idx_t(index), C.duk_int_t(hint)) +} + +// See: http://duktape.org/api.html#duk_to_dynamic_buffer +func (d *Context) ToDynamicBuffer(index int) (rawPtr unsafe.Pointer, outSize uint) { + rawPtr = C._duk_to_dynamic_buffer(d.duk_context, C.duk_idx_t(index), (*C.duk_size_t)(unsafe.Pointer(&outSize))) + return rawPtr, outSize +} + +// See: http://duktape.org/api.html#duk_to_fixed_buffer +func (d *Context) ToFixedBuffer(index int) (rawPtr unsafe.Pointer, outSize uint) { + rawPtr = C._duk_to_fixed_buffer(d.duk_context, C.duk_idx_t(index), (*C.duk_size_t)(unsafe.Pointer(&outSize))) + return rawPtr, outSize +} + +// See: http://duktape.org/api.html#duk_to_int +func (d *Context) ToInt(index int) int { + return int(C.duk_to_int(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_to_int32 +func (d *Context) ToInt32(index int) int32 { + return int32(C.duk_to_int32(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_to_lstring +func (d *Context) ToLstring(index int) string { + if s := C.duk_to_lstring(d.duk_context, C.duk_idx_t(index), nil); s != nil { + return C.GoString(s) + } + return "" +} + +// See: http://duktape.org/api.html#duk_to_null +func (d *Context) ToNull(index int) { + C.duk_to_null(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_to_number +func (d *Context) ToNumber(index int) float64 { + return float64(C.duk_to_number(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_to_object +func (d *Context) ToObject(index int) { + C.duk_to_object(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_to_pointer +func (d *Context) ToPointer(index int) unsafe.Pointer { + return C.duk_to_pointer(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_to_primitive +func (d *Context) ToPrimitive(index int, hint int) { + C.duk_to_primitive(d.duk_context, C.duk_idx_t(index), C.duk_int_t(hint)) +} + +// See: http://duktape.org/api.html#duk_to_string +func (d *Context) ToString(index int) string { + if s := C.duk_to_string(d.duk_context, C.duk_idx_t(index)); s != nil { + return C.GoString(s) + } + return "" +} + +// See: http://duktape.org/api.html#duk_to_uint +func (d *Context) ToUint(index int) uint { + return uint(C.duk_to_uint(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_to_uint16 +func (d *Context) ToUint16(index int) uint16 { + return uint16(C.duk_to_uint16(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_to_uint32 +func (d *Context) ToUint32(index int) uint32 { + return uint32(C.duk_to_uint32(d.duk_context, C.duk_idx_t(index))) +} + +// See: http://duktape.org/api.html#duk_to_undefined +func (d *Context) ToUndefined(index int) { + C.duk_to_undefined(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_trim +func (d *Context) Trim(index int) { + C.duk_trim(d.duk_context, C.duk_idx_t(index)) +} + +// See: http://duktape.org/api.html#duk_xcopy_top +func (d *Context) XcopyTop(fromCtx *Context, count int) { + C._duk_xcopy_top(d.duk_context, fromCtx.duk_context, C.duk_idx_t(count)) +} + +// See: http://duktape.org/api.html#duk_xmove_top +func (d *Context) XmoveTop(fromCtx *Context, count int) { + C._duk_xmove_top(d.duk_context, fromCtx.duk_context, C.duk_idx_t(count)) +} + +// See: http://duktape.org/api.html#duk_push_pointer +func (d *Context) PushPointer(p unsafe.Pointer) { + C.duk_push_pointer(d.duk_context, p) +} + +//---[ Duktape 1.3 API ]--- // +// See: http://duktape.org/api.html#duk_debugger_attach +func (d *Context) DebuggerAttach( + readFn, + writeFn, + peekFn, + readFlushFn, + writeFlushFn, + detachedFn *[0]byte, + uData unsafe.Pointer) { + C.duk_debugger_attach( + d.duk_context, + readFn, + writeFn, + peekFn, + readFlushFn, + writeFlushFn, + nil, + detachedFn, + uData, + ) +} + +// See: http://duktape.org/api.html#duk_debugger_cooperate +func (d *Context) DebuggerCooperate() { + C.duk_debugger_cooperate(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_debugger_detach +func (d *Context) DebuggerDetach() { + C.duk_debugger_detach(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_dump_function +func (d *Context) DumpFunction() { + C.duk_dump_function(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_error_va +func (d *Context) ErrorVa(errCode int, a ...interface{}) { + str := fmt.Sprint(a...) + d.Error(errCode, str) +} + +// See: http://duktape.org/api.html#duk_instanceof +func (d *Context) Instanceof(idx1, idx2 int) bool { + return int(C.duk_instanceof(d.duk_context, C.duk_idx_t(idx1), C.duk_idx_t(idx2))) == 1 +} + +// See: http://duktape.org/api.html#duk_is_lightfunc +func (d *Context) IsLightfunc(index int) bool { + return int(C.duk_is_lightfunc(d.duk_context, C.duk_idx_t(index))) == 1 +} + +// See: http://duktape.org/api.html#duk_load_function +func (d *Context) LoadFunction() { + C.duk_load_function(d.duk_context) +} + +// See: http://duktape.org/api.html#duk_log +func (d *Context) Log(loglevel int, format string, value interface{}) { + __str__ := C.CString(fmt.Sprintf(format, value)) + C._duk_log(d.duk_context, C.duk_int_t(loglevel), __str__) + C.free(unsafe.Pointer(__str__)) +} + +// See: http://duktape.org/api.html#duk_log_va +func (d *Context) LogVa(logLevel int, format string, values ...interface{}) { + __str__ := C.CString(fmt.Sprintf(format, values...)) + C._duk_log(d.duk_context, C.duk_int_t(logLevel), __str__) + C.free(unsafe.Pointer(__str__)) +} + +// See: http://duktape.org/api.html#duk_pnew +func (d *Context) Pnew(nargs int) error { + result := int(C.duk_pnew(d.duk_context, C.duk_idx_t(nargs))) + return d.castStringToError(result) +} + +// See: http://duktape.org/api.html#duk_push_buffer_object +func (d *Context) PushBufferObject(bufferIdx, size, length int, flags uint) { + C.duk_push_buffer_object( + d.duk_context, + C.duk_idx_t(bufferIdx), + C.duk_size_t(size), + C.duk_size_t(length), + C.duk_uint_t(flags), + ) +} + +// See: http://duktape.org/api.html#duk_push_c_lightfunc +func (d *Context) PushCLightfunc(fn *[0]byte, nargs, length, magic int) int { + return int(C.duk_push_c_lightfunc( + d.duk_context, + fn, + C.duk_idx_t(nargs), + C.duk_idx_t(length), + C.duk_int_t(magic), + )) +} + +// See: http://duktape.org/api.html#duk_push_error_object_va +func (d *Context) PushErrorObjectVa(errCode int, format string, values ...interface{}) { + __str__ := C.CString(fmt.Sprintf(format, values...)) + C._duk_push_error_object(d.duk_context, C.duk_errcode_t(errCode), __str__) + C.free(unsafe.Pointer(__str__)) +} + +// See: http://duktape.org/api.html#duk_push_external_buffer +func (d *Context) PushExternalBuffer() { + C._duk_push_external_buffer(d.duk_context) +} + +/** + * Unimplemented. + * + * CharCodeAt see: http://duktape.org/api.html#duk_char_code_at + * CreateHeap see: http://duktape.org/api.html#duk_create_heap + * DecodeString see: http://duktape.org/api.html#duk_decode_string + * Free see: http://duktape.org/api.html#duk_free + * FreeRaw see: http://duktape.org/api.html#duk_free_raw + * GetCFunction see: http://duktape.org/api.html#duk_get_c_function + * GetMemoryFunctions see: http://duktape.org/api.html#duk_get_memory_functions + * MapString see: http://duktape.org/api.html#duk_map_string + * PushSprintf see: http://duktape.org/api.html#duk_push_sprintf + * PushVsprintf see: http://duktape.org/api.html#duk_push_vsprintf + * PutFunctionList see: http://duktape.org/api.html#duk_put_function_list + * PutNumberList see: http://duktape.org/api.html#duk_put_number_list + * Realloc see: http://duktape.org/api.html#duk_realloc + * ReallocRaw see: http://duktape.org/api.html#duk_realloc_raw + * RequireCFunction see: http://duktape.org/api.html#duk_require_c_function + * ConfigBuffer see: http://duktape.org/api.html#duk_config_buffer + * GetBufferData see: http://duktape.org/api.html#duk_get_buffer_data + * StealBuffer see: http://duktape.org/api.html#duk_steal_buffer + * RequireBufferData see: http://duktape.org/api.html#duk_require_buffer_data + * IsEvalError see: http://duktape.org/api.html#duk_is_eval_error + */ diff --git a/vendor/gopkg.in/olebedev/go-duktape.v3/conts.go b/vendor/gopkg.in/olebedev/go-duktape.v3/conts.go new file mode 100644 index 0000000000..f3dbd8bc84 --- /dev/null +++ b/vendor/gopkg.in/olebedev/go-duktape.v3/conts.go @@ -0,0 +1,121 @@ +package duktape + +const ( + CompileEval uint = 1 << iota + CompileFunction + CompileStrict + CompileSafe + CompileNoResult + CompileNoSource + CompileStrlen +) + +const ( + TypeNone Type = iota + TypeUndefined + TypeNull + TypeBoolean + TypeNumber + TypeString + TypeObject + TypeBuffer + TypePointer + TypeLightFunc +) + +const ( + TypeMaskNone uint = 1 << iota + TypeMaskUndefined + TypeMaskNull + TypeMaskBoolean + TypeMaskNumber + TypeMaskString + TypeMaskObject + TypeMaskBuffer + TypeMaskPointer + TypeMaskLightFunc +) + +const ( + EnumIncludeNonenumerable uint = 1 << iota + EnumIncludeInternal + EnumOwnPropertiesOnly + EnumArrayIndicesOnly + EnumSortArrayIndices + NoProxyBehavior +) + +const ( + ErrNone int = 0 + + // Internal to Duktape + ErrUnimplemented int = 50 + iota + ErrUnsupported + ErrInternal + ErrAlloc + ErrAssertion + ErrAPI + ErrUncaughtError +) + +const ( + // Common prototypes + ErrError int = 1 + iota + ErrEval + ErrRange + ErrReference + ErrSyntax + ErrType + ErrURI +) + +const ( + // Returned error values + ErrRetUnimplemented int = -(ErrUnimplemented + iota) + ErrRetUnsupported + ErrRetInternal + ErrRetAlloc + ErrRetAssertion + ErrRetAPI + ErrRetUncaughtError +) + +const ( + ErrRetError int = -(ErrError + iota) + ErrRetEval + ErrRetRange + ErrRetReference + ErrRetSyntax + ErrRetType + ErrRetURI +) + +const ( + ExecSuccess = iota + ExecError +) + +const ( + LogTrace int = iota + LogDebug + LogInfo + LogWarn + LogError + LogFatal +) + +const ( + BufobjDuktapeAuffer = 0 + BufobjNodejsAuffer = 1 + BufobjArraybuffer = 2 + BufobjDataview = 3 + BufobjInt8array = 4 + BufobjUint8array = 5 + BufobjUint8clampedarray = 6 + BufobjInt16array = 7 + BufobjUint16array = 8 + BufobjInt32array = 9 + BufobjUint32array = 10 + BufobjFloat32array = 11 + BufobjFloat64array = 12 +) diff --git a/vendor/gopkg.in/olebedev/go-duktape.v3/duk_alloc_pool.c b/vendor/gopkg.in/olebedev/go-duktape.v3/duk_alloc_pool.c new file mode 100755 index 0000000000..750fa71df1 --- /dev/null +++ b/vendor/gopkg.in/olebedev/go-duktape.v3/duk_alloc_pool.c @@ -0,0 +1,612 @@ +/* + * Pool allocator for low memory targets. + */ + +#include +#include +#include +#include +#include +#include "duktape.h" +#include "duk_alloc_pool.h" + +/* Define to enable some debug printfs. */ +/* #define DUK_ALLOC_POOL_DEBUG */ + +/* Define to enable approximate waste tracking. */ +/* #define DUK_ALLOC_POOL_TRACK_WASTE */ + +/* Define to track global highwater for used and waste bytes. VERY SLOW, only + * useful for manual testing. + */ +/* #define DUK_ALLOC_POOL_TRACK_HIGHWATER */ + +#if defined(DUK_ALLOC_POOL_ROMPTR_COMPRESSION) +#if 0 /* This extern declaration is provided by duktape.h, array provided by duktape.c. */ +extern const void * const duk_rom_compressed_pointers[]; +#endif +const void *duk_alloc_pool_romptr_low = NULL; +const void *duk_alloc_pool_romptr_high = NULL; +static void duk__alloc_pool_romptr_init(void); +#endif + +#if defined(DUK_USE_HEAPPTR16) +void *duk_alloc_pool_ptrcomp_base = NULL; +#endif + +#if defined(DUK_ALLOC_POOL_DEBUG) +static void duk__alloc_pool_dprintf(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); +} +#endif + +/* + * Pool initialization + */ + +void *duk_alloc_pool_init(char *buffer, + size_t size, + const duk_pool_config *configs, + duk_pool_state *states, + int num_pools, + duk_pool_global *global) { + double t_min, t_max, t_curr, x; + int step, i, j, n; + size_t total; + char *p; + + /* XXX: check that 'size' is not too large when using pointer + * compression. + */ + + /* To optimize pool counts first come up with a 't' which still allows + * total pool size to fit within user provided region. After that + * sprinkle any remaining bytes to the counts. Binary search with a + * fixed step count; last round uses 't_min' as 't_curr' to ensure it + * succeeds. + */ + + t_min = 0.0; /* Unless config is insane, this should always be "good". */ + t_max = 1e6; + + for (step = 0; ; step++) { + if (step >= 100) { + /* Force "known good", rerun config, and break out. + * Deals with rounding corner cases where t_curr is + * persistently "bad" even though t_min is a valid + * solution. + */ + t_curr = t_min; + } else { + t_curr = (t_min + t_max) / 2.0; + } + + for (i = 0, total = 0; i < num_pools; i++) { + states[i].size = configs[i].size; + + /* Target bytes = A*t + B ==> target count = (A*t + B) / block_size. + * Rely on A and B being small enough so that 'x' won't wrap. + */ + x = ((double) configs[i].a * t_curr + (double) configs[i].b) / (double) configs[i].size; + + states[i].count = (unsigned int) x; + total += (size_t) states[i].size * (size_t) states[i].count; + if (total > size) { + goto bad; + } + } + + /* t_curr is good. */ +#if defined(DUK_ALLOC_POOL_DEBUG) + duk__alloc_pool_dprintf("duk_alloc_pool_init: step=%d, t=[%lf %lf %lf] -> total %ld/%ld (good)\n", + step, t_min, t_curr, t_max, (long) total, (long) size); +#endif + if (step >= 100) { + /* Keep state[] initialization state. The state was + * created using the highest 't_min'. + */ + break; + } + t_min = t_curr; + continue; + + bad: + /* t_curr is bad. */ +#if defined(DUK_ALLOC_POOL_DEBUG) + duk__alloc_pool_dprintf("duk_alloc_pool_init: step=%d, t=[%lf %lf %lf] -> total %ld/%ld (bad)\n", + step, t_min, t_curr, t_max, (long) total, (long) size); +#endif + + if (step >= 1000) { + /* Cannot find any good solution; shouldn't happen + * unless config is bad or 'size' is so small that + * even a baseline allocation won't fit. + */ + return NULL; + } + t_max = t_curr; + /* continue */ + } + + /* The base configuration is now good; sprinkle any leftovers to + * pools in descending order. Note that for good t_curr, 'total' + * indicates allocated bytes so far and 'size - total' indicates + * leftovers. + */ + for (i = num_pools - 1; i >= 0; i--) { + while (size - total >= states[i].size) { + /* Ignore potential wrapping of states[i].count as the count + * is 32 bits and shouldn't wrap in practice. + */ + states[i].count++; + total += states[i].size; +#if defined(DUK_ALLOC_POOL_DEBUG) + duk__alloc_pool_dprintf("duk_alloc_pool_init: sprinkle %ld bytes (%ld left after) to pool index %ld, new count %ld\n", + (long) states[i].size, (long) (size - total), (long) i, (long) states[i].count); +#endif + } + } + + /* Pool counts are final. Allocate the user supplied region based + * on the final counts, initialize free lists for each block size, + * and otherwise finalize 'state' for use. + */ + p = buffer; + global->num_pools = num_pools; + global->states = states; +#if defined(DUK_ALLOC_POOL_TRACK_HIGHWATER) +#if defined(DUK_ALLOC_POOL_DEBUG) + duk__alloc_pool_dprintf("duk_alloc_pool_init: global highwater mark tracking enabled, THIS IS VERY SLOW!\n"); +#endif + global->hwm_used_bytes = 0U; + global->hwm_waste_bytes = 0U; +#endif +#if defined(DUK_ALLOC_POOL_TRACK_WASTE) +#if defined(DUK_ALLOC_POOL_DEBUG) + duk__alloc_pool_dprintf("duk_alloc_pool_init: approximate waste tracking enabled\n"); +#endif +#endif + +#if defined(DUK_USE_HEAPPTR16) + /* Register global base value for pointer compression, assumes + * a single active pool -4 allows a single subtract to be used and + * still ensures no non-NULL pointer encodes to zero. + */ + duk_alloc_pool_ptrcomp_base = (void *) (p - 4); +#endif + + for (i = 0; i < num_pools; i++) { + n = (int) states[i].count; + if (n > 0) { + states[i].first = (duk_pool_free *) p; + for (j = 0; j < n; j++) { + char *p_next = p + states[i].size; + ((duk_pool_free *) p)->next = + (j == n - 1) ? (duk_pool_free *) NULL : (duk_pool_free *) p_next; + p = p_next; + } + } else { + states[i].first = (duk_pool_free *) NULL; + } + states[i].alloc_end = p; +#if defined(DUK_ALLOC_POOL_TRACK_HIGHWATER) + states[i].hwm_used_count = 0; +#endif + /* All members of 'state' now initialized. */ + +#if defined(DUK_ALLOC_POOL_DEBUG) + duk__alloc_pool_dprintf("duk_alloc_pool_init: block size %5ld, count %5ld, %8ld total bytes, " + "end %p\n", + (long) states[i].size, (long) states[i].count, + (long) states[i].size * (long) states[i].count, + (void *) states[i].alloc_end); +#endif + } + +#if defined(DUK_ALLOC_POOL_ROMPTR_COMPRESSION) + /* ROM pointer compression precomputation. Assumes a single active + * pool. + */ + duk__alloc_pool_romptr_init(); +#endif + + /* Use 'global' as udata. */ + return (void *) global; +} + +/* + * Misc helpers + */ + +#if defined(DUK_ALLOC_POOL_TRACK_WASTE) +static void duk__alloc_pool_set_waste_marker(void *ptr, size_t used, size_t size) { + /* Rely on the base pointer and size being divisible by 4 and thus + * aligned. Use 32-bit markers: a 4-byte resolution is good enough, + * and comparing 32 bits at a time makes false waste estimates less + * likely than when comparing as bytes. + */ + duk_uint32_t *p, *p_start, *p_end; + size_t used_round; + + used_round = (used + 3U) & ~0x03U; /* round up to 4 */ + p_end = (duk_uint32_t *) ((duk_uint8_t *) ptr + size); + p_start = (duk_uint32_t *) ((duk_uint8_t *) ptr + used_round); + p = (duk_uint32_t *) p_start; + while (p != p_end) { + *p++ = DUK_ALLOC_POOL_WASTE_MARKER; + } +} +#else /* DUK_ALLOC_POOL_TRACK_WASTE */ +static void duk__alloc_pool_set_waste_marker(void *ptr, size_t used, size_t size) { + (void) ptr; (void) used; (void) size; +} +#endif /* DUK_ALLOC_POOL_TRACK_WASTE */ + +#if defined(DUK_ALLOC_POOL_TRACK_WASTE) +static size_t duk__alloc_pool_get_waste_estimate(void *ptr, size_t size) { + duk_uint32_t *p, *p_end, *p_start; + + /* Assumes size is >= 4. */ + p_start = (duk_uint32_t *) ptr; + p_end = (duk_uint32_t *) ((duk_uint8_t *) ptr + size); + p = p_end; + + /* This scan may cause harmless valgrind complaints: there may be + * uninitialized bytes within the legitimate allocation or between + * the start of the waste marker and the end of the allocation. + */ + do { + p--; + if (*p == DUK_ALLOC_POOL_WASTE_MARKER) { + ; + } else { + return (size_t) (p_end - p - 1) * 4U; + } + } while (p != p_start); + + return size; +} +#else /* DUK_ALLOC_POOL_TRACK_WASTE */ +static size_t duk__alloc_pool_get_waste_estimate(void *ptr, size_t size) { + (void) ptr; (void) size; + return 0; +} +#endif /* DUK_ALLOC_POOL_TRACK_WASTE */ + +static int duk__alloc_pool_ptr_in_freelist(duk_pool_state *s, void *ptr) { + duk_pool_free *curr; + + for (curr = s->first; curr != NULL; curr = curr->next) { + if ((void *) curr == ptr) { + return 1; + } + } + return 0; +} + +void duk_alloc_pool_get_pool_stats(duk_pool_state *s, duk_pool_stats *res) { + void *curr; + size_t free_count; + size_t used_count; + size_t waste_bytes; + + curr = s->alloc_end - (s->size * s->count); + free_count = 0U; + waste_bytes = 0U; + while (curr != s->alloc_end) { + if (duk__alloc_pool_ptr_in_freelist(s, curr)) { + free_count++; + } else { + waste_bytes += duk__alloc_pool_get_waste_estimate(curr, s->size); + } + curr = curr + s->size; + } + used_count = (size_t) (s->count - free_count); + + res->used_count = used_count; + res->used_bytes = (size_t) (used_count * s->size); + res->free_count = free_count; + res->free_bytes = (size_t) (free_count * s->size); + res->waste_bytes = waste_bytes; +#if defined(DUK_ALLOC_POOL_TRACK_HIGHWATER) + res->hwm_used_count = s->hwm_used_count; +#else + res->hwm_used_count = 0U; +#endif +} + +void duk_alloc_pool_get_global_stats(duk_pool_global *g, duk_pool_global_stats *res) { + int i; + size_t total_used = 0U; + size_t total_free = 0U; + size_t total_waste = 0U; + + for (i = 0; i < g->num_pools; i++) { + duk_pool_state *s = &g->states[i]; + duk_pool_stats stats; + + duk_alloc_pool_get_pool_stats(s, &stats); + + total_used += stats.used_bytes; + total_free += stats.free_bytes; + total_waste += stats.waste_bytes; + } + + res->used_bytes = total_used; + res->free_bytes = total_free; + res->waste_bytes = total_waste; +#if defined(DUK_ALLOC_POOL_TRACK_HIGHWATER) + res->hwm_used_bytes = g->hwm_used_bytes; + res->hwm_waste_bytes = g->hwm_waste_bytes; +#else + res->hwm_used_bytes = 0U; + res->hwm_waste_bytes = 0U; +#endif +} + +#if defined(DUK_ALLOC_POOL_TRACK_HIGHWATER) +static void duk__alloc_pool_update_highwater(duk_pool_global *g) { + int i; + size_t total_used = 0U; + size_t total_free = 0U; + size_t total_waste = 0U; + + /* Per pool highwater used count, useful to checking if a pool is + * too small. + */ + for (i = 0; i < g->num_pools; i++) { + duk_pool_state *s = &g->states[i]; + duk_pool_stats stats; + + duk_alloc_pool_get_pool_stats(s, &stats); + if (stats.used_count > s->hwm_used_count) { +#if defined(DUK_ALLOC_POOL_DEBUG) + duk__alloc_pool_dprintf("duk__alloc_pool_update_highwater: pool %ld (%ld bytes) highwater updated: count %ld -> %ld\n", + (long) i, (long) s->size, + (long) s->hwm_used_count, (long) stats.used_count); +#endif + s->hwm_used_count = stats.used_count; + } + + total_used += stats.used_bytes; + total_free += stats.free_bytes; + total_waste += stats.waste_bytes; + } + + /* Global highwater mark for used and waste bytes. Both fields are + * updated from the same snapshot based on highest used count. + * This is VERY, VERY slow and only useful for development. + * (Note that updating HWM states for pools individually and then + * summing them won't create a consistent global snapshot. There + * are still easy ways to make this much, much faster.) + */ + if (total_used > g->hwm_used_bytes) { +#if defined(DUK_ALLOC_POOL_DEBUG) + duk__alloc_pool_dprintf("duk__alloc_pool_update_highwater: global highwater updated: used=%ld, bytes=%ld -> " + "used=%ld, bytes=%ld\n", + (long) g->hwm_used_bytes, (long) g->hwm_waste_bytes, + (long) total_used, (long) total_waste); +#endif + g->hwm_used_bytes = total_used; + g->hwm_waste_bytes = total_waste; + } +} +#else /* DUK_ALLOC_POOL_TRACK_HIGHWATER */ +static void duk__alloc_pool_update_highwater(duk_pool_global *g) { + (void) g; +} +#endif /* DUK_ALLOC_POOL_TRACK_HIGHWATER */ + +/* + * Allocation providers + */ + +void *duk_alloc_pool(void *udata, duk_size_t size) { + duk_pool_global *g = (duk_pool_global *) udata; + int i, n; + +#if defined(DUK_ALLOC_POOL_DEBUG) + duk__alloc_pool_dprintf("duk_alloc_pool: %p %ld\n", udata, (long) size); +#endif + + if (size == 0) { + return NULL; + } + + for (i = 0, n = g->num_pools; i < n; i++) { + duk_pool_state *st = g->states + i; + + if (size <= st->size) { + duk_pool_free *res = st->first; + if (res != NULL) { + st->first = res->next; + duk__alloc_pool_set_waste_marker((void *) res, size, st->size); + duk__alloc_pool_update_highwater(g); + return (void *) res; + } + } + + /* Allocation doesn't fit or no free entries, try to borrow + * from the next block size. There's no support for preventing + * a borrow at present. + */ + } + + return NULL; +} + +void *duk_realloc_pool(void *udata, void *ptr, duk_size_t size) { + duk_pool_global *g = (duk_pool_global *) udata; + int i, j, n; + +#if defined(DUK_ALLOC_POOL_DEBUG) + duk__alloc_pool_dprintf("duk_realloc_pool: %p %p %ld\n", udata, ptr, (long) size); +#endif + + if (ptr == NULL) { + return duk_alloc_pool(udata, size); + } + if (size == 0) { + duk_free_pool(udata, ptr); + return NULL; + } + + /* Non-NULL pointers are necessarily from the pool so we should + * always be able to find the allocation. + */ + + for (i = 0, n = g->num_pools; i < n; i++) { + duk_pool_state *st = g->states + i; + char *new_ptr; + + /* Because 'ptr' is assumed to be in the pool and pools are + * allocated in sequence, it suffices to check for end pointer + * only. + */ + if ((char *) ptr >= st->alloc_end) { + continue; + } + + if (size <= st->size) { + /* Allocation still fits existing allocation. Check if + * we can shrink the allocation to a smaller block size + * (smallest possible). + */ + for (j = 0; j < i; j++) { + duk_pool_state *st2 = g->states + j; + + if (size <= st2->size) { + new_ptr = (char *) st2->first; + if (new_ptr != NULL) { +#if defined(DUK_ALLOC_POOL_DEBUG) + duk__alloc_pool_dprintf("duk_realloc_pool: shrink, block size %ld -> %ld\n", + (long) st->size, (long) st2->size); +#endif + st2->first = ((duk_pool_free *) new_ptr)->next; + memcpy((void *) new_ptr, (const void *) ptr, (size_t) size); + ((duk_pool_free *) ptr)->next = st->first; + st->first = (duk_pool_free *) ptr; + duk__alloc_pool_set_waste_marker((void *) new_ptr, size, st2->size); + duk__alloc_pool_update_highwater(g); + return (void *) new_ptr; + } + } + } + + /* Failed to shrink; return existing pointer. */ + duk__alloc_pool_set_waste_marker((void *) ptr, size, st->size); + return ptr; + } + + /* Find first free larger block. */ + for (j = i + 1; j < n; j++) { + duk_pool_state *st2 = g->states + j; + + if (size <= st2->size) { + new_ptr = (char *) st2->first; + if (new_ptr != NULL) { + st2->first = ((duk_pool_free *) new_ptr)->next; + memcpy((void *) new_ptr, (const void *) ptr, (size_t) st->size); + ((duk_pool_free *) ptr)->next = st->first; + st->first = (duk_pool_free *) ptr; + duk__alloc_pool_set_waste_marker((void *) new_ptr, size, st2->size); + duk__alloc_pool_update_highwater(g); + return (void *) new_ptr; + } + } + } + + /* Failed to resize. */ + return NULL; + } + + /* We should never be here because 'ptr' should be a valid pool + * entry and thus always found above. + */ + return NULL; +} + +void duk_free_pool(void *udata, void *ptr) { + duk_pool_global *g = (duk_pool_global *) udata; + int i, n; + +#if defined(DUK_ALLOC_POOL_DEBUG) + duk__alloc_pool_dprintf("duk_free_pool: %p %p\n", udata, ptr); +#endif + + if (ptr == NULL) { + return; + } + + for (i = 0, n = g->num_pools; i < n; i++) { + duk_pool_state *st = g->states + i; + + /* Enough to check end address only. */ + if ((char *) ptr >= st->alloc_end) { + continue; + } + + ((duk_pool_free *) ptr)->next = st->first; + st->first = (duk_pool_free *) ptr; +#if 0 /* never necessary when freeing */ + duk__alloc_pool_update_highwater(g); +#endif + return; + } + + /* We should never be here because 'ptr' should be a valid pool + * entry and thus always found above. + */ +} + +/* + * Pointer compression + */ + +#if defined(DUK_ALLOC_POOL_ROMPTR_COMPRESSION) +static void duk__alloc_pool_romptr_init(void) { + /* Scan ROM pointer range for faster detection of "is 'p' a ROM pointer" + * later on. + */ + const void * const * ptrs = (const void * const *) duk_rom_compressed_pointers; + duk_alloc_pool_romptr_low = duk_alloc_pool_romptr_high = (const void *) *ptrs; + while (*ptrs) { + if (*ptrs > duk_alloc_pool_romptr_high) { + duk_alloc_pool_romptr_high = (const void *) *ptrs; + } + if (*ptrs < duk_alloc_pool_romptr_low) { + duk_alloc_pool_romptr_low = (const void *) *ptrs; + } + ptrs++; + } +} +#endif + +/* Encode/decode functions are defined in the header to allow inlining. */ + +#if defined(DUK_ALLOC_POOL_ROMPTR_COMPRESSION) +duk_uint16_t duk_alloc_pool_enc16_rom(void *ptr) { + /* The if-condition should be the fastest possible check + * for "is 'ptr' 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. + */ + const void * const * ptrs = duk_rom_compressed_pointers; + while (*ptrs) { + if (*ptrs == ptr) { + return DUK_ALLOC_POOL_ROMPTR_FIRST + (duk_uint16_t) (ptrs - duk_rom_compressed_pointers); + } + 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. + */ + return 0; +} +#endif diff --git a/vendor/gopkg.in/olebedev/go-duktape.v3/duk_alloc_pool.h b/vendor/gopkg.in/olebedev/go-duktape.v3/duk_alloc_pool.h new file mode 100755 index 0000000000..286ec0166a --- /dev/null +++ b/vendor/gopkg.in/olebedev/go-duktape.v3/duk_alloc_pool.h @@ -0,0 +1,223 @@ +#if !defined(DUK_ALLOC_POOL_H_INCLUDED) +#define DUK_ALLOC_POOL_H_INCLUDED + +#include "duktape.h" + +/* 32-bit (big endian) marker used at the end of pool entries so that wasted + * space can be detected. Waste tracking must be enabled explicitly. + */ +#if defined(DUK_ALLOC_POOL_TRACK_WASTE) +#define DUK_ALLOC_POOL_WASTE_MARKER 0xedcb2345UL +#endif + +/* 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 defined(DUK_USE_ROM_OBJECTS) && defined(DUK_USE_HEAPPTR16) +#define DUK_ALLOC_POOL_ROMPTR_COMPRESSION +#define DUK_ALLOC_POOL_ROMPTR_FIRST DUK_USE_ROM_PTRCOMP_FIRST + +/* This extern declaration is provided by duktape.h, array provided by duktape.c. + * Because duk_config.h may include this file (to get the inline functions) we + * need to forward declare this also here. + */ +extern const void * const duk_rom_compressed_pointers[]; +#endif + +/* Pool configuration for a certain block size. */ +typedef struct { + unsigned int size; /* must be divisible by 4 and >= sizeof(void *) */ + unsigned int a; /* bytes (not count) to allocate: a*t + b, t is an arbitrary scale parameter */ + unsigned int b; +} duk_pool_config; + +/* Freelist entry, must fit into the smallest block size. */ +struct duk_pool_free; +typedef struct duk_pool_free duk_pool_free; +struct duk_pool_free { + duk_pool_free *next; +}; + +/* Pool state for a certain block size. */ +typedef struct { + duk_pool_free *first; + char *alloc_end; + unsigned int size; + unsigned int count; +#if defined(DUK_ALLOC_POOL_TRACK_HIGHWATER) + unsigned int hwm_used_count; +#endif +} duk_pool_state; + +/* Statistics for a certain pool. */ +typedef struct { + size_t used_count; + size_t used_bytes; + size_t free_count; + size_t free_bytes; + size_t waste_bytes; + size_t hwm_used_count; +} duk_pool_stats; + +/* Top level state for all pools. Pointer to this struct is used as the allocator + * userdata pointer. + */ +typedef struct { + int num_pools; + duk_pool_state *states; +#if defined(DUK_ALLOC_POOL_TRACK_HIGHWATER) + size_t hwm_used_bytes; + size_t hwm_waste_bytes; +#endif +} duk_pool_global; + +/* Statistics for the entire set of pools. */ +typedef struct { + size_t used_bytes; + size_t free_bytes; + size_t waste_bytes; + size_t hwm_used_bytes; + size_t hwm_waste_bytes; +} duk_pool_global_stats; + +/* Initialize a pool allocator, arguments: + * - buffer and size: continuous region to use for pool, must align to 4 + * - config: configuration for pools in ascending block size + * - state: state for pools, matches config order + * - num_pools: number of entries in 'config' and 'state' + * - global: global state structure + * + * The 'config', 'state', and 'global' pointers must be valid beyond the init + * call, as long as the pool is used. + * + * Returns a void pointer to be used as userdata for the allocator functions. + * Concretely the return value will be "(void *) global", i.e. the global + * state struct. If pool init fails, the return value will be NULL. + */ +void *duk_alloc_pool_init(char *buffer, + size_t size, + const duk_pool_config *configs, + duk_pool_state *states, + int num_pools, + duk_pool_global *global); + +/* Duktape allocation providers. Typing matches Duktape requirements. */ +void *duk_alloc_pool(void *udata, duk_size_t size); +void *duk_realloc_pool(void *udata, void *ptr, duk_size_t size); +void duk_free_pool(void *udata, void *ptr); + +/* Stats. */ +void duk_alloc_pool_get_pool_stats(duk_pool_state *s, duk_pool_stats *res); +void duk_alloc_pool_get_global_stats(duk_pool_global *g, duk_pool_global_stats *res); + +/* Duktape pointer compression global state (assumes single pool). */ +#if defined(DUK_USE_ROM_OBJECTS) && defined(DUK_USE_HEAPPTR16) +extern const void *duk_alloc_pool_romptr_low; +extern const void *duk_alloc_pool_romptr_high; +duk_uint16_t duk_alloc_pool_enc16_rom(void *ptr); +#endif +#if defined(DUK_USE_HEAPPTR16) +extern void *duk_alloc_pool_ptrcomp_base; +#endif + +#if 0 +duk_uint16_t duk_alloc_pool_enc16(void *ptr); +void *duk_alloc_pool_dec16(duk_uint16_t val); +#endif + +/* Inlined pointer compression functions. Gcc and clang -Os won't in + * practice inline these without an "always inline" attribute because it's + * more size efficient (by a few kB) to use explicit calls instead. Having + * these defined inline here allows performance optimized builds to inline + * pointer compression operations. + * + * Pointer compression assumes there's a single globally registered memory + * pool which makes pointer compression more efficient. This would be easy + * to fix by adding a userdata pointer to the compression functions and + * plumbing the heap userdata from the compression/decompression macros. + */ + +/* DUK_ALWAYS_INLINE is not a public API symbol so it may go away in even a + * minor update. But it's pragmatic for this extra because it handles many + * compilers via duk_config.h detection. Check that the macro exists so that + * if it's gone, we can still compile. + */ +#if defined(DUK_ALWAYS_INLINE) +#define DUK__ALLOC_POOL_ALWAYS_INLINE DUK_ALWAYS_INLINE +#else +#define DUK__ALLOC_POOL_ALWAYS_INLINE /* nop */ +#endif + +#if defined(DUK_USE_HEAPPTR16) +static DUK__ALLOC_POOL_ALWAYS_INLINE duk_uint16_t duk_alloc_pool_enc16(void *ptr) { + if (ptr == NULL) { + /* With 'return 0' gcc and clang -Os generate inefficient code. + * For example, gcc -Os generates: + * + * 0804911d : + * 804911d: 55 push %ebp + * 804911e: 85 c0 test %eax,%eax + * 8049120: 89 e5 mov %esp,%ebp + * 8049122: 74 0b je 804912f + * 8049124: 2b 05 e4 90 07 08 sub 0x80790e4,%eax + * 804912a: c1 e8 02 shr $0x2,%eax + * 804912d: eb 02 jmp 8049131 + * 804912f: 31 c0 xor %eax,%eax + * 8049131: 5d pop %ebp + * 8049132: c3 ret + * + * The NULL path checks %eax for zero; if it is zero, a zero + * is unnecessarily loaded into %eax again. The non-zero path + * has an unnecessary jump as a side effect of this. + * + * Using 'return (duk_uint16_t) (intptr_t) ptr;' generates similarly + * inefficient code; not sure how to make the result better. + */ + return 0; + } +#if defined(DUK_ALLOC_POOL_ROMPTR_COMPRESSION) + if (ptr >= duk_alloc_pool_romptr_low && ptr <= duk_alloc_pool_romptr_high) { + /* This is complex enough now to need a separate function. */ + return duk_alloc_pool_enc16_rom(ptr); + } +#endif + return (duk_uint16_t) (((size_t) ((char *) ptr - (char *) duk_alloc_pool_ptrcomp_base)) >> 2); +} + +static DUK__ALLOC_POOL_ALWAYS_INLINE void *duk_alloc_pool_dec16(duk_uint16_t val) { + if (val == 0) { + /* As with enc16 the gcc and clang -Os output is inefficient, + * e.g. gcc -Os: + * + * 08049133 : + * 8049133: 55 push %ebp + * 8049134: 66 85 c0 test %ax,%ax + * 8049137: 89 e5 mov %esp,%ebp + * 8049139: 74 0e je 8049149 + * 804913b: 8b 15 e4 90 07 08 mov 0x80790e4,%edx + * 8049141: 0f b7 c0 movzwl %ax,%eax + * 8049144: 8d 04 82 lea (%edx,%eax,4),%eax + * 8049147: eb 02 jmp 804914b + * 8049149: 31 c0 xor %eax,%eax + * 804914b: 5d pop %ebp + * 804914c: c3 ret + */ + return NULL; + } +#if defined(DUK_ALLOC_POOL_ROMPTR_COMPRESSION) + if (val >= DUK_ALLOC_POOL_ROMPTR_FIRST) { + /* This is a blind lookup, could check index validity. + * Duktape should never decompress a pointer which would + * be out-of-bounds here. + */ + return (void *) (intptr_t) (duk_rom_compressed_pointers[val - DUK_ALLOC_POOL_ROMPTR_FIRST]); + } +#endif + return (void *) ((char *) duk_alloc_pool_ptrcomp_base + (((size_t) val) << 2)); +} +#endif + +#endif /* DUK_ALLOC_POOL_H_INCLUDED */ diff --git a/vendor/gopkg.in/olebedev/go-duktape.v3/duk_config.h b/vendor/gopkg.in/olebedev/go-duktape.v3/duk_config.h new file mode 100755 index 0000000000..3f217ef7d2 --- /dev/null +++ b/vendor/gopkg.in/olebedev/go-duktape.v3/duk_config.h @@ -0,0 +1,3672 @@ +/* + * duk_config.h configuration header generated by genconfig.py. + * + * Git commit: a459cf3c9bd1779fc01b435d69302b742675a08f + * Git describe: v2.2.0 + * Git branch: master + * + * Supported platforms: + * - Mac OSX, iPhone, Darwin + * - Orbis + * - OpenBSD + * - Generic BSD + * - Atari ST TOS + * - AmigaOS + * - Durango (XboxOne) + * - Windows + * - Flashplayer (Crossbridge) + * - QNX + * - TI-Nspire + * - Emscripten + * - Linux + * - Solaris + * - AIX + * - HPUX + * - 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 */ +/* not configured for DLL build */ +#undef DUK_F_DLL_BUILD + +/* Apple OSX, iOS */ +#if defined(__APPLE__) +#define DUK_F_APPLE +#endif + +/* FreeBSD */ +#if defined(__FreeBSD__) || defined(__FreeBSD) +#define DUK_F_FREEBSD +#endif + +/* Orbis (PS4) variant */ +#if defined(DUK_F_FREEBSD) && defined(__ORBIS__) +#define DUK_F_ORBIS +#endif + +/* OpenBSD */ +#if defined(__OpenBSD__) || defined(__OpenBSD) +#define DUK_F_OPENBSD +#endif + +/* NetBSD */ +#if defined(__NetBSD__) || defined(__NetBSD) +#define DUK_F_NETBSD +#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 + +/* Durango (Xbox One) */ +#if defined(_DURANGO) || defined(_XBOX_ONE) +#define DUK_F_DURANGO +#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 +#if defined(__SUNPRO_C) && (__SUNPRO_C < 0x550) +#define DUK_F_OLD_SOLARIS +/* Defines _ILP32 / _LP64 required by DUK_F_X86/DUK_F_X64. Platforms + * are processed before architectures, so this happens before the + * DUK_F_X86/DUK_F_X64 detection is emitted. + */ +#include +#endif +#endif + +/* AIX */ +#if defined(_AIX) +/* defined(__xlc__) || defined(__IBMC__): works but too wide */ +#define DUK_F_AIX +#endif + +/* HPUX */ +#if defined(__hpux) +#define DUK_F_HPUX +#if defined(__ia64) +#define DUK_F_HPUX_ITANIUM +#endif +#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 + +/* 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/ + * + * With DUK_F_OLD_SOLARIS the header must be included + * before this. + */ +#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) || defined(__aarch64__) +#define DUK_F_ARM +#if defined(__LP64__) || defined(_LP64) || defined(__arm64) || defined(__arm64__) || defined(__aarch64__) +#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 + +#if defined(ANDROID) || defined(__ANDROID__) +#define DUK_F_ANDROID +#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_ORBIS) +/* --- Orbis --- */ +/* Orbis = PS4 */ +#define DUK_USE_DATE_NOW_GETTIMEOFDAY +#define DUK_USE_DATE_TZO_GMTIME_S +/* no parsing (not an error) */ +#define DUK_USE_DATE_FMT_STRFTIME +#include +#include +#include +#include +#include + +#define DUK_USE_OS_STRING "orbis" +#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 +#if !defined(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_DURANGO) +/* --- Durango (XboxOne) --- */ +/* Durango = XboxOne + * Configuration is nearly identical to Windows, except for + * DUK_USE_DATE_TZO_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 + +/* MSVC does not have sys/param.h */ +#define DUK_USE_DATE_NOW_WINDOWS +#define DUK_USE_DATE_TZO_WINDOWS_NO_DST +/* 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 "durango" + +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 1 +#endif +#elif defined(DUK_F_WINDOWS) +/* --- Windows --- */ +/* Windows version can't obviously be determined at compile time, + * but _WIN32_WINNT indicates the minimum version targeted: + * - https://msdn.microsoft.com/en-us/library/6sehtctf.aspx + */ + +/* 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 */ + +#if defined(DUK_COMPILING_DUKTAPE) +/* Only include when compiling Duktape to avoid polluting application build + * with a lot of unnecessary defines. + */ +#include +#endif + +/* GetSystemTimePreciseAsFileTime() available from Windows 8: + * https://msdn.microsoft.com/en-us/library/windows/desktop/hh706895(v=vs.85).aspx + */ +#if defined(DUK_USE_DATE_NOW_WINDOWS_SUBMS) || defined(DUK_USE_DATE_NOW_WINDOWS) +/* User forced provider. */ +#else +#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0602) +#define DUK_USE_DATE_NOW_WINDOWS_SUBMS +#else +#define DUK_USE_DATE_NOW_WINDOWS +#endif +#endif + +#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. + */ + +/* QueryPerformanceCounter() may go backwards in Windows XP, so enable for + * Vista and later: https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx#qpc_support_in_windows_versions + */ +#if !defined(DUK_USE_GET_MONOTONIC_TIME_WINDOWS_QPC) && \ + defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600) +#define DUK_USE_GET_MONOTONIC_TIME_WINDOWS_QPC +#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 --- */ +#if defined(DUK_COMPILING_DUKTAPE) && !defined(_XOPEN_SOURCE) +#define _XOPEN_SOURCE /* e.g. strptime */ +#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 "tinspire" +#elif defined(DUK_F_EMSCRIPTEN) +/* --- Emscripten --- */ +#if defined(DUK_COMPILING_DUKTAPE) +#if !defined(_POSIX_C_SOURCE) +#define _POSIX_C_SOURCE 200809L +#endif +#if !defined(_GNU_SOURCE) +#define _GNU_SOURCE /* e.g. getdate_r */ +#endif +#if !defined(_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) +#if !defined(_POSIX_C_SOURCE) +#define _POSIX_C_SOURCE 200809L +#endif +#if !defined(_GNU_SOURCE) +#define _GNU_SOURCE /* e.g. getdate_r */ +#endif +#if !defined(_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 + +#if 0 /* XXX: safe condition? */ +#define DUK_USE_GET_MONOTONIC_TIME_CLOCK_GETTIME +#endif + +#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 +#if defined(DUK_F_OLD_SOLARIS) +/* Old Solaris with no endian.h, stdint.h */ +#define DUK_F_NO_STDINT_H +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 3 +#endif +#else /* DUK_F_OLD_SOLARIS */ +#include +#endif /* DUK_F_OLD_SOLARIS */ + +#include +#include +#include + +#define DUK_USE_OS_STRING "solaris" +#elif defined(DUK_F_AIX) +/* --- AIX --- */ +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 3 +#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 + +#define DUK_USE_OS_STRING "aix" +#elif defined(DUK_F_HPUX) +/* --- HPUX --- */ +#define DUK_F_NO_STDINT_H +#if !defined(DUK_USE_BYTEORDER) +#define DUK_USE_BYTEORDER 3 +#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 + +#define DUK_USE_OS_STRING "hpux" +#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. */ +#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_MIPS64) +/* --- MIPS 64-bit --- */ +#define DUK_USE_ARCH_STRING "mips64" +/* MIPS 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_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(__clang__) && defined(__has_builtin) +#if __has_builtin(__builtin_unpredictable) +#define DUK_UNPREDICTABLE(x) __builtin_unpredictable((x)) +#endif +#endif + +#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 + +/* DUK_HOT */ +/* DUK_COLD */ + +#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 +/* XXX: equivalent of clang __builtin_unpredictable? */ + +#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_C99) || defined(DUK_F_CPP11)) && \ + defined(DUK_F_GCC_VERSION) && (DUK_F_GCC_VERSION >= 40300) +#define DUK_HOT __attribute__((hot)) +#define DUK_COLD __attribute__((cold)) +#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 + +/* Avoid warning when doing DUK_UNREF(some_function). */ +#if defined(_MSC_VER) && (_MSC_VER < 1500) +#pragma warning(disable: 4100 4101 4550 4551) +#define DUK_UNREF(x) +#else +#define DUK_UNREF(x) do { __pragma(warning(suppress:4100 4101 4550 4551)) (x); } while (0) +#endif + +/* Older versions of MSVC don't support the LL/ULL suffix. */ +#define DUK_U64_CONSTANT(x) x##ui64 +#define DUK_I64_CONSTANT(x) x##i64 +#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(__clang__) && defined(__has_builtin) +#if __has_builtin(__builtin_unpredictable) +#define DUK_UNPREDICTABLE(x) __builtin_unpredictable((x)) +#endif +#endif + +#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)) || \ + ((defined(DUK_F_OLD_SOLARIS) || defined(DUK_F_AIX) || \ + defined(DUK_F_HPUX)) && defined(_ILP32)) || \ + defined(DUK_F_ARM32) +#define DUK_F_32BIT_PTRS +#elif defined(DUK_F_X64) || \ + (defined(__WORDSIZE) && (__WORDSIZE == 64)) || \ + ((defined(DUK_F_OLD_SOLARIS) || defined(DUK_F_AIX) || \ + defined(DUK_F_HPUX)) && defined(_LP64)) || \ + defined(DUK_F_ARM64) +#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) +#define DUK_F_HAVE_64BIT +typedef unsigned long duk_uint64_t; +typedef signed long duk_int64_t; +#endif +#if !defined(DUK_F_HAVE_64BIT) && defined(DUK_F_MSVC) +#define DUK_F_HAVE_64BIT +typedef unsigned __int64 duk_uint64_t; +typedef signed __int64 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 'unsigned int'. */ +typedef duk_small_uint_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 used in public API declarations and user code. Typedef maps to + * 'struct duk_hthread' like the 'duk_hthread' typedef which is used + * exclusively in internals. + */ +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 + */ + +/* An abort()-like primitive is needed by the default fatal error handler. */ +#if !defined(DUK_ABORT) +#define DUK_ABORT abort +#endif + +#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 + +/* 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_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_MEMZERO) +#define DUK_MEMZERO(p,n) DUK_MEMSET((p), 0, (n)) +#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) && \ + !defined(DUK_F_OLD_SOLARIS) && !defined(DUK_F_AIX) +#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) && \ + !defined(DUK_F_OLD_SOLARIS) && !defined(DUK_F_AIX) +#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 +#elif defined(DUK_F_AIX) +/* Older versions may be missing isnan(), etc. */ +#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 + +/* 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_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 + +/* The functions below exist only in C99/C++11 or later and need a workaround + * for platforms that don't include them. MSVC isn't detected as C99, but + * these functions also exist in MSVC 2013 and later so include a clause for + * that too. Android doesn't have log2; disable all of these for Android. + */ +#if (defined(DUK_F_C99) || defined(DUK_F_CPP11) || (defined(_MSC_VER) && (_MSC_VER >= 1800))) && \ + !defined(DUK_F_ANDROID) && !defined(DUK_F_MINT) +#if !defined(DUK_CBRT) +#define DUK_CBRT cbrt +#endif +#if !defined(DUK_LOG2) +#define DUK_LOG2 log2 +#endif +#if !defined(DUK_LOG10) +#define DUK_LOG10 log10 +#endif +#if !defined(DUK_TRUNC) +#define DUK_TRUNC trunc +#endif +#endif /* DUK_F_C99 etc */ + +/* NetBSD 6.0 x86 (at least) has a few problems with pow() semantics, + * see test-bug-netbsd-math-pow.js. MinGW has similar (but different) + * issues, see test-bug-mingw-math-issues.js. Enable pow() workarounds + * for these targets. + */ +#undef DUK_USE_POW_WORKAROUNDS +#if defined(DUK_F_NETBSD) || defined(DUK_F_MINGW) +#define DUK_USE_POW_WORKAROUNDS +#endif + +/* Similar workarounds for atan2() semantics issues. MinGW issues are + * documented in test-bug-mingw-math-issues.js. + */ +#undef DUK_USE_ATAN2_WORKAROUNDS +#if defined(DUK_F_MINGW) +#define DUK_USE_ATAN2_WORKAROUNDS +#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". + */ + +/* 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 + +#if !defined(DUK_U64_CONSTANT) +#define DUK_U64_CONSTANT(x) x##ULL +#endif +#if !defined(DUK_I64_CONSTANT) +#define DUK_I64_CONSTANT(x) x##LL +#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 */ +/* 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 + */ + +#define DUK_USE_ARRAY_BUILTIN +#define DUK_USE_ARRAY_FASTPATH +#define DUK_USE_ARRAY_PROP_FASTPATH +#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_BOOLEAN_BUILTIN +#define DUK_USE_BUFFEROBJECT_SUPPORT +#undef DUK_USE_BUFLEN16 +#define DUK_USE_BYTECODE_DUMP_SUPPORT +#define DUK_USE_CACHE_ACTIVATION +#define DUK_USE_CACHE_CATCHER +#define DUK_USE_CALLSTACK_LIMIT 10000 +#define DUK_USE_COMMONJS_MODULES +#define DUK_USE_COMPILER_RECLIMIT 2500 +#define DUK_USE_COROUTINE_SUPPORT +#undef DUK_USE_CPP_EXCEPTIONS +#undef DUK_USE_DATAPTR16 +#undef DUK_USE_DATAPTR_DEC16 +#undef DUK_USE_DATAPTR_ENC16 +#define DUK_USE_DATE_BUILTIN +#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_DEBUG +#undef DUK_USE_DEBUGGER_DUMPHEAP +#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_DEBUG_LEVEL 0 +#undef DUK_USE_DEBUG_WRITE +#define DUK_USE_DOUBLE_LINKED_HEAP +#define DUK_USE_DUKTAPE_BUILTIN +#define DUK_USE_ENCODING_BUILTINS +#define DUK_USE_ERRCREATE +#define DUK_USE_ERRTHROW +#define DUK_USE_ES6 +#define DUK_USE_ES6_OBJECT_PROTO_PROPERTY +#define DUK_USE_ES6_OBJECT_SETPROTOTYPEOF +#define DUK_USE_ES6_PROXY +#define DUK_USE_ES6_REGEXP_SYNTAX +#define DUK_USE_ES6_UNICODE_ESCAPE +#define DUK_USE_ES7 +#define DUK_USE_ES7_EXP_OPERATOR +#define DUK_USE_ES8 +#define DUK_USE_ES9 +#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_PREFER_SIZE +#define DUK_USE_EXEC_REGCONST_OPTIMIZE +#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 +#undef DUK_USE_FATAL_HANDLER +#define DUK_USE_FATAL_MAXLEN 128 +#define DUK_USE_FINALIZER_SUPPORT +#undef DUK_USE_FINALIZER_TORTURE +#undef DUK_USE_FUNCPTR16 +#undef DUK_USE_FUNCPTR_DEC16 +#undef DUK_USE_FUNCPTR_ENC16 +#define DUK_USE_FUNCTION_BUILTIN +#define DUK_USE_FUNC_FILENAME_PROPERTY +#define DUK_USE_FUNC_NAME_PROPERTY +#undef DUK_USE_GC_TORTURE +#undef DUK_USE_GET_MONOTONIC_TIME +#undef DUK_USE_GET_RANDOM_DOUBLE +#undef DUK_USE_GLOBAL_BINDING +#define DUK_USE_GLOBAL_BUILTIN +#undef DUK_USE_HEAPPTR16 +#undef DUK_USE_HEAPPTR_DEC16 +#undef DUK_USE_HEAPPTR_ENC16 +#define DUK_USE_HEX_FASTPATH +#define DUK_USE_HOBJECT_ARRAY_ABANDON_LIMIT 2 +#define DUK_USE_HOBJECT_ARRAY_FAST_RESIZE_LIMIT 9 +#define DUK_USE_HOBJECT_ARRAY_MINGROW_ADD 16 +#define DUK_USE_HOBJECT_ARRAY_MINGROW_DIVISOR 8 +#define DUK_USE_HOBJECT_ENTRY_MINGROW_ADD 16 +#define DUK_USE_HOBJECT_ENTRY_MINGROW_DIVISOR 8 +#define DUK_USE_HOBJECT_HASH_PART +#define DUK_USE_HOBJECT_HASH_PROP_LIMIT 8 +#define DUK_USE_HSTRING_ARRIDX +#define DUK_USE_HSTRING_CLEN +#undef DUK_USE_HSTRING_EXTDATA +#define DUK_USE_HSTRING_LAZY_CLEN +#define DUK_USE_HTML_COMMENTS +#define DUK_USE_IDCHAR_FASTPATH +#undef DUK_USE_INJECT_HEAP_ALLOC_ERROR +#undef DUK_USE_INTERRUPT_COUNTER +#undef DUK_USE_INTERRUPT_DEBUG_FIXUP +#define DUK_USE_JC +#define DUK_USE_JSON_BUILTIN +#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_JSON_SUPPORT +#define DUK_USE_JX +#define DUK_USE_LEXER_SLIDING_WINDOW +#undef DUK_USE_LIGHTFUNC_BUILTINS +#define DUK_USE_MARK_AND_SWEEP_RECLIMIT 256 +#define DUK_USE_MATH_BUILTIN +#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_SETTER_KEY_ARGUMENT +#define DUK_USE_NONSTD_STRING_FROMCHARCODE_32BIT +#define DUK_USE_NUMBER_BUILTIN +#define DUK_USE_OBJECT_BUILTIN +#undef DUK_USE_OBJSIZES16 +#undef DUK_USE_PARANOID_ERRORS +#define DUK_USE_PC2LINE +#define DUK_USE_PERFORMANCE_BUILTIN +#undef DUK_USE_PREFER_SIZE +#undef DUK_USE_PROMISE_BUILTIN +#define DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS +#undef DUK_USE_REFCOUNT16 +#define DUK_USE_REFCOUNT32 +#define DUK_USE_REFERENCE_COUNTING +#define DUK_USE_REFLECT_BUILTIN +#define DUK_USE_REGEXP_CANON_BITMAP +#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 +#define DUK_USE_SHEBANG_COMMENTS +#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 +#define DUK_USE_STRING_BUILTIN +#undef DUK_USE_STRLEN16 +#define DUK_USE_STRTAB_GROW_LIMIT 17 +#define DUK_USE_STRTAB_MAXSIZE 268435456L +#define DUK_USE_STRTAB_MINSIZE 1024 +#undef DUK_USE_STRTAB_PTRCOMP +#define DUK_USE_STRTAB_RESIZE_CHECK_MASK 255 +#define DUK_USE_STRTAB_SHRINK_LIMIT 6 +#undef DUK_USE_STRTAB_TORTURE +#undef DUK_USE_SYMBOL_BUILTIN +#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 */ +#define DUK_USE_VALSTACK_GROW_SHIFT 2 +#define DUK_USE_VALSTACK_LIMIT 1000000L +#define DUK_USE_VALSTACK_SHRINK_CHECK_SHIFT 2 +#define DUK_USE_VALSTACK_SHRINK_SLACK_SHIFT 4 +#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 + +/* + * 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() +#elif defined(DUK_USE_DATE_NOW_TIME) +#define DUK_USE_DATE_GET_NOW(ctx) duk_bi_date_get_now_time() +#elif defined(DUK_USE_DATE_NOW_WINDOWS) +#define DUK_USE_DATE_GET_NOW(ctx) duk_bi_date_get_now_windows() +#elif defined(DUK_USE_DATE_NOW_WINDOWS_SUBMS) +#define DUK_USE_DATE_GET_NOW(ctx) duk_bi_date_get_now_windows_subms() +#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_S) || 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)) +#elif defined(DUK_USE_DATE_TZO_WINDOWS_NO_DST) +#define DUK_USE_DATE_GET_LOCAL_TZOFFSET(d) duk_bi_date_get_local_tzoffset_windows_no_dst((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 + +#if defined(DUK_USE_GET_MONOTONIC_TIME) +/* External provider already defined. */ +#elif defined(DUK_USE_GET_MONOTONIC_TIME_CLOCK_GETTIME) +#define DUK_USE_GET_MONOTONIC_TIME(ctx) duk_bi_date_get_monotonic_time_clock_gettime() +#elif defined(DUK_USE_GET_MONOTONIC_TIME_WINDOWS_QPC) +#define DUK_USE_GET_MONOTONIC_TIME(ctx) duk_bi_date_get_monotonic_time_windows_qpc() +#else +/* No provider for DUK_USE_GET_MONOTONIC_TIME(), fall back to DUK_USE_DATE_GET_NOW(). */ +#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 +#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 +#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 +#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 +#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 +#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 +#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_BROWSER_LIKE) +#error unsupported config option used (option has been removed): DUK_USE_BROWSER_LIKE +#endif +#if defined(DUK_USE_BUILTIN_INITJS) +#error unsupported config option used (option has been removed): DUK_USE_BUILTIN_INITJS +#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_DDDPRINT) +#error unsupported config option used (option has been removed): DUK_USE_DDDPRINT +#endif +#if defined(DUK_USE_DDPRINT) +#error unsupported config option used (option has been removed): DUK_USE_DDPRINT +#endif +#if defined(DUK_USE_DEBUGGER_FWD_LOGGING) +#error unsupported config option used (option has been removed): DUK_USE_DEBUGGER_FWD_LOGGING +#endif +#if defined(DUK_USE_DEBUGGER_FWD_PRINTALERT) +#error unsupported config option used (option has been removed): DUK_USE_DEBUGGER_FWD_PRINTALERT +#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) +#error unsupported config option used (option has been removed): DUK_USE_DPRINT +#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_DPRINT_COLORS) +#error unsupported config option used (option has been removed): DUK_USE_DPRINT_COLORS +#endif +#if defined(DUK_USE_DPRINT_RDTSC) +#error unsupported config option used (option has been removed): DUK_USE_DPRINT_RDTSC +#endif +#if defined(DUK_USE_ES6_REGEXP_BRACES) +#error unsupported config option used (option has been removed): DUK_USE_ES6_REGEXP_BRACES +#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_FASTINT) && !defined(DUK_USE_64BIT_OPS) +#error config option DUK_USE_FASTINT requires option DUK_USE_64BIT_OPS (which is missing) +#endif +#if defined(DUK_USE_FILE_IO) +#error unsupported config option used (option has been removed): DUK_USE_FILE_IO +#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_MARKANDSWEEP_FINALIZER_TORTURE) +#error unsupported config option used (option has been removed): DUK_USE_MARKANDSWEEP_FINALIZER_TORTURE +#endif +#if defined(DUK_USE_MARK_AND_SWEEP) +#error unsupported config option used (option has been removed): DUK_USE_MARK_AND_SWEEP +#endif +#if defined(DUK_USE_MATH_FMAX) +#error unsupported config option used (option has been removed): DUK_USE_MATH_FMAX +#endif +#if defined(DUK_USE_MATH_FMIN) +#error unsupported config option used (option has been removed): DUK_USE_MATH_FMIN +#endif +#if defined(DUK_USE_MATH_ROUND) +#error unsupported config option used (option has been removed): DUK_USE_MATH_ROUND +#endif +#if defined(DUK_USE_MS_STRINGTABLE_RESIZE) +#error unsupported config option used (option has been removed): DUK_USE_MS_STRINGTABLE_RESIZE +#endif +#if defined(DUK_USE_NONSTD_REGEXP_DOLLAR_ESCAPE) +#error unsupported config option used (option has been removed): DUK_USE_NONSTD_REGEXP_DOLLAR_ESCAPE +#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_OCTAL_SUPPORT) +#error unsupported config option used (option has been removed): DUK_USE_OCTAL_SUPPORT +#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_PANIC_ABORT) +#error unsupported config option used (option has been removed): DUK_USE_PANIC_ABORT +#endif +#if defined(DUK_USE_PANIC_EXIT) +#error unsupported config option used (option has been removed): DUK_USE_PANIC_EXIT +#endif +#if defined(DUK_USE_PANIC_HANDLER) +#error unsupported config option used (option has been removed): DUK_USE_PANIC_HANDLER +#endif +#if defined(DUK_USE_PANIC_SEGFAULT) +#error unsupported config option used (option has been removed): DUK_USE_PANIC_SEGFAULT +#endif +#if defined(DUK_USE_POW_NETBSD_WORKAROUND) +#error unsupported config option used (option has been removed): DUK_USE_POW_NETBSD_WORKAROUND +#endif +#if defined(DUK_USE_RDTSC) +#error unsupported config option used (option has been removed): DUK_USE_RDTSC +#endif +#if defined(DUK_USE_REFZERO_FINALIZER_TORTURE) +#error unsupported config option used (option has been removed): DUK_USE_REFZERO_FINALIZER_TORTURE +#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) +#error unsupported config option used (option has been removed): DUK_USE_STRTAB_CHAIN +#endif +#if defined(DUK_USE_STRTAB_CHAIN_SIZE) +#error unsupported config option used (option has been removed): DUK_USE_STRTAB_CHAIN_SIZE +#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_STRTAB_PROBE) +#error unsupported config option used (option has been removed): DUK_USE_STRTAB_PROBE +#endif +#if defined(DUK_USE_STRTAB_PTRCOMP) && !defined(DUK_USE_HEAPPTR16) +#error config option DUK_USE_STRTAB_PTRCOMP requires option DUK_USE_HEAPPTR16 (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_USER_INITJS) +#error unsupported config option used (option has been removed): DUK_USE_USER_INITJS +#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/vendor/gopkg.in/olebedev/go-duktape.v3/duk_console.c b/vendor/gopkg.in/olebedev/go-duktape.v3/duk_console.c new file mode 100755 index 0000000000..9a29903cfb --- /dev/null +++ b/vendor/gopkg.in/olebedev/go-duktape.v3/duk_console.c @@ -0,0 +1,163 @@ +/* + * Minimal 'console' binding. + * + * https://github.com/DeveloperToolsWG/console-object/blob/master/api.md + * https://developers.google.com/web/tools/chrome-devtools/debug/console/console-reference + * https://developer.mozilla.org/en/docs/Web/API/console + */ + +#include +#include +#include "duktape.h" +#include "duk_console.h" + +/* XXX: Add some form of log level filtering. */ + +/* XXX: For now logs everything to stdout, V8/Node.js logs debug/info level + * to stdout, warn and above to stderr. Should this extra do the same? + */ + +/* XXX: Should all output be written via e.g. console.write(formattedMsg)? + * This would make it easier for user code to redirect all console output + * to a custom backend. + */ + +/* XXX: Init console object using duk_def_prop() when that call is available. */ + +static duk_ret_t duk__console_log_helper(duk_context *ctx, const char *error_name) { + duk_idx_t i, n; + duk_uint_t flags; + + flags = (duk_uint_t) duk_get_current_magic(ctx); + + n = duk_get_top(ctx); + + duk_get_global_string(ctx, "console"); + duk_get_prop_string(ctx, -1, "format"); + + for (i = 0; i < n; i++) { + if (duk_check_type_mask(ctx, i, DUK_TYPE_MASK_OBJECT)) { + /* Slow path formatting. */ + duk_dup(ctx, -1); /* console.format */ + duk_dup(ctx, i); + duk_call(ctx, 1); + duk_replace(ctx, i); /* arg[i] = console.format(arg[i]); */ + } + } + + duk_pop_2(ctx); + + duk_push_string(ctx, " "); + duk_insert(ctx, 0); + duk_join(ctx, n); + + if (error_name) { + duk_push_error_object(ctx, DUK_ERR_ERROR, "%s", duk_require_string(ctx, -1)); + duk_push_string(ctx, "name"); + duk_push_string(ctx, error_name); + duk_def_prop(ctx, -3, DUK_DEFPROP_FORCE | DUK_DEFPROP_HAVE_VALUE); /* to get e.g. 'Trace: 1 2 3' */ + duk_get_prop_string(ctx, -1, "stack"); + } + + fprintf(stdout, "%s\n", duk_to_string(ctx, -1)); + if (flags & DUK_CONSOLE_FLUSH) { + fflush(stdout); + } + return 0; +} + +static duk_ret_t duk__console_assert(duk_context *ctx) { + if (duk_to_boolean(ctx, 0)) { + return 0; + } + duk_remove(ctx, 0); + + return duk__console_log_helper(ctx, "AssertionError"); +} + +static duk_ret_t duk__console_log(duk_context *ctx) { + return duk__console_log_helper(ctx, NULL); +} + +static duk_ret_t duk__console_trace(duk_context *ctx) { + return duk__console_log_helper(ctx, "Trace"); +} + +static duk_ret_t duk__console_info(duk_context *ctx) { + return duk__console_log_helper(ctx, NULL); +} + +static duk_ret_t duk__console_warn(duk_context *ctx) { + return duk__console_log_helper(ctx, NULL); +} + +static duk_ret_t duk__console_error(duk_context *ctx) { + return duk__console_log_helper(ctx, "Error"); +} + +static duk_ret_t duk__console_dir(duk_context *ctx) { + /* For now, just share the formatting of .log() */ + return duk__console_log_helper(ctx, 0); +} + +static void duk__console_reg_vararg_func(duk_context *ctx, duk_c_function func, const char *name, duk_uint_t flags) { + duk_push_c_function(ctx, func, DUK_VARARGS); + duk_push_string(ctx, "name"); + duk_push_string(ctx, name); + duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE); /* Improve stacktraces by displaying function name */ + duk_set_magic(ctx, -1, (duk_int_t) flags); + duk_put_prop_string(ctx, -2, name); +} + +void duk_console_init(duk_context *ctx, duk_uint_t flags) { + duk_push_object(ctx); + + /* Custom function to format objects; user can replace. + * For now, try JX-formatting and if that fails, fall back + * to ToString(v). + */ + duk_eval_string(ctx, + "(function (E) {" + "return function format(v){" + "try{" + "return E('jx',v);" + "}catch(e){" + "return String(v);" /* String() allows symbols, ToString() internal algorithm doesn't. */ + "}" + "};" + "})(Duktape.enc)"); + duk_put_prop_string(ctx, -2, "format"); + + duk__console_reg_vararg_func(ctx, duk__console_assert, "assert", flags); + duk__console_reg_vararg_func(ctx, duk__console_log, "log", flags); + duk__console_reg_vararg_func(ctx, duk__console_log, "debug", flags); /* alias to console.log */ + duk__console_reg_vararg_func(ctx, duk__console_trace, "trace", flags); + duk__console_reg_vararg_func(ctx, duk__console_info, "info", flags); + duk__console_reg_vararg_func(ctx, duk__console_warn, "warn", flags); + duk__console_reg_vararg_func(ctx, duk__console_error, "error", flags); + duk__console_reg_vararg_func(ctx, duk__console_error, "exception", flags); /* alias to console.error */ + duk__console_reg_vararg_func(ctx, duk__console_dir, "dir", flags); + + duk_put_global_string(ctx, "console"); + + /* Proxy wrapping: ensures any undefined console method calls are + * ignored silently. This is required specifically by the + * DeveloperToolsWG proposal (and is implemented also by Firefox: + * https://bugzilla.mozilla.org/show_bug.cgi?id=629607). + */ + + if (flags & DUK_CONSOLE_PROXY_WRAPPER) { + /* Tolerate errors: Proxy may be disabled. */ + duk_peval_string_noresult(ctx, + "(function(){" + "var D=function(){};" + "console=new Proxy(console,{" + "get:function(t,k){" + "var v=t[k];" + "return typeof v==='function'?v:D;" + "}" + "});" + "})();" + ); + } +} diff --git a/vendor/gopkg.in/olebedev/go-duktape.v3/duk_console.h b/vendor/gopkg.in/olebedev/go-duktape.v3/duk_console.h new file mode 100755 index 0000000000..1488a95fa6 --- /dev/null +++ b/vendor/gopkg.in/olebedev/go-duktape.v3/duk_console.h @@ -0,0 +1,14 @@ +#if !defined(DUK_CONSOLE_H_INCLUDED) +#define DUK_CONSOLE_H_INCLUDED + +#include "duktape.h" + +/* Use a proxy wrapper to make undefined methods (console.foo()) no-ops. */ +#define DUK_CONSOLE_PROXY_WRAPPER (1 << 0) + +/* Flush output after every call. */ +#define DUK_CONSOLE_FLUSH (1 << 1) + +extern void duk_console_init(duk_context *ctx, duk_uint_t flags); + +#endif /* DUK_CONSOLE_H_INCLUDED */ diff --git a/vendor/gopkg.in/olebedev/go-duktape.v3/duk_logging.c b/vendor/gopkg.in/olebedev/go-duktape.v3/duk_logging.c new file mode 100755 index 0000000000..9be2c1c273 --- /dev/null +++ b/vendor/gopkg.in/olebedev/go-duktape.v3/duk_logging.c @@ -0,0 +1,380 @@ +/* + * Logging support + */ + +#include +#include +#include +#include "duktape.h" +#include "duk_logging.h" + +/* XXX: uses stderr always for now, configurable? */ + +#define DUK_LOGGING_FLUSH /* Duktape 1.x: flush stderr */ + +/* 3-letter log level strings. */ +static const char duk__log_level_strings[] = { + 'T', 'R', 'C', 'D', 'B', 'G', 'I', 'N', 'F', + 'W', 'R', 'N', 'E', 'R', 'R', 'F', 'T', 'L' +}; + +/* Log method names. */ +static const char *duk__log_method_names[] = { + "trace", "debug", "info", "warn", "error", "fatal" +}; + +/* Constructor. */ +static duk_ret_t duk__logger_constructor(duk_context *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. + */ + + duk_inspect_callstack_entry(ctx, -2); + if (duk_is_object(ctx, -1)) { + if (duk_get_prop_string(ctx, -1, "function")) { + if (duk_get_prop_string(ctx, -1, "fileName")) { + if (duk_is_string(ctx, -1)) { + duk_replace(ctx, 0); + } + } + } + } + /* Leave values on stack on purpose, ignored below. */ + + /* Stripping the filename might be a good idea + * ("/foo/bar/quux.js" -> logger name "quux"), + * but now used verbatim. + */ + } + /* 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_string(ctx, 1, "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. + */ +static duk_ret_t duk__logger_prototype_fmt(duk_context *ctx) { + if (duk_get_prop_string(ctx, 0, "toLogString")) { + /* [ 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; avoid coercing the buffer to a string to + * avoid string table traffic. + */ +static duk_ret_t duk__logger_prototype_raw(duk_context *ctx) { + const char *data; + duk_size_t data_len; + + data = (const char *) duk_require_buffer(ctx, 0, &data_len); + fwrite((const void *) data, 1, data_len, stderr); + fputc((int) '\n', stderr); +#if defined(DUK_LOGGING_FLUSH) + fflush(stderr); +#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. + */ +static duk_ret_t duk__logger_prototype_log_shared(duk_context *ctx) { + duk_double_t now; + duk_time_components comp; + duk_small_int_t entry_lev; + 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[32]; /* maximum format length is 24+1 (NUL), round up. */ + duk_size_t date_len; + duk_small_int_t rc; + + /* 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 into a buffer + * allocated directly with the correct size. If the backend + * function plays nice, it won't coerce the buffer to a string + * (and thus intern it). + */ + + entry_lev = duk_get_current_magic(ctx); + if (entry_lev < DUK_LOG_TRACE || entry_lev > DUK_LOG_FATAL) { + /* Should never happen, check just in case. */ + return 0; + } + nargs = duk_get_top(ctx); + + /* [ arg1 ... argN this ] */ + + /* + * Log level check + */ + + duk_push_this(ctx); + + duk_get_prop_string(ctx, -1, "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_get_now(ctx); + duk_time_to_components(ctx, now, &comp); + sprintf((char *) date_buf, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", + (int) comp.year, (int) comp.month + 1, (int) comp.day, + (int) comp.hours, (int) comp.minutes, (int) comp.seconds, + (int) comp.milliseconds); + + date_len = strlen((const char *) date_buf); + + duk_get_prop_string(ctx, -2, "n"); + duk_to_string(ctx, -1); + + /* [ arg1 ... argN this loggerLevel loggerName ] */ + + /* + * Pass 1 + */ + + /* Line format: