Compare commits

..

116 Commits

Author SHA1 Message Date
5711ca218e improved readme, added asciinema demo 2023-12-29 18:18:22 -06:00
5cd3049d66 cdebug: added cosmoG_disassemble
for now cosmo will disassemble each script before running
2023-12-29 18:17:52 -06:00
cd37cfdae5 cparse: removed stale expressionStatement forward declare 2023-12-29 18:01:33 -06:00
e0455902b0 'return' is now a valid statement outside of a function
lets us do some cool things later. also the repl can print returned values. eg.

`return "hello world"` will print `<string> "hello world"` to the console
2023-12-28 23:41:10 -06:00
43d79a456e cparse.c: removed expressionStatement() 2023-12-28 23:27:08 -06:00
105a3d70c3 cparse.c: refactoring
- remove declaration(), replace calls with statement()
- fix possible UB in parsePrecedence; no idea how this *wasn't* causing a segfault tbh
2023-12-28 23:20:19 -06:00
93f3ae1106 cvm.c:cosmoV_printError -> cosmoV_printBacktrace 2023-12-28 22:56:57 -06:00
4816e64612 cvalue.c:printValue renamed to cosmoV_printValue 2023-12-28 22:52:20 -06:00
0df56bd42a ctable: hash COBJ_CFUNCTION based on cfunc and not the actual <obj> 2023-12-28 19:56:25 -06:00
e7b2d7d833 cbaselib.c:fileB_read() fix MSVC warnings 2023-12-27 21:12:12 -06:00
39060a67e9 fix cosmoB_loadObjLib()
- wrong # of pairs passed to cosmoV_makeTable
- minor refactoring of cmem.c:sweep()
2023-12-26 12:27:45 -06:00
5296495e47 formatting fixes 2023-12-26 10:59:42 -06:00
a337e26229 added <obj>:keys() method
this method returns a table of keys or 'fields' available in the object
2023-12-25 19:08:04 -06:00
b7bb0773b1 removed stale WRITE_GLOBAL_OP macro 2023-12-25 19:08:04 -06:00
7f5e3ae8dc lol oops 2023-11-03 22:59:31 -05:00
7a54230cb9 hated the styling of this
consistency is nice to have
2023-11-03 22:36:59 -05:00
1a7d6caec6 fix __proto object getter 2023-09-11 22:17:35 -05:00
1678194941 minor refactoring 2023-09-06 20:23:18 -05:00
3ea653b26d added file:write() & an optional mode param to os.open() 2023-09-06 17:29:38 -05:00
d3de4c0e66 removed more debug prints 2023-09-05 14:45:03 -05:00
d66d4807b3 removed debug prints, oops 2023-09-05 14:43:50 -05:00
1fcb35168f fix makefile 2023-09-05 14:42:29 -05:00
611162b3be documented the new os.open and file objects 2023-09-05 14:41:59 -05:00
b3587f48a2 added os.open(); new file object
eg.
```lua
local err, file = os.open("LICENSE.md")
if err then
    // do error handling stuff
end

// passing "a" to read() will read the whole file
print(file:read("a"))
```
2023-09-05 14:38:24 -05:00
bf36412699 removed 'roots', replaced with a registry table
- removed cosmoM_addRoot
- removed cosmoM_removeRoot
- renamed cosmoV_register to cosmoV_addGlobals
- added cosmoV_addRegistry
- added cosmoV_getRegistry
- added cosmoV_setProto
- added cosmoV_isValueUserType
2023-09-05 14:35:29 -05:00
6701a63a63 capture freezeGC in CPanic 2023-09-05 02:23:31 -05:00
ffff01e9d1 build a release build for linux as well 2023-09-04 20:15:15 -05:00
89be01aaf6 even more error handling refactoring
removing all of these useless checks has actually made cosmoV_execute just a
lil bit faster :)
2023-09-04 20:14:53 -05:00
cc9eb4a5ec lol oops 2023-09-01 17:16:10 -05:00
789c5210b4 switched to a better linenoise fork
https://github.com/msteveb/linenoise

this version has several benefits, namely win32 support :D
2023-09-01 17:12:39 -05:00
dfdd97e739 fix artifacts path 2023-09-01 14:43:20 -05:00
096d80d8df better repl input, using linenoise 2023-08-31 23:17:13 -05:00
f7bc8e0471 include util path 2023-08-31 23:16:28 -05:00
fce568addc whoops, wrong path to the workflow file 2023-08-30 21:30:20 -05:00
f5e75f09b9 ig that runner doesn't work lol 2023-08-30 21:28:50 -05:00
fe136f84b5 removed CERROR 2023-08-30 20:14:03 -05:00
de8cd481c3 allow manual runs 2023-08-30 20:12:06 -05:00
6654c3b91c wrong output path for windows build 2023-08-30 20:09:06 -05:00
21f7ea5c14 oops 2023-08-30 20:03:30 -05:00
e1591ae3fd switched from appveyor to github workflow 2023-08-30 20:02:04 -05:00
bfdd33e01d fix vm.collect()
we don't freeze the vm on entry to C functions now
2023-08-26 15:03:56 -05:00
c0893b8a14 more refactoring; things seem to work fine
all example scripts run fine with GC_STRESS enabled
2023-08-30 12:00:52 -05:00
d30bcace9a don't freezeGC during GC cycle 2023-08-29 23:32:25 -05:00
6a47c82179 fix more GC bugs 2023-08-29 23:21:52 -05:00
d41126e75f fix cparse.c gc bug 2023-08-29 23:01:47 -05:00
9f19fd4f31 fix this test script 2023-08-29 16:51:04 -05:00
6126b50941 cosmoV_throw() now resets the vm stack as well
also a minor GC bug in cosmoO_newError was fixed.

i'm going to try to phase out cosmoM_freezeGC & friends
since that would cause hell with this new
error handling solution. the only thing still using it is the GC.
2023-08-29 16:48:38 -05:00
7fa7eb8d94 fixed minor memory leak in cparse
we keep track of internal values used by the parser by pushing them onto the stack
and popping them off once complete.
2023-08-29 15:27:22 -05:00
0633e87aa6 WIP: removed stale error handling
currently, scripts seem to run fine. however I'm a bit worried about stack related issues. maybe i'll need to reset state->top as well? but not entirely sure
2023-08-29 14:07:45 -05:00
75d27afe2c WIP: major error handling refactoring
switching to setjmp instead of the really bad global 'panic' flag
2023-08-28 21:13:00 -05:00
3f39211081 minor refactoring 2023-08-25 23:34:21 -05:00
c5e4305ef8 refactored cobj.c:printObject()
uses obj type strings from cosmoO_typeStr
2023-08-25 21:28:41 -05:00
1a78a9ab5f finally fixed this memory bug
we were accidentally tracking frees of stuff that was never
allocated lol
2023-08-25 21:22:10 -05:00
2f0f675159 more debug related refactoring 2023-08-25 20:44:24 -05:00
7c5d2f6b65 minor CTable refactoring 2023-08-25 19:57:16 -05:00
f76f2ffa92 refactoring and cleanup
cosmoB_loadOSLib -> cosmoB_loadOS
2023-08-24 23:36:32 -05:00
155e0829fb minor refactoring 2023-06-03 01:39:35 -05:00
2b3825d258 implemented jump table dispatch
- currently only enabled on supported platforms (GNU C Compiler + Clang)
- when enabled, sped up examples/fibtest.cosmo by about 20% (avg of 11.179s prior and 8.799 after)

NOTE: malicious dumps can trivially cause crashes now by having junk function chunks
2023-06-03 01:17:28 -05:00
7bca6927a9 fixed formatting 2023-06-01 22:28:07 -05:00
d3647c161b added vm.disassemble() 2023-06-01 19:04:12 -05:00
d27d94975e fixed MSVC support 2023-06-01 22:22:44 -05:00
2d0e63f706 forgot to update this a while back 2023-05-28 21:16:30 -05:00
dfcf0c92b5 whoops, need to update the command to run the testsuite 2023-05-28 21:13:51 -05:00
447f874eff update README.md 2023-05-28 21:11:52 -05:00
7b1bd1c9fc minor usage fix 2023-05-28 20:57:53 -05:00
9537a2c7b8 major main.c refactoring
- now takes command line arguments:
Usage: ./bin/cosmo [-clsr] [args]

available options are:
-c <in> <out>   compile <in> and dump to <out>
-l <in>         load dump from <in>
-s <in...>              compile and run <in...> script(s)
-r                      start the repl
2023-05-28 20:52:12 -05:00
c44dc88972 minor stack fixes 2023-05-28 12:19:52 -05:00
d581e68166 proper error handling for dump edgecases 2023-05-28 12:16:00 -05:00
2271681cec undef macros 2023-05-28 12:03:49 -05:00
cf18bbbe54 fixed GC bug in cundump.c 2023-05-28 11:55:48 -05:00
3a872fb83f these values can be defined at the top of the function 2023-05-28 00:08:28 -05:00
4ed1c79b50 formatting changes 2023-05-28 00:03:50 -05:00
bc6eb9b6dc added cdump.[ch] and cundump.[ch] to makefile 2023-05-27 23:17:17 -05:00
635f31863f cvm.c: added cosmoV_undump 2023-05-27 23:16:47 -05:00
49a7f68239 cdump.c: major refactoring; various fixes 2023-05-27 23:15:58 -05:00
8efecf71a4 cdump.c: fix instruction and line info dumping 2023-05-27 23:15:12 -05:00
395f352c6e started cundump.c:cosmoD_undump(); minor fixes 2023-05-25 21:12:21 -05:00
65d37838cd started cdump.c:cosmoD_dump() 2023-05-25 19:41:13 -05:00
3b13ae1624 minor refactoring and typos 2023-05-25 19:40:15 -05:00
d1a16d990c removed stale COBJ_STREAM usage 2023-05-25 18:34:39 -05:00
0a4d36f2f4 update Docs to reflect keyword changes 2023-02-10 20:46:05 -06:00
8ac8085d20 syntax: 'var'->'let' 'function'->'func'
- 'var' has some weird scoping connotations with users of JS. better to change it to 'let', which will decide whether to make the variable a local or a global
- 'func' looks visually appealing lol
- some minor refactoring done in cparse.c
2023-02-09 15:58:25 -06:00
e335fd95d6 minor formatting fixes 2023-02-09 12:52:36 -06:00
b902ac90de removed CObjStream 2023-02-09 12:42:09 -06:00
6056f8eb5b added clang-format 2023-02-09 12:32:48 -06:00
88284a0b6e Removed '-Werror' to the Makefile 2021-06-11 15:01:53 -05:00
Inversion
7998c2ab41 Add documentation for the OS library 2023-11-03 22:55:24 -05:00
7b5825668d Added boilerplate for CObjStream 2021-03-20 01:44:03 -05:00
d13cc398c8 Added os.system() to the os.* library 2021-03-20 01:02:13 -05:00
012d3702bf Updated Appveyor to test testsuite 2021-03-19 22:25:23 -05:00
d761970f17 Added minimal testsuite for IC
- main.c will now report errors for passed scripts
2021-03-19 22:23:04 -05:00
0e730b9c51 Added svg to README, whitelisted commits only effecting the binary 2021-03-16 15:05:20 -05:00
bff2799bb6 Added AppVeyor CI 2021-03-16 14:54:44 -05:00
07ca82f968 Added '__equal' metamethod docs 2021-02-24 12:52:31 -06:00
b545e8e5e3 Added another example script 'compare.comso'
- This stresses the table implementation as well as cosmoV_equals
2021-02-23 11:50:41 -06:00
55e6453589 Improved cosmoO_equals performance for strings 2021-02-23 11:49:57 -06:00
c83dca2ab2 Added '__equal' metamethod, slightly refactored cosmoO_equal
- ISTRING_EQUAL has been added
2021-02-20 12:42:13 -06:00
3890c9dd1e Refactored cosmoO_equals
This sets up room for the '__equal' metamethod to be added

- cosmoO_equals now requires the state to be passed
- cosmoV_equals now requires the state to be passed
- cosmoT_get now requires the state to be passed
2021-02-19 17:04:23 -06:00
40739e9bea cparse.c now throws an error on illegal statements 2021-02-19 12:47:12 -06:00
Inversion
1eec23035f Temporary fix for possible bug 2023-11-03 22:55:19 -05:00
Inversion
c0274d1d77 Update standard library documentation 2023-11-03 22:55:14 -05:00
Inversion
fec26ac380 Add optional custom error message to assert 2023-11-03 22:55:09 -05:00
35466f691f Added C99 support, refactored headers 2021-02-15 16:20:04 -06:00
71c8dc7e34 Added basic standard library docs 2021-02-15 14:25:08 -06:00
7a6e00be41 Added math.rad() and math.deg() 2021-02-15 14:06:43 -06:00
14b091b691 Added trig. functions to the math library 2021-02-15 14:00:26 -06:00
5c71efbe40 Added OP_POW to cdebug.c; refactored cosmoV_registerProtoObject
- cosmoV_registerProtoObject now walks the object list and updates the proto's for objects of the objType which have a NULL proto.
- misc. comment changes in cvm.h as well.
2021-02-13 20:08:35 -06:00
1fff6c7fe9 Added string.rep() to the string library 2021-02-13 19:07:47 -06:00
1a96e411f2 Minor Makefile fix 2021-02-11 20:35:42 -06:00
fdd0d19308 Added CMake support
should make integration into visual studio easier for people
2021-02-11 20:34:04 -06:00
33da88a18a Minor cbaselib.c cleanup 2021-02-11 00:58:12 -06:00
Inversion
50b19e9f4f Added argument type check to string.len 2023-11-03 22:55:03 -05:00
Inversion
472a0ea4c1 Updated baselib in accordance with cosmoV_readCString 2023-11-03 22:54:56 -05:00
Inversion
76574c7860 Added cosmoV_readCString for convenience 2023-11-03 22:54:51 -05:00
Inversion
8b931fa4a7 Add string.len to base library 2023-11-03 22:54:45 -05:00
Inversion
ce844dc110 Added error to the base library 2023-11-03 22:53:59 -05:00
43 changed files with 5783 additions and 1430 deletions

45
.github/workflows/check_build.yaml vendored Normal file
View File

