'jsobject'에 해당되는 글 1건

  1. 2009.07.28 커스텀 JS Object 추가하기 18
WebKit을 갖고 작업할 때, 꼭 풀어야 하는 문제중 하나가 커스텀 JS Object를 추가하는 일이다. 관련 자료를 아래와 같이 요약한다.


JS Object 확장 방식

 

 

관련 뉴스그룹 기사

http://www.nabble.com/Extending-webkit-javascript-td21517784.html#a21536928

l  frameLoadDelegate2::didClearWindowObject() custom JS object JS context에 등록하는 최적의 자리다. 해당 함수는 현재 web 문서의 스크립트가 실행되기 직전에 호출된다.

 

 

커스텀 JS Object 추가 절차 요약

l  $/JavaScriptCore/API/tests/testapi.c 파일과
$/JavaScriptCore/API/JSObjectRef.h
파일에 유용한 정보가 있다.

l  JSClassDefinition 구조체의 staticValues 필드와 staticFunctions 필드를 이용하는 것이 커스텀 속성을 가진 커스텀 JS Object를 정의하는 가장 간단한 방법이다.

l  커스텀 속성을 가진 커스텀 객체를 정의하려면

n  커스텀 속성을 위한 get 함수와 set 함수를 준비한다 (ingeeGetPropFunc 함수)

n  커스텀 속성 테이블 JSStaticValue 배열을 준비한다 (ingeePorpArr 배열)

n  커스텀 객체를 위한 JSClassDefinition을 정의한다 (ingeeClassDef 변수)

n  적절한 시점에 JSClassCreate() 함수를 호출하여 타입을 등록한다

n  적절한 시점에 JSObjectMake() 함수를 호출하여 객체를 생성한다

 

static JSValueRef ingeeGetPropFunc

( JSContextRef ctx

, JSObjectRef object

, JSStringRef propertyName

, JSValueRef* exception

)

{

return JSValueMakeUndefined(ctx); //undefined-value 리턴 (단지 샘플...)

}

 

static JSStaticValue ingeePorpArr[]=

{ { "prop1", ingeeGetPropFunc, 0, kJSPropertyAttributeNone }

, { "prop2", ingeeGetPropFunc, 0, kJSPropertyAttributeNone }

, { "prop3", ingeeGetPropFunc, 0, kJSPropertyAttributeNone }

, { 0, 0, 0, 0 }

};

 

static JSClassDefinition ingeeClassDef=

{ 0 //ver

, kJSClassAttributeNone //attributes

, "PersonObject" //className

, 0 //parentClass

, ingeePorpArr //staticValues

, 0 //staticFunctions

, 0 //initialize

, 0 //finalize

, 0 //hasProperty

, 0 //getProperty

, 0 //setProperty

, 0 //deleteProperty

, 0 //getPropertyNames

, 0 //callAsFunction

, 0 //callAsConstructor

, 0 //hasInstance

, 0 //convertToType

};

 

JSValue* SomeFuncToCreateJSObject( ExecState* exec )

{

static JSClassRef ingeeClass;

if (!ingeeClass)

ingeeClass= JSClassCreate(&ingeeClassDef);

 

JSContextRef ctx= JSC::toRef(exec);

JSObjectRef objRef= JSObjectMake(ctx, ingeeClass, (void*)0/*someData*/ );

return JSC::toJS(objRef);

}

 

 

(끝)

Posted by ingeeC
,