-
Notifications
You must be signed in to change notification settings - Fork 73
/
extension.h
1644 lines (1473 loc) · 49.7 KB
/
extension.h
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef SOCKET_RUNTIME_EXTENSION_H
#define SOCKET_RUNTIME_EXTENSION_H
#if defined(SOCKET_RUNTIME_EXTENSION_WASM)
#include "webassembly.h"
#else
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <uv.h>
#include "platform.h"
#endif
#if defined(__cplusplus)
#define SOCKET_RUNTIME_EXTENSION_EXTERN_BEGIN extern "C" {
#define SOCKET_RUNTIME_EXTENSION_EXTERN_END };
#else
#define SOCKET_RUNTIME_EXTENSION_EXTERN_BEGIN
#define SOCKET_RUNTIME_EXTENSION_EXTERN_END
#endif
#if defined(SOCKET_RUNTIME_EXTENSION_WASM)
# define SOCKET_RUNTIME_EXTENSION_EXPORT extern
#elif SOCKET_RUNTIME_PLATFORM_WINDOWS
# define SOCKET_RUNTIME_EXTENSION_EXPORT __declspec(dllexport)
#elif SOCKET_RUNTIME_PLATFORM_LINUX
# define SOCKET_RUNTIME_EXTENSION_EXPORT __attribute__((visibility("default")))
#elif SOCKET_RUNTIME_PLATFORM_MACOS || SOCKET_RUNTIME_PLATFORM_IOS
# define SOCKET_RUNTIME_EXTENSION_EXPORT __attribute__((visibility("default")))
#else
# define SOCKET_RUNTIME_EXTENSION_EXPORT
#endif
/**
* Major version of the extension ABI.
*/
#define SOCKET_RUNTIME_EXTENSION_ABI_VERSION_MAJOR (unsigned int) 0
/**
* Minor version of the extension ABI.
*/
#define SOCKET_RUNTIME_EXTENSION_ABI_VERSION_MINOR (unsigned int) 0
/**
* Minor version of the extension ABI.
*/
#define SOCKET_RUNTIME_EXTENSION_ABI_VERSION_PATCH (unsigned int) 1
/**
* The packed version of the extension ABI useful for semantic
* comparison purposes.
*/
#define SOCKET_RUNTIME_EXTENSION_ABI_VERSION ((int) ( \
SOCKET_RUNTIME_EXTENSION_ABI_VERSION_MAJOR << 16 | \
SOCKET_RUNTIME_EXTENSION_ABI_VERSION_MINOR << 8 | \
SOCKET_RUNTIME_EXTENSION_ABI_VERSION_PATCH << 0 \
))
/**
* Macro for registering an extension by name with a required initializer.
* @param name - *required* The name of the extension to register
* @param initializer - *required* A function that is called to initialize
* the extension in the runtime
* @param deinitializer - (optional) A function that is called to deinitialize
* the extension in the runtime.
* @param description - (optional) A human readable description of the
* registered extension
* @param version - (optional) A human readable version of the
* registered extension
* @see sapi_extension_registration_initializer
*/
#define SOCKET_RUNTIME_REGISTER_EXTENSION(_name, _initializer, ...) \
SOCKET_RUNTIME_EXTENSION_EXTERN_BEGIN \
static sapi_extension_registration_t __sapi_extension__ = { \
SOCKET_RUNTIME_EXTENSION_ABI_VERSION, \
_name, _initializer, ##__VA_ARGS__ \
}; \
\
SOCKET_RUNTIME_EXTENSION_EXPORT \
unsigned long __sapi_extension_abi () { \
return __sapi_extension__.abi; \
} \
\
SOCKET_RUNTIME_EXTENSION_EXPORT \
const char* __sapi_extension_name () { \
return __sapi_extension__.name; \
} \
\
SOCKET_RUNTIME_EXTENSION_EXPORT \
const char* __sapi_extension_description () { \
return __sapi_extension__.description; \
} \
\
SOCKET_RUNTIME_EXTENSION_EXPORT \
const char* __sapi_extension_version () { \
return __sapi_extension__.version; \
} \
\
SOCKET_RUNTIME_EXTENSION_EXPORT \
const sapi_extension_registration_initializer_t \
__sapi_extension_initializer () { \
return __sapi_extension__.initializer; \
} \
\
SOCKET_RUNTIME_EXTENSION_EXPORT \
const sapi_extension_registration_deinitializer_t \
__sapi_extension_deinitializer () { \
return __sapi_extension__.deinitializer; \
} \
\
SOCKET_RUNTIME_EXTENSION_EXPORT \
const sapi_extension_registration_t* __sapi_extension_init () { \
return &__sapi_extension__; \
} \
SOCKET_RUNTIME_EXTENSION_EXTERN_END \
SOCKET_RUNTIME_EXTENSION_EXTERN_BEGIN
typedef struct sapi_extension_registration sapi_extension_registration_t;
/**
* Internal APIs
*/
/**
* Internal ABI version getter for a registered extension.
* @ignore
* @private
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
unsigned long __sapi_extension_abi ();
/**
* Internal name getter for a registered extension.
* @ignore
* @private
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const char* __sapi_extension_name ();
/**
* Internal description getter for a registered extension.
* @ignore
* @private
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const char* __sapi_extension_description ();
/**
* Internal version getter for a registered extension.
* @ignore
* @private
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const char* __sapi_extension_version ();
/**
* Internal initializer for a registered extension.
* @ignore
* @private
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const sapi_extension_registration_t* __sapi_extension_init ();
/**
* Context API
* The _Context API_ is a general API for interacting with the
* currently available to an extension. Most higher level functions will need
* a context (`sapi_context_t`).
*/
/**
* An opaque pointer for an extension context. A context is provided to initializers,
* deinitializers, and IPC route requests.
*/
typedef struct sapi_context sapi_context_t;
/**
* A callback to be called at a later time given to the
* `sapi_context_dispatch()` function.
* @param context
* @param data
*/
typedef void (*sapi_context_dispatch_callback)(
sapi_context_t* context,
const void* data
);
/**
* Creates a new context. The context is retained if a `parent` is not given
* and therefor emust be disposed with `sapi_context_release()`.
* @param parent - An optional parent to own the new context
* @param retained - `true` if the returned context should be retained (owned) by the caller
* @return The new context (`sapi_context_t*`)
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
sapi_context_t* sapi_context_create (
sapi_context_t* parent,
bool retained
);
/**
* Set user data on a context.
* @param context - An extension context
* @param data - User data
* @return `true` if the user data was set, `false` otherwise
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
bool sapi_context_set_data (
sapi_context_t* context,
const void* data
);
/**
* Queues `callback` to be called sometime in the future for a `context`.
* @param context - An extension context
* @param data - User data to be given to `callback` when called
* @param callback - The callback to dispatch
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
bool sapi_context_dispatch (
sapi_context_t* context,
const void* data,
sapi_context_dispatch_callback callback
);
/**
* Retain a context preventing any allocated memory from being deallocated
* when the context is considered no longer valid. This function SHOULD NOT
* be called from an extension initializer. The `sapi_context_release()`
* function SHOULD be called at a later time to release any allocated memory
* associated with the context.
* @param context - An extension context
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
void sapi_context_retain (sapi_context_t* context);
/**
* Releases a context, freeing any allocated memory associated with the
* context. This function SHOULD NOT be called from an extension initializer.
* This function is typically called when a prior `sapi_context_retain()`
* function was called for a given context.
* @param context - An extension context
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
void sapi_context_release (sapi_context_t* context);
/**
* If a context is retained, then this function returns `true`, otherwise
* `false` is returned.
* @param context - An extension context
* @return `true` if retained, otherwise `false`.
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
bool sapi_context_retained (const sapi_context_t* context);
/**
* Gets the libuv event loop associated with the context.
* @param context - An extension context
* @return The libuv event loop (`uv_loop_t`).
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
uv_loop_t* sapi_context_get_loop (
const sapi_context_t* context
);
/**
* Get the IPC bridge router for this context.
* @param context - An extension context
* @return A IPC bridge router (`sapi_ipc_router_t*`)
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const struct sapi_ipc_router* sapi_context_get_router (
const sapi_context_t* context
);
/**
* Get user data from context.
* @param context - An extension context
* @return An opaque pointer to user data
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const void * sapi_context_get_data (
const sapi_context_t* context
);
/**
* Get parent context for this context
* @param context - An extension context
* @return A pointer to the parent context. This value may be `NULL`.
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const sapi_context_t* sapi_context_get_parent (
const sapi_context_t* context
);
/**
* Set a context error code.
* @param context - An extension context
* @param code - The code of the error
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
void sapi_context_error_set_code (
sapi_context_t* context,
int code
);
/**
* Get a context error code.
* @param context - An extension context
* @return The code of the error
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
int sapi_context_error_get_code (const sapi_context_t* context);
/**
* Set a context error name.
* @param context - An extension context
* @param name - The name of the error
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
void sapi_context_error_set_name (
sapi_context_t* context,
const char* name
);
/**
* Get a context error name.
* @param context - An extension context
* @return The name of the error
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const char* sapi_context_error_get_name (const sapi_context_t* context);
/**
* Set a context error message.
* @param context - An extension context
* @param message - The message of the error
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
void sapi_context_error_set_message (
sapi_context_t* context,
const char* message
);
/**
* Get a context error message.
* @param context - An extension context
* @return The message of the error
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const char* sapi_context_error_get_message (const sapi_context_t* context);
/**
* Set a context error location.
* @param context - An extension context
* @param location - The location of the error
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
void sapi_context_error_set_location (
sapi_context_t* context,
const char* location
);
/**
* Get a context error location.
* @param context - An extension context
* @return The location of the error
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const char* sapi_context_error_get_location (const sapi_context_t* context);
/**
* @param context - An extension context
* @return Allocated memmory for the context
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
void* sapi_context_alloc (sapi_context_t* context, unsigned int size);
/**
* JavaScript API
* The _JavaScript API_ is a general API for evaluating and interacting with the
* available JavaScript context in a WebView associated with a context.
*/
/**
* Evaluate named JavaScript in context.
* @param context - An extension context
* @param name - The name of the script to evaluate
* @param source - The source of the script to evaluate
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
void sapi_javascript_evaluate (
sapi_context_t* context,
const char* name,
const char* source
);
/**
* Get an environment variable
* @param context - An extension context
* @param name - The name of the environment variable
* @return The value of the environment which can be `NULL`
*/
const char* sapi_env_get (
sapi_context_t* context,
const char* name
);
/**
* JSON API
* The _JSON API_ is a general purpose JSON builder.
*/
/**
* JSON type enumeration.
*/
#define SAPI_JSON_TYPE_EMPTY -1
#define SAPI_JSON_TYPE_ANY 0
#define SAPI_JSON_TYPE_NULL 1
#define SAPI_JSON_TYPE_OBJECT 2
#define SAPI_JSON_TYPE_ARRAY 3
#define SAPI_JSON_TYPE_BOOLEAN 4
#define SAPI_JSON_TYPE_NUMBER 5
#define SAPI_JSON_TYPE_STRING 6
#define SAPI_JSON_TYPE_RAW 7
/**
* `true` if `value` is an empty JSON type.
* @param value - The value to test
*/
#define sapi_json_value_is_empty(value) \
(SAPI_JSON_TYPE_EMPTY == sapi_json_typeof((sapi_json_any_t*) (value)))
/**
* `true` if `value` is a null JSON type.
* @param value - The value to test
*/
#define sapi_json_value_is_null(value) \
(SAPI_JSON_TYPE_NULL == sapi_json_typeof((sapi_json_any_t*) (value)))
/**
* `true` if `value` is an object JSON type.
* @param value - The value to test
*/
#define sapi_json_value_is_object(value) \
(SAPI_JSON_TYPE_OBJECT == sapi_json_typeof((sapi_json_any_t*) (value)))
/**
* `true` if `value` is an array JSON type.
* @param value - The value to test
*/
#define sapi_json_value_is_array(value) \
(SAPI_JSON_TYPE_ARRAY == sapi_json_typeof((sapi_json_any_t*) (value)))
/**
* `true` if `value` is a boolean JSON type.
* @param value - The value to test
*/
#define sapi_json_value_is_boolean(value) \
(SAPI_JSON_TYPE_BOOLEAN == sapi_json_typeof((sapi_json_any_t*) (value)))
/**
* `true` if `value` is a number JSON type.
* @param value - The value to test
*/
#define sapi_json_value_is_number(value) \
(SAPI_JSON_TYPE_NUMBER == sapi_json_typeof((sapi_json_any_t*) (value)))
/**
* `true` if `value` is a string JSON type.
* @param value - The value to test
*/
#define sapi_json_value_is_string(value) \
(SAPI_JSON_TYPE_STRING == sapi_json_typeof((sapi_json_any_t*) (value)))
/**
* Set JSON `value` for JSON `object` at `key`. Generally, an alias to the
* `sapi_json_object_set_value` function.
* @param object - The object to set a value on
* @param key - The key of the value to set
* @param value - The JSON value to set
*/
#define sapi_json_object_set(object, key, value) \
sapi_json_object_set_value( \
(sapi_json_object_t*) (object), \
(const char*)(key), \
(sapi_json_any((value))) \
)
/**
* Set JSON `value` for JSON `array` at `index`. Generally, an alias to the
* `sapi_json_array_set` function.
* @param array - The array to set a value on
* @param index - The index of the value to set
* @param value - The JSON value to set
*/
#define sapi_json_array_set(array, index, value) \
sapi_json_array_set_value( \
(sapi_json_array_t*) (array), \
(unsigned int) (index), \
(sapi_json_any((value))) \
)
/**
* Push a JSON `value` to the end of a JSON `array`. Generally, an alias to
* the `sapi_json_array_push_value` function.
* @param array - The array to set a value on
* @param value - The JSON value to set
*/
#define sapi_json_array_push(array, value) \
sapi_json_array_push_value ( \
(sapi_json_array_t*) (array), \
sapi_json_any((value)) \
)
/**
* Convert JSON `value` to a string. Generally, an alias to the
* `sapi_json_string_create` function.
* @param value - The JSON value to convert to a string
* @return The JSON value as a string
*/
#define sapi_json_stringify(value) \
sapi_json_stringify_value(sapi_json_any(value))
/**
* An opaque JSON type that represents "any" JSON value
*/
typedef struct sapi_json_any sapi_json_any_t;
/**
* An opaque JSON type that represents a `null` JSON value.
*/
typedef struct sapi_json_null sapi_json_null_t;
/**
* An opaque JSON type that represents an "object" JSON value.
*/
typedef struct sapi_json_object sapi_json_object_t;
/**
* An opaque JSON type that represents an "array" JSON value.
*/
typedef struct sapi_json_array sapi_json_array_t;
/**
* An opaque JSON type that represents a `true` or `false` JSON value.
*/
typedef struct sapi_json_boolean sapi_json_boolean_t;
/**
* An opaque JSON type that represents a "number" JSON value.
*/
typedef struct sapi_json_number sapi_json_number_t;
/**
* An opaque JSON type that represents a "string" JSON value.
*/
typedef struct sapi_json_string sapi_json_string_t;
/**
* An opaque JSON type that represents to a "raw" JSON value.
*/
typedef struct sapi_json_raw sapi_json_raw_t;
/**
* A scalar type that represents the JSON type enumeration.
*/
typedef int sapi_json_type_t;
/**
* Casts `value` to a `sapi_json_any_t*`.
* @param value - The value to cast
*/
#define sapi_json_any(value) ((sapi_json_any_t*)(value))
/**
* Casts `value` to a `sapi_json_any_t*`.
* @param value - The value to cast
*/
#define sapi_json_bool(value) ((sapi_json_any_t*)(value))
/**
* Computes the `sapi_json_type_t` enumeration value of a given `sapi_json_type_t`
* type.
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const sapi_json_type_t sapi_json_typeof (const sapi_json_any_t*);
/**
* Creates new JSON object.
* @param context - A context associated with the extension
* @return The JSON object
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
sapi_json_object_t* sapi_json_object_create (sapi_context_t* context);
/**
* Creates new JSON array.
* @param context - A context associated with the extension
* @return The JSON array
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
sapi_json_array_t* sapi_json_array_create (sapi_context_t* context);
/**
* Creates new JSON string.
* @param context - A context associated with the extension
* @param string - The raw string
* @return The JSON string
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
sapi_json_string_t* sapi_json_string_create (
sapi_context_t* context,
const char* string
);
/**
* Creates new JSON boolean.
* @param context - A context associated with the extension
* @param boolean - The boolean
* @return The JSON boolean
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
sapi_json_boolean_t* sapi_json_boolean_create (
sapi_context_t* context,
bool boolean
);
/**
* Creates new JSON number.
* @param context - A context associated with the extension
* @param number - The number
* @return The JSON number
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
sapi_json_number_t* sapi_json_number_create (
sapi_context_t* context,
const double number
);
/**
* Creates raw JSON from a source string. The source string is NOT parsed.
* @param context - A context associated with the extension
* @param source - The raw JSON source string
* @return The JSON created from raw source.
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
sapi_json_any_t* sapi_json_raw_from (
sapi_context_t* context,
const char* source
);
/**
* Set JSON `value` for JSON `object` at `key`.
* @param object - The object to set a value on
* @param key - The key of the value to set
* @param value - The JSON value to set
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
void sapi_json_object_set_value (
sapi_json_object_t* object,
const char* key,
sapi_json_any_t* value
);
/**
* Get JSON `value` for JSON `object` at `key`.
* @param object - The object to set a value on
* @param key - The key of the value to set
* @return The JSON value to set
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
sapi_json_any_t* sapi_json_object_get (
const sapi_json_object_t* object,
const char* key
);
/**
* Set JSON `value` for JSON `array` at `index`.
* @param array - The array to set a value on
* @param index - The index of the value to set
* @param value - The JSON value to set
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
void sapi_json_array_set_value (
sapi_json_array_t* array,
unsigned int index,
sapi_json_any_t* value
);
/**
* Get JSON `value` for JSON `array` at `index`.
* @param array - The array to set a value on
* @param index - The index of the value to set
* @return The JSON value to set
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
sapi_json_any_t* sapi_json_array_get (
const sapi_json_array_t* array,
unsigned int index
);
/**
* Push a JSON `value` to the end of a JSON `array`.
* @param array - The array to set a value on
* @param value - The JSON value to set
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
void sapi_json_array_push_value (
sapi_json_array_t* array,
sapi_json_any_t* value
);
/**
* Pop and return the last JSON `value` in a JSON `array`
* @param array - The array to set a value on
* @return The popped JSON value.
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
sapi_json_any_t* sapi_json_array_pop (
sapi_json_array_t* json
);
/**
* Convert JSON `value` to a string.
* @param value - The JSON value to convert to a string
* @return The JSON value as a string
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const char * sapi_json_stringify_value (const sapi_json_any_t*);
/**
* Miscellaneous API
*/
/**
* Called when initializing an extension.
* @param context - An extension context
* @param data - Optional user data
* @return `true` if initialization was successful, otherwise `false`
*/
typedef bool (*sapi_extension_registration_initializer_t)(
sapi_context_t* context,
const void* data
);
/**
* Called when deinitializing an extension.
* @param context - An extension context
* @param data - Optional user data
* @return `true` if deinitialization was successful, otherwise `false`
*/
typedef bool (*sapi_extension_registration_deinitializer_t)(
sapi_context_t* context,
const void* data
);
/**
* A container for an extension registration.
*/
struct sapi_extension_registration {
unsigned long abi:32;
// required
const char* name;
const sapi_extension_registration_initializer_t initializer;
// optional
const sapi_extension_registration_deinitializer_t deinitializer;
const char* description;
const char* version;
// reserved for future ABI changes
char __reserved__[1024];
} __attribute__((packed));
/**
* Register a new extension. There is typically no need to call this directly.
* @param registration - The registration info for an extension
* @return `true` if successful, otherwise `false`
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
bool sapi_extension_register (
const sapi_extension_registration_t* registration
);
/**
* Predicate to determine if a policy is allowed in an extension context.
* Multiple policy names can be given as a list seperated by `,`.
* ie: "ipc,context,process"
* @param context - An extension context
* @param allowed - An extension context policy name
* @return `true` if `allowed` is allowed in `context`.
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
bool sapi_extension_is_allowed (sapi_context_t* context, const char *allowed);
/**
* Print `message` to the stdout log.
* @param context - An extension context
* @param message - A message to write to stdout
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
void sapi_log (const sapi_context_t* ctx, const char *message);
/**
* Print `message` to the stderr log.
* @param context - An extension context
* @param message - A message to write to stderr
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
void sapi_debug (const sapi_context_t* ctx, const char *message);
/**
* A simple printf macro for extension implementers with a total max output
* buffer size of `BUFSIZ` (@see `stdio.h`, `string.h`)
* @param format - Format string for formatted output
* @param ... - `format` argument values
*/
#define sapi_printf(ctx, format, ...) ({ \
char _buffer[BUFSIZ] = {0}; \
snprintf(_buffer, BUFSIZ, format, ##__VA_ARGS__); \
sapi_log(ctx, _buffer); \
})
/**
* Computes a random `uint64_t` value.
* @return The random `uint64_t` value
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
uint64_t sapi_rand64 ();
/**
* IPC API
* The _IPC API_ provides an interface for _interprocess communication_
* between the native runtime extension and the WebView. The API allows for
* creating and responding to IPC router routes.
*/
/**
* An opaque pointer for an IPC message. Use `sapi_ipc_message_*` functions
* for interacting with this type.
*/
typedef struct sapi_ipc_message sapi_ipc_message_t;
/**
* An opaque pointer for an IPC bridge.
*/
typedef struct sapi_ipc_bridge sapi_ipc_bridge_t;
/**
* An opaque pointer for an IPC result. Use `sapi_ipc_result_*` functions
* for interacting with this type.
*/
typedef struct sapi_ipc_result sapi_ipc_result_t;
/**
* An opaque pointer for an IPC router. Use `sapi_ipc_router_*` functions
* for interacting with this type.
*/
typedef struct sapi_ipc_router sapi_ipc_router_t;
/**
* A callback called when an IPC route receives a request.
* @param context - The extension context for this request
* @param message - The IPC message for this request
* @param router - The IPC router associated with the extension context
*/
typedef void (*sapi_ipc_router_message_callback_t)(
sapi_context_t* context,
sapi_ipc_message_t* message,
const sapi_ipc_router_t* router
);
/**
* A callback for `sapi_ipc_invoke()` called when an IPC has been invoked.
* @param result - The result of the invocation
* @param router - The IPC router associated with the extension context
*/
typedef void (*sapi_ipc_router_result_callback_t)(
const sapi_ipc_result_t* result,
const sapi_ipc_router_t* router
);
/**
* Get the window index the IPC message is associated with.
* @param message The IPC message
* @return The window index the IPC message was initiated from or intended for
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
int sapi_ipc_message_get_index (
const sapi_ipc_message_t* message
);
/**
* Get the value associated with the IPC message.
* @param message The IPC message
* @return The IPC message value (possibly `NULL`)
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const char* sapi_ipc_message_get_value (
const sapi_ipc_message_t* message
);
/**
* Get the buffer bytes associated with the IPC message.
* @param message The IPC message
* @return The IPC message va (possibly `NULL`)
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const unsigned char* sapi_ipc_message_get_bytes (
const sapi_ipc_message_t* message
);
/**
* Get the buffer bytes size associated with the IPC message.
* @param message The IPC message
* @return The IPC message va (possibly `NULL`)
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
unsigned int sapi_ipc_message_get_bytes_size (
const sapi_ipc_message_t* message
);
/**
* Get the IPC message name.
* @param message The IPC message
* @return The IPC message name (possibly `NULL`)
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const char* sapi_ipc_message_get_name (
const sapi_ipc_message_t* message
);
/**
* Get the sequence of the IPC message. If
* @param message The IPC message
* @return The IPC message sequence value (possibly `NULL`, "", "-1", or "...")
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const char* sapi_ipc_message_get_seq (
const sapi_ipc_message_t* message
);
/**
* Get the URI of the IPC message.
* @param message The IPC message
* @return The IPC message URI value (possibly `NULL`)
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const char* sapi_ipc_message_get_uri (
const sapi_ipc_message_t* message
);
/**
* Get an arbitrary IPC message parameter value.
* @param message The IPC message
* @return The IPC message parameter value (possibly `NULL`)
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const char* sapi_ipc_message_get (
const sapi_ipc_message_t* message,
const char* key
);
/**
* Create a new IPC result from a given `context` and IPC `message`.
* @param context - An extension context for a IPC request
* @param message - The IPC message for this request
* @return The new IPC result
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
sapi_ipc_result_t* sapi_ipc_result_create (
sapi_context_t* context,
sapi_ipc_message_t *message
);
/**
* Set IPC result sequence value.
* @param result - An IPC request result
* @param seq - An IPC sequence value
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
void sapi_ipc_result_set_seq (
sapi_ipc_result_t* result,
const char* seq
);
/**
* Get IPC result sequence value.
* @param result - An IPC request result
* @return The result sequence value (possibly `NULL`, "", "-1", or "...")
*/
SOCKET_RUNTIME_EXTENSION_EXPORT
const char* sapi_ipc_result_get_seq (
const sapi_ipc_result_t* result
);