/* V7 JavaScript Engine Extension Module */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
#include "v7.h"
#include "../../basext.h"
#include "../../scriba.h"
#include "cbasic.h"
/****************************
Extension Module Functions
****************************/
besVERSION_NEGOTIATE
RETURN_FUNCTION((int)INTERFACE_VERSION);
besEND
besSUB_START
DIM AS long PTR p;
besMODULEPOINTER = besALLOC(sizeof(long));
IF (besMODULEPOINTER EQ NULL) THEN_DO RETURN_FUNCTION(0);
p = (long PTR)besMODULEPOINTER;
RETURN_FUNCTION(0);
besEND
besSUB_FINISH
DIM AS long PTR p;
p = (long PTR)besMODULEPOINTER;
IF (p EQ NULL) THEN_DO RETURN_FUNCTION(0);
RETURN_FUNCTION(0);
besEND
/*****************
Utility Funtions
*****************/
besFUNCTION(SB_shifts)
DIM AS int bp, co, v, x, y, d, p, ar;
besARGUMENTS("ii[i]")
AT v, AT p, AT ar
besARGEND
IF (ar EQ NULL) THEN_DO ar = 0;
x = 0xffffffff & v;
IF (p >= 0) THEN
y = x << p; // bit shift left
ELSE
y = x >> (-p); // bit shift right
END_IF
//
// SUPPORT FOR ARITHMETIC RIGHT SHIFTS
//
IF ((ar) AND (p < 0) AND (x & 0x80000000)) THEN
d = 31 + p;
IF (d < 0) THEN_DO d = 0; // LIMIT
bp = 0x80000000;
DEF_FOR (co = 31 TO co >= d STEP DECR co)
BEGIN_FOR
y = y | bp;
bp = bp >> 1;
NEXT
END_IF
besRETURN_LONG(y);
besEND
besFUNCTION(SB_rotates)
DIM AS int co, v, x, y, d, p, ar;
besARGUMENTS("ii[i]")
AT v, AT p, AT ar
besARGEND
IF (ar EQ NULL) THEN_DO ar = 0;
x = 0xffffffff & v;
d = p;
IF (d < 0) THEN_DO d=-d;
DEF_FOR (co = 1 TO co <= d STEP INCR co)
BEGIN_FOR
IF (p >= 0) THEN
y = x << 1; // bit shift left
IF (x & 0x80000000) THEN_DO y |= 1;
ELSE
y = x >> 1; // bit shift right
IF (x & 1) THEN_DO y |= 0x80000000;
END_IF
x = y;
NEXT
besRETURN_LONG(y);
besEND
besFUNCTION(prop_attr)
V7_DESC_WRITABLE(1);
PRINT ("V7_PROPERTY_NON_WRITABLE: %g\n", V7_PROPERTY_NON_WRITABLE);
PRINT ("V7_PROPERTY_NON_ENUMERABLE: %g\n", V7_PROPERTY_NON_ENUMERABLE);
PRINT ("V7_PROPERTY_NON_CONFIGURABLE: %g\n", V7_PROPERTY_NON_CONFIGURABLE);
PRINT ("V7_PROPERTY_GETTER: %g\n", V7_PROPERTY_GETTER);
PRINT ("V7_PROPERTY_SETTER: %g\n", V7_PROPERTY_SETTER);
PRINT ("_V7_PROPERTY_HIDDEN: %g\n", _V7_PROPERTY_HIDDEN);
PRINT ("_V7_PROPERTY_OFF_HEAP: %g\n", _V7_PROPERTY_OFF_HEAP);
PRINT ("_V7_PROPERTY_USER_DATA_AND_DESTRUCTOR: %g\n", _V7_PROPERTY_USER_DATA_AND_DESTRUCTOR);
PRINT ("_V7_DESC_PRESERVE_VALUE: %g\n", _V7_DESC_PRESERVE_VALUE);
besRETURNVALUE = NULL;
besEND
besCOMMAND(js_iif)
DIM AS NODE nItem;
DIM AS VARIABLE Op1;
DIM long ConditionValue;
USE_CALLER_MORTALS;
nItem = besPARAMETERLIST;
IF (NOT nItem) THEN
RESULT = NULL;
RETURN;
END_IF
Op1 = besEVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
IF (Op1 EQ NULL) THEN
ConditionValue = 0;
ELSE
Op1 = besCONVERT2LONG(Op1);
ConditionValue = LONGVALUE(Op1);
END_IF
IF (NOT ConditionValue) THEN_DO nItem = CDR(nItem);
IF (NOT nItem) THEN
RESULT = NULL;
RETURN;
END_IF
nItem = CDR(nItem);
RESULT = besEVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
RETURN;
besEND_COMMAND
/*******
V7 API
*******/
besFUNCTION(js_create)
struct v7 *v7 = v7_create();
besRETURN_LONG(v7);
besEND
besFUNCTION(js_destroy)
DIM AS unsigned long v7;
besARGUMENTS("i")
AT v7
besARGEND
v7_destroy(v7);
besRETURNVALUE = NULL;
besEND
besFUNCTION(js_exec)
VARIABLE Argument;
LEFTVALUE Lval;
unsigned long __refcount_;
DIM AS unsigned long v7;
DIM AS const char *js_code;
DIM AS v7_val_t result;
enum v7_err rcode = V7_OK;
besARGUMENTS("iz")
AT v7, AT js_code
besARGEND
rcode = v7_exec(v7, js_code, &result);
Argument = besARGUMENT(3);
besLEFTVALUE(Argument,Lval);
besRELEASE(*Lval);
*Lval = besNEWLONG;
LONGVALUE(*Lval) = rcode;
besRETURN_LONG(result);
besEND
besFUNCTION(js_get_int)
DIM AS unsigned long v7;
DIM AS v7_val_t resultptr;
DIM AS int rtnval;
besARGUMENTS("ii")
AT v7, AT resultptr
besARGEND
rtnval = v7_get_int(v7, resultptr);
besRETURN_LONG(rtnval);
besEND
besFUNCTION(js_get_double)
DIM AS unsigned long v7;
DIM AS v7_val_t resultptr;
DIM AS double rtnval;
besARGUMENTS("ii")
AT v7, AT resultptr
besARGEND
rtnval = v7_get_double(v7, resultptr);
besRETURN_DOUBLE(rtnval);
besEND
besFUNCTION(js_get_string)
DIM AS unsigned long v7;
DIM AS v7_val_t resultptr;
DIM const char *sbstr;
DIM size_t *strsize;
besARGUMENTS("ii")
AT v7, AT resultptr
besARGEND
sbstr = v7_get_string(v7, &resultptr, NULL);
besRETURN_STRING(sbstr);
besEND
besFUNCTION(js_get)
DIM AS unsigned long v7;
DIM AS v7_val_t obj, result;
DIM AS const char *name;
DIM AS size_t name_len;
besARGUMENTS("iizi")
AT v7, AT obj, AT name, AT name_len
besARGEND
result = v7_get(v7, obj, name, name_len);
besRETURN_LONG(result);
besEND
besFUNCTION(js_get_global)
DIM AS unsigned long v7;
DIM AS v7_val_t result;
besARGUMENTS("i")
AT v7
besARGEND
result = v7_get_global(v7);
besRETURN_LONG(result);
besEND
besFUNCTION(js_mk_array)
DIM AS unsigned long v7;
DIM AS v7_val_t result;
besARGUMENTS("i")
AT v7
besARGEND
result = v7_mk_array(v7);
besRETURN_LONG(result);
besEND
besFUNCTION(js_array_push)
DIM AS unsigned long v7;
DIM AS v7_val_t arrayptr, arrayval;
DIM AS int rtncode;
besARGUMENTS("iii")
AT v7, AT arrayptr, AT arrayval
besARGEND
rtncode = v7_array_push(v7, arrayptr, arrayval);
besRETURN_LONG(rtncode);
besEND
besFUNCTION(js_mk_number)
DIM AS unsigned long v7;
DIM AS double numval;
DIM AS v7_val_t result;
besARGUMENTS("ir")
AT v7, AT numval
besARGEND
result = v7_mk_number(v7, numval);
besRETURN_LONG(result);
besEND
besFUNCTION(js_apply)
VARIABLE Argument;
LEFTVALUE Lval;
unsigned long __refcount_;
DIM AS unsigned long v7;
DIM AS v7_val_t funcptr, objptr, argsptr, result;
enum v7_err rtncode = V7_OK;
besARGUMENTS("iiii")
AT v7, AT funcptr, AT objptr, AT argsptr
besARGEND
IF (objptr == 0) THEN
rtncode = v7_apply(v7, funcptr, V7_UNDEFINED, argsptr, &result);
ELSE
rtncode = v7_apply(v7, funcptr, objptr, argsptr, &result);
END_IF
Argument = besARGUMENT(4);
besLEFTVALUE(Argument,Lval);
besRELEASE(*Lval);
*Lval = besNEWLONG;
LONGVALUE(*Lval) = rtncode;
besRETURN_LONG(result);
besEND
besFUNCTION(js_get_this)
DIM AS unsigned long v7;
DIM AS v7_val_t result;
besARGUMENTS("i")
AT v7
besARGEND
result = v7_get_this(v7);
besRETURN_LONG(result);
besEND
besFUNCTION(js_get_arguments)
DIM AS unsigned long v7;
DIM AS v7_val_t result;
besARGUMENTS("i")
AT v7
besARGEND
result = v7_get_arguments(v7);
besRETURN_LONG(result);
besEND
besFUNCTION(js_arg)
DIM AS unsigned long v7, arrayidx;
DIM AS v7_val_t result;
besARGUMENTS("ii")
AT v7, AT arrayidx
besARGEND
result = v7_arg(v7, arrayidx);
besRETURN_LONG(result);
besEND
besFUNCTION(js_argc)
DIM AS unsigned long v7, arglen;
besARGUMENTS("i")
AT v7
besARGEND
arglen = v7_argc(v7);
besRETURN_LONG(arglen);
besEND
besFUNCTION(js_own)
DIM AS unsigned long v7;
DIM AS v7_val_t *cvar;
besARGUMENTS("ii")
AT v7, AT cvar
besARGEND
v7_own(v7, &cvar);
besRETURNVALUE = NULL;
besEND
besFUNCTION(js_disown)
DIM AS unsigned long v7;
DIM AS v7_val_t *cvar;
DIM AS int found;
besARGUMENTS("ii")
AT v7, AT cvar
besARGEND
found = v7_disown(v7, &cvar);
besRETURN_LONG(found);
besEND
besFUNCTION(js_get_parser_error)
DIM AS unsigned long v7;
DIM AS const char *errstr;
besARGUMENTS("i")
AT v7
besARGEND
errstr = v7_get_parser_error(v7);
besRETURN_STRING(errstr);
besEND
besFUNCTION(js_is_number)
DIM AS v7_val_t varptr;
DIM AS int istrue;
besARGUMENTS("i")
AT varptr
besARGEND
istrue = v7_is_number(varptr);
besRETURN_LONG(istrue);
besEND
besFUNCTION(js_mk_boolean)
DIM AS unsigned long v7;
DIM AS v7_val_t boolptr;
DIM AS int istrue;
besARGUMENTS("ii")
AT v7, AT istrue
besARGEND
boolptr = v7_mk_boolean(v7, istrue);
besRETURN_LONG(boolptr);
besEND
besFUNCTION(js_get_bool)
DIM AS unsigned long v7;
DIM AS v7_val_t boolptr;
DIM AS int truefalse;
besARGUMENTS("ii")
AT v7, AT truefalse
besARGEND
truefalse = v7_get_bool(v7, boolptr);
besRETURN_LONG(truefalse);
besEND
besFUNCTION(js_is_boolean)
DIM AS v7_val_t boolptr;
DIM AS int istrue;
besARGUMENTS("i")
AT boolptr
besARGEND
istrue = v7_is_boolean(boolptr);
besRETURN_LONG(istrue);
besEND
besFUNCTION(js_mk_null)
DIM AS v7_val_t result;
result = V7_NULL;
besRETURN_LONG(result);
besEND
besFUNCTION(js_is_null)
DIM AS v7_val_t nullptr;
DIM AS int istrue;
besARGUMENTS("i")
AT nullptr
besARGEND
istrue = v7_is_null(nullptr);
besRETURN_LONG(istrue);
besEND
besFUNCTION(js_mk_undefined)
DIM AS v7_val_t result;
result = V7_UNDEFINED;
besRETURN_LONG(result);
besEND
besFUNCTION(js_is_undefined)
DIM AS v7_val_t undefptr;
DIM AS int istrue;
besARGUMENTS("i")
AT undefptr
besARGEND
istrue = v7_is_undefined(undefptr);
besRETURN_LONG(istrue);
besEND
/* HACK - "izii" mask broken - using old school method
besFUNCTION(js_mk_string)
DIM AS unsigned long v7;
DIM AS v7_val_t rtnstrptr;
DIM AS char *jsstr;
DIM AS size_t jsstrlen;
DIM AS int jsstrcopy;
besARGUMENTS("izi")
// AT v7, AT jsstr, AT jsstrlen, AT jsstrcopy
AT v7, AT jsstr, AT jsstrlen
besARGEND
// rtnstrptr = v7_mk_string(v7, jsstr, jsstrlen, jsstrcopy);
rtnstrptr = v7_mk_string(v7, jsstr, jsstrlen, 1);
besRETURN_LONG(rtnstrptr);
besEND
*/
besFUNCTION(js_mk_string)
DIM AS VARIABLE Argument;
DIM AS unsigned long v7;
DIM AS char *jsstr;
DIM AS size_t jsstrlen;
DIM AS int jsstrcopy, i;
DIM AS v7_val_t rtnstrptr;
IF (besARGNR < 4) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
DEF_FOR (i = 1 TO i <= 4 STEP INCR i)
BEGIN_FOR
Argument = besARGUMENT(i);
besDEREFERENCE(Argument);
IF (i EQ 1) THEN_DO v7 = (unsigned long)LONGVALUE(Argument);
IF (i EQ 2) THEN_DO jsstr = STRINGVALUE(Argument);
IF (i EQ 3) THEN_DO jsstrlen = (size_t)LONGVALUE(Argument);
IF (i EQ 4) THEN_DO jsstrcopy = (int)LONGVALUE(Argument);
NEXT
rtnstrptr = v7_mk_string(v7, jsstr, jsstrlen, jsstrcopy);
besRETURN_LONG(rtnstrptr);
besEND
besFUNCTION(js_is_string)
DIM AS v7_val_t jsstrptr;
DIM AS int istrue;
besARGUMENTS("i")
AT jsstrptr
besARGEND
istrue = v7_is_string(jsstrptr);
besRETURN_LONG(istrue);
besEND
besFUNCTION(js_get_cstring)
DIM AS unsigned long v7;
DIM AS v7_val_t cstrptr;
DIM const char *sbcstr;
DIM size_t *cstrlen;
besARGUMENTS("iii")
AT v7, AT cstrptr, AT cstrlen
besARGEND
sbcstr = v7_get_string(v7, &cstrptr, &cstrlen);
besRETURN_STRING(sbcstr);
besEND
besFUNCTION(js_mk_object)
DIM AS unsigned long v7;
DIM AS v7_val_t objptr;
besARGUMENTS("i")
AT v7
besARGEND
objptr = v7_mk_object(v7);
besRETURN_STRING(objptr);
besEND
besFUNCTION(js_is_object)
DIM AS v7_val_t objptr;
DIM AS int istrue;
besARGUMENTS("i")
AT objptr
besARGEND
istrue = v7_is_object(objptr);
besRETURN_LONG(istrue);
besEND
besFUNCTION(js_set_proto)
DIM AS unsigned long v7;
DIM AS v7_val_t objptr, protoptr, oldprotoptr;
besARGUMENTS("iii")
AT v7, AT objptr, AT protoptr
besARGEND
oldprotoptr = v7_set_proto(v7, objptr, protoptr);
besRETURN_LONG(oldprotoptr);
besEND
besFUNCTION(js_def)
DIM AS VARIABLE Argument;
DIM AS unsigned long v7;
DIM AS v7_val_t objptr, valueptr;
DIM AS const char *objname;
DIM AS size_t objnamelen;
DIM AS v7_prop_attr_desc_t attrs_desc;
DIM AS int rtnstatus, i;
IF (besARGNR < 6) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
DEF_FOR (i = 1 TO i <= 6 STEP INCR i)
BEGIN_FOR
Argument = besARGUMENT(i);
besDEREFERENCE(Argument);
IF (i EQ 1) THEN_DO v7 = (unsigned long)LONGVALUE(Argument);
IF (i EQ 2) THEN_DO objptr = (v7_val_t)LONGVALUE(Argument);
IF (i EQ 3) THEN_DO objname = STRINGVALUE(Argument);
IF (i EQ 4) THEN_DO objnamelen = (size_t)LONGVALUE(Argument);
IF (i EQ 5) THEN_DO attrs_desc = (v7_prop_attr_desc_t)LONGVALUE(Argument);
IF (i EQ 6) THEN_DO valueptr = (v7_val_t)LONGVALUE(Argument);
NEXT
rtnstatus = v7_def(v7, objptr, objname, objnamelen, attrs_desc, valueptr);
besRETURN_LONG(rtnstatus);
besEND