⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsint.h

📁 一个类似windows
💻 H
📖 第 1 页 / 共 4 页
字号:

/* Node. */
struct js_node_st
{
  JSNodeType type;

  union
  {
    unsigned int vboolean;

    JSString *vstring;

    long vinteger;
    double vfloat;

    struct js_object_st *vobject;

    JSArray *varray;

    /* Internal values. */

    JSSymbol vsymbol;

    JSBuiltin *vbuiltin;

    JSFunction *vfunction;

    void *iptr;

    struct
    {
      JSUInt32 argc;
      JSUInt32 delta;
    } args_fix;

    struct
    {
      JSUInt32 a;
      JSUInt32 b;
    } copy;
  } u;
};

typedef struct js_node_st JSNode;


/* Object. */

/* Hash node for object's properties. */
struct js_object_prop_hash_bucket_st
{
  struct js_object_prop_hash_bucket_st *next;
  unsigned char *data;
  unsigned int len;
  unsigned int value;
};

typedef struct js_object_prop_hash_bucket_st JSObjectPropHashBucket;

/* The attribute flags for object's properties. */
#define JS_ATTRIB_READONLY		1
#define JS_ATTRIB_DONTENUM		2
#define JS_ATTRIB_DONTDELETE		4
#define JS_ATTRIB_Internal		8

/* Object's property. */
struct js_property_st
{
  JSSymbol name;
  JSNode value;
  unsigned int attributes;
};

typedef struct js_property_st JSProperty;

struct js_object_st
{
  JSObjectPropHashBucket **hash;
  unsigned int *hash_lengths;
  unsigned int num_props;	/* Number of properties in this object. */
  JSProperty *props;
};

typedef struct js_object_st JSObject;


/* Byte code. */

typedef enum
{
  JS_BCST_CODE = 0,
  JS_BCST_CONSTANTS = 1,
  JS_BCST_SYMTAB = 2,
  JS_BCST_DEBUG = 3
} JSBCSectionType;

struct js_bc_sect_st
{
  JSBCSectionType type;
  unsigned int length;
  void *data;			/* <length> bytes of data */
};

typedef struct js_bc_sect_st JSBCSect;

struct js_bc_st
{
  unsigned int num_sects;
  JSBCSect *sects;
};

typedef struct js_bc_st JSByteCode;

/* Debug information. */
#define JS_DI_FILENAME		1
#define JS_DI_LINENUMBER 	2

/* Heap block. */
struct js_heap_block_st
{
  struct js_heap_block_st *next;
  unsigned int size;
  /* <size> bytes of data follows the structure. */
};

typedef struct js_heap_block_st JSHeapBlock;

/* Heap memory block. */

#define JS_MEM_DEBUG 0

/* All allocated blocks have this header. */
struct js_heap_memory_block_st
{
#if JS_MEM_DEBUG
  JSUIntAlign magic;
#endif

  JSUIntAlign flag_mark : 1;
  JSUIntAlign flag_destroyable : 1;
  JSUIntAlign size : (sizeof (JSUIntAlign) * 8 - 2);
  /* <size> bytes of data follows this header. */
};

typedef struct js_heap_memory_block_st JSHeapMemoryBlock;

/*
 * When the block is on the freelist, it has this header.  The first
 * sizeof (void *) bytes of the block's data is used to hold the
 * freelist next pointer.
 */
struct js_heap_freelist_block_st
{
  JSHeapMemoryBlock block;
  JSHeapMemoryBlock *next;
};

typedef struct js_heap_freelist_block_st JSHeapFreelistBlock;


/* Parsed symbol table entry. */
struct js_symtab_entry_st
{
  char *name;
  unsigned int offset;
};

typedef struct js_symtab_entry_st JSSymtabEntry;


/*
 * Entry points to different byte-code instruction dispatcher functions.
 * Each dispatcher must implement these.
 */

typedef int (*JSVMExecute) (struct js_vm_st *vm, JSByteCode *bc,
			    JSSymtabEntry *symtab,
			    unsigned int num_symtab_entries,
			    unsigned int consts_offset,
			    unsigned int anonymous_function_offset,
			    unsigned char *debug_info,
			    unsigned int debug_info_len,
			    JSNode *object, JSNode *func,
			    unsigned int argc, JSNode *argv);

