mirror of
https://github.com/Ylianst/MeshAgent
synced 2025-12-10 21:33:38 +00:00
1765 lines
64 KiB
C
1765 lines
64 KiB
C
/*
|
|
Copyright 2006 - 2018 Intel Corporation
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
#include "duktape.h"
|
|
#include "ILibDuktape_Helpers.h"
|
|
#include "ILibDuktapeModSearch.h"
|
|
#include "ILibDuktape_DuplexStream.h"
|
|
#include "ILibDuktape_EventEmitter.h"
|
|
#include "ILibDuktape_Debugger.h"
|
|
#include "../microstack/ILibParsers.h"
|
|
#include "../microstack/ILibCrypto.h"
|
|
#include "../microstack/ILibRemoteLogging.h"
|
|
|
|
|
|
#define ILibDuktape_Timer_Ptrs "\xFF_DuktapeTimer_PTRS"
|
|
#define ILibDuktape_Queue_Ptr "\xFF_Queue"
|
|
#define ILibDuktape_Stream_Buffer "\xFF_BUFFER"
|
|
#define ILibDuktape_Stream_ReadablePtr "\xFF_ReadablePtr"
|
|
#define ILibDuktape_Stream_WritablePtr "\xFF_WritablePtr"
|
|
#define ILibDuktape_Console_Destination "\xFF_Console_Destination"
|
|
#define ILibDuktape_Console_LOG_Destination "\xFF_Console_Destination"
|
|
#define ILibDuktape_Console_WARN_Destination "\xFF_Console_WARN_Destination"
|
|
#define ILibDuktape_Console_ERROR_Destination "\xFF_Console_ERROR_Destination"
|
|
#define ILibDuktape_Console_INFO_Level "\xFF_Console_INFO_Level"
|
|
#define ILibDuktape_Console_SessionID "\xFF_Console_SessionID"
|
|
|
|
typedef enum ILibDuktape_Console_DestinationFlags
|
|
{
|
|
ILibDuktape_Console_DestinationFlags_DISABLED = 0,
|
|
ILibDuktape_Console_DestinationFlags_StdOut = 1,
|
|
ILibDuktape_Console_DestinationFlags_ServerConsole = 2,
|
|
ILibDuktape_Console_DestinationFlags_WebLog = 4,
|
|
ILibDuktape_Console_DestinationFlags_LogFile = 8
|
|
}ILibDuktape_Console_DestinationFlags;
|
|
|
|
int g_displayStreamPipeMessages = 0;
|
|
int g_displayFinalizerMessages = 0;
|
|
|
|
duk_ret_t ILibDuktape_Pollyfills_Buffer_slice(duk_context *ctx)
|
|
{
|
|
int nargs = duk_get_top(ctx);
|
|
char *buffer;
|
|
char *out;
|
|
duk_size_t bufferLen;
|
|
int offset = 0;
|
|
duk_push_this(ctx);
|
|
|
|
buffer = Duktape_GetBuffer(ctx, -1, &bufferLen);
|
|
if (nargs >= 1)
|
|
{
|
|
offset = duk_require_int(ctx, 0);
|
|
bufferLen -= offset;
|
|
}
|
|
if (nargs == 2)
|
|
{
|
|
bufferLen = (duk_size_t)duk_require_int(ctx, 1) - offset;
|
|
}
|
|
duk_push_fixed_buffer(ctx, bufferLen);
|
|
out = Duktape_GetBuffer(ctx, -1, NULL);
|
|
memcpy_s(out, bufferLen, buffer + offset, bufferLen);
|
|
return 1;
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_Buffer_toString(duk_context *ctx)
|
|
{
|
|
int nargs = duk_get_top(ctx);
|
|
char *buffer, *tmpBuffer;
|
|
duk_size_t bufferLen;
|
|
char *cType;
|
|
|
|
duk_push_this(ctx); // [buffer]
|
|
buffer = Duktape_GetBuffer(ctx, -1, &bufferLen);
|
|
|
|
if (nargs == 0)
|
|
{
|
|
// Just convert to a string
|
|
duk_push_lstring(ctx, buffer, bufferLen); // [buffer][string]
|
|
}
|
|
else
|
|
{
|
|
cType = (char*)duk_require_string(ctx, 0);
|
|
if (strcmp(cType, "base64") == 0)
|
|
{
|
|
duk_push_fixed_buffer(ctx, ILibBase64EncodeLength((int)bufferLen));
|
|
tmpBuffer = Duktape_GetBuffer(ctx, -1, NULL);
|
|
ILibBase64Encode((unsigned char*)buffer, (int)bufferLen, (unsigned char**)&tmpBuffer);
|
|
duk_push_string(ctx, tmpBuffer);
|
|
}
|
|
else if (strcmp(cType, "hex") == 0)
|
|
{
|
|
duk_push_fixed_buffer(ctx, 1 + (bufferLen * 2));
|
|
tmpBuffer = Duktape_GetBuffer(ctx, -1, NULL);
|
|
util_tohex(buffer, (int)bufferLen, tmpBuffer);
|
|
duk_push_string(ctx, tmpBuffer);
|
|
}
|
|
else if (strcmp(cType, "hex:") == 0)
|
|
{
|
|
duk_push_fixed_buffer(ctx, 1 + (bufferLen * 3));
|
|
tmpBuffer = Duktape_GetBuffer(ctx, -1, NULL);
|
|
util_tohex2(buffer, (int)bufferLen, tmpBuffer);
|
|
duk_push_string(ctx, tmpBuffer);
|
|
}
|
|
else
|
|
{
|
|
duk_push_string(ctx, "buffer.toString(): Unrecognized parameter");
|
|
duk_throw(ctx);
|
|
return(DUK_RET_ERROR);
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_Buffer_from(duk_context *ctx)
|
|
{
|
|
int nargs = duk_get_top(ctx);
|
|
char *str;
|
|
duk_size_t strlength;
|
|
char *encoding;
|
|
char *buffer;
|
|
int bufferLen;
|
|
|
|
if (nargs == 1)
|
|
{
|
|
str = (char*)duk_get_lstring(ctx, 0, &strlength);
|
|
duk_push_fixed_buffer(ctx, strlength);
|
|
buffer = Duktape_GetBuffer(ctx, -1, NULL);
|
|
memcpy_s(buffer, strlength, str, strlength);
|
|
duk_push_buffer_object(ctx, -1, 0, strlength, DUK_BUFOBJ_NODEJS_BUFFER);
|
|
return(1);
|
|
}
|
|
else if(!(nargs == 2 && duk_is_string(ctx, 0) && duk_is_string(ctx, 1)))
|
|
{
|
|
duk_push_string(ctx, "Buffer.from(): Usage not supported yet.");
|
|
duk_throw(ctx);
|
|
return(DUK_RET_ERROR);
|
|
}
|
|
|
|
str = (char*)duk_get_lstring(ctx, 0, &strlength);
|
|
encoding = (char*)duk_require_string(ctx, 1);
|
|
|
|
if (strcmp(encoding, "base64") == 0)
|
|
{
|
|
// Base64
|
|
duk_push_fixed_buffer(ctx, ILibBase64DecodeLength((int)strlength));
|
|
buffer = Duktape_GetBuffer(ctx, -1, NULL);
|
|
bufferLen = ILibBase64Decode((unsigned char*)str, (int)strlength, (unsigned char**)&buffer);
|
|
duk_push_buffer_object(ctx, -1, 0, bufferLen, DUK_BUFOBJ_NODEJS_BUFFER);
|
|
}
|
|
else if (strcmp(encoding, "hex") == 0)
|
|
{
|
|
duk_push_fixed_buffer(ctx, strlength / 2);
|
|
buffer = Duktape_GetBuffer(ctx, -1, NULL);
|
|
bufferLen = util_hexToBuf(str, (int)strlength, buffer);
|
|
duk_push_buffer_object(ctx, -1, 0, bufferLen, DUK_BUFOBJ_NODEJS_BUFFER);
|
|
}
|
|
else
|
|
{
|
|
duk_push_string(ctx, "Buffer.from(): Encoding not supported yet.");
|
|
duk_throw(ctx);
|
|
return(DUK_RET_ERROR);
|
|
}
|
|
return 1;
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_Buffer_readInt32BE(duk_context *ctx)
|
|
{
|
|
int offset = duk_require_int(ctx, 0);
|
|
char *buffer;
|
|
duk_size_t bufferLen;
|
|
|
|
duk_push_this(ctx);
|
|
buffer = Duktape_GetBuffer(ctx, -1, &bufferLen);
|
|
|
|
duk_push_int(ctx, ntohl(((int*)(buffer + offset))[0]));
|
|
return(1);
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_Buffer_alloc(duk_context *ctx)
|
|
{
|
|
int sz = duk_require_int(ctx, 0);
|
|
duk_push_fixed_buffer(ctx, sz);
|
|
char *buffer = Duktape_GetBuffer(ctx, -1, NULL);
|
|
memset(buffer, 0, sz);
|
|
duk_push_buffer_object(ctx, -1, 0, sz, DUK_BUFOBJ_NODEJS_BUFFER);
|
|
return(1);
|
|
}
|
|
void ILibDuktape_Polyfills_Buffer(duk_context *ctx)
|
|
{
|
|
char extras[] =
|
|
"Object.defineProperty(Buffer.prototype, \"swap32\",\
|
|
{\
|
|
value: function swap32()\
|
|
{\
|
|
var a = this.readUInt16BE(0);\
|
|
var b = this.readUInt16BE(2);\
|
|
this.writeUInt16LE(a, 2);\
|
|
this.writeUInt16LE(b, 0);\
|
|
return(this);\
|
|
}\
|
|
});";
|
|
|
|
duk_eval_string(ctx, extras); duk_pop(ctx);
|
|
|
|
// Polyfill Buffer.from()
|
|
duk_get_prop_string(ctx, -1, "Buffer"); // [g][Buffer]
|
|
duk_push_c_function(ctx, ILibDuktape_Polyfills_Buffer_from, DUK_VARARGS); // [g][Buffer][func]
|
|
duk_put_prop_string(ctx, -2, "from"); // [g][Buffer]
|
|
duk_pop(ctx); // [g]
|
|
|
|
// Polyfill Buffer.alloc() for Node Buffers)
|
|
duk_get_prop_string(ctx, -1, "Buffer"); // [g][Buffer]
|
|
duk_push_c_function(ctx, ILibDuktape_Polyfills_Buffer_alloc, DUK_VARARGS); // [g][Buffer][func]
|
|
duk_put_prop_string(ctx, -2, "alloc"); // [g][Buffer]
|
|
duk_pop(ctx); // [g]
|
|
|
|
|
|
// Polyfill Buffer.toString() for Node Buffers
|
|
duk_get_prop_string(ctx, -1, "Buffer"); // [g][Buffer]
|
|
duk_get_prop_string(ctx, -1, "prototype"); // [g][Buffer][prototype]
|
|
duk_push_c_function(ctx, ILibDuktape_Polyfills_Buffer_toString, DUK_VARARGS); // [g][Buffer][prototype][func]
|
|
duk_put_prop_string(ctx, -2, "toString"); // [g][Buffer][prototype]
|
|
duk_pop_2(ctx); // [g]
|
|
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_String_startsWith(duk_context *ctx)
|
|
{
|
|
duk_size_t tokenLen;
|
|
char *token = Duktape_GetBuffer(ctx, 0, &tokenLen);
|
|
char *buffer;
|
|
duk_size_t bufferLen;
|
|
|
|
duk_push_this(ctx);
|
|
buffer = Duktape_GetBuffer(ctx, -1, &bufferLen);
|
|
|
|
if (ILibString_StartsWith(buffer, (int)bufferLen, token, (int)tokenLen) != 0)
|
|
{
|
|
duk_push_true(ctx);
|
|
}
|
|
else
|
|
{
|
|
duk_push_false(ctx);
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_String_endsWith(duk_context *ctx)
|
|
{
|
|
duk_size_t tokenLen;
|
|
char *token = Duktape_GetBuffer(ctx, 0, &tokenLen);
|
|
char *buffer;
|
|
duk_size_t bufferLen;
|
|
|
|
duk_push_this(ctx);
|
|
buffer = Duktape_GetBuffer(ctx, -1, &bufferLen);
|
|
|
|
if (ILibString_EndsWith(buffer, (int)bufferLen, token, (int)tokenLen) != 0)
|
|
{
|
|
duk_push_true(ctx);
|
|
}
|
|
else
|
|
{
|
|
duk_push_false(ctx);
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
void ILibDuktape_Polyfills_String(duk_context *ctx)
|
|
{
|
|
// Polyfill 'String.startsWith'
|
|
duk_get_prop_string(ctx, -1, "String"); // [string]
|
|
duk_get_prop_string(ctx, -1, "prototype"); // [string][proto]
|
|
duk_push_c_function(ctx, ILibDuktape_Polyfills_String_startsWith, DUK_VARARGS); // [string][proto][func]
|
|
duk_put_prop_string(ctx, -2, "startsWith"); // [string][proto]
|
|
duk_push_c_function(ctx, ILibDuktape_Polyfills_String_endsWith, DUK_VARARGS); // [string][proto][func]
|
|
duk_put_prop_string(ctx, -2, "endsWith"); // [string][proto]
|
|
duk_pop_2(ctx);
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_Console_log(duk_context *ctx)
|
|
{
|
|
int numargs = duk_get_top(ctx);
|
|
int i, x;
|
|
int len = 0;
|
|
duk_size_t strLen;
|
|
char *str;
|
|
char *PREFIX = NULL;
|
|
char *DESTINATION = NULL;
|
|
duk_push_current_function(ctx);
|
|
ILibDuktape_LogTypes logType = (ILibDuktape_LogTypes)Duktape_GetIntPropertyValue(ctx, -1, "logType", ILibDuktape_LogType_Normal);
|
|
switch (logType)
|
|
{
|
|
case ILibDuktape_LogType_Warn:
|
|
PREFIX = (char*)"WARNING: "; // LENGTH MUST BE <= 9
|
|
DESTINATION = ILibDuktape_Console_WARN_Destination;
|
|
break;
|
|
case ILibDuktape_LogType_Error:
|
|
PREFIX = (char*)"ERROR: "; // LENGTH MUST BE <= 9
|
|
DESTINATION = ILibDuktape_Console_ERROR_Destination;
|
|
break;
|
|
case ILibDuktape_LogType_Info1:
|
|
case ILibDuktape_LogType_Info2:
|
|
case ILibDuktape_LogType_Info3:
|
|
duk_push_this(ctx);
|
|
i = Duktape_GetIntPropertyValue(ctx, -1, ILibDuktape_Console_INFO_Level, 0);
|
|
duk_pop(ctx);
|
|
PREFIX = NULL;
|
|
if (i >= (((int)logType + 1) - (int)ILibDuktape_LogType_Info1))
|
|
{
|
|
DESTINATION = ILibDuktape_Console_LOG_Destination;
|
|
}
|
|
else
|
|
{
|
|
return(0);
|
|
}
|
|
break;
|
|
default:
|
|
PREFIX = NULL;
|
|
DESTINATION = ILibDuktape_Console_LOG_Destination;
|
|
break;
|
|
}
|
|
duk_pop(ctx);
|
|
|
|
// Calculate total length of string
|
|
for (i = 0; i < numargs; ++i)
|
|
{
|
|
if (duk_is_string(ctx, i))
|
|
{
|
|
len += (i == 0 ? 0 : 2);
|
|
duk_get_lstring(ctx, i, &strLen);
|
|
len += (int)strLen;
|
|
}
|
|
else
|
|
{
|
|
duk_dup(ctx, i);
|
|
if (strcmp("[object Object]", duk_to_string(ctx, -1)) == 0)
|
|
{
|
|
duk_pop(ctx);
|
|
duk_dup(ctx, i);
|
|
len += (i == 0 ? 1 : 3);
|
|
duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY);
|
|
int propNum = 0;
|
|
while (duk_next(ctx, -1, 1))
|
|
{
|
|
len += 2;
|
|
len += (propNum++ == 0 ? 1 : 2);
|
|
duk_to_lstring(ctx, -2, &strLen); len += (int)strLen;
|
|
duk_to_lstring(ctx, -1, &strLen); len += (int)strLen;
|
|
duk_pop_2(ctx);
|
|
}
|
|
duk_pop(ctx);
|
|
len += 2;
|
|
}
|
|
else
|
|
{
|
|
len += (i == 0 ? 0 : 2);
|
|
duk_get_lstring(ctx, -1, &strLen); len += (int)strLen;
|
|
}
|
|
}
|
|
}
|
|
len += 2; // NULL Terminator and final carriage return
|
|
strLen = len;
|
|
|
|
str = ILibMemory_AllocateA(strLen + ((PREFIX != NULL) ? strnlen_s(PREFIX, 9) : 0));
|
|
x = (int)(ILibMemory_AllocateA_Size(str) - strLen);
|
|
if (x != 0)
|
|
{
|
|
strLen += sprintf_s(str, strLen, PREFIX);
|
|
}
|
|
for (i = 0; i < numargs; ++i)
|
|
{
|
|
if (duk_is_string(ctx, i))
|
|
{
|
|
x += sprintf_s(str + x, strLen - x, "%s%s", (i == 0 ? "" : ", "), duk_require_string(ctx, i));
|
|
}
|
|
else
|
|
{
|
|
duk_dup(ctx, i);
|
|
if (strcmp("[object Object]", duk_to_string(ctx, -1)) == 0)
|
|
{
|
|
duk_pop(ctx);
|
|
duk_dup(ctx, i);
|
|
x += sprintf_s(str+x, strLen - x, "%s", (i == 0 ? "{" : ", {"));
|
|
duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY);
|
|
int propNum = 0;
|
|
while (duk_next(ctx, -1, 1))
|
|
{
|
|
x += sprintf_s(str + x, strLen - x, "%s%s: %s", ((propNum++ == 0) ? " " : ", "), (char*)duk_to_string(ctx, -2), (char*)duk_to_string(ctx, -1));
|
|
duk_pop_2(ctx);
|
|
}
|
|
duk_pop(ctx);
|
|
x += sprintf_s(str + x, strLen - x, " }");
|
|
}
|
|
else
|
|
{
|
|
x += sprintf_s(str + x, strLen - x, "%s%s", (i == 0 ? "" : ", "), duk_to_string(ctx, -1));
|
|
}
|
|
}
|
|
}
|
|
x += sprintf_s(str + x, strLen - x, "\n");
|
|
|
|
duk_push_this(ctx); // [console]
|
|
int dest = Duktape_GetIntPropertyValue(ctx, -1, DESTINATION, ILibDuktape_Console_DestinationFlags_StdOut);
|
|
|
|
if ((dest & ILibDuktape_Console_DestinationFlags_StdOut) == ILibDuktape_Console_DestinationFlags_StdOut)
|
|
{
|
|
#ifdef WIN32
|
|
DWORD writeLen;
|
|
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), (void*)str, x, &writeLen, NULL);
|
|
#else
|
|
ignore_result(write(STDOUT_FILENO, str, x));
|
|
#endif
|
|
}
|
|
if ((dest & ILibDuktape_Console_DestinationFlags_WebLog) == ILibDuktape_Console_DestinationFlags_WebLog)
|
|
{
|
|
ILibRemoteLogging_printf(ILibChainGetLogger(Duktape_GetChain(ctx)), ILibRemoteLogging_Modules_Microstack_Generic, ILibRemoteLogging_Flags_VerbosityLevel_1, "%s", str);
|
|
}
|
|
if ((dest & ILibDuktape_Console_DestinationFlags_ServerConsole) == ILibDuktape_Console_DestinationFlags_ServerConsole)
|
|
{
|
|
if (duk_peval_string(ctx, "require('MeshAgent');") == 0)
|
|
{
|
|
duk_get_prop_string(ctx, -1, "SendCommand"); // [console][agent][SendCommand]
|
|
duk_swap_top(ctx, -2); // [console][SendCommand][this]
|
|
duk_push_object(ctx); // [console][SendCommand][this][options]
|
|
duk_push_string(ctx, "msg"); duk_put_prop_string(ctx, -2, "action");
|
|
duk_push_string(ctx, "console"); duk_put_prop_string(ctx, -2, "type");
|
|
duk_push_string(ctx, str); duk_put_prop_string(ctx, -2, "value");
|
|
if (duk_has_prop_string(ctx, -4, ILibDuktape_Console_SessionID))
|
|
{
|
|
duk_get_prop_string(ctx, -4, ILibDuktape_Console_SessionID);
|
|
duk_put_prop_string(ctx, -2, "sessionid");
|
|
}
|
|
duk_call_method(ctx, 1);
|
|
}
|
|
}
|
|
if ((dest & ILibDuktape_Console_DestinationFlags_LogFile) == ILibDuktape_Console_DestinationFlags_LogFile)
|
|
{
|
|
duk_size_t pathLen;
|
|
char *path;
|
|
char *tmp = ILibMemory_AllocateA(x + 32);
|
|
int tmpx = ILibGetLocalTime(tmp + 1, (int)ILibMemory_AllocateA_Size(tmp) - 1) + 1;
|
|
tmp[0] = '[';
|
|
tmp[tmpx] = ']';
|
|
tmp[tmpx + 1] = ':';
|
|
tmp[tmpx + 2] = ' ';
|
|
memcpy_s(tmp + tmpx + 3, ILibMemory_AllocateA_Size(tmp) - tmpx - 3, str, x);
|
|
duk_eval_string(ctx, "require('fs');");
|
|
duk_get_prop_string(ctx, -1, "writeFileSync"); // [fs][writeFileSync]
|
|
duk_swap_top(ctx, -2); // [writeFileSync][this]
|
|
duk_push_heapptr(ctx, ILibDuktape_GetProcessObject(ctx)); // [writeFileSync][this][process]
|
|
duk_get_prop_string(ctx, -1, "execPath"); // [writeFileSync][this][process][execPath]
|
|
path = (char*)duk_get_lstring(ctx, -1, &pathLen);
|
|
if (path != NULL)
|
|
{
|
|
if (ILibString_EndsWithEx(path, (int)pathLen, ".exe", 4, 0))
|
|
{
|
|
duk_get_prop_string(ctx, -1, "substring"); // [writeFileSync][this][process][execPath][substring]
|
|
duk_swap_top(ctx, -2); // [writeFileSync][this][process][substring][this]
|
|
duk_push_int(ctx, 0); // [writeFileSync][this][process][substring][this][0]
|
|
duk_push_int(ctx, (int)(pathLen - 4)); // [writeFileSync][this][process][substring][this][0][len]
|
|
duk_call_method(ctx, 2); // [writeFileSync][this][process][path]
|
|
}
|
|
duk_get_prop_string(ctx, -1, "concat"); // [writeFileSync][this][process][path][concat]
|
|
duk_swap_top(ctx, -2); // [writeFileSync][this][process][concat][this]
|
|
duk_push_string(ctx, ".jlog"); // [writeFileSync][this][process][concat][this][.jlog]
|
|
duk_call_method(ctx, 1); // [writeFileSync][this][process][logPath]
|
|
duk_remove(ctx, -2); // [writeFileSync][this][logPath]
|
|
duk_push_string(ctx, tmp); // [writeFileSync][this][logPath][log]
|
|
duk_push_object(ctx); // [writeFileSync][this][logPath][log][options]
|
|
duk_push_string(ctx, "a"); duk_put_prop_string(ctx, -2, "flags");
|
|
duk_call_method(ctx, 3);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_Console_enableWebLog(duk_context *ctx)
|
|
{
|
|
#ifdef _REMOTELOGGING
|
|
void *chain = Duktape_GetChain(ctx);
|
|
int port = duk_require_int(ctx, 0);
|
|
duk_size_t pLen;
|
|
if (duk_peval_string(ctx, "process.argv0") != 0) { return(ILibDuktape_Error(ctx, "console.enableWebLog(): Couldn't fetch argv0")); }
|
|
char *p = (char*)duk_get_lstring(ctx, -1, &pLen);
|
|
if (ILibString_EndsWith(p, (int)pLen, ".js", 3) != 0)
|
|
{
|
|
memcpy_s(ILibScratchPad2, sizeof(ILibScratchPad2), p, pLen - 3);
|
|
sprintf_s(ILibScratchPad2 + (pLen - 3), sizeof(ILibScratchPad2) - 3, ".wlg");
|
|
}
|
|
else if (ILibString_EndsWith(p, (int)pLen, ".exe", 3) != 0)
|
|
{
|
|
memcpy_s(ILibScratchPad2, sizeof(ILibScratchPad2), p, pLen - 4);
|
|
sprintf_s(ILibScratchPad2 + (pLen - 3), sizeof(ILibScratchPad2) - 4, ".wlg");
|
|
}
|
|
else
|
|
{
|
|
sprintf_s(ILibScratchPad2, sizeof(ILibScratchPad2), "%s.wlg", p);
|
|
}
|
|
ILibStartDefaultLoggerEx(chain, (unsigned short)port, ILibScratchPad2);
|
|
#endif
|
|
return (0);
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_Console_displayStreamPipe_getter(duk_context *ctx)
|
|
{
|
|
duk_push_int(ctx, g_displayStreamPipeMessages);
|
|
return(1);
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_Console_displayStreamPipe_setter(duk_context *ctx)
|
|
{
|
|
g_displayStreamPipeMessages = duk_require_int(ctx, 0);
|
|
return(0);
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_Console_displayFinalizer_getter(duk_context *ctx)
|
|
{
|
|
duk_push_int(ctx, g_displayFinalizerMessages);
|
|
return(1);
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_Console_displayFinalizer_setter(duk_context *ctx)
|
|
{
|
|
g_displayFinalizerMessages = duk_require_int(ctx, 0);
|
|
return(0);
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_Console_logRefCount(duk_context *ctx)
|
|
{
|
|
printf("Reference Count => %s[%p]:%d\n", Duktape_GetStringPropertyValue(ctx, 0, ILibDuktape_OBJID, "UNKNOWN"), duk_require_heapptr(ctx, 0), ILibDuktape_GetReferenceCount(ctx, 0) - 1);
|
|
return(0);
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_Console_setDestination(duk_context *ctx)
|
|
{
|
|
int nargs = duk_get_top(ctx);
|
|
int dest = duk_require_int(ctx, 0);
|
|
|
|
duk_push_this(ctx); // console
|
|
if ((dest & ILibDuktape_Console_DestinationFlags_ServerConsole) == ILibDuktape_Console_DestinationFlags_ServerConsole)
|
|
{
|
|
// Mesh Server Console
|
|
if (duk_peval_string(ctx, "require('MeshAgent');") != 0) { return(ILibDuktape_Error(ctx, "Unable to set destination to Mesh Console ")); }
|
|
duk_pop(ctx);
|
|
if (nargs > 1)
|
|
{
|
|
duk_dup(ctx, 1);
|
|
duk_put_prop_string(ctx, -2, ILibDuktape_Console_SessionID);
|
|
}
|
|
else
|
|
{
|
|
duk_del_prop_string(ctx, -1, ILibDuktape_Console_SessionID);
|
|
}
|
|
}
|
|
duk_dup(ctx, 0);
|
|
duk_put_prop_string(ctx, -2, ILibDuktape_Console_Destination);
|
|
return(0);
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_Console_setInfoLevel(duk_context *ctx)
|
|
{
|
|
int val = duk_require_int(ctx, 0);
|
|
if (val < 0) { return(ILibDuktape_Error(ctx, "Invalid Info Level: %d", val)); }
|
|
|
|
duk_push_this(ctx);
|
|
duk_push_int(ctx, val);
|
|
duk_put_prop_string(ctx, -2, ILibDuktape_Console_INFO_Level);
|
|
|
|
return(0);
|
|
}
|
|
void ILibDuktape_Polyfills_Console(duk_context *ctx)
|
|
{
|
|
// Polyfill console.log()
|
|
#ifdef WIN32
|
|
SetConsoleOutputCP(CP_UTF8);
|
|
#endif
|
|
|
|
if (duk_has_prop_string(ctx, -1, "console"))
|
|
{
|
|
duk_get_prop_string(ctx, -1, "console"); // [g][console]
|
|
}
|
|
else
|
|
{
|
|
duk_push_object(ctx); // [g][console]
|
|
duk_dup(ctx, -1); // [g][console][console]
|
|
duk_put_prop_string(ctx, -3, "console"); // [g][console]
|
|
}
|
|
|
|
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "logType", (int)ILibDuktape_LogType_Normal, "log", ILibDuktape_Polyfills_Console_log, DUK_VARARGS);
|
|
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "logType", (int)ILibDuktape_LogType_Warn, "warn", ILibDuktape_Polyfills_Console_log, DUK_VARARGS);
|
|
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "logType", (int)ILibDuktape_LogType_Error, "error", ILibDuktape_Polyfills_Console_log, DUK_VARARGS);
|
|
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "logType", (int)ILibDuktape_LogType_Info1, "info1", ILibDuktape_Polyfills_Console_log, DUK_VARARGS);
|
|
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "logType", (int)ILibDuktape_LogType_Info2, "info2", ILibDuktape_Polyfills_Console_log, DUK_VARARGS);
|
|
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "logType", (int)ILibDuktape_LogType_Info3, "info3", ILibDuktape_Polyfills_Console_log, DUK_VARARGS);
|
|
|
|
|
|
ILibDuktape_CreateInstanceMethod(ctx, "enableWebLog", ILibDuktape_Polyfills_Console_enableWebLog, 1);
|
|
ILibDuktape_CreateEventWithGetterAndSetterEx(ctx, "displayStreamPipeMessages", ILibDuktape_Polyfills_Console_displayStreamPipe_getter, ILibDuktape_Polyfills_Console_displayStreamPipe_setter);
|
|
ILibDuktape_CreateEventWithGetterAndSetterEx(ctx, "displayFinalizerMessages", ILibDuktape_Polyfills_Console_displayFinalizer_getter, ILibDuktape_Polyfills_Console_displayFinalizer_setter);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "logReferenceCount", ILibDuktape_Polyfills_Console_logRefCount, 1);
|
|
|
|
ILibDuktape_CreateInstanceMethod(ctx, "setDestination", ILibDuktape_Polyfills_Console_setDestination, DUK_VARARGS);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "setInfoLevel", ILibDuktape_Polyfills_Console_setInfoLevel, 1);
|
|
|
|
duk_push_object(ctx);
|
|
duk_push_int(ctx, ILibDuktape_Console_DestinationFlags_DISABLED); duk_put_prop_string(ctx, -2, "DISABLED");
|
|
duk_push_int(ctx, ILibDuktape_Console_DestinationFlags_StdOut); duk_put_prop_string(ctx, -2, "STDOUT");
|
|
duk_push_int(ctx, ILibDuktape_Console_DestinationFlags_ServerConsole); duk_put_prop_string(ctx, -2, "SERVERCONSOLE");
|
|
duk_push_int(ctx, ILibDuktape_Console_DestinationFlags_WebLog); duk_put_prop_string(ctx, -2, "WEBLOG");
|
|
duk_push_int(ctx, ILibDuktape_Console_DestinationFlags_LogFile); duk_put_prop_string(ctx, -2, "LOGFILE");
|
|
ILibDuktape_CreateReadonlyProperty(ctx, "Destinations");
|
|
|
|
duk_push_int(ctx, ILibDuktape_Console_DestinationFlags_StdOut | ILibDuktape_Console_DestinationFlags_LogFile);
|
|
duk_put_prop_string(ctx, -2, ILibDuktape_Console_ERROR_Destination);
|
|
|
|
duk_push_int(ctx, ILibDuktape_Console_DestinationFlags_StdOut | ILibDuktape_Console_DestinationFlags_LogFile);
|
|
duk_put_prop_string(ctx, -2, ILibDuktape_Console_WARN_Destination);
|
|
|
|
duk_push_int(ctx, 0); duk_put_prop_string(ctx, -2, ILibDuktape_Console_INFO_Level);
|
|
|
|
duk_pop(ctx); // [g]
|
|
}
|
|
duk_ret_t ILibDuktape_ntohl(duk_context *ctx)
|
|
{
|
|
duk_size_t bufferLen;
|
|
char *buffer = Duktape_GetBuffer(ctx, 0, &bufferLen);
|
|
int offset = duk_require_int(ctx, 1);
|
|
|
|
if ((int)bufferLen < (4 + offset)) { duk_push_string(ctx, "buffer too small"); duk_throw(ctx); return(DUK_RET_ERROR); }
|
|
duk_push_int(ctx, ntohl(((unsigned int*)(buffer + offset))[0]));
|
|
return 1;
|
|
}
|
|
duk_ret_t ILibDuktape_ntohs(duk_context *ctx)
|
|
{
|
|
duk_size_t bufferLen;
|
|
char *buffer = Duktape_GetBuffer(ctx, 0, &bufferLen);
|
|
int offset = duk_require_int(ctx, 1);
|
|
|
|
if ((int)bufferLen < 2 + offset) { duk_push_string(ctx, "buffer too small"); duk_throw(ctx); return(DUK_RET_ERROR); }
|
|
duk_push_int(ctx, ntohs(((unsigned short*)(buffer + offset))[0]));
|
|
return 1;
|
|
}
|
|
duk_ret_t ILibDuktape_htonl(duk_context *ctx)
|
|
{
|
|
duk_size_t bufferLen;
|
|
char *buffer = Duktape_GetBuffer(ctx, 0, &bufferLen);
|
|
int offset = duk_require_int(ctx, 1);
|
|
unsigned int val = (unsigned int)duk_require_int(ctx, 2);
|
|
|
|
if ((int)bufferLen < (4 + offset)) { duk_push_string(ctx, "buffer too small"); duk_throw(ctx); return(DUK_RET_ERROR); }
|
|
((unsigned int*)(buffer + offset))[0] = htonl(val);
|
|
return 0;
|
|
}
|
|
duk_ret_t ILibDuktape_htons(duk_context *ctx)
|
|
{
|
|
duk_size_t bufferLen;
|
|
char *buffer = Duktape_GetBuffer(ctx, 0, &bufferLen);
|
|
int offset = duk_require_int(ctx, 1);
|
|
unsigned int val = (unsigned int)duk_require_int(ctx, 2);
|
|
|
|
if ((int)bufferLen < (2 + offset)) { duk_push_string(ctx, "buffer too small"); duk_throw(ctx); return(DUK_RET_ERROR); }
|
|
((unsigned short*)(buffer + offset))[0] = htons(val);
|
|
return 0;
|
|
}
|
|
void ILibDuktape_Polyfills_byte_ordering(duk_context *ctx)
|
|
{
|
|
ILibDuktape_CreateInstanceMethod(ctx, "ntohl", ILibDuktape_ntohl, 2);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "ntohs", ILibDuktape_ntohs, 2);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "htonl", ILibDuktape_htonl, 3);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "htons", ILibDuktape_htons, 3);
|
|
}
|
|
|
|
typedef enum ILibDuktape_Timer_Type
|
|
{
|
|
ILibDuktape_Timer_Type_TIMEOUT = 0,
|
|
ILibDuktape_Timer_Type_INTERVAL = 1,
|
|
ILibDuktape_Timer_Type_IMMEDIATE = 2
|
|
}ILibDuktape_Timer_Type;
|
|
typedef struct ILibDuktape_Timer
|
|
{
|
|
duk_context *ctx;
|
|
void *object;
|
|
void *callback;
|
|
void *args;
|
|
int timeout;
|
|
ILibDuktape_Timer_Type timerType;
|
|
}ILibDuktape_Timer;
|
|
|
|
duk_ret_t ILibDuktape_Polyfills_timer_finalizer(duk_context *ctx)
|
|
{
|
|
// Make sure we remove any timers just in case, so we don't leak resources
|
|
ILibDuktape_Timer *ptrs;
|
|
if (duk_has_prop_string(ctx, 0, ILibDuktape_Timer_Ptrs))
|
|
{
|
|
duk_get_prop_string(ctx, 0, ILibDuktape_Timer_Ptrs);
|
|
if (duk_has_prop_string(ctx, 0, "\xFF_callback"))
|
|
{
|
|
duk_del_prop_string(ctx, 0, "\xFF_callback");
|
|
}
|
|
if (duk_has_prop_string(ctx, 0, "\xFF_argArray"))
|
|
{
|
|
duk_del_prop_string(ctx, 0, "\xFF_argArray");
|
|
}
|
|
ptrs = (ILibDuktape_Timer*)Duktape_GetBuffer(ctx, -1, NULL);
|
|
|
|
ILibLifeTime_Remove(ILibGetBaseTimer(Duktape_GetChain(ctx)), ptrs);
|
|
}
|
|
return 0;
|
|
}
|
|
void ILibDuktape_Polyfills_timer_elapsed(void *obj)
|
|
{
|
|
ILibDuktape_Timer *ptrs = (ILibDuktape_Timer*)obj;
|
|
int argCount, i;
|
|
duk_context *ctx = ptrs->ctx;
|
|
char *funcName;
|
|
|
|
duk_push_heapptr(ctx, ptrs->callback); // [func]
|
|
funcName = Duktape_GetStringPropertyValue(ctx, -1, "name", "unknown_method");
|
|
duk_push_heapptr(ctx, ptrs->object); // [func][this]
|
|
duk_push_heapptr(ctx, ptrs->args); // [func][this][argArray]
|
|
|
|
if (ptrs->timerType == ILibDuktape_Timer_Type_INTERVAL)
|
|
{
|
|
ILibLifeTime_AddEx(ILibGetBaseTimer(Duktape_GetChain(ctx)), ptrs, ptrs->timeout, ILibDuktape_Polyfills_timer_elapsed, NULL);
|
|
}
|
|
else
|
|
{
|
|
duk_del_prop_string(ctx, -2, "\xFF_callback");
|
|
duk_del_prop_string(ctx, -2, "\xFF_argArray");
|
|
duk_del_prop_string(ctx, -2, ILibDuktape_Timer_Ptrs);
|
|
}
|
|
|
|
argCount = (int)duk_get_length(ctx, -1);
|
|
for (i = 0; i < argCount; ++i)
|
|
{
|
|
duk_get_prop_index(ctx, -1, i); // [func][this][argArray][arg]
|
|
duk_swap_top(ctx, -2); // [func][this][arg][argArray]
|
|
}
|
|
duk_pop(ctx); // [func][this][...arg...]
|
|
if (duk_pcall_method(ctx, argCount) != 0) { ILibDuktape_Process_UncaughtExceptionEx(ctx, "timers.onElapsed() callback handler on '%s()' ", funcName); }
|
|
duk_pop(ctx); // ...
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_timer_set(duk_context *ctx)
|
|
{
|
|
int nargs = duk_get_top(ctx);
|
|
ILibDuktape_Timer *ptrs;
|
|
ILibDuktape_Timer_Type timerType;
|
|
void *chain = Duktape_GetChain(ctx);
|
|
int argx;
|
|
|
|
duk_push_current_function(ctx);
|
|
duk_get_prop_string(ctx, -1, "type");
|
|
timerType = (ILibDuktape_Timer_Type)duk_get_int(ctx, -1);
|
|
|
|
duk_push_object(ctx); //[retVal]
|
|
switch (timerType)
|
|
{
|
|
case ILibDuktape_Timer_Type_IMMEDIATE:
|
|
ILibDuktape_WriteID(ctx, "Timers.immediate");
|
|
break;
|
|
case ILibDuktape_Timer_Type_INTERVAL:
|
|
ILibDuktape_WriteID(ctx, "Timers.interval");
|
|
break;
|
|
case ILibDuktape_Timer_Type_TIMEOUT:
|
|
ILibDuktape_WriteID(ctx, "Timers.timeout");
|
|
break;
|
|
}
|
|
ILibDuktape_CreateFinalizer(ctx, ILibDuktape_Polyfills_timer_finalizer);
|
|
duk_push_fixed_buffer(ctx, sizeof(ILibDuktape_Timer)); //[retVal][ptrs]
|
|
ptrs = (ILibDuktape_Timer*)Duktape_GetBuffer(ctx, -1, NULL);
|
|
memset(ptrs, 0, sizeof(ILibDuktape_Timer));
|
|
duk_put_prop_string(ctx, -2, ILibDuktape_Timer_Ptrs); //[retVal]
|
|
|
|
ptrs->ctx = ctx;
|
|
ptrs->object = duk_get_heapptr(ctx, -1);
|
|
ptrs->timerType = timerType;
|
|
ptrs->timeout = timerType == ILibDuktape_Timer_Type_IMMEDIATE ? 0 : (int)duk_require_int(ctx, 1);
|
|
ptrs->callback = duk_require_heapptr(ctx, 0);
|
|
|
|
duk_push_array(ctx); //[retVal][argArray]
|
|
for (argx = ILibDuktape_Timer_Type_IMMEDIATE == timerType ? 1 : 2; argx < nargs; ++argx)
|
|
{
|
|
duk_dup(ctx, argx); //[retVal][argArray][arg]
|
|
duk_put_prop_index(ctx, -2, argx - (ILibDuktape_Timer_Type_IMMEDIATE == timerType ? 1 : 2));//[retVal][argArray]
|
|
}
|
|
ptrs->args = duk_get_heapptr(ctx, -1); //[retVal]
|
|
duk_put_prop_string(ctx, -2, "\xFF_argArray");
|
|
|
|
duk_dup(ctx, 0); //[retVal][callback]
|
|
duk_put_prop_string(ctx, -2, "\xFF_callback"); //[retVal]
|
|
|
|
ILibLifeTime_AddEx(ILibGetBaseTimer(chain), ptrs, ptrs->timeout, ILibDuktape_Polyfills_timer_elapsed, NULL);
|
|
return 1;
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_timer_clear(duk_context *ctx)
|
|
{
|
|
ILibDuktape_Timer *ptrs;
|
|
ILibDuktape_Timer_Type timerType;
|
|
|
|
duk_push_current_function(ctx);
|
|
duk_get_prop_string(ctx, -1, "type");
|
|
timerType = (ILibDuktape_Timer_Type)duk_get_int(ctx, -1);
|
|
|
|
if(!duk_has_prop_string(ctx, 0, ILibDuktape_Timer_Ptrs))
|
|
{
|
|
switch (timerType)
|
|
{
|
|
case ILibDuktape_Timer_Type_TIMEOUT:
|
|
return(ILibDuktape_Error(ctx, "timers.clearTimeout(): Invalid Parameter"));
|
|
case ILibDuktape_Timer_Type_INTERVAL:
|
|
return(ILibDuktape_Error(ctx, "timers.clearInterval(): Invalid Parameter"));
|
|
case ILibDuktape_Timer_Type_IMMEDIATE:
|
|
return(ILibDuktape_Error(ctx, "timers.clearImmediate(): Invalid Parameter"));
|
|
}
|
|
}
|
|
|
|
duk_get_prop_string(ctx, 0, ILibDuktape_Timer_Ptrs);
|
|
ptrs = (ILibDuktape_Timer*)Duktape_GetBuffer(ctx, -1, NULL);
|
|
|
|
ILibLifeTime_Remove(ILibGetBaseTimer(Duktape_GetChain(ctx)), ptrs);
|
|
return 0;
|
|
}
|
|
void ILibDuktape_Polyfills_timer(duk_context *ctx)
|
|
{
|
|
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "type", ILibDuktape_Timer_Type_TIMEOUT, "setTimeout", ILibDuktape_Polyfills_timer_set, DUK_VARARGS);
|
|
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "type", ILibDuktape_Timer_Type_INTERVAL, "setInterval", ILibDuktape_Polyfills_timer_set, DUK_VARARGS);
|
|
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "type", ILibDuktape_Timer_Type_IMMEDIATE, "setImmediate", ILibDuktape_Polyfills_timer_set, DUK_VARARGS);
|
|
|
|
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "type", ILibDuktape_Timer_Type_TIMEOUT, "clearTimeout", ILibDuktape_Polyfills_timer_clear, 1);
|
|
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "type", ILibDuktape_Timer_Type_INTERVAL, "clearInterval", ILibDuktape_Polyfills_timer_clear, 1);
|
|
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "type", ILibDuktape_Timer_Type_IMMEDIATE, "clearImmediate", ILibDuktape_Polyfills_timer_clear, 1);
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_getJSModule(duk_context *ctx)
|
|
{
|
|
if (ILibDuktape_ModSearch_GetJSModule(ctx, (char*)duk_require_string(ctx, 0)) == 0)
|
|
{
|
|
return(ILibDuktape_Error(ctx, "getJSModule(): (%s) not found", (char*)duk_require_string(ctx, 0)));
|
|
}
|
|
return(1);
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_addModule(duk_context *ctx)
|
|
{
|
|
duk_size_t moduleLen;
|
|
char *module = (char*)Duktape_GetBuffer(ctx, 1, &moduleLen);
|
|
char *moduleName = (char*)duk_require_string(ctx, 0);
|
|
|
|
if (ILibDuktape_ModSearch_AddModule(ctx, moduleName, module, (int)moduleLen) != 0)
|
|
{
|
|
return(ILibDuktape_Error(ctx, "Cannot add module: %s", moduleName));
|
|
}
|
|
return(0);
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_addModuleObject(duk_context *ctx)
|
|
{
|
|
void *module = duk_require_heapptr(ctx, 1);
|
|
char *moduleName = (char*)duk_require_string(ctx, 0);
|
|
|
|
ILibDuktape_ModSearch_AddModuleObject(ctx, moduleName, module);
|
|
return(0);
|
|
}
|
|
duk_ret_t ILibDuktape_Queue_Finalizer(duk_context *ctx)
|
|
{
|
|
duk_get_prop_string(ctx, 0, ILibDuktape_Queue_Ptr);
|
|
ILibQueue_Destroy((ILibQueue)duk_get_pointer(ctx, -1));
|
|
return(0);
|
|
}
|
|
duk_ret_t ILibDuktape_Queue_EnQueue(duk_context *ctx)
|
|
{
|
|
ILibQueue Q;
|
|
int i;
|
|
int nargs = duk_get_top(ctx);
|
|
duk_push_this(ctx); // [queue]
|
|
duk_get_prop_string(ctx, -1, ILibDuktape_Queue_Ptr); // [queue][ptr]
|
|
Q = (ILibQueue)duk_get_pointer(ctx, -1);
|
|
duk_pop(ctx); // [queue]
|
|
|
|
ILibDuktape_Push_ObjectStash(ctx); // [queue][stash]
|
|
duk_push_array(ctx); // [queue][stash][array]
|
|
for (i = 0; i < nargs; ++i)
|
|
{
|
|
duk_dup(ctx, i); // [queue][stash][array][arg]
|
|
duk_put_prop_index(ctx, -2, i); // [queue][stash][array]
|
|
}
|
|
ILibQueue_EnQueue(Q, duk_get_heapptr(ctx, -1));
|
|
duk_put_prop_string(ctx, -2, Duktape_GetStashKey(duk_get_heapptr(ctx, -1))); // [queue][stash]
|
|
return(0);
|
|
}
|
|
duk_ret_t ILibDuktape_Queue_DeQueue(duk_context *ctx)
|
|
{
|
|
duk_push_current_function(ctx);
|
|
duk_get_prop_string(ctx, -1, "peek");
|
|
int peek = duk_get_int(ctx, -1);
|
|
|
|
duk_push_this(ctx); // [Q]
|
|
duk_get_prop_string(ctx, -1, ILibDuktape_Queue_Ptr); // [Q][ptr]
|
|
ILibQueue Q = (ILibQueue)duk_get_pointer(ctx, -1);
|
|
void *h = peek == 0 ? ILibQueue_DeQueue(Q) : ILibQueue_PeekQueue(Q);
|
|
if (h == NULL) { return(ILibDuktape_Error(ctx, "Queue is empty")); }
|
|
duk_pop(ctx); // [Q]
|
|
ILibDuktape_Push_ObjectStash(ctx); // [Q][stash]
|
|
duk_push_heapptr(ctx, h); // [Q][stash][array]
|
|
int length = (int)duk_get_length(ctx, -1);
|
|
int i;
|
|
for (i = 0; i < length; ++i)
|
|
{
|
|
duk_get_prop_index(ctx, -i - 1, i); // [Q][stash][array][args]
|
|
}
|
|
if (peek == 0) { duk_del_prop_string(ctx, -length - 2, Duktape_GetStashKey(h)); }
|
|
return(length);
|
|
}
|
|
duk_ret_t ILibDuktape_Queue_isEmpty(duk_context *ctx)
|
|
{
|
|
duk_push_this(ctx);
|
|
duk_push_boolean(ctx, ILibQueue_IsEmpty((ILibQueue)Duktape_GetPointerProperty(ctx, -1, ILibDuktape_Queue_Ptr)));
|
|
return(1);
|
|
}
|
|
duk_ret_t ILibDuktape_Queue_new(duk_context *ctx)
|
|
{
|
|
duk_push_object(ctx); // [queue]
|
|
duk_push_pointer(ctx, ILibQueue_Create()); // [queue][ptr]
|
|
duk_put_prop_string(ctx, -2, ILibDuktape_Queue_Ptr); // [queue]
|
|
|
|
ILibDuktape_CreateFinalizer(ctx, ILibDuktape_Queue_Finalizer);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "enQueue", ILibDuktape_Queue_EnQueue, DUK_VARARGS);
|
|
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "peek", 0, "deQueue", ILibDuktape_Queue_DeQueue, DUK_VARARGS);
|
|
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "peek", 1, "peekQueue", ILibDuktape_Queue_DeQueue, DUK_VARARGS);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "isEmpty", ILibDuktape_Queue_isEmpty, 0);
|
|
|
|
return(1);
|
|
}
|
|
void ILibDuktape_Queue_Push(duk_context *ctx, void* chain)
|
|
{
|
|
duk_push_c_function(ctx, ILibDuktape_Queue_new, 0);
|
|
}
|
|
|
|
typedef struct ILibDuktape_DynamicBuffer_data
|
|
{
|
|
int start;
|
|
int end;
|
|
int unshiftBytes;
|
|
char *buffer;
|
|
int bufferLen;
|
|
}ILibDuktape_DynamicBuffer_data;
|
|
|
|
typedef struct ILibDuktape_DynamicBuffer_ContextSwitchData
|
|
{
|
|
void *chain;
|
|
void *heapptr;
|
|
ILibDuktape_DuplexStream *stream;
|
|
ILibDuktape_DynamicBuffer_data *data;
|
|
int bufferLen;
|
|
char buffer[];
|
|
}ILibDuktape_DynamicBuffer_ContextSwitchData;
|
|
|
|
ILibTransport_DoneState ILibDuktape_DynamicBuffer_WriteSink(ILibDuktape_DuplexStream *stream, char *buffer, int bufferLen, void *user);
|
|
void ILibDuktape_DynamicBuffer_WriteSink_ChainThread(void *chain, void *user)
|
|
{
|
|
ILibDuktape_DynamicBuffer_ContextSwitchData *data = (ILibDuktape_DynamicBuffer_ContextSwitchData*)user;
|
|
if(ILibMemory_CanaryOK(data->stream))
|
|
{
|
|
ILibDuktape_DynamicBuffer_WriteSink(data->stream, data->buffer, data->bufferLen, data->data);
|
|
ILibDuktape_DuplexStream_Ready(data->stream);
|
|
}
|
|
free(user);
|
|
}
|
|
ILibTransport_DoneState ILibDuktape_DynamicBuffer_WriteSink(ILibDuktape_DuplexStream *stream, char *buffer, int bufferLen, void *user)
|
|
{
|
|
ILibDuktape_DynamicBuffer_data *data = (ILibDuktape_DynamicBuffer_data*)user;
|
|
if (ILibIsRunningOnChainThread(stream->readableStream->chain) == 0)
|
|
{
|
|
ILibDuktape_DynamicBuffer_ContextSwitchData *tmp = (ILibDuktape_DynamicBuffer_ContextSwitchData*)ILibMemory_Allocate(sizeof(ILibDuktape_DynamicBuffer_ContextSwitchData) + bufferLen, 0, NULL, NULL);
|
|
tmp->chain = stream->readableStream->chain;
|
|
tmp->heapptr = stream->ParentObject;
|
|
tmp->stream = stream;
|
|
tmp->data = data;
|
|
tmp->bufferLen = bufferLen;
|
|
memcpy_s(tmp->buffer, bufferLen, buffer, bufferLen);
|
|
ILibChain_RunOnMicrostackThread(tmp->chain, ILibDuktape_DynamicBuffer_WriteSink_ChainThread, tmp);
|
|
return(ILibTransport_DoneState_INCOMPLETE);
|
|
}
|
|
|
|
|
|
if ((data->bufferLen - data->start - data->end) < bufferLen)
|
|
{
|
|
if (data->end > 0)
|
|
{
|
|
// Move the buffer first
|
|
memmove_s(data->buffer, data->bufferLen, data->buffer + data->start, data->end);
|
|
data->start = 0;
|
|
}
|
|
if ((data->bufferLen - data->end) < bufferLen)
|
|
{
|
|
// Need to resize buffer first
|
|
int tmpSize = data->bufferLen;
|
|
while ((tmpSize - data->end) < bufferLen)
|
|
{
|
|
tmpSize += 4096;
|
|
}
|
|
if ((data->buffer = (char*)realloc(data->buffer, tmpSize)) == NULL) { ILIBCRITICALEXIT(254); }
|
|
data->bufferLen = tmpSize;
|
|
}
|
|
}
|
|
|
|
|
|
memcpy_s(data->buffer + data->start + data->end, data->bufferLen - data->start - data->end, buffer, bufferLen);
|
|
data->end += bufferLen;
|
|
|
|
int unshifted = 0;
|
|
do
|
|
{
|
|
duk_push_heapptr(stream->readableStream->ctx, stream->ParentObject); // [ds]
|
|
duk_get_prop_string(stream->readableStream->ctx, -1, "emit"); // [ds][emit]
|
|
duk_swap_top(stream->readableStream->ctx, -2); // [emit][this]
|
|
duk_push_string(stream->readableStream->ctx, "readable"); // [emit][this][readable]
|
|
if (duk_pcall_method(stream->readableStream->ctx, 1) != 0) { ILibDuktape_Process_UncaughtExceptionEx(stream->readableStream->ctx, "DynamicBuffer.WriteSink => readable(): "); }
|
|
duk_pop(stream->readableStream->ctx); // ...
|
|
|
|
ILibDuktape_DuplexStream_WriteData(stream, data->buffer + data->start, data->end);
|
|
if (data->unshiftBytes == 0)
|
|
{
|
|
// All the data was consumed
|
|
data->start = data->end = 0;
|
|
}
|
|
else
|
|
{
|
|
unshifted = (data->end - data->unshiftBytes);
|
|
if (unshifted > 0)
|
|
{
|
|
data->start += unshifted;
|
|
data->end = data->unshiftBytes;
|
|
data->unshiftBytes = 0;
|
|
}
|
|
}
|
|
} while (unshifted != 0);
|
|
|
|
return(ILibTransport_DoneState_COMPLETE);
|
|
}
|
|
void ILibDuktape_DynamicBuffer_EndSink(ILibDuktape_DuplexStream *stream, void *user)
|
|
{
|
|
ILibDuktape_DuplexStream_WriteEnd(stream);
|
|
}
|
|
duk_ret_t ILibDuktape_DynamicBuffer_Finalizer(duk_context *ctx)
|
|
{
|
|
duk_get_prop_string(ctx, 0, "\xFF_buffer");
|
|
ILibDuktape_DynamicBuffer_data *data = (ILibDuktape_DynamicBuffer_data*)Duktape_GetBuffer(ctx, -1, NULL);
|
|
free(data->buffer);
|
|
return(0);
|
|
}
|
|
|
|
int ILibDuktape_DynamicBuffer_unshift(ILibDuktape_DuplexStream *sender, int unshiftBytes, void *user)
|
|
{
|
|
ILibDuktape_DynamicBuffer_data *data = (ILibDuktape_DynamicBuffer_data*)user;
|
|
data->unshiftBytes = unshiftBytes;
|
|
return(unshiftBytes);
|
|
}
|
|
duk_ret_t ILibDuktape_DynamicBuffer_read(duk_context *ctx)
|
|
{
|
|
ILibDuktape_DynamicBuffer_data *data;
|
|
duk_push_this(ctx); // [DynamicBuffer]
|
|
duk_get_prop_string(ctx, -1, "\xFF_buffer"); // [DynamicBuffer][buffer]
|
|
data = (ILibDuktape_DynamicBuffer_data*)Duktape_GetBuffer(ctx, -1, NULL);
|
|
duk_push_external_buffer(ctx); // [DynamicBuffer][buffer][extBuffer]
|
|
duk_config_buffer(ctx, -1, data->buffer + data->start, data->bufferLen - (data->start + data->end));
|
|
duk_push_buffer_object(ctx, -1, 0, data->bufferLen - (data->start + data->end), DUK_BUFOBJ_NODEJS_BUFFER);
|
|
return(1);
|
|
}
|
|
duk_ret_t ILibDuktape_DynamicBuffer_new(duk_context *ctx)
|
|
{
|
|
ILibDuktape_DynamicBuffer_data *data;
|
|
int initSize = 4096;
|
|
if (duk_get_top(ctx) != 0)
|
|
{
|
|
initSize = duk_require_int(ctx, 0);
|
|
}
|
|
|
|
duk_push_object(ctx); // [stream]
|
|
duk_push_fixed_buffer(ctx, sizeof(ILibDuktape_DynamicBuffer_data));
|
|
data = (ILibDuktape_DynamicBuffer_data*)Duktape_GetBuffer(ctx, -1, NULL);
|
|
memset(data, 0, sizeof(ILibDuktape_DynamicBuffer_data));
|
|
duk_put_prop_string(ctx, -2, "\xFF_buffer");
|
|
|
|
data->bufferLen = initSize;
|
|
data->buffer = (char*)malloc(initSize);
|
|
|
|
ILibDuktape_DuplexStream_InitEx(ctx, ILibDuktape_DynamicBuffer_WriteSink, ILibDuktape_DynamicBuffer_EndSink, NULL, NULL, ILibDuktape_DynamicBuffer_unshift, data);
|
|
ILibDuktape_EventEmitter_CreateEventEx(ILibDuktape_EventEmitter_GetEmitter(ctx, -1), "readable");
|
|
ILibDuktape_CreateInstanceMethod(ctx, "read", ILibDuktape_DynamicBuffer_read, DUK_VARARGS);
|
|
ILibDuktape_CreateFinalizer(ctx, ILibDuktape_DynamicBuffer_Finalizer);
|
|
|
|
return(1);
|
|
}
|
|
|
|
void ILibDuktape_DynamicBuffer_Push(duk_context *ctx, void *chain)
|
|
{
|
|
duk_push_c_function(ctx, ILibDuktape_DynamicBuffer_new, DUK_VARARGS);
|
|
}
|
|
|
|
duk_ret_t ILibDuktape_Polyfills_debugCrash(duk_context *ctx)
|
|
{
|
|
void *p = NULL;
|
|
((int*)p)[0] = 55;
|
|
return(0);
|
|
}
|
|
|
|
|
|
void ILibDuktape_Stream_PauseSink(struct ILibDuktape_readableStream *sender, void *user)
|
|
{
|
|
}
|
|
void ILibDuktape_Stream_ResumeSink(struct ILibDuktape_readableStream *sender, void *user)
|
|
{
|
|
int skip = 0;
|
|
duk_size_t bufferLen;
|
|
|
|
duk_push_heapptr(sender->ctx, sender->object); // [stream]
|
|
void *func = Duktape_GetHeapptrProperty(sender->ctx, -1, "_read");
|
|
duk_pop(sender->ctx); // ...
|
|
|
|
while (func != NULL && sender->paused == 0)
|
|
{
|
|
duk_push_heapptr(sender->ctx, sender->object); // [this]
|
|
if (!skip && duk_has_prop_string(sender->ctx, -1, ILibDuktape_Stream_Buffer))
|
|
{
|
|
duk_get_prop_string(sender->ctx, -1, ILibDuktape_Stream_Buffer); // [this][buffer]
|
|
if ((bufferLen = duk_get_length(sender->ctx, -1)) > 0)
|
|
{
|
|
// Buffer is not empty, so we need to 'PUSH' it
|
|
duk_get_prop_string(sender->ctx, -2, "push"); // [this][buffer][push]
|
|
duk_dup(sender->ctx, -3); // [this][buffer][push][this]
|
|
duk_dup(sender->ctx, -3); // [this][buffer][push][this][buffer]
|
|
duk_remove(sender->ctx, -4); // [this][push][this][buffer]
|
|
duk_call_method(sender->ctx, 1); // [this][boolean]
|
|
sender->paused = !duk_get_boolean(sender->ctx, -1);
|
|
duk_pop(sender->ctx); // [this]
|
|
|
|
if (duk_has_prop_string(sender->ctx, -1, ILibDuktape_Stream_Buffer))
|
|
{
|
|
duk_get_prop_string(sender->ctx, -1, ILibDuktape_Stream_Buffer); // [this][buffer]
|
|
if (duk_get_length(sender->ctx, -1) == bufferLen)
|
|
{
|
|
// All the data was unshifted
|
|
skip = !sender->paused;
|
|
}
|
|
duk_pop(sender->ctx); // [this]
|
|
}
|
|
duk_pop(sender->ctx); // ...
|
|
}
|
|
else
|
|
{
|
|
// Buffer is empty
|
|
duk_pop(sender->ctx); // [this]
|
|
duk_del_prop_string(sender->ctx, -1, ILibDuktape_Stream_Buffer);
|
|
duk_pop(sender->ctx); // ...
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// We need to 'read' more data
|
|
duk_push_heapptr(sender->ctx, func); // [this][read]
|
|
duk_swap_top(sender->ctx, -2); // [read][this]
|
|
if (duk_pcall_method(sender->ctx, 0) != 0) { ILibDuktape_Process_UncaughtException(sender->ctx); duk_pop(sender->ctx); break; }
|
|
// // [buffer]
|
|
duk_push_heapptr(sender->ctx, sender->object); // [buffer][this]
|
|
duk_swap_top(sender->ctx, -2); // [this][buffer]
|
|
if (duk_has_prop_string(sender->ctx, -2, ILibDuktape_Stream_Buffer))
|
|
{
|
|
duk_push_global_object(sender->ctx); // [this][buffer][g]
|
|
duk_get_prop_string(sender->ctx, -1, "Buffer"); // [this][buffer][g][Buffer]
|
|
duk_remove(sender->ctx, -2); // [this][buffer][Buffer]
|
|
duk_get_prop_string(sender->ctx, -1, "concat"); // [this][buffer][Buffer][concat]
|
|
duk_swap_top(sender->ctx, -2); // [this][buffer][concat][this]
|
|
duk_push_array(sender->ctx); // [this][buffer][concat][this][Array]
|
|
duk_get_prop_string(sender->ctx, -1, "push"); // [this][buffer][concat][this][Array][push]
|
|
duk_dup(sender->ctx, -2); // [this][buffer][concat][this][Array][push][this]
|
|
duk_get_prop_string(sender->ctx, -7, ILibDuktape_Stream_Buffer); // [this][buffer][concat][this][Array][push][this][buffer]
|
|
duk_call_method(sender->ctx, 1); duk_pop(sender->ctx); // [this][buffer][concat][this][Array]
|
|
duk_get_prop_string(sender->ctx, -1, "push"); // [this][buffer][concat][this][Array][push]
|
|
duk_dup(sender->ctx, -2); // [this][buffer][concat][this][Array][push][this]
|
|
duk_dup(sender->ctx, -6); // [this][buffer][concat][this][Array][push][this][buffer]
|
|
duk_remove(sender->ctx, -7); // [this][concat][this][Array][push][this][buffer]
|
|
duk_call_method(sender->ctx, 1); duk_pop(sender->ctx); // [this][concat][this][Array]
|
|
duk_call_method(sender->ctx, 1); // [this][buffer]
|
|
}
|
|
duk_put_prop_string(sender->ctx, -2, ILibDuktape_Stream_Buffer); // [this]
|
|
duk_pop(sender->ctx); // ...
|
|
skip = 0;
|
|
}
|
|
}
|
|
}
|
|
int ILibDuktape_Stream_UnshiftSink(struct ILibDuktape_readableStream *sender, int unshiftBytes, void *user)
|
|
{
|
|
duk_push_fixed_buffer(sender->ctx, unshiftBytes); // [buffer]
|
|
memcpy_s(Duktape_GetBuffer(sender->ctx, -1, NULL), unshiftBytes, sender->unshiftReserved, unshiftBytes);
|
|
duk_push_heapptr(sender->ctx, sender->object); // [buffer][stream]
|
|
duk_push_buffer_object(sender->ctx, -2, 0, unshiftBytes, DUK_BUFOBJ_NODEJS_BUFFER); // [buffer][stream][buffer]
|
|
duk_put_prop_string(sender->ctx, -2, ILibDuktape_Stream_Buffer); // [buffer][stream]
|
|
duk_pop_2(sender->ctx); // ...
|
|
|
|
return(unshiftBytes);
|
|
}
|
|
duk_ret_t ILibDuktape_Stream_Push(duk_context *ctx)
|
|
{
|
|
duk_push_this(ctx); // [stream]
|
|
ILibDuktape_readableStream *RS = (ILibDuktape_readableStream*)Duktape_GetPointerProperty(ctx, -1, ILibDuktape_Stream_ReadablePtr);
|
|
|
|
duk_size_t bufferLen;
|
|
char *buffer = (char*)Duktape_GetBuffer(ctx, 0, &bufferLen);
|
|
|
|
duk_push_boolean(ctx, !ILibDuktape_readableStream_WriteDataEx(RS, 0, buffer, (int)bufferLen)); // [stream][buffer][retVal]
|
|
return(1);
|
|
}
|
|
duk_ret_t ILibDuktape_Stream_EndSink(duk_context *ctx)
|
|
{
|
|
duk_push_this(ctx); // [stream]
|
|
ILibDuktape_readableStream *RS = (ILibDuktape_readableStream*)Duktape_GetPointerProperty(ctx, -1, ILibDuktape_Stream_ReadablePtr);
|
|
ILibDuktape_readableStream_WriteEnd(RS);
|
|
return(0);
|
|
}
|
|
duk_idx_t ILibDuktape_Stream_newReadable(duk_context *ctx)
|
|
{
|
|
ILibDuktape_readableStream *RS;
|
|
duk_push_object(ctx); // [Readable]
|
|
ILibDuktape_WriteID(ctx, "stream.readable");
|
|
RS = ILibDuktape_ReadableStream_InitEx(ctx, ILibDuktape_Stream_PauseSink, ILibDuktape_Stream_ResumeSink, ILibDuktape_Stream_UnshiftSink, NULL);
|
|
RS->paused = 1;
|
|
|
|
duk_push_pointer(ctx, RS);
|
|
duk_put_prop_string(ctx, -2, ILibDuktape_Stream_ReadablePtr);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "push", ILibDuktape_Stream_Push, DUK_VARARGS);
|
|
ILibDuktape_EventEmitter_AddOnceEx3(ctx, -1, "end", ILibDuktape_Stream_EndSink);
|
|
|
|
if (duk_is_object(ctx, 0))
|
|
{
|
|
void *h = Duktape_GetHeapptrProperty(ctx, 0, "read");
|
|
if (h != NULL) { duk_push_heapptr(ctx, h); duk_put_prop_string(ctx, -2, "_read"); }
|
|
}
|
|
return(1);
|
|
}
|
|
duk_ret_t ILibDuktape_Stream_Writable_WriteSink_Flush(duk_context *ctx)
|
|
{
|
|
duk_push_current_function(ctx);
|
|
ILibTransport_DoneState *retVal = (ILibTransport_DoneState*)Duktape_GetPointerProperty(ctx, -1, "retval");
|
|
if (retVal != NULL)
|
|
{
|
|
*retVal = ILibTransport_DoneState_COMPLETE;
|
|
}
|
|
else
|
|
{
|
|
duk_push_this(ctx);
|
|
ILibDuktape_WritableStream *WS = (ILibDuktape_WritableStream*)Duktape_GetPointerProperty(ctx, -1, ILibDuktape_Stream_WritablePtr);
|
|
ILibDuktape_WritableStream_Ready(WS);
|
|
}
|
|
return(0);
|
|
}
|
|
ILibTransport_DoneState ILibDuktape_Stream_Writable_WriteSink(struct ILibDuktape_WritableStream *stream, char *buffer, int bufferLen, void *user)
|
|
{
|
|
void *h;
|
|
ILibTransport_DoneState retVal = ILibTransport_DoneState_INCOMPLETE;
|
|
|
|
duk_push_this(stream->ctx); // [writable]
|
|
duk_get_prop_string(stream->ctx, -1, "_write"); // [writable][_write]
|
|
duk_swap_top(stream->ctx, -2); // [_write][this]
|
|
if (stream->Reserved)
|
|
{
|
|
duk_push_external_buffer(stream->ctx); // [_write][this][extBuffer]
|
|
duk_insert(stream->ctx, -3); // [extBuffer][_write][this]
|
|
duk_config_buffer(stream->ctx, -3, buffer, (duk_size_t)bufferLen);
|
|
duk_push_buffer_object(stream->ctx, -3, 0, (duk_size_t)bufferLen, DUK_BUFOBJ_NODEJS_BUFFER);// [extBuffer][_write][this][buffer]
|
|
}
|
|
else
|
|
{
|
|
duk_push_lstring(stream->ctx, buffer, (duk_size_t)bufferLen); // [_write][this][string]
|
|
}
|
|
duk_push_c_function(stream->ctx, ILibDuktape_Stream_Writable_WriteSink_Flush, DUK_VARARGS); // [_write][this][string/buffer][callback]
|
|
h = duk_get_heapptr(stream->ctx, -1);
|
|
duk_push_heap_stash(stream->ctx); // [_write][this][string/buffer][callback][stash]
|
|
duk_dup(stream->ctx, -2); // [_write][this][string/buffer][callback][stash][callback]
|
|
duk_put_prop_string(stream->ctx, -2, Duktape_GetStashKey(h)); // [_write][this][string/buffer][callback][stash]
|
|
duk_pop(stream->ctx); // [_write][this][string/buffer][callback]
|
|
|
|
duk_push_pointer(stream->ctx, &retVal); // [_write][this][string/buffer][callback][retval]
|
|
duk_put_prop_string(stream->ctx, -2, "retval"); // [_write][this][string/buffer][callback]
|
|
if (duk_pcall_method(stream->ctx, 2) != 0)
|
|
{
|
|
ILibDuktape_Process_UncaughtExceptionEx(stream->ctx, "stream.writable.write(): "); retVal = ILibTransport_DoneState_ERROR;
|
|
}
|
|
duk_pop(stream->ctx); // ...
|
|
|
|
duk_push_heapptr(stream->ctx, h); // [callback]
|
|
duk_del_prop_string(stream->ctx, -1, "retval");
|
|
duk_pop(stream->ctx); // ...
|
|
|
|
duk_push_heap_stash(stream->ctx);
|
|
duk_del_prop_string(stream->ctx, -1, Duktape_GetStashKey(h));
|
|
duk_pop(stream->ctx);
|
|
|
|
return(retVal);
|
|
}
|
|
void ILibDuktape_Stream_Writable_EndSink(struct ILibDuktape_WritableStream *stream, void *user)
|
|
{
|
|
duk_push_this(stream->ctx); // [writable]
|
|
duk_get_prop_string(stream->ctx, -1, "_final"); // [writable][_final]
|
|
duk_swap_top(stream->ctx, -2); // [_final][this]
|
|
if (duk_pcall_method(stream->ctx, 0) != 0) { ILibDuktape_Process_UncaughtExceptionEx(stream->ctx, "stream.writable._final(): "); }
|
|
duk_pop(stream->ctx); // ...
|
|
}
|
|
duk_ret_t ILibDuktape_Stream_newWritable(duk_context *ctx)
|
|
{
|
|
ILibDuktape_WritableStream *WS;
|
|
duk_push_object(ctx); // [Writable]
|
|
ILibDuktape_WriteID(ctx, "stream.writable");
|
|
WS = ILibDuktape_WritableStream_Init(ctx, ILibDuktape_Stream_Writable_WriteSink, ILibDuktape_Stream_Writable_EndSink, NULL);
|
|
WS->JSCreated = 1;
|
|
|
|
duk_push_pointer(ctx, WS);
|
|
duk_put_prop_string(ctx, -2, ILibDuktape_Stream_WritablePtr);
|
|
|
|
if (duk_is_object(ctx, 0))
|
|
{
|
|
void *h = Duktape_GetHeapptrProperty(ctx, 0, "write");
|
|
if (h != NULL) { duk_push_heapptr(ctx, h); duk_put_prop_string(ctx, -2, "_write"); }
|
|
h = Duktape_GetHeapptrProperty(ctx, 0, "final");
|
|
if (h != NULL) { duk_push_heapptr(ctx, h); duk_put_prop_string(ctx, -2, "_final"); }
|
|
}
|
|
return(1);
|
|
}
|
|
void ILibDuktape_Stream_Duplex_PauseSink(ILibDuktape_DuplexStream *stream, void *user)
|
|
{
|
|
ILibDuktape_Stream_PauseSink(stream->readableStream, user);
|
|
}
|
|
void ILibDuktape_Stream_Duplex_ResumeSink(ILibDuktape_DuplexStream *stream, void *user)
|
|
{
|
|
ILibDuktape_Stream_ResumeSink(stream->readableStream, user);
|
|
}
|
|
int ILibDuktape_Stream_Duplex_UnshiftSink(ILibDuktape_DuplexStream *stream, int unshiftBytes, void *user)
|
|
{
|
|
return(ILibDuktape_Stream_UnshiftSink(stream->readableStream, unshiftBytes, user));
|
|
}
|
|
ILibTransport_DoneState ILibDuktape_Stream_Duplex_WriteSink(ILibDuktape_DuplexStream *stream, char *buffer, int bufferLen, void *user)
|
|
{
|
|
return(ILibDuktape_Stream_Writable_WriteSink(stream->writableStream, buffer, bufferLen, user));
|
|
}
|
|
void ILibDuktape_Stream_Duplex_EndSink(ILibDuktape_DuplexStream *stream, void *user)
|
|
{
|
|
ILibDuktape_Stream_Writable_EndSink(stream->writableStream, user);
|
|
}
|
|
duk_ret_t ILibDuktape_Stream_newDuplex(duk_context *ctx)
|
|
{
|
|
ILibDuktape_DuplexStream *DS;
|
|
duk_push_object(ctx); // [Duplex]
|
|
ILibDuktape_WriteID(ctx, "stream.Duplex");
|
|
DS = ILibDuktape_DuplexStream_InitEx(ctx, ILibDuktape_Stream_Duplex_WriteSink, ILibDuktape_Stream_Duplex_EndSink, ILibDuktape_Stream_Duplex_PauseSink, ILibDuktape_Stream_Duplex_ResumeSink, ILibDuktape_Stream_Duplex_UnshiftSink, NULL);
|
|
DS->writableStream->JSCreated = 1;
|
|
|
|
duk_push_pointer(ctx, DS->writableStream);
|
|
duk_put_prop_string(ctx, -2, ILibDuktape_Stream_WritablePtr);
|
|
|
|
duk_push_pointer(ctx, DS->readableStream);
|
|
duk_put_prop_string(ctx, -2, ILibDuktape_Stream_ReadablePtr);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "push", ILibDuktape_Stream_Push, DUK_VARARGS);
|
|
ILibDuktape_EventEmitter_AddOnceEx3(ctx, -1, "end", ILibDuktape_Stream_EndSink);
|
|
|
|
if (duk_is_object(ctx, 0))
|
|
{
|
|
void *h = Duktape_GetHeapptrProperty(ctx, 0, "write");
|
|
if (h != NULL) { duk_push_heapptr(ctx, h); duk_put_prop_string(ctx, -2, "_write"); }
|
|
h = Duktape_GetHeapptrProperty(ctx, 0, "final");
|
|
if (h != NULL) { duk_push_heapptr(ctx, h); duk_put_prop_string(ctx, -2, "_final"); }
|
|
h = Duktape_GetHeapptrProperty(ctx, 0, "read");
|
|
if (h != NULL) { duk_push_heapptr(ctx, h); duk_put_prop_string(ctx, -2, "_read"); }
|
|
}
|
|
return(1);
|
|
}
|
|
void ILibDuktape_Stream_Init(duk_context *ctx, void *chain)
|
|
{
|
|
duk_push_object(ctx); // [stream
|
|
ILibDuktape_WriteID(ctx, "stream");
|
|
ILibDuktape_CreateInstanceMethod(ctx, "Readable", ILibDuktape_Stream_newReadable, DUK_VARARGS);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "Writable", ILibDuktape_Stream_newWritable, DUK_VARARGS);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "Duplex", ILibDuktape_Stream_newDuplex, DUK_VARARGS);
|
|
}
|
|
void ILibDuktape_Polyfills_debugGC2(duk_context *ctx, void ** args, int argsLen)
|
|
{
|
|
if (g_displayFinalizerMessages) { printf("=> GC();\n"); }
|
|
duk_gc(ctx, 0);
|
|
duk_gc(ctx, 0);
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_debugGC(duk_context *ctx)
|
|
{
|
|
ILibDuktape_Immediate(ctx, (void*[]) { NULL }, 0, ILibDuktape_Polyfills_debugGC2);
|
|
return(0);
|
|
}
|
|
duk_ret_t ILibDuktape_Polyfills_debug(duk_context *ctx)
|
|
{
|
|
#ifdef WIN32
|
|
if (IsDebuggerPresent()) { __debugbreak(); }
|
|
#elif defined(_POSIX)
|
|
raise(SIGTRAP);
|
|
#endif
|
|
return(0);
|
|
}
|
|
#ifndef MICROSTACK_NOTLS
|
|
duk_ret_t ILibDuktape_PKCS7_getSignedDataBlock(duk_context *ctx)
|
|
{
|
|
char *hash = ILibMemory_AllocateA(UTIL_SHA256_HASHSIZE);
|
|
char *pkeyHash = ILibMemory_AllocateA(UTIL_SHA256_HASHSIZE);
|
|
unsigned int size, r;
|
|
BIO *out = NULL;
|
|
PKCS7 *message = NULL;
|
|
char* data2 = NULL;
|
|
STACK_OF(X509) *st = NULL;
|
|
|
|
duk_size_t bufferLen;
|
|
char *buffer = Duktape_GetBuffer(ctx, 0, &bufferLen);
|
|
|
|
message = d2i_PKCS7(NULL, (const unsigned char**)&buffer, (long)bufferLen);
|
|
if (message == NULL) { return(ILibDuktape_Error(ctx, "PKCS7 Error")); }
|
|
|
|
// Lets rebuild the original message and check the size
|
|
size = i2d_PKCS7(message, NULL);
|
|
if (size < (unsigned int)bufferLen) { PKCS7_free(message); return(ILibDuktape_Error(ctx, "PKCS7 Error")); }
|
|
|
|
out = BIO_new(BIO_s_mem());
|
|
|
|
// Check the PKCS7 signature, but not the certificate chain.
|
|
r = PKCS7_verify(message, NULL, NULL, NULL, out, PKCS7_NOVERIFY);
|
|
if (r == 0) { PKCS7_free(message); BIO_free(out); return(ILibDuktape_Error(ctx, "PKCS7 Verify Error")); }
|
|
|
|
// If data block contains less than 32 bytes, fail.
|
|
size = (unsigned int)BIO_get_mem_data(out, &data2);
|
|
if (size <= ILibMemory_AllocateA_Size(hash)) { PKCS7_free(message); BIO_free(out); return(ILibDuktape_Error(ctx, "PKCS7 Size Mismatch Error")); }
|
|
|
|
|
|
duk_push_object(ctx); // [val]
|
|
duk_push_fixed_buffer(ctx, size); // [val][fbuffer]
|
|
duk_dup(ctx, -1); // [val][fbuffer][dup]
|
|
duk_put_prop_string(ctx, -3, "\xFF_fixedbuffer"); // [val][fbuffer]
|
|
duk_swap_top(ctx, -2); // [fbuffer][val]
|
|
duk_push_buffer_object(ctx, -2, 0, size, DUK_BUFOBJ_NODEJS_BUFFER); // [fbuffer][val][buffer]
|
|
ILibDuktape_CreateReadonlyProperty(ctx, "data"); // [fbuffer][val]
|
|
memcpy_s(Duktape_GetBuffer(ctx, -2, NULL), size, data2, size);
|
|
|
|
|
|
// Get the certificate signer
|
|
st = PKCS7_get0_signers(message, NULL, PKCS7_NOVERIFY);
|
|
|
|
// Get a full certificate hash of the signer
|
|
X509_digest(sk_X509_value(st, 0), EVP_sha256(), (unsigned char*)hash, NULL);
|
|
X509_pubkey_digest(sk_X509_value(st, 0), EVP_sha256(), (unsigned char*)pkeyHash, NULL);
|
|
|
|
sk_X509_free(st);
|
|
|
|
// Check certificate hash with first 32 bytes of data.
|
|
if (memcmp(hash, Duktape_GetBuffer(ctx, -2, NULL), ILibMemory_AllocateA_Size(hash)) != 0) { PKCS7_free(message); BIO_free(out); return(ILibDuktape_Error(ctx, "PKCS7 Certificate Hash Mismatch Error")); }
|
|
char *tmp = ILibMemory_AllocateA(1 + (ILibMemory_AllocateA_Size(hash) * 2));
|
|
util_tohex(hash, (int)ILibMemory_AllocateA_Size(hash), tmp);
|
|
duk_push_object(ctx); // [fbuffer][val][cert]
|
|
ILibDuktape_WriteID(ctx, "certificate");
|
|
duk_push_string(ctx, tmp); // [fbuffer][val][cert][fingerprint]
|
|
ILibDuktape_CreateReadonlyProperty(ctx, "fingerprint"); // [fbuffer][val][cert]
|
|
util_tohex(pkeyHash, (int)ILibMemory_AllocateA_Size(pkeyHash), tmp);
|
|
duk_push_string(ctx, tmp); // [fbuffer][val][cert][publickeyhash]
|
|
ILibDuktape_CreateReadonlyProperty(ctx, "publicKeyHash"); // [fbuffer][val][cert]
|
|
|
|
ILibDuktape_CreateReadonlyProperty(ctx, "signingCertificate"); // [fbuffer][val]
|
|
|
|
// Approved, cleanup and return.
|
|
BIO_free(out);
|
|
PKCS7_free(message);
|
|
|
|
return(1);
|
|
}
|
|
duk_ret_t ILibDuktape_PKCS7_signDataBlockFinalizer(duk_context *ctx)
|
|
{
|
|
char *buffer = Duktape_GetPointerProperty(ctx, 0, "\xFF_signature");
|
|
if (buffer != NULL) { free(buffer); }
|
|
return(0);
|
|
}
|
|
duk_ret_t ILibDuktape_PKCS7_signDataBlock(duk_context *ctx)
|
|
{
|
|
duk_get_prop_string(ctx, 1, "secureContext");
|
|
duk_get_prop_string(ctx, -1, "\xFF_SecureContext2CertBuffer");
|
|
struct util_cert *cert = (struct util_cert*)Duktape_GetBuffer(ctx, -1, NULL);
|
|
duk_size_t bufferLen;
|
|
char *buffer = (char*)Duktape_GetBuffer(ctx, 0, &bufferLen);
|
|
|
|
BIO *in = NULL;
|
|
PKCS7 *message = NULL;
|
|
char *signature = NULL;
|
|
int signatureLength = 0;
|
|
|
|
// Sign the block
|
|
in = BIO_new_mem_buf(buffer, (int)bufferLen);
|
|
message = PKCS7_sign(cert->x509, cert->pkey, NULL, in, PKCS7_BINARY);
|
|
if (message != NULL)
|
|
{
|
|
signatureLength = i2d_PKCS7(message, (unsigned char**)&signature);
|
|
PKCS7_free(message);
|
|
}
|
|
if (in != NULL) BIO_free(in);
|
|
if (signatureLength <= 0) { return(ILibDuktape_Error(ctx, "PKCS7_signDataBlockError: ")); }
|
|
|
|
duk_push_external_buffer(ctx);
|
|
duk_config_buffer(ctx, -1, signature, signatureLength);
|
|
duk_push_buffer_object(ctx, -1, 0, signatureLength, DUK_BUFOBJ_NODEJS_BUFFER);
|
|
duk_push_pointer(ctx, signature);
|
|
duk_put_prop_string(ctx, -2, "\xFF_signature");
|
|
ILibDuktape_CreateFinalizer(ctx, ILibDuktape_PKCS7_signDataBlockFinalizer);
|
|
|
|
return(1);
|
|
}
|
|
void ILibDuktape_PKCS7_Push(duk_context *ctx, void *chain)
|
|
{
|
|
duk_push_object(ctx);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "getSignedDataBlock", ILibDuktape_PKCS7_getSignedDataBlock, 1);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "signDataBlock", ILibDuktape_PKCS7_signDataBlock, DUK_VARARGS);
|
|
}
|
|
|
|
extern uint32_t crc32c(uint32_t crc, const unsigned char* buf, uint32_t len);
|
|
duk_ret_t ILibDuktape_Polyfills_crc32c(duk_context *ctx)
|
|
{
|
|
duk_size_t len;
|
|
char *buffer = Duktape_GetBuffer(ctx, 0, &len);
|
|
duk_push_int(ctx, crc32c(0, (unsigned char*)buffer, (uint32_t)len));
|
|
return(1);
|
|
}
|
|
#endif
|
|
duk_ret_t ILibDuktape_Polyfills_Object_hashCode(duk_context *ctx)
|
|
{
|
|
duk_push_this(ctx);
|
|
duk_push_string(ctx, Duktape_GetStashKey(duk_get_heapptr(ctx, -1)));
|
|
return(1);
|
|
}
|
|
void ILibDuktape_Polyfills_object(duk_context *ctx)
|
|
{
|
|
// Polyfill Object._hashCode()
|
|
duk_get_prop_string(ctx, -1, "Object"); // [g][Object]
|
|
duk_get_prop_string(ctx, -1, "prototype"); // [g][Object][prototype]
|
|
duk_push_c_function(ctx, ILibDuktape_Polyfills_Object_hashCode, 0); // [g][Object][prototype][func]
|
|
ILibDuktape_CreateReadonlyProperty(ctx, "_hashCode"); // [g][Object][prototype]
|
|
duk_pop_2(ctx); // [g]
|
|
}
|
|
|
|
|
|
void ILibDuktape_Polyfills_Init(duk_context *ctx)
|
|
{
|
|
ILibDuktape_ModSearch_AddHandler(ctx, "queue", ILibDuktape_Queue_Push);
|
|
ILibDuktape_ModSearch_AddHandler(ctx, "DynamicBuffer", ILibDuktape_DynamicBuffer_Push);
|
|
ILibDuktape_ModSearch_AddHandler(ctx, "stream", ILibDuktape_Stream_Init);
|
|
#ifndef MICROSTACK_NOTLS
|
|
ILibDuktape_ModSearch_AddHandler(ctx, "pkcs7", ILibDuktape_PKCS7_Push);
|
|
#endif
|
|
|
|
// Global Polyfills
|
|
duk_push_global_object(ctx); // [g]
|
|
|
|
ILibDuktape_Polyfills_String(ctx);
|
|
ILibDuktape_Polyfills_Buffer(ctx);
|
|
ILibDuktape_Polyfills_Console(ctx);
|
|
ILibDuktape_Polyfills_byte_ordering(ctx);
|
|
ILibDuktape_Polyfills_timer(ctx);
|
|
ILibDuktape_Polyfills_object(ctx);
|
|
|
|
ILibDuktape_CreateInstanceMethod(ctx, "addModuleObject", ILibDuktape_Polyfills_addModuleObject, 2);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "addModule", ILibDuktape_Polyfills_addModule, 2);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "getJSModule", ILibDuktape_Polyfills_getJSModule, 1);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "_debugCrash", ILibDuktape_Polyfills_debugCrash, 0);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "_debugGC", ILibDuktape_Polyfills_debugGC, 0);
|
|
ILibDuktape_CreateInstanceMethod(ctx, "_debug", ILibDuktape_Polyfills_debug, 0);
|
|
#ifndef MICROSTACK_NOTLS
|
|
ILibDuktape_CreateInstanceMethod(ctx, "crc32c", ILibDuktape_Polyfills_crc32c, DUK_VARARGS);
|
|
#endif
|
|
duk_pop(ctx); // ...
|
|
|
|
ILibDuktape_Debugger_Init(ctx, 9091);
|
|
}
|
|
|
|
#ifdef __DOXY__
|
|
/*!
|
|
\brief String
|
|
*/
|
|
class String
|
|
{
|
|
public:
|
|
/*!
|
|
\brief Finds a String within another String
|
|
\param str \<String\> Substring to search for
|
|
\return <Integer> Index of where the string was found. -1 if not found
|
|
*/
|
|
Integer indexOf(str);
|
|
/*!
|
|
\brief Extracts a String from a String.
|
|
\param startIndex <Integer> Starting index to extract
|
|
\param length <Integer> Number of characters to extract
|
|
\return \<String\> extracted String
|
|
*/
|
|
String substr(startIndex, length);
|
|
/*!
|
|
\brief Extracts a String from a String.
|
|
\param startIndex <Integer> Starting index to extract
|
|
\param endIndex <Integer> Ending index to extract
|
|
\return \<String\> extracted String
|
|
*/
|
|
String splice(startIndex, endIndex);
|
|
/*!
|
|
\brief Split String into substrings
|
|
\param str \<String\> Delimiter to split on
|
|
\return Array of Tokens
|
|
*/
|
|
Array<String> split(str);
|
|
/*!
|
|
\brief Determines if a String starts with the given substring
|
|
\param str \<String\> substring
|
|
\return <boolean> True, if this String starts with the given substring
|
|
*/
|
|
boolean startsWith(str);
|
|
};
|
|
/*!
|
|
\brief Instances of the Buffer class are similar to arrays of integers but correspond to fixed-sized, raw memory allocations.
|
|
*/
|
|
class Buffer
|
|
{
|
|
public:
|
|
/*!
|
|
\brief Create a new Buffer instance of the specified number of bytes
|
|
\param size <integer>
|
|
\return \<Buffer\> new Buffer instance
|
|
*/
|
|
Buffer(size);
|
|
|
|
/*!
|
|
\brief Returns the amount of memory allocated in bytes
|
|
*/
|
|
integer length;
|
|
/*!
|
|
\brief Creates a new Buffer instance from an encoded String
|
|
\param str \<String\> encoded String
|
|
\param encoding \<String\> Encoding. Can be either 'base64' or 'hex'
|
|
\return \<Buffer\> new Buffer instance
|
|
*/
|
|
static Buffer from(str, encoding);
|
|
/*!
|
|
\brief Decodes Buffer to a String
|
|
\param encoding \<String\> Optional. Can be either 'base64' or 'hex'. If not specified, will just encode as an ANSI string
|
|
\param start <integer> Optional. Starting offset. <b>Default:</b> 0
|
|
\param end <integer> Optional. Ending offset (not inclusive) <b>Default:</b> buffer length
|
|
\return \<String\> Encoded String
|
|
*/
|
|
String toString([encoding[, start[, end]]]);
|
|
/*!
|
|
\brief Returns a new Buffer that references the same memory as the original, but offset and cropped by the start and end indices.
|
|
\param start <integer> Where the new Buffer will start. <b>Default:</b> 0
|
|
\param end <integer> Where the new Buffer will end. (Not inclusive) <b>Default:</b> buffer length
|
|
\return \<Buffer\>
|
|
*/
|
|
Buffer slice([start[, end]]);
|
|
};
|
|
/*!
|
|
\brief Console
|
|
*/
|
|
class Console
|
|
{
|
|
public:
|
|
/*!
|
|
\brief Serializes the input parameters to the Console Display
|
|
\param args <any>
|
|
*/
|
|
void log(...args);
|
|
};
|
|
/*!
|
|
\brief Global Timer Methods
|
|
*/
|
|
class Timers
|
|
{
|
|
public:
|
|
/*!
|
|
\brief Schedules the "immediate" execution of the callback after I/O events' callbacks.
|
|
\param callback <func> Function to call at the end of the event loop
|
|
\param args <any> Optional arguments to pass when the callback is called
|
|
\return Immediate for use with clearImmediate().
|
|
*/
|
|
Immediate setImmediate(callback[, ...args]);
|
|
/*!
|
|
\brief Schedules execution of a one-time callback after delay milliseconds.
|
|
\param callback <func> Function to call when the timeout elapses
|
|
\param args <any> Optional arguments to pass when the callback is called
|
|
\return Timeout for use with clearTimeout().
|
|
*/
|
|
Timeout setTimeout(callback, delay[, ...args]);
|
|
/*!
|
|
\brief Schedules repeated execution of callback every delay milliseconds.
|
|
\param callback <func> Function to call when the timer elapses
|
|
\param args <any> Optional arguments to pass when the callback is called
|
|
\return Timeout for use with clearInterval().
|
|
*/
|
|
Timeout setInterval(callback, delay[, ...args]);
|
|
|
|
/*!
|
|
\brief Cancels a Timeout returned by setTimeout()
|
|
\param timeout Timeout
|
|
*/
|
|
void clearTimeout(timeout);
|
|
/*!
|
|
\brief Cancels a Timeout returned by setInterval()
|
|
\param interval Timeout
|
|
*/
|
|
void clearInterval(interval);
|
|
/*!
|
|
\brief Cancels an Immediate returned by setImmediate()
|
|
\param immediate Immediate
|
|
*/
|
|
void clearImmediate(immediate);
|
|
|
|
/*!
|
|
\brief Scheduled Timer
|
|
*/
|
|
class Timeout
|
|
{
|
|
public:
|
|
};
|
|
/*!
|
|
\implements Timeout
|
|
\brief Scheduled Immediate
|
|
*/
|
|
class Immediate
|
|
{
|
|
public:
|
|
};
|
|
};
|
|
|
|
/*!
|
|
\brief Global methods for byte ordering manipulation
|
|
*/
|
|
class BytesOrdering
|
|
{
|
|
public:
|
|
/*!
|
|
\brief Converts 2 bytes from network order to host order
|
|
\param buffer \<Buffer\> bytes to convert
|
|
\param offset <integer> offset to start
|
|
\return <integer> host order value
|
|
*/
|
|
static integer ntohs(buffer, offset);
|
|
/*!
|
|
\brief Converts 4 bytes from network order to host order
|
|
\param buffer \<Buffer\> bytes to convert
|
|
\param offset <integer> offset to start
|
|
\return <integer> host order value
|
|
*/
|
|
static integer ntohl(buffer, offset);
|
|
/*!
|
|
\brief Writes 2 bytes in network order
|
|
\param buffer \<Buffer\> Buffer to write to
|
|
\param offset <integer> offset to start writing
|
|
\param val <integer> host order value to write
|
|
*/
|
|
static void htons(buffer, offset, val);
|
|
/*!
|
|
\brief Writes 4 bytes in network order
|
|
\param buffer \<Buffer\> Buffer to write to
|
|
\param offset <integer> offset to start writing
|
|
\param val <integer> host order value to write
|
|
*/
|
|
static void htonl(buffer, offset, val);
|
|
};
|
|
#endif
|