Initial commit
[yaffs-website] / node_modules / node-sass / src / sass_types / number.cpp
1 #include <nan.h>
2 #include "number.h"
3 #include "../create_string.h"
4
5 namespace SassTypes
6 {
7   Number::Number(Sass_Value* v) : SassValueWrapper(v) {}
8
9   Sass_Value* Number::construct(const std::vector<v8::Local<v8::Value>> raw_val, Sass_Value **out) {
10     double value = 0;
11     char const* unit = "";
12
13     if (raw_val.size() >= 1) {
14       if (!raw_val[0]->IsNumber()) {
15         return fail("First argument should be a number.", out);
16       }
17
18       value = Nan::To<double>(raw_val[0]).FromJust();
19
20       if (raw_val.size() >= 2) {
21         if (!raw_val[1]->IsString()) {
22           return fail("Second argument should be a string.", out);
23         }
24
25         unit = create_string(raw_val[1]);
26       }
27     }
28
29     return *out = sass_make_number(value, unit);
30   }
31
32   void Number::initPrototype(v8::Local<v8::FunctionTemplate> proto) {
33     Nan::SetPrototypeMethod(proto, "getValue", GetValue);
34     Nan::SetPrototypeMethod(proto, "getUnit", GetUnit);
35     Nan::SetPrototypeMethod(proto, "setValue", SetValue);
36     Nan::SetPrototypeMethod(proto, "setUnit", SetUnit);
37   }
38
39   NAN_METHOD(Number::GetValue) {
40     info.GetReturnValue().Set(Nan::New<v8::Number>(sass_number_get_value(unwrap(info.This())->value)));
41   }
42
43   NAN_METHOD(Number::GetUnit) {
44     info.GetReturnValue().Set(Nan::New<v8::String>(sass_number_get_unit(unwrap(info.This())->value)).ToLocalChecked());
45   }
46
47   NAN_METHOD(Number::SetValue) {
48
49     if (info.Length() != 1) {
50       return Nan::ThrowTypeError("Expected just one argument");
51     }
52
53     if (!info[0]->IsNumber()) {
54       return Nan::ThrowTypeError("Supplied value should be a number");
55     }
56
57     sass_number_set_value(unwrap(info.This())->value, Nan::To<double>(info[0]).FromJust());
58   }
59
60   NAN_METHOD(Number::SetUnit) {
61     if (info.Length() != 1) {
62       return Nan::ThrowTypeError("Expected just one argument");
63     }
64
65     if (!info[0]->IsString()) {
66       return Nan::ThrowTypeError("Supplied value should be a string");
67     }
68
69     sass_number_set_unit(unwrap(info.This())->value, create_string(info[0]));
70   }
71 }