Initial commit
[yaffs-website] / node_modules / node-sass / src / sass_types / map.cpp
1 #include <nan.h>
2 #include "map.h"
3
4 namespace SassTypes
5 {
6   Map::Map(Sass_Value* v) : SassValueWrapper(v) {}
7
8   Sass_Value* Map::construct(const std::vector<v8::Local<v8::Value>> raw_val, Sass_Value **out) {
9     size_t length = 0;
10
11     if (raw_val.size() >= 1) {
12       if (!raw_val[0]->IsNumber()) {
13         return fail("First argument should be an integer.", out);
14       }
15
16       length = Nan::To<uint32_t>(raw_val[0]).FromJust();
17     }
18
19     return *out = sass_make_map(length);
20   }
21
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);
28   }
29
30   NAN_METHOD(Map::GetValue) {
31
32     if (info.Length() != 1) {
33       return Nan::ThrowTypeError("Expected just one argument");
34     }
35
36     if (!info[0]->IsNumber()) {
37       return Nan::ThrowTypeError("Supplied index should be an integer");
38     }
39
40     Sass_Value* map = unwrap(info.This())->value;
41     size_t index = Nan::To<uint32_t>(info[0]).FromJust();
42
43
44     if (index >= sass_map_get_length(map)) {
45       return Nan::ThrowRangeError(Nan::New("Out of bound index").ToLocalChecked());
46     }
47
48     info.GetReturnValue().Set(Factory::create(sass_map_get_value(map, Nan::To<uint32_t>(info[0]).FromJust()))->get_js_object());
49   }
50
51   NAN_METHOD(Map::SetValue) {
52     if (info.Length() != 2) {
53       return Nan::ThrowTypeError("Expected two arguments");
54     }
55
56     if (!info[0]->IsNumber()) {
57       return Nan::ThrowTypeError("Supplied index should be an integer");
58     }
59
60     if (!info[1]->IsObject()) {
61       return Nan::ThrowTypeError("Supplied value should be a SassValue object");
62     }
63
64     Value* sass_value = Factory::unwrap(info[1]);
65     if (sass_value) {
66       sass_map_set_value(unwrap(info.This())->value, Nan::To<uint32_t>(info[0]).FromJust(), sass_value->get_sass_value());
67     } else {
68       Nan::ThrowTypeError("A SassValue is expected as a map value");
69     }
70   }
71
72   NAN_METHOD(Map::GetKey) {
73
74     if (info.Length() != 1) {
75       return Nan::ThrowTypeError("Expected just one argument");
76     }
77
78     if (!info[0]->IsNumber()) {
79       return Nan::ThrowTypeError("Supplied index should be an integer");
80     }
81
82     Sass_Value* map = unwrap(info.This())->value;
83     size_t index = Nan::To<uint32_t>(info[0]).FromJust();
84
85
86     if (index >= sass_map_get_length(map)) {
87       return Nan::ThrowRangeError(Nan::New("Out of bound index").ToLocalChecked());
88     }
89
90     info.GetReturnValue().Set(Factory::create(sass_map_get_key(map, Nan::To<uint32_t>(info[0]).FromJust()))->get_js_object());
91   }
92
93   NAN_METHOD(Map::SetKey) {
94     if (info.Length() != 2) {
95       return Nan::ThrowTypeError("Expected two arguments");
96     }
97
98     if (!info[0]->IsNumber()) {
99       return Nan::ThrowTypeError("Supplied index should be an integer");
100     }
101
102     if (!info[1]->IsObject()) {
103       return Nan::ThrowTypeError("Supplied value should be a SassValue object");
104     }
105
106     Value* sass_value = Factory::unwrap(info[1]);
107     if (sass_value) {
108       sass_map_set_key(unwrap(info.This())->value, Nan::To<uint32_t>(info[0]).FromJust(), sass_value->get_sass_value());
109     } else {
110       Nan::ThrowTypeError("A SassValue is expected as a map key");
111     }
112   }
113
114   NAN_METHOD(Map::GetLength) {
115     info.GetReturnValue().Set(Nan::New<v8::Number>(sass_map_get_length(unwrap(info.This())->value)));
116   }
117 }