This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathmap.cpp
118 lines (90 loc) · 3.55 KB
/
map.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <nan.h>
#include "map.h"
namespace SassTypes
{
Map::Map(Sass_Value* v) : SassValueWrapper(v) {}
Sass_Value* Map::construct(const std::vector<v8::Local<v8::Value>> raw_val, Sass_Value **out) {
size_t length = 0;
if (raw_val.size() >= 1) {
if (!raw_val[0]->IsNumber()) {
return fail("First argument should be an integer.", out);
}
length = Nan::To<uint32_t>(raw_val[0]).FromJust();
}
return *out = sass_make_map(length);
}
void Map::initPrototype(v8::Local<v8::FunctionTemplate> proto) {
Nan::SetPrototypeMethod(proto, "getLength", GetLength);
Nan::SetPrototypeMethod(proto, "getKey", GetKey);
Nan::SetPrototypeMethod(proto, "setKey", SetKey);
Nan::SetPrototypeMethod(proto, "getValue", GetValue);
Nan::SetPrototypeMethod(proto, "setValue", SetValue);
}
NAN_METHOD(Map::GetValue) {
if (info.Length() != 1) {
return Nan::ThrowTypeError("Expected just one argument");
}
if (!info[0]->IsNumber()) {
return Nan::ThrowTypeError("Supplied index should be an integer");
}
Sass_Value* map = Map::Unwrap<Map>(info.This())->value;
size_t index = Nan::To<uint32_t>(info[0]).FromJust();
if (index >= sass_map_get_length(map)) {
return Nan::ThrowRangeError(Nan::New("Out of bound index").ToLocalChecked());
}
info.GetReturnValue().Set(Factory::create(sass_map_get_value(map, Nan::To<uint32_t>(info[0]).FromJust()))->get_js_object());
}
NAN_METHOD(Map::SetValue) {
if (info.Length() != 2) {
return Nan::ThrowTypeError("Expected two arguments");
}
if (!info[0]->IsNumber()) {
return Nan::ThrowTypeError("Supplied index should be an integer");
}
if (!info[1]->IsObject()) {
return Nan::ThrowTypeError("Supplied value should be a SassValue object");
}
Value* sass_value = Factory::unwrap(info[1]);
if (sass_value) {
sass_map_set_value(Map::Unwrap<Map>(info.This())->value, Nan::To<uint32_t>(info[0]).FromJust(), sass_value->get_sass_value());
} else {
Nan::ThrowTypeError("A SassValue is expected as a map value");
}
}
NAN_METHOD(Map::GetKey) {
if (info.Length() != 1) {
return Nan::ThrowTypeError("Expected just one argument");
}
if (!info[0]->IsNumber()) {
return Nan::ThrowTypeError("Supplied index should be an integer");
}
Sass_Value* map = Map::Unwrap<Map>(info.This())->value;
size_t index = Nan::To<uint32_t>(info[0]).FromJust();
if (index >= sass_map_get_length(map)) {
return Nan::ThrowRangeError(Nan::New("Out of bound index").ToLocalChecked());
}
SassTypes::Value* obj = Factory::create(sass_map_get_key(map, Nan::To<uint32_t>(info[0]).FromJust()));
v8::Local<v8::Object> js_obj = obj->get_js_object();
info.GetReturnValue().Set(js_obj);
}
NAN_METHOD(Map::SetKey) {
if (info.Length() != 2) {
return Nan::ThrowTypeError("Expected two arguments");
}
if (!info[0]->IsNumber()) {
return Nan::ThrowTypeError("Supplied index should be an integer");
}
if (!info[1]->IsObject()) {
return Nan::ThrowTypeError("Supplied value should be a SassValue object");
}
Value* sass_value = Factory::unwrap(info[1]);
if (sass_value) {
sass_map_set_key(Map::Unwrap<Map>(info.This())->value, Nan::To<uint32_t>(info[0]).FromJust(), sass_value->get_sass_value());
} else {
Nan::ThrowTypeError("A SassValue is expected as a map key");
}
}
NAN_METHOD(Map::GetLength) {
info.GetReturnValue().Set(Nan::New<v8::Number>(sass_map_get_length(Map::Unwrap<Map>(info.This())->value)));
}
}