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