22 #include <torquescript/nativefunction.hpp>
23 #include <torquescript/executionscope.hpp>
24 #include <torquescript/interpreter.hpp>
25 #include <torquescript/executionstate.hpp>
26 #include <torquescript/storedvaluestack.hpp>
27 #include <torquescript/consoleobject.hpp>
28 #include <torquescript/fileobject.hpp>
32 static void EchoBuiltIn(std::shared_ptr<ConsoleObject> thisObject, std::shared_ptr<ExecutionState> state,
const std::size_t argumentCount)
34 StoredValueStack& stack = state->mExecutionScope.getStack();
36 std::string outputString =
"";
38 for (
unsigned int iteration = 0; iteration < argumentCount; ++iteration)
41 std::string printedPayload = stack.popString(state);
42 outputString = printedPayload + outputString;
45 state->mInterpreter->mConfig.mPlatform->logEcho(outputString);
46 stack.push_back(StoredValue(0));
49 static void ExecBuiltIn(std::shared_ptr<ConsoleObject> thisObject, std::shared_ptr<ExecutionState> state,
const std::size_t argumentCount)
51 StoredValueStack& stack = state->mExecutionScope.getStack();
53 for (
unsigned int iteration = 0; iteration < argumentCount; ++iteration)
55 std::string executedFile = stack.popString(state);
57 std::ostringstream output;
58 output <<
"Executing " << executedFile <<
" ...";
59 state->mInterpreter->mConfig.mPlatform->logEcho(output.str());
61 state->mInterpreter->execute(executedFile,
nullptr);
64 stack.push_back(StoredValue(0));
67 static void ActivatePackageBuiltIn(std::shared_ptr<ConsoleObject> thisObject, std::shared_ptr<ExecutionState> state,
const std::size_t argumentCount)
69 StoredValueStack& stack = state->mExecutionScope.getStack();
71 for (
unsigned int iteration = 0; iteration < argumentCount; ++iteration)
73 std::string activatedPackage = stack.popString(state);
74 state->mInterpreter->activateFunctionRegistry(activatedPackage);
77 stack.push_back(StoredValue(0));
80 static void DeactivatePackageBuiltIn(std::shared_ptr<ConsoleObject> thisObject, std::shared_ptr<ExecutionState> state,
const std::size_t argumentCount)
82 StoredValueStack& stack = state->mExecutionScope.getStack();
84 for (
unsigned int iteration = 0; iteration < argumentCount; ++iteration)
86 std::string deactivatedPackage = stack.popString(state);
87 state->mInterpreter->deactivateFunctionRegistry(deactivatedPackage);
90 stack.push_back(StoredValue(0));
93 static void DeleteBuiltIn(std::shared_ptr<ConsoleObject> thisObject, std::shared_ptr<ExecutionState> state,
const std::size_t argumentCount)
95 StoredValueStack& stack = state->mExecutionScope.getStack();
97 state->mInterpreter->mConsoleObjectRegistry.removeConsoleObject(thisObject);
98 stack.push_back(StoredValue(0));
101 static void GetNameBuiltIn(std::shared_ptr<ConsoleObject> thisObject, std::shared_ptr<ExecutionState> state,
const std::size_t argumentCount)
103 StoredValueStack& stack = state->mExecutionScope.getStack();
105 const std::size_t stringID = state->mInterpreter->mStringTable.getOrAssign(state->mInterpreter->mConsoleObjectRegistry.getConsoleObjectName(thisObject));
106 stack.push_back(StoredValue(stringID, StoredValueType::String));
109 static void GetClassNameBuiltIn(std::shared_ptr<ConsoleObject> thisObject, std::shared_ptr<ExecutionState> state,
const std::size_t argumentCount)
111 StoredValueStack& stack = state->mExecutionScope.getStack();
113 const std::size_t stringID = state->mInterpreter->mStringTable.getOrAssign(thisObject->getClassName());
114 stack.push_back(StoredValue(stringID, StoredValueType::String));
117 static void registerCoreLibrary(Interpreter* interpreter)
119 interpreter->addFunction(std::shared_ptr<Function>(
new NativeFunction(EchoBuiltIn, PACKAGE_EMPTY, NAMESPACE_EMPTY,
"echo")));
120 interpreter->addFunction(std::shared_ptr<Function>(
new NativeFunction(ExecBuiltIn, PACKAGE_EMPTY, NAMESPACE_EMPTY,
"exec")));
121 interpreter->addFunction(std::shared_ptr<Function>(
new NativeFunction(ActivatePackageBuiltIn, PACKAGE_EMPTY, NAMESPACE_EMPTY,
"activatePackage")));
122 interpreter->addFunction(std::shared_ptr<Function>(
new NativeFunction(DeactivatePackageBuiltIn, PACKAGE_EMPTY, NAMESPACE_EMPTY,
"deactivatePackage")));
124 interpreter->addFunction(std::shared_ptr<Function>(
new NativeFunction(GetClassNameBuiltIn, PACKAGE_EMPTY,
"ConsoleObject",
"getClassName")));
125 interpreter->addFunction(std::shared_ptr<Function>(
new NativeFunction(GetNameBuiltIn, PACKAGE_EMPTY,
"ConsoleObject",
"getName")));
126 interpreter->addFunction(std::shared_ptr<Function>(
new NativeFunction(DeleteBuiltIn, PACKAGE_EMPTY,
"ConsoleObject",
"delete")));