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 # Targets the PNaCl platform
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'.
24 # Provides PNaCl support for Nit.
25 module pnacl
is platform
28 intrude import standard
::stream
31 #include "ppapi/c/pp_errors.h"
32 #include "ppapi/c/ppp.h"
33 #include "ppapi/c/ppp_instance.h"
34 #include "ppapi/c/pp_bool.h"
35 #include "ppapi/c/ppb_var.h"
36 #include "ppapi/c/ppb_messaging.h"
37 #include "ppapi/c/ppp_messaging.h"
38 #include "ppapi/c/ppb_var_dictionary.h"
39 #include "ppapi/c/ppb_var_array.h"
50 #define MAX_DICTIONARY_QUEUE_SIZE 200
51 #define MAX_MESSAGE_QUEUE_SIZE 10
53 extern int nit_main(int, char**);
55 /* A working thread for Nit. */
56 static pthread_t g_nit_thread;
58 /* Mutex that guards the queues. */
59 static pthread_mutex_t g_dictionary_queue_mutex;
60 static pthread_mutex_t g_message_queue_mutex;
62 /* Condition variables that are signalled when the queues are not empty. */
63 static pthread_cond_t g_dictionary_queue_not_empty_cond;
64 static pthread_cond_t g_message_queue_not_empty_cond;
66 /** Circular queues of dictionaries and messages from JavaScript to be handled.
68 * If g_queue_start < g_queue_end:
69 * all elements in the range [g_queue_start, g_queue_end) are valid.
70 * If g_queue_start > g_queue_end:
71 * all elements in the ranges [0, g_queue_end) and
72 * [g_queue_start, MAX_QUEUE_SIZE) are valid.
73 * If g_queue_start == g_queue_end, and g_queue_size > 0:
74 * all elements in the g_queue are valid.
75 * If g_queue_start == g_queue_end, and g_queue_size == 0:
76 * No elements are valid. */
77 static struct PP_Var g_dictionary_queue[MAX_DICTIONARY_QUEUE_SIZE];
78 static char* g_message_queue[MAX_MESSAGE_QUEUE_SIZE];
80 /* The index of the head of the queues. */
81 static int g_dictionary_queue_start = 0;
82 static int g_message_queue_start = 0;
84 /* The index of the tail of the queues, non-inclusive. */
85 static int g_dictionary_queue_end = 0;
86 static int g_message_queue_end = 0;
88 /* The size of the queues. */
89 static int g_dictionary_queue_size = 0;
90 static int g_message_queue_size = 0;
92 /* PNaCl interfaces. */
93 const PPB_Messaging* g_varMessagingInterface;
94 const PPB_Var* g_varInterface;
95 const PPB_VarDictionary* g_varDictionaryInterface;
96 const PPB_VarArray* g_varArrayInterface;
98 PP_Instance g_instance;
101 /* A wrapper to launch the Nit main on a new thread. */
102 void* WrapperNitMain(void* arg) {
107 /** Return whether the queues are empty.
109 * NOTE: this function assumes g_queue_mutex lock is held.
110 * @return non-zero if the queue is empty. */
111 static int IsDictionaryQueueEmpty() { return g_dictionary_queue_size == 0; }
112 static int IsMessageQueueEmpty() { return g_message_queue_size == 0; }
114 /** Return whether the queues are full.
116 * NOTE: this function assumes g_queue_mutex lock is held.
117 * @return non-zero if the queue is full. */
118 static int IsDictionaryQueueFull() { return g_dictionary_queue_size == MAX_DICTIONARY_QUEUE_SIZE; }
119 static int IsMessageQueueFull() { return g_message_queue_size == MAX_MESSAGE_QUEUE_SIZE; }
121 /* Initialize the queues. */
122 void InitializeQueues() {
123 pthread_mutex_init(&g_dictionary_queue_mutex, NULL);
124 pthread_cond_init(&g_dictionary_queue_not_empty_cond, NULL);
125 pthread_mutex_init(&g_message_queue_mutex, NULL);
126 pthread_cond_init(&g_message_queue_not_empty_cond, NULL);
129 /** Enqueue a dictionary (i.e. add to the end)
131 * If the queue is full, the dictionary will be dropped.
133 * NOTE: this function assumes g_dictionary_queue_mutex is _NOT_ held.
134 * @param[in] dictionary, the dictionary to enqueue.
135 * @return non-zero if the dictionary was added to the queue. */
136 int EnqueueDictionary(struct PP_Var dictionary) {
137 pthread_mutex_lock(&g_dictionary_queue_mutex);
139 /* We shouldn't block the main thread waiting for the queue to not be full,
140 * so just drop the dictionary. */
141 if (IsDictionaryQueueFull()) {
142 pthread_mutex_unlock(&g_dictionary_queue_mutex);
146 g_dictionary_queue[g_dictionary_queue_end] = dictionary;
147 g_dictionary_queue_end = (g_dictionary_queue_end + 1) % MAX_DICTIONARY_QUEUE_SIZE;
148 g_dictionary_queue_size++;
150 pthread_cond_signal(&g_dictionary_queue_not_empty_cond);
152 pthread_mutex_unlock(&g_dictionary_queue_mutex);
157 /** Enqueue a message (i.e. add to the end)
159 * If the queue is full, the message will be dropped.
161 * NOTE: this function assumes g_message_queue_mutex is _NOT_ held.
162 * @param[in] message The message to enqueue.
163 * @return non-zero if the message was added to the queue. */
164 int EnqueueMessage(char* message) {
165 pthread_mutex_lock(&g_message_queue_mutex);
167 /* We shouldn't block the main thread waiting for the queue to not be full,
168 * so just drop the message. */
169 if (IsMessageQueueFull()) {
170 pthread_mutex_unlock(&g_message_queue_mutex);
174 g_message_queue[g_message_queue_end] = message;
175 g_message_queue_end = (g_message_queue_end + 1) % MAX_MESSAGE_QUEUE_SIZE;
176 g_message_queue_size++;
178 pthread_cond_signal(&g_message_queue_not_empty_cond);
180 pthread_mutex_unlock(&g_message_queue_mutex);
185 /** Dequeue a dictionary and return it.
187 * This function blocks until a dictionary is available. It should not be called
188 * on the main thread.
190 * NOTE: this function assumes g_dictionary_queue_mutex is _NOT_ held.
191 * @return The dictionary at the head of the queue. */
192 struct PP_Var DequeueDictionary() {
193 struct PP_Var dictionary = g_varDictionaryInterface->Create();
195 pthread_mutex_lock(&g_dictionary_queue_mutex);
197 while (IsDictionaryQueueEmpty()) {
198 pthread_cond_wait(&g_dictionary_queue_not_empty_cond, &g_dictionary_queue_mutex);
201 dictionary = g_dictionary_queue[g_dictionary_queue_start];
202 g_dictionary_queue_start = (g_dictionary_queue_start + 1) % MAX_DICTIONARY_QUEUE_SIZE;
203 g_dictionary_queue_size--;
205 pthread_mutex_unlock(&g_dictionary_queue_mutex);
210 /** Dequeue a message and return it.
212 * This function blocks until a message is available. It should not be called
213 * on the main thread.
215 * NOTE: this function assumes g_queue_mutex is _NOT_ held.
216 * @return The message at the head of the queue. */
217 char* DequeueMessage() {
218 char* message = NULL;
220 pthread_mutex_lock(&g_message_queue_mutex);
222 while (IsMessageQueueEmpty()) {
223 pthread_cond_wait(&g_message_queue_not_empty_cond, &g_message_queue_mutex);
226 message = g_message_queue[g_message_queue_start];
227 g_message_queue_start = (g_message_queue_start + 1) % MAX_MESSAGE_QUEUE_SIZE;
228 g_message_queue_size--;
230 pthread_mutex_unlock(&g_message_queue_mutex);
235 /* Posts a string message to JS. */
236 void PostMessage(char* message) {
237 /* Create PP_Var containing the message body. */
238 struct PP_Var varString = g_varInterface->VarFromUtf8(message, strlen(message));
240 /* Post message to the JavaScript layer. */
241 g_varMessagingInterface->PostMessage(g_instance, varString);
244 /* Posts a Dictionary (JS like object) to JS. */
245 void PostDictionary(struct PP_Var dictionary) {
246 g_varMessagingInterface->PostMessage(g_instance, dictionary);
249 /* Posts a Variable (aka PepperVar) to JS.
250 Should only be used for testing, conventional conversation is made
251 with Strings or Dictionaries. */
252 void PostVar(struct PP_Var v) {
253 g_varMessagingInterface->PostMessage(g_instance, v);
256 /* char* to PP_Var. */
257 static struct PP_Var CStrToVar(const char* str) {
258 if (g_varInterface != NULL) {
259 return g_varInterface->VarFromUtf8(str, strlen(str));
261 return PP_MakeUndefined();
264 static PP_Bool Instance_DidCreate(PP_Instance instance, uint32_t argc, const char* argn[], const char* argv[]) {
265 g_instance = instance;
267 /* Initialization of the queues and creation of the thread for Nit. */
269 pthread_create(&g_nit_thread, NULL, &WrapperNitMain, NULL);
274 static void Instance_DidDestroy(PP_Instance instance) {
278 static void Instance_DidChangeView(PP_Instance pp_instance, PP_Resource view) {
282 static void Instance_DidChangeFocus(PP_Instance pp_instance, PP_Bool has_focus) {
286 static PP_Bool Instance_HandleDocumentLoad(PP_Instance pp_instance, PP_Resource pp_url_loader) {
291 /* Called when JS sends something, is set to accept Strings or Dictionaries,
292 returns an error if received object is not a String or Dictionary. */
293 void Messaging_HandleMessage(PP_Instance instance, struct PP_Var varMessage) {
294 if(varMessage.type == PP_VARTYPE_DICTIONARY) {
295 if(!EnqueueDictionary(varMessage)) {
296 struct PP_Var errorMessage = CStrToVar("QueueFull : dropped dictionary because the queue was full.");
297 g_varMessagingInterface->PostMessage(g_instance, errorMessage);
300 else if(varMessage.type == PP_VARTYPE_STRING) {
302 char* message = (char*)g_varInterface->VarToUtf8(varMessage, &len);
303 if(!EnqueueMessage(message)) {
304 struct PP_Var errorMessage = CStrToVar("QueueFull : dropped message because the queue was full.");
305 g_varMessagingInterface->PostMessage(g_instance, errorMessage);
309 struct PP_Var errorMessage = CStrToVar("TypeError : only accepts JS objects or Strings");
310 g_varMessagingInterface->PostMessage(g_instance, errorMessage);
314 /* This function is called by Nit when using check_dictionary,
315 returns the dictionary at the head of the queue. */
316 void* NitHandleDictionary() {
317 struct PP_Var dictionary = DequeueDictionary();
318 PnaclApp_handle_dictionary(app, &dictionary);
322 /* This function is called By Nit when waiting for a user input. */
323 char* NitHandleMessage() {
324 return DequeueMessage();
328 PP_EXPORT int32_t PPP_InitializeModule(PP_Module module_id, PPB_GetInterface get_browser_interface) {
329 /* Initializing global pointers. */
330 g_varMessagingInterface = (const PPB_Messaging*) get_browser_interface(PPB_MESSAGING_INTERFACE);
331 g_varInterface = (const PPB_Var*) get_browser_interface(PPB_VAR_INTERFACE);
332 g_varDictionaryInterface = (const PPB_VarDictionary*) get_browser_interface(PPB_VAR_DICTIONARY_INTERFACE);
333 g_varArrayInterface = (const PPB_VarArray*) get_browser_interface(PPB_VAR_ARRAY_INTERFACE);
337 PP_EXPORT void PPP_ShutdownModule() {
341 PP_EXPORT const void* PPP_GetInterface(const char* interface_name) {
342 if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0)
344 static PPP_Instance instance_interface = {
346 &Instance_DidDestroy,
347 &Instance_DidChangeView,
348 &Instance_DidChangeFocus,
349 &Instance_HandleDocumentLoad
351 return &instance_interface;
353 else if (strcmp(interface_name, PPP_MESSAGING_INTERFACE) == 0) {
354 static PPP_Messaging messaging_interface = {
355 &Messaging_HandleMessage
357 return &messaging_interface;
362 /* Hack in order to avoid the problem with file. */
363 int poll(void *fds, int nfds, int timeout) { return 0; }
366 # Nit class representing a Pepper C API PP_Var typed as a Dictionary.
367 extern class PepperDictionary `{ struct PP_Var* `}
370 struct
PP_Var* recv
= malloc
( sizeof
( struct
PP_Var ) );
371 *recv
= g_varDictionaryInterface-
>Create();
375 # Get fonction using PepperVars.
377 # Returns the value that is associated with 'key'.
378 # If 'key' is not a String typed PepperVar, or doesn't exist in the Dictionary, an undefined PepperVar is returned.
379 fun native_get(key: PepperVar): PepperVar `{
380 struct
PP_Var* value
= malloc
( sizeof
( struct
PP_Var ) );
381 *value
= g_varDictionaryInterface-
>Get(*recv
, *key
);
385 # Returns the value associated with 'key'.
387 # 'key' must be a String.
388 # If 'key' is not a String or doesn't exist in the Dictionary, 'null' is returned.
389 fun [](key: nullable Pepperable): nullable Pepperable
391 var native_key = key.to_pepper
392 var native_value = native_get(native_key)
393 return native_value.to_nit
396 # Set function using PepperVars.
398 # Sets the value associated with the specified key.
399 # 'key' must be a String typed PepperVar.
400 # If 'key' hasn't existed in the Dictionary, it is added and associated with 'value'.
401 # Otherwise, the previous value is replaced with 'value'.
402 # Returns a Boolean indicating whether the operation succeeds.
403 fun native_set(key: PepperVar, value: PepperVar): Bool `{
405 b
= g_varDictionaryInterface-
>Set(*recv
, *key
, *value
);
409 # Sets the value associated with the specified key.
411 # 'key' must be a String.
412 # If 'key' hasn't existed in the Dictionary, it is added and associated with 'value'.
413 # Otherwise, the previous value is replaced with 'value'.
414 # Returns a Boolean indicating whether the operation succeeds.
415 fun []=(key: nullable Pepperable, value: nullable Pepperable): Bool
417 var native_key = key.to_pepper
418 var native_value = value.to_pepper
419 return native_set(native_key, native_value)
422 # Deletes the specified key and its associated value, if the key exists.
424 # Takes a String typed PepperVar.
425 fun native_delete(key: PepperVar) `{
426 g_varDictionaryInterface-
>Delete(*recv
, *key
);
429 # Deletes the specified key and its associated value, if the key exists.
432 fun delete(key: String)
434 var native_key = key.to_pepper
435 native_delete native_key
438 # Checks whether a key exists.
440 # Takes a String typed PepperVar.
441 fun native_has_key(key: PepperVar): Bool `{
443 b
= g_varDictionaryInterface-
>HasKey(*recv
, *key
);
447 # Checks whether a key exists.
450 fun has_key(key: String): Bool
452 var native_key = key.to_pepper
453 return native_has_key(native_key)
456 # Gets all the keys in a dictionary.
458 # Returns a PepperArray which contains all the keys of the Dictionary. The elements are string vars.
459 fun get_keys: PepperArray `{
460 struct
PP_Var* array
= malloc
( sizeof
( struct
PP_Var ) );
461 *array
= g_varDictionaryInterface-
>GetKeys(*recv
);
465 # Use this function to copy a dictionary.
466 fun copy: PepperDictionary `{
467 struct
PP_Var* varDictionary
= malloc
( sizeof
( struct
PP_Var ) );
468 *varDictionary
= g_varDictionaryInterface-
>Create();
469 *varDictionary
= *recv
;
470 return varDictionary
;
474 # Nit class representing a Pepper C API PP_Var typed as an Array.
475 extern class PepperArray `{ struct PP_Var* `}
478 struct PP_Var* recv = malloc( sizeof( struct PP_Var ) );
479 *recv = g_varArrayInterface->Create();
483 # Returns the element at the specified position as a PepperVar.
485 # If 'index' is larger than or equal to the array length, an undefined PepperVar is returned.
486 fun native_get
(index
: Int): PepperVar `{
487 struct PP_Var* value = malloc( sizeof( struct PP_Var ) );
488 *value = g_varArrayInterface->Get(*recv, index);
492 # Returns the element at the specified position.
494 # If 'index' is larger than or equal to the array length, 'null' is returned.
495 fun [](index
: Int): nullable Pepperable
497 var native_value
= native_get
(index
)
498 return native_value
.to_nit
501 # Returns an int containing the length of the PepperArray.
503 int length = g_varArrayInterface->GetLength(*recv);
507 # Takes a PepperVar for the 'value' param.
509 # Sets the value of an element in the array at indicated index.
510 # If 'index' is larger than or equal to the array length, the length is updated to be 'index' + 1.
511 # Any position in the array that hasn't been set before is set to undefined, i.e., PepperVar of C type PP_VARTYPE_UNDEFINED.
512 # Returns a Boolean indicating whether the operation succeeds.
513 fun native_set
(index
: Int, value
: PepperVar): Bool `{
515 b = g_varArrayInterface->Set(*recv, index, *value);
519 # Sets the value of an element in the array at indicated index.
521 # If 'index' is larger than or equal to the array length, the length is updated to be 'index' + 1.
522 # Any position in the array that hasn't been set before is set to undefined, i.e., PepperVar of C type PP_VARTYPE_UNDEFINED.
523 # Returns a Boolean indicating whether the operation succeeds.
524 fun []=(index
: Int, value
: nullable Pepperable): Bool
526 var native_value
= value
.to_pepper
527 return native_set
(index
, native_value
)
530 # Sets the array length.
532 # If 'length' is smaller than its current value, the array is truncated to the new length.
533 # Any elements that no longer fit are removed and the references to them will be released.
534 # If 'length' is larger than its current value, undefined PepperVars are appended to increase the array to the specified length.
535 # Returns a Boolean indicating whether the operation succeeds.
536 fun length
=(length
: Int): Bool `{
538 b = g_varArrayInterface->SetLength(*recv, length);
543 # Nit class representing a Pepper C API PP_Var.
544 extern class PepperVar `{ struct PP_Var* `}
547 return malloc
( sizeof
( struct
PP_Var ) );
550 # Converts PepperVar to standard types.
552 # Actually supports bools, ints, floats, strings. To be used with 'isa'.
553 fun to_nit: nullable Pepperable
555 if isa_null then return null
556 if isa_bool then return as_bool
557 if isa_int then return as_int
558 if isa_float then return as_float
559 if isa_string then return as_string
560 if is_undefined then return null
565 private fun isa_null: Bool `{ return recv->type == PP_VARTYPE_NULL; `}
566 private fun isa_bool
: Bool `{ return recv->type == PP_VARTYPE_BOOL; `}
567 private fun isa_int: Bool `{ return recv->type == PP_VARTYPE_INT32; `}
568 private fun isa_float
: Bool `{ return recv->type == PP_VARTYPE_DOUBLE; `}
569 private fun isa_string: Bool `{ return recv->type == PP_VARTYPE_STRING; `}
570 private fun is_undefined
: Bool `{ return recv->type == PP_VARTYPE_UNDEFINED; `}
572 private fun as_bool: Bool `{ return recv->value.as_bool; `}
573 private fun as_int
: Int `{ return recv->value.as_int; `}
574 private fun as_float: Float `{ return recv->value.as_double; `}
575 private fun as_string
: String import NativeString.to_s_with_length
`{
577 char* str = (char*)g_varInterface->VarToUtf8(*recv, &len);
578 return NativeString_to_s_with_length(str, len);
582 # Provides a method to convert in PepperVars.
584 fun to_pepper
: PepperVar is abstract
590 # Converts a Int into a PepperVar with Int type.
591 redef fun to_pepper
`{
592 struct PP_Var* var = malloc( sizeof( struct PP_Var ) );
593 *var = PP_MakeInt32(recv);
601 # Converts a Float into a PepperVar with Float type.
602 redef fun to_pepper
`{
603 struct PP_Var* var = malloc( sizeof( struct PP_Var ) );
604 *var = PP_MakeDouble(recv);
612 # Converts a Bool into a PepperVar with Bool type.
613 redef fun to_pepper
`{
614 struct PP_Var* var = malloc( sizeof( struct PP_Var ) );
615 *var = PP_MakeBool(recv);
623 # Converts a String into a PepperVar with String type.
624 redef fun to_pepper
: PepperVar import String.to_cstring
, String.length
`{
625 char *str = String_to_cstring(recv);
626 struct PP_Var* var = malloc( sizeof( struct PP_Var ) );
627 *var = g_varInterface->VarFromUtf8(str, String_length(recv));
632 # A stream for PNaCl, redefines basic input and output methods.
634 super PollableIStream
636 super BufferedIStream
638 init do prepare_buffer
(10)
640 redef var end_reached
: Bool = false
642 redef fun eof
do return end_reached
644 # Redefintion of 'write' to send messages to the browser.
645 redef fun write
(s
: Text) do app
.post_message s
.to_s
647 redef fun is_writable
: Bool do return true
649 # Checks if there is a message in the queue, and if so the message is handled automatically.
650 fun check_message
: NativeString `{
651 return NitHandleMessage();
654 # fill_buffer now checks for a message in the message queue which is filled by user inputs.
655 redef fun fill_buffer
659 _buffer
.append check_message
.to_s
663 # For a PNaCl app, Sys uses PnaclStreams.
665 fun pnacl_stdstr
: PnaclStream do return once
new PnaclStream
668 redef fun stdin
do return pnacl_stdstr
671 redef fun stdout
do return pnacl_stdstr
673 # NaCl output for errors.
674 redef fun stderr
do return pnacl_stdstr
677 # Class that provides the tools to interact with PNaCl.
680 # Sets everything up to work, need to be called at first.
681 fun initialize
import PnaclApp.handle_message
, PnaclApp.handle_dictionary
, NativeString.to_s_with_length
`{
685 # Posts a message to JS.
686 fun post_message
(message
: String) import String.to_cstring
`{
687 char* str = String_to_cstring(message);
691 # Posts a dictionary to JS.
692 fun post_dictionary
(dictionary
: PepperDictionary) `{
693 PostDictionary(*dictionary);
696 # Posts a PepperVar to JS.
698 # Should be used for testing, not recommanded for conventional conversation.
699 private fun post_var
(v
: PepperVar) `{
703 # Is called when a message is received from JS.
705 # Is set to be redefined in your application to handle like you want.
706 fun handle_message
(message
: String)
708 # To be Implemented by user.
711 # Is called when a Dictionary is received from JS.
713 # Is set to be redefined in your application to handle like you want.
714 # The dictionary is freed after this method returns.
715 fun handle_dictionary
(dictionary
: PepperDictionary)
717 # To be Implemented by user.
720 # Checks if there is a dictionary in the queue, and if so the dictionary is handled automatically.
721 fun check_dictionary
`{
722 NitHandleDictionary();
725 # Infinite loop on check_dictionary
734 # Creates a new thread for Nit.
736 # This function launches the Nit main on a new thread.
737 # Its purpose is to allow Nit to be still operational after an exit when needed,
738 # because reloading the page may not be an option.
740 # Should only be used within the 'exit' before stopping the current thread
741 # when the Nit execution causes a crash.
743 # REQUIRE: g_nit_thread and WrapperNitMain are set.
745 pthread_create(&g_nit_thread, NULL, &WrapperNitMain, NULL);
748 # Calls 'pthread_exit on current thread.
749 fun exit_thread
(exit_value
: Int) `{
750 pthread_exit((void*) exit_value);
753 # Redef of exit in order to avoid the module to crash by terminating only the Nit thread.
754 redef fun exit
(exit_value
: Int)
756 var dictionary
= new PepperDictionary
757 dictionary
["exit"] = exit_value
758 app
.post_dictionary dictionary
759 exit_thread exit_value
762 fun app
: PnaclApp do return once
new PnaclApp