@@ -0,0 +1,45 @@
name: Check Builds
on:
push:
paths:
- src/**
- util/**
- main.c
- Makefile
- CMakeLists.txt
- .github/workflows/check_build.yaml
workflow_dispatch:
jobs:
ubuntu-build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: CMake
run: cmake -B build -DCMAKE_BUILD_TYPE=Release
- name: Build
run: cmake --build build --config Release
- name: Upload build artifact
uses: actions/upload-artifact@v2
with:
name: Cosmo-Ubuntu
path: build/bin
windows-build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Create CMake build files
run: cmake -B build -DCMAKE_BUILD_TYPE=MinSizeRel
- name: Check compilation
run: cmake --build build --config MinSizeRel
- name: Upload build artifact
uses: actions/upload-artifact@v2
with:
name: Cosmo-Windows
path: build/bin

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
*.o
bin
build
.vscode
CMakeFiles

View File

@@ -3,20 +3,25 @@ project(cosmo VERSION 0.1.0 LANGUAGES C)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED True)
set(CMAKE_DISABLE_SOURCE_CHANGES ON CACHE BOOL "Prevent writing files to CMAKE_SOURCE_DIR under configure")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin)
set (CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -Wall")
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fsanitize=address")
set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -Wall")
IF (NOT WIN32)
set (CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fsanitize=address")
set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -Wall")
ENDIF()
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT cosmo)
include(FetchContent)
file(GLOB sources CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/src/*.c)
add_executable(${PROJECT_NAME} main.c)
add_executable(${PROJECT_NAME} main.c ${PROJECT_SOURCE_DIR}/util/linenoise.c)
target_sources(${PROJECT_NAME} PRIVATE ${sources})
target_link_libraries(${PROJECT_NAME} m)
IF (NOT WIN32)
target_link_libraries(${PROJECT_NAME} m)
ENDIF()
target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/src)
target_compile_features(${PROJECT_NAME} PRIVATE c_std_99)

View File

@@ -1,8 +1,8 @@
# make clean && make && ./bin/cosmo
CC=clang
CFLAGS=-fPIE -Wall -O0 -Isrc -std=c99 -g
LDFLAGS=-lm -fsanitize=address
CFLAGS=-fPIE -Wall -Isrc -O3 #-g -fsanitize=address
LDFLAGS=-lm #-fsanitize=address
OUT=bin/cosmo
CHDR=\
@@ -21,6 +21,7 @@ CHDR=\
src/cbaselib.h\
src/cdump.h\
src/cundump.h\
util/linenoise.h\
CSRC=\
src/cchunk.c\
@@ -37,6 +38,7 @@ CSRC=\
src/cbaselib.c\
src/cdump.c\
src/cundump.c\
util/linenoise.c\
main.c\
COBJ=$(CSRC:.c=.o)

View File

@@ -1,5 +1,23 @@
# Cosmo
[![AppVeyor](https://ci.appveyor.com/api/projects/status/github/CPunch/Cosmo?svg=true)](https://ci.appveyor.com/project/CPunch/Cosmo)
```
Usage: ./bin/cosmo [-clsr] [args]
available options are:
-c <in> <out> compile <in> and dump to <out>
-l <in> load dump from <in>
-s <in...> compile and run <in...> script(s)
-r start the repl
```
<p align="center">
<a href="https://github.com/CPunch/Cosmo/actions/workflows/check_build.yaml"><img src="https://github.com/CPunch/Cosmo/actions/workflows/check_build.yaml/badge.svg?branch=main" alt="Workflow"></a>
<a href="https://github.com/CPunch/Cosmo/blob/main/LICENSE.md"><img src="https://img.shields.io/github/license/CPunch/Cosmo" alt="License"></a>
<br>
<a href="https://asciinema.org/a/629355" target="_blank"><img src="https://asciinema.org/a/629355.svg" /></a>
</p>
## What is a 'cosmo'?
Cosmo is a portable scripting language loosely based off of Lua. Cosmo easily allows the user to extend the language through the use of Proto objects, which describe the behavior of Objects. For example the following is a simple Vector Proto which describes behavior for a Vector-like object.
@@ -39,7 +57,4 @@ end
2 : 1
1 : 2
0 : 3
```
# C API
The Cosmo C API is currently undocumented, however as soon as development has reached a stable state documentation on full language features and the C API will start.
```

View File

@@ -1,33 +0,0 @@
version: 'cosmo-0.1.{build}'
# we compile every commit under all branches
#branch:
# only:
# - main
# only run CI if we changed actual code
only_commits:
files:
- src/
- main.c
- Makefile
- CMakeLists.txt
- appveyor.yml
# images we're using
image:
- Ubuntu2004
platform:
- x64
install:
- sudo apt install clang cmake -y
build_script:
- make && ./bin/cosmo examples/testsuite.cosmo examples/getters_setters.cosmo
artifacts:
- path: bin
name: ubuntu20_04-bin-x64
type: zip

View File

@@ -59,7 +59,14 @@ Includes functions that interact with the operating system.
| Name | Type | Behavior | Example |
| ------------ | ------------------------------------------------ | ------------------------------------------------------------------------ | ------------------------ |
| os.read | `(path<string>)` -> `<string>` or `<nil>` | Returns a file's contents or nil if it doesn't exist/an error occurred | `os.read("path")` -> `Hello, World!`|
| os.open | `(path<string>[, mode<string>])` -> `<bool>, <obj>` | Opens a file at `path` and returns a file object. Specify mode to be "r" or "w" optionally, defaults to "r". | `os.open("test.txt")` -> `true, <file>` |
| os.time | `()` -> `<number>` | Returns the system time in Epoch format | `os.time()` -> `1.61691e+09` |
| os.system | `(cmd<string>)` -> `<number>` | Runs a system command as if it were a terminal and returns the exit code | `os.system("mkdir test")` -> `0` |
> -> means 'returns'
> -> means 'returns'
File objects have the following methods:
| Name | Type | Behavior | Example |
| ------------ | ------------------------------------------------ | ------------------------------------------------------------------------ | ------------------------ |
| file:read | `(amt<number> or "a")` -> `<string>` | Reads `amt` bytes from the file and returns them as a string. If `"a"` is passed, the entire file is read. | `file:read("a")` -> `"Hello world!"` |
| file:write | `(data<string>)` -> `<nil>` | Writes `data` to file. | `file:write("hello world!")` -> `<nil>` |
> -> means 'returns'

View File

@@ -1,13 +1,13 @@
let object = {
__setter = [
"field1" = function(self, val)
"field1" = func(self, val)
print("setter for field1 called!")
self.x = val
end
],
__getter = [
"field1" = function(self)
"field1" = func(self)
print("getter for field1 called!")
return self.x + 1

6
examples/reader.cosmo Normal file
View File

@@ -0,0 +1,6 @@
local err, file = os.open("LICENSE.md")
if err then
print("failed to open file")
end
print(file:read("a"))

View File

@@ -6,7 +6,7 @@ end
// instance of test
let obj = test()
test.__index = function(self, key)
test.__index = func(self, key)
print("__index called!")
if (key == "lol") then
return 9001

6
examples/writer.cosmo Normal file
View File

@@ -0,0 +1,6 @@
local err, file = os.open("test.md", "w")
if err then
print("failed to open file")
end
file:write("hello world")

208
main.c
View File

@@ -1,21 +1,34 @@
#include "cosmo.h"
#include "cbaselib.h"
#include "cchunk.h"
#include "cdebug.h"
#include "cvm.h"
#include "cparse.h"
#include "cbaselib.h"
#include "cdump.h"
#include "cmem.h"
#include "cosmo.h"
#include "cparse.h"
#include "cundump.h"
#include "cvm.h"
#include "util/linenoise.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
# include "util/getopt.h"
#else
# include <getopt.h>
#endif
static bool _ACTIVE = false;
int cosmoB_quitRepl(CState *state, int nargs, CValue *args) {
int cosmoB_quitRepl(CState *state, int nargs, CValue *args)
{
_ACTIVE = false;
return 0; // we don't return anything
}
int cosmoB_input(CState *state, int nargs, CValue *args) {
int cosmoB_input(CState *state, int nargs, CValue *args)
{
// input() accepts the same params as print()!
for (int i = 0; i < nargs; i++) {
CObjString *str = cosmoV_toString(state, args[i]);
@@ -26,39 +39,46 @@ int cosmoB_input(CState *state, int nargs, CValue *args) {
char line[1024];
fgets(line, sizeof(line), stdin);
cosmoV_pushRef(state, (CObj*)cosmoO_copyString(state, line, strlen(line)-1)); // -1 for the \n
cosmoV_pushRef(state,
(CObj *)cosmoO_copyString(state, line, strlen(line) - 1)); // -1 for the \n
return 1; // 1 return value
}
static bool interpret(CState *state, const char *script, const char *mod) {
bool ret;
static bool interpret(CState *state, const char *script, const char *mod)
{
// cosmoV_compileString pushes the result onto the stack (COBJ_ERROR or COBJ_CLOSURE)
if (cosmoV_compileString(state, script, mod)) {
COSMOVMRESULT res = cosmoV_call(state, 0, 0); // 0 args being passed, 0 results expected
if (cosmoV_compileString(state, script, mod)) {
cosmoG_disassemble(cosmoV_readClosure(*cosmoV_getTop(state, 0)));
if (!cosmoV_pcall(state, 0, 1)) {
cosmoV_printBacktrace(state, cosmoV_readError(*cosmoV_pop(state)));
return false;
}
if (res == COSMOVM_RUNTIME_ERR)
cosmoV_printError(state, state->error);
// if the result is nil, we don't print it
if (IS_NIL(*cosmoV_getTop(state, 0))) {
cosmoV_pop(state);
return true;
}
// otherwise, we print the result
cosmoV_printValue(*cosmoV_getTop(state, 0));
printf("\n");
cosmoV_pop(state);
} else {
cosmoV_pop(state); // pop the error off the stack
cosmoV_printError(state, state->error);
cosmoV_printBacktrace(state, cosmoV_readError(*cosmoV_pop(state)));
return false;
}
ret = state->panic;
state->panic = false; // so our repl isn't broken
return !ret;
return true;
}
static void repl() {
char line[1024];
static void repl(CState *state)
{
char *line;
_ACTIVE = true;
CState *state = cosmoV_newState();
cosmoB_loadLibrary(state);
cosmoB_loadOSLib(state);
cosmoB_loadVM(state);
// add our custom REPL functions
cosmoV_pushString(state, "quit");
cosmoV_pushCFunction(state, cosmoB_quitRepl);
@@ -66,24 +86,22 @@ static void repl() {
cosmoV_pushString(state, "input");
cosmoV_pushCFunction(state, cosmoB_input);
cosmoV_register(state, 2);
cosmoV_addGlobals(state, 2);
while (_ACTIVE) {
printf("> ");
if (!fgets(line, sizeof(line), stdin)) { // better than gets()
printf("\n> ");
if (!(line = linenoise("> "))) { // better than gets()
break;
}
linenoiseHistoryAdd(line);
interpret(state, line, "REPL");
free(line);
}
cosmoV_freeState(state);
}
static char *readFile(const char* path) {
FILE* file = fopen(path, "rb");
static char *readFile(const char *path)
{
FILE *file = fopen(path, "rb");
if (file == NULL) {
fprintf(stderr, "Could not open file \"%s\".\n", path);
exit(74);
@@ -94,7 +112,7 @@ static char *readFile(const char* path) {
size_t fileSize = ftell(file);
rewind(file);
char *buffer = (char*)malloc(fileSize + 1); // make room for the null byte
char *buffer = (char *)malloc(fileSize + 1); // make room for the null byte
if (buffer == NULL) {
fprintf(stderr, "failed to allocate!");
exit(1);
@@ -106,7 +124,7 @@ static char *readFile(const char* path) {
printf("failed to read file \"%s\"!\n", path);
exit(74);
}
buffer[bytesRead] = '\0'; // place our null terminator
// close the file handler and return the script buffer
@@ -114,36 +132,126 @@ static char *readFile(const char* path) {
return buffer;
}
static bool runFile(const char* fileName) {
static bool runFile(CState *state, const char *fileName)
{
bool ret;
char* script = readFile(fileName);
CState *state = cosmoV_newState();
cosmoB_loadLibrary(state);
cosmoB_loadOSLib(state);
char *script = readFile(fileName);
// add our input() function to the global table
cosmoV_pushString(state, "input");
cosmoV_pushCFunction(state, cosmoB_input);
cosmoV_register(state, 1);
cosmoV_addGlobals(state, 1);
ret = interpret(state, script, fileName);
cosmoV_freeState(state);
free(script);
return ret; // let the caller know if the script failed
}
int main(int argc, const char *argv[]) {
if (argc == 1) {
repl();
} else if (argc >= 2) { // they passed a file (or more lol)
for (int i = 1; i < argc; i++) {
if (!runFile(argv[i])) {
int fileWriter(CState *state, const void *data, size_t size, const void *ud)
{
return !fwrite(data, size, 1, (FILE *)ud);
}
int fileReader(CState *state, void *data, size_t size, const void *ud)
{
return fread(data, size, 1, (FILE *)ud) != 1;
}
void compileScript(CState *state, const char *in, const char *out)
{
char *script = readFile(in);
FILE *fout = fopen(out, "wb");
if (cosmoV_compileString(state, script, in)) {
CObjFunction *func = cosmoV_readClosure(*cosmoV_getTop(state, 0))->function;
cosmoD_dump(state, func, fileWriter, (void *)fout);
} else {
cosmoV_printBacktrace(state, cosmoV_readError(*cosmoV_pop(state)));
}
free(script);
fclose(fout);
printf("[!] compiled %s to %s successfully!\n", in, out);
}
void loadScript(CState *state, const char *in)
{
FILE *file = fopen(in, "rb");
if (!cosmoV_undump(state, fileReader, file)) {
cosmoV_printBacktrace(state, cosmoV_readError(*cosmoV_pop(state)));
return;
};
printf("[!] loaded %s!\n", in);
if (!cosmoV_pcall(state, 0, 0))
cosmoV_printBacktrace(state, cosmoV_readError(*cosmoV_pop(state)));
fclose(file);
}
void printUsage(const char *name)
{
printf("Usage: %s [-clsr] [args]\n\n", name);
printf("available options are:\n"
"-c <in> <out>\tcompile <in> and dump to <out>\n"
"-l <in>\t\tload dump from <in>\n"
"-s <in...>\tcompile and run <in...> script(s)\n"
"-r\t\tstart the repl\n\n");
}
int main(int argc, char *const argv[])
{
CState *state = cosmoV_newState();
cosmoB_loadLibrary(state);
cosmoB_loadOS(state);
cosmoB_loadVM(state);
int opt;
bool isValid = false;
while ((opt = getopt(argc, argv, "clsr")) != -1) {
switch (opt) {
case 'c':
if (optind >= argc - 1) {
printf("Usage: %s -c <in> <out>\n", argv[0]);
exit(EXIT_FAILURE);
} else {
compileScript(state, argv[optind], argv[optind + 1]);
}
isValid = true;
break;
case 'l':
if (optind >= argc) {
printf("Usage: %s -l <in>\n", argv[0]);
exit(EXIT_FAILURE);
} else {
loadScript(state, argv[optind]);
}
isValid = true;
break;
case 's':
for (int i = optind; i < argc; i++) {
if (!runFile(state, argv[i])) {
printf("failed to run %s!\n", argv[i]);
exit(EXIT_FAILURE);
}
}
isValid = true;
break;
case 'r':
repl(state);
isValid = true;
break;
}
}
if (!isValid) {
printUsage(argv[0]);
}
cosmoV_freeState(state);
return 0;
}

56
src/_time.h Normal file
View File

@@ -0,0 +1,56 @@
#ifndef COSMO_TIME_H
#define COSMO_TIME_H
#ifdef _WIN32
# include <time.h>
# include <windows.h>
# if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
# define DELTA_EPOCH_IN_MICROSECS 116444736000000000Ui64
# else
# define DELTA_EPOCH_IN_MICROSECS 116444736000000000ULL
# endif
struct timezone
{
int tz_minuteswest; /* minutes W of Greenwich */
int tz_dsttime; /* type of dst correction */
};
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
FILETIME ft;
unsigned __int64 tmpres = 0;
static int tzflag;
if (NULL != tv) {
GetSystemTimeAsFileTime(&ft);
tmpres |= ft.dwHighDateTime;
tmpres <<= 32;
tmpres |= ft.dwLowDateTime;
/*converting file time to unix epoch*/
tmpres /= 10; /*convert into microseconds*/
tmpres -= DELTA_EPOCH_IN_MICROSECS;
tv->tv_sec = (long)(tmpres / 1000000UL);
tv->tv_usec = (long)(tmpres % 1000000UL);
}
if (NULL != tz) {
if (!tzflag) {
_tzset();
tzflag++;
}
tz->tz_minuteswest = _timezone / 60;
tz->tz_dsttime = _daylight;
}
return 0;
}
#else
# include <sys/time.h>
#endif
#endif

View File

