Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
namespaces.c
Go to the documentation of this file.
1/* -*- C -*-
2 * Serene programming language
3 * Copyright (C) 2019-2026 Sameer Rahmani <[email protected]>
4 *
5 * This library is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library. If not, see <https://www.gnu.org/licenses/>.
17 */
19
20#include "serene/rt/core.h"
21#include "serene/rt/engine.h"
22#include "serene/rt/errors.h"
24#include "serene/rt/strings.h"
25#include "serene/rt/symbols.h"
26#include "serene/utils.h"
27
29 PANIC_IF_NULL(ctx);
30 PANIC_IF_NULL(name);
31
32 // Empty string is not a valid namespace name
33 if (srn_string_is_empty(name)) {
34 return srn_errors_make_error(ctx, metadata, EMPTY_NAMESPACE_NAME, nullptr);
35 };
36
38 return srn_errors_make_error(
39 ctx, metadata, STRING_LENGTH_LIMIT_EXCEEDED, "Namespace name exceeds the length limit"
40 );
41 }
42
44 ns->name = srn_string_copy(ctx, name);
45
48 // The symbol table outlives the creating context, so it is pinned to the
49 // namespace's own root context; the table's keys and trie nodes land in
50 // that chain rather than in the creating caller's block.
52 ns->src_buffer = nullptr;
53
56
57 return srn_value_make(ctx, VNamespace, metadata, (void *)ns);
58}
59
61 PANIC_IF_NULL(ns);
62 return nullptr;
63}
srn_context_t * srn_context_make(srn_engine_t *engine)
Make an empty context, by allocating a new memory block.
Definition context.c:39
#define ALLOC(ctx, T)
Definition context.h:84
srn_value_t * srn_value_make(srn_context_t *ctx, srn_value_tag_t tag, srn_metadata_t *metadata, void *payload)
Creates a new serene value.
Definition core.c:36
@ VNamespace
Definition core.h:122
srn_object_id_t srn_allocate_object_id(srn_engine_t *engine)
Definition engine.c:172
Error handling for the runtime.
@ EMPTY_NAMESPACE_NAME
Definition errors.h:81
@ STRING_LENGTH_LIMIT_EXCEEDED
Definition errors.h:83
hmap_t hmap_empty(const srn_context_t *ctx)
Create, initialize and return a new hashmap pinned to ctx.
Definition hashmap.c:655
This is an implementation of Compressed Hash-Array Mapped Prefix-tree, which is a bit-partitioned,...
srn_namespace_t * srn_namespace_load(srn_namespace_t *ns)
Load the namespace's source from the filesystem into src_buffer, parse it, and populate the symbol ta...
Definition namespaces.c:60
srn_value_t * srn_namespace_make(srn_context_t *ctx, srn_metadata_t *metadata, srn_string_t *name)
Creates a new namespace in the give context.
Definition namespaces.c:28
size_t srn_string_length(const srn_string_t *s)
Definition strings.c:34
bool srn_string_is_empty(const srn_string_t *s)
Definition strings.c:49
srn_string_t * srn_string_copy(srn_context_t *ctx, const srn_string_t *src)
Allocate a copy of src through ctx and return it.
Definition strings.c:39
srn_limits_config_t limits
srn_engine_t * engine
Long term state of the compiler.
Definition context.h:49
srn_configuration_t config
The runtime's tunable knobs, the single source for every configurable value (see configuration....
Definition engine.h:62
size_t ns_name_max_len
Longest namespace name accepted, in bytes.
srn_string_t * name
Definition namespaces.h:45
srn_context_t * root_context
Definition namespaces.h:48
srn_spinlock_t src_buffer_lock
Guards every access to source buffer.
Definition namespaces.h:62
srn_namespace_src_buffer_t * src_buffer
The buffer holding the source code for this namespace.
Definition namespaces.h:59
hmap_t sym_table
A mapping symbol names to (srn_value_t with type symbol).
Definition namespaces.h:53
srn_spinlock_t sym_table_lock
Definition namespaces.h:54
srn_object_id_t id
Definition namespaces.h:46
#define PANIC_IF_NULL(ptr)
Definition utils.h:66
static void srn_spinlock_init(srn_spinlock_t *lock)
Definition utils.h:280