2021-02-07 15:40:00 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Thakee Nathees
|
|
|
|
* Licensed under: MIT License
|
|
|
|
*/
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
#ifndef PK_COMMON_H
|
|
|
|
#define PK_COMMON_H
|
2021-02-07 15:40:00 +08:00
|
|
|
|
2021-05-15 17:29:44 +08:00
|
|
|
#include "include/pocketlang.h"
|
2021-02-25 17:03:06 +08:00
|
|
|
|
2021-02-07 15:40:00 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2021-02-08 02:30:29 +08:00
|
|
|
#include <stdarg.h>
|
2021-02-07 15:40:00 +08:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-02-18 02:27:24 +08:00
|
|
|
// Set this to dump compiled opcodes of each functions.
|
2021-02-25 17:03:06 +08:00
|
|
|
#define DEBUG_DUMP_COMPILED_CODE 0
|
2021-02-07 15:40:00 +08:00
|
|
|
|
2021-05-19 02:59:09 +08:00
|
|
|
// Set this to dump stack frame before executing the next instruction.
|
|
|
|
#define DEBUG_DUMP_CALL_STACK 0
|
|
|
|
|
2021-05-11 14:38:23 +08:00
|
|
|
#include <stdio.h> //< Only needed for ASSERT() macro and for release mode
|
|
|
|
//< TODO; macro use this to print a crash report.
|
|
|
|
// The internal assertion macro, do not use this. Use ASSERT() instead.
|
|
|
|
#define __ASSERT(condition, message) \
|
2021-02-12 01:35:43 +08:00
|
|
|
do { \
|
|
|
|
if (!(condition)) { \
|
|
|
|
fprintf(stderr, "Assertion failed: %s\n\tat %s() (%s:%i)\n", \
|
|
|
|
message, __func__, __FILE__, __LINE__); \
|
|
|
|
DEBUG_BREAK(); \
|
|
|
|
abort(); \
|
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
|
2021-05-11 14:38:23 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define DEBUG_BREAK() __debugbreak()
|
|
|
|
#else
|
|
|
|
#define DEBUG_BREAK() __builtin_trap()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ASSERT(condition, message) __ASSERT(condition, message)
|
|
|
|
|
2021-02-18 02:27:24 +08:00
|
|
|
#define ASSERT_INDEX(index, size) \
|
|
|
|
ASSERT(index >= 0 && index < size, "Index out of bounds.")
|
|
|
|
|
2021-02-12 01:35:43 +08:00
|
|
|
#define UNREACHABLE() \
|
|
|
|
do { \
|
|
|
|
fprintf(stderr, "Execution reached an unreachable path\n" \
|
|
|
|
"\tat %s() (%s:%i)\n", __func__, __FILE__, __LINE__); \
|
|
|
|
abort(); \
|
|
|
|
} while (false)
|
2021-02-07 15:40:00 +08:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2021-02-12 01:35:43 +08:00
|
|
|
#define DEBUG_BREAK()
|
2021-02-07 15:40:00 +08:00
|
|
|
#define ASSERT(condition, message) do { } while (false)
|
2021-02-25 17:03:06 +08:00
|
|
|
#define ASSERT_INDEX(index, size) do {} while (false)
|
2021-02-07 15:40:00 +08:00
|
|
|
|
|
|
|
// Reference : https://github.com/wren-lang/
|
|
|
|
#if defined( _MSC_VER )
|
|
|
|
#define UNREACHABLE() __assume(0)
|
|
|
|
#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
|
|
|
|
#define UNREACHABLE() __builtin_unreachable()
|
|
|
|
#else
|
|
|
|
#define UNREACHABLE()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // DEBUG
|
|
|
|
|
2021-05-11 14:38:23 +08:00
|
|
|
// Using __ASSERT() for make it crash in release binary too.
|
|
|
|
#define TODO __ASSERT(false, "TODO: It hasn't implemented yet.")
|
2021-02-13 01:40:19 +08:00
|
|
|
#define OOPS "Oops a bug!! report plese."
|
2021-02-12 01:35:43 +08:00
|
|
|
|
2021-02-18 02:27:24 +08:00
|
|
|
#define STRINGIFY(x) TOSTRING(x)
|
|
|
|
#define TOSTRING(x) #x
|
2021-05-09 06:44:08 +08:00
|
|
|
#define M_CONC(a, b) M_CONC_(a, b)
|
|
|
|
#define M_CONC_(a, b) a##b
|
2021-02-18 02:27:24 +08:00
|
|
|
|
|
|
|
// The factor by which a buffer will grow when it's capacity reached.
|
|
|
|
#define GROW_FACTOR 2
|
|
|
|
|
|
|
|
// The initial capacity of a buffer.
|
|
|
|
#define MIN_CAPACITY 8
|
|
|
|
|
2021-02-07 15:40:00 +08:00
|
|
|
// Allocate object of [type] using the vmRealloc function.
|
|
|
|
#define ALLOCATE(vm, type) \
|
|
|
|
((type*)vmRealloc(vm, NULL, 0, sizeof(type)))
|
|
|
|
|
|
|
|
// Allocate object of [type] which has a dynamic tail array of type [tail_type]
|
|
|
|
// with [count] entries.
|
|
|
|
#define ALLOCATE_DYNAMIC(vm, type, count, tail_type) \
|
|
|
|
((type*)vmRealloc(vm, NULL, 0, sizeof(type) + sizeof(tail_type) * (count)))
|
|
|
|
|
2021-02-11 01:23:48 +08:00
|
|
|
// Allocate [count] ammount of object of [type] array.
|
|
|
|
#define ALLOCATE_ARRAY(vm, type, count) \
|
|
|
|
((type*)vmRealloc(vm, NULL, 0, sizeof(type) * (count)))
|
|
|
|
|
|
|
|
// Deallocate a pointer allocated by vmRealloc before.
|
|
|
|
#define DEALLOCATE(vm, pointer) \
|
|
|
|
vmRealloc(vm, pointer, 0, 0)
|
|
|
|
|
2021-02-18 02:27:24 +08:00
|
|
|
typedef struct Object Object;
|
|
|
|
typedef struct String String;
|
|
|
|
typedef struct List List;
|
2021-05-03 21:09:23 +08:00
|
|
|
typedef struct Map Map;
|
2021-02-18 02:27:24 +08:00
|
|
|
typedef struct Range Range;
|
|
|
|
|
|
|
|
typedef struct Script Script;
|
|
|
|
typedef struct Function Function;
|
|
|
|
|
|
|
|
// Unique number to identify for various cases.
|
|
|
|
typedef uint32_t ID;
|
|
|
|
|
2021-02-25 17:03:06 +08:00
|
|
|
// VM's fiber type.
|
|
|
|
typedef struct Fiber Fiber;
|
|
|
|
|
2021-05-09 18:28:00 +08:00
|
|
|
#endif //PK_COMMON_H
|