1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Johan Kayser <kayser.johan@gmail.com>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Provides PNaCl support for Nit.
19 # To use this module and compile for PNaCl, you must install the
20 # NaCl SDK (This file is based on Pepper 33).
21 # If NACL_SDK_ROOT is not set in your PATH, you have to work in
22 # 'nacl_sdk/pepper_your_pepper_version/getting_started/your_project_folder'.
23 module pnacl
is platform
26 intrude import core
::stream
29 #include "ppapi/c/pp_errors.h"
30 #include "ppapi/c/ppp.h"
31 #include "ppapi/c/ppp_instance.h"
32 #include "ppapi/c/pp_bool.h"
33 #include "ppapi/c/ppb_var.h"
34 #include "ppapi/c/ppb_messaging.h"
35 #include "ppapi/c/ppp_messaging.h"
36 #include "ppapi/c/ppb_var_dictionary.h"
37 #include "ppapi/c/ppb_var_array.h"
49 #define MAX_DICTIONARY_QUEUE_SIZE 200
50 #define MAX_MESSAGE_QUEUE_SIZE 10
52 extern int nit_main(int, char**);
54 /* A working thread for Nit. */
55 static pthread_t g_nit_thread;
57 /* Mutex that guards the queues. */
58 static pthread_mutex_t g_dictionary_queue_mutex;
59 static pthread_mutex_t g_message_queue_mutex;
61 /* Condition variables that are signalled when the queues are not empty. */
62 static pthread_cond_t g_dictionary_queue_not_empty_cond;
63 static pthread_cond_t g_message_queue_not_empty_cond;
65 /** Circular queues of dictionaries and messages from JavaScript to be handled.
67 * If g_queue_start < g_queue_end:
68 * all elements in the range [g_queue_start, g_queue_end) are valid.
69 * If g_queue_start > g_queue_end:
70 * all elements in the ranges [0, g_queue_end) and
71 * [g_queue_start, MAX_QUEUE_SIZE) are valid.
72 * If g_queue_start == g_queue_end, and g_queue_size > 0:
73 * all elements in the g_queue are valid.
74 * If g_queue_start == g_queue_end, and g_queue_size == 0:
75 * No elements are valid. */
76 static struct PP_Var g_dictionary_queue[MAX_DICTIONARY_QUEUE_SIZE];
77 static char* g_message_queue[MAX_MESSAGE_QUEUE_SIZE];
79 /* The index of the head of the queues. */
80 static int g_dictionary_queue_start = 0;
81 static int g_message_queue_start = 0;
83 /* The index of the tail of the queues, non-inclusive. */
84 static int g_dictionary_queue_end = 0;
85 static int g_message_queue_end = 0;
87 /* The size of the queues. */
88 static int g_dictionary_queue_size = 0;
89 static int g_message_queue_size = 0;
91 /* PNaCl interfaces. */
92 const PPB_Messaging* g_varMessagingInterface;
93 const PPB_Var* g_varInterface;
94 const PPB_VarDictionary* g_varDictionaryInterface;
95 const PPB_VarArray* g_varArrayInterface;
97 PP_Instance g_instance;
100 /* A wrapper to launch the Nit main on a new thread. */
101 void* WrapperNitMain(void* arg) {
106 /** Return whether the queues are empty.
108 * NOTE: this function assumes g_queue_mutex lock is held.
109 * @return non-zero if the queue is empty. */
110 static int IsDictionaryQueueEmpty() { return g_dictionary_queue_size == 0; }
111 static int IsMessageQueueEmpty() { return g_message_queue_size == 0; }
113 /** Return whether the queues are full.
115 * NOTE: this function assumes g_queue_mutex lock is held.
116 * @return non-zero if the queue is full. */
117 static int IsDictionaryQueueFull() { return g_dictionary_queue_size == MAX_DICTIONARY_QUEUE_SIZE; }
118 static int IsMessageQueueFull() { return g_message_queue_size == MAX_MESSAGE_QUEUE_SIZE; }
120 /* Initialize the queues. */
121 void InitializeQueues() {
122 pthread_mutex_init(&g_dictionary_queue_mutex, NULL);
123 pthread_cond_init(&g_dictionary_queue_not_empty_cond, NULL);
124 pthread_mutex_init(&g_message_queue_mutex, NULL);
125 pthread_cond_init(&g_message_queue_not_empty_cond, NULL);
128 /** Enqueue a dictionary (i.e. add to the end)
130 * If the queue is full, the dictionary will be dropped.
132 * NOTE: this function assumes g_dictionary_queue_mutex is _NOT_ held.
133 * @param[in] dictionary, the dictionary to enqueue.
134 * @return non-zero if the dictionary was added to the queue. */
135 int EnqueueDictionary(struct PP_Var dictionary) {
136 pthread_mutex_lock(&g_dictionary_queue_mutex);
138 /* We shouldn't block the main thread waiting for the queue to not be full,
139 * so just drop the dictionary. */
140 if (IsDictionaryQueueFull()) {
141 pthread_mutex_unlock(&g_dictionary_queue_mutex);
145 g_dictionary_queue[g_dictionary_queue_end] = dictionary;
146 g_dictionary_queue_end = (g_dictionary_queue_end + 1) % MAX_DICTIONARY_QUEUE_SIZE;
147 g_dictionary_queue_size++;
149 pthread_cond_signal(&g_dictionary_queue_not_empty_cond);
151 pthread_mutex_unlock(&g_dictionary_queue_mutex);
156 /** Enqueue a message (i.e. add to the end)
158 * If the queue is full, the message will be dropped.
160 * NOTE: this function assumes g_message_queue_mutex is _NOT_ held.
161 * @param[in] message The message to enqueue.
162 * @return non-zero if the message was added to the queue. */
163 int EnqueueMessage(char* message) {
164 pthread_mutex_lock(&g_message_queue_mutex);
166 /* We shouldn't block the main thread waiting for the queue to not be full,
167 * so just drop the message. */
168 if (IsMessageQueueFull()) {
169 pthread_mutex_unlock(&g_message_queue_mutex);
173 g_message_queue[g_message_queue_end] = message;
174 g_message_queue_end = (g_message_queue_end + 1) % MAX_MESSAGE_QUEUE_SIZE;
175 g_message_queue_size++;
177 pthread_cond_signal(&g_message_queue_not_empty_cond);
179 pthread_mutex_unlock(&g_message_queue_mutex);
184 /** Dequeue a dictionary and return it.
186 * This function blocks until a dictionary is available. It should not be called
187 * on the main thread.
189 * NOTE: this function assumes g_dictionary_queue_mutex is _NOT_ held.
190 * @return The dictionary at the head of the queue. */
191 struct PP_Var DequeueDictionary() {
192 struct PP_Var dictionary = g_varDictionaryInterface->Create();
194 pthread_mutex_lock(&g_dictionary_queue_mutex);
196 while (IsDictionaryQueueEmpty()) {
197 pthread_cond_wait(&g_dictionary_queue_not_empty_cond, &g_dictionary_queue_mutex);
200 dictionary = g_dictionary_queue[g_dictionary_queue_start];
201 g_dictionary_queue_start = (g_dictionary_queue_start + 1) % MAX_DICTIONARY_QUEUE_SIZE;
202 g_dictionary_queue_size--;
204 pthread_mutex_unlock(&g_dictionary_queue_mutex);
209 /** Dequeue a message and return it.
211 * This function blocks until a message is available. It should not be called
212 * on the main thread.
214 * NOTE: this function assumes g_queue_mutex is _NOT_ held.
215 * @return The message at the head of the queue. */
216 char* DequeueMessage() {
217 char* message = NULL;
219 pthread_mutex_lock(&g_message_queue_mutex);
221 while (IsMessageQueueEmpty()) {
222 pthread_cond_wait(&g_message_queue_not_empty_cond, &g_message_queue_mutex);
225 message = g_message_queue[g_message_queue_start];
226 g_message_queue_start = (g_message_queue_start + 1) % MAX_MESSAGE_QUEUE_SIZE;
227 g_message_queue_size--;
229 pthread_mutex_unlock(&g_message_queue_mutex);
234 /* Posts a string message to JS. */
235 void PostMessage(char* message) {
236 /* Create PP_Var containing the message body. */
237 struct PP_Var varString = g_varInterface->VarFromUtf8(message, strlen(message));
239 /* Post message to the JavaScript layer. */
240 g_varMessagingInterface->PostMessage(g_instance, varString);
243 /* Posts a Dictionary (JS like object) to JS. */
244 void PostDictionary(struct PP_Var dictionary) {
245 g_varMessagingInterface->PostMessage(g_instance, dictionary);
248 /* Posts a Variable (aka PepperVar) to JS.
249 Should only be used for testing, conventional conversation is made
250 with Strings or Dictionaries. */
251 void PostVar(struct PP_Var v) {
252 g_varMessagingInterface->PostMessage(g_instance, v);
255 /* char* to PP_Var. */
256 static struct PP_Var CStrToVar(const char* str) {
257 if (g_varInterface != NULL) {
258 return g_varInterface->VarFromUtf8(str, strlen(str));
260 return PP_MakeUndefined();
263 static PP_Bool Instance_DidCreate(PP_Instance instance, uint32_t argc, const char* argn[], const char* argv[]) {
264 g_instance = instance;
266 /* Initialization of the queues and creation of the thread for Nit. */
268 pthread_create(&g_nit_thread, NULL, &WrapperNitMain, NULL);
273 static void Instance_DidDestroy(PP_Instance instance) {
277 static void Instance_DidChangeView(PP_Instance pp_instance, PP_Resource view) {
281 static void Instance_DidChangeFocus(PP_Instance pp_instance, PP_Bool has_focus) {
285 static PP_Bool Instance_HandleDocumentLoad(PP_Instance pp_instance, PP_Resource pp_url_loader) {
290 /* Called when JS sends something, is set to accept Strings or Dictionaries,
291 returns an error if received object is not a String or Dictionary. */
292 void Messaging_HandleMessage(PP_Instance instance, struct PP_Var varMessage) {
293 if(varMessage.type == PP_VARTYPE_DICTIONARY) {
294 if(!EnqueueDictionary(varMessage)) {
295 struct PP_Var errorMessage = CStrToVar("QueueFull : dropped dictionary because the queue was full.");
296 g_varMessagingInterface->PostMessage(g_instance, errorMessage);
299 else if(varMessage.type == PP_VARTYPE_STRING) {
301 char* message = (char*)g_varInterface->VarToUtf8(varMessage, &len);
302 if(!EnqueueMessage(message)) {
303 struct PP_Var errorMessage = CStrToVar("QueueFull : dropped message because the queue was full.");
304 g_varMessagingInterface->PostMessage(g_instance, errorMessage);
308 struct PP_Var errorMessage = CStrToVar("TypeError : only accepts JS objects or Strings");
309 g_varMessagingInterface->PostMessage(g_instance, errorMessage);
313 /* This function is called by Nit when using check_dictionary,
314 returns the dictionary at the head of the queue. */
315 void* NitHandleDictionary() {
316 struct PP_Var dictionary = DequeueDictionary();
317 PnaclApp_handle_dictionary(app, &dictionary);
321 /* This function is called By Nit when waiting for a user input. */
322 char* NitHandleMessage() {
323 return DequeueMessage();
327 PP_EXPORT int32_t PPP_InitializeModule(PP_Module module_id, PPB_GetInterface get_browser_interface) {
328 /* Initializing global pointers. */
329 g_varMessagingInterface = (const PPB_Messaging*) get_browser_interface(PPB_MESSAGING_INTERFACE);
330 g_varInterface = (const PPB_Var*) get_browser_interface(PPB_VAR_INTERFACE);
331 g_varDictionaryInterface = (const PPB_VarDictionary*) get_browser_interface(PPB_VAR_DICTIONARY_INTERFACE);
332 g_varArrayInterface = (const PPB_VarArray*) get_browser_interface(PPB_VAR_ARRAY_INTERFACE);
336 PP_EXPORT void PPP_ShutdownModule() {
340 PP_EXPORT const void* PPP_GetInterface(const char* interface_name) {
341 if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0)
343 static PPP_Instance instance_interface = {
345 &Instance_DidDestroy,
346 &Instance_DidChangeView,
347 &Instance_DidChangeFocus,
348 &Instance_HandleDocumentLoad
350 return &instance_interface;
352 else if (strcmp(interface_name, PPP_MESSAGING_INTERFACE) == 0) {
353 static PPP_Messaging messaging_interface = {
354 &Messaging_HandleMessage
356 return &messaging_interface;
361 /* Hack in order to avoid the problem with file. */
362 int poll(struct pollfd* fds, nfds_t nfds, int timeout) { return 0; }
365 # Nit class representing a Pepper C API PP_Var typed as a Dictionary.
366 extern class PepperDictionary `{ struct PP_Var* `}
369 struct
PP_Var* self = malloc
( sizeof
( struct
PP_Var ) );
370 *self = g_varDictionaryInterface-
>Create();
374 # Get fonction using PepperVars.
376 # Returns the value that is associated with 'key'.
377 # If 'key' is not a String typed PepperVar, or doesn't exist in the Dictionary, an undefined PepperVar is returned.
378 fun native_get(key: PepperVar): PepperVar `{
379 struct
PP_Var* value
= malloc
( sizeof
( struct
PP_Var ) );
380 *value
= g_varDictionaryInterface-
>Get(*self, *key
);
384 # Returns the value associated with 'key'.
386 # 'key' must be a String.
387 # If 'key' is not a String or doesn't exist in the Dictionary, 'null' is returned.
388 fun [](key: nullable Pepperable): nullable Pepperable
390 var native_key = key.to_pepper
391 var native_value = native_get(native_key)
392 return native_value.to_nit
395 # Set function using PepperVars.
397 # Sets the value associated with the specified key.
398 # 'key' must be a String typed PepperVar.
399 # If 'key' hasn't existed in the Dictionary, it is added and associated with 'value'.
400 # Otherwise, the previous value is replaced with 'value'.
401 # Returns a Boolean indicating whether the operation succeeds.
402 fun native_set(key: PepperVar, value: PepperVar): Bool `{
404 b
= g_varDictionaryInterface-
>Set(*self, *key
, *value
);
408 # Sets the value associated with the specified key.
410 # 'key' must be a String.
411 # If 'key' hasn't existed in the Dictionary, it is added and associated with 'value'.
412 # Otherwise, the previous value is replaced with 'value'.
413 # Returns a Boolean indicating whether the operation succeeds.
414 fun []=(key: nullable Pepperable, value: nullable Pepperable): Bool
416 var native_key = key.to_pepper
417 var native_value = value.to_pepper
418 return native_set(native_key, native_value)
421 # Deletes the specified key and its associated value, if the key exists.
423 # Takes a String typed PepperVar.
424 fun native_delete(key: PepperVar) `{
425 g_varDictionaryInterface-
>Delete(*self, *key
);
428 # Deletes the specified key and its associated value, if the key exists.
431 fun delete(key: String)
433 var native_key = key.to_pepper
434 native_delete native_key
437 # Checks whether a key exists.
439 # Takes a String typed PepperVar.
440 fun native_has_key(key: PepperVar): Bool `{
442 b
= g_varDictionaryInterface-
>HasKey(*self, *key
);
446 # Checks whether a key exists.
449 fun has_key(key: String): Bool
451 var native_key = key.to_pepper
452 return native_has_key(native_key)
455 # Gets all the keys in a dictionary.
457 # Returns a PepperArray which contains all the keys of the Dictionary. The elements are string vars.
458 fun get_keys: PepperArray `{
459 struct
PP_Var* array
= malloc
( sizeof
( struct
PP_Var ) );
460 *array
= g_varDictionaryInterface-
>GetKeys(*self);
464 # Use this function to copy a dictionary.
465 fun copy: PepperDictionary `{
466 struct
PP_Var* varDictionary
= malloc
( sizeof
( struct
PP_Var ) );
467 *varDictionary
= g_varDictionaryInterface-
>Create();
468 *varDictionary
= *self;
469 return varDictionary
;
473 # Nit class representing a Pepper C API PP_Var typed as an Array.
474 extern class PepperArray `{ struct PP_Var* `}
477 struct PP_Var* self = malloc( sizeof( struct PP_Var ) );
478 *self = g_varArrayInterface->Create();
482 # Returns the element at the specified position as a PepperVar.
484 # If 'index' is larger than or equal to the array length, an undefined PepperVar is returned.
485 fun native_get
(index
: Int): PepperVar `{
486 struct PP_Var* value = malloc( sizeof( struct PP_Var ) );
487 *value = g_varArrayInterface->Get(*self, index);
491 # Returns the element at the specified position.
493 # If 'index' is larger than or equal to the array length, 'null' is returned.
494 fun [](index
: Int): nullable Pepperable
496 var native_value
= native_get
(index
)
497 return native_value
.to_nit
500 # Returns an int containing the length of the PepperArray.
502 int length = g_varArrayInterface->GetLength(*self);
506 # Takes a PepperVar for the 'value' param.
508 # Sets the value of an element in the array at indicated index.
509 # If 'index' is larger than or equal to the array length, the length is updated to be 'index' + 1.
510 # Any position in the array that hasn't been set before is set to undefined, i.e., PepperVar of C type PP_VARTYPE_UNDEFINED.
511 # Returns a Boolean indicating whether the operation succeeds.
512 fun native_set
(index
: Int, value
: PepperVar): Bool `{
514 b = g_varArrayInterface->Set(*self, index, *value);
518 # Sets the value of an element in the array at indicated index.
520 # If 'index' is larger than or equal to the array length, the length is updated to be 'index' + 1.
521 # Any position in the array that hasn't been set before is set to undefined, i.e., PepperVar of C type PP_VARTYPE_UNDEFINED.
522 # Returns a Boolean indicating whether the operation succeeds.
523 fun []=(index
: Int, value
: nullable Pepperable): Bool
525 var native_value
= value
.to_pepper
526 return native_set
(index
, native_value
)
529 # Sets the array length.
531 # If 'length' is smaller than its current value, the array is truncated to the new length.
532 # Any elements that no longer fit are removed and the references to them will be released.
533 # If 'length' is larger than its current value, undefined PepperVars are appended to increase the array to the specified length.
534 # Returns a Boolean indicating whether the operation succeeds.
535 fun length
=(length
: Int): Bool `{
537 b = g_varArrayInterface->SetLength(*self, length);
542 # Nit class representing a Pepper C API PP_Var.
543 extern class PepperVar `{ struct PP_Var* `}
546 return malloc
( sizeof
( struct
PP_Var ) );
549 # Converts PepperVar to standard types.
551 # Actually supports bools, ints, floats, strings. To be used with 'isa'.
552 fun to_nit: nullable Pepperable
554 if isa_null then return null
555 if isa_bool then return as_bool
556 if isa_int then return as_int
557 if isa_float then return as_float
558 if isa_string then return as_string
559 if is_undefined then return null
564 private fun isa_null: Bool `{ return self->type == PP_VARTYPE_NULL; `}
565 private fun isa_bool
: Bool `{ return self->type == PP_VARTYPE_BOOL; `}
566 private fun isa_int: Bool `{ return self->type == PP_VARTYPE_INT32; `}
567 private fun isa_float
: Bool `{ return self->type == PP_VARTYPE_DOUBLE; `}
568 private fun isa_string: Bool `{ return self->type == PP_VARTYPE_STRING; `}
569 private fun is_undefined
: Bool `{ return self->type == PP_VARTYPE_UNDEFINED; `}
571 private fun as_bool: Bool `{ return self->value.as_bool; `}
572 private fun as_int
: Int `{ return self->value.as_int; `}
573 private fun as_float: Float `{ return self->value.as_double; `}
574 private fun as_string
: String import CString.to_s_with_length
`{
576 char* str = (char*)g_varInterface->VarToUtf8(*self, &len);
577 return CString_to_s_with_length(str, len);
581 # Provides a method to convert in PepperVars.
583 fun to_pepper
: PepperVar is abstract
589 # Converts a Int into a PepperVar with Int type.
590 redef fun to_pepper
`{
591 struct PP_Var* var = malloc( sizeof( struct PP_Var ) );
592 *var = PP_MakeInt32(self);
600 # Converts a Float into a PepperVar with Float type.
601 redef fun to_pepper
`{
602 struct PP_Var* var = malloc( sizeof( struct PP_Var ) );
603 *var = PP_MakeDouble(self);
611 # Converts a Bool into a PepperVar with Bool type.
612 redef fun to_pepper
`{
613 struct PP_Var* var = malloc( sizeof( struct PP_Var ) );
614 *var = PP_MakeBool(self);
622 # Converts a String into a PepperVar with String type.
623 redef fun to_pepper
: PepperVar import String.to_cstring
, String.length
`{
624 char *str = String_to_cstring(self);
625 struct PP_Var* var = malloc( sizeof( struct PP_Var ) );
626 *var = g_varInterface->VarFromUtf8(str, String_length(self));
631 # A stream for PNaCl, redefines basic input and output methods.
637 init do prepare_buffer
(10)
639 redef var end_reached
: Bool = false
641 redef fun eof
do return end_reached
643 # Redefintion of 'write' to send messages to the browser.
644 redef fun write
(s
: Text) do app
.post_message s
.to_s
646 redef fun is_writable
: Bool do return true
648 # Checks if there is a message in the queue, and if so the message is handled automatically.
649 fun check_message
: CString `{
650 return NitHandleMessage();
653 # fill_buffer now checks for a message in the message queue which is filled by user inputs.
654 redef fun fill_buffer
657 var nns
= check_message
658 var nslen
= nns
.cstring_length
659 _buffer_length
= nslen
660 nns
.copy_to
(buffer
, nslen
, 0, 0)
664 # For a PNaCl app, Sys uses PnaclStreams.
666 fun pnacl_stdstr
: PnaclStream do return once
new PnaclStream
669 redef fun stdin
do return pnacl_stdstr
672 redef fun stdout
do return pnacl_stdstr
674 # NaCl output for errors.
675 redef fun stderr
do return pnacl_stdstr
678 # Class that provides the tools to interact with PNaCl.
681 # Sets everything up to work, need to be called at first.
682 fun initialize
import PnaclApp.handle_message
, PnaclApp.handle_dictionary
, CString.to_s_with_length
`{
686 # Posts a message to JS.
687 fun post_message
(message
: String) import String.to_cstring
`{
688 char* str = String_to_cstring(message);
692 # Posts a dictionary to JS.
693 fun post_dictionary
(dictionary
: PepperDictionary) `{
694 PostDictionary(*dictionary);
697 # Posts a PepperVar to JS.
699 # Should be used for testing, not recommanded for conventional conversation.
700 private fun post_var
(v
: PepperVar) `{
704 # Is called when a message is received from JS.
706 # Is set to be redefined in your application to handle like you want.
707 fun handle_message
(message
: String)
709 # To be Implemented by user.
712 # Is called when a Dictionary is received from JS.
714 # Is set to be redefined in your application to handle like you want.
715 # The dictionary is freed after this method returns.
716 fun handle_dictionary
(dictionary
: PepperDictionary)
718 # To be Implemented by user.
721 # Checks if there is a dictionary in the queue, and if so the dictionary is handled automatically.
722 fun check_dictionary
`{
723 NitHandleDictionary();
726 # Infinite loop on check_dictionary
735 # Creates a new thread for Nit.
737 # This function launches the Nit main on a new thread.
738 # Its purpose is to allow Nit to be still operational after an exit when needed,
739 # because reloading the page may not be an option.
741 # Should only be used within the 'exit' before stopping the current thread
742 # when the Nit execution causes a crash.
744 # REQUIRE: g_nit_thread and WrapperNitMain are set.
746 pthread_create(&g_nit_thread, NULL, &WrapperNitMain, NULL);
749 # Calls 'pthread_exit on current thread.
750 fun exit_thread
(exit_value
: Int) `{
751 pthread_exit((void*) exit_value);
754 # Redef of exit in order to avoid the module to crash by terminating only the Nit thread.
755 redef fun exit
(exit_value
: Int)
757 var dictionary
= new PepperDictionary
758 dictionary
["exit"] = exit_value
759 app
.post_dictionary dictionary
760 exit_thread exit_value
763 fun app
: PnaclApp do return once
new PnaclApp