Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util, feat: support TextEncoder/TextDecoder.
- Loading branch information
Showing
11 changed files
with
488 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* TextEncoder.h | ||
* | ||
* Created on: Jun 2, 2022 | ||
* Author: lion | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "ifs/TextEncoder.h" | ||
#include "ifs/TextDecoder.h" | ||
|
||
namespace fibjs { | ||
|
||
class TextEncoder : public TextEncoder_base { | ||
public: | ||
TextEncoder(exlib::string codec) | ||
: m_codec(codec) | ||
{ | ||
} | ||
|
||
public: | ||
// TextEncoder_base | ||
virtual result_t encode(exlib::string data, v8::Local<v8::Object> opts, obj_ptr<Buffer_base>& retVal); | ||
virtual result_t get_encoding(exlib::string& retVal); | ||
|
||
private: | ||
exlib::string m_codec; | ||
}; | ||
|
||
class TextDecoder : public TextDecoder_base { | ||
public: | ||
TextDecoder(exlib::string codec) | ||
: m_codec(codec) | ||
{ | ||
} | ||
|
||
public: | ||
// TextDecoder_base | ||
virtual result_t decode(Buffer_base* data, v8::Local<v8::Object> opts, exlib::string& retVal); | ||
virtual result_t get_encoding(exlib::string& retVal); | ||
|
||
private: | ||
exlib::string m_codec; | ||
}; | ||
|
||
} /* namespace fibjs */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/*************************************************************************** | ||
* * | ||
* This file was automatically generated using idlc.js * | ||
* PLEASE DO NOT EDIT!!!! * | ||
* * | ||
***************************************************************************/ | ||
|
||
#pragma once | ||
|
||
/** | ||
@author Leo Hoo <lion@9465.net> | ||
*/ | ||
|
||
#include "../object.h" | ||
|
||
namespace fibjs { | ||
|
||
class Buffer_base; | ||
|
||
class TextDecoder_base : public object_base { | ||
DECLARE_CLASS(TextDecoder_base); | ||
|
||
public: | ||
// TextDecoder_base | ||
static result_t _new(exlib::string codec, v8::Local<v8::Object> opts, obj_ptr<TextDecoder_base>& retVal, v8::Local<v8::Object> This = v8::Local<v8::Object>()); | ||
virtual result_t decode(Buffer_base* data, v8::Local<v8::Object> opts, exlib::string& retVal) = 0; | ||
virtual result_t get_encoding(exlib::string& retVal) = 0; | ||
|
||
public: | ||
template <typename T> | ||
static void __new(const T& args); | ||
|
||
public: | ||
static void s__new(const v8::FunctionCallbackInfo<v8::Value>& args); | ||
static void s_decode(const v8::FunctionCallbackInfo<v8::Value>& args); | ||
static void s_get_encoding(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args); | ||
}; | ||
} | ||
|
||
#include "ifs/Buffer.h" | ||
|
||
namespace fibjs { | ||
inline ClassInfo& TextDecoder_base::class_info() | ||
{ | ||
static ClassData::ClassMethod s_method[] = { | ||
{ "decode", s_decode, false, false } | ||
}; | ||
|
||
static ClassData::ClassProperty s_property[] = { | ||
{ "encoding", s_get_encoding, block_set, false } | ||
}; | ||
|
||
static ClassData s_cd = { | ||
"TextDecoder", false, s__new, NULL, | ||
ARRAYSIZE(s_method), s_method, 0, NULL, ARRAYSIZE(s_property), s_property, 0, NULL, NULL, NULL, | ||
&object_base::class_info() | ||
}; | ||
|
||
static ClassInfo s_ci(s_cd); | ||
return s_ci; | ||
} | ||
|
||
inline void TextDecoder_base::s__new(const v8::FunctionCallbackInfo<v8::Value>& args) | ||
{ | ||
CONSTRUCT_INIT(); | ||
__new(args); | ||
} | ||
|
||
template <typename T> | ||
void TextDecoder_base::__new(const T& args) | ||
{ | ||
obj_ptr<TextDecoder_base> vr; | ||
|
||
METHOD_NAME("new TextDecoder()"); | ||
CONSTRUCT_ENTER(); | ||
|
||
METHOD_OVER(2, 0); | ||
|
||
OPT_ARG(exlib::string, 0, "utf8"); | ||
OPT_ARG(v8::Local<v8::Object>, 1, v8::Object::New(isolate)); | ||
|
||
hr = _new(v0, v1, vr, args.This()); | ||
|
||
CONSTRUCT_RETURN(); | ||
} | ||
|
||
inline void TextDecoder_base::s_decode(const v8::FunctionCallbackInfo<v8::Value>& args) | ||
{ | ||
exlib::string vr; | ||
|
||
METHOD_NAME("TextDecoder.decode"); | ||
METHOD_INSTANCE(TextDecoder_base); | ||
METHOD_ENTER(); | ||
|
||
METHOD_OVER(2, 1); | ||
|
||
ARG(obj_ptr<Buffer_base>, 0); | ||
OPT_ARG(v8::Local<v8::Object>, 1, v8::Object::New(isolate)); | ||
|
||
hr = pInst->decode(v0, v1, vr); | ||
|
||
METHOD_RETURN(); | ||
} | ||
|
||
inline void TextDecoder_base::s_get_encoding(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args) | ||
{ | ||
exlib::string vr; | ||
|
||
METHOD_NAME("TextDecoder.encoding"); | ||
METHOD_INSTANCE(TextDecoder_base); | ||
PROPERTY_ENTER(); | ||
|
||
hr = pInst->get_encoding(vr); | ||
|
||
METHOD_RETURN(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/*************************************************************************** | ||
* * | ||
* This file was automatically generated using idlc.js * | ||
* PLEASE DO NOT EDIT!!!! * | ||
* * | ||
***************************************************************************/ | ||
|
||
#pragma once | ||
|
||
/** | ||
@author Leo Hoo <lion@9465.net> | ||
*/ | ||
|
||
#include "../object.h" | ||
|
||
namespace fibjs { | ||
|
||
class Buffer_base; | ||
|
||
class TextEncoder_base : public object_base { | ||
DECLARE_CLASS(TextEncoder_base); | ||
|
||
public: | ||
// TextEncoder_base | ||
static result_t _new(exlib::string codec, v8::Local<v8::Object> opts, obj_ptr<TextEncoder_base>& retVal, v8::Local<v8::Object> This = v8::Local<v8::Object>()); | ||
virtual result_t encode(exlib::string data, v8::Local<v8::Object> opts, obj_ptr<Buffer_base>& retVal) = 0; | ||
virtual result_t get_encoding(exlib::string& retVal) = 0; | ||
|
||
public: | ||
template <typename T> | ||
static void __new(const T& args); | ||
|
||
public: | ||
static void s__new(const v8::FunctionCallbackInfo<v8::Value>& args); | ||
static void s_encode(const v8::FunctionCallbackInfo<v8::Value>& args); | ||
static void s_get_encoding(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args); | ||
}; | ||
} | ||
|
||
#include "ifs/Buffer.h" | ||
|
||
namespace fibjs { | ||
inline ClassInfo& TextEncoder_base::class_info() | ||
{ | ||
static ClassData::ClassMethod s_method[] = { | ||
{ "encode", s_encode, false, false } | ||
}; | ||
|
||
static ClassData::ClassProperty s_property[] = { | ||
{ "encoding", s_get_encoding, block_set, false } | ||
}; | ||
|
||
static ClassData s_cd = { | ||
"TextEncoder", false, s__new, NULL, | ||
ARRAYSIZE(s_method), s_method, 0, NULL, ARRAYSIZE(s_property), s_property, 0, NULL, NULL, NULL, | ||
&object_base::class_info() | ||
}; | ||
|
||
static ClassInfo s_ci(s_cd); | ||
return s_ci; | ||
} | ||
|
||
inline void TextEncoder_base::s__new(const v8::FunctionCallbackInfo<v8::Value>& args) | ||
{ | ||
CONSTRUCT_INIT(); | ||
__new(args); | ||
} | ||
|
||
template <typename T> | ||
void TextEncoder_base::__new(const T& args) | ||
{ | ||
obj_ptr<TextEncoder_base> vr; | ||
|
||
METHOD_NAME("new TextEncoder()"); | ||
CONSTRUCT_ENTER(); | ||
|
||
METHOD_OVER(2, 0); | ||
|
||
OPT_ARG(exlib::string, 0, "utf8"); | ||
OPT_ARG(v8::Local<v8::Object>, 1, v8::Object::New(isolate)); | ||
|
||
hr = _new(v0, v1, vr, args.This()); | ||
|
||
CONSTRUCT_RETURN(); | ||
} | ||
|
||
inline void TextEncoder_base::s_encode(const v8::FunctionCallbackInfo<v8::Value>& args) | ||
{ | ||
obj_ptr<Buffer_base> vr; | ||
|
||
METHOD_NAME("TextEncoder.encode"); | ||
METHOD_INSTANCE(TextEncoder_base); | ||
METHOD_ENTER(); | ||
|
||
METHOD_OVER(2, 1); | ||
|
||
ARG(exlib::string, 0); | ||
OPT_ARG(v8::Local<v8::Object>, 1, v8::Object::New(isolate)); | ||
|
||
hr = pInst->encode(v0, v1, vr); | ||
|
||
METHOD_RETURN(); | ||
} | ||
|
||
inline void TextEncoder_base::s_get_encoding(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args) | ||
{ | ||
exlib::string vr; | ||
|
||
METHOD_NAME("TextEncoder.encoding"); | ||
METHOD_INSTANCE(TextEncoder_base); | ||
PROPERTY_ENTER(); | ||
|
||
hr = pInst->get_encoding(vr); | ||
|
||
METHOD_RETURN(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* TextEncoder.cpp | ||
* | ||
* Created on: Jun 2, 2022 | ||
* Author: lion | ||
*/ | ||
|
||
#include "object.h" | ||
#include <TextEncoder.h> | ||
#include "ifs/iconv.h" | ||
|
||
namespace fibjs { | ||
|
||
result_t TextEncoder_base::_new(exlib::string codec, v8::Local<v8::Object> opts, obj_ptr<TextEncoder_base>& retVal, | ||
v8::Local<v8::Object> This) | ||
{ | ||
retVal = new TextEncoder(codec); | ||
return 0; | ||
} | ||
|
||
result_t TextEncoder::encode(exlib::string data, v8::Local<v8::Object> opts, obj_ptr<Buffer_base>& retVal) | ||
{ | ||
return iconv_base::encode(m_codec, data, retVal); | ||
} | ||
|
||
result_t TextEncoder::get_encoding(exlib::string& retVal) | ||
{ | ||
retVal = m_codec; | ||
return 0; | ||
} | ||
|
||
result_t TextDecoder_base::_new(exlib::string codec, v8::Local<v8::Object> opts, obj_ptr<TextDecoder_base>& retVal, | ||
v8::Local<v8::Object> This) | ||
{ | ||
retVal = new TextDecoder(codec); | ||
return 0; | ||
} | ||
|
||
result_t TextDecoder::decode(Buffer_base* data, v8::Local<v8::Object> opts, exlib::string& retVal) | ||
{ | ||
return iconv_base::decode(m_codec, data, retVal); | ||
} | ||
|
||
result_t TextDecoder::get_encoding(exlib::string& retVal) | ||
{ | ||
retVal = m_codec; | ||
return 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/*! @brief TextDecoder 解码对象 | ||
|
||
创建方法 | ||
```JavaScript | ||
var textDecoder = new util.TextDecoder('utf8'); | ||
``` | ||
*/ | ||
interface TextDecoder : object | ||
{ | ||
/*! @brief TextDecoder 对象构造函数,使用参数构造 | ||
@param codec 指定解码字符集 | ||
@param opts 指定解码选项 | ||
*/ | ||
TextDecoder(String codec = "utf8", Object opts = {}); | ||
|
||
/*! @brief 将二进制数据转换为文本 | ||
@param data 要转换的二进制 | ||
@param opts 指定解码选项 | ||
@return 返回解码的文本 | ||
*/ | ||
String decode(Buffer data, Object opts = {}); | ||
|
||
/*! @brief 查询当前的编码字符集 */ | ||
readonly String encoding; | ||
}; |
Oops, something went wrong.