2 * Copyright (c) 2011 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
11 #include "native_client/src/include/portability_io.h"
12 #include "native_client/src/include/portability_process.h"
13 #include "native_client/src/shared/platform/nacl_check.h"
14 #include "ppapi/cpp/module.h"
15 #include "ppapi/native_client/src/trusted/plugin/utility.h"
19 int gNaClPluginDebugPrintEnabled = -1;
22 * Prints formatted message to the log.
24 int NaClPluginPrintLog(const char *format, ...) {
28 static const int kStackBufferSize = 512;
29 char stack_buffer[kStackBufferSize];
31 // Just log locally to stderr if we can't use the nacl interface.
32 if (!GetNaClInterface()) {
33 va_start(arg, format);
34 int rc = vfprintf(stderr, format, arg);
39 va_start(arg, format);
40 out_size = vsnprintf(stack_buffer, kStackBufferSize, format, arg);
42 if (out_size < kStackBufferSize) {
43 GetNaClInterface()->Vlog(stack_buffer);
45 // Message too large for our stack buffer; we have to allocate memory
47 char *buffer = static_cast<char*>(malloc(out_size + 1));
48 va_start(arg, format);
49 vsnprintf(buffer, out_size + 1, format, arg);
51 GetNaClInterface()->Vlog(buffer);
58 * Currently looks for presence of NACL_PLUGIN_DEBUG and returns
59 * 0 if absent and 1 if present. In the future we may include notions
62 int NaClPluginDebugPrintCheckEnv() {
63 char* env = getenv("NACL_PLUGIN_DEBUG");
67 bool IsValidIdentifierString(const char* strval, uint32_t* length) {
68 // This function is supposed to recognize valid ECMAScript identifiers,
70 // http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf
71 // It is currently restricted to only the ASCII subset.
72 // TODO(sehr): Recognize the full Unicode formulation of identifiers.
73 // TODO(sehr): Make this table-driven if efficiency becomes a problem.
80 static const char* kValidFirstChars =
81 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$_";
82 static const char* kValidOtherChars =
83 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$_"
85 if (NULL == strchr(kValidFirstChars, strval[0])) {
89 for (pos = 1; ; ++pos) {
98 if (NULL == strchr(kValidOtherChars, c)) {
102 if (NULL != length) {
108 // We cache the NaCl interface pointer and ensure that its set early on, on the
109 // main thread. This allows GetNaClInterface() to be used from non-main threads.
110 static const PPB_NaCl_Private* g_nacl_interface = NULL;
112 const PPB_NaCl_Private* GetNaClInterface() {
113 return g_nacl_interface;
116 void SetNaClInterface(const PPB_NaCl_Private* nacl_interface) {
117 g_nacl_interface = nacl_interface;
120 } // namespace plugin