typedef const char *(*JSVMFuncName) (struct js_vm_st *vm, void *pc);

typedef const char *(*JSVMDebugPosition) (struct js_vm_st *vm,
					  unsigned int *linenum_return);


/* Virtual Machine. */

#define JS_HASH_TABLE_SIZE 256

struct js_hash_bucket_st
{
  struct js_hash_bucket_st *next;
  char *name;
  union
  {
    void *data;
    unsigned int ui;
  } u;
};

typedef struct js_hash_bucket_st JSHashBucket;

/* Error handler frame. */
struct js_error_handler_frame_st
{
  struct js_error_handler_frame_st *next;
  jmp_buf error_jmp;

  /* The value thrown by the throw operand. */
  JSNode thrown;

  /* Saved state for the `try_push' operand. */
  JSNode *sp;
  JSNode *fp;
  void *pc;
  JSInt32 pc_delta;
};

typedef struct js_error_handler_frame_st JSErrorHandlerFrame;

struct js_vm_st
{
  /* Options for the virtual machine. */
  unsigned int verbose;		/* verbosity has different levels. */

  unsigned int stacktrace_on_error : 1;
  unsigned int verbose_stacktrace : 1;
  unsigned int warn_undef : 1;

  /* Security flags. */
  unsigned long security;

  /* The default system streams. */
  JSIOStream *s_stdin;
  JSIOStream *s_stdout;
  JSIOStream *s_stderr;

  /* The byte-code instruction dispatcher. */
  JSVMDispatchMethod	dispatch_method;
  const char 	       *dispatch_method_name;
  JSVMExecute 		dispatch_execute;
  JSVMFuncName		dispatch_func_name;
  JSVMDebugPosition	dispatch_debug_position;

  /* Constants pool. */
  JSNode *consts;
  unsigned int num_consts;
  unsigned int consts_alloc;

  /*
   * Global symbols (both functions and variables).  <globals_hash> is
   * a name-to-index mapping between symbol names and their positions
   * in <globals>.
   */
  JSHashBucket *globals_hash[JS_HASH_TABLE_SIZE];
  JSNode *globals;
  unsigned int num_globals;
  unsigned int globals_alloc;

  /* The next anonymous function id. */
  unsigned int anonymous_function_next_id;

  /* Stack. */
  JSNode *stack;
  unsigned int stack_size;
  JSNode *sp;			/* Fuzzy stack pointer. */

  void *pc;			/* Fuzzy program counter. */

  /* Builtin objects for the primitive datatypes. */
  JSBuiltinInfo *prim[JS_IPTR + 1];

  /* Some commonly used symbols. */
  struct
  {
    JSSymbol s___proto__;
    JSSymbol s_prototype;
    JSSymbol s_toSource;
    JSSymbol s_toString;
    JSSymbol s_valueOf;
  } syms;

  /* Heap. */

  JSHeapBlock *heap;
  JSHeapMemoryBlock *heap_freelists[JS_NUM_HEAP_FREELISTS];
  unsigned long heap_size;

  /* Information for the garbage collector. */
  struct
  {
    unsigned long trigger;
    unsigned long bytes_allocated;
    unsigned long bytes_free;
    unsigned long count;
  } gc;

  /* Error handler frames. */
  JSErrorHandlerFrame *error_handler;

  /* Buffer for the error message.  Sorry, we don't support long errors ;-) */
  char error[1024];

  /*
   * The result from the latest evaluation.  This is set when the
   * js_vm_execute(), js_vm_apply(), or js_vm_call_method() functions
   * return to the caller.
   */
  JSNode exec_result;

  /* Event callback hook. */
  int (*hook) (int event, void *context);
  void *hook_context;
  unsigned int hook_operand_count;
  unsigned int hook_operand_count_trigger;

  /* How many file descriptors can be allocated. */
  unsigned long fd_count;

#if PROFILING

  /* Byte-code operand profiling support. */

