2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-07-06 03:19:19 -04:00
|
|
|
#ifndef DENO_H_
|
|
|
|
#define DENO_H_
|
2019-04-28 15:31:10 -04:00
|
|
|
|
2018-07-06 15:00:45 -04:00
|
|
|
#include <stddef.h>
|
2018-07-08 21:35:34 -04:00
|
|
|
#include <stdint.h>
|
2019-04-28 15:31:10 -04:00
|
|
|
|
|
|
|
#include "buffer.h"
|
|
|
|
|
2018-06-10 08:18:15 -04:00
|
|
|
// Neither Rust nor Go support calling directly into C++ functions, therefore
|
|
|
|
// the public interface to libdeno is done in C.
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2018-06-09 20:24:34 -04:00
|
|
|
|
2019-04-28 15:31:10 -04:00
|
|
|
typedef deno::PinnedBuf::Raw deno_pinned_buf;
|
|
|
|
|
2018-06-09 18:32:04 -04:00
|
|
|
// Data that gets transmitted.
|
2018-06-10 08:18:15 -04:00
|
|
|
typedef struct {
|
2019-05-01 18:18:18 -04:00
|
|
|
uint8_t* data_ptr;
|
|
|
|
size_t data_len;
|
2018-06-10 08:18:15 -04:00
|
|
|
} deno_buf;
|
2018-06-09 21:44:56 -04:00
|
|
|
|
2019-04-08 10:12:43 -04:00
|
|
|
typedef struct {
|
|
|
|
uint8_t* data_ptr;
|
|
|
|
size_t data_len;
|
|
|
|
} deno_snapshot;
|
|
|
|
|
2018-06-09 18:32:04 -04:00
|
|
|
typedef struct deno_s Deno;
|
2018-06-09 21:44:56 -04:00
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
// A callback to receive a message from a libdeno.send() javascript call.
|
|
|
|
// control_buf is valid for only for the lifetime of this callback.
|
|
|
|
// data_buf is valid until deno_respond() is called.
|
2019-03-30 14:45:36 -04:00
|
|
|
typedef void (*deno_recv_cb)(void* user_data, deno_buf control_buf,
|
2019-04-28 15:31:10 -04:00
|
|
|
deno_pinned_buf zero_copy_buf);
|
2018-06-09 18:32:04 -04:00
|
|
|
|
2019-06-06 19:07:47 -04:00
|
|
|
typedef int deno_dyn_import_id;
|
|
|
|
// Called when dynamic import is called in JS: import('foo')
|
|
|
|
// Embedder must call deno_dyn_import() with the specified id and
|
|
|
|
// the module.
|
|
|
|
typedef void (*deno_dyn_import_cb)(void* user_data, const char* specifier,
|
|
|
|
const char* referrer, deno_dyn_import_id id);
|
|
|
|
|
2018-06-10 08:18:15 -04:00
|
|
|
void deno_init();
|
2018-06-10 08:34:59 -04:00
|
|
|
const char* deno_v8_version();
|
2018-09-22 08:47:44 -04:00
|
|
|
void deno_set_v8_flags(int* argc, char** argv);
|
2018-06-09 18:32:04 -04:00
|
|
|
|
2018-12-04 18:06:20 -05:00
|
|
|
typedef struct {
|
2019-04-08 10:12:43 -04:00
|
|
|
int will_snapshot; // Default 0. If calling deno_snapshot_new 1.
|
|
|
|
deno_snapshot load_snapshot; // A startup snapshot to use.
|
|
|
|
deno_buf shared; // Shared buffer to be mapped to libdeno.shared
|
|
|
|
deno_recv_cb recv_cb; // Maps to libdeno.send() calls.
|
2019-06-06 19:07:47 -04:00
|
|
|
deno_dyn_import_cb dyn_import_cb;
|
2018-12-04 18:06:20 -05:00
|
|
|
} deno_config;
|
|
|
|
|
2018-12-17 20:05:18 -05:00
|
|
|
// Create a new deno isolate.
|
2019-04-08 10:12:43 -04:00
|
|
|
// Warning: If config.will_snapshot is set, deno_snapshot_new() must be called
|
2018-12-17 20:05:18 -05:00
|
|
|
// or an error will result.
|
2018-12-13 16:25:42 -05:00
|
|
|
Deno* deno_new(deno_config config);
|
2019-04-08 10:12:43 -04:00
|
|
|
void deno_delete(Deno* d);
|
2018-10-24 01:12:13 -04:00
|
|
|
|
2019-04-08 10:12:43 -04:00
|
|
|
// Generate a snapshot. The resulting buf can be used in as the load_snapshot
|
|
|
|
// member in deno_confg.
|
|
|
|
// When calling this function, the caller must have created the isolate "d" with
|
|
|
|
// "will_snapshot" set to 1.
|
|
|
|
// The caller must free the returned data with deno_snapshot_delete().
|
|
|
|
deno_snapshot deno_snapshot_new(Deno* d);
|
2018-10-24 01:12:13 -04:00
|
|
|
|
2019-04-08 10:12:43 -04:00
|
|
|
// Only for use with data returned from deno_snapshot_new.
|
|
|
|
void deno_snapshot_delete(deno_snapshot);
|
2018-06-09 18:32:04 -04:00
|
|
|
|
2019-02-26 17:36:05 -05:00
|
|
|
void deno_lock(Deno* d);
|
|
|
|
void deno_unlock(Deno* d);
|
|
|
|
|
2018-11-29 15:40:04 -05:00
|
|
|
// Compile and execute a traditional JavaScript script that does not use
|
|
|
|
// module import statements.
|
2019-02-01 19:46:54 -05:00
|
|
|
// If it succeeded deno_last_exception() will return NULL.
|
|
|
|
void deno_execute(Deno* d, void* user_data, const char* js_filename,
|
|
|
|
const char* js_source);
|
2018-06-09 18:32:04 -04:00
|
|
|
|
2019-03-30 14:45:36 -04:00
|
|
|
// deno_respond sends up to one message back for every deno_recv_cb made.
|
2018-09-27 17:33:10 -04:00
|
|
|
//
|
2019-03-30 14:45:36 -04:00
|
|
|
// If this is called during deno_recv_cb, the issuing libdeno.send() in
|
2019-06-13 23:43:54 -04:00
|
|
|
// javascript will synchronously return the specified promise_id(number)
|
|
|
|
// or buf(Uint8Array) (or null if buf and promise_id are both null/empty).
|
|
|
|
// Calling with non-null for both buf and promise_id will result in the
|
|
|
|
// promise_id being returned.
|
2018-09-27 17:33:10 -04:00
|
|
|
//
|
2019-03-30 14:45:36 -04:00
|
|
|
// If this is called after deno_recv_cb has returned, the deno_respond
|
2018-09-27 17:33:10 -04:00
|
|
|
// will call into the JS callback specified by libdeno.recv().
|
|
|
|
//
|
|
|
|
// (Ideally, but not currently: After calling deno_respond(), the caller no
|
|
|
|
// longer owns `buf` and must not use it; deno_respond() is responsible for
|
|
|
|
// releasing its memory.)
|
|
|
|
//
|
2019-02-01 19:46:54 -05:00
|
|
|
// If a JS exception was encountered, deno_last_exception() will be non-NULL.
|
2019-06-13 23:43:54 -04:00
|
|
|
void deno_respond(Deno* d, void* user_data, deno_buf buf, int* promise_id);
|
2019-02-26 17:36:05 -05:00
|
|
|
|
|
|
|
// consumes zero_copy
|
2019-04-28 15:31:10 -04:00
|
|
|
void deno_pinned_buf_delete(deno_pinned_buf* buf);
|
2018-06-13 13:38:22 -04:00
|
|
|
|
2018-10-12 14:22:52 -04:00
|
|
|
void deno_check_promise_errors(Deno* d);
|
|
|
|
|
2018-06-09 18:32:04 -04:00
|
|
|
const char* deno_last_exception(Deno* d);
|
|
|
|
|
|
|
|
void deno_terminate_execution(Deno* d);
|
|
|
|
|
2019-01-30 17:21:31 -05:00
|
|
|
// Module API
|
|
|
|
|
|
|
|
typedef int deno_mod;
|
|
|
|
|
|
|
|
// Returns zero on error - check deno_last_exception().
|
2019-02-26 13:29:45 -05:00
|
|
|
deno_mod deno_mod_new(Deno* d, bool main, const char* name, const char* source);
|
2019-01-30 17:21:31 -05:00
|
|
|
|
|
|
|
size_t deno_mod_imports_len(Deno* d, deno_mod id);
|
|
|
|
|
|
|
|
// Returned pointer is valid for the lifetime of the Deno isolate "d".
|
|
|
|
const char* deno_mod_imports_get(Deno* d, deno_mod id, size_t index);
|
|
|
|
|
|
|
|
typedef deno_mod (*deno_resolve_cb)(void* user_data, const char* specifier,
|
|
|
|
deno_mod referrer);
|
|
|
|
|
2019-02-01 19:46:54 -05:00
|
|
|
// If it succeeded deno_last_exception() will return NULL.
|
2019-01-30 17:21:31 -05:00
|
|
|
void deno_mod_instantiate(Deno* d, void* user_data, deno_mod id,
|
|
|
|
deno_resolve_cb cb);
|
|
|
|
|
2019-02-01 19:46:54 -05:00
|
|
|
// If it succeeded deno_last_exception() will return NULL.
|
2019-01-30 17:21:31 -05:00
|
|
|
void deno_mod_evaluate(Deno* d, void* user_data, deno_mod id);
|
|
|
|
|
2019-06-06 19:07:47 -04:00
|
|
|
// Call exactly once for every deno_dyn_import_cb.
|
2019-06-10 15:27:34 -04:00
|
|
|
// Note this call will execute JS.
|
|
|
|
void deno_dyn_import(Deno* d, void* user_data, deno_dyn_import_id id,
|
|
|
|
deno_mod mod_id);
|
2019-06-06 19:07:47 -04:00
|
|
|
|
2018-06-10 08:18:15 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
2018-07-06 03:19:19 -04:00
|
|
|
#endif // DENO_H_
|