Skip to content

Commit

Permalink
Add unsigned long type (#160)
Browse files Browse the repository at this point in the history
This patch adds `unsigned long` type as follows.
  - The `unsigned long` type is an unsigned integer type that has values
    in the range [0, 4294967295].

ISSUE=#158
  • Loading branch information
usefulhyun authored and romandev committed Nov 11, 2017
1 parent 6c08eee commit 1aebc06
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ Jaeseok Yoon <yjaeseok@gmail.com>
Jinho Bang <zino@chromium.org>
Taxoo Kim <taxookim@gmail.com>
Yeonwoo Jo <yeonwoo.jo.92@gmail.com>
Yonghyun Ryu <usefulhyun@gmail.com>
1 change: 1 addition & 0 deletions core/idl_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct IDLLong final : public IDLBaseHelper<int32_t> {};
struct IDLOctet final : public IDLBaseHelper<uint8_t> {};
struct IDLShort final : public IDLBaseHelper<int16_t> {};
struct IDLUnsignedShort final : public IDLBaseHelper<uint16_t> {};
struct IDLUnsignedLong final : public IDLBaseHelper<uint32_t> {};
struct IDLString final : public IDLBaseHelper<std::string> {};

#endif // CORE_IDL_TYPES_H_
1 change: 1 addition & 0 deletions core/js_type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ JS_TYPE_TRAITS_NUMBER(uint8_t);
JS_TYPE_TRAITS_NUMBER(int16_t);
JS_TYPE_TRAITS_NUMBER(uint16_t);
JS_TYPE_TRAITS_NUMBER(int32_t);
JS_TYPE_TRAITS_NUMBER(uint32_t);
JS_TYPE_TRAITS_NUMBER(int64_t);
JS_TYPE_TRAITS_NUMBER(double);

Expand Down
20 changes: 20 additions & 0 deletions core/native_type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,24 @@ struct NativeTypeTraits<IDLString> : public NativeTypeTraitsBase<IDLString> {
}
};

// The unsigned long type is an unsigned integer type that has values in
// the range [0, 4294967295].
template <>
struct NativeTypeTraits<IDLUnsignedLong>
: public NativeTypeTraitsBase<IDLUnsignedLong> {
static uint32_t NativeValue(const Napi::Env& env,
const Napi::Value& js_value) {
if (!js_value.IsNumber()) {
Napi::TypeError::New(env, "It's an invalid number.")
.ThrowAsJavaScriptException();
return 0;
}
return static_cast<uint32_t>(js_value.ToNumber().Int32Value());
}

static bool IsTypeEquals(const Napi::Value& js_value) {
return js_value.IsNumber();
}
};

#endif // CORE_NATIVE_TYPE_TRAITS_H_
12 changes: 12 additions & 0 deletions test/interface_basic_types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ test('Test for IDL \'unsigned short\' type', async () => {
expect(test_interface.unsignedShortMethod(-1) != -1).toBe(true);
});

test('Test for IDL \'unsigned long\' type', async () => {
let test_interface = new bacardi.TestInterface();

// The unsigned long type is an unsigned integer type that has values in
// the range [0, 4294967295].
expect(test_interface.unsignedLongMethod(4294967295)).toBe(4294967295);
expect(test_interface.unsignedLongMethod(0)).toBe(0);
expect(test_interface.unsignedLongMethod(4294967296) != 4294967296)
.toBe(true);
expect(test_interface.unsignedLongMethod(-1) != -1).toBe(true);
});


// FIXME(zino): We should write a test for long type.
// Please see: https://github.com/lunchclass/bacardi/issues/120
Expand Down
4 changes: 4 additions & 0 deletions test/test_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ uint16_t TestInterface::UnsignedShortMethod(uint16_t number) {
return number;
}

uint32_t TestInterface::UnsignedLongMethod(uint32_t number) {
return number;
}

double TestInterface::DoubleMethod(double number) {
return number;
}
Expand Down
1 change: 1 addition & 0 deletions test/test_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class TestInterface {
uint8_t OctetMethod(uint8_t number);
short ShortMethod(short number);
uint16_t UnsignedShortMethod(uint16_t number);
uint32_t UnsignedLongMethod(uint32_t number);
double DoubleMethod(double number);
const std::string StringMethod(const std::string& string);

Expand Down
1 change: 1 addition & 0 deletions test/test_interface.idl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface TestInterface {
octet octetMethod(octet number);
short shortMethod(short number);
unsigned short unsignedShortMethod(unsigned short number);
unsigned long unsignedLongMethod(unsigned long number);
double doubleMethod(double number);
string stringMethod(string string);

Expand Down

0 comments on commit 1aebc06

Please sign in to comment.