Initial commit
[yaffs-website] / node_modules / node-sass / src / sass_types / list.cpp
1 #include <nan.h>
2 #include "list.h"
3
4 namespace SassTypes
5 {
6   List::List(Sass_Value* v) : SassValueWrapper(v) {}
7
8   Sass_Value* List::construct(const std::vector<v8::Local<v8::Value>> raw_val, Sass_Value **out) {
9     size_t length = 0;
10     bool comma = true;
11     bool is_bracketed = false;
12
13     if (raw_val.size() >= 1) {
14       if (!raw_val[0]->IsNumber()) {
15         return fail("First argument should be an integer.", out);
16       }
17
18       length = Nan::To<uint32_t>(raw_val[0]).FromJust();
19
20       if (raw_val.size() >= 2) {
21         if (!raw_val[1]->IsBoolean()) {
22           return fail("Second argument should be a boolean.", out);
23         }
24
25         comma = Nan::To<bool>(raw_val[1]).FromJust();
26       }
27     }
28
29     return *out = sass_make_list(length, comma ? SASS_COMMA : SASS_SPACE, is_bracketed);
30   }
31
32   void List::initPrototype(v8::Local<v8::FunctionTemplate> proto) {
33     Nan::SetPrototypeMethod(proto, "getLength", GetLength);
34     Nan::SetPrototypeMethod(proto, "getSeparator", GetSeparator);
35     Nan::SetPrototypeMethod(proto, "setSeparator", SetSeparator);
36     Nan::SetPrototypeMethod(proto, "getValue", GetValue);
37     Nan::SetPrototypeMethod(proto, "setValue", SetValue);
38   }
39
40   NAN_METHOD(List::GetValue) {
41
42     if (info.Length() != 1) {
43       return Nan::ThrowTypeError("Expected just one argument");
44     }
45
46     if (!info[0]->IsNumber()) {
47       return Nan::ThrowTypeError("Supplied index should be an integer");
48     }
49
50     Sass_Value* list = unwrap(info.This())->value;
51     size_t index = Nan::To<uint32_t>(info[0]).FromJust();
52
53
54     if (index >= sass_list_get_length(list)) {
55       return Nan::ThrowRangeError(Nan::New("Out of bound index").ToLocalChecked());
56     }
57
58     info.GetReturnValue().Set(Factory::create(sass_list_get_value(list, Nan::To<uint32_t>(info[0]).FromJust()))->get_js_object());
59   }
60
61   NAN_METHOD(List::SetValue) {
62     if (info.Length() != 2) {
63       return Nan::ThrowTypeError("Expected two arguments");
64     }
65
66     if (!info[0]->IsNumber()) {
67       return Nan::ThrowTypeError("Supplied index should be an integer");
68     }
69
70     if (!info[1]->IsObject()) {
71       return Nan::ThrowTypeError("Supplied value should be a SassValue object");
72     }
73
74     Value* sass_value = Factory::unwrap(info[1]);
75     if (sass_value) {
76       sass_list_set_value(unwrap(info.This())->value, Nan::To<uint32_t>(info[0]).FromJust(), sass_value->get_sass_value());
77     } else {
78       Nan::ThrowTypeError("A SassValue is expected as the list item");
79     }
80   }
81
82   NAN_METHOD(List::GetSeparator) {
83     info.GetReturnValue().Set(sass_list_get_separator(unwrap(info.This())->value) == SASS_COMMA);
84   }
85
86   NAN_METHOD(List::SetSeparator) {
87     if (info.Length() != 1) {
88       return Nan::ThrowTypeError("Expected just one argument");
89     }
90
91     if (!info[0]->IsBoolean()) {
92       return Nan::ThrowTypeError("Supplied value should be a boolean");
93     }
94
95     sass_list_set_separator(unwrap(info.This())->value, Nan::To<bool>(info[0]).FromJust() ? SASS_COMMA : SASS_SPACE);
96   }
97
98   NAN_METHOD(List::GetLength) {
99     info.GetReturnValue().Set(Nan::New<v8::Number>(sass_list_get_length(unwrap(info.This())->value)));
100   }
101 }