1 // Copyright 2014 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef FXJS_CJS_UTIL_H_ 8 #define FXJS_CJS_UTIL_H_ 9 10 #include <vector> 11 12 #include "core/fxcrt/widestring.h" 13 #include "fxjs/cjs_object.h" 14 #include "fxjs/js_define.h" 15 #include "v8/include/v8-forward.h" 16 17 class CJS_Util final : public CJS_Object { 18 public: 19 enum class DataType { 20 kInvalid = -1, 21 kInt = 0, 22 kDouble = 1, 23 kString = 2, 24 }; 25 26 static uint32_t GetObjDefnID(); 27 static void DefineJSObjects(CFXJS_Engine* pEngine); 28 29 CJS_Util(v8::Local<v8::Object> pObject, CJS_Runtime* pRuntime); 30 ~CJS_Util() override; 31 32 // Ensure that |sFormat| contains at most one well-understood printf 33 // formatting directive which is safe to use with a single argument, and 34 // return the type of argument expected, or -1 otherwise. If -1 is returned, 35 // it is NOT safe to use |sFormat| with printf() and it must be copied 36 // byte-by-byte. 37 // 38 // Exposed for testing. 39 static DataType ParseDataType(WideString* sFormat); 40 41 // Exposed for testing. 42 static WideString StringPrintx(const WideString& cFormat, 43 const WideString& cSource); 44 45 JS_STATIC_METHOD(printd, CJS_Util) 46 JS_STATIC_METHOD(printf, CJS_Util) 47 JS_STATIC_METHOD(printx, CJS_Util) 48 JS_STATIC_METHOD(scand, CJS_Util) 49 JS_STATIC_METHOD(byteToChar, CJS_Util) 50 51 private: 52 static uint32_t ObjDefnID; 53 static const char kName[]; 54 static const JSMethodSpec MethodSpecs[]; 55 56 CJS_Result printd(CJS_Runtime* pRuntime, 57 const std::vector<v8::Local<v8::Value>>& params); 58 CJS_Result printf(CJS_Runtime* pRuntime, 59 const std::vector<v8::Local<v8::Value>>& params); 60 CJS_Result printx(CJS_Runtime* pRuntime, 61 const std::vector<v8::Local<v8::Value>>& params); 62 CJS_Result scand(CJS_Runtime* pRuntime, 63 const std::vector<v8::Local<v8::Value>>& params); 64 CJS_Result byteToChar(CJS_Runtime* pRuntime, 65 const std::vector<v8::Local<v8::Value>>& params); 66 }; 67 68 #endif // FXJS_CJS_UTIL_H_ 69