1{ 2 "Napi struct reference in": { 3 "prefix": "napistructrefin", 4 "body": [ 5 "napi_value args[1];", 6 "size_t argc = 1;", 7 "napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);", 8 "// Get struct reference from args[0]. data is a reference which passed to JS through napi_create_external.", 9 "void* data;", 10 "napi_get_value_external(env, args[0], &data);" 11 ] 12 }, 13 "Napi struct reference out": { 14 "prefix": "napistructrefout", 15 "body": [ 16 "/** struct MyStruct {", 17 " * public:", 18 " * MyStruct();", 19 " * ~MyStruct();", 20 " * void SetValue(int value);", 21 " * int GetValue() const;", 22 " * private:", 23 " * int value_;", 24 " * };", 25 " */", 26 "MyStruct* data = new MyStruct();", 27 "napi_value result;", 28 "// Create a JS external value that wraps a pointer to native data.", 29 "napi_create_external(env, data, nullptr, nullptr, &result);" 30 ] 31 }, 32 "Napi class reference in": { 33 "prefix": "napiclassrefin", 34 "body": [ 35 "size_t argc = 1;", 36 "napi_value args[1];", 37 "napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);", 38 "void* data;", 39 "// Get class reference from args[0]. data is a reference which passed to JS through napi_create_external.", 40 "napi_get_value_external(env, args[0], &data);" 41 ] 42 }, 43 "Napi class reference out": { 44 "prefix": "napiclassrefout", 45 "body": [ 46 "/** class MyClass {", 47 " * public:", 48 " * MyClass();", 49 " * ~MyClass();", 50 " * void SetValue(int value);", 51 " * int GetValue() const;", 52 " * private:", 53 " * int value_;", 54 " * };", 55 " */", 56 "MyClass* data = new MyClass();", 57 "napi_value result;", 58 "// Create a JS external value that wraps a pointer to native data.", 59 "napi_create_external(env, data, nullptr, nullptr, &result);" 60 ] 61 }, 62 "Napi array reference in": { 63 "prefix": "napiarrayrefin", 64 "body": [ 65 "size_t argc = 1;", 66 "napi_value args[1];", 67 "int32_t* cppArray;", 68 "napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);", 69 "// Get int32_t array from args[0].", 70 "napi_get_value_external(env, args[0], (void**)&cppArray);" 71 ] 72 }, 73 "Napi array reference out": { 74 "prefix": "napiarrayrefout", 75 "body": [ 76 "int32_t* cppArray = new int32_t[5]{1, 2, 3, 4, 5};", 77 "napi_value jsArray;", 78 "// FinalizeCallback is a optional callback that used to release data. (e.g.:delete[] cppArray;).", 79 "napi_create_external(env, cppArray, FinalizeCallback, nullptr, &jsArray);" 80 ] 81 } 82}