Updated Toy
This commit is contained in:
2
Toy
2
Toy
Submodule Toy updated: f2f8aed23a...4b83f1f0d6
@@ -4,12 +4,11 @@
|
|||||||
|
|
||||||
#include "repl_tools.h"
|
#include "repl_tools.h"
|
||||||
#include "toy_memory.h"
|
#include "toy_memory.h"
|
||||||
|
#include "toy_drive_system.h"
|
||||||
#include "toy_literal_array.h"
|
#include "toy_literal_array.h"
|
||||||
|
|
||||||
#include "toy_console_colors.h"
|
#include "toy_console_colors.h"
|
||||||
|
|
||||||
#include "lib_runner.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
//errors here should be fatal
|
//errors here should be fatal
|
||||||
@@ -121,7 +120,7 @@ static int nativeLoadRootNode(Toy_Interpreter* interpreter, Toy_LiteralArray* ar
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_Literal filePathLiteral = Toy_getFilePathLiteral(interpreter, &drivePathLiteral);
|
Toy_Literal filePathLiteral = Toy_getDrivePathLiteral(interpreter, &drivePathLiteral);
|
||||||
|
|
||||||
Toy_freeLiteral(drivePathLiteral); //not needed anymore
|
Toy_freeLiteral(drivePathLiteral); //not needed anymore
|
||||||
|
|
||||||
|
|||||||
@@ -4,11 +4,10 @@
|
|||||||
#include "box_engine.h"
|
#include "box_engine.h"
|
||||||
|
|
||||||
#include "repl_tools.h"
|
#include "repl_tools.h"
|
||||||
|
#include "toy_drive_system.h"
|
||||||
#include "toy_literal_array.h"
|
#include "toy_literal_array.h"
|
||||||
#include "toy_memory.h"
|
#include "toy_memory.h"
|
||||||
|
|
||||||
#include "lib_runner.h"
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
static int nativeLoadNode(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
static int nativeLoadNode(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||||
@@ -32,7 +31,7 @@ static int nativeLoadNode(Toy_Interpreter* interpreter, Toy_LiteralArray* argume
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_Literal filePathLiteral = Toy_getFilePathLiteral(interpreter, &drivePathLiteral);
|
Toy_Literal filePathLiteral = Toy_getDrivePathLiteral(interpreter, &drivePathLiteral);
|
||||||
|
|
||||||
if (!TOY_IS_STRING(filePathLiteral)) {
|
if (!TOY_IS_STRING(filePathLiteral)) {
|
||||||
Toy_freeLiteral(drivePathLiteral);
|
Toy_freeLiteral(drivePathLiteral);
|
||||||
@@ -358,7 +357,7 @@ static int nativeLoadNodeTexture(Toy_Interpreter* interpreter, Toy_LiteralArray*
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_Literal filePathLiteral = Toy_getFilePathLiteral(interpreter, &drivePathLiteral);
|
Toy_Literal filePathLiteral = Toy_getDrivePathLiteral(interpreter, &drivePathLiteral);
|
||||||
|
|
||||||
if (!TOY_IS_STRING(filePathLiteral)) {
|
if (!TOY_IS_STRING(filePathLiteral)) {
|
||||||
Toy_freeLiteral(drivePathLiteral);
|
Toy_freeLiteral(drivePathLiteral);
|
||||||
@@ -968,7 +967,7 @@ static int nativeSetNodeText(Toy_Interpreter* interpreter, Toy_LiteralArray* arg
|
|||||||
}
|
}
|
||||||
|
|
||||||
//get the font
|
//get the font
|
||||||
Toy_Literal fileLiteral = Toy_getFilePathLiteral(interpreter, &fontLiteral);
|
Toy_Literal fileLiteral = Toy_getDrivePathLiteral(interpreter, &fontLiteral);
|
||||||
|
|
||||||
TTF_Font* font = TTF_OpenFont( Toy_toCString(TOY_AS_STRING(fileLiteral)), TOY_AS_INTEGER(sizeLiteral) );
|
TTF_Font* font = TTF_OpenFont( Toy_toCString(TOY_AS_STRING(fileLiteral)), TOY_AS_INTEGER(sizeLiteral) );
|
||||||
|
|
||||||
|
|||||||
155
box/lib_runner.c
155
box/lib_runner.c
@@ -1,13 +1,12 @@
|
|||||||
#include "lib_runner.h"
|
#include "lib_runner.h"
|
||||||
|
|
||||||
#include "toy_memory.h"
|
#include "toy_memory.h"
|
||||||
|
#include "toy_drive_system.h"
|
||||||
#include "toy_interpreter.h"
|
#include "toy_interpreter.h"
|
||||||
|
|
||||||
#include "repl_tools.h"
|
#include "repl_tools.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
typedef struct Toy_Runner {
|
typedef struct Toy_Runner {
|
||||||
Toy_Interpreter interpreter;
|
Toy_Interpreter interpreter;
|
||||||
@@ -38,7 +37,7 @@ static int nativeLoadScript(Toy_Interpreter* interpreter, Toy_LiteralArray* argu
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_Literal filePathLiteral = Toy_getFilePathLiteral(interpreter, &drivePathLiteral);
|
Toy_Literal filePathLiteral = Toy_getDrivePathLiteral(interpreter, &drivePathLiteral);
|
||||||
|
|
||||||
if (TOY_IS_NULL(filePathLiteral)) {
|
if (TOY_IS_NULL(filePathLiteral)) {
|
||||||
Toy_freeLiteral(filePathLiteral);
|
Toy_freeLiteral(filePathLiteral);
|
||||||
@@ -113,70 +112,19 @@ static int nativeLoadScriptBytecode(Toy_Interpreter* interpreter, Toy_LiteralArr
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_RefString* drivePath = Toy_copyRefString(TOY_AS_STRING(drivePathLiteral));
|
Toy_Literal filePathLiteral = Toy_getDrivePathLiteral(interpreter, &drivePathLiteral);
|
||||||
|
|
||||||
//get the drive and path as a string (can't trust that pesky strtok - custom split) TODO: move this to refstring library
|
if (TOY_IS_NULL(filePathLiteral)) {
|
||||||
size_t driveLength = 0;
|
Toy_freeLiteral(filePathLiteral);
|
||||||
while (Toy_toCString(drivePath)[driveLength] != ':') {
|
|
||||||
if (driveLength >= Toy_lengthRefString(drivePath)) {
|
|
||||||
interpreter->errorOutput("Incorrect drive path format given to loadScriptBytecode\n");
|
|
||||||
Toy_deleteRefString(drivePath);
|
|
||||||
Toy_freeLiteral(drivePathLiteral);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
driveLength++;
|
|
||||||
}
|
|
||||||
|
|
||||||
Toy_RefString* drive = Toy_createRefStringLength(Toy_toCString(drivePath), driveLength);
|
|
||||||
Toy_RefString* path = Toy_createRefStringLength( &Toy_toCString(drivePath)[driveLength + 1], Toy_lengthRefString(drivePath) - driveLength );
|
|
||||||
|
|
||||||
//get the real drive file path
|
|
||||||
Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(drive); //NOTE: driveLiteral takes ownership of the refString
|
|
||||||
Toy_Literal realDriveLiteral = Toy_getLiteralDictionary(Toy_getDriveDictionary(), driveLiteral);
|
|
||||||
|
|
||||||
if (!TOY_IS_STRING(realDriveLiteral)) {
|
|
||||||
interpreter->errorOutput("Incorrect literal type found for drive: ");
|
|
||||||
Toy_printLiteralCustom(realDriveLiteral, interpreter->errorOutput);
|
|
||||||
interpreter->errorOutput("\n");
|
|
||||||
Toy_freeLiteral(realDriveLiteral);
|
|
||||||
Toy_freeLiteral(driveLiteral);
|
|
||||||
Toy_deleteRefString(path);
|
|
||||||
Toy_deleteRefString(drivePath);
|
|
||||||
Toy_freeLiteral(drivePathLiteral);
|
Toy_freeLiteral(drivePathLiteral);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//get the final real file path (concat) TODO: move this concat to refstring library
|
|
||||||
Toy_RefString* realDrive = Toy_copyRefString(TOY_AS_STRING(realDriveLiteral));
|
|
||||||
size_t realLength = Toy_lengthRefString(realDrive) + Toy_lengthRefString(path);
|
|
||||||
|
|
||||||
char* filePath = TOY_ALLOCATE(char, realLength + 1); //+1 for null
|
|
||||||
snprintf(filePath, realLength, "%s%s", Toy_toCString(realDrive), Toy_toCString(path));
|
|
||||||
|
|
||||||
//clean up the drivepath stuff
|
|
||||||
Toy_deleteRefString(realDrive);
|
|
||||||
Toy_freeLiteral(realDriveLiteral);
|
|
||||||
Toy_freeLiteral(driveLiteral);
|
|
||||||
Toy_deleteRefString(path);
|
|
||||||
Toy_deleteRefString(drivePath);
|
|
||||||
Toy_freeLiteral(drivePathLiteral);
|
Toy_freeLiteral(drivePathLiteral);
|
||||||
|
|
||||||
//check for file extensions
|
//use raw types - easier
|
||||||
if (!(filePath[realLength - 4] == '.' && filePath[realLength - 3] == 't' && filePath[realLength - 2] == 'b')) {
|
const char* filePath = Toy_toCString(TOY_AS_STRING(filePathLiteral));
|
||||||
interpreter->errorOutput("Bad binary file extension (expected .tb)\n");
|
size_t filePathLength = Toy_lengthRefString(TOY_AS_STRING(filePathLiteral));
|
||||||
TOY_FREE_ARRAY(char, filePath, realLength);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
//check for break-out attempts
|
|
||||||
for (size_t i = 0; i < realLength - 1; i++) {
|
|
||||||
if (filePath[i] == '.' && filePath[i + 1] == '.') {
|
|
||||||
interpreter->errorOutput("Parent directory access not allowed\n");
|
|
||||||
TOY_FREE_ARRAY(char, filePath, realLength);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//load the bytecode
|
//load the bytecode
|
||||||
size_t fileSize = 0;
|
size_t fileSize = 0;
|
||||||
@@ -203,7 +151,8 @@ static int nativeLoadScriptBytecode(Toy_Interpreter* interpreter, Toy_LiteralArr
|
|||||||
Toy_Literal runnerLiteral = TOY_TO_OPAQUE_LITERAL(runner, TOY_OPAQUE_TAG_RUNNER);
|
Toy_Literal runnerLiteral = TOY_TO_OPAQUE_LITERAL(runner, TOY_OPAQUE_TAG_RUNNER);
|
||||||
Toy_pushLiteralArray(&interpreter->stack, runnerLiteral);
|
Toy_pushLiteralArray(&interpreter->stack, runnerLiteral);
|
||||||
|
|
||||||
TOY_FREE_ARRAY(char, filePath, realLength);
|
//free the drive path
|
||||||
|
Toy_freeLiteral(filePathLiteral);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -602,87 +551,3 @@ int Toy_hookRunner(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Lit
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//file system API
|
|
||||||
static Toy_LiteralDictionary Toy_driveDictionary;
|
|
||||||
|
|
||||||
void Toy_initDriveDictionary() {
|
|
||||||
Toy_initLiteralDictionary(&Toy_driveDictionary);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Toy_freeDriveDictionary() {
|
|
||||||
Toy_freeLiteralDictionary(&Toy_driveDictionary);
|
|
||||||
}
|
|
||||||
|
|
||||||
Toy_LiteralDictionary* Toy_getDriveDictionary() {
|
|
||||||
return &Toy_driveDictionary;
|
|
||||||
}
|
|
||||||
|
|
||||||
Toy_Literal Toy_getFilePathLiteral(Toy_Interpreter* interpreter, Toy_Literal* drivePathLiteral) {
|
|
||||||
//check argument types
|
|
||||||
if (!TOY_IS_STRING(*drivePathLiteral)) {
|
|
||||||
interpreter->errorOutput("Incorrect argument type passed to Toy_getFilePathLiteral\n");
|
|
||||||
return TOY_TO_NULL_LITERAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
Toy_RefString* drivePath = Toy_copyRefString(TOY_AS_STRING(*drivePathLiteral));
|
|
||||||
|
|
||||||
//get the drive and path as a string (can't trust that pesky strtok - custom split) TODO: move this to refstring library
|
|
||||||
size_t driveLength = 0;
|
|
||||||
while (Toy_toCString(drivePath)[driveLength] != ':') {
|
|
||||||
if (driveLength >= Toy_lengthRefString(drivePath)) {
|
|
||||||
interpreter->errorOutput("Incorrect drive path format given to Toy_getFilePathLiteral\n");
|
|
||||||
|
|
||||||
return TOY_TO_NULL_LITERAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
driveLength++;
|
|
||||||
}
|
|
||||||
|
|
||||||
Toy_RefString* drive = Toy_createRefStringLength(Toy_toCString(drivePath), driveLength);
|
|
||||||
Toy_RefString* path = Toy_createRefStringLength( &Toy_toCString(drivePath)[driveLength + 1], Toy_lengthRefString(drivePath) - driveLength );
|
|
||||||
|
|
||||||
//get the real drive file path
|
|
||||||
Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(drive); //NOTE: driveLiteral takes ownership of the refString
|
|
||||||
Toy_Literal realDriveLiteral = Toy_getLiteralDictionary(Toy_getDriveDictionary(), driveLiteral);
|
|
||||||
|
|
||||||
if (!TOY_IS_STRING(realDriveLiteral)) {
|
|
||||||
interpreter->errorOutput("Incorrect literal type found for drive: ");
|
|
||||||
Toy_printLiteralCustom(realDriveLiteral, interpreter->errorOutput);
|
|
||||||
interpreter->errorOutput("\n");
|
|
||||||
Toy_freeLiteral(realDriveLiteral);
|
|
||||||
Toy_freeLiteral(driveLiteral);
|
|
||||||
Toy_deleteRefString(path);
|
|
||||||
Toy_deleteRefString(drivePath);
|
|
||||||
|
|
||||||
return TOY_TO_NULL_LITERAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
//get the final real file path (concat) TODO: move this concat to refstring library
|
|
||||||
Toy_RefString* realDrive = Toy_copyRefString(TOY_AS_STRING(realDriveLiteral));
|
|
||||||
size_t realLength = Toy_lengthRefString(realDrive) + Toy_lengthRefString(path);
|
|
||||||
|
|
||||||
char* filePath = TOY_ALLOCATE(char, realLength + 1); //+1 for null
|
|
||||||
snprintf(filePath, realLength, "%s%s", Toy_toCString(realDrive), Toy_toCString(path));
|
|
||||||
|
|
||||||
//clean up the drivepath stuff
|
|
||||||
Toy_deleteRefString(realDrive);
|
|
||||||
Toy_freeLiteral(realDriveLiteral);
|
|
||||||
Toy_freeLiteral(driveLiteral);
|
|
||||||
Toy_deleteRefString(path);
|
|
||||||
Toy_deleteRefString(drivePath);
|
|
||||||
|
|
||||||
//check for break-out attempts
|
|
||||||
for (size_t i = 0; i < realLength - 1; i++) {
|
|
||||||
if (filePath[i] == '.' && filePath[i + 1] == '.') {
|
|
||||||
interpreter->errorOutput("Parent directory access not allowed\n");
|
|
||||||
TOY_FREE_ARRAY(char, filePath, realLength + 1);
|
|
||||||
return TOY_TO_NULL_LITERAL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Toy_Literal result = TOY_TO_STRING_LITERAL(Toy_createRefStringLength(filePath, realLength));
|
|
||||||
|
|
||||||
TOY_FREE_ARRAY(char, filePath, realLength + 1);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
@@ -1,35 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "toy_common.h"
|
|
||||||
#include "toy_interpreter.h"
|
#include "toy_interpreter.h"
|
||||||
|
|
||||||
int Toy_hookRunner(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Literal alias);
|
int Toy_hookRunner(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Literal alias);
|
||||||
|
|
||||||
#define TOY_OPAQUE_TAG_RUNNER 100
|
#define TOY_OPAQUE_TAG_RUNNER 100
|
||||||
|
|
||||||
//platform/compiler-specific instructions - because MSVC + Box Engine are dumber than a bag of rocks
|
|
||||||
#if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__)
|
|
||||||
|
|
||||||
#define LIB_RUNNER_API extern
|
|
||||||
|
|
||||||
#elif defined(_MSC_VER)
|
|
||||||
|
|
||||||
#ifndef LIB_RUNNER_EXPORT
|
|
||||||
#define LIB_RUNNER_API __declspec(dllimport)
|
|
||||||
#else
|
|
||||||
#define LIB_RUNNER_API __declspec(dllexport)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
#define LIB_RUNNER_API extern
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//file system API - these need to be set by the host
|
|
||||||
LIB_RUNNER_API void Toy_initDriveDictionary();
|
|
||||||
LIB_RUNNER_API void Toy_freeDriveDictionary();
|
|
||||||
LIB_RUNNER_API Toy_LiteralDictionary* Toy_getDriveDictionary();
|
|
||||||
|
|
||||||
//file system API - for use with other libs
|
|
||||||
LIB_RUNNER_API Toy_Literal Toy_getFilePathLiteral(Toy_Interpreter* interpreter, Toy_Literal* drivePathLiteral);
|
|
||||||
|
|||||||
@@ -14,54 +14,18 @@ int main(int argc, char* argv[]) {
|
|||||||
#endif//win32 && debug
|
#endif//win32 && debug
|
||||||
|
|
||||||
//the drive system uses a LiteralDictionary, which must be initialized with this
|
//the drive system uses a LiteralDictionary, which must be initialized with this
|
||||||
Toy_initDriveDictionary();
|
Toy_initDriveSystem();
|
||||||
|
|
||||||
{
|
Toy_setDrivePath("scripts", "assets/scripts");
|
||||||
//create a pair of literals, the first for the drive name, the second for the path
|
Toy_setDrivePath("sprites", "assets/sprites");
|
||||||
Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("scripts"));
|
Toy_setDrivePath("fonts", "assets/fonts");
|
||||||
Toy_Literal pathLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("assets/scripts"));
|
|
||||||
|
|
||||||
//set these within the drive dictionary
|
|
||||||
Toy_setLiteralDictionary(Toy_getDriveDictionary(), driveLiteral, pathLiteral);
|
|
||||||
|
|
||||||
//these literals are no longer needed
|
|
||||||
Toy_freeLiteral(driveLiteral);
|
|
||||||
Toy_freeLiteral(pathLiteral);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
//create a pair of literals, the first for the drive name, the second for the path
|
|
||||||
Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("sprites"));
|
|
||||||
Toy_Literal pathLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("assets/sprites"));
|
|
||||||
|
|
||||||
//set these within the drive dictionary
|
|
||||||
Toy_setLiteralDictionary(Toy_getDriveDictionary(), driveLiteral, pathLiteral);
|
|
||||||
|
|
||||||
//these literals are no longer needed
|
|
||||||
Toy_freeLiteral(driveLiteral);
|
|
||||||
Toy_freeLiteral(pathLiteral);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
//create a pair of literals, the first for the drive name, the second for the path
|
|
||||||
Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("fonts"));
|
|
||||||
Toy_Literal pathLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("assets/fonts"));
|
|
||||||
|
|
||||||
//set these within the drive dictionary
|
|
||||||
Toy_setLiteralDictionary(Toy_getDriveDictionary(), driveLiteral, pathLiteral);
|
|
||||||
|
|
||||||
//these literals are no longer needed
|
|
||||||
Toy_freeLiteral(driveLiteral);
|
|
||||||
Toy_freeLiteral(pathLiteral);
|
|
||||||
}
|
|
||||||
|
|
||||||
//run the rest of your program
|
|
||||||
Box_initEngine();
|
Box_initEngine();
|
||||||
Box_execEngine();
|
Box_execEngine();
|
||||||
Box_freeEngine();
|
Box_freeEngine();
|
||||||
|
|
||||||
//clean up the drive dictionary when you're done
|
//clean up the drive dictionary when you're done
|
||||||
Toy_freeDriveDictionary();
|
Toy_freeDriveSystem();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user