1 /*
2  * Copyright (c) 2022 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 #include <cstring>
17 #include <iostream>
18 #include "script_manager.h"
19 #include "script/script_instruction.h"
20 
21 using namespace std;
22 using namespace Uscript;
23 
24 class UserInstruction1 : public UScriptInstruction {
25 public:
Execute(UScriptEnv & env,UScriptContext & context)26     int32_t Execute(UScriptEnv &env, UScriptContext &context) override
27     {
28         context.PushParam("user instruction1");
29         return USCRIPT_SUCCESS;
30     }
31 };
32 
33 class UserInstruction2 : public UScriptInstruction {
34 public:
Execute(UScriptEnv & env,UScriptContext & context)35     int32_t Execute(UScriptEnv &env, UScriptContext &context) override
36     {
37         context.PushParam("user instruction2");
38         return USCRIPT_SUCCESS;
39     }
40 };
41 
42 class UserInstructionAbort : public UScriptInstruction {
43 public:
Execute(UScriptEnv & env,UScriptContext & context)44     int32_t Execute(UScriptEnv &env, UScriptContext &context) override
45     {
46         context.PushParam("user instruction2");
47         return USCRIPT_SUCCESS;
48     }
49 };
50 
51 class UserInstructionFactory : public UScriptInstructionFactory {
52 public:
CreateInstructionInstance(UScriptInstructionPtr & instr,const std::string & name)53     int32_t CreateInstructionInstance(UScriptInstructionPtr& instr, const std::string& name) override
54     {
55         if (name == "uInstruction1") {
56             instr = new (std::nothrow) UserInstruction1(); // 为何不加nothrow???
57         } else if (name == "uInstruction2") {
58             instr = new (std::nothrow) UserInstruction2();
59         } else if (name == "uInstruction3") {
60             instr = nullptr; // mock new failed scene
61         } else if (name == "abort") {
62             instr = new (std::nothrow) UserInstructionAbort(); // mock reserved error
63         } else {
64             return USCRIPT_NOTEXIST_INSTRUCTION;
65         }
66         return USCRIPT_SUCCESS;
67     }
UserInstructionFactory()68     UserInstructionFactory()
69     {
70     }
~UserInstructionFactory()71     ~UserInstructionFactory()
72     {
73     }
74 };
75 
76 extern "C" {
GetInstructionFactory()77 __attribute__((visibility("default"))) Uscript::UScriptInstructionFactoryPtr GetInstructionFactory()
78 {
79     return new (std::nothrow) UserInstructionFactory;
80 }
81 
ReleaseInstructionFactory(Uscript::UScriptInstructionFactoryPtr p)82 __attribute__((visibility("default"))) void ReleaseInstructionFactory(Uscript::UScriptInstructionFactoryPtr p)
83 {
84     delete p;
85 }
86 }