@@ -1,12 +1,13 @@
#include "cbaselib.h"
#include "_time.h"
#include "cdebug.h"
#include "cmem.h"
#include "cobj.h"
#include "cvalue.h"
#include "cvm.h"
#include <math.h>
#include <sys/time.h>
// ================================================================ [BASELIB]
@@ -17,7 +18,7 @@ int cosmoB_print(CState *state, int nargs, CValue *args)
CObjString *str = cosmoV_toString(state, args[i]);
printf("%s", cosmoO_readCString(str));
} else { // else, thats pretty expensive for primitives, just print the raw value
printValue(args[i]);
cosmoV_printValue(args[i]);
}
}
printf("\n");
@@ -29,7 +30,6 @@ int cosmoB_assert(CState *state, int nargs, CValue *args)
{
if (nargs < 1 || nargs > 2) {
cosmoV_error(state, "assert() expected 1 or 2 arguments, got %d!", nargs);
return 0; // nothing pushed onto the stack to return
}
if (!IS_BOOLEAN(args[0]) || (nargs == 2 && !IS_STRING(args[1]))) {
@@ -39,7 +39,6 @@ int cosmoB_assert(CState *state, int nargs, CValue *args)
} else {
cosmoV_typeError(state, "assert()", "<boolean>", "%s", cosmoV_typeStr(args[0]));
}
return 0;
}
if (!cosmoV_readBoolean(args[0])) // expression passed was false, error!
@@ -52,7 +51,6 @@ int cosmoB_type(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "type() expected 1 argument, got %d!", nargs);
return 0;
}
// push the type string to the stack
@@ -64,20 +62,14 @@ int cosmoB_pcall(CState *state, int nargs, CValue *args)
{
if (nargs < 1) {
cosmoV_error(state, "pcall() expected at least 1 argument!");
return 0;
}
// unfreeze the state GC before calling the function
cosmoM_unfreezeGC(state);
// call the passed callable
COSMOVMRESULT res = cosmoV_pcall(state, nargs - 1, 1);
// call the passed callable, the passed arguments are already in the
// proper order lol, so we can just call it
bool res = cosmoV_pcall(state, nargs - 1, 1);
// insert false before the result
cosmo_insert(state, 0, cosmoV_newBoolean(res == COSMOVM_OK));
// refreeze the state GC so it can be properly unfrozen
cosmoM_freezeGC(state);
cosmoV_insert(state, 0, cosmoV_newBoolean(res));
return 2;
}
@@ -85,7 +77,6 @@ int cosmoB_tonumber(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "tonumber() expected 1 argument, got %d!", nargs);
return 0;
}
cosmoV_pushNumber(state, cosmoV_toNumber(state, args[0]));
@@ -94,10 +85,8 @@ int cosmoB_tonumber(CState *state, int nargs, CValue *args)
int cosmoB_tostring(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
if (nargs != 1)
cosmoV_error(state, "tostring() expected 1 argument, got %d!", nargs);
return 0;
}
cosmoV_pushRef(state, (CObj *)cosmoV_toString(state, args[0]));
return 1;
@@ -107,18 +96,16 @@ int cosmoB_loadstring(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "loadstring() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_STRING(args[0])) {
cosmoV_typeError(state, "loadstring()", "<string>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
CObjString *str = cosmoV_readString(args[0]);
bool res = cosmoV_compileString(state, str->str, "");
cosmo_insert(state, 0, cosmoV_newBoolean(res));
cosmoV_insert(state, 0, cosmoV_newBoolean(res));
return 2; // <boolean>, <closure> or <error>
}
@@ -126,12 +113,10 @@ int cosmoB_error(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "error() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_STRING(args[0])) {
cosmoV_typeError(state, "error()", "<string>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
cosmoV_error(state, "%s", cosmoV_readCString(args[0]));
@@ -154,7 +139,7 @@ void cosmoB_loadLibrary(CState *state)
}
// register all the pushed c functions and the strings as globals
cosmoV_register(state, i);
cosmoV_addGlobals(state, i);
// load other libraries
cosmoB_loadObjLib(state);
@@ -180,13 +165,46 @@ int cosmoB_osetProto(CState *state, int nargs, CValue *args)
int cosmoB_ogetProto(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
if (nargs != 1)
cosmoV_error(state, "Expected 1 argument, got %d!", nargs);
return 0;
if (!IS_REF(args[0])) {
cosmoV_typeError(state, "__getter.__proto", "<object>", "%s", cosmoV_typeStr(args[0]));
}
cosmoV_pushRef(state, (CObj *)cosmoV_readObject(args[0])->_obj.proto); // just return the proto
CObj *proto = (CObj *)cosmoV_readRef(args[0])->proto;
if (proto == NULL) {
cosmoV_pushNil(state);
return 1;
}
cosmoV_pushRef(state, proto); // just return the proto
return 1; // 1 result
}
int cosmoB_ogetKeys(CState *state, int nargs, CValue *args)
{
if (nargs != 1)
cosmoV_error(state, "Expected 1 argument, got %d!", nargs);
if (!IS_OBJECT(args[0])) {
cosmoV_typeError(state, "object.__keys", "<object>", "%s", cosmoV_typeStr(args[0]));
}
// push keys
CObjObject *obj = cosmoV_readObject(args[0]);
int cap = cosmoT_getCapacity(&obj->tbl);
int indx = 0;
for (int i = 0; i < cap; i++) {
CTableEntry *entry = &obj->tbl.table[i];
if (IS_NIL(entry->key))
continue;
cosmoV_pushNumber(state, indx++);
cosmoV_pushValue(state, entry->key);
}
cosmoV_makeTable(state, indx);
return 1; // 1 result
}
@@ -194,13 +212,11 @@ int cosmoB_oisChild(CState *state, int nargs, CValue *args)
{
if (nargs != 2) {
cosmoV_error(state, "object.ischild() expected 2 arguments, got %d!", nargs);
return 0;
}
if (!IS_REF(args[0]) || !IS_OBJECT(args[1])) {
cosmoV_typeError(state, "object.ischild()", "<reference obj>, <object>", "%s, %s",
cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
return 0;
}
CObj *obj = cosmoV_readRef(args[0]);
@@ -213,9 +229,9 @@ int cosmoB_oisChild(CState *state, int nargs, CValue *args)
COSMO_API void cosmoB_loadObjLib(CState *state)
{
const char *identifiers[] = {"ischild"};
const char *identifiers[] = {"ischild", "keys"};
CosmoCFunction objLib[] = {cosmoB_oisChild};
CosmoCFunction objLib[] = {cosmoB_oisChild, cosmoB_ogetKeys};
// make object library object
cosmoV_pushString(state, "object");
@@ -223,7 +239,7 @@ COSMO_API void cosmoB_loadObjLib(CState *state)
// make __getter object for debug proto
cosmoV_pushString(state, "__getter");
// key & value pair
// key & value pairs
cosmoV_pushString(state, "__proto"); // key
cosmoV_pushCFunction(state, cosmoB_ogetProto); // value
@@ -249,54 +265,176 @@ COSMO_API void cosmoB_loadObjLib(CState *state)
cosmoV_registerProtoObject(state, COBJ_OBJECT, obj);
// register "object" to the global table
cosmoV_register(state, 1);
cosmoV_addGlobals(state, 1);
}
// ================================================================ [OS.*]
// os.read()
int cosmoB_osRead(CState *state, int nargs, CValue *args)
int fileB_read(CState *state, int nargs, CValue *args)
{
if (nargs != 2) {
cosmoV_error(state, "file:read() expected 2 arguments, got %d!", nargs);
}
if (!cosmoV_isValueUserType(state, args[0], COSMO_USER_FILE)) {
cosmoV_typeError(state, "file:read()", "<file>, <number> or \"a\"", "%s, %s",
cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
}
CObjObject *fileObj = cosmoV_readObject(args[0]);
FILE *file = cosmoO_getUserP(fileObj);
if (IS_NUMBER(args[1])) {
CValue temp;
char *buffer;
cosmo_Number length = cosmoV_readNumber(args[1]);
// make sure the length is within the bounds of the file
if (length < 0) {
cosmoV_error(state, "file:read() expected length to be >= 0, got %d!", length);
}
// allocate a buffer for the read data
buffer = cosmoM_xmalloc(state, (size_t)length + 1);
// read the data
fread(buffer, sizeof(char), (size_t)length, file);
buffer[(int)length] = '\0'; // write the NULL terminator
// push the read data
temp = cosmoV_newRef(cosmoO_takeString(state, buffer, (size_t)length));
cosmoV_pushValue(state, temp);
} else if (IS_STRING(args[1])) {
if (strcmp(cosmoV_readCString(args[1]), "a") == 0) {
CValue temp;
char *buffer;
long length;
// get the length of the file
fseek(file, 0, SEEK_END);
length = ftell(file);
fseek(file, 0, SEEK_SET);
// allocate a buffer for the read data
buffer = cosmoM_xmalloc(state, (size_t)length + 1);
// read the data
fread(buffer, sizeof(char), (size_t)length, file);
buffer[length] = '\0'; // write the NULL terminator
// push the read data
temp = cosmoV_newRef(cosmoO_takeString(state, buffer, (size_t)length));
cosmoV_pushValue(state, temp);
} else {
cosmoV_error(state, "file:read() expected \"a\" or <number>, got \"%s\"!",
cosmoV_readCString(args[1]));
}
} else {
cosmoV_typeError(state, "file:read()", "<file>, <number> or \"a\"", "%s, %s",
cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
}
return 1;
}
int fileB_write(CState *state, int nargs, CValue *args)
{
CObjObject *fileObj;
CObjString *str;
FILE *file;
if (nargs != 2) {
cosmoV_error(state, "file:write() expected 2 arguments, got %d!", nargs);
}
if (!cosmoV_isValueUserType(state, args[0], COSMO_USER_FILE)) {
cosmoV_typeError(state, "file:write()", "<file>, <string>", "%s, %s",
cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
}
if (!IS_STRING(args[1])) {
cosmoV_typeError(state, "file:write()", "<file>, <string>", "%s, %s",
cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
}
fileObj = cosmoV_readObject(args[0]);
str = cosmoV_readString(args[1]);
file = (FILE *)cosmoO_getUserP(fileObj);
fwrite(str->str, sizeof(char), str->length, file);
return 0;
}
int fileB_gc(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "os.read() expected 1 argument, got %d!", nargs);
return 0;
cosmoV_error(state, "file:read() expected 1 argument, got %d!", nargs);
}
if (!IS_STRING(args[0])) {
cosmoV_typeError(state, "os.read()", "<string>", "%s", cosmoV_typeStr(args[0]));
return 0;
if (!cosmoV_isValueUserType(state, args[0], COSMO_USER_FILE)) {
cosmoV_typeError(state, "file:__gc()", "<file>", "%s", cosmoV_typeStr(args[0]));
}
CObjString *str = cosmoV_readString(args[0]);
CObjObject *fileObj = cosmoV_readObject(args[0]);
FILE *file = cosmoO_getUserP(fileObj);
// open file
FILE *file = fopen(str->str, "rb");
char *buf;
size_t size, bRead;
fclose(file);
return 0;
}
CObjObject *pushFileObj(CState *state, FILE *file)
{
CObjObject *fileObj = cosmoO_newObject(state);
cosmoV_pushRef(state, (CObj *)fileObj);
cosmoO_setUserP(fileObj, file);
cosmoO_setUserT(fileObj, COSMO_USER_FILE);
// grab and set proto from the registry
cosmoV_pushRef(state, (CObj *)fileObj);
cosmoV_pushString(state, "file");
cosmoV_getRegistry(state);
cosmoV_setProto(state);
cosmoO_lock(fileObj);
return fileObj;
}
int cosmoB_osOpen(CState *state, int nargs, CValue *args)
{
const char *filePath, *mode = "rb";
FILE *file;
if (nargs >= 1) {
if (!IS_STRING(args[0])) {
cosmoV_typeError(state, "os.open()", "<string>", "%s", cosmoV_typeStr(args[0]));
}
if (nargs == 2) {
if (!IS_STRING(args[1])) {
cosmoV_typeError(state, "os.open()", "<string>, <string>", "%s, %s",
cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
}
mode = cosmoV_readCString(args[1]);
} else if (nargs != 1) {
cosmoV_error(state, "os.open() expected 1 or 2 arguments, got %d!", nargs);
}
} else {
cosmoV_error(state, "os.open() expected 1 or 2 arguments, got %d!", nargs);
}
filePath = cosmoV_readCString(args[0]);
file = fopen(filePath, mode);
if (file == NULL) {
// return nil, file doesn't exist
return 0;
cosmoV_pushBoolean(state, true);
cosmoV_pushFString(state, "Failed to open %s!", filePath);
return 2;
}
// grab the size of the file
fseek(file, 0L, SEEK_END);
size = ftell(file);
rewind(file);
buf = cosmoM_xmalloc(state, size + 1); // +1 for the NULL terminator
bRead = fread(buf, sizeof(char), size, file); // read the file into the buffer
if (bRead < size) {
// an error occured! we don't need to really throw an error, returning a nil is good enough
return 0;
}
buf[bRead] = '\0'; // place the NULL terminator at the end of the buffer
// push the string to the stack to return
cosmoV_pushValue(state, cosmoV_newRef(cosmoO_takeString(state, buf, bRead)));
return 1;
cosmoV_pushBoolean(state, false);
pushFileObj(state, file);
return 2;
}
// os.time()
@@ -305,7 +443,6 @@ int cosmoB_osTime(CState *state, int nargs, CValue *args)
struct timeval time;
if (nargs > 0) {
cosmoV_error(state, "os.time() expected no arguments, got %d!", nargs);
return 0;
}
gettimeofday(&time, NULL);
@@ -318,12 +455,10 @@ int cosmoB_osSystem(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "os.system() expects 1 argument, got %d!", nargs);
return 0;
}
if (!IS_STRING(args[0])) {
cosmoV_typeError(state, "os.system()", "<string>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
// run the command and return the exit code
@@ -331,11 +466,10 @@ int cosmoB_osSystem(CState *state, int nargs, CValue *args)
return 1;
}
COSMO_API void cosmoB_loadOSLib(CState *state)
COSMO_API void cosmoB_loadOS(CState *state)
{
const char *identifiers[] = {"read", "time", "system"};
CosmoCFunction osLib[] = {cosmoB_osRead, cosmoB_osTime, cosmoB_osSystem};
const char *identifiers[] = {"open", "time", "system"};
CosmoCFunction osLib[] = {cosmoB_osOpen, cosmoB_osTime, cosmoB_osSystem};
cosmoV_pushString(state, "os");
@@ -346,7 +480,30 @@ COSMO_API void cosmoB_loadOSLib(CState *state)
}
cosmoV_makeObject(state, i);
cosmoV_register(state, 1); // register the os.* object to the global table
cosmoV_addGlobals(state, 1); // register the os.* object to the global table
// make file proto
cosmoV_pushString(state, "file");
CObjObject *fileProto = cosmoO_newObject(state);
cosmoV_pushRef(state, (CObj *)fileProto);
cosmoV_pushRef(state, (CObj *)fileProto);
cosmoV_pushString(state, "read");
cosmoV_pushCFunction(state, fileB_read);
cosmoV_set(state);
cosmoV_pushRef(state, (CObj *)fileProto);
cosmoV_pushString(state, "write");
cosmoV_pushCFunction(state, fileB_write);
cosmoV_set(state);
cosmoV_pushRef(state, (CObj *)fileProto);
cosmoV_pushString(state, "__gc");
cosmoV_pushCFunction(state, fileB_gc);
cosmoV_set(state);
cosmoV_addRegistry(state, 1);
}
// ================================================================ [STRING.*]
@@ -358,7 +515,6 @@ int cosmoB_sSub(CState *state, int nargs, CValue *args)
if (!IS_STRING(args[0]) || !IS_NUMBER(args[1])) {
cosmoV_typeError(state, "string.sub()", "<string>, <number>", "%s, %s",
cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
return 0;
}
CObjString *str = cosmoV_readString(args[0]);
@@ -368,7 +524,6 @@ int cosmoB_sSub(CState *state, int nargs, CValue *args)
if (indx < 0 || indx >= str->length) {
cosmoV_error(state, "string.sub() expected index to be 0-%d, got %d!", str->length - 1,
indx);
return 0;
}
cosmoV_pushLString(state, str->str + ((int)indx), str->length - ((int)indx));
@@ -377,7 +532,6 @@ int cosmoB_sSub(CState *state, int nargs, CValue *args)
cosmoV_typeError(state, "string.sub()", "<string>, <number>, <number>", "%s, %s, %s",
cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]),
cosmoV_typeStr(args[2]));
return 0;
}
CObjString *str = cosmoV_readString(args[0]);
@@ -389,13 +543,11 @@ int cosmoB_sSub(CState *state, int nargs, CValue *args)
cosmoV_error(
state, "string.sub() expected subbed string goes out of bounds, max length is %d!",
str->length);
return 0;
}
cosmoV_pushLString(state, str->str + ((int)indx), ((int)length));
} else {
cosmoV_error(state, "string.sub() expected 2 or 3 arguments, got %d!", nargs);
return 0;
}
return 1;
@@ -408,7 +560,6 @@ int cosmoB_sFind(CState *state, int nargs, CValue *args)
if (!IS_STRING(args[0]) || !IS_STRING(args[1])) {
cosmoV_typeError(state, "string.find()", "<string>, <string>", "%s, %s",
cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
return 0;
}
CObjString *str = cosmoV_readString(args[0]);
@@ -423,13 +574,12 @@ int cosmoB_sFind(CState *state, int nargs, CValue *args)
}
// success! push the index
cosmoV_pushNumber(state, indx - str->str);
cosmoV_pushNumber(state, (cosmo_Number)(indx - str->str));
} else if (nargs == 3) {
if (!IS_STRING(args[0]) || !IS_STRING(args[1]) || !IS_NUMBER(args[2])) {
cosmoV_typeError(state, "string.find()", "<string>, <string>, <number>", "%s, %s, %s",
cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]),
cosmoV_typeStr(args[2]));
return 0;
}
CObjString *str = cosmoV_readString(args[0]);
@@ -445,10 +595,9 @@ int cosmoB_sFind(CState *state, int nargs, CValue *args)
}
// success! push the index
cosmoV_pushNumber(state, indx - str->str);
cosmoV_pushNumber(state, (cosmo_Number)(indx - str->str));
} else {
cosmoV_error(state, "string.find() expected 2 or 3 arguments, got %d!", nargs);
return 0;
}
return 1;
@@ -459,13 +608,11 @@ int cosmoB_sSplit(CState *state, int nargs, CValue *args)
{
if (nargs != 2) {
cosmoV_error(state, "string.split() expected 2 arguments, got %d!", nargs);
return 0;
}
if (!IS_STRING(args[0]) || !IS_STRING(args[1])) {
cosmoV_typeError(state, "string.split()", "<string>, <string>", "%s, %s",
cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
return 0;
}
CObjString *str = cosmoV_readString(args[0]);
@@ -496,12 +643,10 @@ int cosmoB_sByte(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "string.byte() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_STRING(args[0])) {
cosmoV_typeError(state, "string.byte", "<string>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
CObjString *str = cosmoV_readString(args[0]);
@@ -522,12 +667,10 @@ int cosmoB_sChar(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "string.char() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_NUMBER(args[0])) {
cosmoV_typeError(state, "string.char", "<number>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
// small side effect of truncating the number, but ignoring the decimal instead of throwing an
@@ -537,7 +680,6 @@ int cosmoB_sChar(CState *state, int nargs, CValue *args)
if (num > 255 || num < 0) {
cosmoV_error(state, "Character expected to be in range 0-255, got %d!", num);
return 0;
}
// basically, treat the character value on the C stack as an """"array"""" with a length of 1
@@ -549,15 +691,13 @@ int cosmoB_sLen(CState *state, int nargs, CValue *args)
{
if (nargs < 1) {
cosmoV_error(state, "string.len() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_STRING(args[0])) {
cosmoV_typeError(state, "string.len", "<string>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
cosmoV_pushNumber(state, strlen(cosmoV_readCString(args[0])));
cosmoV_pushNumber(state, (cosmo_Number)strlen(cosmoV_readCString(args[0])));
return 1;
}
@@ -566,14 +706,12 @@ int cosmoB_sRep(CState *state, int nargs, CValue *args)
{
if (nargs != 2) {
cosmoV_error(state, "string.rep() expected 2 arguments, got %d!", nargs);
return 0;
}
// expects <string>, <number>
if (!IS_STRING(args[0]) || !IS_NUMBER(args[1])) {
cosmoV_typeError(state, "string.rep", "<string>, <number>", "%s, %s",
cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
return 0;
}
CObjString *str = cosmoV_readString(args[0]);
@@ -589,8 +727,9 @@ int cosmoB_sRep(CState *state, int nargs, CValue *args)
char *newStr = cosmoM_xmalloc(state, length + 1); // + 1 for the NULL terminator
// copy the string over the new buffer
for (int i = 0; i < times; i++)
for (int i = 0; i < times; i++) {
memcpy(&newStr[i * str->length], str->str, str->length);
}
// write the NULL terminator
newStr[length] = '\0';
@@ -621,7 +760,7 @@ void cosmoB_loadStrLib(CState *state)
cosmoV_registerProtoObject(state, COBJ_STRING, obj);
// register "string" to the global table
cosmoV_register(state, 1);
cosmoV_addGlobals(state, 1);
}
// ================================================================ [MATH]
@@ -631,12 +770,10 @@ int cosmoB_mAbs(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "math.abs() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_NUMBER(args[0])) {
cosmoV_typeError(state, "math.abs", "<number>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
cosmoV_pushNumber(state, fabs(cosmoV_readNumber(args[0])));
@@ -648,12 +785,10 @@ int cosmoB_mFloor(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "math.floor() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_NUMBER(args[0])) {
cosmoV_typeError(state, "math.floor", "<number>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
cosmoV_pushNumber(state, (int)cosmoV_readNumber(args[0]));
@@ -665,12 +800,10 @@ int cosmoB_mCeil(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "math.ceil() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_NUMBER(args[0])) {
cosmoV_typeError(state, "math.ceil", "<number>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
int roundedDown = (int)cosmoV_readNumber(args[0]);
@@ -689,12 +822,10 @@ int cosmoB_mSin(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "math.sin() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_NUMBER(args[0])) {
cosmoV_typeError(state, "math.sin", "<number>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
cosmoV_pushNumber(state, sin(cosmoV_readNumber(args[0])));
@@ -705,12 +836,10 @@ int cosmoB_mCos(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "math.cos() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_NUMBER(args[0])) {
cosmoV_typeError(state, "math.cos", "<number>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
cosmoV_pushNumber(state, cos(cosmoV_readNumber(args[0])));
@@ -721,12 +850,10 @@ int cosmoB_mTan(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "math.tan() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_NUMBER(args[0])) {
cosmoV_typeError(state, "math.tan", "<number>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
cosmoV_pushNumber(state, tan(cosmoV_readNumber(args[0])));
@@ -737,12 +864,10 @@ int cosmoB_mASin(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "math.asin() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_NUMBER(args[0])) {
cosmoV_typeError(state, "math.asin", "<number>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
cosmoV_pushNumber(state, asin(cosmoV_readNumber(args[0])));
@@ -753,12 +878,10 @@ int cosmoB_mACos(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "math.acos() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_NUMBER(args[0])) {
cosmoV_typeError(state, "math.acos", "<number>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
cosmoV_pushNumber(state, acos(cosmoV_readNumber(args[0])));
@@ -769,12 +892,10 @@ int cosmoB_mATan(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "math.atan() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_NUMBER(args[0])) {
cosmoV_typeError(state, "math.atan", "<number>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
cosmoV_pushNumber(state, atan(cosmoV_readNumber(args[0])));
@@ -785,12 +906,10 @@ int cosmoB_mRad(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "math.rad() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_NUMBER(args[0])) {
cosmoV_typeError(state, "math.rad", "<number>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
// convert the degree to radians
@@ -802,12 +921,10 @@ int cosmoB_mDeg(CState *state, int nargs, CValue *args)
{
if (nargs != 1) {
cosmoV_error(state, "math.deg() expected 1 argument, got %d!", nargs);
return 0;
}
if (!IS_NUMBER(args[0])) {
cosmoV_typeError(state, "math.deg", "<number>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
// convert the degree to radians
@@ -823,10 +940,10 @@ void cosmoB_loadMathLib(CState *state)
CosmoCFunction mathLib[] = {cosmoB_mAbs, cosmoB_mFloor, cosmoB_mCeil, cosmoB_mSin,
cosmoB_mCos, cosmoB_mTan, cosmoB_mASin, cosmoB_mACos,
cosmoB_mATan, cosmoB_mRad, cosmoB_mDeg};
int i;
// make math library object
cosmoV_pushString(state, "math");
int i;
for (i = 0; i < sizeof(identifiers) / sizeof(identifiers[0]); i++) {
cosmoV_pushString(state, identifiers[i]);
cosmoV_pushCFunction(state, mathLib[i]);
@@ -838,7 +955,7 @@ void cosmoB_loadMathLib(CState *state)
// make the object and register it as a global to the state
cosmoV_makeObject(state, i);
cosmoV_register(state, 1);
cosmoV_addGlobals(state, 1);
}
// ================================================================ [VM.*]
@@ -856,13 +973,11 @@ int cosmoB_vsetGlobal(CState *state, int nargs, CValue *args)
{
if (nargs != 2) {
cosmoV_error(state, "Expected 2 argumenst, got %d!", nargs);
return 0;
}
if (!IS_TABLE(args[1])) {
cosmoV_typeError(state, "vm.__setter[\"globals\"]", "<object>, <table>", "%s, %s",
cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
return 0;
}
// this makes me very nervous ngl
@@ -871,17 +986,37 @@ int cosmoB_vsetGlobal(CState *state, int nargs, CValue *args)
return 0;
}
// vm.disassemble()
int cosmoB_vdisassemble(CState *state, int nargs, CValue *args)
{
CObjClosure *closure;
if (nargs != 1) {
cosmoV_error(state, "Expected 1 argument, got %d!", nargs);
}
// get the closure
if (!IS_CLOSURE(args[0])) {
cosmoV_typeError(state, "vm.disassemble", "<closure>", "%s", cosmoV_typeStr(args[0]));
}
closure = cosmoV_readClosure(args[0]);
// print the disasembly
disasmChunk(&closure->function->chunk,
closure->function->name ? closure->function->name->str : UNNAMEDCHUNK, 0);
return 0;
}
int cosmoB_vindexBProto(CState *state, int nargs, CValue *args)
{
if (nargs != 2) {
cosmoV_error(state, "Expected 2 arguments, got %d!", nargs);
return 0;
}
if (!IS_NUMBER(args[1])) {
cosmoV_typeError(state, "baseProtos.__index", "<object>, <number>", "%s, %s",
cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
return 0;
}
int indx = (int)cosmoV_readNumber(args[1]);
@@ -903,14 +1038,12 @@ int cosmoB_vnewindexBProto(CState *state, int nargs, CValue *args)
{
if (nargs != 3) {
cosmoV_error(state, "Expected 3 arguments, got %d!", nargs);
return 0;
}
if (!IS_NUMBER(args[1]) || !IS_OBJECT(args[2])) {
cosmoV_typeError(state, "baseProtos.__newindex", "<object>, <number>, <object>",
"%s, %s, %s", cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]),
cosmoV_typeStr(args[2]));
return 0;
}
int indx = (int)cosmoV_readNumber(args[1]);
@@ -918,7 +1051,6 @@ int cosmoB_vnewindexBProto(CState *state, int nargs, CValue *args)
if (indx >= COBJ_MAX || indx < 0) {
cosmoV_error(state, "index out of range! expected 0 - %d, got %d!", COBJ_MAX, indx);
return 0;
}
cosmoV_registerProtoObject(state, indx, proto);
@@ -928,16 +1060,8 @@ int cosmoB_vnewindexBProto(CState *state, int nargs, CValue *args)
// vm.collect()
int cosmoB_vcollect(CState *state, int nargs, CValue *args)
{
// first, unfreeze the state (we start frozen on entry to any C Function)
cosmoM_unfreezeGC(state);
// now force a garbage collection
// force a garbage collection
cosmoM_collectGarbage(state);
// and re-freeze the state
cosmoM_freezeGC(state);
// the end!
return 0;
}
@@ -976,10 +1100,11 @@ void cosmoB_loadVM(CState *state)
cosmoV_pushString(state, "collect");
cosmoV_pushCFunction(state, cosmoB_vcollect);
cosmoV_makeObject(state, 4); // makes the vm object
cosmoV_pushString(state, "disassemble");
cosmoV_pushCFunction(state, cosmoB_vdisassemble);
cosmoV_makeObject(state, 5); // makes the vm object
// register "vm" to the global table
cosmoV_register(state, 1);
printf("[WARNING] the vm.* library has been loaded!\n");
cosmoV_addGlobals(state, 1);
}

View File

@@ -3,6 +3,13 @@
#include "cstate.h"
enum
{
COSMO_USER_NONE, // CObjObject is not a userdata object
COSMO_USER_FILE, // CObjObject is a file object (see cosmoB_osOpen)
COSMO_USER_START // the first user type for user-defined userdata
};
/* loads all of the base library, including:
- base library ("print", "assert", "type", "pcall", "loadstring", etc.)
- object library
@@ -22,7 +29,7 @@ COSMO_API void cosmoB_loadObjLib(CState *state);
- os.system()
- os.time()
*/
COSMO_API void cosmoB_loadOSLib(CState *state);
COSMO_API void cosmoB_loadOS(CState *state);
/* loads the base string library, including:
- string.sub & <string>:sub()
@@ -50,6 +57,7 @@ COSMO_API void cosmoB_loadMathLib(CState *state);
- manually setting/grabbing base protos of any object (vm.baseProtos)
- manually setting/grabbing the global table (vm.globals)
- manually invoking a garbage collection event (vm.collect())
- printing closure disassemblies (vm.disassemble())
for this reason, it is recommended to NOT load this library in production
*/

View File

@@ -27,9 +27,9 @@ void initChunk(CState *state, CChunk *chunk, size_t startCapacity)
void cleanChunk(CState *state, CChunk *chunk)
{
// first, free the chunk buffer
cosmoM_freearray(state, INSTRUCTION, chunk->buf, chunk->capacity);
cosmoM_freeArray(state, INSTRUCTION, chunk->buf, chunk->capacity);
// then the line info
cosmoM_freearray(state, int, chunk->lineInfo, chunk->capacity);
cosmoM_freeArray(state, int, chunk->lineInfo, chunk->capacity);
// free the constants
cleanValArray(state, &chunk->constants);
}
@@ -49,9 +49,10 @@ int addConstant(CState *state, CChunk *chunk, CValue value)
return i; // we already have a matching constant!
}
cosmoM_freezeGC(state); // so our GC doesn't free it
cosmoV_pushValue(state, value); // push the value to the stack so our GC can see it
appendValArray(state, &chunk->constants, value);
cosmoM_unfreezeGC(state);
cosmoV_pop(state);
return chunk->constants.count - 1; // return the index of the new constants
}
@@ -60,8 +61,8 @@ int addConstant(CState *state, CChunk *chunk, CValue value)
void writeu8Chunk(CState *state, CChunk *chunk, INSTRUCTION i, int line)
{
// does the buffer need to be reallocated?
cosmoM_growarray(state, INSTRUCTION, chunk->buf, chunk->count, chunk->capacity);
cosmoM_growarray(state, int, chunk->lineInfo, chunk->count, chunk->lineCapacity);
cosmoM_growArray(state, INSTRUCTION, chunk->buf, chunk->count, chunk->capacity);
cosmoM_growArray(state, int, chunk->lineInfo, chunk->count, chunk->lineCapacity);
// write data to the chunk :)
chunk->lineInfo[chunk->count] = line;

View File

@@ -21,6 +21,8 @@ void cleanChunk(CState *state, CChunk *chunk); // frees everything but the struc
void freeChunk(CState *state, CChunk *chunk); // frees everything including the struct
int addConstant(CState *state, CChunk *chunk, CValue value);
bool validateChunk(CState *state, CChunk *chunk);
// write to chunk
void writeu8Chunk(CState *state, CChunk *chunk, INSTRUCTION i, int line);
void writeu16Chunk(CState *state, CChunk *chunk, uint16_t i, int line);

View File

@@ -5,63 +5,64 @@
void printIndent(int indent)
{
for (int i = 0; i < indent; i++)
for (int i = 0; i < indent; i++) {
printf("\t");
}
}
int simpleInstruction(const char *name, int offset)
static int simpleInstruction(const char *name, int offset)
{
printf("%s", name);
return offset + 1; // consume opcode
}
int u8OperandInstruction(const char *name, CChunk *chunk, int offset)
static int u8OperandInstruction(const char *name, CChunk *chunk, int offset)
{
printf("%-16s [%03d]", name, readu8Chunk(chunk, offset + 1));
return offset + 2;
}
int u16OperandInstruction(const char *name, CChunk *chunk, int offset)
static int u16OperandInstruction(const char *name, CChunk *chunk, int offset)
{
printf("%-16s [%05d]", name, readu16Chunk(chunk, offset + 1));
return offset + 1 + (sizeof(uint16_t) / sizeof(INSTRUCTION));
}
int JumpInstruction(const char *name, CChunk *chunk, int offset, int dir)
static int JumpInstruction(const char *name, CChunk *chunk, int offset, int dir)
{
int jmp = ((int)readu16Chunk(chunk, offset + 1)) * dir;
printf("%-16s [%05d] - jumps to %04d", name, jmp, offset + 3 + jmp);
return offset + 1 + (sizeof(uint16_t) / sizeof(INSTRUCTION));
}
int u8u8OperandInstruction(const char *name, CChunk *chunk, int offset)
static int u8u8OperandInstruction(const char *name, CChunk *chunk, int offset)
{
printf("%-16s [%03d] [%03d]", name, readu8Chunk(chunk, offset + 1),
readu8Chunk(chunk, offset + 2));
return offset + 3; // op + u8 + u8
}
int u8u16OperandInstruction(const char *name, CChunk *chunk, int offset)
static int u8u16OperandInstruction(const char *name, CChunk *chunk, int offset)
{
printf("%-16s [%03d] [%05d]", name, readu8Chunk(chunk, offset + 1),
readu16Chunk(chunk, offset + 2));
return offset + 4; // op + u8 + u16
}
int u8u8u16OperandInstruction(const char *name, CChunk *chunk, int offset)
static int u8u8u16OperandInstruction(const char *name, CChunk *chunk, int offset)
{
printf("%-16s [%03d] [%03d] [%05d]", name, readu8Chunk(chunk, offset + 1),
readu8Chunk(chunk, offset + 2), readu16Chunk(chunk, offset + 3));
return offset + 5; // op + u8 + u8 + u16
}
int constInstruction(const char *name, CChunk *chunk, int offset)
static int constInstruction(const char *name, CChunk *chunk, int offset)
{
int index = readu16Chunk(chunk, offset + 1);
printf("%-16s [%05d] - ", name, index);
CValue val = chunk->constants.values[index];
printValue(val);
cosmoV_printValue(val);
return offset + 1 + (sizeof(uint16_t) / sizeof(INSTRUCTION)); // consume opcode + uint
}
@@ -127,7 +128,7 @@ int disasmInstr(CChunk *chunk, int offset, int indent)
CObjFunction *cobjFunc = (CObjFunction *)cosmoV_readRef(val);
offset += 3; // we consumed the opcode + u16
printValue(val);
cosmoV_printValue(val);
printf("\n");
// list the upvalues/locals that are captured
@@ -222,3 +223,8 @@ int disasmInstr(CChunk *chunk, int offset, int indent)
return 1;
}
void cosmoG_disassemble(CObjClosure *closure)
{
disasmChunk(&closure->function->chunk, closure->function->name == NULL ? UNNAMEDCHUNK : closure->function->name->str, 0);
}

View File

@@ -2,10 +2,13 @@
#define CDEBUG_H
#include "cchunk.h"
#include "cobj.h"
COSMO_API void disasmChunk(CChunk *chunk, const char *name, int indent);
COSMO_API int disasmInstr(CChunk *chunk, int offset, int indent);
void printIndent(int indent);
COSMO_API void cosmoG_disassemble(CObjClosure *closure);
#endif

View File

@@ -4,6 +4,7 @@
#include "cmem.h"
#include "cobj.h"
#include "cvalue.h"
#include "cvm.h"
typedef struct
{
@@ -13,7 +14,12 @@ typedef struct
int writerStatus;
} DumpState;
static void writeCValue(DumpState *dstate, CValue val);
static bool writeCValue(DumpState *dstate, CValue val);
#define check(e) \
if (!e) { \
return false; \
}
static void initDumpState(CState *state, DumpState *dstate, cosmo_Writer writer,
const void *userData)
@@ -24,82 +30,92 @@ static void initDumpState(CState *state, DumpState *dstate, cosmo_Writer writer,
dstate->writerStatus = 0;
}
static void writeBlock(DumpState *dstate, const void *data, size_t size)
static bool writeBlock(DumpState *dstate, const void *data, size_t size)
{
if (dstate->writerStatus == 0) {
dstate->writerStatus = dstate->writer(dstate->state, data, size, dstate->userData);
}
return dstate->writerStatus == 0;
}
static void writeu8(DumpState *dstate, uint8_t d)
static bool writeu8(DumpState *dstate, uint8_t d)
{
writeBlock(dstate, &d, sizeof(uint8_t));
return writeBlock(dstate, &d, sizeof(uint8_t));
}
static void writeu32(DumpState *dstate, uint32_t d)
static bool writeu32(DumpState *dstate, uint32_t d)
{
writeBlock(dstate, &d, sizeof(uint32_t));
return writeBlock(dstate, &d, sizeof(uint32_t));
}
static void writeSize(DumpState *dstate, size_t d)
static bool writeSize(DumpState *dstate, size_t d)
{
writeBlock(dstate, &d, sizeof(size_t));
return writeBlock(dstate, &d, sizeof(size_t));
}
static void writeVector(DumpState *dstate, const void *data, size_t size, size_t count)
static bool writeVector(DumpState *dstate, const void *data, size_t size, size_t count)
{
writeSize(dstate, count);
writeBlock(dstate, data, size * count);
check(writeSize(dstate, count));
check(writeBlock(dstate, data, size * count));
return true;
}
static void writeHeader(DumpState *dstate)
static bool writeHeader(DumpState *dstate)
{
writeBlock(dstate, COSMO_MAGIC, COSMO_MAGIC_LEN);
check(writeBlock(dstate, COSMO_MAGIC, COSMO_MAGIC_LEN));
/* after the magic, we write some platform information */
writeu8(dstate, cosmoD_isBigEndian());
writeu8(dstate, sizeof(cosmo_Number));
writeu8(dstate, sizeof(size_t));
writeu8(dstate, sizeof(int));
check(writeu8(dstate, cosmoD_isBigEndian()));
check(writeu8(dstate, sizeof(cosmo_Number)));
check(writeu8(dstate, sizeof(size_t)));
check(writeu8(dstate, sizeof(int)));
return true;
}
static void writeCObjString(DumpState *dstate, CObjString *obj)
static bool writeCObjString(DumpState *dstate, CObjString *obj)
{
if (obj == NULL) { /* this is in case cobjfunction's name or module strings are null */
writeu32(dstate, 0);
return;
check(writeu32(dstate, 0));
return true;
}
/* write string length */
writeu32(dstate, obj->length);
check(writeu32(dstate, obj->length));
/* write string data */
writeBlock(dstate, obj->str, obj->length);
check(writeBlock(dstate, obj->str, obj->length));
return true;
}
static void writeCObjFunction(DumpState *dstate, CObjFunction *obj)
static bool writeCObjFunction(DumpState *dstate, CObjFunction *obj)
{
writeCObjString(dstate, obj->name);
writeCObjString(dstate, obj->module);
check(writeCObjString(dstate, obj->name));
check(writeCObjString(dstate, obj->module));
writeu32(dstate, obj->args);
writeu32(dstate, obj->upvals);
writeu8(dstate, obj->variadic);
check(writeu32(dstate, obj->args));
check(writeu32(dstate, obj->upvals));
check(writeu8(dstate, obj->variadic));
/* write chunk info */
writeVector(dstate, obj->chunk.buf, sizeof(uint8_t), obj->chunk.count);
check(writeVector(dstate, obj->chunk.buf, sizeof(uint8_t), obj->chunk.count));
/* write line info */
writeVector(dstate, obj->chunk.lineInfo, sizeof(int), obj->chunk.count);
check(writeVector(dstate, obj->chunk.lineInfo, sizeof(int), obj->chunk.count));
/* write constants */
writeSize(dstate, obj->chunk.constants.count);
check(writeSize(dstate, obj->chunk.constants.count));
for (int i = 0; i < obj->chunk.constants.count; i++) {
writeCValue(dstate, obj->chunk.constants.values[i]);
check(writeCValue(dstate, obj->chunk.constants.values[i]));
}
return true;
}
static void writeCObj(DumpState *dstate, CObj *obj)
static bool writeCObj(DumpState *dstate, CObj *obj)
{
/*
we can kind of cheat here since our parser only emits a few very limited CObjs...
@@ -117,24 +133,27 @@ static void writeCObj(DumpState *dstate, CObj *obj)
/* write object payload/body */
switch (t) {
case COBJ_STRING:
writeCObjString(dstate, (CObjString *)obj);
check(writeCObjString(dstate, (CObjString *)obj));
break;
case COBJ_FUNCTION:
writeCObjFunction(dstate, (CObjFunction *)obj);
check(writeCObjFunction(dstate, (CObjFunction *)obj));
break;
default:
break;
cosmoV_error(dstate->state, "invalid cobj type: %d", t);
return false;
}
return true;
}
#define WRITE_VAR(dstate, type, expression) \
{ \
type _tmp = expression; \
writeBlock(dstate, &_tmp, sizeof(_tmp)); \
check(writeBlock(dstate, &_tmp, sizeof(_tmp))); \
break; \
}
static void writeCValue(DumpState *dstate, CValue val)
static bool writeCValue(DumpState *dstate, CValue val)
{
CosmoType t = GET_TYPE(val);
@@ -148,12 +167,16 @@ static void writeCValue(DumpState *dstate, CValue val)
case COSMO_TBOOLEAN:
WRITE_VAR(dstate, bool, cosmoV_readBoolean(val))
case COSMO_TREF:
writeCObj(dstate, cosmoV_readRef(val));
check(writeCObj(dstate, cosmoV_readRef(val)));
break;
case COSMO_TNIL: /* no body */
break;
case COSMO_TNIL: /* fallthrough, no body */
default:
break;
cosmoV_error(dstate->state, "invalid value type: %d", t);
return false;
}
return true;
}
#undef WRITE_VAR
@@ -174,8 +197,8 @@ int cosmoD_dump(CState *state, CObjFunction *func, cosmo_Writer writer, const vo
DumpState dstate;
initDumpState(state, &dstate, writer, userData);
writeHeader(&dstate);
writeCObjFunction(&dstate, func);
check(writeHeader(&dstate));
check(writeCObjFunction(&dstate, func));
return dstate.writerStatus;
}

View File

@@ -54,7 +54,7 @@ static void resetBuffer(CLexState *state)
// cancels the token heap buffer and frees it
static void freeBuffer(CLexState *state)
{
cosmoM_freearray(state->cstate, char, state->buffer, state->bufCap);
cosmoM_freeArray(state->cstate, char, state->buffer, state->bufCap);
resetBuffer(state);
}
@@ -62,7 +62,7 @@ static void freeBuffer(CLexState *state)
// adds character to buffer
static void appendBuffer(CLexState *state, char c)
{
cosmoM_growarray(state->cstate, char, state->buffer, state->bufCount, state->bufCap);
cosmoM_growArray(state->cstate, char, state->buffer, state->bufCount, state->bufCap);
state->buffer[state->bufCount++] = c;
}
@@ -150,7 +150,7 @@ static bool match(CLexState *state, char expected)
return true;
}
char peek(CLexState *state)
static char peek(CLexState *state)
{
return *state->currentChar;
}
@@ -163,7 +163,7 @@ static char peekNext(CLexState *state)
return state->currentChar[1];
}
char next(CLexState *state)
static char next(CLexState *state)
{
if (isEnd(state))
return '\0'; // return a null terminator
@@ -171,12 +171,12 @@ char next(CLexState *state)
return state->currentChar[-1];
}
bool isHex(char c)
static bool isHex(char c)
{
return isNumerical(c) || ('A' <= c && 'F' >= c) || ('a' <= c && 'f' >= c);
}
CTokenType identifierType(CLexState *state)
static CTokenType identifierType(CLexState *state)
{
int length = state->currentChar - state->startChar;
@@ -192,7 +192,7 @@ CTokenType identifierType(CLexState *state)
return TOKEN_IDENTIFIER;
}
void skipWhitespace(CLexState *state)
static void skipWhitespace(CLexState *state)
{
while (true) {
char c = peek(state);
@@ -235,7 +235,7 @@ void skipWhitespace(CLexState *state)
}
}
CToken parseString(CLexState *state)
static CToken parseString(CLexState *state)
{
makeBuffer(state); // buffer mode
while (peek(state) != '"' && !isEnd(state)) {
@@ -341,7 +341,7 @@ CToken parseString(CLexState *state)
return makeToken(state, TOKEN_STRING);
}
CToken parseNumber(CLexState *state)
static CToken parseNumber(CLexState *state)
{
switch (peek(state)) {
case 'x': // hexadecimal number
@@ -380,7 +380,7 @@ CToken parseNumber(CLexState *state)
return makeToken(state, TOKEN_NUMBER);
}
CToken parseIdentifier(CLexState *state)
static CToken parseIdentifier(CLexState *state)
{
// read literal
while ((isAlpha(peek(state)) || isNumerical(peek(state))) && !isEnd(state))
@@ -389,9 +389,8 @@ CToken parseIdentifier(CLexState *state)
return makeToken(state, identifierType(state)); // is it a reserved word?
}
CLexState *cosmoL_newLexState(CState *cstate, const char *source)
void cosmoL_initLexState(CState *cstate, CLexState *state, const char *source)
{
CLexState *state = cosmoM_xmalloc(cstate, sizeof(CLexState));
state->startChar = (char *)source;
state->currentChar = (char *)source;
state->line = 1;
@@ -400,13 +399,11 @@ CLexState *cosmoL_newLexState(CState *cstate, const char *source)
state->cstate = cstate;
resetBuffer(state);
return state;
}
void cosmoL_freeLexState(CState *state, CLexState *lstate)
void cosmoL_cleanupLexState(CState *state, CLexState *lstate)
{
cosmoM_free(state, CLexState, lstate);
// stubbed
}
CToken cosmoL_scanToken(CLexState *state)

View File

@@ -93,7 +93,7 @@ typedef struct
char *currentChar;
char *startChar;
char *buffer; // if non-NULL & bufCount > 0, token->start & token->length will be set to buffer
// & bufCount respectively
// & bufCount respectively. used exclusively for string literals
size_t bufCount;
size_t bufCap;
int line; // current line
@@ -103,8 +103,8 @@ typedef struct
CState *cstate;
} CLexState;
CLexState *cosmoL_newLexState(CState *state, const char *source);
void cosmoL_freeLexState(CState *state, CLexState *lstate);
void cosmoL_initLexState(CState *cstate, CLexState *state, const char *source);
void cosmoL_cleanupLexState(CState *state, CLexState *lstate);
CToken cosmoL_scanToken(CLexState *state);

View File

@@ -6,11 +6,29 @@
#include "cstate.h"
#include "ctable.h"
#include "cvalue.h"
#include "cvm.h"
// realloc wrapper
void *cosmoM_reallocate(CState *state, void *buf, size_t oldSize, size_t newSize)
{
if (buf == NULL)
oldSize = 0;
#ifdef GC_DEBUG
printf("old allocated bytes: %ld\n", state->allocatedBytes);
if (buf) {
if (newSize == 0) {
printf("freeing %p, reclaiming %ld bytes...\n", buf, oldSize);
} else {
printf("realloc %p, byte difference: %ld\n", buf, newSize - oldSize);
}
}
#endif
state->allocatedBytes += newSize - oldSize;
#ifdef GC_DEBUG
printf("new allocated bytes: %ld\n", state->allocatedBytes);
fflush(stdout);
#endif
if (newSize == 0) { // it needs to be freed
free(buf);
@@ -33,8 +51,13 @@ void *cosmoM_reallocate(CState *state, void *buf, size_t oldSize, size_t newSize
// if NULL is passed, realloc() acts like malloc()
void *newBuf = realloc(buf, newSize);
#ifdef GC_DEBUG
printf("allocating new buffer of size %ld at %p\n", newSize - oldSize, newBuf);
fflush(stdout);
#endif
if (newBuf == NULL) {
CERROR("failed to allocate memory!");
printf("[ERROR] failed to allocate memory!");
exit(1);
}
@@ -51,15 +74,15 @@ COSMO_API bool cosmoM_checkGarbage(CState *state, size_t needed)
return false;
}
void markObject(CState *state, CObj *obj);
void markValue(CState *state, CValue val);
static void markObject(CState *state, CObj *obj);
static void markValue(CState *state, CValue val);
void markTable(CState *state, CTable *tbl)
static void markTable(CState *state, CTable *tbl)
{
if (tbl->table == NULL) // table is still being initialized
return;
int cap = tbl->capacityMask + 1;
int cap = cosmoT_getCapacity(tbl);
for (int i = 0; i < cap; i++) {
CTableEntry *entry = &tbl->table[i];
markValue(state, entry->key);
@@ -67,13 +90,18 @@ void markTable(CState *state, CTable *tbl)
}
}
// frees white members from the table
void tableRemoveWhite(CState *state, CTable *tbl)
// removes white members from the table
static void tableRemoveWhite(CState *state, CTable *tbl)
{
if (tbl->table == NULL) // table is still being initialized
return;
int cap = tbl->capacityMask + 1;
int cap = cosmoT_getCapacity(tbl);
#ifdef GC_DEBUG
printf("tableRemoveWhite: %p, cap: %d\n", tbl, cap);
#endif
for (int i = 0; i < cap; i++) {
CTableEntry *entry = &tbl->table[i];
if (IS_REF(entry->key) &&
@@ -86,7 +114,7 @@ void tableRemoveWhite(CState *state, CTable *tbl)
cosmoT_checkShrink(state, tbl); // recovers the memory we're no longer using
}
void markArray(CState *state, CValueArray *array)
static void markArray(CState *state, CValueArray *array)
{
for (size_t i = 0; i < array->count; i++) {
markValue(state, array->values[i]);
@@ -95,7 +123,7 @@ void markArray(CState *state, CValueArray *array)
// mark all references associated with the object
// black = keep, white = discard
void blackenObject(CState *state, CObj *obj)
static void blackenObject(CState *state, CObj *obj)
{
markObject(state, (CObj *)obj->proto);
switch (obj->type) {
@@ -137,8 +165,9 @@ void blackenObject(CState *state, CObj *obj)
markValue(state, err->err);
// mark callframes
for (int i = 0; i < err->frameCount; i++)
for (int i = 0; i < err->frameCount; i++) {
markObject(state, (CObj *)err->frames[i].closure);
}
break;
}
@@ -161,7 +190,7 @@ void blackenObject(CState *state, CObj *obj)
}
}
void markObject(CState *state, CObj *obj)
static void markObject(CState *state, CObj *obj)
{
if (obj == NULL || obj->isMarked) // skip if NULL or already marked
return;
@@ -179,20 +208,20 @@ void markObject(CState *state, CObj *obj)
return;
// we can use cosmoM_growarray because we lock the GC when we entered in cosmoM_collectGarbage
cosmoM_growarray(state, CObj *, state->grayStack.array, state->grayStack.count,
cosmoM_growArray(state, CObj *, state->grayStack.array, state->grayStack.count,
state->grayStack.capacity);
state->grayStack.array[state->grayStack.count++] = obj;
}
void markValue(CState *state, CValue val)
static void markValue(CState *state, CValue val)
{
if (IS_REF(val))
markObject(state, cosmoV_readRef(val));
}
// trace our gray references
void traceGrays(CState *state)
static void traceGrays(CState *state)
{
while (state->grayStack.count > 0) {
CObj *obj = state->grayStack.array[--state->grayStack.count];
@@ -200,13 +229,13 @@ void traceGrays(CState *state)
}
}
void sweep(CState *state)
static void sweep(CState *state)
{
CObj *prev = NULL;
CObj *object = state->objects;
CObj *prev = NULL, *object = state->objects;
while (object != NULL) {
if (object->isMarked) { // skip over it
object->isMarked = false; // rest to white
object->isMarked = false; // reset to white
prev = object;
object = object->next;
} else { // free it!
@@ -219,23 +248,23 @@ void sweep(CState *state)
prev->next = object;
}
// call __gc on the object
CObjObject *protoObject = cosmoO_grabProto(oldObj);
CValue res;
// use user-defined __gc
if (protoObject != NULL && cosmoO_getIString(state, protoObject, ISTRING_GC, &res)) {
cosmoV_pushValue(state, res);
cosmoV_pushRef(state, (CObj *)oldObj);
cosmoV_call(state, 1, 0);
}
cosmoO_free(state, oldObj);
}
}
}
void markUserRoots(CState *state)
{
CObj *root = state->userRoots;
// traverse userRoots and mark all the object
while (root != NULL) {
markObject(state, root);
root = root->nextRoot;
}
}
void markRoots(CState *state)
static void markRoots(CState *state)
{
// mark all values on the stack
for (StkPtr value = state->stack; value < state->top; value++) {
@@ -255,29 +284,26 @@ void markRoots(CState *state)
markObject(state, (CObj *)state->globals);
// mark all internal strings
for (int i = 0; i < ISTRING_MAX; i++)
for (int i = 0; i < ISTRING_MAX; i++) {
markObject(state, (CObj *)state->iStrings[i]);
}
// mark the user defined roots
markUserRoots(state);
markTable(state, &state->registry);
// mark other misc. internally reserved objects
markObject(state, (CObj *)state->error);
for (int i = 0; i < COBJ_MAX; i++)
for (int i = 0; i < COBJ_MAX; i++) {
markObject(state, (CObj *)state->protoObjects[i]);
}
traceGrays(state);
}
COSMO_API void cosmoM_collectGarbage(CState *state)
{
cosmoM_freezeGC(state);
#ifdef GC_DEBUG
printf("-- GC start\n");
size_t start = state->allocatedBytes;
#endif
cosmoM_freezeGC(state); // we don't want a recursive garbage collection event!
markRoots(state);
tableRemoveWhite(
@@ -288,59 +314,15 @@ COSMO_API void cosmoM_collectGarbage(CState *state)
// set our next GC event
cosmoM_updateThreshhold(state);
state->freezeGC--; // we don't want to use cosmoM_unfreezeGC because that might trigger a GC
// event (if GC_STRESS is defined)
#ifdef GC_DEBUG
printf("-- GC end, reclaimed %ld bytes (started at %ld, ended at %ld), next garbage collection "
"scheduled at %ld bytes\n",
start - state->allocatedBytes, start, state->allocatedBytes, state->nextGC);
getchar(); // pauses execution
#endif
cosmoM_unfreezeGC(state);
}
COSMO_API void cosmoM_updateThreshhold(CState *state)
{
state->nextGC = state->allocatedBytes * HEAP_GROW_FACTOR;
}
COSMO_API void cosmoM_addRoot(CState *state, CObj *newRoot)
{
// first, check and make sure this root doesn't already exist in the list
CObj *root = state->userRoots;
while (root != NULL) {
if (root == newRoot) // found in the list, abort
return;
root = root->nextRoot;
}
// adds root to userRoot linked list
newRoot->nextRoot = state->userRoots;
state->userRoots = newRoot;
}
COSMO_API void cosmoM_removeRoot(CState *state, CObj *oldRoot)
{
CObj *prev = NULL;
CObj *root = state->userRoots;
// traverse the userRoot linked list
while (root != NULL) {
if (root == oldRoot) { // found root in list
// remove from the linked list
if (prev == NULL) {
state->userRoots = root->nextRoot;
} else {
prev->nextRoot = root->nextRoot;
}
root->nextRoot = NULL;
break;
}
prev = root;
root = root->nextRoot;
}
}

View File

@@ -12,16 +12,16 @@
#define ARRAY_START 8
#ifdef GC_DEBUG
# define cosmoM_freearray(state, type, buf, capacity) \
# define cosmoM_freeArray(state, type, buf, capacity) \
printf("freeing array %p [size %lu] at %s:%d\n", buf, sizeof(type) * capacity, __FILE__, \
__LINE__); \
cosmoM_reallocate(state, buf, sizeof(type) * capacity, 0)
#else
# define cosmoM_freearray(state, type, buf, capacity) \
# define cosmoM_freeArray(state, type, buf, capacity) \
cosmoM_reallocate(state, buf, sizeof(type) * capacity, 0)
#endif
#define cosmoM_growarray(state, type, buf, count, capacity) \
#define cosmoM_growArray(state, type, buf, count, capacity) \
if (count >= capacity || buf == NULL) { \
int old = capacity; \
capacity = old * GROW_FACTOR; \
@@ -38,6 +38,7 @@
#define cosmoM_isFrozen(state) (state->freezeGC > 0)
// cosmoM_freezeGC should only be used in the garbage collector !
// if debugging, print the locations of when the state is frozen/unfrozen
#ifdef GC_DEBUG
# define cosmoM_freezeGC(state) \
@@ -66,13 +67,7 @@ COSMO_API bool cosmoM_checkGarbage(CState *state,
COSMO_API void cosmoM_collectGarbage(CState *state);
COSMO_API void cosmoM_updateThreshhold(CState *state);
// lets the VM know you are holding a reference to a CObj and to not free it
COSMO_API void cosmoM_addRoot(CState *state, CObj *newRoot);
// lets the VM know this root is no longer held in a reference and is able to be freed
COSMO_API void cosmoM_removeRoot(CState *state, CObj *oldRoot);
// wrapper for cosmoM_reallocate so we can track our memory usage (it's also safer :P)
// wrapper for cosmoM_reallocate so we can track our memory usage
static inline void *cosmoM_xmalloc(CState *state, size_t sz)
{
return cosmoM_reallocate(state, NULL, 0, sz);

View File

@@ -8,7 +8,6 @@
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
// we don't actually hash the whole string :eyes:
uint32_t hashString(const char *str, size_t sz)
@@ -16,8 +15,9 @@ uint32_t hashString(const char *str, size_t sz)
uint32_t hash = sz;
size_t step = (sz >> 5) + 1;
for (size_t i = sz; i >= step; i -= step)
for (size_t i = sz; i >= step; i -= step) {
hash = ((hash << 5) + (hash >> 2)) + str[i - 1];
}
return hash;
}
@@ -32,9 +32,8 @@ CObj *cosmoO_allocateBase(CState *state, size_t sz, CObjType type)
obj->next = state->objects;
state->objects = obj;
obj->nextRoot = NULL;
#ifdef GC_DEBUG
printf("allocated %p with OBJ_TYPE %d\n", obj, type);
printf("allocated %s %p\n", cosmoO_typeStr(obj), obj);
#endif
return obj;
}
@@ -42,21 +41,19 @@ CObj *cosmoO_allocateBase(CState *state, size_t sz, CObjType type)
void cosmoO_free(CState *state, CObj *obj)
{
#ifdef GC_DEBUG
printf("freeing %p [", obj);
printObject(obj);
printf("]\n");
printf("freeing %s %p\n", cosmoO_typeStr(obj), obj);
#endif
switch (obj->type) {
case COBJ_STRING: {
CObjString *objStr = (CObjString *)obj;
cosmoM_freearray(state, char, objStr->str, objStr->length + 1);
cosmoM_freeArray(state, char, objStr->str, objStr->length + 1);
cosmoM_free(state, CObjString, objStr);
break;
}
case COBJ_OBJECT: {
CObjObject *objTbl = (CObjObject *)obj;
cosmoT_clearTable(state, &objTbl->tbl);
cosmoM_free(state, CObjObject, objTbl);
CObjObject *objObj = (CObjObject *)obj;
cosmoT_clearTable(state, &objObj->tbl);
cosmoM_free(state, CObjObject, objObj);
break;
}
case COBJ_TABLE: {
@@ -85,13 +82,13 @@ void cosmoO_free(CState *state, CObj *obj)
}
case COBJ_ERROR: {
CObjError *err = (CObjError *)obj;
cosmoM_freearray(state, CCallFrame, err->frames, err->frameCount);
cosmoM_freeArray(state, CCallFrame, err->frames, err->frameCount);
cosmoM_free(state, CObjError, obj);
break;
}
case COBJ_CLOSURE: {
CObjClosure *closure = (CObjClosure *)obj;
cosmoM_freearray(state, CObjUpval *, closure->upvalues, closure->upvalueCount);
cosmoM_freeArray(state, CObjUpval *, closure->upvalues, closure->upvalueCount);
cosmoM_free(state, CObjClosure, closure);
break;
}
@@ -167,14 +164,12 @@ _eqFail:
cosmoV_pushValue(state, eq1);
cosmoV_pushRef(state, obj1);
cosmoV_pushRef(state, obj2);
if (cosmoV_call(state, 2, 1) != COSMOVM_OK)
return false;
cosmoV_call(state, 2, 1);
// check return value and make sure it's a boolean
if (!IS_BOOLEAN(*cosmoV_getTop(state, 0))) {
cosmoV_error(state, "__equal expected to return <boolean>, got %s!",
cosmoV_typeStr(*cosmoV_pop(state)));
return false;
}
// return the result
@@ -191,10 +186,10 @@ CObjObject *cosmoO_newObject(CState *state)
obj->userP = NULL; // reserved for C API
obj->userT = 0;
obj->isLocked = false;
cosmoV_pushRef(state, (CObj *)obj); // so our GC can keep track of it
cosmoT_initTable(state, &obj->tbl, ARRAY_START);
cosmoV_pop(state);
return obj;
}
@@ -234,17 +229,17 @@ CObjCFunction *cosmoO_newCFunction(CState *state, CosmoCFunction func)
CObjError *cosmoO_newError(CState *state, CValue err)
{
CCallFrame *frames = cosmoM_xmalloc(state, sizeof(CCallFrame) * state->frameCount);
CObjError *cerror = (CObjError *)cosmoO_allocateBase(state, sizeof(CObjError), COBJ_ERROR);
cerror->err = err;
cerror->frameCount = state->frameCount;
cerror->frames = frames;
cerror->parserError = false;
// allocate the callframe
cerror->frames = cosmoM_xmalloc(state, sizeof(CCallFrame) * cerror->frameCount);
// clone the call frame
for (int i = 0; i < state->frameCount; i++)
for (int i = 0; i < state->frameCount; i++) {
cerror->frames[i] = state->callFrame[i];
}
return cerror;
}
@@ -311,7 +306,7 @@ CObjString *cosmoO_takeString(CState *state, char *str, size_t length)
// have we already interned this string?
if (lookup != NULL) {
cosmoM_freearray(state, char, str,
cosmoM_freeArray(state, char, str,
length + 1); // free our passed character array, it's unneeded!
return lookup;
}
@@ -327,8 +322,7 @@ CObjString *cosmoO_allocateString(CState *state, const char *str, size_t sz, uin
strObj->length = sz;
strObj->hash = hash;
// we push & pop the string so our GC can find it (we don't use freezeGC/unfreezeGC because we
// *want* a GC event to happen)
// push/pop to make sure GC doesn't collect it
cosmoV_pushRef(state, (CObj *)strObj);
cosmoT_insert(state, &state->strings, cosmoV_newRef((CObj *)strObj));
cosmoV_pop(state);
@@ -404,29 +398,27 @@ bool cosmoO_isDescendant(CObj *obj, CObjObject *proto)
}
// returns false if error thrown
bool cosmoO_getRawObject(CState *state, CObjObject *proto, CValue key, CValue *val, CObj *obj)
void cosmoO_getRawObject(CState *state, CObjObject *proto, CValue key, CValue *val, CObj *obj)
{
if (!cosmoT_get(state, &proto->tbl, key,
val)) { // if the field doesn't exist in the object, check the proto
if (cosmoO_getIString(state, proto, ISTRING_GETTER, val) && IS_TABLE(*val) &&
cosmoT_get(state, &cosmoV_readTable(*val)->tbl, key, val)) {
cosmoV_pushValue(state, *val); // push function
cosmoV_pushRef(state, (CObj *)obj); // push object
if (cosmoV_call(state, 1, 1) != COSMOVM_OK) // call the function with the 1 argument
return false;
*val = *cosmoV_pop(state); // set value to the return value of __index
return true;
cosmoV_pushValue(state, *val); // push function
cosmoV_pushRef(state, (CObj *)obj); // push object
cosmoV_call(state, 1, 1); // call the function with the 1 argument
*val = *cosmoV_pop(state); // set value to the return value of __index
return;
}
if (proto->_obj.proto != NULL &&
cosmoO_getRawObject(state, proto->_obj.proto, key, val, obj))
return true;
// maybe the field is defined in the proto?
if (proto->_obj.proto != NULL) {
cosmoO_getRawObject(state, proto->_obj.proto, key, val, obj);
return;
}
*val = cosmoV_newNil();
return true; // no protoobject to check against / key not found
}
return true;
}
void cosmoO_setRawObject(CState *state, CObjObject *proto, CValue key, CValue val, CObj *obj)
@@ -527,24 +519,20 @@ bool cosmoO_getIString(CState *state, CObjObject *object, int flag, CValue *val)
return false; // obj->proto was false, the istring doesn't exist in this object chain
}
bool cosmoO_indexObject(CState *state, CObjObject *object, CValue key, CValue *val)
void cosmoO_indexObject(CState *state, CObjObject *object, CValue key, CValue *val)
{
if (cosmoO_getIString(state, object, ISTRING_INDEX, val)) {
cosmoV_pushValue(state, *val); // push function
cosmoV_pushRef(state, (CObj *)object); // push object
cosmoV_pushValue(state, key); // push key
if (cosmoV_call(state, 2, 1) != COSMOVM_OK) // call the function with the 2 arguments
return false;
*val = *cosmoV_pop(state); // set value to the return value of __index
return true;
} else { // there's no __index function defined!
cosmoV_pushValue(state, *val); // push function
cosmoV_pushRef(state, (CObj *)object); // push object
cosmoV_pushValue(state, key); // push key
cosmoV_call(state, 2, 1); // call the function with the 2 arguments
*val = *cosmoV_pop(state); // set value to the return value of __index
} else { // there's no __index function defined!
cosmoV_error(state, "Couldn't index object without __index function!");
}
return false;
}
bool cosmoO_newIndexObject(CState *state, CObjObject *object, CValue key, CValue val)
void cosmoO_newIndexObject(CState *state, CObjObject *object, CValue key, CValue val)
{
CValue ret; // return value for cosmoO_getIString
@@ -553,12 +541,10 @@ bool cosmoO_newIndexObject(CState *state, CObjObject *object, CValue key, CValue
cosmoV_pushRef(state, (CObj *)object); // push object
cosmoV_pushValue(state, key); // push key & value pair
cosmoV_pushValue(state, val);
return cosmoV_call(state, 3, 0) == COSMOVM_OK;
cosmoV_call(state, 3, 0);
} else { // there's no __newindex function defined
cosmoV_error(state, "Couldn't set index on object without __newindex function!");
}
return false;
}
CObjString *cosmoO_toString(CState *state, CObj *obj)
@@ -570,8 +556,7 @@ CObjString *cosmoO_toString(CState *state, CObj *obj)
if (protoObject != NULL && cosmoO_getIString(state, protoObject, ISTRING_TOSTRING, &res)) {
cosmoV_pushValue(state, res);
cosmoV_pushRef(state, (CObj *)obj);
if (cosmoV_call(state, 1, 1) != COSMOVM_OK)
return cosmoO_copyString(state, "<err>", 5);
cosmoV_call(state, 1, 1);
// make sure the __tostring function returned a string
StkPtr ret = cosmoV_getTop(state, 0);
@@ -636,14 +621,12 @@ cosmo_Number cosmoO_toNumber(CState *state, CObj *obj)
if (proto != NULL && cosmoO_getIString(state, proto, ISTRING_TONUMBER, &res)) {
cosmoV_pushValue(state, res);
cosmoV_pushRef(state, (CObj *)obj);
if (cosmoV_call(state, 1, 1) != COSMOVM_OK) // call res, expect 1 return val of <number>
return 0;
cosmoV_call(state, 1, 1); // call res, expect 1 return val of <number>
StkPtr temp = cosmoV_getTop(state, 0);
if (!IS_NUMBER(*temp)) {
cosmoV_error(state, "__tonumber expected to return <number>, got %s!",
cosmoV_typeStr(*temp));
return 0;
}
// return number
@@ -669,9 +652,7 @@ int cosmoO_count(CState *state, CObj *obj)
if (proto != NULL && cosmoO_getIString(state, proto, ISTRING_COUNT, &res)) {
cosmoV_pushValue(state, res);
cosmoV_pushRef(state, (CObj *)obj);
if (cosmoV_call(state, 1, 1) !=
COSMOVM_OK) // call res, we expect 1 return value of type <number>
return 0;
cosmoV_call(state, 1, 1); // call res, we expect 1 return value of type <number>
StkPtr ret = cosmoV_getTop(state, 0);
if (!IS_NUMBER(*ret)) {
@@ -702,60 +683,61 @@ int cosmoO_count(CState *state, CObj *obj)
void printObject(CObj *o)
{
printf("%s ", cosmoO_typeStr(o));
switch (o->type) {
case COBJ_STRING: {
CObjString *objStr = (CObjString *)o;
printf("<string> \"%.*s\"", objStr->length, objStr->str);
printf("\"%.*s\"", objStr->length, objStr->str);
break;
}
case COBJ_OBJECT: {
printf("<obj> %p", (void *)o);
printf("%p", (void *)o);
break;
}
case COBJ_TABLE: {
CObjTable *tbl = (CObjTable *)o;
printf("<tbl> %p", (void *)tbl);
printf("%p", (void *)tbl);
break;
}
case COBJ_FUNCTION: {
CObjFunction *objFunc = (CObjFunction *)o;
if (objFunc->name != NULL)
printf("<function> %.*s", objFunc->name->length, objFunc->name->str);
printf("%.*s", objFunc->name->length, objFunc->name->str);
else
printf("<function> %s", UNNAMEDCHUNK);
printf("%s", UNNAMEDCHUNK);
break;
}
case COBJ_CFUNCTION: {
CObjCFunction *objCFunc = (CObjCFunction *)o;
printf("<c function> %p", (void *)objCFunc->cfunc);
printf("%p", (void *)objCFunc->cfunc);
break;
}
case COBJ_ERROR: {
CObjError *err = (CObjError *)o;
printf("<error> %p -> ", (void *)o);
printValue(err->err);
printf("%p -> ", (void *)o);
cosmoV_printValue(err->err);
break;
}
case COBJ_METHOD: {
CObjMethod *method = (CObjMethod *)o;
printf("<method> %p -> ", (void *)method);
printValue(method->func);
printf("%p -> ", (void *)method);
cosmoV_printValue(method->func);
break;
}
case COBJ_CLOSURE: {
CObjClosure *closure = (CObjClosure *)o;
printf("<closure> %p -> ", (void *)closure);
printf("%p -> ", (void *)closure);
printObject((CObj *)closure->function); // just print the function
break;
}
case COBJ_UPVALUE: {
CObjUpval *upval = (CObjUpval *)o;
printf("<upvalue> %p -> ", (void *)upval->val);
printValue(*upval->val);
printf("%p -> ", (void *)upval->val);
cosmoV_printValue(*upval->val);
break;
}
default:
printf("<unkn obj %p>", (void *)o);
printf("%p", (void *)o);
}
}
@@ -772,6 +754,8 @@ const char *cosmoO_typeStr(CObj *obj)
return "<function>";
case COBJ_CFUNCTION:
return "<c function>";
case COBJ_ERROR:
return "<error>";
case COBJ_METHOD:
return "<method>";
case COBJ_CLOSURE:

View File

@@ -3,6 +3,8 @@
#include "cosmo.h"
#include <stdarg.h>
typedef enum CObjType
{
COBJ_STRING,
@@ -32,7 +34,6 @@ typedef int (*CosmoCFunction)(CState *state, int argCount, CValue *args);
struct CObj
{
struct CObj *next;
struct CObj *nextRoot; // for the root linked list
struct CObjObject *proto; // protoobject, describes the behavior of the object
CObjType type;
bool isMarked; // for the GC
@@ -54,7 +55,7 @@ struct CObjError
CCallFrame *frames;
int frameCount;
int line; // reserved for parser errors
bool parserError; // if true, cosmoV_printError will format the error to the lexer
bool parserError; // if true, cosmoV_printBacktrace will format the error to the lexer
};
struct CObjObject
@@ -136,6 +137,7 @@ struct CObjUpval
#define cosmoV_readCFunction(x) (((CObjCFunction *)cosmoV_readRef(x))->cfunc)
#define cosmoV_readMethod(x) ((CObjMethod *)cosmoV_readRef(x))
#define cosmoV_readClosure(x) ((CObjClosure *)cosmoV_readRef(x))
#define cosmoV_readError(x) ((CObjError *)cosmoV_readRef(x))
#define cosmoO_readCString(x) ((CObjString *)x)->str
#define cosmoO_readType(x) ((CObj *)x)->type
@@ -145,7 +147,6 @@ static inline bool isObjType(CValue val, CObjType type)
return IS_REF(val) && cosmoV_readRef(val)->type == type;
}
// just protects against macro expansion
static inline bool IS_CALLABLE(CValue val)
{
return IS_CLOSURE(val) || IS_CFUNCTION(val) || IS_METHOD(val);
@@ -172,10 +173,10 @@ static inline CObjObject *cosmoO_grabProto(CObj *obj)
return obj->type == COBJ_OBJECT ? (CObjObject *)obj : obj->proto;
}
bool cosmoO_getRawObject(CState *state, CObjObject *proto, CValue key, CValue *val, CObj *obj);
void cosmoO_getRawObject(CState *state, CObjObject *proto, CValue key, CValue *val, CObj *obj);
void cosmoO_setRawObject(CState *state, CObjObject *proto, CValue key, CValue val, CObj *obj);
bool cosmoO_indexObject(CState *state, CObjObject *object, CValue key, CValue *val);
bool cosmoO_newIndexObject(CState *state, CObjObject *object, CValue key, CValue val);
void cosmoO_indexObject(CState *state, CObjObject *object, CValue key, CValue *val);
void cosmoO_newIndexObject(CState *state, CObjObject *object, CValue key, CValue val);
// sets the user-defined pointer, if a user-define integer is already defined it will be over
// written

View File

@@ -64,7 +64,7 @@ typedef enum
OP_FALSE,
OP_NIL,
OP_RETURN
OP_RETURN,
} COPCODE; // there can be a max of 256 instructions
#endif

View File

@@ -14,7 +14,15 @@
performance, however this will produce undefined behavior as you reach the stack limit (and may
cause a seg fault!). It is recommended to keep this enabled.
*/
#define SAFE_STACK
// #define SAFE_STACK
/*
NAN_BOXXED:
if undefined, the interpreter will use a tagged union to store values. This is the default.
Note that even though the sizeof(CValue) is 8 bytes for NAN_BOXXED (as opposed to 16 bytes for
the tagged union) no performance benefits were measured. I recommend keeping this undefined for
now.
*/
// #define NAN_BOXXED
// forward declare *most* stuff so our headers are cleaner
@@ -56,6 +64,4 @@ typedef int (*cosmo_Writer)(CState *state, const void *data, size_t size, const
#define UNNAMEDCHUNK "_main"
#define COSMOASSERT(x) assert(x)
#define CERROR(err) printf("%s : %s\n", "[ERROR]", err)
#endif

View File

@@ -59,14 +59,15 @@ typedef struct CCompilerState
typedef struct
{
CLexState lex;
CState *state;
CLexState *lex;
CCompilerState *compiler;
CObjString *module; // name of the module
CToken current;
CToken previous; // token right after the current token
bool hadError;
bool panic;
CToken previous; // token right after the current token
int workingStackCount; // we push CValues of objects we need onto the stack so the garbage
// collector can see them. this is the count of those values so we'll
// know how many to pop off when we're done
} CParseState;
typedef enum
@@ -101,14 +102,18 @@ static int expressionPrecedence(CParseState *pstate, int needed, Precedence prec
// returns # of pushed values onto the stack
static int expression(CParseState *pstate, int needed, bool forceNeeded);
static void statement(CParseState *pstate);
static void declaration(CParseState *pstate);
static void parseFunction(CParseState *pstate, FunctionType type);
static void expressionStatement(CParseState *pstate);
static ParseRule *getRule(CTokenType type);
static CObjFunction *endCompiler(CParseState *pstate);
// ================================================================ [FRONT END/TALK TO LEXER]
static void keepTrackOf(CParseState *pstate, CValue val)
{
pstate->workingStackCount++;
cosmoV_pushValue(pstate->state, val);
}
static void initCompilerState(CParseState *pstate, CCompilerState *ccstate, FunctionType type,
CCompilerState *enclosing)
{
@@ -124,6 +129,8 @@ static void initCompilerState(CParseState *pstate, CCompilerState *ccstate, Func
ccstate->function = cosmoO_newFunction(pstate->state);
ccstate->function->module = pstate->module;
keepTrackOf(pstate, cosmoV_newRef((CObj *)ccstate->function));
ccstate->loop.scope = -1; // there is no loop yet
if (type != FTYPE_SCRIPT) {
@@ -146,27 +153,27 @@ static void initCompilerState(CParseState *pstate, CCompilerState *ccstate, Func
static void initParseState(CParseState *pstate, CCompilerState *ccstate, CState *s,
const char *source, const char *module)
{
pstate->lex = cosmoL_newLexState(s, source);
cosmoL_initLexState(s, &pstate->lex, source);
pstate->state = s;
pstate->hadError = false;
pstate->panic = false;
pstate->compiler = ccstate;
pstate->module = cosmoO_copyString(s, module, strlen(module));
pstate->workingStackCount = 0;
keepTrackOf(pstate, cosmoV_newRef((CObj *)pstate->module));
initCompilerState(pstate, ccstate, FTYPE_SCRIPT, NULL); // enclosing starts as NULL
}
static void freeParseState(CParseState *pstate)
{
cosmoL_freeLexState(pstate->state, pstate->lex);
cosmoL_cleanupLexState(pstate->state, &pstate->lex);
// pop our working values off the stack
cosmoV_setTop(pstate->state, pstate->workingStackCount);
}
static void errorAt(CParseState *pstate, CToken *token, const char *format, va_list args)
{
if (pstate->hadError)
return;
if (token->type == TOKEN_EOF) {
cosmoV_pushString(pstate->state, "At end: ");
} else if (!(token->type == TOKEN_ERROR)) {
@@ -177,14 +184,9 @@ static void errorAt(CParseState *pstate, CToken *token, const char *format, va_l
cosmoO_pushVFString(pstate->state, format, args);
cosmoV_concat(pstate->state, 2); // concats the two strings together
CObjError *err = cosmoV_throw(pstate->state);
err->line = token->line;
err->parserError = true;
pstate->hadError = true;
pstate->panic = true;
// throw complete error string
cosmoV_concat(pstate->state, 2);
cosmoV_throw(pstate->state);
}
static void errorAtCurrent(CParseState *pstate, const char *format, ...)
@@ -206,7 +208,7 @@ static void error(CParseState *pstate, const char *format, ...)
static void advance(CParseState *pstate)
{
pstate->previous = pstate->current;
pstate->current = cosmoL_scanToken(pstate->lex);
pstate->current = cosmoL_scanToken(&pstate->lex);
if (pstate->current.type == TOKEN_ERROR) {
errorAtCurrent(pstate, pstate->current.start);
@@ -280,7 +282,6 @@ uint16_t makeConstant(CParseState *pstate, CValue val)
int indx = addConstant(pstate->state, getChunk(pstate), val);
if (indx > UINT16_MAX) {
error(pstate, "UInt overflow! Too many constants in one chunk!");
return 0;
}
return (uint16_t)indx;
@@ -350,7 +351,6 @@ static void addLocal(CParseState *pstate, CToken name)
{
if (pstate->compiler->localCount > UINT8_MAX) {
error(pstate, "UInt overflow! Too many locals in scope!");
return;
}
Local *local = &pstate->compiler->locals[pstate->compiler->localCount++];
@@ -365,7 +365,6 @@ static int addUpvalue(CParseState *pstate, CCompilerState *ccstate, uint8_t indx
if (upvals > UINT8_MAX) {
error(pstate, "UInt overflow! Too many upvalues in scope!");
return -1;
}
// check and make sure we haven't already captured it
@@ -481,6 +480,7 @@ static void string(CParseState *pstate, bool canAssign, Precedence prec)
{
CObjString *strObj =
cosmoO_takeString(pstate->state, pstate->previous.start, pstate->previous.length);
keepTrackOf(pstate, cosmoV_newRef((CObj *)strObj));
writeConstant(pstate, cosmoV_newRef((CObj *)strObj));
}
@@ -590,8 +590,6 @@ static void group(CParseState *pstate, bool canAssign, Precedence prec)
consume(pstate, TOKEN_RIGHT_PAREN, "Expected ')'");
}
#define WRITE_GLOBAL_OP(pstate, op, arg)
static void _etterAB(CParseState *pstate, uint8_t a, int b, bool isGlobal)
{
writeu8(pstate, a);
@@ -768,7 +766,6 @@ static void table(CParseState *pstate, bool canAssign, Precedence prec)
tblType = 1; // array-like
} else {
error(pstate, "Can't change table description type mid-definition!");
return;
}
entries++;
@@ -818,7 +815,7 @@ static void object(CParseState *pstate, bool canAssign, Precedence prec)
// "pop" the 1 value
valuePopped(pstate, 1);
entries++;
} while (match(pstate, TOKEN_COMMA) && !pstate->hadError);
} while (match(pstate, TOKEN_COMMA));
consume(pstate, TOKEN_RIGHT_BRACE, "Expected '}' to end object definition.");
}
@@ -1100,18 +1097,19 @@ static ParseRule *getRule(CTokenType type)
// returns true if it got past the first token (aka prefix wasn't null)
static bool parsePrecedence(CParseState *pstate, Precedence prec)
{
bool canAssign;
ParseFunc prefix, infix;
advance(pstate);
ParseFunc prefix = getRule(pstate->previous.type)->prefix;
if (prefix == NULL)
if ((prefix = getRule(pstate->previous.type)->prefix) == NULL)
return false;
bool canAssign = prec <= PREC_ASSIGNMENT;
canAssign = prec <= PREC_ASSIGNMENT;
prefix(pstate, canAssign, prec);
while (prec <= getRule(pstate->current.type)->level) {
ParseFunc infix = getRule(pstate->current.type)->infix;
if ((infix = getRule(pstate->current.type)->infix) == NULL)
break;
advance(pstate);
infix(pstate, canAssign, prec);
}
@@ -1158,9 +1156,6 @@ static uint16_t parseVariable(CParseState *pstate, const char *errorMessage, boo
static void defineVariable(CParseState *pstate, uint16_t global, bool forceLocal)
{
if (pstate->hadError)
return;
if (pstate->compiler->scopeDepth > 0 || forceLocal) {
markInitialized(pstate, global);
valuePopped(pstate, 1); // the local stays on the stack!
@@ -1177,7 +1172,7 @@ static void _proto(CParseState *pstate)
{
int entries = 0;
while (!match(pstate, TOKEN_END) && !match(pstate, TOKEN_EOF) && !pstate->hadError) {
while (!match(pstate, TOKEN_END) && !match(pstate, TOKEN_EOF)) {
if (match(pstate, TOKEN_FUNC)) {
// define method
consume(pstate, TOKEN_IDENTIFIER, "Expected identifier for method!");
@@ -1224,9 +1219,6 @@ static void localProto(CParseState *pstate)
static void popLocals(CParseState *pstate, int toScope)
{
if (pstate->hadError)
return;
// count the locals in scope to pop
int localsToPop = 0;
@@ -1271,7 +1263,7 @@ static void endScope(CParseState *pstate)
static void block(CParseState *pstate)
{
while (!check(pstate, TOKEN_END) && !check(pstate, TOKEN_EOF) && !check(pstate, TOKEN_ERROR)) {
declaration(pstate);
statement(pstate);
}
consume(pstate, TOKEN_END, "'end' expected to end block.'");
@@ -1328,7 +1320,7 @@ static void ifStatement(CParseState *pstate)
while (!check(pstate, TOKEN_END) && !check(pstate, TOKEN_ELSE) &&
!check(pstate, TOKEN_ELSEIF) && !check(pstate, TOKEN_EOF) &&
!check(pstate, TOKEN_ERROR)) {
declaration(pstate);
statement(pstate);
}
endScope(pstate);
@@ -1376,7 +1368,7 @@ static void endLoop(CParseState *pstate)
patchJmp(pstate, pstate->compiler->loop.breaks[--pstate->compiler->loop.breakCount]);
}
cosmoM_freearray(pstate->state, int, pstate->compiler->loop.breaks,
cosmoM_freeArray(pstate->state, int, pstate->compiler->loop.breaks,
pstate->compiler->loop.breakCapacity);
}
@@ -1478,10 +1470,10 @@ static void functionDeclaration(CParseState *pstate)
static void returnStatement(CParseState *pstate)
{
if (pstate->compiler->type != FTYPE_FUNCTION && pstate->compiler->type != FTYPE_METHOD) {
error(pstate, "Expected 'return' in function!");
return;
}
// if (pstate->compiler->type != FTYPE_FUNCTION && pstate->compiler->type != FTYPE_METHOD) {
// error(pstate, "Expected 'return' in function!");
// return;
// }
if (blockFollow(pstate->current)) { // does this return have a value
writeu8(pstate, OP_NIL);
@@ -1596,7 +1588,7 @@ static void forLoop(CParseState *pstate)
// parse initializer
if (!match(pstate, TOKEN_EOS)) {
expressionStatement(pstate);
statement(pstate);
consume(pstate, TOKEN_EOS, "Expected ';' after initializer");
}
@@ -1665,7 +1657,7 @@ static void breakStatement(CParseState *pstate)
pstate->compiler->localCount = savedLocals;
// add break to loop
cosmoM_growarray(pstate->state, int, pstate->compiler->loop.breaks,
cosmoM_growArray(pstate->state, int, pstate->compiler->loop.breaks,
pstate->compiler->loop.breakCount, pstate->compiler->loop.breakCapacity);
pstate->compiler->loop.breaks[pstate->compiler->loop.breakCount++] = writeJmp(pstate, OP_JMP);
}
@@ -1686,18 +1678,6 @@ static void continueStatement(CParseState *pstate)
writeJmpBack(pstate, pstate->compiler->loop.startBytecode);
}
static void synchronize(CParseState *pstate)
{
pstate->panic = false;
while (pstate->current.type != TOKEN_EOF) {
if (pstate->previous.type == TOKEN_EOS)
return;
advance(pstate);
}
}
static int expressionPrecedence(CParseState *pstate, int needed, Precedence prec, bool forceNeeded)
{
int lastExpected = pstate->compiler->expectedValues;
@@ -1724,7 +1704,7 @@ static int expression(CParseState *pstate, int needed, bool forceNeeded)
forceNeeded); // anything above assignments are an expression
}
static void expressionStatement(CParseState *pstate)
static void statement(CParseState *pstate)
{
int savedPushed = pstate->compiler->pushedValues;
@@ -1779,20 +1759,6 @@ static void expressionStatement(CParseState *pstate)
alignStack(pstate, savedPushed);
}
static void statement(CParseState *pstate)
{
expressionStatement(pstate);
}
static void declaration(CParseState *pstate)
{
statement(pstate);
// if we paniced, skip the whole statement!
if (pstate->panic)
synchronize(pstate);
}
static CObjFunction *endCompiler(CParseState *pstate)
{
popLocals(pstate, pstate->compiler->scopeDepth + 1); // remove the locals from other scopes
@@ -1812,37 +1778,22 @@ CObjFunction *cosmoP_compileString(CState *state, const char *source, const char
{
CParseState parser;
CCompilerState compiler;
cosmoM_freezeGC(state); // ignore all GC events while compiling
initParseState(&parser, &compiler, state, source, module);
advance(&parser);
while (!match(&parser, TOKEN_EOF)) {
declaration(&parser);
statement(&parser);
}
consume(&parser, TOKEN_EOF, "End of file expected!");
popLocals(&parser, 0);
if (parser.hadError) { // we don't free the function, the state already has a reference to it in
// it's linked list of objects!
endCompiler(&parser);
freeParseState(&parser);
cosmoM_unfreezeGC(state);
return NULL;
}
CObjFunction *resFunc = compiler.function;
// finally free out parser states
endCompiler(&parser);
freeParseState(&parser);
// push the funciton onto the stack so if we cause an GC event, it won't be free'd
cosmoV_pushRef(state, (CObj *)resFunc);
cosmoM_unfreezeGC(state);
cosmoV_pop(state);
return resFunc;
}

View File

@@ -4,8 +4,7 @@
#include "clex.h"
#include "cosmo.h"
// compiles source into CChunk, if NULL is returned, a syntaxical error has occurred and pushed onto
// the stack
// compiles source into CChunk
CObjFunction *cosmoP_compileString(CState *state, const char *source, const char *module);
#endif

View File

@@ -7,26 +7,45 @@
#include <string.h>
CPanic *cosmoV_newPanic(CState *state)
{
CPanic *panic = cosmoM_xmalloc(state, sizeof(CPanic));
panic->top = state->top;
panic->frameCount = state->frameCount;
panic->freezeGC = state->freezeGC;
panic->prev = state->panic;
state->panic = panic;
return panic;
}
void cosmoV_freePanic(CState *state)
{
CPanic *panic = state->panic;
state->panic = panic->prev;
cosmoM_free(state, CPanic, panic);
}
CState *cosmoV_newState()
{
// we use C's malloc because we don't want to trigger a GC with an invalid state
CState *state = malloc(sizeof(CState));
if (state == NULL) {
CERROR("failed to allocate memory!");
printf("[ERROR] failed to allocate memory!");
exit(1);
}
state->panic = false;
state->freezeGC = 1; // we start frozen
state->panic = NULL;
// GC
state->objects = NULL;
state->userRoots = NULL;
state->grayStack.count = 0;
state->grayStack.capacity = 2;
state->grayStack.array = NULL;
state->allocatedBytes = sizeof(CState);
state->allocatedBytes = 0;
state->nextGC = 1024 * 8; // threshhold starts at 8kb
// init stack
@@ -34,21 +53,23 @@ CState *cosmoV_newState()
state->frameCount = 0;
state->openUpvalues = NULL;
state->error = NULL;
// set default proto objects
for (int i = 0; i < COBJ_MAX; i++)
for (int i = 0; i < COBJ_MAX; i++) {
state->protoObjects[i] = NULL;
}
for (int i = 0; i < ISTRING_MAX; i++)
for (int i = 0; i < ISTRING_MAX; i++) {
state->iStrings[i] = NULL;
}
cosmoT_initTable(state, &state->strings, 16); // init string table
cosmoT_initTable(state, &state->registry, 16);
state->globals = cosmoO_newTable(state); // init global table
// setup all strings used by the VM
state->iStrings[ISTRING_INIT] = cosmoO_copyString(state, "__init", 6);
state->iStrings[ISTRING_GC] = cosmoO_copyString(state, "__gc", 4);
state->iStrings[ISTRING_TOSTRING] = cosmoO_copyString(state, "__tostring", 10);
state->iStrings[ISTRING_TONUMBER] = cosmoO_copyString(state, "__tonumber", 10);
state->iStrings[ISTRING_INDEX] = cosmoO_copyString(state, "__index", 7);
@@ -68,8 +89,9 @@ CState *cosmoV_newState()
state->iStrings[ISTRING_RESERVED] = cosmoO_copyString(state, "__reserved", 10);
// set the IString flags
for (int i = 0; i < ISTRING_MAX; i++)
for (int i = 0; i < ISTRING_MAX; i++) {
state->iStrings[i]->isIString = true;
}
state->freezeGC = 0; // unfreeze the state
return state;
@@ -80,38 +102,45 @@ void cosmoV_freeState(CState *state)
#ifdef GC_DEBUG
printf("state %p is being free'd!\n", state);
#endif
cosmoM_freezeGC(state);
// frees all the objects
CObj *objs = state->objects;
while (objs != NULL) {
CObj *next = objs->next;
#ifdef GC_DEBUG
printf("STATE FREEING %p\n", objs);
fflush(stdout);
#endif
cosmoO_free(state, objs);
objs = next;
}
// mark our internal VM strings NULL
for (int i = 0; i < ISTRING_MAX; i++)
for (int i = 0; i < ISTRING_MAX; i++) {
state->iStrings[i] = NULL;
}
// free our string table (the string table includes the internal VM strings)
cosmoT_clearTable(state, &state->strings);
// free our gray stack & finally free the state structure
cosmoM_freearray(state, CObj *, state->grayStack.array, state->grayStack.capacity);
cosmoT_clearTable(state, &state->registry);
// free our gray stack & finally free the state structure
cosmoM_freeArray(state, CObj *, state->grayStack.array, state->grayStack.capacity);
#ifdef GC_DEBUG
if (state->allocatedBytes != 0) {
printf("state->allocatedBytes doesn't match, got %lu\n", state->allocatedBytes);
}
#endif
// TODO: yeah idk, it looks like im missing 520 bytes somewhere? i'll look into it later
/*#ifdef GC_DEBUG
if (state->allocatedBytes != sizeof(CState)) {
printf("state->allocatedBytes doesn't match expected value (%lu), got %lu!",
sizeof(CState), state->allocatedBytes); exit(0);
}
#endif*/
free(state);
}
// expects 2*pairs values on the stack, each pair should consist of 1 key and 1 value
void cosmoV_register(CState *state, int pairs)
void cosmoV_addGlobals(CState *state, int pairs)
{
for (int i = 0; i < pairs; i++) {
StkPtr key = cosmoV_getTop(state, 1);
@@ -124,12 +153,57 @@ void cosmoV_register(CState *state, int pairs)
}
}
// expects 2*pairs values on the stack, each pair should consist of 1 key and 1 value
void cosmoV_addRegistry(CState *state, int pairs)
{
for (int i = 0; i < pairs; i++) {
StkPtr key = cosmoV_getTop(state, 1);
StkPtr val = cosmoV_getTop(state, 0);
CValue *oldVal = cosmoT_insert(state, &state->registry, *key);
*oldVal = *val;
cosmoV_setTop(state, 2); // pops the 2 values off the stack
}
}
// expects 1 key on the stack, pushes result
void cosmoV_getRegistry(CState *state)
{
CValue key = *cosmoV_pop(state);
CValue val;
if (!cosmoT_get(state, &state->registry, key, &val)) {
cosmoV_error(state, "failed to grab %s from registry", cosmoV_typeStr(key));
}
cosmoV_pushValue(state, val);
}
void cosmoV_setProto(CState *state)
{
StkPtr objVal = cosmoV_getTop(state, 1);
StkPtr protoVal = cosmoV_getTop(state, 0);
if (!IS_REF(*objVal) || !IS_OBJECT(*protoVal)) {
cosmoV_error(state, "cannot set %s to proto of type %s", cosmoV_typeStr(*objVal),
cosmoV_typeStr(*protoVal));
}
// actually set the protos
CObj *obj = cosmoV_readRef(*objVal);
CObjObject *proto = cosmoV_readObject(*protoVal);
obj->proto = proto;
cosmoV_setTop(state, 2);
}
void cosmoV_printStack(CState *state)
{
printf("==== [[ stack dump ]] ====\n");
for (CValue *top = state->top - 1; top >= state->stack; top--) {
printf("%d: ", (int)(top - state->stack));
printValue(*top);
cosmoV_printValue(*top);
printf("\n");
}
}

View File

@@ -6,6 +6,8 @@
#include "ctable.h"
#include "cvalue.h"
#include <setjmp.h>
struct CCallFrame
{
CObjClosure *closure;
@@ -16,6 +18,7 @@ struct CCallFrame
typedef enum IStringEnum
{
ISTRING_INIT, // __init
ISTRING_GC, // __gc
ISTRING_TOSTRING, // __tostring
ISTRING_TONUMBER, // __tonumber
ISTRING_EQUAL, // __equals
@@ -38,36 +41,56 @@ typedef struct ArrayCObj
int capacity;
} ArrayCObj;
typedef struct CPanic
{
jmp_buf jmp;
StkPtr top;
struct CPanic *prev;
int frameCount;
int freezeGC;
} CPanic;
struct CState
{
bool panic;
int freezeGC; // when > 0, GC events will be ignored (for internal use)
int frameCount;
CObjError *error; // NULL, unless panic is true
CObj *objects; // tracks all of our allocated objects
CObj *userRoots; // user definable roots, this holds CObjs that should be considered "roots",
// lets the VM know you are holding a reference to a CObj in your code
ArrayCObj grayStack; // keeps track of which objects *haven't yet* been traversed in our GC, but
// *have been* found
size_t allocatedBytes;
size_t nextGC; // when allocatedBytes reaches this threshhold, trigger a GC event
CObjUpval *openUpvalues; // tracks all of our still open (meaning still on the stack) upvalues
CTable strings;
CObjTable *globals;
CValue *top; // top of the stack
CObjObject *protoObjects[COBJ_MAX]; // proto object for each COBJ type [NULL = no default proto]
CObjString *iStrings[ISTRING_MAX]; // strings used internally by the VM, eg. __init, __index
CCallFrame callFrame[FRAME_MAX]; // call frames
CValue stack[STACK_MAX]; // stack
CObjObject *protoObjects[COBJ_MAX]; // proto object for each COBJ type [NULL = no default proto]
CObjString *iStrings[ISTRING_MAX]; // strings used internally by the VM, eg. __init, __index
CTable strings;
CTable registry;
ArrayCObj grayStack; // keeps track of which objects *haven't yet* been traversed in our GC, but
// *have been* found
CObjUpval *openUpvalues; // tracks all of our still open (meaning still on the stack) upvalues
CObjTable *globals;
CValue *top; // top of the stack
CObj *objects; // tracks all of our allocated objects
CPanic *panic;
size_t allocatedBytes;
size_t nextGC; // when allocatedBytes reaches this threshhold, trigger a GC event
int freezeGC; // when > 0, GC events will be ignored (for internal use)
int frameCount;
};
CPanic *cosmoV_newPanic(CState *state);
void cosmoV_freePanic(CState *state);
COSMO_API CState *cosmoV_newState();
// expects 2*pairs values on the stack, each pair should consist of 1 key and 1 value
COSMO_API void cosmoV_register(CState *state, int pairs);
COSMO_API void cosmoV_freeState(CState *state);
// expects 2*pairs values on the stack, each pair should consist of 1 key and 1 value
COSMO_API void cosmoV_addGlobals(CState *state, int pairs);
// expects 2*pairs values on the stack, each pair should consist of 1 key and 1 value
COSMO_API void cosmoV_addRegistry(CState *state, int pairs);
// expects 1 key on the stack, pushes result
COSMO_API void cosmoV_getRegistry(CState *state);
// expects <object>->proto = <object> (2 total) to be on the stack
COSMO_API void cosmoV_setProto(CState *state);
COSMO_API void cosmoV_printStack(CState *state);
#endif

View File

@@ -11,7 +11,7 @@
#define MIN_TABLE_CAPACITY ARRAY_START
// bit-twiddling hacks, gets the next power of 2
unsigned int nextPow2(unsigned int x)
static unsigned int nextPow2(unsigned int x)
{
if (x <= ARRAY_START - 1)
return ARRAY_START; // sanity check
@@ -34,6 +34,7 @@ void cosmoT_initTable(CState *state, CTable *tbl, int startCap)
tbl->capacityMask = startCap - 1;
tbl->count = 0;
tbl->tombstones = 0;
tbl->tombThreshold = 32;
tbl->table = NULL; // to let out GC know we're initalizing
tbl->table = cosmoM_xmalloc(state, sizeof(CTableEntry) * startCap);
@@ -46,33 +47,36 @@ void cosmoT_initTable(CState *state, CTable *tbl, int startCap)
void cosmoT_addTable(CState *state, CTable *from, CTable *to)
{
int cap = from->capacityMask + 1;
CTableEntry *entry;
int cap = cosmoT_getCapacity(from);
for (int i = 0; i < cap; i++) {
CTableEntry *entry = &from->table[i];
entry = &from->table[i];
if (!(IS_NIL(entry->key))) {
CValue *newVal = cosmoT_insert(state, to, entry->key);
*newVal = entry->val;
*cosmoT_insert(state, to, entry->key) = entry->val;
}
}
}
void cosmoT_clearTable(CState *state, CTable *tbl)
{
cosmoM_freearray(state, CTableEntry, tbl->table, (tbl->capacityMask + 1));
cosmoM_freeArray(state, CTableEntry, tbl->table, cosmoT_getCapacity(tbl));
}
uint32_t getObjectHash(CObj *obj)
static uint32_t getObjectHash(CObj *obj)
{
switch (obj->type) {
case COBJ_STRING:
return ((CObjString *)obj)->hash;
case COBJ_CFUNCTION:
return (uint32_t)((CObjCFunction *)obj)->cfunc;
default:
return (uint32_t)obj; // just "hash" the pointer
}
}
uint32_t getValueHash(CValue *val)
static uint32_t getValueHash(CValue *val)
{
switch (GET_TYPE(*val)) {
case COSMO_TREF:
@@ -85,8 +89,9 @@ uint32_t getValueHash(CValue *val)
return 0;
memcpy(buf, &num, sizeof(buf));
for (size_t i = 0; i < sizeof(cosmo_Number) / sizeof(uint32_t); i++)
for (size_t i = 0; i < sizeof(cosmo_Number) / sizeof(uint32_t); i++) {
buf[0] += buf[i];
}
return buf[0];
}
// TODO: add support for other types
@@ -112,10 +117,10 @@ static CTableEntry *findEntry(CState *state, CTableEntry *entries, int mask, CVa
if (IS_NIL(entry->val)) {
// it's empty! if we found a tombstone, return that so it'll be reused
return tomb != NULL ? tomb : entry;
} else {
// its a tombstone!
tomb = entry;
}
// its a tombstone!
tomb = entry;
} else if (cosmoV_equal(state, entry->key, key)) {
return entry;
}
@@ -140,7 +145,7 @@ static void resizeTbl(CState *state, CTable *tbl, int newCapacity, bool canShrin
return;
CTableEntry *entries = cosmoM_xmalloc(state, size);
oldCap = tbl->capacityMask + 1;
oldCap = cosmoT_getCapacity(tbl);
newCount = 0;
// set all nodes as NIL : NIL
@@ -163,7 +168,7 @@ static void resizeTbl(CState *state, CTable *tbl, int newCapacity, bool canShrin
}
// free the old table
cosmoM_freearray(state, CTableEntry, tbl->table, oldCap);
cosmoM_freeArray(state, CTableEntry, tbl->table, oldCap);
tbl->table = entries;
tbl->capacityMask = newCapacity - 1;
@@ -175,10 +180,10 @@ bool cosmoT_checkShrink(CState *state, CTable *tbl)
{
// if count > 8 and active entries < tombstones
if (tbl->count > MIN_TABLE_CAPACITY &&
(tbl->count - tbl->tombstones < tbl->tombstones ||
tbl->tombstones > 50)) { // TODO: 50 should be a threshhold
resizeTbl(state, tbl, nextPow2(tbl->count - tbl->tombstones) * GROW_FACTOR,
false); // shrink based on active entries to the next pow of 2
(tbl->count - tbl->tombstones < tbl->tombstones || tbl->tombstones > tbl->tombThreshold)) {
// shrink based on active entries to the next pow of 2
resizeTbl(state, tbl, nextPow2(tbl->count - tbl->tombstones) * GROW_FACTOR, false);
tbl->tombThreshold = tbl->count / 4;
return true;
}
@@ -189,7 +194,7 @@ bool cosmoT_checkShrink(CState *state, CTable *tbl)
COSMO_API CValue *cosmoT_insert(CState *state, CTable *tbl, CValue key)
{
// make sure we have enough space allocated
int cap = tbl->capacityMask + 1;
int cap = cosmoT_getCapacity(tbl);
if (tbl->count + 1 > (int)(cap * MAX_TABLE_FILL)) {
// grow table
int newCap = cap * GROW_FACTOR;
@@ -197,8 +202,7 @@ COSMO_API CValue *cosmoT_insert(CState *state, CTable *tbl, CValue key)
}
// insert into the table
CTableEntry *entry =
findEntry(state, tbl->table, tbl->capacityMask, key); // -1 for our capacity mask
CTableEntry *entry = findEntry(state, tbl->table, tbl->capacityMask, key);
if (IS_NIL(entry->key)) {
if (IS_NIL(entry->val)) // is it empty?
@@ -254,9 +258,10 @@ CObjString *cosmoT_lookupString(CTable *tbl, const char *str, int length, uint32
{
if (tbl->count == 0)
return 0; // sanity check
uint32_t indx =
hash & tbl->capacityMask; // since we know the capacity will *always* be a power of 2, we
// can use bitwise & to perform a MUCH faster mod operation
// since we know the capacity will *always* be a power of 2, we
// can use bitwise & to perform a MUCH faster mod operation
uint32_t indx = hash & tbl->capacityMask;
// keep looking for an open slot in the entries array
while (true) {
@@ -279,13 +284,13 @@ CObjString *cosmoT_lookupString(CTable *tbl, const char *str, int length, uint32
void cosmoT_printTable(CTable *tbl, const char *name)
{
printf("==== [[%s]] ====\n", name);
int cap = tbl->capacityMask + 1;
int cap = cosmoT_getCapacity(tbl);
for (int i = 0; i < cap; i++) {
CTableEntry *entry = &tbl->table[i];
if (!(IS_NIL(entry->key))) {
printValue(entry->key);
cosmoV_printValue(entry->key);
printf(" - ");
printValue(entry->val);
cosmoV_printValue(entry->val);
printf("\n");
}
}

View File

@@ -18,9 +18,12 @@ typedef struct CTable
int count;
int capacityMask; // +1 to get the capacity
int tombstones;
int tombThreshold;
CTableEntry *table;
} CTable;
#define cosmoT_getCapacity(tbl) ((tbl)->capacityMask + 1)
COSMO_API void cosmoT_initTable(CState *state, CTable *tbl, int startCap);
COSMO_API void cosmoT_clearTable(CState *state, CTable *tbl);
COSMO_API int cosmoT_count(CTable *tbl);

View File

@@ -17,7 +17,6 @@ static bool readCValue(UndumpState *udstate, CValue *val);
#define check(e) \
if (!e) { \
printf("FAILED %d\n", __LINE__); \
return false; \
}
@@ -33,6 +32,7 @@ static void initUndumpState(CState *state, UndumpState *udstate, cosmo_Reader re
static bool readBlock(UndumpState *udstate, void *data, size_t size)
{
if (udstate->readerStatus == 0) {
/* if reader returns 1, we expect an error was thrown */
udstate->readerStatus = udstate->reader(udstate->state, data, size, udstate->userData);
}
@@ -74,7 +74,7 @@ static bool checkHeader(UndumpState *udstate)
uint8_t tmp;
/* check header */
readBlock(udstate, magic, COSMO_MAGIC_LEN);
check(readBlock(udstate, magic, COSMO_MAGIC_LEN));
if (memcmp(magic, COSMO_MAGIC, COSMO_MAGIC_LEN) != 0) {
cosmoV_error(udstate->state, "bad header!");
return false;
@@ -117,6 +117,9 @@ static bool readCObjFunction(UndumpState *udstate, CObjFunction **func)
*func = cosmoO_newFunction(udstate->state);
/* make sure our GC can see that we're currently using this function (and the values it uses) */
cosmoV_pushRef(udstate->state, (CObj *)*func);
check(readCObjString(udstate, &(*func)->name));
check(readCObjString(udstate, &(*func)->module));
@@ -137,6 +140,8 @@ static bool readCObjFunction(UndumpState *udstate, CObjFunction **func)
addConstant(udstate->state, &(*func)->chunk, val);
}
/* pop function off stack */
cosmoV_pop(udstate->state);
return true;
}
@@ -168,10 +173,10 @@ static bool readCObj(UndumpState *udstate, CObj **obj)
static bool readCValue(UndumpState *udstate, CValue *val)
{
uint8_t type;
check(readu8(udstate, &type));
uint8_t t;
check(readu8(udstate, &t));
switch (type) {
switch (t) {
case COSMO_TNUMBER:
READ_VAR(udstate, val, cosmo_Number, cosmoV_newNumber)
case COSMO_TBOOLEAN:
@@ -186,24 +191,26 @@ static bool readCValue(UndumpState *udstate, CValue *val)
*val = cosmoV_newNil();
break;
default:
break;
cosmoV_error(udstate->state, "invalid value type: %d", t);
return false;
}
return true;
}
#undef READ_VAR
#undef check
int cosmoD_undump(CState *state, cosmo_Reader reader, const void *userData, CObjFunction **func)
{
UndumpState udstate;
initUndumpState(state, &udstate, reader, userData);
if (!checkHeader(&udstate)) {
cosmoV_pushNil(state);
return 1;
}
if (!readCObjFunction(&udstate, func)) {
cosmoV_pushNil(state);
return 1;
}

View File

@@ -13,12 +13,12 @@ void initValArray(CState *state, CValueArray *val, size_t startCapacity)
void cleanValArray(CState *state, CValueArray *array)
{
cosmoM_freearray(state, CValue, array->values, array->capacity);
cosmoM_freeArray(state, CValue, array->values, array->capacity);
}
void appendValArray(CState *state, CValueArray *array, CValue val)
{
cosmoM_growarray(state, CValue, array->values, array->count, array->capacity);
cosmoM_growArray(state, CValue, array->values, array->count, array->capacity);
array->values[array->count++] = val;
}
@@ -101,7 +101,7 @@ const char *cosmoV_typeStr(CValue val)
}
}
void printValue(CValue val)
void cosmoV_printValue(CValue val)
{
switch (GET_TYPE(val)) {
case COSMO_TNUMBER:

View File

@@ -119,7 +119,7 @@ void initValArray(CState *state, CValueArray *val, size_t startCapacity);
void cleanValArray(CState *state, CValueArray *array); // cleans array
void appendValArray(CState *state, CValueArray *array, CValue val);
void printValue(CValue val);
void cosmoV_printValue(CValue val);
COSMO_API bool cosmoV_equal(CState *state, CValue valA, CValue valB);
COSMO_API CObjString *cosmoV_toString(CState *state, CValue val);
COSMO_API cosmo_Number cosmoV_toNumber(CState *state, CValue val);

1404
src/cvm.c

File diff suppressed because it is too large Load Diff

View File

@@ -8,30 +8,39 @@
// #define VM_DEBUG
typedef enum
{
COSMOVM_OK,
COSMOVM_RUNTIME_ERR,
COSMOVM_BUILDTIME_ERR
} COSMOVMRESULT;
/*
if we're using GNUC or clang, we can use computed gotos which speeds up
cosmoV_execute by about 20% from benchmarking. of course, if you know
your compiler supports computed gotos, you can define VM_JUMPTABLE
although, this is disabled when VM_DEBUG is defined, since it can cause
issues with debugging
BTW: be weary of maliciously crafted cosmo dumps!! it's very easy to crash
cosmo with this enabled and reading invalid opcodes due to us just using the
opcode as an index into the jump table
*/
#if (defined(__GNUC__) || defined(__clang__)) && !defined(VM_DEBUG)
# define VM_JUMPTABLE
#endif
// args = # of pass parameters, nresults = # of expected results
COSMO_API COSMOVMRESULT cosmoV_call(CState *state, int args, int nresults);
COSMO_API COSMOVMRESULT cosmoV_pcall(CState *state, int args, int nresults);
COSMO_API void cosmoV_call(CState *state, int args, int nresults);
COSMO_API bool cosmoV_pcall(CState *state, int args, int nresults);
// pushes new object onto the stack & returns a pointer to the new object
COSMO_API CObjObject *cosmoV_makeObject(CState *state, int pairs);
COSMO_API void cosmoV_makeTable(CState *state, int pairs);
COSMO_API void cosmoV_concat(CState *state, int vals);
COSMO_API void cosmoV_pushFString(CState *state, const char *format, ...);
COSMO_API void cosmoV_printError(CState *state, CObjError *err);
COSMO_API CObjError *cosmoV_throw(CState *state);
COSMO_API void cosmoV_printBacktrace(CState *state, CObjError *err);
COSMO_API void cosmoV_throw(CState *state);
COSMO_API void cosmoV_error(CState *state, const char *format, ...);
COSMO_API void cosmo_insert(CState *state, int indx, CValue val);
COSMO_API void cosmoV_insert(CState *state, int indx, CValue val);
/*
Sets the default proto objects for the passed objType. Also walks through the object heap and
updates protos for the passed objType if that CObj* has no proto.
updates protos for the passed objType if that CObj* has no proto.
returns true if replacing a previously registered proto object for this type
*/
@@ -58,27 +67,21 @@ COSMO_API bool cosmoV_compileString(CState *state, const char *src, const char *
COSMO_API bool cosmoV_undump(CState *state, cosmo_Reader reader, const void *ud);
/*
expects object to be pushed, then the key.
returns false if an error was thrown, returns true if the value was pushed onto the stack and
the object and key were popped
expects object to be pushed, then the key. pops the key & object and pushes the value
*/
COSMO_API bool cosmoV_get(CState *state);
COSMO_API void cosmoV_get(CState *state);
/*
expects object to be pushed, then the key, and finally the new value.
returns false if an error was thrown, returns true if the value was set and the object key, and
value were popped
expects object to be pushed, then the key, and finally the new value. pops the object, key &
value
*/
COSMO_API bool cosmoV_set(CState *state);
COSMO_API void cosmoV_set(CState *state);
// wraps the closure into a CObjMethod, so the function is called as an invoked method
COSMO_API bool cosmoV_getMethod(CState *state, CObj *obj, CValue key, CValue *val);
COSMO_API void cosmoV_getMethod(CState *state, CObj *obj, CValue key, CValue *val);
// clears the stack, callstack and restores the state into a usable state after a calloverflow or
// another hard to recover error (keeps the global table intact)
COSMO_API bool cosmoV_restore(CState *state);
// check if the value at the top of the stack is a <obj> user type
COSMO_API bool cosmoV_isValueUserType(CState *state, CValue val, int userType);
// nice to have wrappers
@@ -91,13 +94,6 @@ static inline void cosmoV_pushValue(CState *state, CValue val)
// we reserve 8 slots for the error string and whatever c api we might be in
if (stackSize >= STACK_MAX - 8) {
if (state->panic) { // we're in a panic state, let the 8 reserved slots be filled
if (stackSize < STACK_MAX)
*(state->top++) = val;
return;
}
cosmoV_error(state, "Stack overflow!");
return;
}

653
util/getopt.h Normal file
View File

@@ -0,0 +1,653 @@
#ifndef __GETOPT_H__
/**
* DISCLAIMER
* This file is part of the mingw-w64 runtime package.
*
* The mingw-w64 runtime package and its code is distributed in the hope that it
* will be useful but WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESSED OR
* IMPLIED ARE HEREBY DISCLAIMED. This includes but is not limited to
* warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
* Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Sponsored in part by the Defense Advanced Research Projects
* Agency (DARPA) and Air Force Research Laboratory, Air Force
* Materiel Command, USAF, under agreement number F39502-99-1-0512.
*/
/*-
* Copyright (c) 2000 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Dieter Baron and Thomas Klausner.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#pragma warning(disable:4996)
#define __GETOPT_H__
/* All the headers include this file. */
#include <crtdefs.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <windows.h>
#ifdef __cplusplus
extern "C" {
#endif
#define REPLACE_GETOPT /* use this getopt as the system getopt(3) */
#ifdef REPLACE_GETOPT
int opterr = 1; /* if error message should be printed */
int optind = 1; /* index into parent argv vector */
int optopt = '?'; /* character checked for validity */
#undef optreset /* see getopt.h */
#define optreset __mingw_optreset
int optreset; /* reset getopt */
char *optarg; /* argument associated with option */
#endif
//extern int optind; /* index of first non-option in argv */
//extern int optopt; /* single option character, as parsed */
//extern int opterr; /* flag to enable built-in diagnostics... */
// /* (user may set to zero, to suppress) */
//
//extern char *optarg; /* pointer to argument of current option */
#define PRINT_ERROR ((opterr) && (*options != ':'))
#define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
#define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
#define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
/* return values */
#define BADCH (int)'?'
#define BADARG ((*options == ':') ? (int)':' : (int)'?')
#define INORDER (int)1
#ifndef __CYGWIN__
#define __progname __argv[0]
#else
extern char __declspec(dllimport) *__progname;
#endif
#ifdef __CYGWIN__
static char EMSG[] = "";
#else
#define EMSG ""
#endif
static int getopt_internal(int, char * const *, const char *,
const struct option *, int *, int);
static int parse_long_options(char * const *, const char *,
const struct option *, int *, int);
static int gcd(int, int);
static void permute_args(int, int, int, char * const *);
static char *place = EMSG; /* option letter processing */
/* XXX: set optreset to 1 rather than these two */
static int nonopt_start = -1; /* first non option argument (for permute) */
static int nonopt_end = -1; /* first option after non options (for permute) */
/* Error messages */
static const char recargchar[] = "option requires an argument -- %c";
static const char recargstring[] = "option requires an argument -- %s";
static const char ambig[] = "ambiguous option -- %.*s";
static const char noarg[] = "option doesn't take an argument -- %.*s";
static const char illoptchar[] = "unknown option -- %c";
static const char illoptstring[] = "unknown option -- %s";
static void
_vwarnx(const char *fmt,va_list ap)
{
(void)fprintf(stderr,"%s: ",__progname);
if (fmt != NULL)
(void)vfprintf(stderr,fmt,ap);
(void)fprintf(stderr,"\n");
}
static void
warnx(const char *fmt,...)
{
va_list ap;
va_start(ap,fmt);
_vwarnx(fmt,ap);
va_end(ap);
}
/*
* Compute the greatest common divisor of a and b.
*/
static int
gcd(int a, int b)
{
int c;
c = a % b;
while (c != 0) {
a = b;
b = c;
c = a % b;
}
return (b);
}
/*
* Exchange the block from nonopt_start to nonopt_end with the block
* from nonopt_end to opt_end (keeping the same order of arguments
* in each block).
*/
static void
permute_args(int panonopt_start, int panonopt_end, int opt_end,
char * const *nargv)
{
int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
char *swap;
/*
* compute lengths of blocks and number and size of cycles
*/
nnonopts = panonopt_end - panonopt_start;
nopts = opt_end - panonopt_end;
ncycle = gcd(nnonopts, nopts);
cyclelen = (opt_end - panonopt_start) / ncycle;
for (i = 0; i < ncycle; i++) {
cstart = panonopt_end+i;
pos = cstart;
for (j = 0; j < cyclelen; j++) {
if (pos >= panonopt_end)
pos -= nnonopts;
else
pos += nopts;
swap = nargv[pos];
/* LINTED const cast */
((char **) nargv)[pos] = nargv[cstart];
/* LINTED const cast */
((char **)nargv)[cstart] = swap;
}
}
}
#ifdef REPLACE_GETOPT
/*
* getopt --
* Parse argc/argv argument vector.
*
* [eventually this will replace the BSD getopt]
*/
int
getopt(int nargc, char * const *nargv, const char *options)
{
/*
* We don't pass FLAG_PERMUTE to getopt_internal() since
* the BSD getopt(3) (unlike GNU) has never done this.
*
* Furthermore, since many privileged programs call getopt()
* before dropping privileges it makes sense to keep things
* as simple (and bug-free) as possible.
*/
return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
}
#endif /* REPLACE_GETOPT */
//extern int getopt(int nargc, char * const *nargv, const char *options);
#ifdef _BSD_SOURCE
/*
* BSD adds the non-standard `optreset' feature, for reinitialisation
* of `getopt' parsing. We support this feature, for applications which
* proclaim their BSD heritage, before including this header; however,
* to maintain portability, developers are advised to avoid it.
*/
# define optreset __mingw_optreset
extern int optreset;
#endif
#ifdef __cplusplus
}
#endif
/*
* POSIX requires the `getopt' API to be specified in `unistd.h';
* thus, `unistd.h' includes this header. However, we do not want
* to expose the `getopt_long' or `getopt_long_only' APIs, when
* included in this manner. Thus, close the standard __GETOPT_H__
* declarations block, and open an additional __GETOPT_LONG_H__
* specific block, only when *not* __UNISTD_H_SOURCED__, in which
* to declare the extended API.
*/
#endif /* !defined(__GETOPT_H__) */
#if !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__)
#define __GETOPT_LONG_H__
#ifdef __cplusplus
extern "C" {
#endif
struct option /* specification for a long form option... */
{
const char *name; /* option name, without leading hyphens */
int has_arg; /* does it take an argument? */
int *flag; /* where to save its status, or NULL */
int val; /* its associated status value */
};
enum /* permitted values for its `has_arg' field... */
{
no_argument = 0, /* option never takes an argument */
required_argument, /* option always requires an argument */
optional_argument /* option may take an argument */
};
/*
* parse_long_options --
* Parse long options in argc/argv argument vector.
* Returns -1 if short_too is set and the option does not match long_options.
*/
static int
parse_long_options(char * const *nargv, const char *options,
const struct option *long_options, int *idx, int short_too)
{
char *current_argv, *has_equal;
size_t current_argv_len;
int i, ambiguous, match;
#define IDENTICAL_INTERPRETATION(_x, _y) \
(long_options[(_x)].has_arg == long_options[(_y)].has_arg && \
long_options[(_x)].flag == long_options[(_y)].flag && \
long_options[(_x)].val == long_options[(_y)].val)
current_argv = place;
match = -1;
ambiguous = 0;
optind++;
if ((has_equal = strchr(current_argv, '=')) != NULL) {
/* argument found (--option=arg) */
current_argv_len = has_equal - current_argv;
has_equal++;
} else
current_argv_len = strlen(current_argv);
for (i = 0; long_options[i].name; i++) {
/* find matching long option */
if (strncmp(current_argv, long_options[i].name,
current_argv_len))
continue;
if (strlen(long_options[i].name) == current_argv_len) {
/* exact match */
match = i;
ambiguous = 0;
break;
}
/*
* If this is a known short option, don't allow
* a partial match of a single character.
*/
if (short_too && current_argv_len == 1)
continue;
if (match == -1) /* partial match */
match = i;
else if (!IDENTICAL_INTERPRETATION(i, match))
ambiguous = 1;
}
if (ambiguous) {
/* ambiguous abbreviation */
if (PRINT_ERROR)
warnx(ambig, (int)current_argv_len,
current_argv);
optopt = 0;
return (BADCH);
}
if (match != -1) { /* option found */
if (long_options[match].has_arg == no_argument
&& has_equal) {
if (PRINT_ERROR)
warnx(noarg, (int)current_argv_len,
current_argv);
/*
* XXX: GNU sets optopt to val regardless of flag
*/
if (long_options[match].flag == NULL)
optopt = long_options[match].val;
else
optopt = 0;
return (BADARG);
}
if (long_options[match].has_arg == required_argument ||
long_options[match].has_arg == optional_argument) {
if (has_equal)
optarg = has_equal;
else if (long_options[match].has_arg ==
required_argument) {
/*
* optional argument doesn't use next nargv
*/
optarg = nargv[optind++];
}
}
if ((long_options[match].has_arg == required_argument)
&& (optarg == NULL)) {
/*
* Missing argument; leading ':' indicates no error
* should be generated.
*/
if (PRINT_ERROR)
warnx(recargstring,
current_argv);
/*
* XXX: GNU sets optopt to val regardless of flag
*/
if (long_options[match].flag == NULL)
optopt = long_options[match].val;
else
optopt = 0;
--optind;
return (BADARG);
}
} else { /* unknown option */
if (short_too) {
--optind;
return (-1);
}
if (PRINT_ERROR)
warnx(illoptstring, current_argv);
optopt = 0;
return (BADCH);
}
if (idx)
*idx = match;
if (long_options[match].flag) {
*long_options[match].flag = long_options[match].val;
return (0);
} else
return (long_options[match].val);
#undef IDENTICAL_INTERPRETATION
}
/*
* getopt_internal --
* Parse argc/argv argument vector. Called by user level routines.
*/
static int
getopt_internal(int nargc, char * const *nargv, const char *options,
const struct option *long_options, int *idx, int flags)
{
char *oli; /* option letter list index */
int optchar, short_too;
static int posixly_correct = -1;
if (options == NULL)
return (-1);
/*
* XXX Some GNU programs (like cvs) set optind to 0 instead of
* XXX using optreset. Work around this braindamage.
*/
if (optind == 0)
optind = optreset = 1;
/*
* Disable GNU extensions if POSIXLY_CORRECT is set or options
* string begins with a '+'.
*
* CV, 2009-12-14: Check POSIXLY_CORRECT anew if optind == 0 or
* optreset != 0 for GNU compatibility.
*/
if (posixly_correct == -1 || optreset != 0)
posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
if (*options == '-')
flags |= FLAG_ALLARGS;
else if (posixly_correct || *options == '+')
flags &= ~FLAG_PERMUTE;
if (*options == '+' || *options == '-')
options++;
optarg = NULL;
if (optreset)
nonopt_start = nonopt_end = -1;
start:
if (optreset || !*place) { /* update scanning pointer */
optreset = 0;
if (optind >= nargc) { /* end of argument vector */
place = EMSG;
if (nonopt_end != -1) {
/* do permutation, if we have to */
permute_args(nonopt_start, nonopt_end,
optind, nargv);
optind -= nonopt_end - nonopt_start;
}
else if (nonopt_start != -1) {
/*
* If we skipped non-options, set optind
* to the first of them.
*/
optind = nonopt_start;
}
nonopt_start = nonopt_end = -1;
return (-1);
}
if (*(place = nargv[optind]) != '-' ||
(place[1] == '\0' && strchr(options, '-') == NULL)) {
place = EMSG; /* found non-option */
if (flags & FLAG_ALLARGS) {
/*
* GNU extension:
* return non-option as argument to option 1
*/
optarg = nargv[optind++];
return (INORDER);
}
if (!(flags & FLAG_PERMUTE)) {
/*
* If no permutation wanted, stop parsing
* at first non-option.
*/
return (-1);
}
/* do permutation */
if (nonopt_start == -1)
nonopt_start = optind;
else if (nonopt_end != -1) {
permute_args(nonopt_start, nonopt_end,
optind, nargv);
nonopt_start = optind -
(nonopt_end - nonopt_start);
nonopt_end = -1;
}
optind++;
/* process next argument */
goto start;
}
if (nonopt_start != -1 && nonopt_end == -1)
nonopt_end = optind;
/*
* If we have "-" do nothing, if "--" we are done.
*/
if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
optind++;
place = EMSG;
/*
* We found an option (--), so if we skipped
* non-options, we have to permute.
*/
if (nonopt_end != -1) {
permute_args(nonopt_start, nonopt_end,
optind, nargv);
optind -= nonopt_end - nonopt_start;
}
nonopt_start = nonopt_end = -1;
return (-1);
}
}
/*
* Check long options if:
* 1) we were passed some
* 2) the arg is not just "-"
* 3) either the arg starts with -- we are getopt_long_only()
*/
if (long_options != NULL && place != nargv[optind] &&
(*place == '-' || (flags & FLAG_LONGONLY))) {
short_too = 0;
if (*place == '-')
place++; /* --foo long option */
else if (*place != ':' && strchr(options, *place) != NULL)
short_too = 1; /* could be short option too */
optchar = parse_long_options(nargv, options, long_options,
idx, short_too);
if (optchar != -1) {
place = EMSG;
return (optchar);
}
}
if ((optchar = (int)*place++) == (int)':' ||
(optchar == (int)'-' && *place != '\0') ||
(oli = (char*)strchr(options, optchar)) == NULL) {
/*
* If the user specified "-" and '-' isn't listed in
* options, return -1 (non-option) as per POSIX.
* Otherwise, it is an unknown option character (or ':').
*/
if (optchar == (int)'-' && *place == '\0')
return (-1);
if (!*place)
++optind;
if (PRINT_ERROR)
warnx(illoptchar, optchar);
optopt = optchar;
return (BADCH);
}
if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
/* -W long-option */
if (*place) /* no space */
/* NOTHING */;
else if (++optind >= nargc) { /* no arg */
place = EMSG;
if (PRINT_ERROR)
warnx(recargchar, optchar);
optopt = optchar;
return (BADARG);
} else /* white space */
place = nargv[optind];
optchar = parse_long_options(nargv, options, long_options,
idx, 0);
place = EMSG;
return (optchar);
}
if (*++oli != ':') { /* doesn't take argument */
if (!*place)
++optind;
} else { /* takes (optional) argument */
optarg = NULL;
if (*place) /* no white space */
optarg = place;
else if (oli[1] != ':') { /* arg not optional */
if (++optind >= nargc) { /* no arg */
place = EMSG;
if (PRINT_ERROR)
warnx(recargchar, optchar);
optopt = optchar;
return (BADARG);
} else
optarg = nargv[optind];
}
place = EMSG;
++optind;
}
/* dump back option letter */
return (optchar);
}
/*
* getopt_long --
* Parse argc/argv argument vector.
*/
int
getopt_long(int nargc, char * const *nargv, const char *options,
const struct option *long_options, int *idx)
{
return (getopt_internal(nargc, nargv, options, long_options, idx,
FLAG_PERMUTE));
}
/*
* getopt_long_only --
* Parse argc/argv argument vector.
*/
int
getopt_long_only(int nargc, char * const *nargv, const char *options,
const struct option *long_options, int *idx)
{
return (getopt_internal(nargc, nargv, options, long_options, idx,
FLAG_PERMUTE|FLAG_LONGONLY));
}
//extern int getopt_long(int nargc, char * const *nargv, const char *options,
// const struct option *long_options, int *idx);
//extern int getopt_long_only(int nargc, char * const *nargv, const char *options,
// const struct option *long_options, int *idx);
/*
* Previous MinGW implementation had...
*/
#ifndef HAVE_DECL_GETOPT
/*
* ...for the long form API only; keep this for compatibility.
*/
# define HAVE_DECL_GETOPT 1
#endif
#ifdef __cplusplus
}
#endif
#endif /* !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__) */

379
util/linenoise-win32.c Normal file
View File

@@ -0,0 +1,379 @@
/* this code is not standalone
* it is included into linenoise.c
* for windows.
* It is deliberately kept separate so that
* applications that have no need for windows
* support can omit this
*/
static DWORD orig_consolemode = 0;
static int flushOutput(struct current *current);
static void outputNewline(struct current *current);
static void refreshStart(struct current *current)
{
(void)current;
}
static void refreshEnd(struct current *current)
{
(void)current;
}
static void refreshStartChars(struct current *current)
{
assert(current->output == NULL);
/* We accumulate all output here */
current->output = sb_alloc();
#ifdef USE_UTF8
current->ubuflen = 0;
#endif
}
static void refreshNewline(struct current *current)
{
DRL("<nl>");
outputNewline(current);
}
static void refreshEndChars(struct current *current)
{
assert(current->output);
flushOutput(current);
sb_free(current->output);
current->output = NULL;
}
static int enableRawMode(struct current *current) {
DWORD n;
INPUT_RECORD irec;
current->outh = GetStdHandle(STD_OUTPUT_HANDLE);
current->inh = GetStdHandle(STD_INPUT_HANDLE);
if (!PeekConsoleInput(current->inh, &irec, 1, &n)) {
return -1;
}
if (getWindowSize(current) != 0) {
return -1;
}
if (GetConsoleMode(current->inh, &orig_consolemode)) {
SetConsoleMode(current->inh, ENABLE_PROCESSED_INPUT);
}
#ifdef USE_UTF8
/* XXX is this the right thing to do? */
SetConsoleCP(65001);
#endif
return 0;
}
static void disableRawMode(struct current *current)
{
SetConsoleMode(current->inh, orig_consolemode);
}
void linenoiseClearScreen(void)
{
/* XXX: This is ugly. Should just have the caller pass a handle */
struct current current;
current.outh = GetStdHandle(STD_OUTPUT_HANDLE);
if (getWindowSize(&current) == 0) {
COORD topleft = { 0, 0 };
DWORD n;
FillConsoleOutputCharacter(current.outh, ' ',
current.cols * current.rows, topleft, &n);
FillConsoleOutputAttribute(current.outh,
FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN,
current.cols * current.rows, topleft, &n);
SetConsoleCursorPosition(current.outh, topleft);
}
}
static void cursorToLeft(struct current *current)
{
COORD pos;
DWORD n;
pos.X = 0;
pos.Y = (SHORT)current->y;
FillConsoleOutputAttribute(current->outh,
FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN, current->cols, pos, &n);
current->x = 0;
}
#ifdef USE_UTF8
static void flush_ubuf(struct current *current)
{
COORD pos;
DWORD nwritten;
pos.Y = (SHORT)current->y;
pos.X = (SHORT)current->x;
SetConsoleCursorPosition(current->outh, pos);
WriteConsoleW(current->outh, current->ubuf, current->ubuflen, &nwritten, 0);
current->x += current->ubufcols;
current->ubuflen = 0;
current->ubufcols = 0;
}
static void add_ubuf(struct current *current, int ch)
{
/* This code originally by: Author: Mark E. Davis, 1994. */
static const int halfShift = 10; /* used for shifting by 10 bits */
static const DWORD halfBase = 0x0010000UL;
static const DWORD halfMask = 0x3FFUL;
#define UNI_SUR_HIGH_START 0xD800
#define UNI_SUR_HIGH_END 0xDBFF
#define UNI_SUR_LOW_START 0xDC00
#define UNI_SUR_LOW_END 0xDFFF
#define UNI_MAX_BMP 0x0000FFFF
if (ch > UNI_MAX_BMP) {
/* convert from unicode to utf16 surrogate pairs
* There is always space for one extra word in ubuf
*/
ch -= halfBase;
current->ubuf[current->ubuflen++] = (WORD)((ch >> halfShift) + UNI_SUR_HIGH_START);
current->ubuf[current->ubuflen++] = (WORD)((ch & halfMask) + UNI_SUR_LOW_START);
}
else {
current->ubuf[current->ubuflen++] = ch;
}
current->ubufcols += utf8_width(ch);
if (current->ubuflen >= UBUF_MAX_CHARS) {
flush_ubuf(current);
}
}
#endif
static int flushOutput(struct current *current)
{
const char *pt = sb_str(current->output);
int len = sb_len(current->output);
#ifdef USE_UTF8
/* convert utf8 in current->output into utf16 in current->ubuf
*/
while (len) {
int ch;
int n = utf8_tounicode(pt, &ch);
pt += n;
len -= n;
add_ubuf(current, ch);
}
flush_ubuf(current);
#else
DWORD nwritten;
COORD pos;
pos.Y = (SHORT)current->y;
pos.X = (SHORT)current->x;
SetConsoleCursorPosition(current->outh, pos);
WriteConsoleA(current->outh, pt, len, &nwritten, 0);
current->x += len;
#endif
sb_clear(current->output);
return 0;
}
static int outputChars(struct current *current, const char *buf, int len)
{
if (len < 0) {
len = strlen(buf);
}
assert(current->output);
sb_append_len(current->output, buf, len);
return 0;
}
static void outputNewline(struct current *current)
{
/* On the last row output a newline to force a scroll */
if (current->y + 1 == current->rows) {
outputChars(current, "\n", 1);
}
flushOutput(current);
current->x = 0;
current->y++;
}
static void setOutputHighlight(struct current *current, const int *props, int nprops)
{
int colour = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;
int bold = 0;
int reverse = 0;
int i;
for (i = 0; i < nprops; i++) {
switch (props[i]) {
case 0:
colour = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;
bold = 0;
reverse = 0;
break;
case 1:
bold = FOREGROUND_INTENSITY;
break;
case 7:
reverse = 1;
break;
case 30:
colour = 0;
break;
case 31:
colour = FOREGROUND_RED;
break;
case 32:
colour = FOREGROUND_GREEN;
break;
case 33:
colour = FOREGROUND_RED | FOREGROUND_GREEN;
break;
case 34:
colour = FOREGROUND_BLUE;
break;
case 35:
colour = FOREGROUND_RED | FOREGROUND_BLUE;
break;
case 36:
colour = FOREGROUND_BLUE | FOREGROUND_GREEN;
break;
case 37:
colour = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;
break;
}
}
flushOutput(current);
if (reverse) {
SetConsoleTextAttribute(current->outh, BACKGROUND_INTENSITY);
}
else {
SetConsoleTextAttribute(current->outh, colour | bold);
}
}
static void eraseEol(struct current *current)
{
COORD pos;
DWORD n;
pos.X = (SHORT) current->x;
pos.Y = (SHORT) current->y;
FillConsoleOutputCharacter(current->outh, ' ', current->cols - current->x, pos, &n);
}
static void setCursorXY(struct current *current)
{
COORD pos;
pos.X = (SHORT) current->x;
pos.Y = (SHORT) current->y;
SetConsoleCursorPosition(current->outh, pos);
}
static void setCursorPos(struct current *current, int x)
{
current->x = x;
setCursorXY(current);
}
static void cursorUp(struct current *current, int n)
{
current->y -= n;
setCursorXY(current);
}
static void cursorDown(struct current *current, int n)
{
current->y += n;
setCursorXY(current);
}
static int fd_read(struct current *current)
{
while (1) {
INPUT_RECORD irec;
DWORD n;
if (WaitForSingleObject(current->inh, INFINITE) != WAIT_OBJECT_0) {
break;
}
if (!ReadConsoleInputW(current->inh, &irec, 1, &n)) {
break;
}
if (irec.EventType == KEY_EVENT) {
KEY_EVENT_RECORD *k = &irec.Event.KeyEvent;
if (k->bKeyDown || k->wVirtualKeyCode == VK_MENU) {
if (k->dwControlKeyState & ENHANCED_KEY) {
switch (k->wVirtualKeyCode) {
case VK_LEFT:
return SPECIAL_LEFT;
case VK_RIGHT:
return SPECIAL_RIGHT;
case VK_UP:
return SPECIAL_UP;
case VK_DOWN:
return SPECIAL_DOWN;
case VK_INSERT:
return SPECIAL_INSERT;
case VK_DELETE:
return SPECIAL_DELETE;
case VK_HOME:
return SPECIAL_HOME;
case VK_END:
return SPECIAL_END;
case VK_PRIOR:
return SPECIAL_PAGE_UP;
case VK_NEXT:
return SPECIAL_PAGE_DOWN;
case VK_RETURN:
return k->uChar.UnicodeChar;
}
}
/* Note that control characters are already translated in AsciiChar */
else if (k->wVirtualKeyCode == VK_CONTROL)
continue;
else {
return k->uChar.UnicodeChar;
}
}
}
}
return -1;
}
static int getWindowSize(struct current *current)
{
CONSOLE_SCREEN_BUFFER_INFO info;
if (!GetConsoleScreenBufferInfo(current->outh, &info)) {
return -1;
}
current->cols = info.dwSize.X;
current->rows = info.dwSize.Y;
if (current->cols <= 0 || current->rows <= 0) {
current->cols = 80;
return -1;
}
current->y = info.dwCursorPosition.Y;
current->x = info.dwCursorPosition.X;
return 0;
}

2786
util/linenoise.c Normal file

File diff suppressed because it is too large Load Diff

152
util/linenoise.h Normal file
View File

@@ -0,0 +1,152 @@
/* linenoise.h -- guerrilla line editing library against the idea that a
* line editing lib needs to be 20,000 lines of C code.
*
* See linenoise.c for more information.
*
* ------------------------------------------------------------------------
*
* Copyright (c) 2010, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __LINENOISE_H
#define __LINENOISE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <inttypes.h>
#include <string.h>
#ifndef NO_COMPLETION
typedef struct linenoiseCompletions {
size_t len;
char **cvec;
} linenoiseCompletions;
/*
* The callback type for tab completion handlers.
*/
typedef void(linenoiseCompletionCallback)(const char *prefix, linenoiseCompletions *comp, void *userdata);
/*
* Sets the current tab completion handler and returns the previous one, or NULL
* if no prior one has been set.
*/
linenoiseCompletionCallback * linenoiseSetCompletionCallback(linenoiseCompletionCallback *comp, void *userdata);
/*
* Adds a copy of the given string to the given completion list. The copy is owned
* by the linenoiseCompletions object.
*/
void linenoiseAddCompletion(linenoiseCompletions *comp, const char *str);
typedef char*(linenoiseHintsCallback)(const char *, int *color, int *bold, void *userdata);
typedef void(linenoiseFreeHintsCallback)(void *hint, void *userdata);
void linenoiseSetHintsCallback(linenoiseHintsCallback *callback, void *userdata);
void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *callback);
#endif
/*
* Prompts for input using the given string as the input
* prompt. Returns when the user has tapped ENTER or (on an empty
* line) EOF (Ctrl-D on Unix, Ctrl-Z on Windows). Returns either
* a copy of the entered string (for ENTER) or NULL (on EOF). The
* caller owns the returned string and must eventually free() it.
*/
char *linenoise(const char *prompt);
/**
* Like linenoise() but starts with an initial buffer.
*/
char *linenoiseWithInitial(const char *prompt, const char *initial);
/**
* Clear the screen.
*/
void linenoiseClearScreen(void);
/*
* Adds a copy of the given line of the command history.
*/
int linenoiseHistoryAdd(const char *line);
/*
* Sets the maximum length of the command history, in lines.
* If the history is currently longer, it will be trimmed,
* retaining only the most recent entries. If len is 0 or less
* then this function does nothing.
*/
int linenoiseHistorySetMaxLen(int len);
/*
* Returns the current maximum length of the history, in lines.
*/
int linenoiseHistoryGetMaxLen(void);
/*
* Saves the current contents of the history to the given file.
* Returns 0 on success.
*/
int linenoiseHistorySave(const char *filename);
/*
* Replaces the current history with the contents
* of the given file. Returns 0 on success.
*/
int linenoiseHistoryLoad(const char *filename);
/*
* Frees all history entries, clearing the history.
*/
void linenoiseHistoryFree(void);
/*
* Returns a pointer to the list of history entries, writing its
* length to *len if len is not NULL. The memory is owned by linenoise
* and must not be freed.
*/
char **linenoiseHistory(int *len);
/*
* Returns the number of display columns in the current terminal.
*/
int linenoiseColumns(void);
/**
* Enable or disable multiline mode (disabled by default)
*/
void linenoiseSetMultiLine(int enableml);
#ifdef __cplusplus
}
#endif
#endif /* __LINENOISE_H */