Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: DISCO-F469NI_javascript_blinker
v7.h
00001 #ifdef V7_MODULE_LINES 00002 #line 1 "v7/src/license.h" 00003 #endif 00004 /* 00005 * Copyright (c) 2013-2014 Cesanta Software Limited 00006 * All rights reserved 00007 * 00008 * This software is dual-licensed: you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License version 2 as 00010 * published by the Free Software Foundation. For the terms of this 00011 * license, see <http://www.gnu.org/licenses/>. 00012 * 00013 * You are free to use this software under the terms of the GNU General 00014 * Public License, but WITHOUT ANY WARRANTY; without even the implied 00015 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00016 * See the GNU General Public License for more details. 00017 * 00018 * Alternatively, you can license this software under a commercial 00019 * license, as set out in <https://www.cesanta.com/license>. 00020 */ 00021 00022 #ifdef V7_EXPOSE_PRIVATE 00023 #define V7_PRIVATE 00024 #define V7_EXTERN extern 00025 #else 00026 #define V7_PRIVATE static 00027 #define V7_EXTERN static 00028 #endif /* CS_V7_SRC_LICENSE_H_ */ 00029 #ifdef V7_MODULE_LINES 00030 #line 1 "v7/src/features_profiles.h" 00031 #endif 00032 /* 00033 * Copyright (c) 2014 Cesanta Software Limited 00034 * All rights reserved 00035 */ 00036 00037 #ifndef CS_V7_SRC_FEATURES_PROFILES_H_ 00038 #define CS_V7_SRC_FEATURES_PROFILES_H_ 00039 00040 #define V7_BUILD_PROFILE_MINIMAL 1 00041 #define V7_BUILD_PROFILE_MEDIUM 2 00042 #define V7_BUILD_PROFILE_FULL 3 00043 00044 #ifndef V7_BUILD_PROFILE 00045 #define V7_BUILD_PROFILE V7_BUILD_PROFILE_FULL 00046 #endif 00047 00048 #endif /* CS_V7_SRC_FEATURES_PROFILES_H_ */ 00049 #ifdef V7_MODULE_LINES 00050 #line 1 "v7/src/features_minimal.h" 00051 #endif 00052 /* 00053 * Copyright (c) 2014 Cesanta Software Limited 00054 * All rights reserved 00055 */ 00056 00057 /* Amalgamated: #include "v7/src/features_profiles.h" */ 00058 00059 #if V7_BUILD_PROFILE == V7_BUILD_PROFILE_MINIMAL 00060 00061 /* This space is intentionally left blank. */ 00062 00063 #endif /* CS_V7_SRC_FEATURES_MINIMAL_H_ */ 00064 #ifdef V7_MODULE_LINES 00065 #line 1 "v7/src/features_medium.h" 00066 #endif 00067 /* 00068 * Copyright (c) 2014 Cesanta Software Limited 00069 * All rights reserved 00070 */ 00071 00072 /* Amalgamated: #include "v7/src/features_profiles.h" */ 00073 00074 #if V7_BUILD_PROFILE == V7_BUILD_PROFILE_MEDIUM 00075 00076 #define V7_ENABLE__Date 1 00077 #define V7_ENABLE__Date__now 1 00078 #define V7_ENABLE__Date__UTC 1 00079 #define V7_ENABLE__Math 1 00080 #define V7_ENABLE__Math__atan2 1 00081 #define V7_ENABLE__RegExp 1 00082 00083 #endif /* CS_V7_SRC_FEATURES_MEDIUM_H_ */ 00084 #ifdef V7_MODULE_LINES 00085 #line 1 "v7/src/features_full.h" 00086 #endif 00087 /* 00088 * Copyright (c) 2014 Cesanta Software Limited 00089 * All rights reserved 00090 */ 00091 00092 #ifndef CS_V7_SRC_FEATURES_FULL_H_ 00093 #define CS_V7_SRC_FEATURES_FULL_H_ 00094 00095 /* Amalgamated: #include "v7/src/features_profiles.h" */ 00096 00097 #if V7_BUILD_PROFILE == V7_BUILD_PROFILE_FULL 00098 /* 00099 * DO NOT EDIT. 00100 * This file is generated by scripts/gen-features-full.pl. 00101 */ 00102 #ifndef CS_ENABLE_UTF8 00103 #define CS_ENABLE_UTF8 1 00104 #endif 00105 00106 #define V7_ENABLE__Array__reduce 1 00107 #define V7_ENABLE__Blob 1 00108 #define V7_ENABLE__Date 1 00109 #define V7_ENABLE__Date__UTC 1 00110 #define V7_ENABLE__Date__getters 1 00111 #define V7_ENABLE__Date__now 1 00112 #define V7_ENABLE__Date__parse 1 00113 #define V7_ENABLE__Date__setters 1 00114 #define V7_ENABLE__Date__toJSON 1 00115 #define V7_ENABLE__Date__toLocaleString 1 00116 #define V7_ENABLE__Date__toString 1 00117 #define V7_ENABLE__File__list 1 00118 #define V7_ENABLE__File__require 1 00119 #define V7_ENABLE__Function__bind 1 00120 #define V7_ENABLE__Function__call 1 00121 #define V7_ENABLE__Math 1 00122 #define V7_ENABLE__Math__abs 1 00123 #define V7_ENABLE__Math__acos 1 00124 #define V7_ENABLE__Math__asin 1 00125 #define V7_ENABLE__Math__atan 1 00126 #define V7_ENABLE__Math__atan2 1 00127 #define V7_ENABLE__Math__ceil 1 00128 #define V7_ENABLE__Math__constants 1 00129 #define V7_ENABLE__Math__cos 1 00130 #define V7_ENABLE__Math__exp 1 00131 #define V7_ENABLE__Math__floor 1 00132 #define V7_ENABLE__Math__log 1 00133 #define V7_ENABLE__Math__max 1 00134 #define V7_ENABLE__Math__min 1 00135 #define V7_ENABLE__Math__pow 1 00136 #define V7_ENABLE__Math__random 1 00137 #define V7_ENABLE__Math__round 1 00138 #define V7_ENABLE__Math__sin 1 00139 #define V7_ENABLE__Math__sqrt 1 00140 #define V7_ENABLE__Math__tan 1 00141 #define V7_ENABLE__Memory__stats 1 00142 #define V7_ENABLE__NUMBER__NEGATIVE_INFINITY 1 00143 #define V7_ENABLE__NUMBER__POSITIVE_INFINITY 1 00144 #define V7_ENABLE__Object__create 1 00145 #define V7_ENABLE__Object__defineProperties 1 00146 #define V7_ENABLE__Object__getOwnPropertyDescriptor 1 00147 #define V7_ENABLE__Object__getOwnPropertyNames 1 00148 #define V7_ENABLE__Object__getPrototypeOf 1 00149 #define V7_ENABLE__Object__hasOwnProperty 1 00150 #define V7_ENABLE__Object__isExtensible 1 00151 #define V7_ENABLE__Object__isFrozen 1 00152 #define V7_ENABLE__Object__isPrototypeOf 1 00153 #define V7_ENABLE__Object__isSealed 1 00154 #define V7_ENABLE__Object__keys 1 00155 #define V7_ENABLE__Object__preventExtensions 1 00156 #define V7_ENABLE__Object__propertyIsEnumerable 1 00157 #define V7_ENABLE__Proxy 1 00158 #define V7_ENABLE__RegExp 1 00159 #define V7_ENABLE__StackTrace 1 00160 #define V7_ENABLE__String__localeCompare 1 00161 #define V7_ENABLE__String__localeLowerCase 1 00162 #define V7_ENABLE__String__localeUpperCase 1 00163 00164 #endif /* V7_BUILD_PROFILE == V7_BUILD_PROFILE_FULL */ 00165 00166 #endif /* CS_V7_SRC_FEATURES_FULL_H_ */ 00167 #ifdef V7_MODULE_LINES 00168 #line 1 "v7/src/v7_features.h" 00169 #endif 00170 /* 00171 * Copyright (c) 2014 Cesanta Software Limited 00172 * All rights reserved 00173 */ 00174 00175 #ifndef CS_V7_SRC_V7_FEATURES_H_ 00176 #define CS_V7_SRC_V7_FEATURES_H_ 00177 00178 /* Only one will actually be used based on V7_BUILD_PROFILE. */ 00179 /* Amalgamated: #include "v7/src/features_minimal.h" */ 00180 /* Amalgamated: #include "v7/src/features_medium.h" */ 00181 /* Amalgamated: #include "v7/src/features_full.h" */ 00182 00183 #endif /* CS_V7_SRC_V7_FEATURES_H_ */ 00184 #ifdef V7_MODULE_LINES 00185 #line 1 "v7/src/platform.h" 00186 #endif 00187 /* 00188 * Copyright (c) 2014 Cesanta Software Limited 00189 * All rights reserved 00190 */ 00191 00192 #ifndef CS_V7_SRC_PLATFORM_H_ 00193 #define CS_V7_SRC_PLATFORM_H_ 00194 00195 #ifdef __arm 00196 #undef V7_ENABLE__Date 00197 #define V7_ENABLE__Date 0 00198 #endif 00199 00200 #endif /* CS_V7_SRC_PLATFORM_H_ */ 00201 #ifdef V7_MODULE_LINES 00202 #line 1 "v7/src/core_public.h" 00203 #endif 00204 /* 00205 * Copyright (c) 2014 Cesanta Software Limited 00206 * All rights reserved 00207 */ 00208 00209 /* 00210 * === Core 00211 */ 00212 00213 #ifndef CS_V7_SRC_CORE_PUBLIC_H_ 00214 #define CS_V7_SRC_CORE_PUBLIC_H_ 00215 00216 #ifndef _POSIX_C_SOURCE 00217 #define _POSIX_C_SOURCE 200809L 00218 #endif 00219 00220 /* Amalgamated: #include "v7/src/license.h" */ 00221 /* Amalgamated: #include "v7/src/v7_features.h" */ 00222 /* Amalgamated: #include "v7/src/platform.h" */ 00223 00224 #include <stddef.h> /* For size_t */ 00225 #include <stdio.h> /* For FILE */ 00226 00227 #if defined(__cplusplus) 00228 extern "C" { 00229 #endif /* __cplusplus */ 00230 00231 /* 00232 * TODO(dfrank) : improve amalgamation, so that we'll be able to include 00233 * files here, and include common/platform.h 00234 * 00235 * For now, copy-pasting `WARN_UNUSED_RESULT` here 00236 */ 00237 #ifdef __GNUC__ 00238 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) 00239 #define NOINSTR __attribute__((no_instrument_function)) 00240 #else 00241 #define WARN_UNUSED_RESULT 00242 #define NOINSTR 00243 #endif 00244 00245 #define V7_VERSION "1.0" 00246 00247 #if (defined(_WIN32) && !defined(__MINGW32__) && !defined(__MINGW64__)) || \ 00248 (defined(_MSC_VER) && _MSC_VER <= 1200) 00249 #define V7_WINDOWS 00250 #endif 00251 00252 #ifdef V7_WINDOWS 00253 typedef unsigned __int64 uint64_t; 00254 #else 00255 #include <inttypes.h> 00256 #endif 00257 /* 64-bit value, used to store JS values */ 00258 typedef uint64_t v7_val_t; 00259 00260 /* JavaScript `null` value */ 00261 #define V7_NULL ((uint64_t) 0xfffe << 48) 00262 00263 /* JavaScript `undefined` value */ 00264 #define V7_UNDEFINED ((uint64_t) 0xfffd << 48) 00265 00266 /* This if-0 is a dirty workaround to force etags to pick `struct v7` */ 00267 #if 0 00268 /* Opaque structure. V7 engine context. */ 00269 struct v7 { 00270 /* ... */ 00271 }; 00272 #endif 00273 00274 struct v7; 00275 00276 /* 00277 * Code which is returned by some of the v7 functions. If something other than 00278 * `V7_OK` is returned from some function, the caller function typically should 00279 * either immediately cleanup and return the code further, or handle the error. 00280 */ 00281 enum v7_err { 00282 V7_OK, 00283 V7_SYNTAX_ERROR, 00284 V7_EXEC_EXCEPTION, 00285 V7_AST_TOO_LARGE, 00286 V7_INTERNAL_ERROR, 00287 }; 00288 00289 /* JavaScript -> C call interface */ 00290 WARN_UNUSED_RESULT 00291 typedef enum v7_err(v7_cfunction_t)(struct v7 *v7, v7_val_t *res); 00292 00293 /* Create V7 instance */ 00294 struct v7 *v7_create(void); 00295 00296 /* 00297 * Customizations of initial V7 state; used by `v7_create_opt()`. 00298 */ 00299 struct v7_create_opts { 00300 size_t object_arena_size; 00301 size_t function_arena_size; 00302 size_t property_arena_size; 00303 #ifdef V7_STACK_SIZE 00304 void *c_stack_base; 00305 #endif 00306 #ifdef V7_FREEZE 00307 /* if not NULL, dump JS heap after init */ 00308 char *freeze_file; 00309 #endif 00310 }; 00311 00312 /* 00313 * Like `v7_create()`, but allows to customize initial v7 state, see `struct 00314 * v7_create_opts`. 00315 */ 00316 struct v7 *v7_create_opt(struct v7_create_opts opts); 00317 00318 /* Destroy V7 instance */ 00319 void v7_destroy(struct v7 *v7); 00320 00321 /* Return root level (`global`) object of the given V7 instance. */ 00322 v7_val_t v7_get_global(struct v7 *v); 00323 00324 /* Return current `this` object. */ 00325 v7_val_t v7_get_this(struct v7 *v); 00326 00327 /* Return current `arguments` array */ 00328 v7_val_t v7_get_arguments(struct v7 *v); 00329 00330 /* Return i-th argument */ 00331 v7_val_t v7_arg(struct v7 *v, unsigned long i); 00332 00333 /* Return the length of `arguments` */ 00334 unsigned long v7_argc(struct v7 *v7); 00335 00336 /* 00337 * Tells the GC about a JS value variable/field owned 00338 * by C code. 00339 * 00340 * User C code should own v7_val_t variables 00341 * if the value's lifetime crosses any invocation 00342 * to the v7 runtime that creates new objects or new 00343 * properties and thus can potentially trigger GC. 00344 * 00345 * The registration of the variable prevents the GC from mistakenly treat 00346 * the object as garbage. The GC might be triggered potentially 00347 * allows the GC to update pointers 00348 * 00349 * User code should also explicitly disown the variables with v7_disown once 00350 * it goes out of scope or the structure containing the v7_val_t field is freed. 00351 * 00352 * Example: 00353 * 00354 * ``` 00355 * struct v7_val cb; 00356 * v7_own(v7, &cb); 00357 * cb = v7_array_get(v7, args, 0); 00358 * // do something with cb 00359 * v7_disown(v7, &cb); 00360 * ``` 00361 */ 00362 void v7_own(struct v7 *v7, v7_val_t *v); 00363 00364 /* 00365 * Returns 1 if value is found, 0 otherwise 00366 */ 00367 int v7_disown(struct v7 *v7, v7_val_t *v); 00368 00369 /* 00370 * Enable or disable GC. 00371 * 00372 * Must be called before invoking v7_exec or v7_apply 00373 * from within a cfunction unless you know what you're doing. 00374 * 00375 * GC is disabled during execution of cfunctions in order to simplify 00376 * memory management of simple cfunctions. 00377 * However executing even small snippets of JS code causes a lot of memory 00378 * pressure. Enabling GC solves that but forces you to take care of the 00379 * reachability of your temporary V7 v7_val_t variables, as the GC needs 00380 * to know where they are since objects and strings can be either reclaimed 00381 * or relocated during a GC pass. 00382 */ 00383 void v7_set_gc_enabled(struct v7 *v7, int enabled); 00384 00385 /* 00386 * Set an optional C stack limit. 00387 * 00388 * It sets a flag that will cause the interpreter 00389 * to throw an InterruptedError. 00390 * It's safe to call it from signal handlers and ISRs 00391 * on single threaded environments. 00392 */ 00393 void v7_interrupt(struct v7 *v7); 00394 00395 /* Returns last parser error message. TODO: rename it to `v7_get_error()` */ 00396 const char *v7_get_parser_error(struct v7 *v7); 00397 00398 #if defined(V7_ENABLE_STACK_TRACKING) 00399 /* 00400 * Available if only `V7_ENABLE_STACK_TRACKING` is defined. 00401 * 00402 * Stack metric id. See `v7_stack_stat()` 00403 */ 00404 enum v7_stack_stat_what { 00405 /* max stack size consumed by `i_exec()` */ 00406 V7_STACK_STAT_EXEC, 00407 /* max stack size consumed by `parse()` (which is called from `i_exec()`) */ 00408 V7_STACK_STAT_PARSER, 00409 00410 V7_STACK_STATS_CNT 00411 }; 00412 00413 /* 00414 * Available if only `V7_ENABLE_STACK_TRACKING` is defined. 00415 * 00416 * Returns stack metric specified by the metric id `what`. See 00417 * `v7_stack_stat_clean()` 00418 */ 00419 int v7_stack_stat(struct v7 *v7, enum v7_stack_stat_what what); 00420 00421 /* 00422 * Available if only `V7_ENABLE_STACK_TRACKING` is defined. 00423 * 00424 * Clean all stack statistics gathered so far. See `v7_stack_stat()` 00425 */ 00426 void v7_stack_stat_clean(struct v7 *v7); 00427 #endif 00428 00429 #if defined(__cplusplus) 00430 } 00431 #endif /* __cplusplus */ 00432 00433 #endif /* CS_V7_SRC_CORE_PUBLIC_H_ */ 00434 #ifndef V7_EXPORT_INTERNAL_HEADERS 00435 #ifdef V7_MODULE_LINES 00436 #line 1 "v7/src/core_public.h" 00437 #endif 00438 /* 00439 * Copyright (c) 2014 Cesanta Software Limited 00440 * All rights reserved 00441 */ 00442 00443 /* 00444 * === Core 00445 */ 00446 00447 #ifndef CS_V7_SRC_CORE_PUBLIC_H_ 00448 #define CS_V7_SRC_CORE_PUBLIC_H_ 00449 00450 #ifndef _POSIX_C_SOURCE 00451 #define _POSIX_C_SOURCE 200809L 00452 #endif 00453 00454 /* Amalgamated: #include "v7/src/license.h" */ 00455 /* Amalgamated: #include "v7/src/v7_features.h" */ 00456 /* Amalgamated: #include "v7/src/platform.h" */ 00457 00458 #include <stddef.h> /* For size_t */ 00459 #include <stdio.h> /* For FILE */ 00460 00461 #if defined(__cplusplus) 00462 extern "C" { 00463 #endif /* __cplusplus */ 00464 00465 /* 00466 * TODO(dfrank) : improve amalgamation, so that we'll be able to include 00467 * files here, and include common/platform.h 00468 * 00469 * For now, copy-pasting `WARN_UNUSED_RESULT` here 00470 */ 00471 #ifdef __GNUC__ 00472 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) 00473 #define NOINSTR __attribute__((no_instrument_function)) 00474 #else 00475 #define WARN_UNUSED_RESULT 00476 #define NOINSTR 00477 #endif 00478 00479 #define V7_VERSION "1.0" 00480 00481 #if (defined(_WIN32) && !defined(__MINGW32__) && !defined(__MINGW64__)) || \ 00482 (defined(_MSC_VER) && _MSC_VER <= 1200) 00483 #define V7_WINDOWS 00484 #endif 00485 00486 #ifdef V7_WINDOWS 00487 typedef unsigned __int64 uint64_t; 00488 #else 00489 #include <inttypes.h> 00490 #endif 00491 /* 64-bit value, used to store JS values */ 00492 typedef uint64_t v7_val_t; 00493 00494 /* JavaScript `null` value */ 00495 #define V7_NULL ((uint64_t) 0xfffe << 48) 00496 00497 /* JavaScript `undefined` value */ 00498 #define V7_UNDEFINED ((uint64_t) 0xfffd << 48) 00499 00500 /* This if-0 is a dirty workaround to force etags to pick `struct v7` */ 00501 #if 0 00502 /* Opaque structure. V7 engine context. */ 00503 struct v7 { 00504 /* ... */ 00505 }; 00506 #endif 00507 00508 struct v7; 00509 00510 /* 00511 * Code which is returned by some of the v7 functions. If something other than 00512 * `V7_OK` is returned from some function, the caller function typically should 00513 * either immediately cleanup and return the code further, or handle the error. 00514 */ 00515 enum v7_err { 00516 V7_OK, 00517 V7_SYNTAX_ERROR, 00518 V7_EXEC_EXCEPTION, 00519 V7_AST_TOO_LARGE, 00520 V7_INTERNAL_ERROR, 00521 }; 00522 00523 /* JavaScript -> C call interface */ 00524 WARN_UNUSED_RESULT 00525 typedef enum v7_err(v7_cfunction_t)(struct v7 *v7, v7_val_t *res); 00526 00527 /* Create V7 instance */ 00528 struct v7 *v7_create(void); 00529 00530 /* 00531 * Customizations of initial V7 state; used by `v7_create_opt()`. 00532 */ 00533 struct v7_create_opts { 00534 size_t object_arena_size; 00535 size_t function_arena_size; 00536 size_t property_arena_size; 00537 #ifdef V7_STACK_SIZE 00538 void *c_stack_base; 00539 #endif 00540 #ifdef V7_FREEZE 00541 /* if not NULL, dump JS heap after init */ 00542 char *freeze_file; 00543 #endif 00544 }; 00545 00546 /* 00547 * Like `v7_create()`, but allows to customize initial v7 state, see `struct 00548 * v7_create_opts`. 00549 */ 00550 struct v7 *v7_create_opt(struct v7_create_opts opts); 00551 00552 /* Destroy V7 instance */ 00553 void v7_destroy(struct v7 *v7); 00554 00555 /* Return root level (`global`) object of the given V7 instance. */ 00556 v7_val_t v7_get_global(struct v7 *v); 00557 00558 /* Return current `this` object. */ 00559 v7_val_t v7_get_this(struct v7 *v); 00560 00561 /* Return current `arguments` array */ 00562 v7_val_t v7_get_arguments(struct v7 *v); 00563 00564 /* Return i-th argument */ 00565 v7_val_t v7_arg(struct v7 *v, unsigned long i); 00566 00567 /* Return the length of `arguments` */ 00568 unsigned long v7_argc(struct v7 *v7); 00569 00570 /* 00571 * Tells the GC about a JS value variable/field owned 00572 * by C code. 00573 * 00574 * User C code should own v7_val_t variables 00575 * if the value's lifetime crosses any invocation 00576 * to the v7 runtime that creates new objects or new 00577 * properties and thus can potentially trigger GC. 00578 * 00579 * The registration of the variable prevents the GC from mistakenly treat 00580 * the object as garbage. The GC might be triggered potentially 00581 * allows the GC to update pointers 00582 * 00583 * User code should also explicitly disown the variables with v7_disown once 00584 * it goes out of scope or the structure containing the v7_val_t field is freed. 00585 * 00586 * Example: 00587 * 00588 * ``` 00589 * struct v7_val cb; 00590 * v7_own(v7, &cb); 00591 * cb = v7_array_get(v7, args, 0); 00592 * // do something with cb 00593 * v7_disown(v7, &cb); 00594 * ``` 00595 */ 00596 void v7_own(struct v7 *v7, v7_val_t *v); 00597 00598 /* 00599 * Returns 1 if value is found, 0 otherwise 00600 */ 00601 int v7_disown(struct v7 *v7, v7_val_t *v); 00602 00603 /* 00604 * Enable or disable GC. 00605 * 00606 * Must be called before invoking v7_exec or v7_apply 00607 * from within a cfunction unless you know what you're doing. 00608 * 00609 * GC is disabled during execution of cfunctions in order to simplify 00610 * memory management of simple cfunctions. 00611 * However executing even small snippets of JS code causes a lot of memory 00612 * pressure. Enabling GC solves that but forces you to take care of the 00613 * reachability of your temporary V7 v7_val_t variables, as the GC needs 00614 * to know where they are since objects and strings can be either reclaimed 00615 * or relocated during a GC pass. 00616 */ 00617 void v7_set_gc_enabled(struct v7 *v7, int enabled); 00618 00619 /* 00620 * Set an optional C stack limit. 00621 * 00622 * It sets a flag that will cause the interpreter 00623 * to throw an InterruptedError. 00624 * It's safe to call it from signal handlers and ISRs 00625 * on single threaded environments. 00626 */ 00627 void v7_interrupt(struct v7 *v7); 00628 00629 /* Returns last parser error message. TODO: rename it to `v7_get_error()` */ 00630 const char *v7_get_parser_error(struct v7 *v7); 00631 00632 #if defined(V7_ENABLE_STACK_TRACKING) 00633 /* 00634 * Available if only `V7_ENABLE_STACK_TRACKING` is defined. 00635 * 00636 * Stack metric id. See `v7_stack_stat()` 00637 */ 00638 enum v7_stack_stat_what { 00639 /* max stack size consumed by `i_exec()` */ 00640 V7_STACK_STAT_EXEC, 00641 /* max stack size consumed by `parse()` (which is called from `i_exec()`) */ 00642 V7_STACK_STAT_PARSER, 00643 00644 V7_STACK_STATS_CNT 00645 }; 00646 00647 /* 00648 * Available if only `V7_ENABLE_STACK_TRACKING` is defined. 00649 * 00650 * Returns stack metric specified by the metric id `what`. See 00651 * `v7_stack_stat_clean()` 00652 */ 00653 int v7_stack_stat(struct v7 *v7, enum v7_stack_stat_what what); 00654 00655 /* 00656 * Available if only `V7_ENABLE_STACK_TRACKING` is defined. 00657 * 00658 * Clean all stack statistics gathered so far. See `v7_stack_stat()` 00659 */ 00660 void v7_stack_stat_clean(struct v7 *v7); 00661 #endif 00662 00663 #if defined(__cplusplus) 00664 } 00665 #endif /* __cplusplus */ 00666 00667 #endif /* CS_V7_SRC_CORE_PUBLIC_H_ */ 00668 #ifdef V7_MODULE_LINES 00669 #line 1 "v7/src/primitive_public.h" 00670 #endif 00671 /* 00672 * Copyright (c) 2014 Cesanta Software Limited 00673 * All rights reserved 00674 */ 00675 00676 /* 00677 * === Primitives 00678 * 00679 * All primitive values but strings. 00680 * 00681 * "foreign" values are also here, see `v7_mk_foreign()`. 00682 */ 00683 00684 #ifndef CS_V7_SRC_PRIMITIVE_PUBLIC_H_ 00685 #define CS_V7_SRC_PRIMITIVE_PUBLIC_H_ 00686 00687 /* Amalgamated: #include "v7/src/core_public.h" */ 00688 00689 #if defined(__cplusplus) 00690 extern "C" { 00691 #endif /* __cplusplus */ 00692 00693 /* Make numeric primitive value */ 00694 NOINSTR v7_val_t v7_mk_number(struct v7 *v7, double num); 00695 00696 /* 00697 * Returns number value stored in `v7_val_t` as `double`. 00698 * 00699 * Returns NaN for non-numbers. 00700 */ 00701 NOINSTR double v7_get_double(struct v7 *v7, v7_val_t v); 00702 00703 /* 00704 * Returns number value stored in `v7_val_t` as `int`. If the number value is 00705 * not an integer, the fraction part will be discarded. 00706 * 00707 * If the given value is a non-number, or NaN, the result is undefined. 00708 */ 00709 NOINSTR int v7_get_int(struct v7 *v7, v7_val_t v); 00710 00711 /* Returns true if given value is a primitive number value */ 00712 int v7_is_number(v7_val_t v); 00713 00714 /* Make boolean primitive value (either `true` or `false`) */ 00715 NOINSTR v7_val_t v7_mk_boolean(struct v7 *v7, int is_true); 00716 00717 /* 00718 * Returns boolean stored in `v7_val_t`: 00719 * 0 for `false` or non-boolean, non-0 for `true` 00720 */ 00721 NOINSTR int v7_get_bool(struct v7 *v7, v7_val_t v); 00722 00723 /* Returns true if given value is a primitive boolean value */ 00724 int v7_is_boolean(v7_val_t v); 00725 00726 /* 00727 * Make `null` primitive value. 00728 * 00729 * NOTE: this function is deprecated and will be removed in future releases. 00730 * Use `V7_NULL` instead. 00731 */ 00732 NOINSTR v7_val_t v7_mk_null(void); 00733 00734 /* Returns true if given value is a primitive `null` value */ 00735 int v7_is_null(v7_val_t v); 00736 00737 /* 00738 * Make `undefined` primitive value. 00739 * 00740 * NOTE: this function is deprecated and will be removed in future releases. 00741 * Use `V7_UNDEFINED` instead. 00742 */ 00743 NOINSTR v7_val_t v7_mk_undefined(void); 00744 00745 /* Returns true if given value is a primitive `undefined` value */ 00746 int v7_is_undefined(v7_val_t v); 00747 00748 /* 00749 * Make JavaScript value that holds C/C++ `void *` pointer. 00750 * 00751 * A foreign value is completely opaque and JS code cannot do anything useful 00752 * with it except holding it in properties and passing it around. 00753 * It behaves like a sealed object with no properties. 00754 * 00755 * NOTE: 00756 * Only valid pointers (as defined by each supported architecture) will fully 00757 * preserved. In particular, all supported 64-bit architectures (x86_64, ARM-64) 00758 * actually define a 48-bit virtual address space. 00759 * Foreign values will be sign-extended as required, i.e creating a foreign 00760 * value of something like `(void *) -1` will work as expected. This is 00761 * important because in some 64-bit OSs (e.g. Solaris) the user stack grows 00762 * downwards from the end of the address space. 00763 * 00764 * If you need to store exactly sizeof(void*) bytes of raw data where 00765 * `sizeof(void*)` >= 8, please use byte arrays instead. 00766 */ 00767 NOINSTR v7_val_t v7_mk_foreign(struct v7 *v7, void *ptr); 00768 00769 /* 00770 * Returns `void *` pointer stored in `v7_val_t`. 00771 * 00772 * Returns NULL if the value is not a foreign pointer. 00773 */ 00774 NOINSTR void *v7_get_ptr(struct v7 *v7, v7_val_t v); 00775 00776 /* Returns true if given value holds `void *` pointer */ 00777 int v7_is_foreign(v7_val_t v); 00778 00779 #if defined(__cplusplus) 00780 } 00781 #endif /* __cplusplus */ 00782 00783 #endif /* CS_V7_SRC_PRIMITIVE_PUBLIC_H_ */ 00784 #ifdef V7_MODULE_LINES 00785 #line 1 "v7/src/string_public.h" 00786 #endif 00787 /* 00788 * Copyright (c) 2014 Cesanta Software Limited 00789 * All rights reserved 00790 */ 00791 00792 /* 00793 * === Strings 00794 */ 00795 00796 #ifndef CS_V7_SRC_STRING_PUBLIC_H_ 00797 #define CS_V7_SRC_STRING_PUBLIC_H_ 00798 00799 /* Amalgamated: #include "v7/src/core_public.h" */ 00800 00801 #if defined(__cplusplus) 00802 extern "C" { 00803 #endif /* __cplusplus */ 00804 00805 /* 00806 * Creates a string primitive value. 00807 * `str` must point to the utf8 string of length `len`. 00808 * If `len` is ~0, `str` is assumed to be NUL-terminated and `strlen(str)` is 00809 * used. 00810 * 00811 * If `copy` is non-zero, the string data is copied and owned by the GC. The 00812 * caller can free the string data afterwards. Otherwise (`copy` is zero), the 00813 * caller owns the string data, and is responsible for not freeing it while it 00814 * is used. 00815 */ 00816 v7_val_t v7_mk_string(struct v7 *v7, const char *str, size_t len, int copy); 00817 00818 /* Returns true if given value is a primitive string value */ 00819 int v7_is_string(v7_val_t v); 00820 00821 /* 00822 * Returns a pointer to the string stored in `v7_val_t`. 00823 * 00824 * String length returned in `len`, which is allowed to be NULL. Returns NULL 00825 * if the value is not a string. 00826 * 00827 * JS strings can contain embedded NUL chars and may or may not be NUL 00828 * terminated. 00829 * 00830 * CAUTION: creating new JavaScript object, array, or string may kick in a 00831 * garbage collector, which in turn may relocate string data and invalidate 00832 * pointer returned by `v7_get_string()`. 00833 * 00834 * Short JS strings are embedded inside the `v7_val_t` value itself. This is why 00835 * a pointer to a `v7_val_t` is required. It also means that the string data 00836 * will become invalid once that `v7_val_t` value goes out of scope. 00837 */ 00838 const char *v7_get_string(struct v7 *v7, v7_val_t *v, size_t *len); 00839 00840 /* 00841 * Returns a pointer to the string stored in `v7_val_t`. 00842 * 00843 * Returns NULL if the value is not a string or if the string is not compatible 00844 * with a C string. 00845 * 00846 * C compatible strings contain exactly one NUL char, in terminal position. 00847 * 00848 * All strings owned by the V7 engine (see `v7_mk_string()`) are guaranteed to 00849 * be NUL terminated. Out of these, those that don't include embedded NUL chars 00850 * are guaranteed to be C compatible. 00851 */ 00852 const char *v7_get_cstring(struct v7 *v7, v7_val_t *v); 00853 00854 #if defined(__cplusplus) 00855 } 00856 #endif /* __cplusplus */ 00857 00858 #endif /* CS_V7_SRC_STRING_PUBLIC_H_ */ 00859 #ifdef V7_MODULE_LINES 00860 #line 1 "v7/src/object_public.h" 00861 #endif 00862 /* 00863 * Copyright (c) 2014 Cesanta Software Limited 00864 * All rights reserved 00865 */ 00866 00867 /* 00868 * === Objects 00869 */ 00870 00871 #ifndef CS_V7_SRC_OBJECT_PUBLIC_H_ 00872 #define CS_V7_SRC_OBJECT_PUBLIC_H_ 00873 00874 /* Amalgamated: #include "v7/src/core_public.h" */ 00875 00876 #if defined(__cplusplus) 00877 extern "C" { 00878 #endif /* __cplusplus */ 00879 00880 /* 00881 * Property attributes bitmask 00882 */ 00883 typedef unsigned short v7_prop_attr_t; 00884 #define V7_PROPERTY_NON_WRITABLE (1 << 0) 00885 #define V7_PROPERTY_NON_ENUMERABLE (1 << 1) 00886 #define V7_PROPERTY_NON_CONFIGURABLE (1 << 2) 00887 #define V7_PROPERTY_GETTER (1 << 3) 00888 #define V7_PROPERTY_SETTER (1 << 4) 00889 #define _V7_PROPERTY_HIDDEN (1 << 5) 00890 /* property not managed by V7 HEAP */ 00891 #define _V7_PROPERTY_OFF_HEAP (1 << 6) 00892 /* special property holding user data and destructor cb */ 00893 #define _V7_PROPERTY_USER_DATA_AND_DESTRUCTOR (1 << 7) 00894 /* 00895 * not a property attribute, but a flag for `v7_def()`. It's here in order to 00896 * keep all offsets in one place 00897 */ 00898 #define _V7_DESC_PRESERVE_VALUE (1 << 8) 00899 00900 #define V7_PROP_ATTR_IS_WRITABLE(a) (!(a & V7_PROPERTY_NON_WRITABLE)) 00901 #define V7_PROP_ATTR_IS_ENUMERABLE(a) (!(a & V7_PROPERTY_NON_ENUMERABLE)) 00902 #define V7_PROP_ATTR_IS_CONFIGURABLE(a) (!(a & V7_PROPERTY_NON_CONFIGURABLE)) 00903 00904 /* 00905 * Internal helpers for `V7_DESC_...` macros 00906 */ 00907 #define _V7_DESC_SHIFT 16 00908 #define _V7_DESC_MASK ((1 << _V7_DESC_SHIFT) - 1) 00909 #define _V7_MK_DESC(v, n) \ 00910 (((v7_prop_attr_desc_t)(n)) << _V7_DESC_SHIFT | ((v) ? (n) : 0)) 00911 #define _V7_MK_DESC_INV(v, n) _V7_MK_DESC(!(v), (n)) 00912 00913 /* 00914 * Property attribute descriptors that may be given to `v7_def()`: for each 00915 * attribute (`v7_prop_attr_t`), there is a corresponding macro, which takes 00916 * param: either 1 (set attribute) or 0 (clear attribute). If some particular 00917 * attribute isn't mentioned at all, it's left unchanged (or default, if the 00918 * property is being created) 00919 * 00920 * There is additional flag: `V7_DESC_PRESERVE_VALUE`. If it is set, the 00921 * property value isn't changed (or set to `undefined` if the property is being 00922 * created) 00923 */ 00924 typedef unsigned long v7_prop_attr_desc_t; 00925 #define V7_DESC_WRITABLE(v) _V7_MK_DESC_INV(v, V7_PROPERTY_NON_WRITABLE) 00926 #define V7_DESC_ENUMERABLE(v) _V7_MK_DESC_INV(v, V7_PROPERTY_NON_ENUMERABLE) 00927 #define V7_DESC_CONFIGURABLE(v) _V7_MK_DESC_INV(v, V7_PROPERTY_NON_CONFIGURABLE) 00928 #define V7_DESC_GETTER(v) _V7_MK_DESC(v, V7_PROPERTY_GETTER) 00929 #define V7_DESC_SETTER(v) _V7_MK_DESC(v, V7_PROPERTY_SETTER) 00930 #define V7_DESC_PRESERVE_VALUE _V7_DESC_PRESERVE_VALUE 00931 00932 #define _V7_DESC_HIDDEN(v) _V7_MK_DESC(v, _V7_PROPERTY_HIDDEN) 00933 #define _V7_DESC_OFF_HEAP(v) _V7_MK_DESC(v, _V7_PROPERTY_OFF_HEAP) 00934 00935 /* See `v7_set_destructor_cb` */ 00936 typedef void(v7_destructor_cb_t)(struct v7 *v7, void *ud); 00937 00938 /* Make an empty object */ 00939 v7_val_t v7_mk_object(struct v7 *v7); 00940 00941 /* 00942 * Returns true if the given value is an object or function. 00943 * i.e. it returns true if the value holds properties and can be 00944 * used as argument to `v7_get`, `v7_set` and `v7_def`. 00945 */ 00946 int v7_is_object(v7_val_t v); 00947 00948 /* Set object's prototype. Return old prototype or undefined on error. */ 00949 v7_val_t v7_set_proto(struct v7 *v7, v7_val_t obj, v7_val_t proto); 00950 00951 /* Get object's prototype. */ 00952 v7_val_t v7_get_proto(struct v7 *v7, v7_val_t obj); 00953 00954 /* 00955 * Lookup property `name` in object `obj`. If `obj` holds no such property, 00956 * an `undefined` value is returned. 00957 * 00958 * If `name_len` is ~0, `name` is assumed to be NUL-terminated and 00959 * `strlen(name)` is used. 00960 */ 00961 v7_val_t v7_get(struct v7 *v7, v7_val_t obj, const char *name, size_t name_len); 00962 00963 /* 00964 * Like `v7_get()`, but "returns" value through `res` pointer argument. 00965 * `res` must not be `NULL`. 00966 * 00967 * Caller should check the error code returned, and if it's something other 00968 * than `V7_OK`, perform cleanup and return this code further. 00969 */ 00970 WARN_UNUSED_RESULT 00971 enum v7_err v7_get_throwing(struct v7 *v7, v7_val_t obj, const char *name, 00972 size_t name_len, v7_val_t *res); 00973 00974 /* 00975 * Define object property, similar to JavaScript `Object.defineProperty()`. 00976 * 00977 * `name`, `name_len` specify property name, `val` is a property value. 00978 * `attrs_desc` is a set of flags which can affect property's attributes, 00979 * see comment of `v7_prop_attr_desc_t` for details. 00980 * 00981 * If `name_len` is ~0, `name` is assumed to be NUL-terminated and 00982 * `strlen(name)` is used. 00983 * 00984 * Returns non-zero on success, 0 on error (e.g. out of memory). 00985 * 00986 * See also `v7_set()`. 00987 */ 00988 int v7_def(struct v7 *v7, v7_val_t obj, const char *name, size_t name_len, 00989 v7_prop_attr_desc_t attrs_desc, v7_val_t v); 00990 00991 /* 00992 * Set object property. Behaves just like JavaScript assignment. 00993 * 00994 * See also `v7_def()`. 00995 */ 00996 int v7_set(struct v7 *v7, v7_val_t obj, const char *name, size_t len, 00997 v7_val_t val); 00998 00999 /* 01000 * A helper function to define object's method backed by a C function `func`. 01001 * `name` must be NUL-terminated. 01002 * 01003 * Return value is the same as for `v7_set()`. 01004 */ 01005 int v7_set_method(struct v7 *, v7_val_t obj, const char *name, 01006 v7_cfunction_t *func); 01007 01008 /* 01009 * Delete own property `name` of the object `obj`. Does not follow the 01010 * prototype chain. 01011 * 01012 * If `name_len` is ~0, `name` is assumed to be NUL-terminated and 01013 * `strlen(name)` is used. 01014 * 01015 * Returns 0 on success, -1 on error. 01016 */ 01017 int v7_del(struct v7 *v7, v7_val_t obj, const char *name, size_t name_len); 01018 01019 #if V7_ENABLE__Proxy 01020 struct prop_iter_proxy_ctx; 01021 #endif 01022 01023 /* 01024 * Context for property iteration, see `v7_next_prop()`. 01025 * 01026 * Clients should not interpret contents of this structure, it's here merely to 01027 * allow clients to allocate it not from the heap. 01028 */ 01029 struct prop_iter_ctx { 01030 #if V7_ENABLE__Proxy 01031 struct prop_iter_proxy_ctx *proxy_ctx; 01032 #endif 01033 struct v7_property *cur_prop; 01034 01035 unsigned init : 1; 01036 }; 01037 01038 /* 01039 * Initialize the property iteration context `ctx`, see `v7_next_prop()` for 01040 * usage example. 01041 */ 01042 enum v7_err v7_init_prop_iter_ctx(struct v7 *v7, v7_val_t obj, 01043 struct prop_iter_ctx *ctx); 01044 01045 /* 01046 * Destruct the property iteration context `ctx`, see `v7_next_prop()` for 01047 * usage example 01048 */ 01049 void v7_destruct_prop_iter_ctx(struct v7 *v7, struct prop_iter_ctx *ctx); 01050 01051 /* 01052 * Iterate over the `obj`'s properties. 01053 * 01054 * Usage example (here we assume we have some `v7_val_t obj`): 01055 * 01056 * struct prop_iter_ctx ctx; 01057 * v7_val_t name, val; 01058 * v7_prop_attr_t attrs; 01059 * 01060 * v7_init_prop_iter_ctx(v7, obj, &ctx); 01061 * while (v7_next_prop(v7, &ctx, &name, &val, &attrs)) { 01062 * if (V7_PROP_ATTR_IS_ENUMERABLE(attrs)) continue; 01063 * ... 01064 * } 01065 * v7_destruct_prop_iter_ctx(v7, &ctx); 01066 * 01067 * As you see, v7_next_prop will iterate through all properties, including 01068 * non-enumerable ones, and it's your responsibility to test the attributes 01069 * with the provided `V7_PROP_ATTR_*` macros and proceed as you see fit. 01070 */ 01071 int v7_next_prop(struct v7 *v7, struct prop_iter_ctx *ctx, v7_val_t *name, 01072 v7_val_t *value, v7_prop_attr_t *attrs); 01073 01074 /* Returns true if the object is an instance of a given constructor. */ 01075 int v7_is_instanceof(struct v7 *v7, v7_val_t o, const char *c); 01076 01077 /* Returns true if the object is an instance of a given constructor. */ 01078 int v7_is_instanceof_v(struct v7 *v7, v7_val_t o, v7_val_t c); 01079 01080 /* 01081 * Associates an opaque C value (anything that can be casted to a `void * ) 01082 * with an object. 01083 * 01084 * You can achieve a similar effect by just setting a special property with 01085 * a foreign value (see `v7_mk_foreign`), except user data offers the following 01086 * advantages: 01087 * 01088 * 1. You don't have to come up with some arbitrary "special" property name. 01089 * 2. JS scripts cannot access user data by mistake via property lookup. 01090 * 3. The user data is available to the destructor. When the desctructor is 01091 * invoked you cannot access any of its properties. 01092 * 4. Allows the implementation to use a more compact encoding 01093 * 01094 * Does nothing if `obj` is not a mutable object. 01095 */ 01096 void v7_set_user_data(struct v7 *v7, v7_val_t obj, void *ud); 01097 01098 /* 01099 * Get the opaque user data set with `v7_set_user_data`. 01100 * 01101 * Returns NULL if there is no user data set or if `obj` is not an object. 01102 */ 01103 void *v7_get_user_data(struct v7 *v7, v7_val_t obj); 01104 01105 /* 01106 * Register a callback which will be invoked when a given object gets 01107 * reclaimed by the garbage collector. 01108 * 01109 * The callback will be invoked while garbage collection is still in progress 01110 * and hence the internal state of the JS heap is in an undefined state. 01111 * 01112 * The only v7 API which is safe to use in this callback is `v7_disown()`, 01113 * that's why `v7` pointer is given to it. *Calls to any other v7 functions are 01114 * illegal here*. 01115 * 01116 * The intended use case is to reclaim resources allocated by C code. 01117 */ 01118 void v7_set_destructor_cb(struct v7 *v7, v7_val_t obj, v7_destructor_cb_t *d); 01119 01120 #if defined(__cplusplus) 01121 } 01122 #endif /* __cplusplus */ 01123 01124 #endif /* CS_V7_SRC_OBJECT_PUBLIC_H_ */ 01125 #ifdef V7_MODULE_LINES 01126 #line 1 "v7/src/array_public.h" 01127 #endif 01128 /* 01129 * Copyright (c) 2014 Cesanta Software Limited 01130 * All rights reserved 01131 */ 01132 01133 /* 01134 * === Arrays 01135 */ 01136 01137 #ifndef CS_V7_SRC_ARRAY_PUBLIC_H_ 01138 #define CS_V7_SRC_ARRAY_PUBLIC_H_ 01139 01140 /* Amalgamated: #include "v7/src/core_public.h" */ 01141 01142 #if defined(__cplusplus) 01143 extern "C" { 01144 #endif /* __cplusplus */ 01145 01146 /* Make an empty array object */ 01147 v7_val_t v7_mk_array(struct v7 *v7); 01148 01149 /* Returns true if given value is an array object */ 01150 int v7_is_array(struct v7 *v7, v7_val_t v); 01151 01152 /* Returns length on an array. If `arr` is not an array, 0 is returned. */ 01153 unsigned long v7_array_length(struct v7 *v7, v7_val_t arr); 01154 01155 /* Insert value `v` in array `arr` at the end of the array. */ 01156 int v7_array_push(struct v7 *, v7_val_t arr, v7_val_t v); 01157 01158 /* 01159 * Like `v7_array_push()`, but "returns" value through the `res` pointer 01160 * argument. `res` is allowed to be `NULL`. 01161 * 01162 * Caller should check the error code returned, and if it's something other 01163 * than `V7_OK`, perform cleanup and return this code further. 01164 */ 01165 WARN_UNUSED_RESULT 01166 enum v7_err v7_array_push_throwing(struct v7 *v7, v7_val_t arr, v7_val_t v, 01167 int *res); 01168 01169 /* 01170 * Return array member at index `index`. If `index` is out of bounds, undefined 01171 * is returned. 01172 */ 01173 v7_val_t v7_array_get(struct v7 *, v7_val_t arr, unsigned long index); 01174 01175 /* Insert value `v` into `arr` at index `index`. */ 01176 int v7_array_set(struct v7 *v7, v7_val_t arr, unsigned long index, v7_val_t v); 01177 01178 /* 01179 * Like `v7_array_set()`, but "returns" value through the `res` pointer 01180 * argument. `res` is allowed to be `NULL`. 01181 * 01182 * Caller should check the error code returned, and if it's something other 01183 * than `V7_OK`, perform cleanup and return this code further. 01184 */ 01185 WARN_UNUSED_RESULT 01186 enum v7_err v7_array_set_throwing(struct v7 *v7, v7_val_t arr, 01187 unsigned long index, v7_val_t v, int *res); 01188 01189 /* Delete value in array `arr` at index `index`, if it exists. */ 01190 void v7_array_del(struct v7 *v7, v7_val_t arr, unsigned long index); 01191 01192 #if defined(__cplusplus) 01193 } 01194 #endif /* __cplusplus */ 01195 01196 #endif /* CS_V7_SRC_ARRAY_PUBLIC_H_ */ 01197 #ifdef V7_MODULE_LINES 01198 #line 1 "v7/src/function_public.h" 01199 #endif 01200 /* 01201 * Copyright (c) 2014 Cesanta Software Limited 01202 * All rights reserved 01203 */ 01204 01205 /* 01206 * === Functions 01207 */ 01208 01209 #ifndef CS_V7_SRC_FUNCTION_PUBLIC_H_ 01210 #define CS_V7_SRC_FUNCTION_PUBLIC_H_ 01211 01212 /* Amalgamated: #include "v7/src/core_public.h" */ 01213 01214 #if defined(__cplusplus) 01215 extern "C" { 01216 #endif /* __cplusplus */ 01217 01218 /* 01219 * Make a JS function object backed by a cfunction. 01220 * 01221 * `func` is a C callback. 01222 * 01223 * A function object is JS object having the Function prototype that holds a 01224 * cfunction value in a hidden property. 01225 * 01226 * The function object will have a `prototype` property holding an object that 01227 * will be used as the prototype of objects created when calling the function 01228 * with the `new` operator. 01229 */ 01230 v7_val_t v7_mk_function(struct v7 *, v7_cfunction_t *func); 01231 01232 /* 01233 * Make f a JS function with specified prototype `proto`, so that the resulting 01234 * function is better suited for the usage as a constructor. 01235 */ 01236 v7_val_t v7_mk_function_with_proto(struct v7 *v7, v7_cfunction_t *f, 01237 v7_val_t proto); 01238 01239 /* 01240 * Make a JS value that holds C/C++ callback pointer. 01241 * 01242 * CAUTION: This is a low-level function value. It's not a real object and 01243 * cannot hold user defined properties. You should use `v7_mk_function` unless 01244 * you know what you're doing. 01245 */ 01246 v7_val_t v7_mk_cfunction(v7_cfunction_t *func); 01247 01248 /* 01249 * Returns true if given value is callable (i.e. it's either a JS function or 01250 * cfunction) 01251 */ 01252 int v7_is_callable(struct v7 *v7, v7_val_t v); 01253 01254 #if defined(__cplusplus) 01255 } 01256 #endif /* __cplusplus */ 01257 01258 #endif /* CS_V7_SRC_FUNCTION_PUBLIC_H_ */ 01259 #ifdef V7_MODULE_LINES 01260 #line 1 "v7/src/regexp_public.h" 01261 #endif 01262 /* 01263 * Copyright (c) 2014 Cesanta Software Limited 01264 * All rights reserved 01265 */ 01266 01267 /* 01268 * === RegExp 01269 */ 01270 01271 #ifndef CS_V7_SRC_REGEXP_PUBLIC_H_ 01272 #define CS_V7_SRC_REGEXP_PUBLIC_H_ 01273 01274 /* Amalgamated: #include "v7/src/core_public.h" */ 01275 01276 #if defined(__cplusplus) 01277 extern "C" { 01278 #endif /* __cplusplus */ 01279 01280 /* 01281 * Make RegExp object. 01282 * `regex`, `regex_len` specify a pattern, `flags` and `flags_len` specify 01283 * flags. Both utf8 encoded. For example, `regex` is `(.+)`, `flags` is `gi`. 01284 * If `regex_len` is ~0, `regex` is assumed to be NUL-terminated and 01285 * `strlen(regex)` is used. 01286 */ 01287 WARN_UNUSED_RESULT 01288 enum v7_err v7_mk_regexp(struct v7 *v7, const char *regex, size_t regex_len, 01289 const char *flags, size_t flags_len, v7_val_t *res); 01290 01291 /* Returns true if given value is a JavaScript RegExp object*/ 01292 int v7_is_regexp(struct v7 *v7, v7_val_t v); 01293 01294 #if defined(__cplusplus) 01295 } 01296 #endif /* __cplusplus */ 01297 01298 #endif /* CS_V7_SRC_REGEXP_PUBLIC_H_ */ 01299 #ifdef V7_MODULE_LINES 01300 #line 1 "v7/src/conversion_public.h" 01301 #endif 01302 /* 01303 * Copyright (c) 2014 Cesanta Software Limited 01304 * All rights reserved 01305 */ 01306 01307 /* 01308 * === Conversion 01309 */ 01310 01311 #ifndef CS_V7_SRC_CONVERSION_PUBLIC_H_ 01312 #define CS_V7_SRC_CONVERSION_PUBLIC_H_ 01313 01314 /* Amalgamated: #include "v7/src/core_public.h" */ 01315 01316 #if defined(__cplusplus) 01317 extern "C" { 01318 #endif /* __cplusplus */ 01319 01320 /* Stringify mode, see `v7_stringify()` and `v7_stringify_throwing()` */ 01321 enum v7_stringify_mode { 01322 V7_STRINGIFY_DEFAULT, 01323 V7_STRINGIFY_JSON, 01324 V7_STRINGIFY_DEBUG, 01325 }; 01326 01327 /* 01328 * Generate string representation of the JavaScript value `val` into a buffer 01329 * `buf`, `len`. If `len` is too small to hold a generated string, 01330 * `v7_stringify()` allocates required memory. In that case, it is caller's 01331 * responsibility to free the allocated buffer. Generated string is guaranteed 01332 * to be 0-terminated. 01333 * 01334 * Available stringification modes are: 01335 * 01336 * - `V7_STRINGIFY_DEFAULT`: 01337 * Convert JS value to string, using common JavaScript semantics: 01338 * - If value is an object: 01339 * - call `toString()`; 01340 * - If `toString()` returned non-primitive value, call `valueOf()`; 01341 * - If `valueOf()` returned non-primitive value, throw `TypeError`. 01342 * - Now we have a primitive, and if it's not a string, then stringify it. 01343 * 01344 * - `V7_STRINGIFY_JSON`: 01345 * Generate JSON output 01346 * 01347 * - `V7_STRINGIFY_DEBUG`: 01348 * Mostly like JSON, but will not omit non-JSON objects like functions. 01349 * 01350 * Example code: 01351 * 01352 * char buf[100], *p; 01353 * p = v7_stringify(v7, obj, buf, sizeof(buf), V7_STRINGIFY_DEFAULT); 01354 * printf("JSON string: [%s]\n", p); 01355 * if (p != buf) { 01356 * free(p); 01357 * } 01358 */ 01359 char *v7_stringify(struct v7 *v7, v7_val_t v, char *buf, size_t len, 01360 enum v7_stringify_mode mode); 01361 01362 /* 01363 * Like `v7_stringify()`, but "returns" value through the `res` pointer 01364 * argument. `res` must not be `NULL`. 01365 * 01366 * Caller should check the error code returned, and if it's something other 01367 * than `V7_OK`, perform cleanup and return this code further. 01368 */ 01369 WARN_UNUSED_RESULT 01370 enum v7_err v7_stringify_throwing(struct v7 *v7, v7_val_t v, char *buf, 01371 size_t size, enum v7_stringify_mode mode, 01372 char **res); 01373 01374 /* 01375 * A shortcut for `v7_stringify()` with `V7_STRINGIFY_JSON` 01376 */ 01377 #define v7_to_json(a, b, c, d) v7_stringify(a, b, c, d, V7_STRINGIFY_JSON) 01378 01379 /* Returns true if given value evaluates to true, as in `if (v)` statement. */ 01380 int v7_is_truthy(struct v7 *v7, v7_val_t v); 01381 01382 #if defined(__cplusplus) 01383 } 01384 #endif /* __cplusplus */ 01385 01386 #endif /* CS_V7_SRC_CONVERSION_PUBLIC_H_ */ 01387 #ifdef V7_MODULE_LINES 01388 #line 1 "v7/src/exec_public.h" 01389 #endif 01390 /* 01391 * Copyright (c) 2014 Cesanta Software Limited 01392 * All rights reserved 01393 */ 01394 01395 /* 01396 * === Execution of JavaScript code 01397 */ 01398 01399 #ifndef CS_V7_SRC_EXEC_PUBLIC_H_ 01400 #define CS_V7_SRC_EXEC_PUBLIC_H_ 01401 01402 /* Amalgamated: #include "v7/src/core_public.h" */ 01403 01404 #if defined(__cplusplus) 01405 extern "C" { 01406 #endif /* __cplusplus */ 01407 01408 /* 01409 * Execute JavaScript `js_code`. The result of evaluation is stored in 01410 * the `result` variable. 01411 * 01412 * Return: 01413 * 01414 * - V7_OK on success. `result` contains the result of execution. 01415 * - V7_SYNTAX_ERROR if `js_code` in not a valid code. `result` is undefined. 01416 * - V7_EXEC_EXCEPTION if `js_code` threw an exception. `result` stores 01417 * an exception object. 01418 * - V7_AST_TOO_LARGE if `js_code` contains an AST segment longer than 16 bit. 01419 * `result` is undefined. To avoid this error, build V7 with V7_LARGE_AST. 01420 */ 01421 WARN_UNUSED_RESULT 01422 enum v7_err v7_exec(struct v7 *v7, const char *js_code, v7_val_t *result); 01423 01424 /* 01425 * Options for `v7_exec_opt()`. To get default options, like `v7_exec()` uses, 01426 * just zero out this struct. 01427 */ 01428 struct v7_exec_opts { 01429 /* Filename, used for stack traces only */ 01430 const char *filename; 01431 01432 /* 01433 * Object to be used as `this`. Note: when it is zeroed out, i.e. it's a 01434 * number `0`, the `undefined` value is assumed. It means that it's 01435 * impossible to actually use the number `0` as `this` object, but it makes 01436 * little sense anyway. 01437 */ 01438 v7_val_t this_obj; 01439 01440 /* Whether the given `js_code` should be interpreted as JSON, not JS code */ 01441 unsigned is_json : 1; 01442 }; 01443 01444 /* 01445 * Customizable version of `v7_exec()`: allows to specify various options, see 01446 * `struct v7_exec_opts`. 01447 */ 01448 enum v7_err v7_exec_opt(struct v7 *v7, const char *js_code, 01449 const struct v7_exec_opts *opts, v7_val_t *res); 01450 01451 /* 01452 * Same as `v7_exec()`, but loads source code from `path` file. 01453 */ 01454 WARN_UNUSED_RESULT 01455 enum v7_err v7_exec_file(struct v7 *v7, const char *path, v7_val_t *result); 01456 01457 /* 01458 * Parse `str` and store corresponding JavaScript object in `res` variable. 01459 * String `str` should be '\0'-terminated. 01460 * Return value and semantic is the same as for `v7_exec()`. 01461 */ 01462 WARN_UNUSED_RESULT 01463 enum v7_err v7_parse_json(struct v7 *v7, const char *str, v7_val_t *res); 01464 01465 /* 01466 * Same as `v7_parse_json()`, but loads JSON string from `path`. 01467 */ 01468 WARN_UNUSED_RESULT 01469 enum v7_err v7_parse_json_file(struct v7 *v7, const char *path, v7_val_t *res); 01470 01471 #if !defined(V7_NO_COMPILER) 01472 01473 /* 01474 * Compile JavaScript code `js_code` into the byte code and write generated 01475 * byte code into opened file stream `fp`. If `generate_binary_output` is 0, 01476 * then generated byte code is in human-readable text format. Otherwise, it is 01477 * in the binary format, suitable for execution by V7 instance. 01478 * NOTE: `fp` must be a valid, opened, writable file stream. 01479 */ 01480 WARN_UNUSED_RESULT 01481 enum v7_err v7_compile(const char *js_code, int generate_binary_output, 01482 int use_bcode, FILE *fp); 01483 01484 #endif /* V7_NO_COMPILER */ 01485 01486 /* 01487 * Call function `func` with arguments `args`, using `this_obj` as `this`. 01488 * `args` should be an array containing arguments or `undefined`. 01489 * 01490 * `res` can be `NULL` if return value is not required. 01491 */ 01492 WARN_UNUSED_RESULT 01493 enum v7_err v7_apply(struct v7 *v7, v7_val_t func, v7_val_t this_obj, 01494 v7_val_t args, v7_val_t *res); 01495 01496 #if defined(__cplusplus) 01497 } 01498 #endif /* __cplusplus */ 01499 01500 #endif /* CS_V7_SRC_EXEC_PUBLIC_H_ */ 01501 #ifdef V7_MODULE_LINES 01502 #line 1 "v7/src/exceptions_public.h" 01503 #endif 01504 /* 01505 * Copyright (c) 2014 Cesanta Software Limited 01506 * All rights reserved 01507 */ 01508 01509 /* 01510 * === Exceptions 01511 */ 01512 01513 #ifndef CS_V7_SRC_EXCEPTIONS_PUBLIC_H_ 01514 #define CS_V7_SRC_EXCEPTIONS_PUBLIC_H_ 01515 01516 /* Amalgamated: #include "v7/src/core_public.h" */ 01517 01518 #if defined(__cplusplus) 01519 extern "C" { 01520 #endif /* __cplusplus */ 01521 01522 /* Throw an exception with an already existing value. */ 01523 WARN_UNUSED_RESULT 01524 enum v7_err v7_throw(struct v7 *v7, v7_val_t v); 01525 01526 /* 01527 * Throw an exception with given formatted message. 01528 * 01529 * Pass "Error" as typ for a generic error. 01530 */ 01531 WARN_UNUSED_RESULT 01532 enum v7_err v7_throwf(struct v7 *v7, const char *typ, const char *err_fmt, ...); 01533 01534 /* 01535 * Rethrow the currently thrown object. In fact, it just returns 01536 * V7_EXEC_EXCEPTION. 01537 */ 01538 WARN_UNUSED_RESULT 01539 enum v7_err v7_rethrow(struct v7 *v7); 01540 01541 /* 01542 * Returns the value that is being thrown at the moment, or `undefined` if 01543 * nothing is being thrown. If `is_thrown` is not `NULL`, it will be set 01544 * to either 0 or 1, depending on whether something is thrown at the moment. 01545 */ 01546 v7_val_t v7_get_thrown_value(struct v7 *v7, unsigned char *is_thrown); 01547 01548 /* Clears currently thrown value, if any. */ 01549 void v7_clear_thrown_value(struct v7 *v7); 01550 01551 #if defined(__cplusplus) 01552 } 01553 #endif /* __cplusplus */ 01554 01555 #endif /* CS_V7_SRC_EXCEPTIONS_PUBLIC_H_ */ 01556 #ifdef V7_MODULE_LINES 01557 #line 1 "v7/src/gc_public.h" 01558 #endif 01559 /* 01560 * Copyright (c) 2014 Cesanta Software Limited 01561 * All rights reserved 01562 */ 01563 01564 /* 01565 * === Garbage Collector 01566 */ 01567 01568 #ifndef CS_V7_SRC_GC_PUBLIC_H_ 01569 #define CS_V7_SRC_GC_PUBLIC_H_ 01570 01571 /* Amalgamated: #include "v7/src/core_public.h" */ 01572 01573 #if defined(__cplusplus) 01574 extern "C" { 01575 #endif /* __cplusplus */ 01576 01577 #if V7_ENABLE__Memory__stats 01578 01579 /* Heap metric id, see `v7_heap_stat()` */ 01580 enum v7_heap_stat_what { 01581 V7_HEAP_STAT_HEAP_SIZE, 01582 V7_HEAP_STAT_HEAP_USED, 01583 V7_HEAP_STAT_STRING_HEAP_RESERVED, 01584 V7_HEAP_STAT_STRING_HEAP_USED, 01585 V7_HEAP_STAT_OBJ_HEAP_MAX, 01586 V7_HEAP_STAT_OBJ_HEAP_FREE, 01587 V7_HEAP_STAT_OBJ_HEAP_CELL_SIZE, 01588 V7_HEAP_STAT_FUNC_HEAP_MAX, 01589 V7_HEAP_STAT_FUNC_HEAP_FREE, 01590 V7_HEAP_STAT_FUNC_HEAP_CELL_SIZE, 01591 V7_HEAP_STAT_PROP_HEAP_MAX, 01592 V7_HEAP_STAT_PROP_HEAP_FREE, 01593 V7_HEAP_STAT_PROP_HEAP_CELL_SIZE, 01594 V7_HEAP_STAT_FUNC_AST_SIZE, 01595 V7_HEAP_STAT_BCODE_OPS_SIZE, 01596 V7_HEAP_STAT_BCODE_LIT_TOTAL_SIZE, 01597 V7_HEAP_STAT_BCODE_LIT_DESER_SIZE, 01598 V7_HEAP_STAT_FUNC_OWNED, 01599 V7_HEAP_STAT_FUNC_OWNED_MAX 01600 }; 01601 01602 /* Returns a given heap statistics */ 01603 int v7_heap_stat(struct v7 *v7, enum v7_heap_stat_what what); 01604 #endif 01605 01606 /* 01607 * Perform garbage collection. 01608 * Pass true to full in order to reclaim unused heap back to the OS. 01609 */ 01610 void v7_gc(struct v7 *v7, int full); 01611 01612 #if defined(__cplusplus) 01613 } 01614 #endif /* __cplusplus */ 01615 01616 #endif /* CS_V7_SRC_GC_PUBLIC_H_ */ 01617 #ifdef V7_MODULE_LINES 01618 #line 1 "v7/src/util_public.h" 01619 #endif 01620 /* 01621 * Copyright (c) 2014 Cesanta Software Limited 01622 * All rights reserved 01623 */ 01624 01625 /* 01626 * === Utility functions 01627 */ 01628 01629 #ifndef CS_V7_SRC_UTIL_PUBLIC_H_ 01630 #define CS_V7_SRC_UTIL_PUBLIC_H_ 01631 01632 /* Amalgamated: #include "v7/src/core_public.h" */ 01633 01634 #if defined(__cplusplus) 01635 extern "C" { 01636 #endif /* __cplusplus */ 01637 01638 /* Output a string representation of the value to stdout. 01639 * V7_STRINGIFY_DEBUG mode is used. */ 01640 void v7_print(struct v7 *v7, v7_val_t v); 01641 01642 /* Output a string representation of the value to stdout followed by a newline. 01643 * V7_STRINGIFY_DEBUG mode is used. */ 01644 void v7_println(struct v7 *v7, v7_val_t v); 01645 01646 /* Output a string representation of the value to a file. 01647 * V7_STRINGIFY_DEBUG mode is used. */ 01648 void v7_fprint(FILE *f, struct v7 *v7, v7_val_t v); 01649 01650 /* Output a string representation of the value to a file followed by a newline. 01651 * V7_STRINGIFY_DEBUG mode is used. */ 01652 void v7_fprintln(FILE *f, struct v7 *v7, v7_val_t v); 01653 01654 /* Output stack trace recorded in the exception `e` to file `f` */ 01655 void v7_fprint_stack_trace(FILE *f, struct v7 *v7, v7_val_t e); 01656 01657 /* Output error object message and possibly stack trace to f */ 01658 void v7_print_error(FILE *f, struct v7 *v7, const char *ctx, v7_val_t e); 01659 01660 #if V7_ENABLE__Proxy 01661 01662 struct v7_property; 01663 01664 /* 01665 * C callback, analogue of JS callback `getOwnPropertyDescriptor()`. 01666 * Callbacks of this type are used for C API only, see `m7_mk_proxy()`. 01667 * 01668 * `name` is the name of the property, and the function should fill `attrs` and 01669 * `value` with the property data. Before this callback is called, `attrs` is 01670 * set to 0, and `value` is `V7_UNDEFINED`. 01671 * 01672 * It should return non-zero if the property should be considered existing, or 01673 * zero otherwise. 01674 * 01675 * You can inspect the property attributes with the `V7_PROP_ATTR_IS_*` macros. 01676 */ 01677 typedef int(v7_get_own_prop_desc_cb_t)(struct v7 *v7, v7_val_t target, 01678 v7_val_t name, v7_prop_attr_t *attrs, 01679 v7_val_t *value); 01680 01681 /* Handler for `v7_mk_proxy()`; each item is a cfunction */ 01682 typedef struct { 01683 v7_cfunction_t *get; 01684 v7_cfunction_t *set; 01685 v7_cfunction_t *own_keys; 01686 v7_get_own_prop_desc_cb_t *get_own_prop_desc; 01687 } v7_proxy_hnd_t; 01688 01689 /* 01690 * Create a Proxy object, see: 01691 * https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy 01692 * 01693 * Only two traps are implemented so far: `get()` and `set()`. Note that 01694 * `Object.defineProperty()` bypasses the `set()` trap. 01695 * 01696 * If `target` is not an object, the empty object will be used, so it's safe 01697 * to pass `V7_UNDEFINED` as `target`. 01698 */ 01699 v7_val_t v7_mk_proxy(struct v7 *v7, v7_val_t target, 01700 const v7_proxy_hnd_t *handler); 01701 01702 #endif /* V7_ENABLE__Proxy */ 01703 01704 #if defined(__cplusplus) 01705 } 01706 #endif /* __cplusplus */ 01707 01708 #endif /* CS_V7_SRC_UTIL_PUBLIC_H_ */ 01709 #ifdef V7_MODULE_LINES 01710 #line 1 "v7/src/main_public.h" 01711 #endif 01712 /* 01713 * Copyright (c) 2014 Cesanta Software Limited 01714 * All rights reserved 01715 */ 01716 01717 /* 01718 * === v7 main() 01719 */ 01720 01721 #ifndef CS_V7_SRC_MAIN_PUBLIC_H_ 01722 #define CS_V7_SRC_MAIN_PUBLIC_H_ 01723 01724 /* Amalgamated: #include "v7/src/core_public.h" */ 01725 01726 #if defined(__cplusplus) 01727 extern "C" { 01728 #endif /* __cplusplus */ 01729 01730 /* 01731 * V7 executable main function. 01732 * 01733 * There are various callbacks available: 01734 * 01735 * `pre_freeze_init()` and `pre_init()` are optional intialization functions, 01736 * aimed to export any extra functionality into vanilla v7 engine. They are 01737 * called after v7 initialization, before executing given files or inline 01738 * expressions. `pre_freeze_init()` is called before "freezing" v7 state; 01739 * whereas `pre_init` called afterwards. 01740 * 01741 * `post_init()`, if provided, is called after executing files and expressions, 01742 * before destroying v7 instance and exiting. 01743 */ 01744 int v7_main(int argc, char *argv[], void (*pre_freeze_init)(struct v7 *), 01745 void (*pre_init)(struct v7 *), void (*post_init)(struct v7 *)); 01746 01747 #if defined(__cplusplus) 01748 } 01749 #endif /* __cplusplus */ 01750 01751 #endif /* CS_V7_SRC_MAIN_PUBLIC_H_ */ 01752 #endif /* V7_EXPORT_INTERNAL_HEADERS */
Generated on Thu Jul 14 2022 12:18:29 by
1.7.2