Skip to content

Commit

Permalink
Expose a non nul-terminated exec
Browse files Browse the repository at this point in the history
Requested in #576

PUBLISHED_FROM=f950c3ef6db3d69a54e1588544f6f84570584a66
  • Loading branch information
Marko Mikulicic authored and cesantabot committed Oct 26, 2016
1 parent 3d4c9df commit ae9d3dc
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/c-api/exec_public.h/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ decl_name: "exec_public.h"
items:
- { name: v7_exec.md }
- { name: v7_exec_opt.md }
- { name: v7_exec_buf.md }
- { name: v7_exec_file.md }
- { name: v7_parse_json.md }
- { name: v7_parse_json_file.md }
Expand Down
1 change: 1 addition & 0 deletions docs/c-api/exec_public.h/v7_exec.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ signature: |

Execute JavaScript `js_code`. The result of evaluation is stored in
the `result` variable.
The code can be either a JavaScript source or a precompiled bytecode.

Return:

Expand Down
14 changes: 14 additions & 0 deletions docs/c-api/exec_public.h/v7_exec_buf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: "v7_exec_buf()"
decl_name: "v7_exec_buf"
symbol_kind: "func"
signature: |
enum v7_err v7_exec_buf(struct v7 *v7, const char *js_code, size_t len,
v7_val_t *result);
---

Like v7_exec but it expects an explicit length instead of treating the code
as a null terminated string.

The code can be either a JS source or a precompiled bytecode.

37 changes: 36 additions & 1 deletion v7.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,24 @@ typedef struct _stati64 cs_stat_t;
#include <sys/types.h>
#include <unistd.h>

#ifdef __APPLE__
#include <machine/endian.h>
#ifndef BYTE_ORDER
#define LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
#define BIG_ENDIAN __DARWIN_BIG_ENDIAN
#define PDP_ENDIAN __DARWIN_PDP_ENDIAN
#define BYTE_ORDER __DARWIN_BYTE_ORDER
#endif
#endif

/*
* osx correctly avoids defining strtoll when compiling in strict ansi mode.
* c++ 11 standard defines strtoll as well.
* We require strtoll, and if your embedded pre-c99 compiler lacks one, please
* implement a shim.
*/
#if !(defined(__DARWIN_C_LEVEL) && __DARWIN_C_LEVEL >= 200809L)
#if !(defined(__cplusplus) && __cplusplus >= 201103L) && \
!(defined(__DARWIN_C_LEVEL) && __DARWIN_C_LEVEL >= 200809L)
long long strtoll(const char *, char **, int);
#endif

Expand Down Expand Up @@ -871,6 +883,10 @@ struct timeval {

#if MG_NET_IF == MG_NET_IF_SIMPLELINK

#define MG_SIMPLELINK_NO_OSI 1

#include <simplelink.h>

typedef int sock_t;
#define INVALID_SOCKET (-1)

Expand Down Expand Up @@ -908,6 +924,8 @@ int inet_pton(int af, const char *src, void *dst);
#include <string.h>
#include <time.h>

#define to64(x) strtoll(x, NULL, 10)

#define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL
#define LWIP_TIMEVAL_PRIVATE 0
#define LWIP_PROVIDE_ERRNO 1
Expand Down Expand Up @@ -5383,6 +5401,7 @@ extern "C" {
/*
* Execute JavaScript `js_code`. The result of evaluation is stored in
* the `result` variable.
* The code can be either a JavaScript source or a precompiled bytecode.
*
* Return:
*
Expand Down Expand Up @@ -5423,6 +5442,16 @@ struct v7_exec_opts {
enum v7_err v7_exec_opt(struct v7 *v7, const char *js_code,
const struct v7_exec_opts *opts, v7_val_t *res);

/*
* Like v7_exec but it expects an explicit length instead of treating the code
* as a null terminated string.
*
* The code can be either a JS source or a precompiled bytecode.
*/
WARN_UNUSED_RESULT
enum v7_err v7_exec_buf(struct v7 *v7, const char *js_code, size_t len,
v7_val_t *result);

/*
* Same as `v7_exec()`, but loads source code from `path` file.
*/
Expand Down Expand Up @@ -17579,6 +17608,12 @@ enum v7_err v7_exec_opt(struct v7 *v7, const char *js_code,
opts->is_json, 0, 0, res);
}

enum v7_err v7_exec_buf(struct v7 *v7, const char *js_code, size_t len,
v7_val_t *res) {
return b_exec(v7, js_code, len, NULL, V7_UNDEFINED, V7_UNDEFINED,
V7_UNDEFINED, 0, 0, 0, res);
}

enum v7_err v7_parse_json(struct v7 *v7, const char *str, v7_val_t *res) {
return b_exec(v7, str, strlen(str), NULL, V7_UNDEFINED, V7_UNDEFINED,
V7_UNDEFINED, 1, 0, 0, res);
Expand Down
11 changes: 11 additions & 0 deletions v7.h
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,7 @@ extern "C" {
/*
* Execute JavaScript `js_code`. The result of evaluation is stored in
* the `result` variable.
* The code can be either a JavaScript source or a precompiled bytecode.
*
* Return:
*
Expand Down Expand Up @@ -1771,6 +1772,16 @@ struct v7_exec_opts {
enum v7_err v7_exec_opt(struct v7 *v7, const char *js_code,
const struct v7_exec_opts *opts, v7_val_t *res);

/*
* Like v7_exec but it expects an explicit length instead of treating the code
* as a null terminated string.
*
* The code can be either a JS source or a precompiled bytecode.
*/
WARN_UNUSED_RESULT
enum v7_err v7_exec_buf(struct v7 *v7, const char *js_code, size_t len,
v7_val_t *result);

/*
* Same as `v7_exec()`, but loads source code from `path` file.
*/
Expand Down

0 comments on commit ae9d3dc

Please sign in to comment.