diff --git a/include/CrossLang.hpp b/include/CrossLang.hpp index 06c76fd..3805a2c 100644 --- a/include/CrossLang.hpp +++ b/include/CrossLang.hpp @@ -1611,6 +1611,9 @@ class TString : public TByteView { TObject rhs); bool opGreaterThanEqual(InterperterThread *thrd, std::shared_ptr gc, TObject rhs); + bool opExecuteMethod(InterperterThread *thrd, std::shared_ptr gc, + const std::string &name, + const std::vector &args); public: TString(); @@ -2240,16 +2243,13 @@ class TYieldEnumerator : public TEnumerator { class TStringEnumerator : public TEnumerator { bool hasStarted; size_t index; - std::string str; + TString *str; public: - TStringEnumerator(std::string str); - [[deprecated("Use GCList::Create() instead")]] - static TStringEnumerator *Create(GCList &ls, std::string str); - [[deprecated("Use GCList::Create() instead")]] - static TStringEnumerator *Create(GCList *ls, std::string str); + TStringEnumerator(TString *str); bool MoveNext(std::shared_ptr ls); TObject GetCurrent(GCList &ls); + void Mark(); }; class TListEnumerator : public TEnumerator { diff --git a/src/types/byteviews/string.cpp b/src/types/byteviews/string.cpp index 689257c..54b30cd 100644 --- a/src/types/byteviews/string.cpp +++ b/src/types/byteviews/string.cpp @@ -2,6 +2,35 @@ namespace Tesses::CrossLang { +#define CROSSLANG_TSTRING_NAMES \ + CROSSLANG_TSTRING_NAME_ENT(ToLower) \ + CROSSLANG_TSTRING_NAME_ENT(ToUpper) \ + CROSSLANG_TSTRING_NAME_ENT(PadLeft) \ + CROSSLANG_TSTRING_NAME_ENT(PadRight) \ + CROSSLANG_TSTRING_NAME_ENT(Contains) \ + CROSSLANG_TSTRING_NAME_ENT(IndexOf) \ + CROSSLANG_TSTRING_NAME_ENT(LastIndexOf) \ + CROSSLANG_TSTRING_NAME_ENT(GetEnumerator) \ + CROSSLANG_TSTRING_NAME_ENT(Substring) \ + CROSSLANG_TSTRING_NAME_ENT(Remove) \ + CROSSLANG_TSTRING_NAME_ENT(TrimStart) \ + CROSSLANG_TSTRING_NAME_ENT(TrimEnd) \ + CROSSLANG_TSTRING_NAME_ENT(StartsWith) \ + CROSSLANG_TSTRING_NAME_ENT(EndsWith) + +enum TStringFuncNameEnum { +#define CROSSLANG_TSTRING_NAME_ENT(name) TSF_##name, + CROSSLANG_TSTRING_NAMES +#undef CROSSLANG_TSTRING_NAME_ENT +}; // namespace Tesses::CrossLang + +static std::unordered_map + string_func_names = { +#define CROSSLANG_TSTRING_NAME_ENT(name) {#name, TSTF_##name}, + CROSSLANG_TSTRING_NAMES +#undef CROSSLANG_TSTRING_NAME_ENT +}; + TString::TString() {} TString::TString(std::string_view str) : text(str) {} TString::TString(const char *text, size_t len) : text(text, len) {} @@ -101,6 +130,302 @@ bool TString::opGreaterThanEqual(InterperterThread *thrd, thrd->call_stack_entries.back()->Push(gc, Undefined()); return false; } +bool TString::opExecuteMethod(InterperterThread *thrd, std::shared_ptr gc, + const std::string &name, + const std::vector &args) { + auto result = string_func_names.find(name); + + if (result == string_func_names.end()) + return TByteView::opExecuteMethod(thrd, gc, name, args); + + switch (result->second) { + case TSF_ToLower: { + std::string str = this->text; + for (size_t i = 0; i < str.size(); i++) { + if (str[i] >= 'A' && str[i] <= 'Z') { + str[i] = 'a' + (str[i] - 'A'); + } + } + GCList ls(gc); + + thrd->call_stack_entries.back()->Push(gc, ls.Create(str)); + return false; + } + case TSF_ToUpper: { + std::string str = this->text; + for (size_t i = 0; i < str.size(); i++) { + if (str[i] >= 'a' && str[i] <= 'z') { + str[i] = 'A' + (str[i] - 'a'); + } + } + GCList ls(gc); + + thrd->call_stack_entries.back()->Push(gc, ls.Create(str)); + return false; + } + case TSF_PadLeft: { + std::string str = this->text; + int64_t pad; + char padChar = ' '; + + if (args.size() == 0 || args.size() > 2) { + throw VMException( + "String.PadLeft must only accept 1 or 2 arguments"); + } + + if (GetArgument(args, 0, pad)) { + if ((size_t)pad < str.size()) { + thrd->call_stack_entries.back()->Push(gc, str); + return false; + } + GetArgument(args, 1, padChar); + size_t diff = (size_t)pad - str.size(); + str.insert(str.begin(), diff, padChar); + thrd->call_stack_entries.back()->Push(gc, str); + return false; + } else + throw VMException("String.PadLeft must have a long for width"); + } + case TSF_PadRight: { + + std::string str = this->text; + int64_t pad; + char padChar = ' '; + + if (args.size() == 0 || args.size() > 2) { + throw VMException( + "String.PadRight must only accept 1 or 2 arguments"); + } + + if (GetArgument(args, 0, pad)) { + if ((size_t)pad < str.size()) { + thrd->call_stack_entries.back()->Push(gc, str); + return false; + } + GetArgument(args, 1, padChar); + size_t diff = (size_t)pad - str.size(); + str.insert(str.end(), diff, padChar); + thrd->call_stack_entries.back()->Push(gc, str); + return false; + } else + throw VMException("String.PadRight must have a long for width"); + } + case TSF_Contains: { + std::string str = this->text; + TString *str2; + char c; + if (GetArgumentHeap(args, 0, str2)) { + + int64_t index = 0; + GetArgument(args, 1, index); + + auto res = str.find(str2->text, (std::size_t)index); + + thrd->call_stack_entries.back()->Push(gc, res != std::string::npos); + return false; + } + + else if (GetArgument(args, 0, c)) { + int64_t index = 0; + GetArgument(args, 1, index); + + auto res = str.find_first_of(c, (std::size_t)index); + + thrd->call_stack_entries.back()->Push(gc, res != std::string::npos); + return false; + } + thrd->call_stack_entries.back()->Push(gc, nullptr); + return false; + } + case TSF_IndexOf: { + std::string str = this->text; + TString *str2; + char c; + if (GetArgumentHeap(args, 0, str2)) { + + int64_t index = 0; + GetArgument(args, 1, index); + + auto res = str.find(str2->text, (std::size_t)index); + if (res == std::string::npos) + thrd->call_stack_entries.back()->Push(gc, (int64_t)-1); + else + thrd->call_stack_entries.back()->Push(gc, (int64_t)res); + return false; + } + + else if (GetArgument(args, 0, c)) { + int64_t index = 0; + GetArgument(args, 1, index); + + auto res = str.find_first_of(c, (std::size_t)index); + if (res == std::string::npos) + thrd->call_stack_entries.back()->Push(gc, (int64_t)-1); + else + thrd->call_stack_entries.back()->Push(gc, (int64_t)res); + return false; + } + thrd->call_stack_entries.back()->Push(gc, nullptr); + return false; + } + case TSF_LastIndexOf: { + std::string str = this->text; + TString *str2; + char c; + if (GetArgument(args, 0, str2)) { + + int64_t index = str.size(); + GetArgument(args, 1, index); + + auto res = str.rfind(str2->text, (std::size_t)index); + if (res == std::string::npos) + thrd->call_stack_entries.back()->Push(gc, (int64_t)-1); + else + thrd->call_stack_entries.back()->Push(gc, (int64_t)res); + return false; + } else if (GetArgument(args, 0, c)) { + int64_t index = str.size(); + GetArgument(args, 1, index); + + auto res = str.find_last_of(c, (std::size_t)index); + if (res == std::string::npos) + thrd->call_stack_entries.back()->Push(gc, (int64_t)-1); + else + thrd->call_stack_entries.back()->Push(gc, (int64_t)res); + return false; + } + thrd->call_stack_entries.back()->Push(gc, nullptr); + return false; + } + case TSF_GetEnumerator: { + GCList ls(gc); + thrd->call_stack_entries.back()->Push( + gc, ls.Create(this)); + return false; + } + case TSF_Substring: { + + std::string str = this->text; + if (args.size() >= 1 && std::holds_alternative(args[0])) { + size_t offset = (size_t)std::get(args[0]); + size_t count = std::string::npos; + if (args.size() == 2 && std::holds_alternative(args[1])) { + count = (size_t)std::get(args[1]); + } + GCList ls(gc); + thrd->call_stack_entries.back()->Push( + gc, ls.Create(str.substr(offset, count))); + return false; + } + + thrd->call_stack_entries.back()->Push(gc, nullptr); + return false; + } + case TSF_Remove: { + + std::string str = this->text; + if (args.size() >= 1 && std::holds_alternative(args[0])) { + size_t offset = (size_t)std::get(args[0]); + size_t count = std::string::npos; + if (args.size() == 2 && std::holds_alternative(args[1])) { + count = (size_t)std::get(args[1]); + } + GCList ls(gc); + thrd->call_stack_entries.back()->Push( + gc, ls.Create(str.erase(offset, count))); + return false; + } + thrd->call_stack_entries.back()->Push(gc, nullptr); + return false; + } + case TSF_TrimStart: { + + std::string str = this->text; + if (args.size() >= 0) { + char c = (args.size() == 1 && std::holds_alternative(args[0])) + ? std::get(args[0]) + : ' '; + + size_t i = 0; + for (; i < str.size(); i++) { + if (str[i] != c) + break; + } + GCList ls(gc); + thrd->call_stack_entries.back()->Push( + gc, ls.Create( + (!str.empty() && i < str.size()) ? str.substr(i) : "")); + return false; + } + thrd->call_stack_entries.back()->Push(gc, nullptr); + return false; + } + case TSF_TrimEnd: { + + std::string str = this->text; + if (args.size() >= 0) { + char c = (args.size() == 1 && std::holds_alternative(args[0])) + ? std::get(args[0]) + : ' '; + size_t i = str.size() - 1; + for (; i >= 0; i--) { + if (str[i] != c) + break; + } + GCList ls(gc); + thrd->call_stack_entries.back()->Push( + gc, ls.Create((!str.empty() && i < str.size()) + ? str.substr(0, i + 1) + : "")); + return false; + } + thrd->call_stack_entries.back()->Push(gc, nullptr); + return false; + } + case TSF_StartsWith: { + std::string str = this->text; + TString *v; + if (GetArgumentHeap(args, 0, v)) { + if (str.size() < v->text.size()) { + thrd->call_stack_entries.back()->Push(gc, false); + return false; + } + for (size_t i = 0; i < v->text.size(); i++) { + if (v->text[i] != str[i]) { + thrd->call_stack_entries.back()->Push(gc, false); + return false; + } + } + thrd->call_stack_entries.back()->Push(gc, true); + return false; + } + thrd->call_stack_entries.back()->Push(gc, false); + return false; + } + case TSF_EndsWith: { + std::string str = this->text; + TString *v; + if (GetArgumentHeap(args, 0, v)) { + if (str.size() < v->text.size()) { + thrd->call_stack_entries.back()->Push(gc, false); + return false; + } + + size_t _end = str.size() - v->text.size(); + for (size_t i = 0; i < v->text.size(); i++) { + if (v->text[i] != str[i + _end]) { + thrd->call_stack_entries.back()->Push(gc, false); + return false; + } + } + thrd->call_stack_entries.back()->Push(gc, true); + return false; + } + thrd->call_stack_entries.back()->Push(gc, false); + return false; + } + } +} bool TString::IsEqualTo(std::shared_ptr gc, TObject rhs) { TString *rStr; if (GetObjectHeap(rhs, rStr)) { diff --git a/src/types/ittr.cpp b/src/types/ittr.cpp index a9b3381..73fd5d6 100644 --- a/src/types/ittr.cpp +++ b/src/types/ittr.cpp @@ -92,15 +92,15 @@ TCustomEnumerator *TCustomEnumerator::Create(GCList &ls, TDictionary *dict) { return ls.Create(dict); } TEnumerator *TEnumerator::CreateFromObject(GCList &ls, TObject obj) { - std::string str; + TString *str; TList *mls; TDynamicList *dynList; TDynamicDictionary *dynDict; TDictionary *dict; TEnumerator *enumerator; TQueryable *q; - if (GetObject(obj, str)) { - return TStringEnumerator::Create(ls, str); + if (GetObjectHeap(obj, str)) { + return ls.Create(str); } else if (GetObjectHeap(obj, mls)) { return TListEnumerator::Create(ls, mls); } else if (GetObjectHeap(obj, dynList)) { @@ -283,32 +283,33 @@ void TDynamicListEnumerator::Mark() { this->marked = true; this->ls->Mark(); } -TStringEnumerator::TStringEnumerator(std::string str) +TStringEnumerator::TStringEnumerator(TString *str) : str(str), hasStarted(false) {} -TStringEnumerator *TStringEnumerator::Create(GCList &ls, std::string str) { - return ls.Create(str); -} -TStringEnumerator *TStringEnumerator::Create(GCList *ls, std::string str) { - return ls->Create(str); -} + bool TStringEnumerator::MoveNext(std::shared_ptr ls) { if (!this->hasStarted) { this->hasStarted = true; this->index = 0; - return !this->str.empty(); + return !this->str->GetString().empty(); } else { - if (this->index >= this->str.size()) + if (this->index >= this->str->GetString().size()) return false; this->index++; - return this->index < this->str.size(); + return this->index < this->str->GetString().size(); } } TObject TStringEnumerator::GetCurrent(GCList &ls) { if (!this->hasStarted) return nullptr; - if (this->index < this->str.size()) - return this->str[this->index]; + if (this->index < this->str->GetString().size()) + return this->str->GetString()[this->index]; return nullptr; } +void TStringEnumerator::Mark() { + if (this->marked) + return; + this->marked = true; + this->str->Mark(); +} }; // namespace Tesses::CrossLang \ No newline at end of file diff --git a/src/types/list.cpp b/src/types/list.cpp index 3988e8d..deaba71 100644 --- a/src/types/list.cpp +++ b/src/types/list.cpp @@ -111,10 +111,6 @@ TObject TDynamicList::SetAt(GCList &ls, int64_t index, TObject val) { TDynamicList::~TDynamicList() {} -TByteArray *TByteArray::Create(GCList &ls) { return ls.Create(); } - -TByteArray *TByteArray::Create(GCList *ls) { return ls->Create(); } - void TList::Add(TObject value) { this->items.push_back(value); } void TList::Set(int64_t index, TObject value) { if (index >= 0 && index < this->Count()) { @@ -209,7 +205,7 @@ bool TList::opExecuteMethod(InterperterThread *thrd, std::shared_ptr gc, for (int64_t i = 0; i < this->Count(); i++) { auto item = this->Get(i); gc->BarrierEnd(); - if (Equals(gc, args[0], item)) { + if (ObjectEquals(gc, args[0], item)) { cse.back()->Push(gc, true); return false; } @@ -232,7 +228,7 @@ bool TList::opExecuteMethod(InterperterThread *thrd, std::shared_ptr gc, for (; i < this->Count(); i++) { auto item = this->Get(i); gc->BarrierEnd(); - if (Equals(gc, args[0], item)) { + if (ObjectEquals(gc, args[0], item)) { cse.back()->Push(gc, i); return false; @@ -252,7 +248,7 @@ bool TList::opExecuteMethod(InterperterThread *thrd, std::shared_ptr gc, for (int64_t i = 0; i < this->Count(); i++) { auto item = this->Get(i); gc->BarrierEnd(); - if (Equals(gc, args[0], item)) { + if (ObjectEquals(gc, args[0], item)) { gc->BarrierBegin(); this->RemoveAt(i); i--; @@ -271,7 +267,7 @@ bool TList::opExecuteMethod(InterperterThread *thrd, std::shared_ptr gc, for (int64_t i = 0; i < this->Count(); i++) { auto item = this->Get(i); gc->BarrierEnd(); - if (Equals(gc, args[0], item)) { + if (ObjectEquals(gc, args[0], item)) { gc->BarrierBegin(); this->RemoveAt(i); gc->BarrierEnd(); diff --git a/src/vm/bc/executemethod2.cpp b/src/vm/bc/executemethod2.cpp index 6a0fdfb..856e8dd 100644 --- a/src/vm/bc/executemethod2.cpp +++ b/src/vm/bc/executemethod2.cpp @@ -379,262 +379,7 @@ bool InterperterThread::ExecuteMethod2(std::shared_ptr gc, TObject instance, } } else if (std::holds_alternative(instance)) { std::string str = std::get(instance); - if (key == "ToLower") { - for (size_t i = 0; i < str.size(); i++) { - if (str[i] >= 'A' && str[i] <= 'Z') { - str[i] = 'a' + (str[i] - 'A'); - } - } - cse.back()->Push(gc, str); - return false; - } - if (key == "ToUpper") { - for (size_t i = 0; i < str.size(); i++) { - if (str[i] >= 'a' && str[i] <= 'z') { - str[i] = 'A' + (str[i] - 'a'); - } - } - cse.back()->Push(gc, str); - return false; - } - if (key == "PadLeft") { - int64_t pad; - char padChar = ' '; - - if (args.size() == 0 || args.size() > 2) { - throw VMException( - "String.PadLeft must only accept 1 or 2 arguments"); - } - - if (GetArgument(args, 0, pad)) { - if ((size_t)pad < str.size()) { - cse.back()->Push(gc, str); - return false; - } - GetArgument(args, 1, padChar); - size_t diff = (size_t)pad - str.size(); - str.insert(str.begin(), diff, padChar); - cse.back()->Push(gc, str); - return false; - } else - throw VMException( - "String.PadLeft must have a long for width"); - } - if (key == "PadRight") { - int64_t pad; - char padChar = ' '; - - if (args.size() == 0 || args.size() > 2) { - throw VMException( - "String.PadRight must only accept 1 or 2 arguments"); - } - - if (GetArgument(args, 0, pad)) { - if ((size_t)pad < str.size()) { - cse.back()->Push(gc, str); - return false; - } - GetArgument(args, 1, padChar); - size_t diff = (size_t)pad - str.size(); - str.insert(str.end(), diff, padChar); - cse.back()->Push(gc, str); - return false; - } else - throw VMException( - "String.PadRight must have a long for width"); - } - if (key == "Contains") { - std::string str2; - char c; - if (GetArgument(args, 0, str2)) { - - int64_t index = 0; - GetArgument(args, 1, index); - - auto res = str.find(str2, (std::size_t)index); - - cse.back()->Push(gc, res != std::string::npos); - return false; - } - - else if (GetArgument(args, 0, c)) { - int64_t index = 0; - GetArgument(args, 1, index); - - auto res = str.find_first_of(c, (std::size_t)index); - - cse.back()->Push(gc, res != std::string::npos); - return false; - } - cse.back()->Push(gc, nullptr); - return false; - } - if (key == "IndexOf") { - std::string str2; - char c; - if (GetArgument(args, 0, str2)) { - - int64_t index = 0; - GetArgument(args, 1, index); - - auto res = str.find(str2, (std::size_t)index); - if (res == std::string::npos) - cse.back()->Push(gc, (int64_t)-1); - else - cse.back()->Push(gc, (int64_t)res); - return false; - } - - else if (GetArgument(args, 0, c)) { - int64_t index = 0; - GetArgument(args, 1, index); - - auto res = str.find_first_of(c, (std::size_t)index); - if (res == std::string::npos) - cse.back()->Push(gc, (int64_t)-1); - else - cse.back()->Push(gc, (int64_t)res); - return false; - } - cse.back()->Push(gc, nullptr); - return false; - } - if (key == "LastIndexOf") { - std::string str2; - char c; - if (GetArgument(args, 0, str2)) { - - int64_t index = str.size(); - GetArgument(args, 1, index); - - auto res = str.rfind(str2, (std::size_t)index); - if (res == std::string::npos) - cse.back()->Push(gc, (int64_t)-1); - else - cse.back()->Push(gc, (int64_t)res); - return false; - } else if (GetArgument(args, 0, c)) { - int64_t index = str.size(); - GetArgument(args, 1, index); - - auto res = str.find_last_of(c, (std::size_t)index); - if (res == std::string::npos) - cse.back()->Push(gc, (int64_t)-1); - else - cse.back()->Push(gc, (int64_t)res); - return false; - } - cse.back()->Push(gc, nullptr); - return false; - } - if (key == "GetEnumerator") { - cse.back()->Push(gc, TStringEnumerator::Create(ls, str)); - return false; - } - if (key == "Substring") { - if (args.size() >= 1 && - std::holds_alternative(args[0])) { - size_t offset = (size_t)std::get(args[0]); - size_t count = std::string::npos; - if (args.size() == 2 && - std::holds_alternative(args[1])) { - count = (size_t)std::get(args[1]); - } - cse.back()->Push(gc, str.substr(offset, count)); - return false; - } - } - if (key == "Remove") { - if (args.size() >= 1 && - std::holds_alternative(args[0])) { - size_t offset = (size_t)std::get(args[0]); - size_t count = std::string::npos; - if (args.size() == 2 && - std::holds_alternative(args[1])) { - count = (size_t)std::get(args[1]); - } - cse.back()->Push(gc, str.erase(offset, count)); - return false; - } - } - if (key == "TrimStart") { - if (args.size() >= 0) { - char c = (args.size() == 1 && - std::holds_alternative(args[0])) - ? std::get(args[0]) - : ' '; - - size_t i = 0; - for (; i < str.size(); i++) { - if (str[i] != c) - break; - } - - cse.back()->Push(gc, (!str.empty() && i < str.size()) - ? str.substr(i) - : ""); - return false; - } - } - if (key == "TrimEnd") { - if (args.size() >= 0) { - char c = (args.size() == 1 && - std::holds_alternative(args[0])) - ? std::get(args[0]) - : ' '; - size_t i = str.size() - 1; - for (; i >= 0; i--) { - if (str[i] != c) - break; - } - - cse.back()->Push(gc, (!str.empty() && i < str.size()) - ? str.substr(0, i + 1) - : ""); - return false; - } - } - if (key == "EndsWith") { - std::string v; - if (GetArgument(args, 0, v)) { - if (str.size() < v.size()) { - cse.back()->Push(gc, false); - return false; - } - - size_t _end = str.size() - v.size(); - for (size_t i = 0; i < v.size(); i++) { - if (v[i] != str[i + _end]) { - cse.back()->Push(gc, false); - return false; - } - } - cse.back()->Push(gc, true); - return false; - } - cse.back()->Push(gc, false); - return false; - } - if (key == "StartsWith") { - std::string v; - if (GetArgument(args, 0, v)) { - if (str.size() < v.size()) { - cse.back()->Push(gc, false); - return false; - } - for (size_t i = 0; i < v.size(); i++) { - if (v[i] != str[i]) { - cse.back()->Push(gc, false); - return false; - } - } - cse.back()->Push(gc, true); - return false; - } - cse.back()->Push(gc, false); - return false; - } if (key == "Escape") { bool quote; if (!GetArgument(args, 0, quote)) @@ -704,7 +449,7 @@ bool InterperterThread::ExecuteMethod2(std::shared_ptr gc, TObject instance, size_t index = (size_t)std::get(args[0]); size_t sz = str.size(); - if (index >= 0 && index < sz) { + if (index < sz) { cse.back()->Push(gc, str[index]); return false; }