mirror of
https://github.com/Ylianst/MeshAgent
synced 2025-12-10 13:23:41 +00:00
Added canary checks
Fixed behavior of 'finish' to match node documentation
This commit is contained in:
@@ -87,9 +87,20 @@ ILibDuktape_WritableStream* ILibDuktape_WritableStream_GetStream(duk_context *ct
|
|||||||
duk_pop_2(ctx); // ...
|
duk_pop_2(ctx); // ...
|
||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
void ILibDuktape_WritableStream_Finish(ILibDuktape_WritableStream *stream)
|
||||||
|
{
|
||||||
|
if (stream == NULL || !ILibMemory_CanaryOK(stream)) { return; }
|
||||||
|
|
||||||
|
duk_push_heapptr(stream->ctx, stream->obj); // [stream]
|
||||||
|
duk_get_prop_string(stream->ctx, -1, "emit"); // [stream][emit]
|
||||||
|
duk_swap_top(stream->ctx, -2); // [emit][this]
|
||||||
|
duk_push_string(stream->ctx, "finish"); // [emit][this][finish]
|
||||||
|
if (duk_pcall_method(stream->ctx, 1) != 0) { ILibDuktape_Process_UncaughtException(stream->ctx); }
|
||||||
|
duk_pop(stream->ctx); // ...
|
||||||
|
}
|
||||||
void ILibDuktape_WritableStream_Ready(ILibDuktape_WritableStream *stream)
|
void ILibDuktape_WritableStream_Ready(ILibDuktape_WritableStream *stream)
|
||||||
{
|
{
|
||||||
|
if (stream == NULL || !ILibMemory_CanaryOK(stream)) { return; }
|
||||||
if (stream->WaitForEnd == 0)
|
if (stream->WaitForEnd == 0)
|
||||||
{
|
{
|
||||||
if (stream->OnWriteFlushEx != NULL)
|
if (stream->OnWriteFlushEx != NULL)
|
||||||
@@ -139,13 +150,6 @@ void ILibDuktape_WritableStream_Ready(ILibDuktape_WritableStream *stream)
|
|||||||
{
|
{
|
||||||
stream->EndSink(stream, stream->WriteSink_User);
|
stream->EndSink(stream, stream->WriteSink_User);
|
||||||
}
|
}
|
||||||
|
|
||||||
duk_push_heapptr(stream->ctx, stream->obj); // [stream]
|
|
||||||
duk_get_prop_string(stream->ctx, -1, "emit"); // [stream][emit]
|
|
||||||
duk_swap_top(stream->ctx, -2); // [emit][this]
|
|
||||||
duk_push_string(stream->ctx, "finish"); // [emit][this][finish]
|
|
||||||
if (duk_pcall_method(stream->ctx, 1) != 0) { ILibDuktape_Process_UncaughtException(stream->ctx); }
|
|
||||||
duk_pop(stream->ctx); // ...
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,16 +226,22 @@ duk_ret_t ILibDuktape_WritableStream_End(duk_context *ctx)
|
|||||||
ILibDuktape_WritableStream *stream = ILibDuktape_WritableStream_GetStream(ctx);
|
ILibDuktape_WritableStream *stream = ILibDuktape_WritableStream_GetStream(ctx);
|
||||||
duk_size_t bufferLen;
|
duk_size_t bufferLen;
|
||||||
char *buffer;
|
char *buffer;
|
||||||
|
int i;
|
||||||
|
|
||||||
if (nargs > 0)
|
if (nargs > 0)
|
||||||
{
|
{
|
||||||
buffer = Duktape_GetBuffer(ctx, 0, &bufferLen);
|
for (i = 0; i < nargs; ++i)
|
||||||
if (stream->WriteSink != NULL)
|
|
||||||
{
|
{
|
||||||
if (nargs > 2 && !duk_is_null_or_undefined(ctx, 2))
|
if (duk_is_function(ctx, i))
|
||||||
{
|
{
|
||||||
ILibDuktape_EventEmitter_AddOnce(ILibDuktape_EventEmitter_GetEmitter_fromThis(ctx), "finish", duk_require_heapptr(ctx, 2));
|
ILibDuktape_EventEmitter_AddOnce(ILibDuktape_EventEmitter_GetEmitter_fromThis(ctx), "finish", duk_require_heapptr(ctx, i));
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = Duktape_GetBuffer(ctx, 0, &bufferLen);
|
||||||
|
if (buffer != NULL && stream->WriteSink != NULL)
|
||||||
|
{
|
||||||
stream->endBytes = (int)bufferLen;
|
stream->endBytes = (int)bufferLen;
|
||||||
if (stream->WriteSink(stream, buffer, (int)bufferLen, stream->WriteSink_User) == ILibTransport_DoneState_INCOMPLETE)
|
if (stream->WriteSink(stream, buffer, (int)bufferLen, stream->WriteSink_User) == ILibTransport_DoneState_INCOMPLETE)
|
||||||
{
|
{
|
||||||
@@ -245,13 +255,6 @@ duk_ret_t ILibDuktape_WritableStream_End(duk_context *ctx)
|
|||||||
{
|
{
|
||||||
// Continue with closing stream
|
// Continue with closing stream
|
||||||
if (stream->EndSink != NULL) { stream->EndSink(stream, stream->WriteSink_User); }
|
if (stream->EndSink != NULL) { stream->EndSink(stream, stream->WriteSink_User); }
|
||||||
|
|
||||||
duk_push_heapptr(stream->ctx, stream->obj); // [stream]
|
|
||||||
duk_get_prop_string(stream->ctx, -1, "emit"); // [stream][emit]
|
|
||||||
duk_swap_top(stream->ctx, -2); // [emit][this]
|
|
||||||
duk_push_string(stream->ctx, "finish"); // [emit][this][finish]
|
|
||||||
if (duk_pcall_method(stream->ctx, 1) != 0) { ILibDuktape_Process_UncaughtException(stream->ctx); }
|
|
||||||
duk_pop(stream->ctx); // ...
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -54,5 +54,6 @@ typedef struct ILibDuktape_WritableStream
|
|||||||
|
|
||||||
ILibDuktape_WritableStream* ILibDuktape_WritableStream_Init(duk_context *ctx, ILibDuktape_WritableStream_WriteHandler WriteHandler, ILibDuktape_WritableStream_EndHandler EndHandler, void *user);
|
ILibDuktape_WritableStream* ILibDuktape_WritableStream_Init(duk_context *ctx, ILibDuktape_WritableStream_WriteHandler WriteHandler, ILibDuktape_WritableStream_EndHandler EndHandler, void *user);
|
||||||
void ILibDuktape_WritableStream_Ready(ILibDuktape_WritableStream *stream);
|
void ILibDuktape_WritableStream_Ready(ILibDuktape_WritableStream *stream);
|
||||||
|
void ILibDuktape_WritableStream_Finish(ILibDuktape_WritableStream *stream);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user