CPP   28

number.cpp

Guest on 20th May 2022 01:36:14 AM

  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.         *out = sass_make_number(value, unit);
  27.         delete unit;
  28.         return *out;
  29.  
  30.       }
  31.     }
  32.  
  33.     return *out = sass_make_number(value, unit);
  34.   }
  35.  
  36.   void Number::initPrototype(v8::Local<v8::FunctionTemplate> proto) {
  37.     Nan::SetPrototypeMethod(proto, "getValue", GetValue);
  38.     Nan::SetPrototypeMethod(proto, "getUnit", GetUnit);
  39.     Nan::SetPrototypeMethod(proto, "setValue", SetValue);
  40.     Nan::SetPrototypeMethod(proto, "setUnit", SetUnit);
  41.   }
  42.  
  43.   NAN_METHOD(Number::GetValue) {
  44.     info.GetReturnValue().Set(Nan::New<v8::Number>(sass_number_get_value(Number::Unwrap<Number>(info.This())->value)));
  45.   }
  46.  
  47.   NAN_METHOD(Number::GetUnit) {
  48.     info.GetReturnValue().Set(Nan::New<v8::String>(sass_number_get_unit(Number::Unwrap<Number>(info.This())->value)).ToLocalChecked());
  49.   }
  50.  
  51.   NAN_METHOD(Number::SetValue) {
  52.  
  53.     if (info.Length() != 1) {
  54.       return Nan::ThrowTypeError("Expected just one argument");
  55.     }
  56.  
  57.     if (!info[0]->IsNumber()) {
  58.       return Nan::ThrowTypeError("Supplied value should be a number");
  59.     }
  60.  
  61.     sass_number_set_value(Number::Unwrap<Number>(info.This())->value, Nan::To<double>(info[0]).FromJust());
  62.   }
  63.  
  64.   NAN_METHOD(Number::SetUnit) {
  65.     if (info.Length() != 1) {
  66.       return Nan::ThrowTypeError("Expected just one argument");
  67.     }
  68.  
  69.     if (!info[0]->IsString()) {
  70.       return Nan::ThrowTypeError("Supplied value should be a string");
  71.     }
  72.  
  73.     sass_number_set_unit(Number::Unwrap<Number>(info.This())->value, create_string(info[0]));
  74.   }
  75. }

Raw Paste


Login or Register to edit or fork this paste. It's free.