6 #include <unordered_map> 27 auto selName = selNames[sel];
30 std::cerr << std::endl <<
"ERROR: method " << selName
31 <<
" called on null object." << std::endl;
35 std::cerr << std::endl <<
"ERROR: " << cls->
className 36 <<
" does not respond to selector " << selName <<
'.' << std::endl;
67 f->
fd =
open(filename.c_str(), O_RDWR | O_CREAT, 0600);
81 return reinterpret_cast<Obj>(f);
90 int fd = f->
fd ? f->
fd : STDIN_FILENO;
94 while (1 == read(fd, &c, 1))
102 uintptr_t len = buffer.size();
107 String *newStr = gcAlloc<String>(len);
110 memcpy(newStr->
characters, buffer.c_str(), len);
124 int fd = f->
fd ? f->
fd : STDOUT_FILENO;
127 return reinterpret_cast<Obj>(f);
151 if (i >= len && (i > 0))
183 if (i >= len && (i > 0))
212 size_t newSize = i + 1;
213 Obj *buffer =
reinterpret_cast<Obj*
>(GC_MALLOC(newSize *
sizeof(
Obj)));
214 memcpy(buffer, arr->
buffer, len *
sizeof(
Obj));
272 uintptr_t lenTotal = len1+len2;
273 String *newStr = gcAlloc<String>(lenTotal);
278 return reinterpret_cast<Obj>(newStr);
293 uintptr_t len = std::min(len1, len2);
301 else if (len1 < len2)
355 static_assert(
sizeof(StaticSelectorNames) /
sizeof(
char*) ==
479 const char *
ArrayIvars[] = {
"length",
"bufferSize",
"buffer" };
555 static std::unordered_map<std::string, Selector> selectors;
558 if (selectors.empty())
568 size_t next = selectors.size() + 1;
584 static std::unordered_map<std::string, struct Class*> classTable;
586 static void registerClasses()
588 if (classTable.empty())
599 classTable[name] = cls;
604 return classTable[name];
638 assert(0 &&
"Too many arguments!");
641 return (
reinterpret_cast<Obj(*)(
Obj,
Selector)
>(m))(receiver, sel);
643 return (
reinterpret_cast<Obj(*)(
Obj,
Selector,
Obj)
>(m))(receiver, sel, args[0]);
649 args[0], args[1], args[2]);
652 sel, args[0], args[1], args[2], args[3]);
662 assert(0 &&
"Too many arguments!");
665 return (
reinterpret_cast<Obj(*)(
Closure*)
>(m))(receiver);
667 return (
reinterpret_cast<Obj(*)(
Closure*,
Obj)
>(m))(receiver, args[0]);
669 return (
reinterpret_cast<Obj(*)(
Closure*,
Obj,
Obj)
>(m))(receiver, args[0], args[1]);
675 args[1], args[2], args[3]);
Obj StringLength(String *str, Selector sel)
The .length() method for String objects.
struct Class * isa
The pointer to the class of this object.
Object *(* CompiledMethod)(Object *, Selector,...)
A compiled method is a function that takes an object (the receiver) and the selector as implicit argu...
Class * isa
The class pointer.
Obj StringCmp(String *str, Selector sel, String *other)
Compare two strings, returning an integer representing the ordering.
Obj FileClose(File *f, Selector sel)
The close method on File objects.
Object * Obj
Object pointer.
Obj mysoreScriptAdd(Obj lhs, Obj rhs)
Helper function called by compiled code for the + operator on objects that are not small (embedded in...
Obj callCompiledClosure(ClosureInvoke m, Closure *receiver, Obj *args, int argCount)
Calls a compiled closure from the specified argument list.
String * FileReadLine(File *f, Selector sel)
The readline method on File objects.
Obj ArrayLength(Array *arr, Selector sel)
The .length() method for Array objects.
Obj mysoreScriptDiv(Obj lhs, Obj rhs)
Helper function called by compiled code for the / operator on objects that are not small (embedded in...
uint32_t Selector
Selectors are unique identifiers for methods.
struct Method StringMethods[]
Method table for the String class.
Obj bufferSize
The size of the buffer.
StaticSelectors
Selectors for methods that are defined as part of the runtime.
struct Class ArrayClass
The Array class structure.
struct Class SmallIntClass
The SmallInt (Number) class structure.
Class * isa
Class pointer.
Obj NumberDump(Obj str, Selector sel)
The .dump() method for Number objects.
struct Method * methodList
An array of methodCount elements describing the methods that this class implements.
intptr_t fd
The file descriptor to use.
Selector lookupSelector(const std::string &str)
Looks up the selector for a specified string value, registering a new value if this is the first time...
Obj newObject(struct Class *cls)
Instantiate an object.
Object *(* ClosureInvoke)(Closure *,...)
A compiled closure invoke function.
Obj mysoreScriptMul(Obj lhs, Obj rhs)
Helper function called by compiled code for the * operator on objects that are not small (embedded in...
void registerClass(const std::string &name, struct Class *cls)
Register a newly constructed class.
struct Method FileMethods[]
Methods for the file class.
struct Class * lookupClass(const std::string &name)
Look up an existing class.
const char * StringIvars[]
The names of the instance variables in the String class.
A generic MysoreScript object.
Obj ArrayAtPut(Array *arr, Selector sel, Obj idx, Obj obj)
The .atPut(idx, obj) method for Array objects.
struct Class StringClass
The String class structure.
Obj StringCharAt(String *str, Selector sel, Obj idx)
The .charAt(idx) method for String objects.
Obj NumberPrint(Obj str, Selector sel)
The .print() method for Number objects.
struct Class ClosureClass
The Closure class structure.
CompiledMethod function
The compiled method, if one exists.
Method * methodForSelector(Class *cls, Selector sel)
Looks up the Method that should be invoked for the specified selector on this class.
CompiledMethod compiledMethodForSelector(Obj obj, Selector sel)
Look up the compiled method to call for a specific selector.
Obj callCompiledMethod(CompiledMethod m, Obj receiver, Selector sel, Obj *args, int argCount)
Calls a compiled method, constructing the correct argument frame based on the arguments.
char characters[0]
An array of characters.
Obj StringAdd(String *str, Selector sel, String *other)
The + method on a string, allocates a new string with the specified length.
The layout of the primitive Array class in MysoreScript.
Obj ArrayAt(Array *arr, Selector sel, Obj idx)
The .at(idx) method for Array objects.
struct Class * superclass
The superclass of this class, if it has one, or a null pointer if it is a root class.
const char * ArrayIvars[]
The names of the instance variables in the Array class.
intptr_t getInteger(Obj o)
Assuming that o is a small integer (an integer embedded in a pointer), return it as a C integer...
int32_t methodCount
The number of methods that this class implements.
const char * FileIvars[]
The names of the instance variables in the File class.
The primitive String class in MysoreScript.
int32_t indexedIVarCount
The number of indexed instance variables that this class has.
struct Method NumberMethods[]
Method table for the Number class.
std::vector< std::string > selNames
Global vector of selector names.
Obj StringDump(String *str, Selector sel)
The .dump() method for String objects.
const char * className
The name of this class.
Obj createSmallInteger(intptr_t i)
Construct a small integer object from the given integer.
Obj invalidMethod(Obj obj, Selector sel)
Invalid method function.
struct Class FileClass
The File class structure.
Obj * buffer
The buffer storing the values in this array.
Obj FileWrite(File *f, Selector sel, String *data)
The write method on File objects.
Obj mysoreScriptSub(Obj lhs, Obj rhs)
Helper function called by compiled code for the - operator on objects that are not small (embedded in...
Obj length
Length of the array (number of elements in it).
File * FileOpen(File *f, Selector sel, String *file)
The open method on File objects.
Obj length
The number of characters in the string.
Struct holding metadata about a class.
Obj StringPrint(String *str, Selector sel)
The .dump() method for String objects.
Methods in a class's method list.
The structure representing MysoreScript File objects.
const char * StaticSelectorNames[]
The names of the selectors in the StaticSelectors enumeration.
bool isInteger(Obj o)
Is this object a small integer (lowest bit is 1, next two bits are 0).
struct Method ArrayMethods[]
Method table for the Array class.
The layout of all closures in MysoreScript.
Selector selector
The selector that this method applies to.