  unsigned int prof_count[256];
  unsigned char prof_op;

#endif /* PROFILING */
};

typedef struct js_vm_st JSVirtualMachine;

#include "kjs_structs.h"

/*
 * Global variables.
 */

extern unsigned char js_latin1_tolower[256];
extern unsigned char js_latin1_toupper[256];

/*
 * Prototypes for global functions.
 */

/*
 * Memory allocation routines.  If the allocation request fails, the
 * error recovery is performed according to the argument <vm>.  If
 * <vm> is not NULL, an error message is formatted to vm->error and an
 * error is raise with js_vm_error().  If the <vm> is NULL, the
 * functions will return value NULL.  It is an error to call these
 * functions with a non-NULL <vm> that has no error handler
 * initialized.
 */

#ifndef JS_DEBUG_MEMORY_LEAKS
#define JS_DEBUG_MEMORY_LEAKS 0
#endif /* not JS_DEBUG_MEMORY_LEAKS */

#if JS_DEBUG_MEMORY_LEAKS

#define js_malloc(vm, size)		js_malloc_i ((vm), (size), \
						     __FILE__, __LINE__)
#define js_calloc(vm, num, size)	js_calloc_i ((vm), (num), (size), \
						     __FILE__, __LINE__)
#define js_realloc(vm, ptr, size) 	js_realloc_i ((vm), (ptr), (size), \
						      __FILE__, __LINE__)
#define js_strdup(vm, str)		js_strdup_i ((vm), (str), \
						     __FILE__, __LINE__)

void *js_malloc_i (JSVirtualMachine *vm, size_t size, char *, int);
void *js_calloc_i (JSVirtualMachine *vm, size_t num, size_t size, char *, int);
void *js_realloc_i (JSVirtualMachine *vm, void *ptr, size_t size, char *, int);
void js_free (void *ptr);
char *js_strdup_i (JSVirtualMachine *vm, const char *str, char *, int);

#else /* not JS_DEBUG_MEMORY_LEAKS */

void *js_malloc (JSVirtualMachine *vm, size_t size);
void *js_calloc (JSVirtualMachine *vm, size_t num, size_t size);
void *js_realloc (JSVirtualMachine *vm, void *ptr, size_t size);
void js_free (void *ptr);
char *js_strdup (JSVirtualMachine *vm, const char *str);

#endif /* not JS_DEBUG_MEMORY_LEAKS */


/* Byte code. */

JSByteCode *js_bc_read_file (FILE *fp);

JSByteCode *js_bc_read_data (unsigned char *data, unsigned int datalen);

void js_bc_free (JSByteCode *bc);


/* I/O streams. */

/* Allocate one I/O stream handle. */
JSIOStream *js_iostream_new ();

JSIOStream *js_iostream_file (FILE *fp, int readp, int writep, int do_close);

JSIOStream *js_iostream_pipe (FILE *fp, int readp);

size_t js_iostream_read (JSIOStream *stream, void *ptr, size_t size);

size_t js_iostream_write (JSIOStream *stream, void *ptr, size_t size);

int js_iostream_flush (JSIOStream *stream);

int js_iostream_unget (JSIOStream *stream, int byte);

int js_iostream_close (JSIOStream *stream);

int js_iostream_seek (JSIOStream *stream, long offset, int whence);

long js_iostream_get_position (JSIOStream *stream);

long js_iostream_get_length (JSIOStream *stream);

void js_iostream_fill_buffer (JSIOStream *stream);


/* Virtual machine. */

struct _KJS;

JSVirtualMachine *js_vm_create (struct _KJS *kjs,
				unsigned int stack_size,
				JSVMDispatchMethod dispatch_method,
				unsigned int verbose, int stacktrace_on_error,
				JSIOStream *s_stdin, JSIOStream *s_stdout,
				JSIOStream *s_stderr);

void js_vm_destroy (JSVirtualMachine *vm);

/*
 * Execute byte code <bc>.  Function returns 1 if the operation was
 * successful or 0 if any errors were encountered.  In case of errors,
 * the error message is stored at vm->error.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -