forked from atomvm/AtomVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemscripten_sys.h
More file actions
142 lines (122 loc) · 3.6 KB
/
Copy pathemscripten_sys.h
File metadata and controls
142 lines (122 loc) · 3.6 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* This file is part of AtomVM.
*
* Copyright 2023 by Paul Guyot <pguyot@kallisys.net>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
*/
#ifndef _EMSCRIPTEN_SYS_H_
#define _EMSCRIPTEN_SYS_H_
#include <pthread.h>
#include <time.h>
#include <erl_nif.h>
#include <list.h>
#include <sys.h>
#include <term_typedef.h>
#include <emscripten.h>
#include <emscripten/fetch.h>
#include <emscripten/promise.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/entropy.h>
#if defined(MBEDTLS_VERSION_NUMBER) && (MBEDTLS_VERSION_NUMBER >= 0x03000000)
#include <mbedtls/build_info.h>
#else
#include <mbedtls/config.h>
#endif
#include "sys_mbedtls.h"
struct PromiseResource
{
em_promise_t promise;
bool resolved;
};
struct HTMLEventUserDataResource
{
int32_t target_pid;
ErlNifMonitor monitor;
bool prevent_default;
bool unregistered;
int event;
term user_data;
const char *target_element;
char *target_element_str; // storage or NULL
term storage[];
};
enum EmscriptenMessageType
{
Cast,
Call,
HTMLEvent,
UnregisterHTMLEvent,
Signal
};
struct EmscriptenMessageBase
{
struct ListHead message_head;
enum EmscriptenMessageType message_type;
};
struct EmscriptenMessageCast
{
struct EmscriptenMessageBase base;
char *target_name;
char *message;
};
struct EmscriptenMessageCall
{
struct EmscriptenMessageBase base;
char *target_name;
char *message;
struct PromiseResource *promise_rsrc;
};
struct EmscriptenMessageHTMLEvent
{
struct EmscriptenMessageBase base;
int32_t target_pid;
term message;
term user_data;
HeapFragment *message_heap;
};
struct EmscriptenMessageUnregisterHTMLEvent
{
struct EmscriptenMessageBase base;
struct HTMLEventUserDataResource *rsrc;
};
struct EmscriptenPlatformData
{
pthread_mutex_t poll_mutex;
pthread_cond_t poll_cond;
struct ListHead messages;
atomic_size_t next_tracked_object_key;
ErlNifResourceType *promise_resource_type;
ErlNifResourceType *htmlevent_user_data_resource_type;
ErlNifResourceType *websocket_resource_type;
#ifndef AVM_NO_SMP
Mutex *entropy_mutex;
#endif
mbedtls_entropy_context entropy_ctx;
bool entropy_is_initialized;
#ifndef AVM_NO_SMP
Mutex *random_mutex;
#endif
mbedtls_ctr_drbg_context random_ctx;
bool random_is_initialized;
};
void sys_enqueue_emscripten_cast_message(GlobalContext *glb, const char *target, const char *message);
em_promise_t sys_enqueue_emscripten_call_message(GlobalContext *glb, const char *target, const char *message);
void sys_enqueue_emscripten_htmlevent_message(GlobalContext *glb, int32_t target_pid, term message, term user_data, HeapFragment *heap);
void sys_enqueue_emscripten_unregister_htmlevent_message(GlobalContext *glb, struct HTMLEventUserDataResource *rsrc);
size_t sys_get_next_tracked_object_key(GlobalContext *glb);
void sys_promise_resolve_int_and_destroy(em_promise_t promise, em_promise_result_t result, int value);
void sys_promise_resolve_str_and_destroy(em_promise_t promise, em_promise_result_t result, int value);
#endif