/* function min(value: Number): Number */ static EjsObj *math_min(Ejs *ejs, EjsObj *unused, int argc, EjsObj **argv) { MprNumber x, y; x = ejsGetNumber(ejs, argv[0]); y = ejsGetNumber(ejs, argv[1]); if (x < y) { return argv[0]; } return argv[1]; }
/* function sendBlock(content, options): Number */ static EjsNumber *ws_sendBlock(Ejs *ejs, EjsWebSocket *ws, int argc, EjsObj **argv) { EjsByteArray *ba; EjsAny *content, *vp; ssize nbytes; cchar *str; int last, mode, type, flags; assert(argc == 2); if (ws->conn->state < HTTP_STATE_PARSED && !waitForHttpState(ws, HTTP_STATE_PARSED, -1, 1)) { return ESV(null); } content = argv[0]; last = ejsGetPropertyByName(ejs, argv[1], EN("last")) != ESV(false); if ((vp = ejsGetPropertyByName(ejs, argv[1], EN("mode"))) != 0) { mode = (int) ejsGetNumber(ejs, vp); if (mode != HTTP_BUFFER && mode != HTTP_BLOCK && mode != HTTP_NON_BLOCK) { ejsThrowArgError(ejs, "Bad message mode"); return 0; } } else { mode = HTTP_BUFFER; } if ((vp = ejsGetPropertyByName(ejs, argv[1], EN("type"))) != 0) { type = (int) ejsGetNumber(ejs, vp); if (type != WS_MSG_CONT && type != WS_MSG_TEXT && type != WS_MSG_BINARY) { ejsThrowArgError(ejs, "Bad message type"); return 0; } } else { type = WS_MSG_TEXT; } flags = mode; if (!last) { flags |= HTTP_MORE; } if (ejsIs(ejs, content, ByteArray)) { ba = (EjsByteArray*) content; nbytes = ejsGetByteArrayAvailableData(ba); nbytes = httpSendBlock(ws->conn, type, (cchar*) &ba->value[ba->readPosition], nbytes, flags); } else { str = ejsToMulti(ejs, content); nbytes = httpSendBlock(ws->conn, type, str, slen(str), flags); } if (nbytes < 0) { ejsThrowIOError(ejs, "Cannot send block"); return 0; } return ejsCreateNumber(ejs, (MprNumber) nbytes); }
/* function pow(x: Number, y: Number): Number */ static EjsNumber *math_pow(Ejs *ejs, EjsObj *unused, int argc, EjsObj **argv) { MprNumber x, y, result; x = ejsGetNumber(ejs, argv[0]); y = ejsGetNumber(ejs, argv[1]); result = pow(x, y); #if CYGWIN /* Cygwin computes (0.0 / -1) == -Infinity */ if (result < 0 && x == 0.0) { result = -result; } #endif return ejsCreateNumber(ejs, (MprNumber) result); }
/* function round(value: Number): Number */ static EjsNumber *math_round(Ejs *ejs, EjsObj *unused, int argc, EjsObj **argv) { MprNumber n; n = ejsGetNumber(ejs, argv[0]); if (-0.5 <= n && n < 0) { n = -0.0; } else { n += 0.5; } return ejsCreateNumber(ejs, floor(n)); }
/* function exec(str: String, start: Number = 0): Array */ static EjsArray *regex_exec(Ejs *ejs, EjsRegExp *rp, int argc, EjsObj **argv) { EjsArray *results; EjsString *match, *str; int matches[BIT_MAX_REGEX_MATCHES * 3]; int count, start, len, i, index; str = (EjsString*) argv[0]; if (argc == 2) { start = (int) ejsGetNumber(ejs, argv[1]); } else { start = rp->endLastMatch; } rp->matched = 0; assert(rp->compiled); count = pcre_exec(rp->compiled, NULL, str->value, (int) str->length, start, 0, matches, sizeof(matches) / sizeof(int)); if (count < 0) { rp->endLastMatch = 0; return ESV(null); } results = ejsCreateArray(ejs, count); for (index = 0, i = 0; i < count; i++, index += 2) { len = matches[index + 1] - matches[index]; match = ejsCreateString(ejs, &str->value[matches[index]], len); ejsSetProperty(ejs, results, i, match); if (index == 0) { rp->matched = match; } } if (rp->global) { /* Only save if global flag used as per spec */ rp->startLastMatch = matches[0]; rp->endLastMatch = matches[1]; } return results; }
/* function set lastIndex(value: Number): Void */ static EjsObj *regex_setLastIndex(Ejs *ejs, EjsRegExp *rp, int argc, EjsObj **argv) { rp->endLastMatch = (int) ejsGetNumber(ejs, argv[0]); return 0; }
/* function floor(value: Number): Number */ static EjsNumber *math_floor(Ejs *ejs, EjsObj *unused, int argc, EjsObj **argv) { return ejsCreateNumber(ejs, (MprNumber) floor(ejsGetNumber(ejs, argv[0]))); }
/* function atan2(x: Number, y: Number): Number */ static EjsNumber *math_atan2(Ejs *ejs, EjsObj *unused, int argc, EjsObj **argv) { return ejsCreateNumber(ejs, (MprNumber) atan2(ejsGetNumber(ejs, argv[0]), ejsGetNumber(ejs, argv[1]))); }
/* function abs(value: Number): Number */ static EjsNumber *math_abs(Ejs *ejs, EjsObj *unused, int argc, EjsObj **argv) { // TODO - can a null slip through? return ejsCreateNumber(ejs, (MprNumber) fabs(ejsGetNumber(ejs, argv[0]))); }