Skip to content

Commit 58a44ec

Browse files
committed
refactoring
1 parent 22dda39 commit 58a44ec

File tree

27 files changed

+281
-213
lines changed

27 files changed

+281
-213
lines changed

cli/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,14 @@ static inline int run_bytecode(HkClosure *cl, ParsedArgs *parsedArgs)
249249
int exitCode = EXIT_FAILURE;
250250
if (hk_vm_is_ok(&vm))
251251
{
252-
HkValue result = vm.stackSlots[vm.stackTop];
252+
HkValue result = hk_stack_get(&vm.vstk, 0);
253253
exitCode = hk_is_int(result) ? (int) hk_as_number(result) : EXIT_SUCCESS;
254254
hk_vm_pop(&vm);
255255
goto end;
256256
}
257257
if (hk_vm_is_exit(&vm))
258258
{
259-
HkValue result = vm.stackSlots[vm.stackTop];
259+
HkValue result = hk_stack_get(&vm.vstk, 0);
260260
hk_assert(hk_is_int(result), "exit code must be an integer");
261261
exitCode = (int) hk_as_number(result);
262262
hk_vm_pop(&vm);

core/json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static inline HkValue json_to_value(HkVM *vm, cJSON *json)
122122
json_field = json_field->next;
123123
}
124124
hk_vm_construct(vm, length);
125-
HkInstance *inst = hk_as_instance(vm->stackSlots[vm->stackTop]);
125+
HkInstance *inst = hk_as_instance(hk_stack_get(&vm->vstk, 0));
126126
hk_incr_ref(inst);
127127
hk_vm_pop(vm);
128128
hk_decr_ref(inst);

include/hook.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "hook/iterator.h"
2121
#include "hook/memory.h"
2222
#include "hook/range.h"
23+
#include "hook/stack.h"
2324
#include "hook/string.h"
2425
#include "hook/struct.h"
2526
#include "hook/userdata.h"

include/hook/stack.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// stack.h
3+
//
4+
// Copyright 2021 The Hook Programming Language Authors.
5+
//
6+
// This file is part of the Hook project.
7+
// For detailed license information, please refer to the LICENSE file
8+
// located in the root directory of this project.
9+
//
10+
11+
#ifndef HK_STACK_H
12+
#define HK_STACK_H
13+
14+
#include "memory.h"
15+
16+
#define HkStack(T) \
17+
struct { \
18+
T *base; \
19+
T *top; \
20+
T *limit; \
21+
}
22+
23+
#define hk_stack_init(stk, sz) \
24+
do { \
25+
size_t _size = sizeof(*(stk)->base) * (sz); \
26+
void *base = hk_allocate(_size); \
27+
(stk)->base = base; \
28+
(stk)->top = &(stk)->base[-1]; \
29+
(stk)->limit = &(stk)->base[(sz) - 1]; \
30+
} while (0)
31+
32+
#define hk_stack_deinit(stk) \
33+
do { \
34+
hk_free((stk)->base); \
35+
} while (0)
36+
37+
#define hk_stack_is_empty(stk) ((stk)->top == &(stk)->base[-1])
38+
39+
#define hk_stack_is_full(stk) ((stk)->top == (stk)->limit)
40+
41+
#define hk_stack_get(stk, i) ((stk)->top[- (i)])
42+
43+
#define hk_stack_set(stk, i, d) \
44+
do { \
45+
(stk)->top[- (i)] = (d); \
46+
} while (0)
47+
48+
#define hk_stack_push(stk, d) \
49+
do { \
50+
++(stk)->top; \
51+
(stk)->top[0] = (d); \
52+
} while (0)
53+
54+
#define hk_stack_pop(stk) \
55+
do { \
56+
--(stk)->top; \
57+
} while (0)
58+
59+
#endif // HK_STACK_H

include/hook/vm.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414
#include "callable.h"
1515
#include "range.h"
16+
#include "stack.h"
1617
#include "struct.h"
1718
#include "userdata.h"
1819

1920
#define HK_VM_FLAG_NONE 0x00
2021
#define HK_VM_FLAG_NO_TRACE 0x01
2122

22-
#define HK_STACK_MIN_CAPACITY (1 << 8)
23+
#define HK_VM_STACK_DEFAULT_SIZE (1 << 10)
2324

2425
#define hk_vm_is_no_trace(s) ((s)->flags & HK_VM_FLAG_NO_TRACE)
2526

@@ -42,14 +43,12 @@ typedef enum
4243

4344
typedef struct HkVM
4445
{
45-
int stackEnd;
46-
int stackTop;
47-
HkValue *stackSlots;
48-
int flags;
49-
HkSateStatus status;
46+
HkStack(HkValue) vstk;
47+
int flags;
48+
HkSateStatus status;
5049
} HkVM;
5150

52-
void hk_vm_init(HkVM *vm, int minCapacity);
51+
void hk_vm_init(HkVM *vm, int size);
5352
void hk_vm_deinit(HkVM *vm);
5453
void hk_vm_runtime_error(HkVM *vm, const char *fmt, ...);
5554
void hk_vm_check_argument_type(HkVM *vm, HkValue *args, int index, HkType type);

src/array.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
// located in the root directory of this project.
99
//
1010

11-
#include <hook/array.h>
12-
#include <hook/memory.h>
13-
#include <hook/utils.h>
11+
#include "hook/array.h"
12+
#include "hook/memory.h"
13+
#include "hook/utils.h"
1414

1515
typedef struct
1616
{

src/builtin.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
#include <limits.h>
1515
#include <stdlib.h>
1616
#include <string.h>
17-
#include <hook/iterable.h>
18-
#include <hook/struct.h>
19-
#include <hook/utils.h>
17+
#include "hook/iterable.h"
18+
#include "hook/struct.h"
19+
#include "hook/utils.h"
2020

2121
#ifdef _WIN32
2222
#include <Windows.h>

src/builtin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#ifndef BUILTIN_H
1212
#define BUILTIN_H
1313

14-
#include <hook/vm.h>
14+
#include "hook/vm.h"
1515

1616
void load_globals(HkVM *vm);
1717
int num_globals(void);

src/callable.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
// located in the root directory of this project.
99
//
1010

11-
#include <hook/callable.h>
12-
#include <hook/memory.h>
13-
#include <hook/utils.h>
11+
#include "hook/callable.h"
12+
#include "hook/memory.h"
13+
#include "hook/utils.h"
1414

1515
#define MIN_CAPACITY (1 << 3)
1616

src/chunk.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
// located in the root directory of this project.
99
//
1010

11-
#include <hook/chunk.h>
12-
#include <hook/memory.h>
13-
#include <hook/utils.h>
11+
#include "hook/chunk.h"
12+
#include "hook/memory.h"
13+
#include "hook/utils.h"
1414

1515
#define MIN_CAPACITY (1 << 3)
1616

0 commit comments

Comments
 (0)