1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_ACELITE_SCOPE_JS_VALUE_H
17 #define OHOS_ACELITE_SCOPE_JS_VALUE_H
18 
19 #include "jerryscript.h"
20 #include "non_copyable.h"
21 
22 #include "stdlib.h"
23 
24 namespace OHOS {
25 namespace ACELite {
26 // Local scope JS value, which can be released (decrease the ref count) automatically
27 // when leaving its scope, do not transfer one ScopeJSValue out of its outtest scope.
28 class ScopeJSValue final {
29 public:
30     ACE_DISALLOW_COPY_AND_MOVE(ScopeJSValue);
ScopeJSValue(jerry_value_t value)31     ScopeJSValue(jerry_value_t value) : value_(value) {}
~ScopeJSValue()32     ~ScopeJSValue()
33     {
34         if (value_ == 0) {
35             return;
36         }
37         jerry_release_value(value_);
38         value_ = 0;
39     }
40 
Obain()41     jerry_value_t Obain() const
42     {
43         return value_;
44     }
45 
46 private:
47     // disallow create the object on the heap, only can use on the stack
new(size_t size)48     void *operator new(size_t size)
49     {
50         (void)(size);
51         // return buffer to dismiss the compolier warning,
52         // actually no-fact as this function never be used
53         const size_t bufferLen = 4;
54         return malloc(bufferLen);
55     }
delete(void * ptr)56     void operator delete(void *ptr)
57     {
58         (void)(ptr);
59     }
60 
61     jerry_value_t value_ = 0;
62 };
63 } // namespace ACELite
64 } // namespace OHOS
65 #endif // OHOS_ACELITE_SCOPE_JS_VALUE_H
66