Skip to content

Commit

Permalink
util, feat: support TextEncoder/TextDecoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Jun 2, 2022
1 parent ea5147c commit 7ad39e6
Show file tree
Hide file tree
Showing 11 changed files with 488 additions and 9 deletions.
47 changes: 47 additions & 0 deletions fibjs/include/TextEncoder.h
@@ -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 */
117 changes: 117 additions & 0 deletions fibjs/include/ifs/TextDecoder.h
@@ -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();
}
}
117 changes: 117 additions & 0 deletions fibjs/include/ifs/TextEncoder.h
@@ -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();
}
}
8 changes: 7 additions & 1 deletion fibjs/include/ifs/util.h
Expand Up @@ -16,6 +16,8 @@
namespace fibjs {

class LruCache_base;
class TextDecoder_base;
class TextEncoder_base;
class Buffer_base;

class util_base : public object_base {
Expand Down Expand Up @@ -159,6 +161,8 @@ class util_base : public object_base {
}

#include "ifs/LruCache.h"
#include "ifs/TextDecoder.h"
#include "ifs/TextEncoder.h"
#include "ifs/Buffer.h"

namespace fibjs {
Expand Down Expand Up @@ -226,7 +230,9 @@ inline ClassInfo& util_base::class_info()
};

static ClassData::ClassObject s_object[] = {
{ "LruCache", LruCache_base::class_info }
{ "LruCache", LruCache_base::class_info },
{ "TextDecoder", TextDecoder_base::class_info },
{ "TextEncoder", TextEncoder_base::class_info }
};

static ClassData s_cd = {
Expand Down
50 changes: 50 additions & 0 deletions fibjs/src/util/TextEncoder.cpp
@@ -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;
}

}
25 changes: 25 additions & 0 deletions idl/zh-cn/TextDecoder.idl
@@ -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;
};

0 comments on commit 7ad39e6

Please sign in to comment.