Back to home page

Enduro/X

 
 

    


0001 /* see copyright notice in pscript.h */
0002 #include <pscript.h>
0003 #include <math.h>
0004 #include <stdlib.h>
0005 #include <psstdmath.h>
0006 
0007 #define SINGLE_ARG_FUNC(_funcname) static PSInteger math_##_funcname(HPSCRIPTVM v){ \
0008     PSFloat f; \
0009     ps_getfloat(v,2,&f); \
0010     ps_pushfloat(v,(PSFloat)_funcname(f)); \
0011     return 1; \
0012 }
0013 
0014 #define TWO_ARGS_FUNC(_funcname) static PSInteger math_##_funcname(HPSCRIPTVM v){ \
0015     PSFloat p1,p2; \
0016     ps_getfloat(v,2,&p1); \
0017     ps_getfloat(v,3,&p2); \
0018     ps_pushfloat(v,(PSFloat)_funcname(p1,p2)); \
0019     return 1; \
0020 }
0021 
0022 static PSInteger math_srand(HPSCRIPTVM v)
0023 {
0024     PSInteger i;
0025     if(PS_FAILED(ps_getinteger(v,2,&i)))
0026         return ps_throwerror(v,_SC("invalid param"));
0027     srand((unsigned int)i);
0028     return 0;
0029 }
0030 
0031 static PSInteger math_rand(HPSCRIPTVM v)
0032 {
0033     ps_pushinteger(v,rand());
0034     return 1;
0035 }
0036 
0037 static PSInteger math_abs(HPSCRIPTVM v)
0038 {
0039     PSInteger n;
0040     ps_getinteger(v,2,&n);
0041     ps_pushinteger(v,(PSInteger)abs((int)n));
0042     return 1;
0043 }
0044 
0045 SINGLE_ARG_FUNC(sqrt)
0046 SINGLE_ARG_FUNC(fabs)
0047 SINGLE_ARG_FUNC(sin)
0048 SINGLE_ARG_FUNC(cos)
0049 SINGLE_ARG_FUNC(asin)
0050 SINGLE_ARG_FUNC(acos)
0051 SINGLE_ARG_FUNC(log)
0052 SINGLE_ARG_FUNC(log10)
0053 SINGLE_ARG_FUNC(tan)
0054 SINGLE_ARG_FUNC(atan)
0055 TWO_ARGS_FUNC(atan2)
0056 TWO_ARGS_FUNC(pow)
0057 SINGLE_ARG_FUNC(floor)
0058 SINGLE_ARG_FUNC(ceil)
0059 SINGLE_ARG_FUNC(exp)
0060 
0061 #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name),math_##name,nparams,tycheck}
0062 static const PSRegFunction mathlib_funcs[] = {
0063     _DECL_FUNC(sqrt,2,_SC(".n")),
0064     _DECL_FUNC(sin,2,_SC(".n")),
0065     _DECL_FUNC(cos,2,_SC(".n")),
0066     _DECL_FUNC(asin,2,_SC(".n")),
0067     _DECL_FUNC(acos,2,_SC(".n")),
0068     _DECL_FUNC(log,2,_SC(".n")),
0069     _DECL_FUNC(log10,2,_SC(".n")),
0070     _DECL_FUNC(tan,2,_SC(".n")),
0071     _DECL_FUNC(atan,2,_SC(".n")),
0072     _DECL_FUNC(atan2,3,_SC(".nn")),
0073     _DECL_FUNC(pow,3,_SC(".nn")),
0074     _DECL_FUNC(floor,2,_SC(".n")),
0075     _DECL_FUNC(ceil,2,_SC(".n")),
0076     _DECL_FUNC(exp,2,_SC(".n")),
0077     _DECL_FUNC(srand,2,_SC(".n")),
0078     _DECL_FUNC(rand,1,NULL),
0079     _DECL_FUNC(fabs,2,_SC(".n")),
0080     _DECL_FUNC(abs,2,_SC(".n")),
0081     {NULL,(PSFUNCTION)0,0,NULL}
0082 };
0083 #undef _DECL_FUNC
0084 
0085 #ifndef M_PI
0086 #define M_PI (3.14159265358979323846)
0087 #endif
0088 
0089 PSRESULT psstd_register_mathlib(HPSCRIPTVM v)
0090 {
0091     PSInteger i=0;
0092     while(mathlib_funcs[i].name!=0) {
0093         ps_pushstring(v,mathlib_funcs[i].name,-1);
0094         ps_newclosure(v,mathlib_funcs[i].f,0);
0095         ps_setparamscheck(v,mathlib_funcs[i].nparamscheck,mathlib_funcs[i].typemask);
0096         ps_setnativeclosurename(v,-1,mathlib_funcs[i].name);
0097         ps_newslot(v,-3,PSFalse);
0098         i++;
0099     }
0100     ps_pushstring(v,_SC("RAND_MAX"),-1);
0101     ps_pushinteger(v,RAND_MAX);
0102     ps_newslot(v,-3,PSFalse);
0103     ps_pushstring(v,_SC("PI"),-1);
0104     ps_pushfloat(v,(PSFloat)M_PI);
0105     ps_newslot(v,-3,PSFalse);
0106     return PS_OK;
0107 }