source
stringlengths
14
113
code
stringlengths
10
21.3k
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; hilog.info(0x0000, 'testTag', 'Test Node-API 2 + 3 = %{public}d', testNapi.add(2, 3));
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const createObjectWithProperties: (data: string) => Object;
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; let value = testNapi.createObjectWithProperties('createObject'); hilog.info(0x0000, 'testTag', 'Node-API napi_create_object_with_properties:%{public}s', JSON.stringify(value));
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const createObjectWithNameProperties: (data: string) => string | { name: string };
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; let value = testNapi.createObjectWithNameProperties('ls'); hilog.info(0x0000, 'testTag', 'Node-API napi_create_object_with_named_properties:%{public}s', JSON.stringify(value));
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const runScriptPath: () => boolean;
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; try { // Return true is the script is executed successfully; return false otherwise. hilog.info(0x0000, 'testTag', 'Test Node-API napi_run_script_path: %{public}s', testNapi.runScriptPath()); } catch (error) { hilog.error(0x0000, 'testTag', 'Te...
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const getAddress: () => number; export const getSetSize: () => number; export const store: (a: number) => void; export const erase: (a: number) => void; export const clear: () => void;
application-dev\napi\use-napi-about-extension.md
// index.ets import testNapi from 'libentry.so'; import taskpool from '@ohos.taskpool'; @Concurrent function getAddress() { let address: number = testNapi.getAddress(); console.info("taskpool:: address is " + address); } @Concurrent function store(a:number, b:number, c:number) { let size:number = testNapi.getSe...
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const aboutSerialize: (obj: Object) => number;
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; class Obj { numKey:number = 0; } let obj: Obj = { numKey: 500 }; hilog.info(0x0000, 'testTag', ' Node-API aboutSerialize: %{public}d', testNapi.aboutSerialize(obj));
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const isSendable: <T>(a: T) => boolean;
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; let value = testNapi.isSendable('createObject'); hilog.info(0x0000, 'testTag', 'Node-API napi_is_sendable: %{public}s', JSON.stringify(value));
application-dev\napi\use-napi-about-extension.md
// index.d.ts @Sendable export class SendableClass { static staticStr: string; static staticFunc(): string; str: string; func(): string; }
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; let value = new testNapi.SendableClass(); hilog.info(0x0000, 'testTag', 'Node-API napi_define_sendable_class: %{public}s', value.str);
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const getSendableObject: () => { x: true };
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; let value = testNapi.getSendableObject(); hilog.info(0x0000, 'testTag', 'Node-API napi_create_sendable_object_with_properties: %{public}s', JSON.stringify(value));
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const getSendableArray: () => [];
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; let value = testNapi.getSendableArray(); hilog.info(0x0000, 'testTag', 'Node-API napi_create_sendable_array: %{public}s', JSON.stringify(value));
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const getSendableArrayWithLength: () => [];
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; let value = testNapi.getSendableArrayWithLength(); hilog.info(0x0000, 'testTag', 'Node-API napi_create_sendable_array_with_length: %{public}s', JSON.stringify(value.length));
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const getSendableArrayBuffer: () => void;
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; testNapi.getSendableArrayBuffer();
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const getSendableTypedArray: () => void;
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; testNapi.getSendableTypedArray();
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const wrapSendable: () => void;
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; testNapi.wrapSendable();
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const wrapSendableWithSize: () => void;
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; testNapi.wrapSendableWithSize();
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const unwrapSendable: () => void;
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; testNapi.unwrapSendable();
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const removeWrapSendable: () => void;
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; testNapi.removeWrapSendable();
application-dev\napi\use-napi-about-extension.md
// index.d.ts export const testNapiWrapEnhance: () => void;
application-dev\napi\use-napi-about-extension.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; testNapi.testNapiWrapEnhance();
application-dev\napi\use-napi-about-function.md
// index.d.ts export const getCbArgs: <T>(arg: T) => T; // The input parameters of getCbArgQuantity are customized. In this example, two input parameters are used: string and number. export const getCbArgQuantity: (str: string, num: number) => number; export const getCbContext: () => Object;
application-dev\napi\use-napi-about-function.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; function summation(arr: Array<number>) { let sum: number = 0; for (let i = 0; i < arr.length; i++) { sum += arr[i]; } return sum; } const str = 'message'; const arr = [0, 1, 2, 3, 4, 5]; const num = 526; class Student { name: string; a...
application-dev\napi\use-napi-about-function.md
// index.d.ts export const callFunction: (func: Function) => number; export const objCallFunction: (obj: Object, func: Function) => number;
application-dev\napi\use-napi-about-function.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; function returnNumber() { return 10; } class Person { age(): number { return 11; } } const person = new Person(); hilog.info(0x0000, 'testTag', 'Test Node-API call_function:%{public}d', testNapi.callFunction(returnNumber)); hilog.info(0x000...
application-dev\napi\use-napi-about-function.md
// index.d.ts export const calculateArea: (width: number, height: number) => number;
application-dev\napi\use-napi-about-function.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; hilog.info(0x0000, 'testTag', 'Test Node-API create_function:%{public}d ', testNapi.calculateArea(1.2, 4));
application-dev\napi\use-napi-about-object.md
// index.d.ts export const getPrototype: (object: Object) => Object;
application-dev\napi\use-napi-about-object.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; // Define a class. class Person { // Property. name: string; age: number; // Constructor. constructor(name: string, age: number) { this.name = name; this.age = age; } } // Create a class instance. const person = new Person('Alice',...
application-dev\napi\use-napi-about-object.md
// index.d.ts export const createObject: () => { name: string };
application-dev\napi\use-napi-about-object.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; try { const myObject = testNapi.createObject(); hilog.info(0x0000, 'testTag', 'Test Node-API napi_create_object: %{public}s', myObject.name); } catch (error) { hilog.error(0x0000, 'testTag', 'Test Node-API napi_create_object errorCode: %{public}...
application-dev\napi\use-napi-about-object.md
// index.d.ts export interface Obj { data: number message: string } export const objectFreeze: (objFreeze: Object) => Obj;
application-dev\napi\use-napi-about-object.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; try { class Obj { data: number = 0 message: string = "" } let obj: Obj = {data: 0, message: "hello world"}; let objFreeze = testNapi.objectFreeze(obj); hilog.info(0x0000, 'testTag', 'Test Node-API napi_object_freeze: %{public}s', (ob...
application-dev\napi\use-napi-about-object.md
// index.d.ts export interface Obj { data: number message: string id: number } export const objectSeal : (objSeal: Object) => Obj;
application-dev\napi\use-napi-about-object.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; try { class Obj { data: number = 0 message: string = "" // Optional property. address?: number = 0 } let obj: Obj = { data: 0, message: "hello world"}; let objSeal = testNapi.objectSeal(obj); hilog.info(0x0000, 'testTag', 'Te...
application-dev\napi\use-napi-about-object.md
// index.d.ts export const napiTypeOf : <T>(value: T) => string | void;
application-dev\napi\use-napi-about-object.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; try { let varUndefined: undefined; hilog.info(0x0000, 'testTag', 'Test Node-API napi_typeof: %{public}s', testNapi.napiTypeOf(varUndefined)); let varNull: null = null; hilog.info(0x0000, 'testTag', 'Test Node-API napi_typeof: %{public}s', test...
application-dev\napi\use-napi-about-object.md
// index.d.ts export const napiInstanceOf: (date: Object, construct: Object) => boolean | void;
application-dev\napi\use-napi-about-object.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; try { class Person { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } } const person = new Person("Alice", 30); class Obj { data: number = 0 message: s...
application-dev\napi\use-napi-about-object.md
// index.d.ts export const setTypeTagToObject: (obj: Object, index: number) => boolean | void; export const checkObjectTypeTag: (obj: Object, index: number) => boolean;
application-dev\napi\use-napi-about-object.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; class Obj { data: number = 0 message: string = "" } let objA: Obj = { data: 0, message: "hello world"}; let objB: Obj = { data: 10, message: "typeTag"}; hilog.info(0x0000, 'testTag', 'Test Node-API napi_type_tag_object objA -> 0: %{public}s', test...
application-dev\napi\use-napi-about-object.md
// index.d.ts export const createExternal: () => Object; export const getExternalType: (externalData: Object) => boolean;
application-dev\napi\use-napi-about-object.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; const externalData = testNapi.createExternal(); hilog.info(0x0000, 'testTag', 'Test Node-API napi_create_external:%{public}s', testNapi.getExternalType(externalData));
application-dev\napi\use-napi-about-object.md
// index.d.ts export const getValueExternal: () => number;
application-dev\napi\use-napi-about-object.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; hilog.info(0x0000, 'Node-API', 'get_value_external:%{public}d', testNapi.getValueExternal());
application-dev\napi\use-napi-about-object.md
// index.d.ts export const createSymbol : () => symbol;
application-dev\napi\use-napi-about-object.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; let varSymbol = testNapi.createSymbol(); hilog.info(0x0000, 'Node-API', 'createSymbol:%{public}s', typeof varSymbol);
application-dev\napi\use-napi-about-other-practical-tools.md
// index.d.ts export const getModuleFileName: () => string | void;
application-dev\napi\use-napi-about-other-practical-tools.md
import hilog from '@ohos.hilog' import testNapi from 'libentry.so' let filename = testNapi.getModuleFileName(); hilog.info(0x0000, 'testTag', 'Test Node-API node_api_get_module_file_name:%{public}s', filename);
application-dev\napi\use-napi-about-other-practical-tools.md
// index.d.ts export const strictEquals : (lhs: string, rhs: string | number) => boolean | void;
application-dev\napi\use-napi-about-other-practical-tools.md
import hilog from '@ohos.hilog' import testNapi from 'libentry.so' try { let lhs = "123"; let rhs = "123"; let str = "456"; let num = 123; hilog.info(0x0000, 'testTag', 'Test Node-API napi_strict_equals: %{public}s', testNapi.strictEquals(lhs, rhs)); hilog.info(0x0000, 'testTag', 'Test Node-API napi_strict_...
application-dev\napi\use-napi-about-primitive.md
// index.d.ts export const coerceToBool: <T>(data: T) => boolean;
application-dev\napi\use-napi-about-primitive.md
import hilog from '@ohos.hilog' import testNapi from 'libentry.so' let value = testNapi.coerceToBool<number>(0); let str = testNapi.coerceToBool<string>('111111111'); let obj = new Object(); let res = testNapi.coerceToBool<Object>(obj); let result = testNapi.coerceToBool<null>(null); // false hilog.info(0x0000, 'testT...
application-dev\napi\use-napi-about-primitive.md
// index.d.ts export const coerceToNumber: <T>(data: T) => number;
application-dev\napi\use-napi-about-primitive.md
import hilog from '@ohos.hilog' import testNapi from 'libentry.so' let value = testNapi.coerceToNumber<string>('2556'); let str = testNapi.coerceToNumber<string>('sssss'); let bool = testNapi.coerceToNumber<boolean>(true); hilog.info(0x0000, 'testTag', 'Test Node-API napi_coerce_to_number:%{public}d', value); // Not-a...
application-dev\napi\use-napi-about-primitive.md
// index.d.ts export const coerceToObject: <T>(data: T) => Object;
application-dev\napi\use-napi-about-primitive.md
import hilog from '@ohos.hilog' import testNapi from 'libentry.so' let value = testNapi.coerceToObject<string>('222222'); let result = testNapi.coerceToObject<number>(111); hilog.info(0x0000, 'testTag', 'Node-API coerceToObject:%{public}s.', typeof result); if (typeof value === 'object') { hilog.info(0x0000, 'testTa...
application-dev\napi\use-napi-about-primitive.md
// index.d.ts export const coerceToString: <T>(data: T) => string;
application-dev\napi\use-napi-about-primitive.md
import hilog from '@ohos.hilog' import testNapi from 'libentry.so' let value = testNapi.coerceToString<number>(212); let obj = new Object(); let res = testNapi.coerceToString<Object>(obj); let bool = testNapi.coerceToString<boolean>(false); hilog.info(0x0000, 'testTag', 'Test Node-API napi_coerce_to_string:%{public}s'...
application-dev\napi\use-napi-about-primitive.md
// index.d.ts export const getBoolean: <T>(data: T, value: String) => boolean;
application-dev\napi\use-napi-about-primitive.md
import hilog from '@ohos.hilog' import testNapi from 'libentry.so' let value = testNapi.getBoolean<number>(1, '1'); let data = testNapi.getBoolean<string>('sss', '1'); hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_boolean:%{public}s', value); hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_boolean:%{publ...
application-dev\napi\use-napi-about-primitive.md
// index.d.ts export const getValueBool: (value: boolean | string) => boolean | void;
application-dev\napi\use-napi-about-primitive.md
import hilog from '@ohos.hilog' import testNapi from 'libentry.so' // Pass in a Boolean value and a non-Boolean value. After the Boolean value is passed in, the Boolean value is returned. After the non-Boolean value is passed in, undefined is returned. hilog.info(0x0000, 'Node-API', 'get_value_bool_not_bool %{public}s...
application-dev\napi\use-napi-about-primitive.md
// index.d.ts export const getGlobal: () => Object;
application-dev\napi\use-napi-about-primitive.md
import hilog from '@ohos.hilog' import testNapi from 'libentry.so' let globalObj = testNapi.getGlobal(); // Check whether the obtained global object has its own properties. hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_global:%{public}s', globalObj.hasOwnProperty!("undefined"));
application-dev\napi\use-napi-about-primitive.md
// index.d.ts export const getNull: () => null;
application-dev\napi\use-napi-about-primitive.md
import hilog from '@ohos.hilog' import testNapi from 'libentry.so' let value = testNapi.getNull(); hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_null:%{public}s', value);
application-dev\napi\use-napi-about-primitive.md
// index.d.ts export const getUndefined: (value: undefined) => boolean;
application-dev\napi\use-napi-about-primitive.md
import hilog from '@ohos.hilog' import testNapi from 'libentry.so' let data: undefined = undefined; let value = testNapi.getUndefined(data); hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_undefined:%{public}s', value);
application-dev\napi\use-napi-about-promise.md
// index.d.ts export const isPromise: <T>(value: T) => boolean;
application-dev\napi\use-napi-about-promise.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; let value = Promise.resolve(); // Return true if the object passed in is a promise; return false otherwise. hilog.info(0x0000, 'Node-API', 'napi_is_promise %{public}s', testNapi.isPromise(value)); hilog.info(0x0000, 'Node-API', 'napi_is_promise strin...
application-dev\napi\use-napi-about-promise.md
// index.d.ts export const createPromise: () => boolean | void; export const resolveRejectDeferred: (resolve: string, reject: string, status: boolean) => Promise<string> | void;
application-dev\napi\use-napi-about-promise.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; // Create a promise. Return true if the operation is successful, and return false otherwise. hilog.info(0x0000, 'Node-API', 'napi_create_promise %{public}s', testNapi.createPromise()); // Call resolveRejectDeferred to resolve or reject the promise an...
application-dev\napi\use-napi-about-property.md
// index.d.ts export const getPropertyNames: (obj: Object) => Array<string> | void;
application-dev\napi\use-napi-about-property.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; try { class Obj { data: number = 0 message: string = "" } let obj: Obj = { data: 0, message: "hello world"}; let propertyNames = testNapi.getPropertyNames(obj); if (Array.isArray(propertyNames) && propertyNames.length > 0) { hilo...
application-dev\napi\use-napi-about-property.md
// index.d.ts export const setProperty: (obj: Object, key: String, value: string) => Object | void;
application-dev\napi\use-napi-about-property.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; try { class Obj { data: number = 0 message: string = "" } let obj: Obj = { data: 0, message: "hello world"}; let result = testNapi.setProperty(obj, "code", "hi"); hilog.info(0x0000, 'testTag', 'Test Node-API napi_set_property: %{publ...
application-dev\napi\use-napi-about-property.md
// index.d.ts export const getProperty: (obj: Object, key: string) => string | void;
application-dev\napi\use-napi-about-property.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; try { class Obj { data: number = 0 message: string = "" } let obj: Obj = { data: 0, message: "hello world"}; hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_property: %{public}s', testNapi.getProperty(obj, "message")); } catch (e...
application-dev\napi\use-napi-about-property.md
// index.d.ts export const hasProperty: (obj: Object, key: number | string) => boolean | void;
application-dev\napi\use-napi-about-property.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; try { class Obj { data: number = 0 message: string = "" } let obj: Obj = { data: 0, message: "hello world"}; let resultFalse = testNapi.hasProperty(obj, 0); let resultTrue = testNapi.hasProperty(obj, "data"); hilog.info(0x0000, 'te...
application-dev\napi\use-napi-about-property.md
// index.d.ts export const deleteProperty: (obj: Object, key:string) => boolean;
application-dev\napi\use-napi-about-property.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; class Obj { first: number = 0; } let obj: Obj = { first: 1}; hilog.info(0x0000, 'testTag', 'Test Node-API napi_delete_property first: %{public}s', testNapi.deleteProperty(obj, 'first')); // Set the new property to unconfigurable. // The Object.defin...
application-dev\napi\use-napi-about-property.md
// index.d.ts export const napiHasOwnProperty: (obj: Object, key:string) => boolean | void;
application-dev\napi\use-napi-about-property.md
import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; let myObj = { 'myProperty': 1 }; let inheritedObj = { 'inheritedProperty': 2 }; // The Object.setPrototypeOf method is not supported in DevEco Studio 4.1.0.400 or later. It must be used in TS. Object.setPrototypeOf(myObj, inheritedObj); hilog.info(0x...
application-dev\napi\use-napi-about-property.md
// index.d.ts export const napiSetNamedProperty: (key: string) => Object | void;