6 Map::Map(Sass_Value* v) : SassValueWrapper(v) {}
8 Sass_Value* Map::construct(const std::vector<v8::Local<v8::Value>> raw_val, Sass_Value **out) {
11 if (raw_val.size() >= 1) {
12 if (!raw_val[0]->IsNumber()) {
13 return fail("First argument should be an integer.", out);
16 length = Nan::To<uint32_t>(raw_val[0]).FromJust();
19 return *out = sass_make_map(length);
22 void Map::initPrototype(v8::Local<v8::FunctionTemplate> proto) {
23 Nan::SetPrototypeMethod(proto, "getLength", GetLength);
24 Nan::SetPrototypeMethod(proto, "getKey", GetKey);
25 Nan::SetPrototypeMethod(proto, "setKey", SetKey);
26 Nan::SetPrototypeMethod(proto, "getValue", GetValue);
27 Nan::SetPrototypeMethod(proto, "setValue", SetValue);
30 NAN_METHOD(Map::GetValue) {
32 if (info.Length() != 1) {
33 return Nan::ThrowTypeError("Expected just one argument");
36 if (!info[0]->IsNumber()) {
37 return Nan::ThrowTypeError("Supplied index should be an integer");
40 Sass_Value* map = unwrap(info.This())->value;
41 size_t index = Nan::To<uint32_t>(info[0]).FromJust();
44 if (index >= sass_map_get_length(map)) {
45 return Nan::ThrowRangeError(Nan::New("Out of bound index").ToLocalChecked());
48 info.GetReturnValue().Set(Factory::create(sass_map_get_value(map, Nan::To<uint32_t>(info[0]).FromJust()))->get_js_object());
51 NAN_METHOD(Map::SetValue) {
52 if (info.Length() != 2) {
53 return Nan::ThrowTypeError("Expected two arguments");
56 if (!info[0]->IsNumber()) {
57 return Nan::ThrowTypeError("Supplied index should be an integer");
60 if (!info[1]->IsObject()) {
61 return Nan::ThrowTypeError("Supplied value should be a SassValue object");
64 Value* sass_value = Factory::unwrap(info[1]);
66 sass_map_set_value(unwrap(info.This())->value, Nan::To<uint32_t>(info[0]).FromJust(), sass_value->get_sass_value());
68 Nan::ThrowTypeError("A SassValue is expected as a map value");
72 NAN_METHOD(Map::GetKey) {
74 if (info.Length() != 1) {
75 return Nan::ThrowTypeError("Expected just one argument");
78 if (!info[0]->IsNumber()) {
79 return Nan::ThrowTypeError("Supplied index should be an integer");
82 Sass_Value* map = unwrap(info.This())->value;
83 size_t index = Nan::To<uint32_t>(info[0]).FromJust();
86 if (index >= sass_map_get_length(map)) {
87 return Nan::ThrowRangeError(Nan::New("Out of bound index").ToLocalChecked());
90 info.GetReturnValue().Set(Factory::create(sass_map_get_key(map, Nan::To<uint32_t>(info[0]).FromJust()))->get_js_object());
93 NAN_METHOD(Map::SetKey) {
94 if (info.Length() != 2) {
95 return Nan::ThrowTypeError("Expected two arguments");
98 if (!info[0]->IsNumber()) {
99 return Nan::ThrowTypeError("Supplied index should be an integer");
102 if (!info[1]->IsObject()) {
103 return Nan::ThrowTypeError("Supplied value should be a SassValue object");
106 Value* sass_value = Factory::unwrap(info[1]);
108 sass_map_set_key(unwrap(info.This())->value, Nan::To<uint32_t>(info[0]).FromJust(), sass_value->get_sass_value());
110 Nan::ThrowTypeError("A SassValue is expected as a map key");
114 NAN_METHOD(Map::GetLength) {
115 info.GetReturnValue().Set(Nan::New<v8::Number>(sass_map_get_length(unwrap(info.This())->value)));