summaryrefslogtreecommitdiff
path: root/docs/application/namespace.md
blob: 38752ce38970c09698da4b3f27be795b9d2c6123 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Namespace

---

## Object Definitions

```c
typedef struct _kpl_export {
    KPL_SLAB_TREE_HEADER(struct _kpl_export);
    kpl_type_ptr type;
} kpl_export;

typedef struct _kpl_namespace_native {
    KPL_SLAB_TREE_HEADER(struct _kpl_namespace_native);
    const char *name;
    kpl_export *export_tree;
} kpl_namespace_native;

static kpl_namespace_native namespace_native_tree;

typedef enum : uint8_t {
    // ...
} namespace_lifecycle;

typedef struct _kpl_namespace_module {
    KPL_SLAB_TREE_HEADER(struct _kpl_namespace_module);
    kpl_type_ptr ast;
    kpl_buffer *module_string, *module_name;
    kpl_task *_Atomic task;
    kpl_export *export_tree;
    kpl_mutex export_mutex;
} kpl_namespace_module;

static kpl_namespace_module *namespace_module_tree;

kpl_mutex namespace_modul_tree_mutex;

static kpl_namespace_module namespace_string;
```

Each `NAMESPACE_IDENTIFIER` maps to `kpl_namespace_native*` or `kpl_namespace_module*` depending on flags

## Lookup and Storage

The native and namespace objects are stored as a tree

The exports are stored under a tree for both the native and namespace objects

# Registering

## Native

TODO C code for exposing native code

## Module/String/REPL

### Main

Begin life cycle and [Register](../lifecycle/register.md)

### Scan ``import`

Add a [Register](../lifecycle/register.md) task to `namespace_module_mutex`

# Updating

## Native

### Native code should not be updated at runtime, doing so is undefined behavior

## Module

### ``export`

Add exports directly to the namespaces `export_tree`, thread safe

## String/REPL

### Cannot export for String/REPL

# Using

## Native

### ``use`

Since the native modules are loaded statically the `native_module_tree` becomes read only at runtime, access is thread safe

## Module/String/REPL

### Import ``import`

1. Add find namespace task to the `namespace_module_mutex`, the namespace not existing is an error
2. Add a get export task to the namespaces `export_mutex`
3. The `export_mutex` will not start processing it's queue until the `task` field is `NULL`
4. The importer will get a copy of the read only `export_tree`