<commit_before>;; Kahua plugin manager
;;
;;  Copyright (c) 2003 Scheme Arts, L.L.C., All rights reserved.
;;  Copyright (c) 2003 Time Intermedia Corporation, All rights reserved.
;;  See COPYING for terms and conditions of using this software
;;
;; $Id: plugin.scm,v 1.4 2005/06/26 12:27:40 tahara Exp $

(define-module kahua.plugin
  (use srfi-1)
  (use srfi-13)
  (use file.util)
  (use kahua.config)
  (export define-export <kahua-plugin> lookup-exports
          expand-define %load-plugin use-plugin
          define-plugin allow-module register-plugin
          initialize-plugins refresh-plugin
          all-plugins)
  )

(select-module kahua.plugin)

;; plugin container
(define *plugins* (make-hash-table 'string=?))

;; make anonymous module for binding plugin procedues.
(define (make-sandbox-module)
  (let ((m (make-module #f)))
    (eval '(import kahua.plugin) m)
    m))

(define (get-sandbox-module name)
  (if (hash-table-exists? *sandbox-plugin* name)
      (hash-table-get *sandbox-plugin* name)
      (let ((m (make-sandbox-module)))
        (hash-table-put! *sandbox-plugin* name m)
        m)))

;; plugin's procedues are bound this module.
(define *sandbox-plugin* (make-hash-table 'eq?))

;; plugin class.
(define-class <kahua-plugin> ()
  (  
   (name :init-keyword :name)
   (version :init-keyword :version)
   (depend :init-keyword :depend :init-value '())
   (export :init-keyword :export :init-value '())
   ))

;; explicitly. define a procedue globally in somewhere,
;; normally in *sandbox-plugin*.
(define-macro (define-export def . body)
  (if (pair? def)
    `(define-export ,(car def) (lambda ,(cdr def) ,@body))
    `(begin
       (define ,def ,@body)
       )))

;; find out which symbols a plugin defines.
(define (lookup-exports name)
  (let ((symbols (ref (hash-table-get *plugins*
                                      (symbol->string name)) 'export))
        (modules (cons (get-sandbox-module name)
                       (append (filter (lambda (m) (eq? (module-name m) name))
                                       (all-modules))
                               (hash-table-values *sandbox-plugin*)
                               (all-modules)))))
    (map (lambda (s)
           (cons s
                 (or (find (lambda (m)
                             (hash-table-exists? (module-table m) s))
                           modules)
                     (error "symbol not found." name s))))
         symbols)))

;; find symbol then define in sandbox plugin module.
(define-macro (expand-define name module)
  (let ((exports (lookup-exports name)))
    `(begin
       ,@(map (lambda (e)
                `(define-in-module ,module
                   ,(car e)
                   (eval ',(car e) ,(cdr e))))
              exports)
       )))

;; safe plugin loader for sandbox.
(define-syntax use-plugin
  (syntax-rules ()
    ((use-plugin name)
     (if (hash-table-exists? *plugins* (symbol->string 'name))
         (%load-plugin 'name (current-module))
         (error "cannot find plugin" 'name)))))<commit_msg>Deal with plugins module which written and export by export-all operator. This fix keeps normal working area, this means all pass for make check on Kahua.<commit_after>;; Kahua plugin manager
;;
;;  Copyright (c) 2003 Scheme Arts, L.L.C., All rights reserved.
;;  Copyright (c) 2003 Time Intermedia Corporation, All rights reserved.
;;  See COPYING for terms and conditions of using this software
;;
;; $Id: plugin.scm,v 1.5 2005/08/17 06:02:34 cut-sea Exp $

(define-module kahua.plugin
  (use srfi-1)
  (use srfi-13)
  (use file.util)
  (use kahua.config)
  (export define-export <kahua-plugin> lookup-exports
          expand-define %load-plugin use-plugin
          define-plugin allow-module register-plugin
          initialize-plugins refresh-plugin
          all-plugins)
  )

(select-module kahua.plugin)

;; plugin container
(define *plugins* (make-hash-table 'string=?))

;; make anonymous module for binding plugin procedues.
(define (make-sandbox-module)
  (let ((m (make-module #f)))
    (eval '(import kahua.plugin) m)
    m))

(define (get-sandbox-module name)
  (if (hash-table-exists? *sandbox-plugin* name)
      (hash-table-get *sandbox-plugin* name)
      (let ((m (make-sandbox-module)))
        (hash-table-put! *sandbox-plugin* name m)
        m)))

;; plugin's procedues are bound this module.
(define *sandbox-plugin* (make-hash-table 'eq?))

;; plugin class.
(define-class <kahua-plugin> ()
  (  
   (name :init-keyword :name)
   (version :init-keyword :version)
   (depend :init-keyword :depend :init-value '())
   (export :init-keyword :export :init-value '())
   ))

;; explicitly. define a procedue globally in somewhere,
;; normally in *sandbox-plugin*.
(define-macro (define-export def . body)
  (if (pair? def)
    `(define-export ,(car def) (lambda ,(cdr def) ,@body))
    `(begin
       (define ,def ,@body)
       )))

;; get symbols which exported from module,
;; this procedure treats the case of 'export-all'd module.
(define (%get-export-symbols name)
  (let1 m (filter (lambda (m)
		    (eq? (module-name m) name))
		  (all-modules))
    (if (null? m)
	'()
	(hash-table-map
	 (module-table (car m))
	 (lambda (k v) k)))))

;; find out which symbols a plugin defines.
(define (lookup-exports name)
  (let ((symbols (ref (hash-table-get *plugins*
                                      (symbol->string name)) 'export))
        (modules (cons (get-sandbox-module name)
                       (append (filter (lambda (m) (eq? (module-name m) name))
                                       (all-modules))
                               (hash-table-values *sandbox-plugin*)
                               (all-modules)))))
    (map (lambda (s)
           (cons s
                 (or (find (lambda (m)
                             (hash-table-exists? (module-table m) s))
                           modules)
                     (error "symbol not found." name s))))
         (if (eq? symbols #t)
	     (%get-export-symbols name)
	     symbols))))

;; find symbol then define in sandbox plugin module.
(define-macro (expand-define name module)
  (let ((exports (lookup-exports name)))
    `(begin
       ,@(map (lambda (e)
                `(define-in-module ,module
                   ,(car e)
                   (eval ',(car e) ,(cdr e))))
              exports)
       )))

;; safe plugin loader for sandbox.
(define-syntax use-plugin
  (syntax-rules ()
    ((use-plugin name)
     (if (hash-table-exists? *plugins* (symbol->string 'name))
         (%load-plugin 'name (current-module))
         (error "cannot find plugin" 'name)))))
{'fraction_non_alphanumeric': 0.14302957151478576, 'fraction_numerical': 0.008298129149064574, 'mean_word_length': 2.2671266633809757, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4292696', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2024, 'n_words': 610}