Skip to content

Commit

Permalink
Remove static allocated String for readJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
rajsite committed Jun 1, 2019
1 parent 32ed82c commit b66ae13
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 39 deletions.
31 changes: 10 additions & 21 deletions source/core/CEntryPoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,33 +242,22 @@ VIREO_EXPORT EggShellResult EggShell_WriteValueString(TypeManagerRef tm, const T
}
//------------------------------------------------------------
//! Read a symbol's value as a string. Value will be formatted according to designated format.
VIREO_EXPORT EggShellResult EggShell_ReadValueString(TypeManagerRef tm, const TypeRef typeRef, void* pData, const char* format, UInt8** valueString)
VIREO_EXPORT EggShellResult EggShell_ReadValueString(TypeManagerRef tm, const TypeRef typeRef, void* pData, const char* format,
TypeRef responseJSONTypeRef, void* responseJSONDataRef)
{
TypeManagerScope scope(tm);
if (typeRef == nullptr || !typeRef->IsValid())
return kEggShellResult_InvalidTypeRef;

static StringRef returnBuffer = nullptr;
if (returnBuffer == nullptr) {
// Allocate a string the first time it is used.
// After that it will be resized as needed.
STACK_VAR(String, tempReturn);
returnBuffer = tempReturn.DetachValue();
} else {
returnBuffer->Resize1D(0);
}

if (returnBuffer) {
SubString formatss(format);
TDViaFormatter formatter(returnBuffer, true, 0, &formatss, kJSONEncodingEggShell);
formatter.FormatData(typeRef, pData);
// Add an explicit null terminator so it looks like a C string.
returnBuffer->Append((Utf8Char)'\0');
*valueString = returnBuffer->Begin();
return kEggShellResult_Success;
}
if (responseJSONTypeRef == nullptr || !responseJSONTypeRef->IsValid() || !responseJSONTypeRef->IsString())
return kEggShellResult_InvalidTypeRef;

return kEggShellResult_UnableToCreateReturnBuffer;
StringRef returnBuffer = *(static_cast<const StringRef*>(responseJSONDataRef));
returnBuffer->Resize1D(0);
SubString formatss(format);
TDViaFormatter formatter(returnBuffer, true, 0, &formatss, kJSONEncodingEggShell);
formatter.FormatData(typeRef, pData);
return kEggShellResult_Success;
}
void CopyArrayTypeNameStringToBuffer(StringRef arrayTypeNameBuffer, SubString arrayTypeName)
{
Expand Down
16 changes: 8 additions & 8 deletions source/core/TDCodecVia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2201,29 +2201,29 @@ void TDViaFormatter::FormatPointerData(TypeRef pointerType, void* pData)
} else {
// For types that do not support serialization
// serialize the pointer type and whether it is null or not
if(Fmt().UseFieldNames())
if (Fmt().UseFieldNames())
_string->Append(Fmt()._quote);
_string->Append('^');
_string->Append(name.Length(), (Utf8Char*)name.Begin());
if ((*(void**)pData) == nullptr) {
_string->Append(5, (Utf8Char*)"_null");
}
if(Fmt().UseFieldNames())
if (Fmt().UseFieldNames())
_string->Append(Fmt()._quote);
}
}
//------------------------------------------------------------
void TDViaFormatter::FormatType(TypeRef type)
{
if(Fmt().UseFieldNames())
if (Fmt().UseFieldNames())
_string->Append(Fmt()._quote);
if (type) {
TDViaFormatterTypeVisitor visitor(this);
type->Accept(&visitor);
} else {
_string->Append(4, (Utf8Char*)"null");
}
if(Fmt().UseFieldNames())
if (Fmt().UseFieldNames())
_string->Append(Fmt()._quote);
}
//------------------------------------------------------------
Expand Down Expand Up @@ -2433,10 +2433,10 @@ void TDViaFormatter::FormatData(TypeRef type, void *pData)
_string->AppendCStr((*(AQBlock1*) pData) ? "true" : "false");
break;
case kEncoding_Generic:
if(Fmt().UseFieldNames())
if (Fmt().UseFieldNames())
_string->Append(Fmt()._quote);
_string->Append('*');
if(Fmt().UseFieldNames())
if (Fmt().UseFieldNames())
_string->Append(Fmt()._quote);
break;
case kEncoding_Array:
Expand All @@ -2454,7 +2454,7 @@ void TDViaFormatter::FormatData(TypeRef type, void *pData)
break;
case kEncoding_RefNum:
{
if(Fmt().UseFieldNames())
if (Fmt().UseFieldNames())
_string->Append(Fmt()._quote);
RefNumVal *refVal = (RefNumVal*)pData;
SubString name = type->Name();
Expand All @@ -2468,7 +2468,7 @@ void TDViaFormatter::FormatData(TypeRef type, void *pData)
_string->Append('(');
FormatInt(kEncoding_RefNum, refnum);
_string->Append(')');
if(Fmt().UseFieldNames())
if (Fmt().UseFieldNames())
_string->Append(Fmt()._quote);
}
break;
Expand Down
3 changes: 2 additions & 1 deletion source/include/CEntryPoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ VIREO_EXPORT EggShellResult EggShell_FindSubValue(TypeManagerRef tm, const TypeR
VIREO_EXPORT EggShellResult EggShell_WriteDouble(TypeManagerRef tm, const TypeRef typeRef, void* pData, Double value);
VIREO_EXPORT EggShellResult EggShell_ReadDouble(TypeManagerRef tm, const TypeRef typeRef, const void* pData, Double* result);
VIREO_EXPORT EggShellResult EggShell_WriteValueString(TypeManagerRef tm, TypeRef typeRef, void* pData, const char* format, const char* value);
VIREO_EXPORT EggShellResult EggShell_ReadValueString(TypeManagerRef tm, TypeRef typeRef, void* pData, const char* format, UInt8** valueString);
VIREO_EXPORT EggShellResult EggShell_ReadValueString(TypeManagerRef tm, const TypeRef typeRef, void* pData, const char* format,
TypeRef responseJSONTypeRef, void* responseJSONDataRef);
VIREO_EXPORT EggShellResult EggShell_ResizeArray(TypeManagerRef tm, const TypeRef typeRef, const void* pData,
Int32 rank, Int32 dimensionLengths[]);
VIREO_EXPORT EggShellResult EggShell_GetVariantAttribute(TypeManagerRef tm, const TypeRef typeRef, void* pData, const char* attributeNameCStr,
Expand Down
12 changes: 5 additions & 7 deletions source/io/module_eggShell.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,22 +286,20 @@ var assignEggShell;
var stack = Module.stackSave(); // Stack save only needed for input parameter string or array

var type = 'JSON';
var jsonStackDoublePointer = Module.stackAlloc(POINTER_SIZE);
var typeStackPointer = Module.coreHelpers.writeJSStringToStack(type);

var eggShellError = Module._EggShell_ReadValueString(Module.eggShell.v_userShell, valueRef.typeRef, valueRef.dataRef, typeStackPointer, jsonStackDoublePointer);

var stringTypeRef = Module.typeHelpers.findType('String');
var jsonResponseValueRef = Module.eggShell.allocateData(stringTypeRef);
var eggShellError = Module._EggShell_ReadValueString(Module.eggShell.v_userShell, valueRef.typeRef, valueRef.dataRef, typeStackPointer, jsonResponseValueRef.typeRef, jsonResponseValueRef.dataRef);
if (eggShellError !== 0) {
throw new Error('Performing readJSON failed for the following reason: ' + eggShellResultEnum[eggShellError] +
' (error code: ' + eggShellError + ')' +
' (typeRef: ' + valueRef.typeRef + ')' +
' (dataRef: ' + valueRef.dataRef + ')');
}

var jsonStackPointer = Module.getValue(jsonStackDoublePointer, 'i32');
var responseLength = Module.coreHelpers.findCStringLength(Module.HEAPU8, jsonStackPointer);
var response = Module.coreHelpers.sizedUtf8ArrayToJSString(Module.HEAPU8, jsonStackPointer, responseLength);

var response = Module.eggShell.readString(jsonResponseValueRef);
Module.eggShell.deallocateData(jsonResponseValueRef);
Module.stackRestore(stack);
return response;
};
Expand Down
4 changes: 2 additions & 2 deletions test-it/karma/vtrsuite/VtrTestSuite.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('The Vireo VTR test suite', function () {

// Sharing Vireo instances across tests make them run soooo much faster
var vireo;
beforeEach(async function () {
beforeAll(async function () {
vireo = await vireoHelpers.createInstance();

// VTR tests can't fire JS events, so register no-op registration functions
Expand All @@ -20,7 +20,7 @@ describe('The Vireo VTR test suite', function () {
// no-op
});
});
afterEach(function () {
afterAll(function () {
vireo = undefined;
});

Expand Down

0 comments on commit b66ae13

Please sign in to comment.