Skip to content

Commit

Permalink
changes for fable python compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Aug 19, 2024
1 parent 01645ae commit 897c049
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
54 changes: 28 additions & 26 deletions src/DynamicObj/FablePy.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,56 @@


open Fable.Core

open System.Collections.Generic

module FablePy =

module PropertyDescriptor =
type PropertyObject =
abstract fget : obj
abstract fset : obj

module PropertyObject =

[<Emit("$0.$1")>]
let tryGetPropertyValue (o:obj) (propName:string) : obj option =
PyInterop.em
[<Emit("$0.fget")>]
let tryGetGetter (o:PropertyObject) : obj option =
nativeOnly

let tryGetIsWritable (o:obj) : bool option =
tryGetPropertyValue o "writable"
|> Option.map (fun v -> v :?> bool)
[<Emit("$0.fset")>]
let tryGetSetter (o:PropertyObject) : obj option =
nativeOnly

let containsGetter (o:obj) : bool =
match tryGetPropertyValue o "get" with
match tryGetGetter o with
| Some _ -> true
| None -> false

let containsSetter (o:obj) : bool =
match tryGetPropertyValue o "set" with
match tryGetSetter o with
| Some _ -> true
| None -> false

let isWritable (o:obj) : bool =
match tryGetIsWritable o with
| Some v -> v
| None -> containsSetter o
containsSetter o

[<Emit("typeof $0 === 'function'")>]
let valueIsFunction (o:obj) : bool =
[<Emit("isinstance($0, property)")>]
let isProperty (o:obj) : bool =
nativeOnly

let isFunction (o:obj) : bool =
match tryGetPropertyValue o "value" with
| Some v -> valueIsFunction v
| None -> false

[<Emit("Object.getOwnPropertyNames($0)")>]
let getOwnPropertyNames (o:obj) : string [] =
[<Emit("vars($0)")>]
let getOwnMemberObjects (o:obj) : Dictionary<string,obj> =
nativeOnly

[<Emit("Object.getPrototypeOf($0)")>]
let getPrototype (o:obj) : obj =
[<Emit("$0.__class__")>]
let getClass (o:obj) : obj =
nativeOnly

let getStaticPropertyNames (o:obj) =
getPrototype o
let getOwnPropertyObjects (o:obj) : Dictionary<string,obj> =
getOwnMemberObjects o



let getStaticPropertyObjects (o:obj) =
getClass o
|> getOwnPropertyNames
|> Array.filter (fun n -> n <> "constructor")

Expand Down
2 changes: 1 addition & 1 deletion tests/DynamicObject.Tests/Inheritance.fs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ let tests_set = testList "Set" [
Expect.equal p1 p2 "Values should be equal"
Expect.equal (p1.GetHashCode()) (p2.GetHashCode()) "Hash codes should be equal"

ptestCase "Dynamic Property Only on one" <| fun _ ->
testCase "Dynamic Property Only on one" <| fun _ ->
let p1 = Person("John")
let p2 = Person("John")

Expand Down

0 comments on commit 897c049

Please sign in to comment.