Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure self to be a RawData object #180

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion MiniScript-cpp/src/ShellIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ static unsigned char *rawDataGetBytes(Value& rawData, long& offset, long& nBytes
case rdnaNull:
return nullptr;
case rdnaRaise: case rdnaAdjust:
IndexException(String("Index Error (index out of range)")).raise();
IndexException(String("Index Error (index out of range)")).raise(); // [sic in Mini Micro]
}
}
RawDataHandleStorage *storage = (RawDataHandleStorage*)dataWrapper.data.ref;
Expand All @@ -930,6 +930,9 @@ static Value rawDataGetInteger(Context *context, long nBytes, bool isSigned) {
Value self = context->GetVar("self");
long offset = context->GetVar("offset").IntValue();
bool isLittleEndian = self.Lookup("littleEndian").BoolValue();
if (!self.IsA(RawDataType(), context->vm)) {
IndexException(String("Index Error (index out of range)")).raise(); // [sic in Mini Micro]
}
unsigned char *data = rawDataGetBytes(self, offset, nBytes);
uint64_t word = bufReadWord(data, nBytes, isLittleEndian);
if (!isSigned) return Value(word);
Expand Down Expand Up @@ -969,6 +972,9 @@ static void rawDataSetInteger(Context *context, long nBytes, bool isSigned) {
Value self = context->GetVar("self");
long offset = context->GetVar("offset").IntValue();
bool littleEndian = self.Lookup("littleEndian").BoolValue();
if (!self.IsA(RawDataType(), context->vm)) {
IndexException(String("Index Error (index out of range)")).raise(); // [sic in Mini Micro]
}
unsigned char *data = rawDataGetBytes(self, offset, nBytes);
union {
uint64_t u;
Expand All @@ -986,6 +992,9 @@ static Value rawDataGetReal(Context *context, long nBytes) {
Value self = context->GetVar("self");
long offset = context->GetVar("offset").IntValue();
bool isLittleEndian = self.Lookup("littleEndian").BoolValue();
if (!self.IsA(RawDataType(), context->vm)) {
IndexException(String("Index Error (index out of range)")).raise(); // [sic in Mini Micro]
}
unsigned char *data = rawDataGetBytes(self, offset, nBytes);
uint64_t word = bufReadWord(data, nBytes, isLittleEndian);
switch (nBytes) {
Expand Down Expand Up @@ -1015,6 +1024,9 @@ static void rawDataSetReal(Context *context, long nBytes) {
Value self = context->GetVar("self");
long offset = context->GetVar("offset").IntValue();
bool littleEndian = self.Lookup("littleEndian").BoolValue();
if (!self.IsA(RawDataType(), context->vm)) {
IndexException(String("Index Error (index out of range)")).raise(); // [sic in Mini Micro]
}
unsigned char *data = rawDataGetBytes(self, offset, nBytes);
switch (nBytes) {
case 4:
Expand Down Expand Up @@ -1126,6 +1138,9 @@ static IntrinsicResult intrinsic_rawDataUtf8(Context *context, IntrinsicResult p
Value self = context->GetVar("self");
long offset = context->GetVar("offset").IntValue();
long nBytes = context->GetVar("bytes").IntValue();
if (!self.IsA(RawDataType(), context->vm)) {
IndexException(String("Index Error (index out of range)")).raise(); // [sic in Mini Micro]
}
const char *data = (const char *)rawDataGetBytes(self, offset, nBytes, rdnaNull);
if (!data) return IntrinsicResult::Null;
String result(data, nBytes);
Expand All @@ -1137,6 +1152,9 @@ static IntrinsicResult intrinsic_rawDataSetUtf8(Context *context, IntrinsicResul
long offset = context->GetVar("offset").IntValue();
String value = context->GetVar("value").GetString();
long nBytes = value.LengthB();
if (!self.IsA(RawDataType(), context->vm)) {
IndexException(String("Index Error (index out of range)")).raise(); // [sic in Mini Micro]
}
unsigned char *data = rawDataGetBytes(self, offset, nBytes, rdnaAdjust);
if (!data) return IntrinsicResult::Null;
memcpy(data, value.c_str(), nBytes);
Expand Down