2021-02-11 01:23:48 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Thakee Nathees
|
|
|
|
* Licensed under: MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CORE_H
|
|
|
|
#define CORE_H
|
|
|
|
|
|
|
|
#include "var.h"
|
2021-02-25 17:03:06 +08:00
|
|
|
#include "common.h"
|
2021-02-12 01:35:43 +08:00
|
|
|
|
2021-04-25 23:19:39 +08:00
|
|
|
// Initialize core language, builtin function and "std" scripts.
|
|
|
|
// Note (TODO: refactore required):
|
|
|
|
// Since the builtin function doesn't require any allocation they're
|
|
|
|
// elements of a static `builtins` array but the "std" scripts are `Script`
|
|
|
|
// objects they required memory management and they're bound with the VM.
|
|
|
|
// It contradicts `initializeCore()` to be called for each VM or only once.
|
|
|
|
// 1. Make the scripts share between VMs.
|
|
|
|
// 2. Destroy scripts buffer only when the last VM die.
|
2021-05-09 18:28:00 +08:00
|
|
|
void initializeCore(PKVM* vm);
|
2021-02-12 01:35:43 +08:00
|
|
|
|
2021-04-26 17:34:30 +08:00
|
|
|
// Mark the heap allocated core object at the mark phase.
|
2021-05-09 18:28:00 +08:00
|
|
|
void markCoreObjects(PKVM* vm);
|
2021-04-26 17:34:30 +08:00
|
|
|
|
2021-02-12 01:35:43 +08:00
|
|
|
// Find the builtin function name and returns it's index in the builtins array
|
|
|
|
// if not found returns -1.
|
2021-05-05 13:55:34 +08:00
|
|
|
int findBuiltinFunction(const char* name, uint32_t length);
|
2021-02-12 01:35:43 +08:00
|
|
|
|
2021-02-18 02:27:24 +08:00
|
|
|
// Returns the builtin function at index [index].
|
2021-02-12 01:35:43 +08:00
|
|
|
Function* getBuiltinFunction(int index);
|
|
|
|
|
2021-02-18 02:27:24 +08:00
|
|
|
// Returns the builtin function's name at index [index].
|
|
|
|
const char* getBuiltinFunctionName(int index);
|
|
|
|
|
2021-05-07 17:41:19 +08:00
|
|
|
// Return the core library with the [name] if exists in the core libs,
|
|
|
|
// otherwise returns NULL.
|
|
|
|
Script* getCoreLib(String* name);
|
|
|
|
|
2021-02-12 01:35:43 +08:00
|
|
|
// Operators //////////////////////////////////////////////////////////////////
|
2021-02-11 01:23:48 +08:00
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
Var varAdd(PKVM* vm, Var v1, Var v2);
|
|
|
|
Var varSubtract(PKVM* vm, Var v1, Var v2);
|
|
|
|
Var varMultiply(PKVM* vm, Var v1, Var v2);
|
|
|
|
Var varDivide(PKVM* vm, Var v1, Var v2);
|
2021-02-11 01:23:48 +08:00
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
bool varGreater(PKVM* vm, Var v1, Var v2);
|
|
|
|
bool varLesser(PKVM* vm, Var v1, Var v2);
|
2021-02-16 02:51:00 +08:00
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
Var varGetAttrib(PKVM* vm, Var on, String* attrib);
|
|
|
|
void varSetAttrib(PKVM* vm, Var on, String* name, Var value);
|
2021-02-16 02:51:00 +08:00
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
Var varGetSubscript(PKVM* vm, Var on, Var key);
|
|
|
|
void varsetSubscript(PKVM* vm, Var on, Var key, Var value);
|
2021-02-15 20:49:19 +08:00
|
|
|
|
2021-02-12 14:53:52 +08:00
|
|
|
// Functions //////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Parameter [iterator] should be VAR_NULL before starting the iteration.
|
|
|
|
// If an element is obtained by iteration it'll return true otherwise returns
|
|
|
|
// false indicating that the iteration is over.
|
2021-05-09 18:28:00 +08:00
|
|
|
bool varIterate(PKVM* vm, Var seq, Var* iterator, Var* value);
|
2021-02-12 01:35:43 +08:00
|
|
|
|
2021-02-11 01:23:48 +08:00
|
|
|
#endif // CORE_H
|